aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs117
1 files changed, 81 insertions, 36 deletions
diff --git a/src/config.rs b/src/config.rs
index b5abb1e..6d0680c 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,6 +1,6 @@
//! Home of the user configuratable content of graf karto, like keybindings and (TODO) colours etc.
-use crate::button::*;
+use crate::input::{Binding, Button, Input, MouseButton, Scancode};
use ron::de::from_reader;
use ron::ser::{to_string_pretty, PrettyConfig};
use serde::{Deserialize, Serialize};
@@ -12,49 +12,49 @@ use std::path::Path;
#[allow(missing_docs)]
#[derive(Deserialize, Serialize)]
pub struct Config {
- pub tool_activation_keys: ToolActivationKeys,
- pub tool_general_keys: ToolGeneralKeys,
- pub icon_keys: IconToolKeys,
+ pub tool_activation_binds: ToolActivationBinds,
+ pub tool_general_binds: ToolGeneralBinds,
+ pub icon_binds: IconToolBinds,
}
#[allow(missing_docs)]
#[derive(Deserialize, Serialize)]
/// The keys used to activate the individual tools. These keystrokes will not be sent to the tools,
/// but instead will be handled by the editor where the tools are registered.
-pub struct ToolActivationKeys {
- pub deletion: Button,
- pub icon: Button,
- pub polygon_room: Button,
- pub rect_room: Button,
- pub selection: Button,
- pub wall: Button,
+pub struct ToolActivationBinds {
+ pub deletion: Binding,
+ pub icon: Binding,
+ pub polygon_room: Binding,
+ pub rect_room: Binding,
+ pub selection: Binding,
+ pub wall: Binding,
}
#[derive(Deserialize, Serialize)]
/// Keys that are useful to most tools. These are packaged so that not every tool has the same n keys
/// and then some more.
-pub struct ToolGeneralKeys {
+pub struct ToolGeneralBinds {
/// Keybinding to, where applicable, place a single node (usually a vertex) for the tool in
/// question.
- pub place_single: Button,
+ pub place_single: Binding,
/// Finish up whatever one is doing with the current tool, without removing information.
- pub finish: Button,
+ pub finish: Binding,
/// Abort whatever one is doing with the current tool which means the last atomic action will not
/// be pushed into the map items.
- pub abort: Button,
+ pub abort: Binding,
}
#[derive(Clone, Serialize, Deserialize)]
/// Key bindings that are individually interesting to the icon tool.
-pub struct IconToolKeys {
+pub struct IconToolBinds {
/// Key to change to the next icon of the icon list.
- pub next: Button,
+ pub next: Binding,
/// Key to change to the previous icon of the icon list.
- pub previous: Button,
+ pub previous: Binding,
/// Rotate the working icon clockwise by a certain amount (currently 45 degrees)
- pub rotate_clockwise: Button,
+ pub rotate_clockwise: Binding,
/// Rotate the working icon counterclockwise by a certain amount (currently 45 degrees)
- pub rotate_counterclockwise: Button,
+ pub rotate_counterclockwise: Binding,
}
impl Config {
@@ -94,27 +94,72 @@ impl Config {
}
}
+/// Registers all bindings from the given configuration into the input handler. Should the
+/// configuration change at runtime, the global bindings of the input handler need to be cleared and
+/// this function must be called again.
+pub fn register_bindings(config: &Config, input: &mut Input) {
+ if !input.add_global(config.tool_activation_binds.deletion.clone()) {
+ warn!("Tried to add deletion binding twice.");
+ }
+ if !input.add_global(config.tool_activation_binds.icon.clone()) {
+ warn!("Tried to add icon binding twice.");
+ }
+ if !input.add_global(config.tool_activation_binds.polygon_room.clone()) {
+ warn!("Tried to add polygon room binding twice.");
+ }
+ if !input.add_global(config.tool_activation_binds.rect_room.clone()) {
+ warn!("Tried to add rect room binding twice.");
+ }
+ if !input.add_global(config.tool_activation_binds.selection.clone()) {
+ warn!("Tried to add selection binding twice.");
+ }
+ if !input.add_global(config.tool_activation_binds.wall.clone()) {
+ warn!("Tried to add wall binding twice.");
+ }
+ if !input.add_global(config.tool_general_binds.place_single.clone()) {
+ warn!("Tried to add place single binding twice.");
+ }
+ if !input.add_global(config.tool_general_binds.finish.clone()) {
+ warn!("Tried to add finish binding twice.");
+ }
+ if !input.add_global(config.tool_general_binds.abort.clone()) {
+ warn!("Tried to add abort binding twice.");
+ }
+ if !input.add_global(config.icon_binds.next.clone()) {
+ warn!("Tried to add next binding twice.");
+ }
+ if !input.add_global(config.icon_binds.previous.clone()) {
+ warn!("Tried to add previous binding twice.");
+ }
+ if !input.add_global(config.icon_binds.rotate_clockwise.clone()) {
+ warn!("Tried to add rotate clockwise binding twice.");
+ }
+ if !input.add_global(config.icon_binds.rotate_counterclockwise.clone()) {
+ warn!("Tried to add rotate counterclockwise binding twice.");
+ }
+}
+
impl Default for Config {
fn default() -> Self {
Config {
- tool_activation_keys: ToolActivationKeys {
- deletion: Button::Keyboard(KeyboardKey::D),
- icon: Button::Keyboard(KeyboardKey::I),
- polygon_room: Button::Keyboard(KeyboardKey::P),
- rect_room: Button::Keyboard(KeyboardKey::R),
- selection: Button::Keyboard(KeyboardKey::S),
- wall: Button::Keyboard(KeyboardKey::W),
+ tool_activation_binds: ToolActivationBinds {
+ deletion: Button::Text('d').into(),
+ icon: Button::Text('i').into(),
+ polygon_room: Button::Text('p').into(),
+ rect_room: Button::Text('r').into(),
+ selection: Button::Text('s').into(),
+ wall: Button::Text('w').into(),
},
- tool_general_keys: ToolGeneralKeys {
- place_single: Button::Mouse(MouseButton::Left),
- finish: Button::Keyboard(KeyboardKey::Enter),
- abort: Button::Mouse(MouseButton::Right),
+ tool_general_binds: ToolGeneralBinds {
+ place_single: Button::Mouse(MouseButton::Left).into(),
+ finish: Button::Scancode(Scancode::Enter).into(),
+ abort: Button::Mouse(MouseButton::Right).into(),
},
- icon_keys: IconToolKeys {
- next: Button::Keyboard(KeyboardKey::I),
- previous: Button::Keyboard(KeyboardKey::J),
- rotate_clockwise: Button::Mouse(MouseButton::Right),
- rotate_counterclockwise: Button::Keyboard(KeyboardKey::Minus),
+ icon_binds: IconToolBinds {
+ next: Button::Text('j').into(),
+ previous: Button::Text('k').into(),
+ rotate_clockwise: Button::Text('+').into(),
+ rotate_counterclockwise: Button::Text('-').into(),
},
}
}