Initial commit: OpenRA game engine
Fork from OpenRA/OpenRA with one-click launch script (start-ra.cmd) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
104
OpenRA.Game/Input/Hotkey.cs
Normal file
104
OpenRA.Game/Input/Hotkey.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright (c) The OpenRA Developers and Contributors
|
||||
* 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
|
||||
{
|
||||
public readonly struct Hotkey : IEquatable<Hotkey>
|
||||
{
|
||||
public static Hotkey Invalid = new(Keycode.UNKNOWN, Modifiers.None);
|
||||
public bool IsValid()
|
||||
{
|
||||
return Key != Keycode.UNKNOWN;
|
||||
}
|
||||
|
||||
public readonly Keycode Key;
|
||||
public readonly Modifiers Modifiers;
|
||||
|
||||
public static bool TryParse(string s, out Hotkey result)
|
||||
{
|
||||
result = Invalid;
|
||||
if (s == null)
|
||||
return false;
|
||||
|
||||
Span<Range> ranges = stackalloc Range[2];
|
||||
var span = s.AsSpan();
|
||||
var count = span.Split(ranges, ' ');
|
||||
if (count == 0)
|
||||
return false;
|
||||
|
||||
if (!Enum.TryParse(span[ranges[0]], true, out Keycode key))
|
||||
return false;
|
||||
|
||||
var mods = Modifiers.None;
|
||||
if (count == 2 && !Enum.TryParse(span[ranges[1]], true, out mods))
|
||||
return false;
|
||||
|
||||
result = new Hotkey(key, mods);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Hotkey FromKeyInput(KeyInput ki)
|
||||
{
|
||||
return new Hotkey(ki.Key, ki.Modifiers);
|
||||
}
|
||||
|
||||
public Hotkey(Keycode virtKey, Modifiers mod)
|
||||
{
|
||||
Key = virtKey;
|
||||
Modifiers = mod;
|
||||
}
|
||||
|
||||
public static bool operator !=(Hotkey a, Hotkey b) { return !(a == b); }
|
||||
public static bool operator ==(Hotkey a, Hotkey b)
|
||||
{
|
||||
// Unknown keys are never equal
|
||||
if (a.Key == Keycode.UNKNOWN)
|
||||
return false;
|
||||
|
||||
return a.Key == b.Key && a.Modifiers == b.Modifiers;
|
||||
}
|
||||
|
||||
public override int GetHashCode() { return Key.GetHashCode() ^ Modifiers.GetHashCode(); }
|
||||
|
||||
public bool Equals(Hotkey other)
|
||||
{
|
||||
return other == this;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return obj is Hotkey o && (Hotkey?)o == this;
|
||||
}
|
||||
|
||||
public override string ToString() { return $"{Key} {Modifiers:F}"; }
|
||||
|
||||
public string DisplayString()
|
||||
{
|
||||
var ret = KeycodeExts.DisplayString(Key);
|
||||
|
||||
if (Modifiers.HasModifier(Modifiers.Shift))
|
||||
ret = $"{ModifiersExts.DisplayString(Modifiers.Shift)} + {ret}";
|
||||
|
||||
if (Modifiers.HasModifier(Modifiers.Alt))
|
||||
ret = $"{ModifiersExts.DisplayString(Modifiers.Alt)} + {ret}";
|
||||
|
||||
if (Modifiers.HasModifier(Modifiers.Ctrl))
|
||||
ret = $"{ModifiersExts.DisplayString(Modifiers.Ctrl)} + {ret}";
|
||||
|
||||
if (Modifiers.HasModifier(Modifiers.Meta))
|
||||
ret = $"{ModifiersExts.DisplayString(Modifiers.Meta)} + {ret}";
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user