From 9799d3c6a8f0c242668203a1c70d7b6cfed3e855 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Tue, 15 Dec 2020 00:46:54 +0100 Subject: Refactor to make interaction between tools easier --- src/tool/polygon_room_tool.rs | 137 ++++++++++++------------------------------ 1 file changed, 40 insertions(+), 97 deletions(-) (limited to 'src/tool/polygon_room_tool.rs') diff --git a/src/tool/polygon_room_tool.rs b/src/tool/polygon_room_tool.rs index 8cd2c25..4f8edce 100644 --- a/src/tool/polygon_room_tool.rs +++ b/src/tool/polygon_room_tool.rs @@ -1,14 +1,9 @@ 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::map::Map; use crate::math::{self, Polygon, PolygonError, Vec2}; use crate::transform::Transform; use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle}; use raylib::ffi::Color; -use raylib::RaylibHandle; struct UnfinishedPolygon { pub corners: Vec>, @@ -16,9 +11,7 @@ struct UnfinishedPolygon { } pub struct PolygonRoomTool { - keybindings: PolygonRoomToolKeybindings, unfinished_polygon: Option, - dimension_indicator: DimensionIndicator, } impl UnfinishedPolygon { @@ -83,108 +76,26 @@ impl UnfinishedPolygon { } impl PolygonRoomTool { - pub fn new(keybindings: PolygonRoomToolKeybindings) -> Self { + pub fn new() -> 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); - + fn update(&mut self, _map: &Map, mouse_pos_m: &Vec2) { // Update the position of the node that would be placed into the polygon next. if let Some(ref mut polygon) = &mut self.unfinished_polygon { - polygon.working_corner = snapped_mouse_pos_m; - - polygon.corners.push(polygon.working_corner); - self.dimension_indicator.update_dimensions(&polygon.corners); - polygon.working_corner = polygon.corners.pop().unwrap(); - } - - /* Check if the finishing keybinding has been pressed. If so, try to turn the part of the - * polygon that is already completed into a proper polygon and push it into the map data. - */ - if self.keybindings.finish.is_pressed(rl, mouse_blocked) { - if let Some(ref mut polygon) = self.unfinished_polygon { - if let Some(polygon) = polygon.try_into_completed() { - self.dimension_indicator.clear_dimensions(); - map.polygons_mut().push(polygon); - self.unfinished_polygon = None; - } - } - } - - /* Handle placing a new corner of the polygon. If the corner is placed on the first node, - * the polygon will be created. - */ - if self.keybindings.place_node.is_pressed(rl, mouse_blocked) { - if let Some(ref mut polygon) = &mut self.unfinished_polygon { - if polygon.working_corner == polygon.corners[0] { - /* The working corner will be ignored, since it would double the vertex at the - * polygon starting position. - */ - if let Some(polygon) = polygon.try_into_completed() { - self.dimension_indicator.clear_dimensions(); - map.polygons_mut().push(polygon); - self.unfinished_polygon = None; - } - } else { - // Check if we can add the corner to the polygon without ruining it. - if let Err(e) = polygon.try_push_working() { - error!("Cannot add corner to polygon: {}", e); - } - } - } else { - // Start a new unfinished polygon - self.unfinished_polygon = Some(UnfinishedPolygon { - corners: vec![snapped_mouse_pos_m], - working_corner: snapped_mouse_pos_m, - }); - } - } - - if self.keybindings.abort.is_pressed(rl, false) { - self.unfinished_polygon = None; + polygon.working_corner = *mouse_pos_m; } } - fn draw(&self, map: &MapData, rld: &mut RaylibDrawHandle, transform: &Transform) { - // TODO: Buffer triangles so the polygons don't always have to be retriangulated. - for polygon in map.polygons() { - let triangles = math::triangulate(polygon.clone()); - for triangle in triangles { - let triangle: [Vec2; 3] = triangle.into(); - rld.draw_triangle( - transform.point_m_to_px(&triangle[0]), - transform.point_m_to_px(&triangle[1]), - transform.point_m_to_px(&triangle[2]), - Color { - r: 180, - g: 180, - b: 180, - a: 255, - }, - ) - } - } - + fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) { if let Some(polygon) = &self.unfinished_polygon { // The first corner is guaranteed to be set, so we can at least draw a line. if polygon.corners.len() == 1 { @@ -252,12 +163,44 @@ impl Tool for PolygonRoomTool { } } } + } + } + + fn place_single(&mut self, map: &mut Map, mouse_pos_m: &Vec2) { + if let Some(ref mut polygon) = &mut self.unfinished_polygon { + if polygon.working_corner == polygon.corners[0] { + /* The working corner will be ignored, since it would double the vertex at the + * polygon starting position. + */ + if let Some(polygon) = polygon.try_into_completed() { + map.push_polygon_room(polygon); + self.unfinished_polygon = None; + } + } else { + // Check if we can add the corner to the polygon without ruining it. + if let Err(e) = polygon.try_push_working() { + error!("Cannot add corner to polygon: {}", e); + } + } + } else { + // Start a new unfinished polygon + self.unfinished_polygon = Some(UnfinishedPolygon { + corners: vec![*mouse_pos_m], + working_corner: *mouse_pos_m, + }); + } + } - self.dimension_indicator.draw(rld, transform); + fn finish(&mut self, map: &mut Map) { + if let Some(ref mut polygon) = self.unfinished_polygon { + if let Some(polygon) = polygon.try_into_completed() { + map.push_polygon_room(polygon); + self.unfinished_polygon = None; + } } } - fn activation_key(&self) -> Button { - self.keybindings.activation_key() + fn abort(&mut self) { + self.unfinished_polygon = None; } } -- cgit v1.2.3-70-g09d2