use super::Tool; use crate::math; use crate::transform::Transform; use raylib::core::drawing::RaylibDraw; use raylib::ffi::{Color, MouseButton}; use raylib::math::{Rectangle, Vector2}; use raylib::RaylibHandle; pub struct RoomTool { /// Vector of all Rectangles representing rooms that have already been drawn. room_rects: Vec, /// The rectangle that is currently being drawn by the user. Once it is finished, it will be /// pushed into the room_rects. unfinished_rect: Option<(Vector2, Vector2)>, } impl RoomTool { /// Create a new room tool where no rooms have been drawn yet. pub fn new() -> Self { Self { room_rects: Vec::new(), unfinished_rect: None, } } } impl Tool for RoomTool { fn update(&mut self, rl: &RaylibHandle, transform: &Transform) { let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position()); // Update the currently drawn rectangle, if it exists if let Some((_, ref mut pos2)) = &mut self.unfinished_rect { *pos2 = mouse_pos_m; } // Start or finish drawing the currently unfinished rectangle if rl.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) { if let Some((pos1, pos2)) = self.unfinished_rect { self.room_rects.push(math::bounding_rect(pos1, pos2)); self.unfinished_rect = None; } else { self.unfinished_rect = Some((mouse_pos_m, mouse_pos_m)) } } // Abort drawing the room (if any) in case the right mouse button was pressed. if rl.is_mouse_button_pressed(MouseButton::MOUSE_RIGHT_BUTTON) { self.unfinished_rect = None; } } fn draw(&self, rld: &mut D, transform: &Transform) where D: RaylibDraw, { // Draw all finished rectangles. for &rect in &self.room_rects { rld.draw_rectangle_rec( transform.rect_m_to_px(rect), Color { r: 180, g: 180, b: 180, a: 255, }, ); } // Do the same for the unfinished rectangle if let Some((pos1, pos2)) = self.unfinished_rect { rld.draw_rectangle_rec( transform.rect_m_to_px(math::bounding_rect(pos1, pos2)), Color { r: 150, g: 200, b: 150, a: 255, }, ); } } }