aboutsummaryrefslogtreecommitdiff
path: root/src/tool/polygon_room_tool.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tool/polygon_room_tool.rs')
-rw-r--r--src/tool/polygon_room_tool.rs167
1 files changed, 45 insertions, 122 deletions
diff --git a/src/tool/polygon_room_tool.rs b/src/tool/polygon_room_tool.rs
index 8cd2c25..1b079d2 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 crate::colours::DEFAULT_COLOURS;
use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle};
-use raylib::ffi::Color;
-use raylib::RaylibHandle;
struct UnfinishedPolygon {
pub corners: Vec<Vec2<f64>>,
@@ -16,9 +11,7 @@ struct UnfinishedPolygon {
}
pub struct PolygonRoomTool {
- keybindings: PolygonRoomToolKeybindings,
unfinished_polygon: Option<UnfinishedPolygon>,
- 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<f64>) {
// 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<f64>; 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 {
@@ -192,12 +103,7 @@ impl Tool for PolygonRoomTool {
transform.point_m_to_px(&polygon.corners[0]),
transform.point_m_to_px(&polygon.working_corner),
transform.length_m_to_px(0.1) as f32,
- Color {
- r: 150,
- g: 200,
- b: 150,
- a: 255,
- },
+ DEFAULT_COLOURS.room_selected
);
} else if polygon.corners.len() == 2 {
// We have three valid corners, so we can draw a triangle.
@@ -205,12 +111,7 @@ impl Tool for PolygonRoomTool {
transform.point_m_to_px(&polygon.corners[0]),
transform.point_m_to_px(&polygon.corners[1]),
transform.point_m_to_px(&polygon.working_corner),
- Color {
- r: 150,
- g: 200,
- b: 150,
- a: 255,
- },
+ DEFAULT_COLOURS.room_selected
)
} else {
// A proper polygon can be drawn.
@@ -225,12 +126,7 @@ impl Tool for PolygonRoomTool {
transform.point_m_to_px(&triangle[0]),
transform.point_m_to_px(&triangle[1]),
transform.point_m_to_px(&triangle[2]),
- Color {
- r: 150,
- g: 200,
- b: 150,
- a: 255,
- },
+ DEFAULT_COLOURS.room_selected
)
}
} else if polygon.check_validity_completed().is_ok() {
@@ -242,22 +138,49 @@ impl Tool for PolygonRoomTool {
transform.point_m_to_px(&triangle[0]),
transform.point_m_to_px(&triangle[1]),
transform.point_m_to_px(&triangle[2]),
- Color {
- r: 150,
- g: 200,
- b: 150,
- a: 255,
- },
+ DEFAULT_COLOURS.room_selected
)
}
}
}
+ }
+ }
+
+ fn place_single(&mut self, map: &mut Map, mouse_pos_m: &Vec2<f64>) {
+ 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;
}
}