From 42bf232b3763df11a84dab1a0242ef19b34ab41f Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 13 Aug 2017 10:44:45 +0000 Subject: [PATCH] Introduce NamedHotkey type. --- OpenRA.Game/FieldLoader.cs | 4 +++ OpenRA.Game/Input/NamedHotkey.cs | 49 ++++++++++++++++++++++++++++++++ OpenRA.Game/OpenRA.Game.csproj | 1 + OpenRA.Game/Settings.cs | 9 ++++++ 4 files changed, 63 insertions(+) create mode 100644 OpenRA.Game/Input/NamedHotkey.cs diff --git a/OpenRA.Game/FieldLoader.cs b/OpenRA.Game/FieldLoader.cs index b9a79760d0..bc45436a1b 100644 --- a/OpenRA.Game/FieldLoader.cs +++ b/OpenRA.Game/FieldLoader.cs @@ -273,6 +273,10 @@ namespace OpenRA return InvalidValueAction(value, fieldType, fieldName); } + else if (fieldType == typeof(NamedHotkey)) + { + return new NamedHotkey(value, Game.Settings.Keys); + } else if (fieldType == typeof(WDist)) { WDist res; diff --git a/OpenRA.Game/Input/NamedHotkey.cs b/OpenRA.Game/Input/NamedHotkey.cs new file mode 100644 index 0000000000..a104f0c187 --- /dev/null +++ b/OpenRA.Game/Input/NamedHotkey.cs @@ -0,0 +1,49 @@ +#region Copyright & License Information +/* + * Copyright 2007-2017 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System; + +namespace OpenRA +{ + /// + /// A reference to either a named hotkey (defined in the game settings) or a statically assigned hotkey + /// + public class NamedHotkey + { + static readonly Func Invalid = () => Hotkey.Invalid; + + readonly Func getValue; + + public NamedHotkey() + { + getValue = Invalid; + } + + public NamedHotkey(string name, KeySettings settings) + { + // Try parsing the value as a reference to a named hotkey + getValue = settings.GetHotkeyReference(name); + + if (getValue == null) + { + // Try parsing the value as a normal (static) hotkey + var staticKey = Hotkey.Invalid; + Hotkey.TryParse(name, out staticKey); + getValue = () => staticKey; + } + } + + public Hotkey GetValue() + { + return getValue(); + } + } +} diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index a809d1c9bb..738ad33d50 100644 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -223,6 +223,7 @@ + diff --git a/OpenRA.Game/Settings.cs b/OpenRA.Game/Settings.cs index fdc00f88aa..4dc9a15a1d 100644 --- a/OpenRA.Game/Settings.cs +++ b/OpenRA.Game/Settings.cs @@ -307,6 +307,15 @@ namespace OpenRA Expression.Field(keySettings, "{0}{1:D2}Key".F(prefix, i + 1)), keySettings).Compile()); } + internal Func GetHotkeyReference(string name) + { + var field = typeof(KeySettings).GetField(name + "Key"); + if (field == null) + return null; + + return () => (Hotkey)field.GetValue(this); + } + public Hotkey GetProductionHotkey(int index) { return GetKey(ProductionKeys, index);