diff options
| author | Arne Dußin | 2020-12-21 21:12:01 +0100 |
|---|---|---|
| committer | GitHub | 2020-12-21 21:12:01 +0100 |
| commit | c435f278eddcada279fdc424120e4a1448843c20 (patch) | |
| tree | be9a5601e99608966d4ccd146c3bfb3a70c7fc02 /src/tool/rect_room_tool.rs | |
| parent | 3bc690803fb59493ea8180fd630d65b3e26642d0 (diff) | |
| parent | 82d11b7d3e15d8175accf7579db1fbe528fc6583 (diff) | |
| download | graf_karto-c435f278eddcada279fdc424120e4a1448843c20.tar.gz graf_karto-c435f278eddcada279fdc424120e4a1448843c20.zip | |
Merge pull request #24 from LordSentox/refactor
Refactor to make interaction between tools easier
Diffstat (limited to 'src/tool/rect_room_tool.rs')
| -rw-r--r-- | src/tool/rect_room_tool.rs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/tool/rect_room_tool.rs b/src/tool/rect_room_tool.rs new file mode 100644 index 0000000..dfda495 --- /dev/null +++ b/src/tool/rect_room_tool.rs @@ -0,0 +1,73 @@ +use super::Tool; +use crate::map::Map; +use crate::math::{Rect, Vec2}; +use crate::transform::Transform; +use crate::colours::DEFAULT_COLOURS; +use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle}; + +pub struct RectRoomTool { + /// 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<(Vec2<f64>, Vec2<f64>)>, +} + +impl RectRoomTool { + /// Create a new room tool where no rooms have been drawn yet. + pub fn new() -> Self { + Self { + unfinished_rect: None, + } + } +} + +impl Tool for RectRoomTool { + fn deactivate(&mut self) { + self.unfinished_rect = None; + } + + fn update(&mut self, _map: &Map, mouse_pos_m: &Vec2<f64>) { + if let Some((_, ref mut pos2)) = &mut self.unfinished_rect { + *pos2 = *mouse_pos_m; + } + } + + fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) { + if let Some((pos1, pos2)) = self.unfinished_rect { + rld.draw_rectangle_rec( + transform.rect_m_to_px(&Rect::bounding_rect(pos1, pos2)), + DEFAULT_COLOURS.room_selected + ); + } + } + + fn place_single(&mut self, map: &mut Map, mouse_pos_m: &Vec2<f64>) { + // Try to finish the rectangle if it has been started. + if let Some((pos1, pos2)) = self.unfinished_rect { + if pos1 == pos2 { + warn!("Cannot place rectangle with start and endpoint being the same"); + return; + } + + map.push_rect_room(Rect::bounding_rect(pos1, pos2)); + self.unfinished_rect = None; + } else { + self.unfinished_rect = Some((*mouse_pos_m, *mouse_pos_m)); + } + } + + fn finish(&mut self, map: &mut Map) { + if let Some((pos1, pos2)) = self.unfinished_rect { + if pos1 == pos2 { + warn!("Cannot place rectangle with start and endpoint being the same"); + return; + } + + map.push_rect_room(Rect::bounding_rect(pos1, pos2)); + self.unfinished_rect = None; + } + } + + fn abort(&mut self) { + self.unfinished_rect = None; + } +} |
