Fix language and ui resolution in menus not updating when they are changed ingame

This commit is contained in:
Imbris 2020-11-12 22:06:57 -05:00
parent b0458bb21c
commit f1ca3ccc69
5 changed files with 40 additions and 6 deletions

View File

@ -57,9 +57,16 @@ impl CharSelectionState {
} }
impl PlayState for CharSelectionState { impl PlayState for CharSelectionState {
fn enter(&mut self, _: &mut GlobalState, _: Direction) { fn enter(&mut self, global_state: &mut GlobalState, _: Direction) {
// Load the player's character list // Load the player's character list
self.client.borrow_mut().load_character_list(); self.client.borrow_mut().load_character_list();
// Updated localization in case the selected language was changed
let localized_strings = crate::i18n::Localization::load_expect(
&crate::i18n::i18n_asset_key(&global_state.settings.language.selected_language),
);
self.char_selection_ui
.update_language(std::sync::Arc::clone(&localized_strings));
} }
fn tick(&mut self, global_state: &mut GlobalState, events: Vec<WinEvent>) -> PlayStateResult { fn tick(&mut self, global_state: &mut GlobalState, events: Vec<WinEvent>) -> PlayStateResult {

View File

@ -1410,6 +1410,12 @@ impl CharSelectionUi {
} }
} }
pub fn update_language(&mut self, i18n: std::sync::Arc<Localization>) {
self.controls.i18n = i18n;
self.controls.fonts = Fonts::load(&self.controls.i18n.fonts, &mut self.ui)
.expect("Impossible to load fonts!");
}
// TODO: do we need whole client here or just character list? // TODO: do we need whole client here or just character list?
pub fn maintain(&mut self, global_state: &mut GlobalState, client: &mut Client) -> Vec<Event> { pub fn maintain(&mut self, global_state: &mut GlobalState, client: &mut Client) -> Vec<Event> {
let mut events = Vec::new(); let mut events = Vec::new();

View File

@ -46,6 +46,15 @@ impl PlayState for MainMenuState {
{ {
global_state.singleplayer = None; global_state.singleplayer = None;
} }
// Updated localization in case the selected language was changed
let localized_strings = crate::i18n::Localization::load_expect(
&crate::i18n::i18n_asset_key(&global_state.settings.language.selected_language),
);
self.main_menu_ui.update_language(
std::sync::Arc::clone(&localized_strings),
&global_state.settings,
);
} }
fn tick(&mut self, global_state: &mut GlobalState, events: Vec<Event>) -> PlayStateResult { fn tick(&mut self, global_state: &mut GlobalState, events: Vec<Event>) -> PlayStateResult {
@ -258,8 +267,10 @@ impl PlayState for MainMenuState {
&global_state.settings.language.selected_language, &global_state.settings.language.selected_language,
)); ));
localized_strings.log_missing_entries(); localized_strings.log_missing_entries();
self.main_menu_ui self.main_menu_ui.update_language(
.update_language(std::sync::Arc::clone(&localized_strings)); std::sync::Arc::clone(&localized_strings),
&global_state.settings,
);
}, },
#[cfg(feature = "singleplayer")] #[cfg(feature = "singleplayer")]
MainMenuEvent::StartSingleplayer => { MainMenuEvent::StartSingleplayer => {

View File

@ -363,7 +363,6 @@ impl Controls {
}, },
Message::Username(new_value) => self.login_info.username = new_value, Message::Username(new_value) => self.login_info.username = new_value,
Message::LanguageChanged(new_value) => { Message::LanguageChanged(new_value) => {
self.selected_language_index = Some(new_value);
events.push(Event::ChangeLanguage(language_metadatas.remove(new_value))); events.push(Event::ChangeLanguage(language_metadatas.remove(new_value)));
}, },
Message::OpenLanguageMenu => self.is_selecting_language = !self.is_selecting_language, Message::OpenLanguageMenu => self.is_selecting_language = !self.is_selecting_language,
@ -522,10 +521,14 @@ impl<'a> MainMenuUi {
Self { ui, controls } Self { ui, controls }
} }
pub fn update_language(&mut self, i18n: std::sync::Arc<Localization>) { pub fn update_language(&mut self, i18n: std::sync::Arc<Localization>, settings: &Settings) {
self.controls.i18n = i18n; self.controls.i18n = i18n;
self.controls.fonts = Fonts::load(&self.controls.i18n.fonts, &mut self.ui) self.controls.fonts = Fonts::load(&self.controls.i18n.fonts, &mut self.ui)
.expect("Impossible to load fonts!"); .expect("Impossible to load fonts!");
let language_metadatas = crate::i18n::list_localizations();
self.controls.selected_language_index = language_metadatas
.iter()
.position(|f| f.language_identifier == settings.language.selected_language);
} }
pub fn auth_trust_prompt(&mut self, auth_server: String) { pub fn auth_trust_prompt(&mut self, auth_server: String) {

View File

@ -647,8 +647,15 @@ impl Window {
pub fn fetch_events(&mut self) -> Vec<Event> { pub fn fetch_events(&mut self) -> Vec<Event> {
// Refresh ui size (used when changing playstates) // Refresh ui size (used when changing playstates)
if self.needs_refresh_resize { if self.needs_refresh_resize {
let logical_size = self.logical_size();
self.events self.events
.push(Event::Ui(ui::Event::new_resize(self.logical_size()))); .push(Event::Ui(ui::Event::new_resize(logical_size)));
self.events.push(Event::IcedUi(iced::Event::Window(
iced::window::Event::Resized {
width: logical_size.x as u32,
height: logical_size.y as u32,
},
)));
self.needs_refresh_resize = false; self.needs_refresh_resize = false;
} }