veloren_voxygen/hud/
esc_menu.rs

1use super::{TEXT_COLOR, img_ids::Imgs, settings_window::SettingsTab};
2use crate::ui::fonts::Fonts;
3use conrod_core::{
4    Color, Labelable, Positionable, Sizeable, Widget, WidgetCommon,
5    widget::{self, Button, Image},
6    widget_ids,
7};
8use i18n::Localization;
9
10widget_ids! {
11    struct Ids {
12        esc_bg,
13        fireplace,
14        banner_top,
15        menu_button_1,
16        menu_button_2,
17        menu_button_3,
18        menu_button_4,
19        menu_button_5,
20        menu_button_6,
21    }
22}
23
24#[derive(WidgetCommon)]
25pub struct EscMenu<'a> {
26    imgs: &'a Imgs,
27    fonts: &'a Fonts,
28    localized_strings: &'a Localization,
29
30    #[conrod(common_builder)]
31    common: widget::CommonBuilder,
32}
33
34impl<'a> EscMenu<'a> {
35    pub fn new(imgs: &'a Imgs, fonts: &'a Fonts, localized_strings: &'a Localization) -> Self {
36        Self {
37            imgs,
38            fonts,
39            localized_strings,
40            common: widget::CommonBuilder::default(),
41        }
42    }
43}
44
45pub struct State {
46    ids: Ids,
47}
48
49pub enum Event {
50    OpenSettings(SettingsTab),
51    CharacterSelection,
52    Logout,
53    Quit,
54    Close,
55}
56
57impl Widget for EscMenu<'_> {
58    type Event = Option<Event>;
59    type State = State;
60    type Style = ();
61
62    fn init_state(&self, id_gen: widget::id::Generator) -> Self::State {
63        State {
64            ids: Ids::new(id_gen),
65        }
66    }
67
68    fn style(&self) -> Self::Style {}
69
70    fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
71        common_base::prof_span!("EscMenu::update");
72        let widget::UpdateArgs { state, ui, .. } = args;
73
74        Image::new(self.imgs.esc_frame)
75            .w_h(240.0, 380.0)
76            .color(Some(Color::Rgba(1.0, 1.0, 1.0, 0.9)))
77            .middle_of(ui.window)
78            .set(state.ids.esc_bg, ui);
79
80        Image::new(self.imgs.banner_top)
81            .w_h(250.0, 34.0)
82            .mid_top_with_margin_on(state.ids.esc_bg, -34.0)
83            .set(state.ids.banner_top, ui);
84
85        // Resume
86        if Button::image(self.imgs.button)
87            .mid_bottom_with_margin_on(state.ids.banner_top, -60.0)
88            .w_h(210.0, 50.0)
89            .hover_image(self.imgs.button_hover)
90            .press_image(self.imgs.button_press)
91            .label(&self.localized_strings.get_msg("common-resume"))
92            .label_y(conrod_core::position::Relative::Scalar(3.0))
93            .label_color(TEXT_COLOR)
94            .label_font_size(self.fonts.cyri.scale(20))
95            .label_font_id(self.fonts.cyri.conrod_id)
96            .set(state.ids.menu_button_1, ui)
97            .was_clicked()
98        {
99            return Some(Event::Close);
100        };
101
102        // Settings
103        if Button::image(self.imgs.button)
104            .mid_bottom_with_margin_on(state.ids.menu_button_1, -65.0)
105            .w_h(210.0, 50.0)
106            .hover_image(self.imgs.button_hover)
107            .press_image(self.imgs.button_press)
108            .label(&self.localized_strings.get_msg("common-settings"))
109            .label_y(conrod_core::position::Relative::Scalar(3.0))
110            .label_color(TEXT_COLOR)
111            .label_font_size(self.fonts.cyri.scale(20))
112            .label_font_id(self.fonts.cyri.conrod_id)
113            .set(state.ids.menu_button_2, ui)
114            .was_clicked()
115        {
116            return Some(Event::OpenSettings(SettingsTab::Interface));
117        };
118        // Controls
119        if Button::image(self.imgs.button)
120            .mid_bottom_with_margin_on(state.ids.menu_button_2, -55.0)
121            .w_h(210.0, 50.0)
122            .hover_image(self.imgs.button_hover)
123            .press_image(self.imgs.button_press)
124            .label(&self.localized_strings.get_msg("common-controls"))
125            .label_y(conrod_core::position::Relative::Scalar(3.0))
126            .label_color(TEXT_COLOR)
127            .label_font_size(self.fonts.cyri.scale(20))
128            .label_font_id(self.fonts.cyri.conrod_id)
129            .set(state.ids.menu_button_3, ui)
130            .was_clicked()
131        {
132            return Some(Event::OpenSettings(SettingsTab::Controls));
133        };
134        // Characters
135        if Button::image(self.imgs.button)
136            .mid_bottom_with_margin_on(state.ids.menu_button_3, -55.0)
137            .w_h(210.0, 50.0)
138            .hover_image(self.imgs.button_hover)
139            .press_image(self.imgs.button_press)
140            .label(&self.localized_strings.get_msg("common-characters"))
141            .label_y(conrod_core::position::Relative::Scalar(3.0))
142            .label_color(TEXT_COLOR)
143            .label_font_size(self.fonts.cyri.scale(20))
144            .label_font_id(self.fonts.cyri.conrod_id)
145            .set(state.ids.menu_button_4, ui)
146            .was_clicked()
147        {
148            return Some(Event::CharacterSelection);
149        };
150        // Logout
151        if Button::image(self.imgs.button)
152            .mid_bottom_with_margin_on(state.ids.menu_button_4, -65.0)
153            .w_h(210.0, 50.0)
154            .hover_image(self.imgs.button_hover)
155            .press_image(self.imgs.button_press)
156            .label(&self.localized_strings.get_msg("esc_menu-logout"))
157            .label_y(conrod_core::position::Relative::Scalar(3.0))
158            .label_color(TEXT_COLOR)
159            .label_font_size(self.fonts.cyri.scale(20))
160            .label_font_id(self.fonts.cyri.conrod_id)
161            .set(state.ids.menu_button_5, ui)
162            .was_clicked()
163        {
164            return Some(Event::Logout);
165        };
166        // Quit
167        if Button::image(self.imgs.button)
168            .mid_bottom_with_margin_on(state.ids.menu_button_5, -55.0)
169            .w_h(210.0, 50.0)
170            .hover_image(self.imgs.button_hover)
171            .press_image(self.imgs.button_press)
172            .label(&self.localized_strings.get_msg("esc_menu-quit_game"))
173            .label_y(conrod_core::position::Relative::Scalar(3.0))
174            .label_color(TEXT_COLOR)
175            .label_font_size(self.fonts.cyri.scale(20))
176            .label_font_id(self.fonts.cyri.conrod_id)
177            .set(state.ids.menu_button_6, ui)
178            .was_clicked()
179        {
180            return Some(Event::Quit);
181        };
182        None
183    }
184}