diff options
Diffstat (limited to 'src/tool/polygon_room_tool.rs')
| -rw-r--r-- | src/tool/polygon_room_tool.rs | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/tool/polygon_room_tool.rs b/src/tool/polygon_room_tool.rs new file mode 100644 index 0000000..4aab7f7 --- /dev/null +++ b/src/tool/polygon_room_tool.rs @@ -0,0 +1,91 @@ +use super::Tool; +use crate::button::Button; +use crate::config::{PolygonRoomToolKeybindings, ToolKeybindings}; +use crate::dimension_indicator::DimensionIndicator; +use crate::grid::{snap_to_grid, SNAP_SIZE}; +use crate::map_data::MapData; +use crate::math::{Polygon, Vec2}; +use crate::transform::Transform; +use raylib::core::drawing::RaylibDrawHandle; +use raylib::RaylibHandle; + +pub struct PolygonRoomTool { + keybindings: PolygonRoomToolKeybindings, + unfinished_polygon: Option<Vec<Vec2<f32>>>, + dimension_indicator: DimensionIndicator, +} + +impl PolygonRoomTool { + pub fn new(keybindings: PolygonRoomToolKeybindings) -> Self { + Self { + keybindings, + unfinished_polygon: None, + dimension_indicator: DimensionIndicator::new(), + } + } +} + +impl Tool for PolygonRoomTool { + fn activate(&mut self) {} + + fn deactivate(&mut self) { + self.unfinished_polygon = None; + } + + fn active_update( + &mut self, + map: &mut MapData, + rl: &RaylibHandle, + transform: &Transform, + mouse_blocked: bool, + ) { + let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position().into()); + let snapped_mouse_pos_m = snap_to_grid(mouse_pos_m, SNAP_SIZE); + // Update the position of the node that would be placed into the polygon next. + if let Some(ref mut corners) = &mut self.unfinished_polygon { + let last_element = corners.len() - 1; + corners[last_element] = snapped_mouse_pos_m; + self.dimension_indicator.update_dimensions(&corners); + } + + if self.keybindings.finish.is_pressed(rl, mouse_blocked) + && self.unfinished_polygon.is_some() + { + // Make sure the polygon is at least a triangle, so it can be drawn. + if self.unfinished_polygon.as_ref().unwrap().len() >= 3 { + let polygon = Polygon::new(self.unfinished_polygon.take().unwrap()); + self.dimension_indicator.clear_dimensions(); + map.polygons_mut().push(polygon); + } + } + + if self.keybindings.place_node.is_pressed(rl, mouse_blocked) { + if let Some(ref mut corners) = self.unfinished_polygon.as_mut() { + if snapped_mouse_pos_m == corners[0] { + // Make sure the polygon is at least a triangle, so it can be drawn. + if corners.len() >= 3 { + // The last corner is redundant. + corners.pop(); + let polygon = Polygon::new(self.unfinished_polygon.take().unwrap()); + self.dimension_indicator.clear_dimensions(); + map.polygons_mut().push(polygon); + } + } else { + corners.push(snapped_mouse_pos_m); + } + } else { + self.unfinished_polygon = Some(vec![snapped_mouse_pos_m]); + } + } + + if self.keybindings.abort.is_pressed(rl, false) { + self.unfinished_polygon = None; + } + } + + fn draw(&self, _map: &MapData, _rld: &mut RaylibDrawHandle, _transform: &Transform) {} + + fn activation_key(&self) -> Button { + self.keybindings.activation_key() + } +} |
