From b3ffc6111992077a1b7b3824fa2ff34804d6b61d Mon Sep 17 00:00:00 2001 From: timokoesters Date: Tue, 7 May 2019 19:50:53 +0200 Subject: [PATCH] Don't let widgets have mutable access to `show` Former-commit-id: 4c525c60d25bcc6551ddeabd325992fcd513619d --- voxygen/src/hud/minimap.rs | 12 ++++++------ voxygen/src/hud/mod.rs | 16 ++++++++++++---- voxygen/src/hud/settings_window.rs | 13 ++++++++----- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/voxygen/src/hud/minimap.rs b/voxygen/src/hud/minimap.rs index 00a8a80648..34cc17bd55 100644 --- a/voxygen/src/hud/minimap.rs +++ b/voxygen/src/hud/minimap.rs @@ -1,10 +1,10 @@ use conrod_core::{ color, - widget::{self, Text, Button, Image, Rectangle}, + widget::{self, Button, Image, Rectangle, Text}, widget_ids, Colorable, Positionable, Sizeable, Widget, WidgetCommon, }; -use super::{TEXT_COLOR, Show, font_ids::Fonts, img_ids::Imgs}; +use super::{font_ids::Fonts, img_ids::Imgs, Show, TEXT_COLOR}; widget_ids! { struct Ids { @@ -17,7 +17,7 @@ widget_ids! { #[derive(WidgetCommon)] pub struct MiniMap<'a> { - show: &'a mut Show, + show: &'a Show, imgs: &'a Imgs, fonts: &'a Fonts, @@ -27,7 +27,7 @@ pub struct MiniMap<'a> { } impl<'a> MiniMap<'a> { - pub fn new(show: &'a mut Show, imgs: &'a Imgs, fonts: &'a Fonts) -> Self { + pub fn new(show: &'a Show, imgs: &'a Imgs, fonts: &'a Fonts) -> Self { Self { show, imgs, @@ -42,7 +42,7 @@ pub struct State { } pub enum Event { - Close, + Toggle, } impl<'a> Widget for MiniMap<'a> { @@ -99,7 +99,7 @@ impl<'a> Widget for MiniMap<'a> { .set(state.ids.mmap_button, ui) .was_clicked() { - self.show.toggle_mini_map(); + return Some(Event::Toggle); } // Title diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index c8972f149a..8a89eb6c6f 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -1,12 +1,12 @@ mod bag; mod buttons; mod character_window; -mod minimap; mod chat; mod esc_menu; mod font_ids; mod img_ids; mod map; +mod minimap; mod settings_window; mod skillbar; mod small_window; @@ -14,12 +14,12 @@ mod small_window; use bag::Bag; use buttons::Buttons; use character_window::CharacterWindow; -use minimap::MiniMap; use chat::Chat; use esc_menu::EscMenu; use font_ids::Fonts; use img_ids::Imgs; use map::Map; +use minimap::MiniMap; use settings_window::SettingsWindow; use skillbar::Skillbar; use small_window::{SmallWindow, SmallWindowType}; @@ -320,7 +320,10 @@ impl Hud { } // MiniMap - MiniMap::new(&mut self.show, &self.imgs, &self.fonts).set(self.ids.minimap, ui_widgets); + match MiniMap::new(&self.show, &self.imgs, &self.fonts).set(self.ids.minimap, ui_widgets) { + Some(minimap::Event::Toggle) => self.show.toggle_mini_map(), + None => {} + } // Bag contents if self.show.bag { @@ -356,9 +359,14 @@ impl Hud { // Settings if let Windows::Settings = self.show.open_windows { - match SettingsWindow::new(&mut self.show, &self.imgs, &self.fonts) + match SettingsWindow::new(&self.show, &self.imgs, &self.fonts) .set(self.ids.settings_window, ui_widgets) { + Some(settings_window::Event::ToggleHelp) => self.show.toggle_help(), + Some(settings_window::Event::ToggleInventoryTestButton) => { + self.show.inventory_test_button = !self.show.inventory_test_button + } + Some(settings_window::Event::ToggleDebug) => self.show.debug = !self.show.debug, Some(settings_window::Event::Close) => { self.show.open_windows = Windows::None; } diff --git a/voxygen/src/hud/settings_window.rs b/voxygen/src/hud/settings_window.rs index 72fa9a6583..1a57cce990 100644 --- a/voxygen/src/hud/settings_window.rs +++ b/voxygen/src/hud/settings_window.rs @@ -47,7 +47,7 @@ enum SettingsTab { #[derive(WidgetCommon)] pub struct SettingsWindow<'a> { - show: &'a mut Show, + show: &'a Show, imgs: &'a Imgs, fonts: &'a Fonts, @@ -57,7 +57,7 @@ pub struct SettingsWindow<'a> { } impl<'a> SettingsWindow<'a> { - pub fn new(show: &'a mut Show, imgs: &'a Imgs, fonts: &'a Fonts) -> Self { + pub fn new(show: &'a Show, imgs: &'a Imgs, fonts: &'a Fonts) -> Self { Self { show, imgs, @@ -74,6 +74,9 @@ pub struct State { } pub enum Event { + ToggleHelp, + ToggleInventoryTestButton, + ToggleDebug, Close, } @@ -177,7 +180,7 @@ impl<'a> Widget for SettingsWindow<'a> { .set(state.ids.button_help, ui); if self.show.help != show_help { - self.show.toggle_help(); + return Some(Event::ToggleHelp); } Text::new("Show Help") @@ -201,7 +204,7 @@ impl<'a> Widget for SettingsWindow<'a> { .set(state.ids.inventory_test_button, ui); if self.show.inventory_test_button != inventory_test_button { - self.show.inventory_test_button = inventory_test_button; + return Some(Event::ToggleInventoryTestButton); } Text::new("Show Inventory Test Button") @@ -222,7 +225,7 @@ impl<'a> Widget for SettingsWindow<'a> { .set(state.ids.debug_button, ui); if self.show.debug != show_debug { - self.show.debug = show_debug; + return Some(Event::ToggleDebug); } Text::new("Show Debug Window")