use super::Tool; use crate::map_data::MapData; use crate::math::{self, Vec2}; use crate::transform::Transform; use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle}; use raylib::ffi::{Color, MouseButton, Vector2}; use raylib::RaylibHandle; pub struct WallTool { unfinished_wall: Option<(Vec2, Vec2)>, } impl WallTool { pub fn new() -> Self { Self { unfinished_wall: None, } } } impl Tool for WallTool { fn active_update(&mut self, map_data: &mut MapData, rl: &RaylibHandle, transform: &Transform) { let mouse_pos_m = transform.point_px_to_m(rl.get_mouse_position().into()); if let Some((_, ref mut pos2)) = &mut self.unfinished_wall { let snapped_mouse_pos = Vec2::new( math::round(mouse_pos_m.x, 0.5), math::round(mouse_pos_m.y, 0.5), ); *pos2 = snapped_mouse_pos; } if rl.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) { if let Some((pos1, pos2)) = self.unfinished_wall { map_data.walls_mut().push((pos1, pos2)); self.unfinished_wall = None; } else { let snapped_mouse_pos = Vec2::new( math::round(mouse_pos_m.x, 0.5), math::round(mouse_pos_m.y, 0.5), ); self.unfinished_wall = Some((snapped_mouse_pos, snapped_mouse_pos)) } } if rl.is_mouse_button_pressed(MouseButton::MOUSE_RIGHT_BUTTON) { self.unfinished_wall = None; } } fn draw(&self, map_data: &MapData, rld: &mut RaylibDrawHandle, transform: &Transform) { for &(pos1, pos2) in map_data.walls() { let pos1: Vector2 = transform.point_m_to_px(pos1).into(); let pos2: Vector2 = transform.point_m_to_px(pos2).into(); rld.draw_line_ex( pos1, pos2, transform.length_m_to_px(0.1), 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(); rld.draw_line_ex( pos1, pos2, transform.length_m_to_px(0.1), Color { r: 150, g: 200, b: 150, a: 255, }, ); } } }