Merge branch 'juliancoffee/hud_refactor' into 'master'

Skillbar hud refactoring (+ shortened key names)

See merge request veloren/veloren!2663
This commit is contained in:
Imbris 2021-07-29 16:08:56 +00:00
commit 9fb6b84670
7 changed files with 540 additions and 586 deletions

View File

@ -116,6 +116,9 @@ impl Energy {
});
}
}
/// Returns the fraction of energy an entity has remaining
pub fn fraction(&self) -> f32 { self.current as f32 / self.maximum.max(1) as f32 }
}
pub struct EnergyChange {

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

@ -316,6 +316,88 @@ widget_ids! {
}
}
/// Specifier to use with `Position::position`
/// Read its documentation for more
// TODO: extend as you need it
#[derive(Clone, Copy)]
pub enum PositionSpecifier {
MidBottomWithMarginOn(widget::Id, f64),
TopRightWithMarginsOn(widget::Id, f64, f64),
BottomRightWithMarginsOn(widget::Id, f64, f64),
BottomLeftWithMarginsOn(widget::Id, f64, f64),
RightFrom(widget::Id, f64),
}
/// Trait which enables you to declare widget position
/// to use later on widget creation.
/// It is implemented for all widgets which are implement Positionable,
/// so you can easily change your code to use this method.
///
/// Consider this example:
/// ```text
/// let slot1 = slot_maker
/// .fabricate(hotbar::Slot::One, [40.0; 2])
/// .filled_slot(self.imgs.skillbar_slot)
/// .bottom_left_with_margins_on(state.ids.frame, 0.0, 0.0);
/// if condition {
/// call_slot1(slot1);
/// } else {
/// call_slot2(slot1);
/// }
/// let slot2 = slot_maker
/// .fabricate(hotbar::Slot::Two, [40.0; 2])
/// .filled_slot(self.imgs.skillbar_slot)
/// .right_from(state.ids.slot1, slot_offset);
/// if condition {
/// call_slot1(slot2);
/// } else {
/// call_slot2(slot2);
/// }
/// ```
/// Despite being identical, you can't easily deduplicate code
/// which uses slot1 and slot2 as they are calling methods to position itself.
/// This can be solved if you declare position and use it later like so
/// ```text
/// let slots = [
/// (hotbar::Slot::One, BottomLeftWithMarginsOn(state.ids.frame, 0.0, 0.0)),
/// (hotbar::Slot::Two, RightFrom(state.ids.slot1, slot_offset)),
/// ];
/// for (slot, pos) in slots {
/// let slot = slot_maker
/// .fabricate(slot, [40.0; 2])
/// .filled_slot(self.imgs.skillbar_slot)
/// .position(pos);
/// if condition {
/// call_slot1(slot);
/// } else {
/// call_slot2(slot);
/// }
/// }
/// ```
pub trait Position {
fn position(self, request: PositionSpecifier) -> Self;
}
impl<W: Positionable> Position for W {
fn position(self, request: PositionSpecifier) -> Self {
match request {
PositionSpecifier::MidBottomWithMarginOn(other, margin) => {
self.mid_bottom_with_margin_on(other, margin)
},
PositionSpecifier::TopRightWithMarginsOn(other, top, right) => {
self.top_right_with_margins_on(other, top, right)
},
PositionSpecifier::BottomRightWithMarginsOn(other, bottom, right) => {
self.bottom_right_with_margins_on(other, bottom, right)
},
PositionSpecifier::BottomLeftWithMarginsOn(other, bottom, left) => {
self.bottom_left_with_margins_on(other, bottom, left)
},
PositionSpecifier::RightFrom(other, offset) => self.right_from(other, offset),
}
}
}
#[derive(Clone, Copy)]
pub struct BuffInfo {
kind: comp::BuffKind,

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

File diff suppressed because it is too large Load Diff

View File

@ -111,9 +111,7 @@ fn main() {
Panic Payload: {:?}\n\
PanicInfo: {}\n\
Game version: {} [{}]",
logs_dir
.join("voxygen.log.<date>")
.display(),
logs_dir.join("voxygen.log.<date>").display(),
reason,
panic_info,
common::util::GIT_HASH.to_string(),

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