From 50c6518d89d57f479230fbca2f9975e7f72a17fa Mon Sep 17 00:00:00 2001
From: Yeedo <yeedo@tutanota.com>
Date: Thu, 25 Apr 2019 14:10:01 +0100
Subject: [PATCH] Update help window to use player's keybindings (fix #42)

Former-commit-id: 5f35a153f05b48ae8c21ae9735080c5c7392e2dc
---
 voxygen/src/hud/mod.rs | 48 ++++++++++++++++++++++++++++++------------
 voxygen/src/window.rs  | 12 +++++++++++
 2 files changed, 47 insertions(+), 13 deletions(-)

diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs
index a8d1f55c48..25293d1c36 100644
--- a/voxygen/src/hud/mod.rs
+++ b/voxygen/src/hud/mod.rs
@@ -2,6 +2,7 @@ mod chat;
 
 use crate::{
     render::Renderer,
+    settings::{Settings, ControlSettings},
     ui::{self, ScaleMode, ToggleButton, Ui},
     window::{Event as WinEvent, Key, Window},
     GlobalState,
@@ -479,6 +480,7 @@ pub struct Hud {
     mana_percentage: f64,
     inventorytest_button: bool,
     settings_tab: SettingsTab,
+    help_text: String,
 }
 
 //#[inline]
@@ -530,6 +532,7 @@ impl Hud {
             xp_percentage: 0.4,
             hp_percentage: 1.0,
             mana_percentage: 1.0,
+            help_text: get_help_text(&Settings::default().controls),
         }
     }
 
@@ -597,25 +600,14 @@ impl Hud {
         if self.show_help {
             Image::new(self.imgs.window_frame_2)
                 .top_left_with_margins_on(ui_widgets.window, 3.0, 3.0)
-                .w_h(300.0, 190.0)
+                .w_h(300.0, 450.0)
                 .set(self.ids.help_bg, ui_widgets);
-
-            Text::new(
-                "Tab = Free Cursor       \n\
-                 Esc = Open/Close Menus  \n\
-                 \n\
-                 F1 = Toggle this Window \n\
-                 F2 = Toggle Interface   \n\
-                 \n\
-                 Enter = Open Chat       \n\
-                 Mouse Wheel = Scroll Chat"
-            )
+            Text::new(self.help_text.as_str())
                 .color(TEXT_COLOR)
                 .top_left_with_margins_on(self.ids.help_bg, 20.0, 20.0)
                 .font_id(self.font_opensans)
                 .font_size(18)
                 .set(self.ids.help, ui_widgets);
-
             // X-button
             if Button::image(self.imgs.close_button)
                 .w_h(100.0 * 0.2, 100.0 * 0.2)
@@ -1771,6 +1763,10 @@ impl Hud {
                 _ => self.typing(),
             },
             WinEvent::Char(_) => self.typing(),
+            WinEvent::SettingsChanged => {
+                self.help_text = get_help_text(&global_state.settings.controls);
+                true
+            },
             _ => false,
         }
     }
@@ -1785,3 +1781,29 @@ impl Hud {
         self.ui.render(renderer);
     }
 }
+
+fn get_help_text(cs: &ControlSettings) -> String {
+    [
+        format!("{:?} = Free cursor\n", cs.toggle_cursor),
+        format!("{:?} = Open/close menus\n", cs.escape),
+        String::from("\n"),
+        format!("{:?} = Toggle this window\n", cs.help),
+        format!("{:?} = Toggle interface\n", cs.toggle_interface),
+        String::from("\n"),
+        format!("{:?} = Open chat\n", cs.enter),
+        String::from("Mouse Wheel = Scroll chat\n"),
+        String::from("\n"),
+        format!("{:?} = Move forward\n", cs.move_forward),
+        format!("{:?} = Move left\n", cs.move_left),
+        format!("{:?} = Move right\n", cs.move_right),
+        format!("{:?} = Move backwards\n", cs.move_back),
+        String::from("\n"),
+        format!("{:?} = Map\n", cs.map),
+        format!("{:?} = Bag\n", cs.bag),
+        format!("{:?} = Quest log\n", cs.quest_log),
+        format!("{:?} = Character window\n", cs.character_window),
+        format!("{:?} = Social\n", cs.social),
+        format!("{:?} = Spellbook\n", cs.spellbook),
+        format!("{:?} = Settings\n", cs.settings)
+    ].concat()
+}
\ No newline at end of file
diff --git a/voxygen/src/window.rs b/voxygen/src/window.rs
index f13a515455..bec293bc14 100644
--- a/voxygen/src/window.rs
+++ b/voxygen/src/window.rs
@@ -11,6 +11,7 @@ pub struct Window {
     window: glutin::GlWindow,
     cursor_grabbed: bool,
     needs_refresh_resize: bool,
+    settings_changed: bool,
     key_map: HashMap<glutin::VirtualKeyCode, Key>,
 }
 
@@ -59,6 +60,7 @@ impl Window {
             window,
             cursor_grabbed: false,
             needs_refresh_resize: false,
+            settings_changed: true,
             key_map,
         });
         tmp
@@ -79,6 +81,10 @@ impl Window {
             self.needs_refresh_resize = false;
         }
 
+        if self.settings_changed {
+            events.push(Event::SettingsChanged);
+        }
+
         // Copy data that is needed by the events closure to avoid lifetime errors
         // TODO: Remove this if/when the compiler permits it
         let cursor_grabbed = self.cursor_grabbed;
@@ -161,6 +167,10 @@ impl Window {
             .into();
         Vec2::new(w, h)
     }
+    
+    pub fn settings_changed(&mut self) {
+        self.settings_changed = true;
+    }
 }
 
 /// Represents a key that the game recognises after keyboard mapping
@@ -203,4 +213,6 @@ pub enum Event {
     KeyUp(Key),
     /// Event that the ui uses
     Ui(ui::Event),
+    /// Game settings have changed
+    SettingsChanged,
 }