Introduce NamedHotkey type.

This commit is contained in:
Paul Chote
2017-08-13 10:44:45 +00:00
committed by reaperrr
parent 13a06c6e8d
commit 42bf232b37
4 changed files with 63 additions and 0 deletions

View File

@@ -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;

View File

@@ -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
{
/// <summary>
/// A reference to either a named hotkey (defined in the game settings) or a statically assigned hotkey
/// </summary>
public class NamedHotkey
{
static readonly Func<Hotkey> Invalid = () => Hotkey.Invalid;
readonly Func<Hotkey> 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();
}
}
}

View File

@@ -223,6 +223,7 @@
<Compile Include="Input\InputHandler.cs" />
<Compile Include="Input\Keycode.cs" />
<Compile Include="Input\Hotkey.cs" />
<Compile Include="Input\NamedHotkey.cs" />
<Compile Include="Graphics\PlatformInterfaces.cs" />
<Compile Include="Sound\Sound.cs" />
<Compile Include="Sound\SoundDevice.cs" />

View File

@@ -307,6 +307,15 @@ namespace OpenRA
Expression.Field(keySettings, "{0}{1:D2}Key".F(prefix, i + 1)), keySettings).Compile());
}
internal Func<Hotkey> 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);