From fcb2c42e2aa2055bd22c1536052fac442b6899c2 Mon Sep 17 00:00:00 2001 From: Falko Galperin Date: Sat, 16 Nov 2024 16:44:24 +0100 Subject: [PATCH] Ask user for confirmation before rebinding key mapping (fix #683) --- Assets/SEE/Controls/KeyActions/KeyMap.cs | 14 +++- Assets/SEE/UI/SettingsMenu.cs | 90 ++++++++++++++---------- 2 files changed, 64 insertions(+), 40 deletions(-) diff --git a/Assets/SEE/Controls/KeyActions/KeyMap.cs b/Assets/SEE/Controls/KeyActions/KeyMap.cs index 16a47c4fc0..338487db9e 100644 --- a/Assets/SEE/Controls/KeyActions/KeyMap.cs +++ b/Assets/SEE/Controls/KeyActions/KeyMap.cs @@ -134,8 +134,8 @@ internal void ResetKeyCode(KeyActionDescriptor descriptor, KeyCode keyCode) { if (TryGetKeyAction(keyCode, out KeyAction action)) { - throw new($"Cannot register key {keyCode} for {descriptor.Name}." - + $" Key {keyCode} is already bound to {action}.\n"); + throw new KeyBindingsExistsException($"Cannot register key {keyCode} for {descriptor.Name}." + + $" Key {keyCode} is already bound to {action}.\n"); } else { @@ -351,5 +351,13 @@ IEnumerator IEnumerable.GetEnumerator() } } #endregion + + /// + /// An exception indicating that the attempted rebind would result in a duplicate key binding. + /// + public class KeyBindingsExistsException : Exception + { + public KeyBindingsExistsException(string message) : base(message) { } + }; } -} \ No newline at end of file +} diff --git a/Assets/SEE/UI/SettingsMenu.cs b/Assets/SEE/UI/SettingsMenu.cs index 8ce5e9c412..6aff99b3e9 100644 --- a/Assets/SEE/UI/SettingsMenu.cs +++ b/Assets/SEE/UI/SettingsMenu.cs @@ -1,13 +1,17 @@ -using UnityEngine.UI; -using UnityEngine; -using TMPro; -using SEE.Controls; -using SEE.Utils; -using SEE.GO; using System; using System.Collections.Generic; -using SEE.UI.Notification; +using System.Linq; +using Cysharp.Threading.Tasks; +using SEE.Controls; using SEE.Controls.KeyActions; +using SEE.GO; +using SEE.UI.Menu; +using SEE.UI.Notification; +using SEE.Utils; +using TMPro; +using UnityEditor; +using UnityEngine; +using UnityEngine.UI; namespace SEE.UI { @@ -19,20 +23,20 @@ public class SettingsMenu : PlatformDependentComponent /// /// Prefab for the . /// - private string SettingsPrefab => UIPrefabFolder + "SettingsMenu"; + private const string settingsPrefab = UIPrefabFolder + "SettingsMenu"; /// - /// Prefab for the KeyBindingContent. + /// Prefab for the keyBindingContent. /// - private string KeyBindingContent => UIPrefabFolder + "KeyBindingContent"; + private const string keyBindingContent = UIPrefabFolder + "KeyBindingContent"; /// /// Prefab for the ScrollView. /// - private string ScrollPrefab => UIPrefabFolder + "ScrollPrefab"; + private const string scrollPrefab = UIPrefabFolder + "ScrollPrefab"; /// - /// The game object instantiated for the . + /// The game object instantiated for the . /// private GameObject settingsMenuGameObject; @@ -44,14 +48,14 @@ public class SettingsMenu : PlatformDependentComponent private readonly Dictionary shortNameOfBindingToLabel = new(); /// - /// Sets the and adds the onClick event + /// Sets the and adds the onClick event /// to the ExitButton. /// protected override void StartDesktop() { KeyBindings.LoadKeyBindings(); // instantiates the SettingsMenu - settingsMenuGameObject = PrefabInstantiator.InstantiatePrefab(SettingsPrefab, Canvas.transform, false); + settingsMenuGameObject = PrefabInstantiator.InstantiatePrefab(settingsPrefab, Canvas.transform, false); // adds the ExitGame method to the exit button settingsMenuGameObject.transform.Find("ExitPanel/Buttons/Content/Exit").gameObject.MustGetComponent