diff options
| author | Arne Dußin | 2020-11-24 13:16:47 +0100 |
|---|---|---|
| committer | Arne Dußin | 2020-11-24 13:16:47 +0100 |
| commit | 58ca28d4b21667e9b86939ad574f477e02f2b290 (patch) | |
| tree | 8b836c9c635d6d08f641cd5c56e2242f426a369a | |
| parent | e3f5f944ed90fa7fb96ad2b670ea34c0765df1ad (diff) | |
| download | graf_karto-58ca28d4b21667e9b86939ad574f477e02f2b290.tar.gz graf_karto-58ca28d4b21667e9b86939ad574f477e02f2b290.zip | |
Add unstable polygon room tool
| -rw-r--r-- | src/editor.rs | 8 | ||||
| -rw-r--r-- | src/math/polygon/mod.rs | 2 | ||||
| -rw-r--r-- | src/tool/polygon_room_tool.rs | 67 |
3 files changed, 69 insertions, 8 deletions
diff --git a/src/editor.rs b/src/editor.rs index 2bb5328..b2260f1 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -18,11 +18,13 @@ impl Editor { 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(config.room_keybindings))); - assert_eq!(ToolType::WallTool as u8, 1); + assert_eq!(ToolType::PolygonRoomTool as u8, 1); + tools.push(Box::new(PolygonRoomTool::new(config.polygon_keybindings))); + assert_eq!(ToolType::WallTool as u8, 2); tools.push(Box::new(WallTool::new(config.wall_keybindings))); - assert_eq!(ToolType::IconTool as u8, 2); + assert_eq!(ToolType::IconTool as u8, 3); tools.push(Box::new(IconTool::new(rl, rlt, config.icon_keybindings))); - assert_eq!(ToolType::DeletionTool as u8, 3); + assert_eq!(ToolType::DeletionTool as u8, 4); tools.push(Box::new(DeletionTool::new(config.deletion_keybindings))); assert_eq!(ToolType::NumTools as usize, tools.len()); diff --git a/src/math/polygon/mod.rs b/src/math/polygon/mod.rs index f84211d..90d46bb 100644 --- a/src/math/polygon/mod.rs +++ b/src/math/polygon/mod.rs @@ -13,7 +13,7 @@ use num_traits::Zero; use serde::{Deserialize, Serialize}; use std::ops::Neg; -#[derive(Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] // TODO: Support polygons with holes pub struct Polygon<T: Scalar + Copy> { corners: Vec<Vec2<T>>, diff --git a/src/tool/polygon_room_tool.rs b/src/tool/polygon_room_tool.rs index 4aab7f7..b37774b 100644 --- a/src/tool/polygon_room_tool.rs +++ b/src/tool/polygon_room_tool.rs @@ -4,9 +4,10 @@ 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::math::{self, Polygon, Vec2}; use crate::transform::Transform; -use raylib::core::drawing::RaylibDrawHandle; +use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle}; +use raylib::ffi::Color; use raylib::RaylibHandle; pub struct PolygonRoomTool { @@ -74,7 +75,7 @@ impl Tool for PolygonRoomTool { corners.push(snapped_mouse_pos_m); } } else { - self.unfinished_polygon = Some(vec![snapped_mouse_pos_m]); + self.unfinished_polygon = Some(vec![snapped_mouse_pos_m, snapped_mouse_pos_m]); } } @@ -83,7 +84,65 @@ impl Tool for PolygonRoomTool { } } - fn draw(&self, _map: &MapData, _rld: &mut RaylibDrawHandle, _transform: &Transform) {} + 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<f32>; 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, + }, + ) + } + } + + // Draw the current polygon + if let Some(corners) = &self.unfinished_polygon { + let mut corners = corners.clone(); + corners.dedup(); + match corners.len() { + 0 | 1 => {} + 2 => rld.draw_line_ex( + transform.point_m_to_px(corners[0]), + transform.point_m_to_px(corners[1]), + transform.length_m_to_px(0.1), + Color { + r: 150, + g: 200, + b: 150, + a: 255, + }, + ), + _ => { + let polygon = Polygon::new(corners); + let triangles = math::triangulate(polygon); + for triangle in triangles { + let triangle: [Vec2<f32>; 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: 150, + g: 200, + b: 150, + a: 255, + }, + ) + } + } + } + self.dimension_indicator.draw(rld, transform); + } + } fn activation_key(&self) -> Button { self.keybindings.activation_key() |
