diff options
Diffstat (limited to 'src/tool')
| -rw-r--r-- | src/tool/deletion_tool.rs | 7 | ||||
| -rw-r--r-- | src/tool/mod.rs | 52 | ||||
| -rw-r--r-- | src/tool/room_tool.rs | 25 | ||||
| -rw-r--r-- | src/tool/wall_tool.rs | 36 |
4 files changed, 38 insertions, 82 deletions
diff --git a/src/tool/deletion_tool.rs b/src/tool/deletion_tool.rs index 4d255d9..3eca92a 100644 --- a/src/tool/deletion_tool.rs +++ b/src/tool/deletion_tool.rs @@ -1,4 +1,5 @@ use super::Tool; +use crate::map_data::MapData; use crate::transform::Transform; use raylib::core::drawing::RaylibDrawHandle; use raylib::RaylibHandle; @@ -12,8 +13,8 @@ impl DeletionTool { } impl Tool for DeletionTool { - fn update(&mut self, rl: &RaylibHandle, transform: &Transform) {} - fn active_update(&mut self, rl: &RaylibHandle, transform: &Transform) {} + fn update(&mut self, map_data: &MapData, rl: &RaylibHandle, transform: &Transform) {} + fn active_update(&mut self, map_data: &mut MapData, rl: &RaylibHandle, transform: &Transform) {} - fn draw(&self, _rld: &mut RaylibDrawHandle, _transform: &Transform) {} + fn draw(&self, map_data: &MapData, _rld: &mut RaylibDrawHandle, _transform: &Transform) {} } diff --git a/src/tool/mod.rs b/src/tool/mod.rs index da95c61..73654d9 100644 --- a/src/tool/mod.rs +++ b/src/tool/mod.rs @@ -6,9 +6,9 @@ pub use deletion_tool::DeletionTool; pub use room_tool::RoomTool; pub use wall_tool::WallTool; +use crate::map_data::MapData; use crate::transform::Transform; use raylib::core::drawing::RaylibDrawHandle; -use raylib::ffi::KeyboardKey; use raylib::RaylibHandle; #[derive(Debug)] @@ -21,52 +21,8 @@ pub enum ToolType { } pub trait Tool { - fn update(&mut self, rl: &RaylibHandle, transform: &Transform) {} - fn active_update(&mut self, rl: &RaylibHandle, transform: &Transform); + fn update(&mut self, _map: &MapData, _rl: &RaylibHandle, _transform: &Transform) {} + fn active_update(&mut self, map: &mut MapData, rl: &RaylibHandle, transform: &Transform); - fn draw(&self, _rld: &mut RaylibDrawHandle, _transform: &Transform) {} -} - -pub struct ToolShed { - tools: Vec<Box<dyn Tool>>, - active: usize, -} - -impl ToolShed { - pub fn new() -> Self { - let mut tools: Vec<Box<dyn Tool>> = Vec::with_capacity(ToolType::NumTools as usize); - assert_eq!(ToolType::RoomTool as u8, 0); - tools.push(Box::new(RoomTool::new())); - assert_eq!(ToolType::WallTool as u8, 1); - tools.push(Box::new(WallTool::new())); - assert_eq!(ToolType::DeletionTool as u8, 2); - tools.push(Box::new(DeletionTool::new())); - - assert_eq!(ToolType::NumTools as usize, tools.len()); - - Self { tools, active: 0 } - } - - pub fn update(&mut self, rl: &RaylibHandle, transform: &Transform) { - // Handle keybindings for tool change - self.active = if rl.is_key_pressed(KeyboardKey::KEY_R) { - ToolType::RoomTool as usize - } else if rl.is_key_pressed(KeyboardKey::KEY_W) { - ToolType::WallTool as usize - } else { - self.active - }; - - for tool in &mut self.tools { - tool.update(rl, transform); - } - - self.tools[self.active].active_update(rl, transform); - } - - pub fn draw_tools(&self, rld: &mut RaylibDrawHandle, transform: &Transform) { - for tool in &self.tools { - tool.draw(rld, transform); - } - } + fn draw(&self, _map: &MapData, _rld: &mut RaylibDrawHandle, _transform: &Transform) {} } diff --git a/src/tool/room_tool.rs b/src/tool/room_tool.rs index 10b9edf..0321ff3 100644 --- a/src/tool/room_tool.rs +++ b/src/tool/room_tool.rs @@ -1,35 +1,32 @@ use super::Tool; -use crate::math; +use crate::map_data::MapData; +use crate::math::{self, Rect, Vec2}; use crate::transform::Transform; use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle}; 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<Rectangle>, /// 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)>, + unfinished_rect: Option<(Vec2<f32>, Vec2<f32>)>, } 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 active_update(&mut self, rl: &RaylibHandle, transform: &Transform) { - let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position()); + fn active_update(&mut self, map_data: &mut MapData, rl: &RaylibHandle, transform: &Transform) { + let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position().into()); // Update the currently drawn rectangle, if it exists if let Some((_, ref mut pos2)) = &mut self.unfinished_rect { - let snapped_mouse_pos = Vector2::new( + let snapped_mouse_pos = Vec2::new( math::round(mouse_pos_m.x, 0.5), math::round(mouse_pos_m.y, 0.5), ); @@ -39,10 +36,10 @@ impl Tool for RoomTool { // 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)); + map_data.rooms_mut().push(Rect::bounding_rect(pos1, pos2)); self.unfinished_rect = None; } else { - let snapped_mouse_pos = Vector2::new( + let snapped_mouse_pos = Vec2::new( math::round(mouse_pos_m.x, 0.5), math::round(mouse_pos_m.y, 0.5), ); @@ -56,9 +53,9 @@ impl Tool for RoomTool { } } - fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) { + fn draw(&self, map_data: &MapData, rld: &mut RaylibDrawHandle, transform: &Transform) { // Draw all finished rectangles. - for &rect in &self.room_rects { + for &rect in map_data.rooms() { rld.draw_rectangle_rec( transform.rect_m_to_px(rect), Color { @@ -73,7 +70,7 @@ impl Tool for RoomTool { // 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)), + transform.rect_m_to_px(Rect::bounding_rect(pos1, pos2)), Color { r: 150, g: 200, diff --git a/src/tool/wall_tool.rs b/src/tool/wall_tool.rs index 21eb895..a1d3c15 100644 --- a/src/tool/wall_tool.rs +++ b/src/tool/wall_tool.rs @@ -1,30 +1,28 @@ use super::Tool; -use crate::math; +use crate::map_data::MapData; +use crate::math::{self, Vec2}; use crate::transform::Transform; use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle}; -use raylib::ffi::{Color, MouseButton}; -use raylib::math::Vector2; +use raylib::ffi::{Color, MouseButton, Vector2}; use raylib::RaylibHandle; pub struct WallTool { - walls: Vec<(Vector2, Vector2)>, - unfinished_wall: Option<(Vector2, Vector2)>, + unfinished_wall: Option<(Vec2<f32>, Vec2<f32>)>, } impl WallTool { pub fn new() -> Self { Self { - walls: Vec::new(), unfinished_wall: None, } } } impl Tool for WallTool { - fn active_update(&mut self, rl: &RaylibHandle, transform: &Transform) { - let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position()); + fn active_update(&mut self, map_data: &mut MapData, rl: &RaylibHandle, transform: &Transform) { + let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position().into()); if let Some((_, ref mut pos2)) = &mut self.unfinished_wall { - let snapped_mouse_pos = Vector2::new( + let snapped_mouse_pos = Vec2::new( math::round(mouse_pos_m.x, 0.5), math::round(mouse_pos_m.y, 0.5), ); @@ -33,10 +31,10 @@ impl Tool for WallTool { if rl.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) { if let Some((pos1, pos2)) = self.unfinished_wall { - self.walls.push((pos1, pos2)); + map_data.walls_mut().push((pos1, pos2)); self.unfinished_wall = None; } else { - let snapped_mouse_pos = Vector2::new( + let snapped_mouse_pos = Vec2::new( math::round(mouse_pos_m.x, 0.5), math::round(mouse_pos_m.y, 0.5), ); @@ -49,11 +47,13 @@ impl Tool for WallTool { } } - fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) { - for &(pos1, pos2) in &self.walls { + fn draw(&self, map_data: &MapData, rld: &mut RaylibDrawHandle, transform: &Transform) { + for &(pos1, pos2) in map_data.walls() { + let pos1: Vector2 = transform.point_m_to_px(pos1).into(); + let pos2: Vector2 = transform.point_m_to_px(pos2).into(); rld.draw_line_ex( - transform.point_m_to_px(pos1), - transform.point_m_to_px(pos2), + pos1, + pos2, 5., Color { r: 200, @@ -65,9 +65,11 @@ impl Tool for WallTool { } if let Some((pos1, pos2)) = self.unfinished_wall { + let pos1: Vector2 = transform.point_m_to_px(pos1).into(); + let pos2: Vector2 = transform.point_m_to_px(pos2).into(); rld.draw_line_ex( - transform.point_m_to_px(pos1), - transform.point_m_to_px(pos2), + pos1, + pos2, 5., Color { r: 150, |
