// Copyright (c) 2007-2017 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; using System.Collections.Generic; using System.Linq; using OpenTK.Input; namespace osu.Game.Input { /// /// Represent a combination of more than one s. /// public class KeyCombination : IEquatable { public readonly IEnumerable Keys; public KeyCombination(params Key[] keys) { Keys = keys; } public KeyCombination(IEnumerable keys) { Keys = keys; } public KeyCombination(string stringRepresentation) { Keys = stringRepresentation.Split(',').Select(s => (Key)int.Parse(s)); } public bool CheckValid(IEnumerable keys, bool requireExactMatch = false) { if (requireExactMatch) return Keys.SequenceEqual(keys); else return !Keys.Except(keys).Any(); } public bool Equals(KeyCombination other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return Keys.SequenceEqual(other.Keys); } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; if (obj.GetType() != GetType()) return false; return Equals((KeyCombination)obj); } public override int GetHashCode() => Keys != null ? Keys.Select(k => k.GetHashCode()).Aggregate((h1, h2) => h1 + h2) : 0; public static implicit operator KeyCombination(Key singleKey) => new KeyCombination(singleKey); public static implicit operator KeyCombination(string stringRepresentation) => new KeyCombination(stringRepresentation); public static implicit operator KeyCombination(Key[] keys) => new KeyCombination(keys); public override string ToString() => Keys.Select(k => ((int)k).ToString()).Aggregate((s1, s2) => $"{s1},{s2}"); } }