|
| 1 | +#include "selection_window.hpp" |
| 2 | + |
| 3 | +#include "coordinates.hpp" |
| 4 | +#include "graphics/aspect_ratio/aspect_ratio.hpp" |
| 5 | + |
| 6 | +namespace fractal { |
| 7 | + |
| 8 | +void SelectionWindow::on_mouse_moved(const sf::Event::MouseMoveEvent& event) |
| 9 | +{ |
| 10 | + current_mouse_x_ = static_cast<float>(event.x); |
| 11 | + current_mouse_y_ = static_cast<float>(event.y); |
| 12 | +} |
| 13 | + |
| 14 | +void SelectionWindow::on_mouse_button_pressed(const sf::Event::MouseButtonEvent& event) |
| 15 | +{ |
| 16 | + if (event.button != sf::Mouse::Left) { |
| 17 | + return; |
| 18 | + } |
| 19 | + selection_start_x_ = static_cast<float>(event.x); |
| 20 | + selection_start_y_ = static_cast<float>(event.y); |
| 21 | + left_mouse_down_ = true; |
| 22 | +} |
| 23 | + |
| 24 | +void SelectionWindow::on_mouse_button_released(const sf::Event::MouseButtonEvent& event) |
| 25 | +{ |
| 26 | + if (event.button != sf::Mouse::Left) { |
| 27 | + return; |
| 28 | + } |
| 29 | + left_mouse_down_ = false; |
| 30 | +} |
| 31 | + |
| 32 | +std::optional<std::unique_ptr<sf::Drawable>> SelectionWindow::get_drawable() |
| 33 | +{ |
| 34 | + if (!left_mouse_down_) |
| 35 | + return std::nullopt; |
| 36 | + display_coordinate end_point = calculate_rectangle_end_point( |
| 37 | + {selection_start_x_, selection_start_y_}, {current_mouse_x_, current_mouse_y_} |
| 38 | + ); |
| 39 | + |
| 40 | + auto rectangle = std::make_unique<sf::RectangleShape>(sf::Vector2f{ |
| 41 | + static_cast<float>(end_point.first) - selection_start_x_, |
| 42 | + static_cast<float>(end_point.second) - selection_start_y_ |
| 43 | + }); |
| 44 | + rectangle->setPosition(selection_start_x_, selection_start_y_); |
| 45 | + rectangle->setFillColor(sf::Color(255, 0, 0, 127)); |
| 46 | + return rectangle; |
| 47 | +} |
| 48 | +} // namespace fractal |
0 commit comments