Skip to main content

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