From b06e0075bf4dfd51f8ad5df801f9c43fbd73df1f Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Mon, 9 Nov 2020 21:15:35 +0100 Subject: Add configuration options --- src/tool/deletion_tool.rs | 29 ++++++++++++++++------------- src/tool/icon_tool.rs | 22 ++++++++++++++++------ src/tool/mod.rs | 3 +++ src/tool/room_tool.rs | 27 ++++++++++++++++----------- src/tool/wall_tool.rs | 27 +++++++++++++++++---------- 5 files changed, 68 insertions(+), 40 deletions(-) (limited to 'src/tool') diff --git a/src/tool/deletion_tool.rs b/src/tool/deletion_tool.rs index 142e0cb..6f5ac24 100644 --- a/src/tool/deletion_tool.rs +++ b/src/tool/deletion_tool.rs @@ -1,18 +1,22 @@ use super::Tool; +use crate::button::Button; +use crate::config::{DeletionToolKeybindings, ToolKeybindings}; use crate::map_data::MapData; use crate::math::{Rect, Vec2}; use crate::transform::Transform; use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle}; -use raylib::ffi::{Color, MouseButton}; +use raylib::ffi::Color; use raylib::RaylibHandle; pub struct DeletionTool { + keybindings: DeletionToolKeybindings, deletion_rect: Option<(Vec2, Vec2)>, } impl DeletionTool { - pub fn new() -> Self { + pub fn new(keybindings: DeletionToolKeybindings) -> Self { Self { + keybindings, deletion_rect: None, } } @@ -38,17 +42,12 @@ impl Tool for DeletionTool { *pos2 = mouse_pos_m; } - if rl.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) { - if let Some((pos1, pos2)) = self.deletion_rect { - Self::delete_rect(map_data, Rect::bounding_rect(pos1, pos2)); - self.deletion_rect = None; - } else { - self.deletion_rect = Some((mouse_pos_m, mouse_pos_m)) - } - } - - // Abort deletion. - if rl.is_mouse_button_pressed(MouseButton::MOUSE_RIGHT_BUTTON) { + if self.keybindings.do_delete.is_pressed(rl) && self.deletion_rect.is_some() { + let (pos1, pos2) = self.deletion_rect.take().unwrap(); + Self::delete_rect(map_data, Rect::bounding_rect(pos1, pos2)); + } else if self.keybindings.start_selection.is_pressed(rl) { + self.deletion_rect = Some((mouse_pos_m, mouse_pos_m)) + } else if self.keybindings.abort_deletion.is_pressed(rl) { self.deletion_rect = None; } } @@ -77,4 +76,8 @@ impl Tool for DeletionTool { ); } } + + fn activation_key(&self) -> Button { + self.keybindings.activation_key() + } } diff --git a/src/tool/icon_tool.rs b/src/tool/icon_tool.rs index 89a07e8..cf90056 100644 --- a/src/tool/icon_tool.rs +++ b/src/tool/icon_tool.rs @@ -1,3 +1,5 @@ +use crate::button::Button; +use crate::config::{IconToolKeybindings, ToolKeybindings}; use crate::grid::snap_to_grid; use crate::map_data::MapData; use crate::math::Vec2; @@ -34,6 +36,7 @@ pub struct IconInfo { pub struct IconTool { // TODO: support svg + keybindings: IconToolKeybindings, /// The icon data, containing the image texture and the info for that image texture like the /// scale the image actually has. icon_data: Vec<(Texture2D, IconFileInfo)>, @@ -43,7 +46,11 @@ pub struct IconTool { } impl IconTool { - pub fn new(rl: &mut RaylibHandle, rlt: &RaylibThread) -> Self { + pub fn new( + rl: &mut RaylibHandle, + rlt: &RaylibThread, + keybindings: IconToolKeybindings, + ) -> Self { /* Read all available icons from the icon directory. SVGs do not need any special scale * file, but pixel-based file formats require a RON-file declaring what the scale of the * picture is right beside them. @@ -87,6 +94,7 @@ impl IconTool { } Self { + keybindings, icon_data, current_icon: None, } @@ -114,18 +122,16 @@ impl Tool for IconTool { { // Unwrap the current icon, since it is now definitely set, as we are in the active update. let active_icon = self.current_icon.as_mut().unwrap(); - // Activate the next icon when pressing the icon tool key. - if rl.is_key_pressed(KeyboardKey::KEY_I) { + if self.keybindings.next.is_pressed(rl) { active_icon.icon_id = (active_icon.icon_id + 1) % self.icon_data.len(); } - if rl.is_mouse_button_pressed(MouseButton::MOUSE_RIGHT_BUTTON) { - // Rotate the currently active icon by a quarter rotation. + if self.keybindings.rotate_clockwise.is_pressed(rl) { active_icon.rotation += 45.; } } // Handle placing the icon on the map - if rl.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) { + if self.keybindings.place.is_pressed(rl) { map.icons_mut().push(self.current_icon.take().unwrap()); } } @@ -175,4 +181,8 @@ impl Tool for IconTool { ); } } + + fn activation_key(&self) -> Button { + self.keybindings.activation_key() + } } diff --git a/src/tool/mod.rs b/src/tool/mod.rs index a3d5964..6b9eba7 100644 --- a/src/tool/mod.rs +++ b/src/tool/mod.rs @@ -8,6 +8,7 @@ pub use icon_tool::IconTool; pub use room_tool::RoomTool; pub use wall_tool::WallTool; +use crate::button::Button; use crate::map_data::MapData; use crate::transform::Transform; use raylib::core::drawing::RaylibDrawHandle; @@ -28,4 +29,6 @@ pub trait Tool { fn active_update(&mut self, map: &mut MapData, rl: &RaylibHandle, transform: &Transform); fn draw(&self, _map: &MapData, _rld: &mut RaylibDrawHandle, _transform: &Transform) {} + + fn activation_key(&self) -> Button; } diff --git a/src/tool/room_tool.rs b/src/tool/room_tool.rs index 09126c9..1bfb225 100644 --- a/src/tool/room_tool.rs +++ b/src/tool/room_tool.rs @@ -1,4 +1,6 @@ use super::Tool; +use crate::button::Button; +use crate::config::{RoomToolKeybindings, ToolKeybindings}; use crate::grid::snap_to_grid; use crate::map_data::MapData; use crate::math::{Rect, Vec2}; @@ -8,6 +10,7 @@ use raylib::ffi::{Color, MouseButton}; use raylib::RaylibHandle; pub struct RoomTool { + keybindings: RoomToolKeybindings, /// The rectangle that is currently being drawn by the user. Once it is finished, it will be /// pushed into the room_rects. unfinished_rect: Option<(Vec2, Vec2)>, @@ -15,8 +18,9 @@ pub struct RoomTool { impl RoomTool { /// Create a new room tool where no rooms have been drawn yet. - pub fn new() -> Self { + pub fn new(keybindings: RoomToolKeybindings) -> Self { Self { + keybindings, unfinished_rect: None, } } @@ -31,18 +35,15 @@ impl Tool for RoomTool { } // Start or finish drawing the currently unfinished rectangle - if rl.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) { - if let Some((pos1, pos2)) = self.unfinished_rect { - map_data.rooms_mut().push(Rect::bounding_rect(pos1, pos2)); - self.unfinished_rect = None; - } else { - let snapped_mouse_pos = snap_to_grid(mouse_pos_m, 0.5); - self.unfinished_rect = Some((snapped_mouse_pos, snapped_mouse_pos)) - } + if self.keybindings.finish_draw.is_pressed(rl) && self.unfinished_rect.is_some() { + let (pos1, pos2) = self.unfinished_rect.take().unwrap(); + map_data.rooms_mut().push(Rect::bounding_rect(pos1, pos2)); + } else if self.keybindings.start_draw.is_pressed(rl) { + let snapped_mouse_pos = snap_to_grid(mouse_pos_m, 0.5); + self.unfinished_rect = Some((snapped_mouse_pos, snapped_mouse_pos)) } - // Abort drawing the room (if any) in case the right mouse button was pressed. - if rl.is_mouse_button_pressed(MouseButton::MOUSE_RIGHT_BUTTON) { + if self.keybindings.abort_draw.is_pressed(rl) { self.unfinished_rect = None; } } @@ -74,4 +75,8 @@ impl Tool for RoomTool { ); } } + + fn activation_key(&self) -> Button { + self.keybindings.activation_key() + } } diff --git a/src/tool/wall_tool.rs b/src/tool/wall_tool.rs index 5eda8e0..31a3770 100644 --- a/src/tool/wall_tool.rs +++ b/src/tool/wall_tool.rs @@ -1,4 +1,6 @@ use super::Tool; +use crate::button::Button; +use crate::config::{ToolKeybindings, WallToolKeybindings}; use crate::grid::snap_to_grid; use crate::map_data::MapData; use crate::math::Vec2; @@ -8,12 +10,14 @@ use raylib::ffi::{Color, MouseButton, Vector2}; use raylib::RaylibHandle; pub struct WallTool { + keybindings: WallToolKeybindings, unfinished_wall: Option<(Vec2, Vec2)>, } impl WallTool { - pub fn new() -> Self { + pub fn new(keybindings: WallToolKeybindings) -> Self { Self { + keybindings, unfinished_wall: None, } } @@ -26,17 +30,16 @@ impl Tool for WallTool { *pos2 = snap_to_grid(mouse_pos_m, 0.5); } - 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 = Some((pos2, pos2)); - } else { - let snapped_mouse_pos = snap_to_grid(mouse_pos_m, 0.5); - self.unfinished_wall = Some((snapped_mouse_pos, snapped_mouse_pos)) - } + if self.keybindings.finish_segment.is_pressed(rl) && 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) { + let snapped_mouse_pos = snap_to_grid(mouse_pos_m, 0.5); + self.unfinished_wall = Some((snapped_mouse_pos, snapped_mouse_pos)) } - if rl.is_mouse_button_pressed(MouseButton::MOUSE_RIGHT_BUTTON) { + if self.keybindings.abort_segment.is_pressed(rl) { self.unfinished_wall = None; } } @@ -74,4 +77,8 @@ impl Tool for WallTool { ); } } + + fn activation_key(&self) -> Button { + self.keybindings.activation_key() + } } -- cgit v1.2.3-70-g09d2