Ejemplo n.º 1
0
        public virtual InputState Clone()
        {
            var clone = (InputState)MemberwiseClone();

            clone.Keyboard = Keyboard?.Clone();
            clone.Mouse    = Mouse?.Clone();
            clone.Joystick = Joystick?.Clone();
            return(clone);
        }
Ejemplo n.º 2
0
        public virtual InputState Clone()
        {
            var clone = (InputState)MemberwiseClone();

            clone.Keyboard = Keyboard?.Clone();
            clone.Mouse    = Mouse?.Clone();
            clone.Last     = Last;

            return(clone);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// In order to provide a reliable event system to drawables, we want to ensure that we reprocess input queues (via the
        /// main loop in <see cref="InputManager"/> after each and every button or key change. This allows
        /// correct behaviour in a case where the input queues change based on triggered by a button or key.
        /// </summary>
        /// <param name="newStates">One ore more states which are to be converted to distinct states.</param>
        /// <returns>Processed states such that at most one attribute change occurs between any two consecutive states.</returns>
        private IEnumerable <InputState> createDistinctStates(IEnumerable <InputState> newStates)
        {
            IKeyboardState lastKeyboard = CurrentState.Keyboard;
            IMouseState    lastMouse    = CurrentState.Mouse;

            foreach (var state in newStates)
            {
                if (state.Mouse == null && state.Keyboard == null)
                {
                    // we still want to return at least one state change.
                    yield return(state);
                }

                if (state.Mouse != null)
                {
                    // first we want to create a copy of ourselves without any button changes
                    // this is done only for mouse handlers, as they have positional data we want to handle in a separate pass.
                    var iWithoutButtons = state.Mouse.Clone();

                    for (MouseButton b = 0; b < MouseButton.LastButton; b++)
                    {
                        iWithoutButtons.SetPressed(b, lastMouse?.IsPressed(b) ?? false);
                    }

                    //we start by adding this state to the processed list...
                    var newState = state.Clone();
                    newState.Mouse = lastMouse = iWithoutButtons;
                    yield return(newState);

                    //and then iterate over each button/key change, adding intermediate states along the way.
                    for (MouseButton b = 0; b < MouseButton.LastButton; b++)
                    {
                        if (state.Mouse.IsPressed(b) != (lastMouse?.IsPressed(b) ?? false))
                        {
                            lastMouse = lastMouse?.Clone() ?? new MouseState();

                            //add our single local change
                            lastMouse.SetPressed(b, state.Mouse.IsPressed(b));

                            newState       = state.Clone();
                            newState.Mouse = lastMouse;
                            yield return(newState);
                        }
                    }
                }

                if (state.Keyboard != null)
                {
                    if (lastKeyboard != null)
                    {
                        foreach (var releasedKey in lastKeyboard.Keys.Except(state.Keyboard.Keys))
                        {
                            var newState = state.Clone();
                            newState.Keyboard = lastKeyboard = new KeyboardState {
                                Keys = lastKeyboard.Keys.Where(d => d != releasedKey).ToArray()
                            };
                            yield return(newState);
                        }
                    }

                    foreach (var pressedKey in state.Keyboard.Keys.Except(lastKeyboard?.Keys ?? new Key[] { }))
                    {
                        var newState = state.Clone();
                        newState.Keyboard = lastKeyboard = new KeyboardState {
                            Keys = lastKeyboard?.Keys.Union(new[] { pressedKey }) ?? new[] { pressedKey }
                        };
                        yield return(newState);
                    }
                }
            }
        }
Ejemplo n.º 4
0
 public InputState Clone() => new InputState
 {
     Keyboard = Keyboard?.Clone(),
     Mouse    = Mouse?.Clone(),
     Last     = Last
 };