Beispiel #1
0
 /// <summary>
 /// Register new Actions triggered by a keyboard event
 /// </summary>
 /// <param name="action">The action</param>
 /// <param name="inputActivationMode">The input mode that will be needed to make this action fired</param>
 /// <param name="bindingKey">The keybard binding</param>
 public void AddActions(KeyboardTriggeredAction keyboardAction, bool rebind = false)
 {
     if (rebind)
     {
         //Remove the previously added Action
         int nbr = _keyboardActions.RemoveAll(x => x.ActionId == keyboardAction.ActionId && x.TriggerType == keyboardAction.TriggerType);
         logger.Warn("Rebinded KeyboardBinding remove more than one action : {0}", nbr);
     }
     _keyboardActions.Add(keyboardAction);
 }
Beispiel #2
0
        private void ProcessKeyboardStates()
        {
            //Refresh Keyboard states
            _prevKeyboardState = _curKeyboardState;
            _curKeyboardState  = Keyboard.GetState();

            //Check if an action needs to be triggered
            for (int i = 0; i < _keyboardActions.Count; i++)
            {
                _keyboardAction = _keyboardActions[i];
                switch (_keyboardAction.TriggerType)
                {
                case KeyboardTriggerMode.KeyDown:
                    //Set the Action Flag if required
                    if (_curKeyboardState.IsKeyDown(_keyboardAction.Binding))
                    {
                        _bufferedActionsInProgress[_keyboardAction.ActionId].Triggered      = true;
                        _bufferedActionsInProgress[_keyboardAction.ActionId].RaisedSources |= ActionRaisedSources.Keyboard;
                    }
                    break;

                case KeyboardTriggerMode.KeyReleased:
                    //Set start Action Flag if required
                    if (_keyboardAction.WithTimeElapsed &&
                        _prevKeyboardState.IsKeyDown(_keyboardAction.Binding) &&
                        _keyboardAction.StartTimeElapsedInTick == 0)
                    {
                        _keyboardAction.StartTimeElapsedInTick = Stopwatch.GetTimestamp();
                    }

                    if (_prevKeyboardState.IsKeyDown(_keyboardAction.Binding) && _curKeyboardState.IsKeyUp(_keyboardAction.Binding))
                    {
                        _bufferedActionsInProgress[_keyboardAction.ActionId].Triggered      = true;
                        _bufferedActionsInProgress[_keyboardAction.ActionId].RaisedSources |= ActionRaisedSources.Keyboard;
                        if (_keyboardAction.WithTimeElapsed)
                        {
                            _actionTimeElapsedInS = (float)(((Stopwatch.GetTimestamp() - _keyboardAction.StartTimeElapsedInTick) / (float)Stopwatch.Frequency));
                            if (_actionTimeElapsedInS > _keyboardAction.MaxTimeElapsedInS)
                            {
                                _actionTimeElapsedInS = _keyboardAction.MaxTimeElapsedInS;
                            }
                            _bufferedActionsInProgress[_keyboardAction.ActionId].ElapsedTimeInS = _actionTimeElapsedInS;
                            _keyboardAction.StartTimeElapsedInTick = 0;
                        }
                    }
                    break;

                case KeyboardTriggerMode.KeyPressed:
                    //Set the Action Flag if required
                    if (_keyboardAction.WithAutoResetButtonPressed && _keyboardAction.StartTimeAutoResetTick == 0 && _curKeyboardState.IsKeyDown(_keyboardAction.Binding))
                    {
                        //Mouse Button DOWN and autoresetting its ButtonPressed value - Memorize when the time when the mouse has been pressed
                        _keyboardAction.StartTimeAutoResetTick = Stopwatch.GetTimestamp();
                    }

                    if (_keyboardAction.WithAutoResetButtonPressed)
                    {
                        _actionTimeElapsedInS = (float)(((Stopwatch.GetTimestamp() - _keyboardAction.StartTimeAutoResetTick) / (float)Stopwatch.Frequency));
                    }

                    if ((_curKeyboardState.IsKeyDown(_keyboardAction.Binding) && _prevKeyboardState.IsKeyUp(_keyboardAction.Binding)) ||
                        (_keyboardAction.WithAutoResetButtonPressed && (_curKeyboardState.IsKeyDown(_keyboardAction.Binding) && _actionTimeElapsedInS >= _keyboardAction.AutoResetTimeInS))
                        )
                    {
                        _bufferedActionsInProgress[_keyboardAction.ActionId].Triggered      = true;
                        _bufferedActionsInProgress[_keyboardAction.ActionId].RaisedSources |= ActionRaisedSources.Keyboard;
                        if (_prevKeyboardState.IsKeyDown(_keyboardAction.Binding))
                        {
                            _bufferedActionsInProgress[_keyboardAction.ActionId].IsAutoRepeatedEvent = true;
                        }
                        if (_keyboardAction.WithAutoResetButtonPressed)
                        {
                            _keyboardAction.StartTimeAutoResetTick = Stopwatch.GetTimestamp();
                        }
                    }

                    if (_keyboardAction.WithAutoResetButtonPressed && _curKeyboardState.IsKeyUp(_keyboardAction.Binding))
                    {
                        _keyboardAction.StartTimeAutoResetTick = 0;
                    }
                    break;
                }
            }
        }