From e19e524f3a05a750cf7cb97fe351c599cdf86c62 Mon Sep 17 00:00:00 2001 From: juliancoffee <lightdarkdaughter@gmail.com> Date: Wed, 21 Jul 2021 17:53:03 +0300 Subject: [PATCH] display_shortened for keys --- voxygen/src/hud/buttons.rs | 7 ++++-- voxygen/src/hud/settings_window/controls.rs | 15 ++++++++--- voxygen/src/hud/skillbar.rs | 4 ++- voxygen/src/window.rs | 28 +++++++++++++++++++-- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/voxygen/src/hud/buttons.rs b/voxygen/src/hud/buttons.rs index 67384475b5..79bde0effd 100644 --- a/voxygen/src/hud/buttons.rs +++ b/voxygen/src/hud/buttons.rs @@ -425,9 +425,12 @@ impl<'a> Buttons<'a> { text: widget::Id, ) { let key_layout = &self.global_state.window.key_layout; + let key_desc = key_mouse + .display_shortened(key_layout) + .unwrap_or_else(|| key_mouse.display_string(key_layout)); //Create shadow - Text::new(key_mouse.display_string(key_layout).as_str()) + Text::new(&key_desc) .bottom_right_with_margins_on(button_identifier, 0.0, 0.0) .font_size(10) .font_id(self.fonts.cyri.conrod_id) @@ -435,7 +438,7 @@ impl<'a> Buttons<'a> { .set(text_background, ui); //Create button - Text::new(key_mouse.display_string(key_layout).as_str()) + Text::new(&key_desc) .bottom_right_with_margins_on(text_background, 1.0, 1.0) .font_size(10) .font_id(self.fonts.cyri.conrod_id) diff --git a/voxygen/src/hud/settings_window/controls.rs b/voxygen/src/hud/settings_window/controls.rs index ee9b129ecd..51b842f936 100644 --- a/voxygen/src/hud/settings_window/controls.rs +++ b/voxygen/src/hud/settings_window/controls.rs @@ -123,12 +123,19 @@ impl<'a> Widget for Controls<'a> { let (key_string, key_color) = if self.global_state.window.remapping_keybindings == Some(game_input) { ( - String::from(self.localized_strings.get("hud.settings.awaitingkey")), + self.localized_strings + .get("hud.settings.awaitingkey") + .to_owned(), TEXT_COLOR, ) } else if let Some(key) = controls.get_binding(game_input) { ( - key.display_string(key_layout), + format!( + "{} {}", + key.display_string(key_layout), + key.display_shortened(key_layout) + .map_or("".to_owned(), |short| format!("({})", short)) + ), if controls.has_conflicting_bindings(key) { TEXT_BIND_CONFLICT_COLOR } else { @@ -137,7 +144,9 @@ impl<'a> Widget for Controls<'a> { ) } else { ( - String::from(self.localized_strings.get("hud.settings.unbound")), + self.localized_strings + .get("hud.settings.unbound") + .to_owned(), ERROR_COLOR, ) }; diff --git a/voxygen/src/hud/skillbar.rs b/voxygen/src/hud/skillbar.rs index 96f43d0f0e..fe34b6a8a3 100644 --- a/voxygen/src/hud/skillbar.rs +++ b/voxygen/src/hud/skillbar.rs @@ -731,7 +731,9 @@ impl<'a> Widget for Skillbar<'a> { let position_bg = entry.shortcut_position_bg; let (id, id_bg) = entry.shortcut_widget_ids; - let key_desc = key.display_string(key_layout); + let key_desc = key + .display_shortened(key_layout) + .unwrap_or_else(|| key.display_string(key_layout)); // shortcut text Text::new(&key_desc) .position(position) diff --git a/voxygen/src/window.rs b/voxygen/src/window.rs index 6df984ff32..3663e3443a 100644 --- a/voxygen/src/window.rs +++ b/voxygen/src/window.rs @@ -106,6 +106,7 @@ pub enum KeyMouse { } impl KeyMouse { + /// Returns key description (e.g Left Shift) pub fn display_string(&self, key_layout: &Option<KeyLayout>) -> String { use self::KeyMouse::*; use winit::event::{MouseButton, VirtualKeyCode::*}; @@ -225,6 +226,7 @@ impl KeyMouse { Key(MediaSelect) => "MediaSelect", Key(MediaStop) => "MediaStop", Key(Minus) => "-", + Key(Plus) => "+", Key(NumpadMultiply) => "Numpad *", Key(Mute) => "Mute", Key(MyComputer) => "My Computer", @@ -322,7 +324,6 @@ impl KeyMouse { Key(Paste) => "Paste", Key(Cut) => "Cut", Key(Asterisk) => "*", - Key(Plus) => "+", Mouse(MouseButton::Left) => "Left Click", Mouse(MouseButton::Right) => "Right Click", Mouse(MouseButton::Middle) => "Middle Click", @@ -339,7 +340,30 @@ impl KeyMouse { }, }; - String::from(key_string) + key_string.to_owned() + } + + /// Returns shortened key name (e.g. Left Click -> LMB) + /// + /// Use it in case if space does really matter. + pub fn display_shortened(&self, _key_layout: &Option<KeyLayout>) -> Option<String> { + use self::KeyMouse::*; + use winit::event::{MouseButton, VirtualKeyCode::*}; + let key_string = match self { + Mouse(MouseButton::Left) => "M1", + Mouse(MouseButton::Right) => "M2", + Mouse(MouseButton::Middle) => "M3", + Mouse(MouseButton::Other(button)) => { + // Additional mouse buttons after middle click start at 1 + return Some(format!("M{}", button + 3)); + }, + Key(Back) => "Back", + Key(LShift) => "LShft", + Key(RShift) => "RShft", + _ => return None, + }; + + Some(key_string.to_owned()) } }