display_shortened for keys

This commit is contained in:
juliancoffee 2021-07-21 17:53:03 +03:00
parent 034913a22d
commit 930a156cab
4 changed files with 46 additions and 8 deletions

View File

@ -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)

View File

@ -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,
)
};

View File

@ -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)

View File

@ -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())
}
}