veloren_voxygen/hud/settings_window/
language.rs

1use crate::{
2    GlobalState,
3    hud::{TEXT_COLOR, img_ids::Imgs},
4    session::settings_change::{Language as LanguageChange, Language::*},
5    ui::{ToggleButton, fonts::Fonts},
6};
7use conrod_core::{
8    Colorable, Labelable, Positionable, Sizeable, Widget, WidgetCommon, color,
9    widget::{self, Button, Rectangle, Scrollbar, Text},
10    widget_ids,
11};
12use i18n::{Localization, list_localizations};
13
14widget_ids! {
15    struct Ids {
16        window,
17        window_r,
18        english_fallback_button,
19        english_fallback_button_label,
20        send_to_server_checkbox,
21        send_to_server_checkbox_label,
22        window_scrollbar,
23        language_list[],
24    }
25}
26
27#[derive(WidgetCommon)]
28pub struct Language<'a> {
29    global_state: &'a GlobalState,
30    localized_strings: &'a Localization,
31    imgs: &'a Imgs,
32    fonts: &'a Fonts,
33    #[conrod(common_builder)]
34    common: widget::CommonBuilder,
35}
36impl<'a> Language<'a> {
37    pub fn new(
38        global_state: &'a GlobalState,
39        imgs: &'a Imgs,
40        fonts: &'a Fonts,
41        localized_strings: &'a Localization,
42    ) -> Self {
43        Self {
44            global_state,
45            localized_strings,
46            imgs,
47            fonts,
48            common: widget::CommonBuilder::default(),
49        }
50    }
51}
52
53pub struct State {
54    ids: Ids,
55}
56
57impl Widget for Language<'_> {
58    type Event = Vec<LanguageChange>;
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!("Language::update");
72        let widget::UpdateArgs { state, ui, .. } = args;
73
74        let mut events = Vec::new();
75
76        Rectangle::fill_with(args.rect.dim(), color::TRANSPARENT)
77            .xy(args.rect.xy())
78            .graphics_for(args.id)
79            .scroll_kids()
80            .scroll_kids_vertically()
81            .set(state.ids.window, ui);
82        Rectangle::fill_with([args.rect.w() / 2.0, args.rect.h()], color::TRANSPARENT)
83            .top_right()
84            .parent(state.ids.window)
85            .set(state.ids.window_r, ui);
86        Scrollbar::y_axis(state.ids.window)
87            .thickness(5.0)
88            .rgba(0.33, 0.33, 0.33, 1.0)
89            .set(state.ids.window_scrollbar, ui);
90
91        // Share with server button
92        let send_to_server = ToggleButton::new(
93            self.global_state.settings.language.send_to_server,
94            self.imgs.checkbox,
95            self.imgs.checkbox_checked,
96        )
97        .w_h(18.0, 18.0)
98        .top_left_with_margin_on(state.ids.window, 20.0)
99        .hover_images(self.imgs.checkbox_mo, self.imgs.checkbox_checked_mo)
100        .press_images(self.imgs.checkbox_press, self.imgs.checkbox_checked)
101        .set(state.ids.send_to_server_checkbox, ui);
102
103        if send_to_server != self.global_state.settings.language.send_to_server {
104            events.push(ToggleSendToServer(send_to_server));
105        }
106
107        Text::new(
108            &self
109                .localized_strings
110                .get_msg("hud-settings-language_send_to_server"),
111        )
112        .right_from(state.ids.send_to_server_checkbox, 10.0)
113        .font_size(self.fonts.cyri.scale(14))
114        .font_id(self.fonts.cyri.conrod_id)
115        .graphics_for(state.ids.send_to_server_checkbox)
116        .color(TEXT_COLOR)
117        .set(state.ids.send_to_server_checkbox_label, ui);
118
119        // English as fallback language
120        let show_english_fallback = ToggleButton::new(
121            self.global_state.settings.language.use_english_fallback,
122            self.imgs.checkbox,
123            self.imgs.checkbox_checked,
124        )
125        .w_h(18.0, 18.0)
126        .down_from(state.ids.send_to_server_checkbox, 10.0)
127        .hover_images(self.imgs.checkbox_mo, self.imgs.checkbox_checked_mo)
128        .press_images(self.imgs.checkbox_press, self.imgs.checkbox_checked)
129        .set(state.ids.english_fallback_button, ui);
130
131        if self.global_state.settings.language.use_english_fallback != show_english_fallback {
132            events.push(ToggleEnglishFallback(show_english_fallback));
133        }
134
135        Text::new(
136            &self
137                .localized_strings
138                .get_msg("hud-settings-english_fallback"),
139        )
140        .right_from(state.ids.english_fallback_button, 10.0)
141        .font_size(self.fonts.cyri.scale(14))
142        .font_id(self.fonts.cyri.conrod_id)
143        .graphics_for(state.ids.english_fallback_button)
144        .color(TEXT_COLOR)
145        .set(state.ids.english_fallback_button_label, ui);
146
147        // List available languages
148        let selected_language = &self.global_state.settings.language.selected_language;
149        let language_list = list_localizations();
150        if state.ids.language_list.len() < language_list.len() {
151            state.update(|state| {
152                state
153                    .ids
154                    .language_list
155                    .resize(language_list.len(), &mut ui.widget_id_generator())
156            });
157        };
158        for (i, language) in language_list.iter().enumerate() {
159            let button_w = 400.0;
160            let button_h = 50.0;
161            let button = Button::image(if selected_language == &language.language_identifier {
162                self.imgs.selection
163            } else {
164                self.imgs.nothing
165            });
166            let button = if i == 0 {
167                button.mid_top_with_margin_on(state.ids.window, 58.0)
168            } else {
169                button.mid_bottom_with_margin_on(state.ids.language_list[i - 1], -button_h)
170            };
171            if button
172                .label(&language.language_name)
173                .w_h(button_w, button_h)
174                .hover_image(self.imgs.selection_hover)
175                .press_image(self.imgs.selection_press)
176                .label_color(TEXT_COLOR)
177                .label_font_size(self.fonts.cyri.scale(22))
178                .label_font_id(self.fonts.cyri.conrod_id)
179                .label_y(conrod_core::position::Relative::Scalar(2.0))
180                .set(state.ids.language_list[i], ui)
181                .was_clicked()
182            {
183                events.push(ChangeLanguage(Box::new(language.to_owned())));
184            }
185        }
186
187        events
188    }
189}