Beispiel #1
0
        private void OnKeyEvent(object sender, KeyEventArgs <TKeyId, TAdditionalKeyEventData> eventArgs)
        {
            if (eventArgs.EventType == KeyEventType.Repeat)
            {
                return;
            }

            var isPositiveKey = eventArgs.KeyId.Equals(_positiveDirectionKeyId);

            if (!isPositiveKey && !eventArgs.KeyId.Equals(_negativeDirectionKeyId))
            {
                return; // Ignore anything besides our two keys
            }

            var newMovementDirection = MovementDirection;

            if (eventArgs.EventType == KeyEventType.Pressed)
            {
                if (MovementDirection == BidirectionalMovement.Still)
                {
                    newMovementDirection = isPositiveKey ? BidirectionalMovement.Positive : BidirectionalMovement.Negatize;
                }
            }
            else if (eventArgs.EventType == KeyEventType.Released)
            {
                if (MovementDirection != BidirectionalMovement.Still)
                {
                    var isMovementForward = MovementDirection == BidirectionalMovement.Positive;

                    if (isPositiveKey == isMovementForward)
                    {
                        newMovementDirection = BidirectionalMovement.Still; // Stop only if the correct direction key was pressed.
                    }
                }
            }

            if (MovementDirection != newMovementDirection)
            {
                MovementDirection = newMovementDirection;

                if (OnDirectionChange != null)
                {
                    OnDirectionChange(this, new BidirectionalMovementKeysEventArgs(newMovementDirection));
                }
            }
        }
 public BidirectionalMovementKeysEventArgs(BidirectionalMovement newDirection)
 {
     _newDirection = newDirection;
 }
 private void AssertEventAndReset(BidirectionalMovement expectedState)
 {
     Assert.That(_keyEvent, Is.Not.Null);
     Assert.That(_keyEvent.NewDirection, Is.EqualTo(expectedState));
     ResetKeyEvent();
 }