diff options
| author | Arne Dußin | 2021-01-17 13:33:04 +0100 |
|---|---|---|
| committer | Arne Dußin | 2021-01-17 13:33:04 +0100 |
| commit | 51b7747e62c189d430318c67368a5c84e50ece61 (patch) | |
| tree | 328be6230d392027eb106fd963b5ec97b9034f9f /src/editor.rs | |
| parent | b1179849c28e50c39ac3c94af9dda86ee24beca0 (diff) | |
| download | graf_karto-51b7747e62c189d430318c67368a5c84e50ece61.tar.gz graf_karto-51b7747e62c189d430318c67368a5c84e50ece61.zip | |
Input revamp to make keybindings controlable.input
Diffstat (limited to 'src/editor.rs')
| -rw-r--r-- | src/editor.rs | 86 |
1 files changed, 29 insertions, 57 deletions
diff --git a/src/editor.rs b/src/editor.rs index abbc401..7bf8f5e 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -5,8 +5,8 @@ //! currently a difference between things that are being created (inside the editor) and things that //! are part of the environment (the map). -use crate::button::{Button, MouseButton}; use crate::config::Config; +use crate::input::{Binding, Input}; use crate::map::Map; use crate::snapping::Snapper; use crate::tool::*; @@ -20,7 +20,7 @@ pub struct Editor { map: Map, /// HashMap that matches the ToolType with its proper activation key and of course the tool /// itself. - tools: HashMap<ToolType, (Box<dyn Tool>, Button)>, + tools: HashMap<ToolType, (Box<dyn Tool>, Binding)>, active: ToolType, config: Config, } @@ -31,46 +31,52 @@ impl Editor { pub fn new(rl: &mut RaylibHandle, rlt: &RaylibThread, config: Config) -> Self { let map = Map::new(rl, rlt); - let mut tools: HashMap<ToolType, (Box<dyn Tool>, Button)> = + let mut tools: HashMap<ToolType, (Box<dyn Tool>, Binding)> = HashMap::with_capacity(ToolType::NumTools as usize); tools.insert( ToolType::RectRoomTool, ( Box::new(RectRoomTool::new()), - config.tool_activation_keys.rect_room, + config.tool_activation_binds.rect_room.clone(), ), ); tools.insert( ToolType::PolygonRoomTool, ( Box::new(PolygonRoomTool::new()), - config.tool_activation_keys.polygon_room, + config.tool_activation_binds.polygon_room.clone(), ), ); tools.insert( ToolType::WallTool, - (Box::new(WallTool::new()), config.tool_activation_keys.wall), + ( + Box::new(WallTool::new()), + config.tool_activation_binds.wall.clone(), + ), ); tools.insert( ToolType::IconTool, ( - Box::new(IconTool::new(config.icon_keys.clone(), map.icon_renderer())), - config.tool_activation_keys.icon, + Box::new(IconTool::new( + config.icon_binds.clone(), + map.icon_renderer(), + )), + config.tool_activation_binds.icon.clone(), ), ); tools.insert( ToolType::DeletionTool, ( Box::new(DeletionTool::new()), - config.tool_activation_keys.deletion, + config.tool_activation_binds.deletion.clone(), ), ); tools.insert( ToolType::SelectionTool, ( Box::new(SelectionTool::new()), - config.tool_activation_keys.selection, + config.tool_activation_binds.selection.clone(), ), ); @@ -111,22 +117,19 @@ impl Editor { rl: &mut RaylibHandle, transform: &Transform, snapper: &Snapper, - mouse_blocked: bool, - keyboard_captured: bool, + input: &mut Input, ) { // Handle keybindings for tool change - if !keyboard_captured { - for (&tool_type, (_, activation_key)) in self.tools.iter() { - if activation_key.is_pressed(rl, false) { - // Don't do anything if the tool does not change. - if tool_type == self.active { - break; - } - - // Activate the tool of which the key binding has been pressed. - self.set_active(tool_type); + for (&tool_type, (_, activation_bind)) in self.tools.iter() { + if input.poll_global(&activation_bind) { + // Don't do anything if the tool does not change. + if tool_type == self.active { break; } + + // Activate the tool of which the key binding has been pressed. + self.set_active(tool_type); + break; } } @@ -138,49 +141,18 @@ impl Editor { active_tool.update(&self.map, &snapped_mouse_pos); // Handle common keybindings many of the tools have. - if self - .config - .tool_general_keys - .place_single - .is_pressed(rl, mouse_blocked) - { + if input.poll_global(&self.config.tool_general_binds.place_single) { active_tool.place_single(&mut self.map, &snapped_mouse_pos); } - if self - .config - .tool_general_keys - .finish - .is_pressed(rl, mouse_blocked) - { + if input.poll_global(&self.config.tool_general_binds.finish) { active_tool.finish(&mut self.map); } - if self - .config - .tool_general_keys - .abort - .is_pressed(rl, mouse_blocked) - { + if input.poll_global(&self.config.tool_general_binds.abort) { active_tool.abort(); } // Handle custom keybindings in case the tool has any. - let latest_button = if let Some(keyboard_key) = rl.get_key_pressed() { - Some(Button::Keyboard(keyboard_key.into())) - } else { - let mouse_buttons = [ - Button::Mouse(MouseButton::Left), - Button::Mouse(MouseButton::Middle), - Button::Mouse(MouseButton::Right), - ]; - mouse_buttons - .iter() - .find(|button| button.is_pressed(rl, mouse_blocked)) - .copied() - }; - - if let Some(latest_button) = latest_button { - active_tool.on_button_pressed(&mut self.map, latest_button); - } + active_tool.handle_custom_bindings(&mut self.map, input); } /// Draw all tools and in case of the active tool also what is currently being edited by it, if |
