11#include " basic_display.hpp"
22
33#include " coordinates.hpp"
4- #include " graphics/color_conversions .hpp"
4+ #include " ends .hpp"
55
66#include < fmt/format.h>
77
88#include < cmath>
99
1010namespace fractal {
11- void BasicDisplay::set_pixel (display_coordinate coordinate, uint16_t value)
12- {
13- auto tuple = number_to_rgb (value);
14-
15- image_.setPixel (
16- static_cast <unsigned int >(coordinate.first ),
17- static_cast <unsigned int >(coordinate.second ),
18- sf::Color (std::get<0 >(tuple), std::get<1 >(tuple), std::get<2 >(tuple))
19- );
20- }
21-
22- display_coordinate calculate_end_points (
23- display_coordinate start, display_coordinate current,
24- float target_aspect_ratio = 800 .0f / 600 .0f
25- )
26- {
27- auto width = static_cast <float >(std::abs (current.first - start.first ));
28- auto height = static_cast <float >(std::abs (current.second - start.second ));
29-
30- // Adjust the dimensions to maintain the target aspect ratio
31- if (width / height > target_aspect_ratio) {
32- // Too wide, adjust width
33- width = height * target_aspect_ratio;
34- }
35- else {
36- // Too tall, adjust height
37- height = width / target_aspect_ratio;
38- }
39-
40- auto x = static_cast <float >(std::min (current.first , start.first ));
41- auto y = static_cast <float >(std::min (current.second , start.second ));
42-
43- // Adjust the top-left corner based on new dimensions
44- if (current.first < start.first ) {
45- x = static_cast <float >(start.first ) - width;
46- }
47- if (current.second < start.second ) {
48- y = static_cast <float >(start.second ) - height;
49- }
50-
51- // Return the top-left and bottom-right corners as a pair of sf::Vector2f
52- return {x + width, y + height};
53- }
5411
5512void BasicDisplay::display_window ()
5613{
57- texture_.loadFromImage (image_);
58- sf::Sprite sprite{texture_};
5914 bool left_mouse_down{};
6015 float selection_start_x{};
6116 float selection_start_y{};
@@ -81,6 +36,12 @@ void BasicDisplay::display_window()
8136 left_mouse_down = true ;
8237 selection_start_x = static_cast <float >(event.mouseButton .x );
8338 selection_start_y = static_cast <float >(event.mouseButton .y );
39+ std::for_each (
40+ observers_.begin (), observers_.end (),
41+ [&](const auto & observer) {
42+ observer->on_mouse_button_pressed (event.mouseButton );
43+ }
44+ );
8445 break ;
8546 default :
8647 break ;
@@ -89,24 +50,26 @@ void BasicDisplay::display_window()
8950 if (event.mouseButton .button != sf::Mouse::Left) {
9051 break ;
9152 }
92- auto ends = calculate_end_points (
93- {selection_start_x, selection_start_y}, {mouse_x, mouse_y}
94- );
95- on_resize_ (
96- sf::Vector2f (
97- std::min (mouse_x, selection_start_x),
98- std::min (mouse_y, selection_start_y)
99- ),
100- sf::Vector2f (ends.first , ends.second )
53+ std::for_each (
54+ observers_.begin (), observers_.end (),
55+ [&](const auto & observer) {
56+ observer->on_mouse_button_released (event.mouseButton );
57+ }
10158 );
10259 left_mouse_down = false ;
103- return ;
60+ break ;
10461 }
10562 }
10663
10764 window_.clear (sf::Color::Black);
10865
109- window_.draw (sprite);
66+ for (const auto & observer : observers_) {
67+ auto opt = observer->get_drawable ();
68+ if (opt) {
69+ window_.draw (opt.value ());
70+ }
71+ }
72+
11073 if (left_mouse_down) {
11174 display_coordinate end_point = calculate_end_points (
11275 {selection_start_x, selection_start_y}, {mouse_x, mouse_y}
0 commit comments