Ejemplo n.º 1
0
        /// <summary>
        /// Check whether the provided pressed keys are valid for this <see cref="KeyCombination"/>.
        /// </summary>
        /// <param name="pressedKeys">The potential pressed keys for this <see cref="KeyCombination"/>.</param>
        /// <param name="matchingMode">The method for handling exact key matches.</param>
        /// <returns>Whether the pressedKeys keys are valid.</returns>
        public bool IsPressed(KeyCombination pressedKeys, KeyCombinationMatchingMode matchingMode)
        {
            Debug.Assert(!pressedKeys.Keys.Contains(InputKey.None)); // Having None in pressed keys will break IsPressed

            if (Keys == pressedKeys.Keys)                            // Fast test for reference equality of underlying array
            {
                return(true);
            }

            switch (matchingMode)
            {
            case KeyCombinationMatchingMode.Any:
                return(containsAll(pressedKeys.Keys, Keys, false));

            case KeyCombinationMatchingMode.Exact:
                // Keys are always ordered
                return(pressedKeys.Keys.SequenceEqual(Keys));

            case KeyCombinationMatchingMode.Modifiers:
                return(containsAll(pressedKeys.Keys, Keys, true));

            default:
                return(false);
            }
        }
            public KeyBindingTester(SimultaneousBindingMode concurrency, KeyCombinationMatchingMode matchingMode)
            {
                RelativeSizeAxes = Axes.Both;
                Direction        = FillDirection.Vertical;

                testButtons = Enum.GetValues(typeof(TestAction)).Cast <TestAction>().Select(t =>
                {
                    if (t.ToString().Contains("Wheel"))
                    {
                        return(new ScrollTestButton(t, concurrency));
                    }

                    return(new TestButton(t, concurrency));
                }).ToArray();

                Children = new Drawable[]
                {
                    new SpriteText
                    {
                        Text = $"{concurrency} / {matchingMode}"
                    },
                    new TestInputManager(concurrency, matchingMode)
                    {
                        RelativeSizeAxes = Axes.Both,
                        Child            = new FillFlowContainer
                        {
                            RelativeSizeAxes = Axes.Both,
                            Children         = testButtons
                        }
                    },
                };
            }
        /// <summary>
        /// Create a new instance.
        /// </summary>
        /// <param name="simultaneousMode">Specify how to deal with multiple matches of <see cref="KeyCombination"/>s and <see cref="T"/>s.</param>
        /// <param name="matchingMode">Specify how to deal with exact <see cref="KeyCombination"/> matches.</param>
        protected KeyBindingContainer(SimultaneousBindingMode simultaneousMode = SimultaneousBindingMode.None, KeyCombinationMatchingMode matchingMode = KeyCombinationMatchingMode.Any)
        {
            RelativeSizeAxes = Axes.Both;

            this.simultaneousMode = simultaneousMode;
            this.matchingMode     = matchingMode;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Check whether the provided pressed keys are valid for this <see cref="KeyCombination"/>.
        /// </summary>
        /// <param name="pressedKeys">The potential pressed keys for this <see cref="KeyCombination"/>.</param>
        /// <param name="matchingMode">The method for handling exact key matches.</param>
        /// <returns>Whether the pressedKeys keys are valid.</returns>
        public bool IsPressed(KeyCombination pressedKeys, KeyCombinationMatchingMode matchingMode)
        {
            Debug.Assert(!pressedKeys.Keys.Contains(InputKey.None)); // Having None in pressed keys will break IsPressed

            if (Keys == pressedKeys.Keys)                            // Fast test for reference equality of underlying array
            {
                return(true);
            }

            return(ContainsAll(Keys, pressedKeys.Keys, matchingMode));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Check whether the provided pressed keys are valid for this <see cref="KeyCombination"/>.
        /// </summary>
        /// <param name="pressedKeys">The potential pressed keys for this <see cref="KeyCombination"/>.</param>
        /// <param name="matchingMode">The method for handling exact key matches.</param>
        /// <returns>Whether the pressedKeys keys are valid.</returns>
        public bool IsPressed(KeyCombination pressedKeys, KeyCombinationMatchingMode matchingMode)
        {
            switch (matchingMode)
            {
            case KeyCombinationMatchingMode.Any:
                return(!Keys.Except(pressedKeys.Keys).Any());

            case KeyCombinationMatchingMode.Exact:
                return(pressedKeys.Keys.Count() == Keys.Count() && pressedKeys.Keys.All(Keys.Contains));

            case KeyCombinationMatchingMode.Modifiers:
                if (Keys.Except(pressedKeys.Keys).Any())
                {
                    return(false);
                }
                var pressedModifiers  = pressedKeys.Keys.Where(IsModifierKey);
                var requiredModifiers = Keys.Where(IsModifierKey);
                return(pressedModifiers.Count() == requiredModifiers.Count() && pressedModifiers.All(requiredModifiers.Contains));

            default:
                return(false);
            }
        }
Ejemplo n.º 6
0
        internal static bool ContainsAll(ImmutableArray <InputKey> candidateKey, ImmutableArray <InputKey> pressedKey, KeyCombinationMatchingMode matchingMode)
        {
            // first, check that all the candidate keys are contained in the provided pressed keys.
            // regardless of the matching mode, every key needs to at least be present (matching modes only change
            // the behaviour of excess keys).
            foreach (var key in candidateKey)
            {
                if (!ContainsKey(pressedKey, key))
                {
                    return(false);
                }
            }

            switch (matchingMode)
            {
            case KeyCombinationMatchingMode.Exact:
                foreach (var key in pressedKey)
                {
                    // in exact matching mode, every pressed key needs to be in the candidate.
                    if (!ContainsKeyPermissive(candidateKey, key))
                    {
                        return(false);
                    }
                }

                break;

            case KeyCombinationMatchingMode.Modifiers:
                foreach (var key in pressedKey)
                {
                    // in modifiers match mode, the same check applies as exact but only for modifier keys.
                    if (IsModifierKey(key) && !ContainsKeyPermissive(candidateKey, key))
                    {
                        return(false);
                    }
                }

                break;

            case KeyCombinationMatchingMode.Any:
                // any match mode needs no further checks.
                break;
            }

            return(true);
        }
Ejemplo n.º 7
0
        internal static bool ContainsAll(ImmutableArray <InputKey> candidateKey, ImmutableArray <InputKey> pressedKey, KeyCombinationMatchingMode matchingMode)
        {
            // can be local function once attribute on local functions are implemented
            // optimized to avoid allocation
            // Usually Keys.Count <= 3. Does not worth special logic for Contains().
            foreach (var key in candidateKey)
            {
                if (!ContainsKey(pressedKey, key))
                {
                    return(false);
                }
            }

            switch (matchingMode)
            {
            case KeyCombinationMatchingMode.Exact:
                foreach (var key in pressedKey)
                {
                    if (!ContainsKeyPermissive(candidateKey, key))
                    {
                        return(false);
                    }
                }

                break;

            case KeyCombinationMatchingMode.Modifiers:
                foreach (var key in pressedKey)
                {
                    if (IsModifierKey(key) && !ContainsKeyPermissive(candidateKey, key))
                    {
                        return(false);
                    }
                }

                break;
            }

            return(true);
        }
 public TestInputManager(SimultaneousBindingMode concurrencyMode = SimultaneousBindingMode.None, KeyCombinationMatchingMode matchingMode = KeyCombinationMatchingMode.Any)
     : base(concurrencyMode, matchingMode)
 {
 }
Ejemplo n.º 9
0
 public GlobalActionContainer(KeyCombinationMatchingMode keyCombinationMatchingMode = KeyCombinationMatchingMode.Exact, SimultaneousBindingMode simultaneousBindingMode = SimultaneousBindingMode.All)
     : base(simultaneousBindingMode, keyCombinationMatchingMode)
 {
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Create a new instance.
        /// </summary>
        /// <param name="ruleset">A reference to identify the current <see cref="Ruleset"/>. Used to lookup mappings. Null for global mappings.</param>
        /// <param name="variant">An optional variant for the specified <see cref="Ruleset"/>. Used when a ruleset has more than one possible keyboard layouts.</param>
        /// <param name="simultaneousMode">Specify how to deal with multiple matches of <see cref="KeyCombination"/>s and <typeparamref name="T"/>s.</param>
        /// <param name="matchingMode">Specify how to deal with exact <see cref="KeyCombination"/> matches.</param>
        public DatabasedKeyBindingContainer(RulesetInfo ruleset = null, int?variant = null, SimultaneousBindingMode simultaneousMode = SimultaneousBindingMode.None, KeyCombinationMatchingMode matchingMode = KeyCombinationMatchingMode.Any)
            : base(simultaneousMode, matchingMode)
        {
            this.ruleset = ruleset;
            this.variant = variant;

            if (ruleset != null && variant == null)
            {
                throw new InvalidOperationException($"{nameof(variant)} can not be null when a non-null {nameof(ruleset)} is provided.");
            }
        }
Ejemplo n.º 11
0
 public FileDialogActionContainer(KeyCombinationMatchingMode keyCombinationMatchingMode = Exact, SimultaneousBindingMode simultaneousBindingMode = All)
     : base(simultaneousBindingMode, keyCombinationMatchingMode)
 {
 }
 public void TestLeftRightModifierHandling(KeyCombination candidate, KeyCombination pressed, KeyCombinationMatchingMode matchingMode, bool shouldContain)
 => Assert.AreEqual(shouldContain, KeyCombination.ContainsAll(candidate.Keys, pressed.Keys, matchingMode));
Ejemplo n.º 13
0
 public GaleKeyBindingContainer(KeyCombinationMatchingMode keyCombinationMatchingMode = KeyCombinationMatchingMode.Any,
                                SimultaneousBindingMode simultaneousBindingMode       = SimultaneousBindingMode.All) : base(simultaneousBindingMode, keyCombinationMatchingMode)
 {
 }
Ejemplo n.º 14
0
 public HoloTrackKeyBindingContainer(SimultaneousBindingMode simultaneousMode = SimultaneousBindingMode.None, KeyCombinationMatchingMode matchingMode = KeyCombinationMatchingMode.Modifiers)
     : base(simultaneousMode, matchingMode)
 {
 }