1 #region Copyright & License Information
2 /*
3  * Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
4  * This file is part of OpenRA, which is free software. It is made
5  * available to you under the terms of the GNU General Public License
6  * as published by the Free Software Foundation, either version 3 of
7  * the License, or (at your option) any later version. For more
8  * information, see COPYING.
9  */
10 #endregion
11 
12 using System;
13 
14 namespace OpenRA
15 {
16 	public struct Hotkey : IEquatable<Hotkey>
17 	{
18 		public static Hotkey Invalid = new Hotkey(Keycode.UNKNOWN, Modifiers.None);
IsValidOpenRA.Hotkey19 		public bool IsValid()
20 		{
21 			return Key != Keycode.UNKNOWN;
22 		}
23 
24 		public readonly Keycode Key;
25 		public readonly Modifiers Modifiers;
26 
TryParseOpenRA.Hotkey27 		public static bool TryParse(string s, out Hotkey result)
28 		{
29 			result = Invalid;
30 			if (string.IsNullOrWhiteSpace(s))
31 				return false;
32 
33 			var parts = s.Split(' ');
34 
35 			Keycode key;
36 			if (!Enum<Keycode>.TryParse(parts[0], true, out key))
37 			{
38 				int c;
39 				if (!int.TryParse(parts[0], out c))
40 					return false;
41 				key = (Keycode)c;
42 			}
43 
44 			var mods = Modifiers.None;
45 			if (parts.Length >= 2)
46 			{
47 				var modString = s.Substring(s.IndexOf(' '));
48 				if (!Enum<Modifiers>.TryParse(modString, true, out mods))
49 					return false;
50 			}
51 
52 			result = new Hotkey(key, mods);
53 			return true;
54 		}
55 
FromKeyInputOpenRA.Hotkey56 		public static Hotkey FromKeyInput(KeyInput ki)
57 		{
58 			return new Hotkey(ki.Key, ki.Modifiers);
59 		}
60 
HotkeyOpenRA.Hotkey61 		public Hotkey(Keycode virtKey, Modifiers mod)
62 		{
63 			Key = virtKey;
64 			Modifiers = mod;
65 		}
66 
operator !=OpenRA.Hotkey67 		public static bool operator !=(Hotkey a, Hotkey b) { return !(a == b); }
operator ==OpenRA.Hotkey68 		public static bool operator ==(Hotkey a, Hotkey b)
69 		{
70 			// Unknown keys are never equal
71 			if (a.Key == Keycode.UNKNOWN)
72 				return false;
73 
74 			return a.Key == b.Key && a.Modifiers == b.Modifiers;
75 		}
76 
GetHashCodeOpenRA.Hotkey77 		public override int GetHashCode() { return Key.GetHashCode() ^ Modifiers.GetHashCode(); }
78 
EqualsOpenRA.Hotkey79 		public bool Equals(Hotkey other)
80 		{
81 			return other == this;
82 		}
83 
EqualsOpenRA.Hotkey84 		public override bool Equals(object obj)
85 		{
86 			var o = obj as Hotkey?;
87 			return o != null && o == this;
88 		}
89 
ToStringOpenRA.Hotkey90 		public override string ToString() { return "{0} {1}".F(Key, Modifiers.ToString("F")); }
91 
DisplayStringOpenRA.Hotkey92 		public string DisplayString()
93 		{
94 			var ret = KeycodeExts.DisplayString(Key);
95 
96 			if (Modifiers.HasModifier(Modifiers.Shift))
97 				ret = "Shift + " + ret;
98 
99 			if (Modifiers.HasModifier(Modifiers.Alt))
100 				ret = "Alt + " + ret;
101 
102 			if (Modifiers.HasModifier(Modifiers.Ctrl))
103 				ret = "Ctrl + " + ret;
104 
105 			if (Modifiers.HasModifier(Modifiers.Meta))
106 				ret = (Platform.CurrentPlatform == PlatformType.OSX ? "Cmd + " : "Meta + ") + ret;
107 
108 			return ret;
109 		}
110 	}
111 }
112