namespace UIWidgets.Menu { using System; using System.Collections.Generic; using System.Text; using UnityEngine; /// /// Hot key. /// [Serializable] public class HotKey : IEquatable { [SerializeField] HotKeyCode key; /// /// Key. /// public HotKeyCode Key { get { return key; } } [SerializeField] bool ctrl; /// /// Should be control key pressed? /// public bool Ctrl { get { return ctrl; } } [SerializeField] bool alt; /// /// Should be alt key pressed? /// public bool Alt { get { return alt; } } [SerializeField] bool shift; /// /// Should be shift key pressed? /// public bool Shift { get { return shift; } } /// /// Is pressed? /// public bool IsPressed { get { if (!Valid) { return false; } var ctrl = Ctrl == CompatibilityInput.CtrlPressed; var shift = Shift == CompatibilityInput.ShiftPressed; var alt = Alt == CompatibilityInput.AltPressed; var key = CompatibilityInput.IsPressed(Key); return ctrl && shift && alt && key; } } int lastFramePressed; /// /// Is up? /// public bool IsUp { get { if (!Valid) { return false; } if (IsPressed) { lastFramePressed = UtilitiesTime.GetFrameCount(); } return (lastFramePressed > 0) && (lastFramePressed == (UtilitiesTime.GetFrameCount() - 1)); } } /// /// Is hot key valid? /// public bool Valid { get { return key != HotKeyCode.None; } } /// /// Modifiers count. /// public int Modifiers { get { var result = 0; if (ctrl) { result += 1; } if (alt) { result += 1; } if (shift) { result += 1; } return result; } } /// /// Initializes a new instance of the class. /// /// Key. /// Should be control key pressed? /// Should be alt key pressed? /// Should be shift key pressed? public HotKey(HotKeyCode key, bool ctrl, bool alt, bool shift) { this.key = key; this.ctrl = ctrl; this.alt = alt; this.shift = shift; lastFramePressed = -2; } /// /// Determines whether the specified object is equal to the current object. /// /// The object to compare with the current object. /// true if the specified object is equal to the current object; otherwise, false. public override bool Equals(object obj) { if (obj is HotKey) { return Equals((HotKey)obj); } return false; } /// /// Determines whether the specified object is equal to the current object. /// /// The object to compare with the current object. /// true if the specified object is equal to the current object; otherwise, false. public bool Equals(HotKey other) { if (ReferenceEquals(other, null)) { return false; } return (key == other.key) && (ctrl == other.ctrl) && (shift == other.shift) && (alt == other.alt); } /// /// Hash function. /// /// A hash code for the current object. public override int GetHashCode() { var code = EqualityComparer.Default.GetHashCode(key); return code ^ ctrl.GetHashCode() ^ shift.GetHashCode() ^ alt.GetHashCode(); } /// /// Compare specified hot keys. /// /// First hot key. /// Second hot key. /// true if the hot keys are equal; otherwise, false. public static bool operator ==(HotKey hotkey1, HotKey hotkey2) { if (ReferenceEquals(hotkey1, null)) { return ReferenceEquals(hotkey2, null); } return hotkey1.Equals(hotkey2); } /// /// Compare specified hot keys. /// /// First hot key. /// Second hot key. /// true if the hot keys not equal; otherwise, false. public static bool operator !=(HotKey hotkey1, HotKey hotkey2) { return !(hotkey1 == hotkey2); } /// /// Returns a string that represents the current object. /// /// A string that represents the current object. public override string ToString() { if (!Valid) { return string.Empty; } var s = new StringBuilder(); if (ctrl) { s.Append("Ctrl+"); } if (shift) { s.Append("Shift+"); } if (alt) { s.Append("Alt+"); } s.Append(key.ToHumanString()); return s.ToString(); } } }