veloren/voxygen/src/hud/buttons.rs

288 lines
8.0 KiB
Rust
Raw Normal View History

2020-04-26 01:44:56 +00:00
use super::{
img_ids::{Imgs, ImgsRot},
2022-12-23 15:22:13 +00:00
BLACK, TEXT_COLOR,
2020-04-26 01:44:56 +00:00
};
use crate::{
game_input::GameInput,
ui::{fonts::Fonts, ImageFrame, Tooltip, TooltipManager, Tooltipable},
window::KeyMouse,
2020-04-26 01:44:56 +00:00
GlobalState,
};
use conrod_core::{
2022-12-23 15:22:13 +00:00
widget::{self, Button, Text, UpdateArgs},
widget_ids, Color, Colorable, Positionable, Sizeable, UiCell, Widget, WidgetCommon,
};
2021-07-29 18:47:45 +00:00
use i18n::Localization;
widget_ids! {
struct Ids {
bag_show_map,
map_button,
2020-04-26 01:44:56 +00:00
map_text,
map_text_bg,
settings_button,
2020-04-26 01:44:56 +00:00
settings_text,
settings_text_bg,
social_button,
social_button_bg,
2020-04-26 01:44:56 +00:00
social_text,
social_text_bg,
spellbook_button,
spellbook_button_bg,
2020-04-26 01:44:56 +00:00
spellbook_text,
spellbook_text_bg,
2020-07-14 20:11:39 +00:00
crafting_button,
crafting_button_bg,
crafting_text,
crafting_text_bg,
group_button,
sp_arrow,
sp_arrow_txt_bg,
sp_arrow_txt,
}
}
#[derive(WidgetCommon)]
pub struct Buttons<'a> {
imgs: &'a Imgs,
fonts: &'a Fonts,
#[conrod(common_builder)]
common: widget::CommonBuilder,
2020-04-26 01:44:56 +00:00
global_state: &'a GlobalState,
rot_imgs: &'a ImgsRot,
tooltip_manager: &'a mut TooltipManager,
localized_strings: &'a Localization,
}
impl<'a> Buttons<'a> {
pub fn new(
imgs: &'a Imgs,
fonts: &'a Fonts,
2020-04-26 01:44:56 +00:00
global_state: &'a GlobalState,
rot_imgs: &'a ImgsRot,
tooltip_manager: &'a mut TooltipManager,
localized_strings: &'a Localization,
) -> Self {
Self {
imgs,
2020-02-17 16:22:49 +00:00
fonts,
common: widget::CommonBuilder::default(),
2020-04-26 01:44:56 +00:00
global_state,
rot_imgs,
tooltip_manager,
localized_strings,
}
}
}
pub struct State {
ids: Ids,
}
2022-07-15 16:59:37 +00:00
#[allow(clippy::enum_variant_names)] //think about renaming to ToggleEvent
pub enum Event {
ToggleSettings,
ToggleMap,
ToggleSocial,
2020-07-14 20:11:39 +00:00
ToggleCrafting,
}
impl<'a> Widget for Buttons<'a> {
type Event = Option<Event>;
type State = State;
type Style = ();
fn init_state(&self, id_gen: widget::id::Generator) -> Self::State {
State {
ids: Ids::new(id_gen),
}
}
fn style(&self) -> Self::Style {}
fn update(self, args: UpdateArgs<Self>) -> Self::Event {
2021-06-19 05:04:05 +00:00
common_base::prof_span!("Buttons::update");
let UpdateArgs { state, ui, .. } = args;
2020-04-26 01:44:56 +00:00
let localized_strings = self.localized_strings;
2020-04-26 01:44:56 +00:00
let button_tooltip = Tooltip::new({
// Edge images [t, b, r, l]
// Corner images [tr, tl, br, bl]
let edge = &self.rot_imgs.tt_side;
let corner = &self.rot_imgs.tt_corner;
ImageFrame::new(
[edge.cw180, edge.none, edge.cw270, edge.cw90],
[corner.none, corner.cw270, corner.cw90, corner.cw180],
Color::Rgba(0.08, 0.07, 0.04, 1.0),
5.0,
)
})
.title_font_size(self.fonts.cyri.scale(15))
.parent(ui.window)
.desc_font_size(self.fonts.cyri.scale(12))
.font_id(self.fonts.cyri.conrod_id)
.desc_text_color(TEXT_COLOR);
2022-12-23 15:22:13 +00:00
2020-04-26 01:44:56 +00:00
// Settings
if Button::image(self.imgs.settings)
.w_h(29.0, 25.0)
2022-12-23 15:22:13 +00:00
.bottom_right_with_margins_on(ui.window, 5.0, 5.0)
.hover_image(self.imgs.settings_hover)
.press_image(self.imgs.settings_press)
2020-04-26 01:44:56 +00:00
.with_tooltip(
self.tooltip_manager,
&localized_strings.get_msg("common-settings"),
2020-04-26 01:44:56 +00:00
"",
&button_tooltip,
2020-10-07 02:23:20 +00:00
TEXT_COLOR,
2020-04-26 01:44:56 +00:00
)
.set(state.ids.settings_button, ui)
.was_clicked()
{
return Some(Event::ToggleSettings);
};
2020-04-26 01:44:56 +00:00
if let Some(settings) = &self
.global_state
.settings
.controls
.get_binding(GameInput::Settings)
{
self.create_new_button_with_shadow(
ui,
2021-07-11 18:41:52 +00:00
settings,
state.ids.settings_button,
state.ids.settings_text_bg,
state.ids.settings_text,
);
2020-04-26 01:44:56 +00:00
};
2020-04-26 01:44:56 +00:00
// Social
if Button::image(self.imgs.social)
2019-07-26 02:28:53 +00:00
.w_h(25.0, 25.0)
.left_from(state.ids.settings_button, 10.0)
2020-04-26 01:44:56 +00:00
.hover_image(self.imgs.social_hover)
.press_image(self.imgs.social_press)
.with_tooltip(
self.tooltip_manager,
&localized_strings.get_msg("hud-social"),
2020-04-26 01:44:56 +00:00
"",
&button_tooltip,
2020-10-07 02:23:20 +00:00
TEXT_COLOR,
2020-04-26 01:44:56 +00:00
)
.set(state.ids.social_button, ui)
.was_clicked()
{
return Some(Event::ToggleSocial);
}
if let Some(social) = &self
.global_state
.settings
.controls
.get_binding(GameInput::Social)
{
self.create_new_button_with_shadow(
ui,
2021-07-11 18:41:52 +00:00
social,
state.ids.social_button,
state.ids.social_text_bg,
state.ids.social_text,
);
2020-04-26 01:44:56 +00:00
};
// Map
if Button::image(self.imgs.map_button)
.w_h(22.0, 25.0)
2020-04-26 01:44:56 +00:00
.left_from(state.ids.social_button, 10.0)
.hover_image(self.imgs.map_hover)
.press_image(self.imgs.map_press)
2020-04-26 01:44:56 +00:00
.with_tooltip(
self.tooltip_manager,
&localized_strings.get_msg("hud-map-map_title"),
2020-04-26 01:44:56 +00:00
"",
&button_tooltip,
2020-10-07 02:23:20 +00:00
TEXT_COLOR,
2020-04-26 01:44:56 +00:00
)
.set(state.ids.map_button, ui)
.was_clicked()
{
return Some(Event::ToggleMap);
};
2020-04-26 01:44:56 +00:00
if let Some(map) = &self
.global_state
.settings
.controls
.get_binding(GameInput::Map)
{
self.create_new_button_with_shadow(
ui,
2021-07-11 18:41:52 +00:00
map,
state.ids.map_button,
state.ids.map_text_bg,
state.ids.map_text,
);
2020-04-26 01:44:56 +00:00
}
2022-12-23 15:22:13 +00:00
2020-07-14 20:11:39 +00:00
// Crafting
if Button::image(self.imgs.crafting_icon)
.w_h(25.0, 25.0)
2022-12-23 15:22:13 +00:00
.left_from(state.ids.map_button, 10.0)
2020-07-14 20:11:39 +00:00
.hover_image(self.imgs.crafting_icon_hover)
.press_image(self.imgs.crafting_icon_press)
.with_tooltip(
self.tooltip_manager,
&localized_strings.get_msg("hud-crafting"),
2020-07-14 20:11:39 +00:00
"",
&button_tooltip,
2020-10-07 02:23:20 +00:00
TEXT_COLOR,
2020-07-14 20:11:39 +00:00
)
.set(state.ids.crafting_button, ui)
.was_clicked()
{
return Some(Event::ToggleCrafting);
}
if let Some(crafting) = &self
.global_state
.settings
.controls
.get_binding(GameInput::Crafting)
{
self.create_new_button_with_shadow(
ui,
2021-07-11 18:41:52 +00:00
crafting,
state.ids.crafting_button,
state.ids.crafting_text_bg,
state.ids.crafting_text,
);
2020-07-14 20:11:39 +00:00
}
None
}
}
impl<'a> Buttons<'a> {
fn create_new_button_with_shadow(
&self,
ui: &mut UiCell,
key_mouse: &KeyMouse,
button_identifier: widget::Id,
text_background: widget::Id,
text: widget::Id,
) {
let key_layout = &self.global_state.window.key_layout;
let key_desc = key_mouse.display_shortest(key_layout);
//Create shadow
2021-07-21 14:53:03 +00:00
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)
.color(BLACK)
.set(text_background, ui);
//Create button
2021-07-21 14:53:03 +00:00
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)
.color(TEXT_COLOR)
.set(text, ui);
}
}