Add HotkeyManager class.

This commit is contained in:
Paul Chote
2017-12-06 20:07:15 +00:00
committed by abcdefg30
parent 811427adc4
commit f98907f42e
6 changed files with 122 additions and 2 deletions

View File

@@ -0,0 +1,40 @@
#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.Collections.Generic;
using System.Linq;
namespace OpenRA
{
public sealed class HotkeyDefinition
{
public readonly string Name;
public readonly Hotkey Default = Hotkey.Invalid;
public readonly string Description = "";
public readonly HashSet<string> Types = new HashSet<string>();
public HotkeyDefinition(string name, MiniYaml node)
{
Name = name;
if (!string.IsNullOrEmpty(node.Value))
Default = FieldLoader.GetValue<Hotkey>("value", node.Value);
var descriptionNode = node.Nodes.FirstOrDefault(n => n.Key == "Description");
if (descriptionNode != null)
Description = descriptionNode.Value.Value;
var typesNode = node.Nodes.FirstOrDefault(n => n.Key == "Types");
if (typesNode != null)
Types = FieldLoader.GetValue<HashSet<string>>("Types", typesNode.Value.Value);
}
}
}

View File

@@ -0,0 +1,69 @@
#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;
using System.Collections.Generic;
using OpenRA.FileSystem;
namespace OpenRA
{
public sealed class HotkeyManager
{
readonly KeySettings settings;
readonly Dictionary<string, HotkeyDefinition> keys = new Dictionary<string, HotkeyDefinition>();
public HotkeyManager(IReadOnlyFileSystem fileSystem, KeySettings settings, Manifest manifest)
{
this.settings = settings;
var keyDefinitions = MiniYaml.Load(fileSystem, manifest.Hotkeys, null);
foreach (var kd in keyDefinitions)
keys[kd.Key] = new HotkeyDefinition(kd.Key, kd.Value);
}
internal Func<Hotkey> GetHotkeyReference(string name)
{
var ret = settings.GetHotkeyReference(name);
if (ret != null)
return ret;
HotkeyDefinition keyDefinition;
if (keys.TryGetValue(name, out keyDefinition))
return () => keyDefinition.Default;
// Not a mod-defined hotkey, so try and parse as a hardcoded definition
Hotkey key;
if (!Hotkey.TryParse(name, out key))
key = Hotkey.Invalid;
return () => key;
}
public void Set(string name, Hotkey value)
{
var field = settings.GetType().GetField(name + "Key");
if (field == null)
return;
field.SetValue(settings, value);
}
public HotkeyReference this[string name]
{
get
{
return new HotkeyReference(GetHotkeyReference(name));
}
}
public IEnumerable<HotkeyDefinition> Definitions { get { return keys.Values; } }
}
}

View File

@@ -27,6 +27,11 @@ namespace OpenRA
getValue = Invalid;
}
internal HotkeyReference(Func<Hotkey> getValue)
{
this.getValue = getValue;
}
public HotkeyReference(string name, KeySettings settings)
{
// Try parsing the value as a reference to a named hotkey

View File

@@ -59,7 +59,7 @@ namespace OpenRA
Rules, ServerTraits,
Sequences, ModelSequences, Cursors, Chrome, Assemblies, ChromeLayout,
Weapons, Voices, Notifications, Music, Translations, TileSets,
ChromeMetrics, MapCompatibility, Missions;
ChromeMetrics, MapCompatibility, Missions, Hotkeys;
public readonly IReadOnlyDictionary<string, string> Packages;
public readonly IReadOnlyDictionary<string, string> MapFolders;
@@ -72,7 +72,7 @@ namespace OpenRA
readonly string[] reservedModuleNames = { "Metadata", "Folders", "MapFolders", "Packages", "Rules",
"Sequences", "ModelSequences", "Cursors", "Chrome", "Assemblies", "ChromeLayout", "Weapons",
"Voices", "Notifications", "Music", "Translations", "TileSets", "ChromeMetrics", "Missions",
"Voices", "Notifications", "Music", "Translations", "TileSets", "ChromeMetrics", "Missions", "Hotkeys",
"ServerTraits", "LoadScreen", "Fonts", "SupportsMapsFrom", "SoundFormats", "SpriteFormats",
"RequiresMods", "PackageFormats" };
@@ -111,6 +111,7 @@ namespace OpenRA
TileSets = YamlList(yaml, "TileSets");
ChromeMetrics = YamlList(yaml, "ChromeMetrics");
Missions = YamlList(yaml, "Missions");
Hotkeys = YamlList(yaml, "Hotkeys");
ServerTraits = YamlList(yaml, "ServerTraits");

View File

@@ -31,6 +31,7 @@ namespace OpenRA
public readonly ISpriteLoader[] SpriteLoaders;
public readonly ISpriteSequenceLoader SpriteSequenceLoader;
public readonly IModelSequenceLoader ModelSequenceLoader;
public readonly HotkeyManager Hotkeys;
public ILoadScreen LoadScreen { get; private set; }
public CursorProvider CursorProvider { get; private set; }
public FS ModFiles;
@@ -89,6 +90,8 @@ namespace OpenRA
ModelSequenceLoader = (IModelSequenceLoader)modelCtor.Invoke(new[] { this });
ModelSequenceLoader.OnMissingModelError = s => Log.Write("debug", s);
Hotkeys = new HotkeyManager(ModFiles, Game.Settings.Keys, Manifest);
defaultRules = Exts.Lazy(() => Ruleset.LoadDefaults(this));
defaultTileSets = Exts.Lazy(() =>
{

View File

@@ -261,6 +261,8 @@
<Compile Include="UtilityCommands\RegisterModCommand.cs" />
<Compile Include="UtilityCommands\UnregisterModCommand.cs" />
<Compile Include="UtilityCommands\ClearInvalidModRegistrationsCommand.cs" />
<Compile Include="HotkeyManager.cs" />
<Compile Include="HotkeyDefinition.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="FileSystem\Folder.cs" />