diff options
Diffstat (limited to 'src/tool/wall_tool.rs')
| -rw-r--r-- | src/tool/wall_tool.rs | 113 |
1 files changed, 25 insertions, 88 deletions
diff --git a/src/tool/wall_tool.rs b/src/tool/wall_tool.rs index d86d0af..b958799 100644 --- a/src/tool/wall_tool.rs +++ b/src/tool/wall_tool.rs @@ -1,23 +1,17 @@ use super::Tool; -use crate::button::Button; -use crate::config::{ToolKeybindings, WallToolKeybindings}; -use crate::grid::{snap_to_grid, SNAP_SIZE}; -use crate::map_data::MapData; -use crate::math::Vec2; +use crate::map::Map; +use crate::math::{LineSegment, Vec2}; use crate::transform::Transform; use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle}; use raylib::ffi::{Color, Vector2}; -use raylib::RaylibHandle; pub struct WallTool { - keybindings: WallToolKeybindings, - unfinished_wall: Option<(Vec2<f64>, Vec2<f64>)>, + unfinished_wall: Option<LineSegment<f64>>, } impl WallTool { - pub fn new(keybindings: WallToolKeybindings) -> Self { + pub fn new() -> Self { Self { - keybindings, unfinished_wall: None, } } @@ -28,86 +22,19 @@ impl Tool for WallTool { self.unfinished_wall = None; } - fn active_update( - &mut self, - map_data: &mut MapData, - rl: &RaylibHandle, - transform: &Transform, - mouse_blocked: bool, - ) { - let mouse_pos_m = transform.point_px_to_m(&rl.get_mouse_position().into()); - if let Some((_, ref mut pos2)) = &mut self.unfinished_wall { - *pos2 = snap_to_grid(mouse_pos_m, SNAP_SIZE); - } - - if self - .keybindings - .finish_segment - .is_pressed(rl, mouse_blocked) - && self.unfinished_wall.is_some() - { - let (pos1, pos2) = self.unfinished_wall.unwrap(); - map_data.walls_mut().push((pos1, pos2)); - self.unfinished_wall = Some((pos2, pos2)); - } else if self.keybindings.start_wall.is_pressed(rl, mouse_blocked) { - let snapped_mouse_pos = snap_to_grid(mouse_pos_m, SNAP_SIZE); - self.unfinished_wall = Some((snapped_mouse_pos, snapped_mouse_pos)) - } - - if self.keybindings.abort_segment.is_pressed(rl, false) { - self.unfinished_wall = None; + fn update(&mut self, _map: &Map, mouse_pos_m: &Vec2<f64>) { + if let Some(ref mut wall) = &mut self.unfinished_wall { + wall.end = *mouse_pos_m; } } - fn draw(&self, map_data: &MapData, rld: &mut RaylibDrawHandle, transform: &Transform) { - for &(pos1, pos2) in map_data.walls() { - let pos1_px = transform.point_m_to_px(&pos1); - let pos2_px = transform.point_m_to_px(&pos2); - rld.draw_line_ex( - pos1_px, - pos2_px, - transform.length_m_to_px(0.1) as f32, - Color { - r: 200, - g: 120, - b: 120, - a: 255, - }, - ); - - /* Find walls that end/start at the start or end of this wall and draw part of a circle - * to join these two walls more nicely. - */ - for &(other1, other2) in map_data.walls() { - // Ignore the line segment if it's the same wall - if pos1 == other1 && pos2 == other2 { - continue; - } - - // TODO: Only draw segments when introducing transparency. - for pos in [pos1, pos2].iter() { - if *pos == other1 || *pos == other2 { - rld.draw_circle_v( - transform.point_m_to_px(&pos), - transform.length_m_to_px(0.05) as f32, - Color { - r: 200, - g: 120, - b: 120, - a: 255, - }, - ); - } - } - } - } - - if let Some((pos1, pos2)) = self.unfinished_wall { - let pos1: Vector2 = transform.point_m_to_px(&pos1).into(); - let pos2: Vector2 = transform.point_m_to_px(&pos2).into(); + fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) { + if let Some(ref wall) = self.unfinished_wall { + let start: Vector2 = transform.point_m_to_px(&wall.start).into(); + let end: Vector2 = transform.point_m_to_px(&wall.end).into(); rld.draw_line_ex( - pos1, - pos2, + start, + end, transform.length_m_to_px(0.1) as f32, Color { r: 150, @@ -119,7 +46,17 @@ impl Tool for WallTool { } } - fn activation_key(&self) -> Button { - self.keybindings.activation_key() + fn place_single(&mut self, map: &mut Map, mouse_pos_m: &Vec2<f64>) { + if let Some(wall) = self.unfinished_wall.take() { + // Continue with the next wall straight away. + self.unfinished_wall = Some(LineSegment::new(wall.end, wall.end)); + map.push_wall(wall); + } else { + self.unfinished_wall = Some(LineSegment::new(*mouse_pos_m, *mouse_pos_m)); + } + } + + fn abort(&mut self) { + self.unfinished_wall = None; } } |
