Exemple #1
0
        private void OnComposition(IntPtr hWnd, int lParam)
        {
            TextInputEvent compEvent;

            if (lParam == 0)
            {
                // Clear composition
                compEvent = InputEventPool <TextInputEvent> .GetOrCreate(this);

                compEvent.Text              = "";
                compEvent.CompositionStart  = 0;
                compEvent.CompositionLength = 0;
                compEvent.Type              = TextInputEventType.Composition;
                textEvents.Add(compEvent);
                return;
            }

            if ((lParam & Win32Native.GCS_COMPSTR) != 0)
            {
                // Update the composition string
                var context = Win32Native.ImmGetContext(hWnd);

                var compString = GetCompositionString(context, Win32Native.GCS_COMPSTR);

                compEvent = InputEventPool <TextInputEvent> .GetOrCreate(this);

                compEvent.Type              = TextInputEventType.Composition;
                compEvent.Text              = compString;
                compEvent.CompositionStart  = 0;
                compEvent.CompositionLength = 0;
                textEvents.Add(compEvent);

                Win32Native.ImmReleaseContext(hWnd, context);
            }
        }
Exemple #2
0
        public void HandleMouseWheel(float wheelDelta)
        {
            var wheelEvent = InputEventPool <MouseWheelEvent> .GetOrCreate(MouseDevice);

            wheelEvent.WheelDelta = wheelDelta;
            Events.Add(wheelEvent);
        }
Exemple #3
0
        public void SetAxis(GamePadAxis axis, float value)
        {
            var axisEvent = InputEventPool <GamePadAxisEvent> .GetOrCreate(this);

            axisEvent.Axis  = axis;
            axisEvent.Value = value;
            PendingEvents.Add(axisEvent);
        }
Exemple #4
0
        private unsafe void OnTextInputActions(SDL.SDL_TextInputEvent e)
        {
            var textInputEvent = InputEventPool <TextInputEvent> .GetOrCreate(this);

            textInputEvent.Text = SDLBufferToString(e.text);
            textInputEvent.Type = TextInputEventType.Input;
            textEvents.Add(textInputEvent);
        }
Exemple #5
0
        private GamePadAxisEvent CreateAxisEvent(GamePadAxis axis, float newValue)
        {
            GamePadAxisEvent axisEvent = InputEventPool <GamePadAxisEvent> .GetOrCreate(this);

            axisEvent.Value = newValue;
            axisEvent.Axis  = axis;
            state.Update(axisEvent);
            return(axisEvent);
        }
Exemple #6
0
        private unsafe void OnTextEditingActions(SDL.SDL_TextEditingEvent e)
        {
            var textInputEvent = InputEventPool <TextInputEvent> .GetOrCreate(this);

            textInputEvent.Text              = SDLBufferToString(e.text);
            textInputEvent.Type              = TextInputEventType.Composition;
            textInputEvent.CompositionStart  = e.start;
            textInputEvent.CompositionLength = e.length;
            textEvents.Add(textInputEvent);
        }
Exemple #7
0
        /// <summary>
        /// Processes a <see cref="InputEvent"/>, converting it to a <see cref="PointerEvent"/>. Also calls <see cref="OnPointer"/> and updates <see cref="CurrentPointerEvents"/>
        /// </summary>
        /// <param name="evt"></param>
        public PointerEvent ProcessPointerEvent(InputEvent evt)
        {
            var pointerEvent = InputEventPool <PointerEvent> .GetOrCreate(SourceDevice);

            pointerEvent.Position  = evt.Position;
            pointerEvent.PointerId = evt.Id;
            pointerEvent.EventType = evt.Type;
            UpdatePointerState(pointerEvent);

            return(pointerEvent);
        }
Exemple #8
0
        public override void Update(List <InputEvent> inputEvents)
        {
            var reading = Gamepad.GetCurrentReading();

            ClearButtonStates();

            // Check buttons
            for (int i = 0; i < 14; i++)
            {
                int           mask     = 1 << i;
                GamePadButton button   = buttonMap[(GamepadButtons)mask];
                var           oldState = (state.Buttons & button) != 0;
                var           newState = (reading.Buttons & (GamepadButtons)mask) != 0;
                if (oldState != newState)
                {
                    GamePadButtonEvent buttonEvent = InputEventPool <GamePadButtonEvent> .GetOrCreate(this);

                    buttonEvent.IsDown = newState;
                    buttonEvent.Button = button;
                    inputEvents.Add(buttonEvent);
                    if (state.Update(buttonEvent))
                    {
                        UpdateButtonState(buttonEvent);
                    }
                }
            }
            if (ChangeAxis(0, reading.LeftThumbstickX))
            {
                inputEvents.Add(CreateAxisEvent(GamePadAxis.LeftThumbX, reading.LeftThumbstickX));
            }
            if (ChangeAxis(1, reading.LeftThumbstickY))
            {
                inputEvents.Add(CreateAxisEvent(GamePadAxis.LeftThumbY, reading.LeftThumbstickY));
            }

            if (ChangeAxis(2, reading.RightThumbstickX))
            {
                inputEvents.Add(CreateAxisEvent(GamePadAxis.RightThumbX, reading.RightThumbstickX));
            }
            if (ChangeAxis(3, reading.RightThumbstickY))
            {
                inputEvents.Add(CreateAxisEvent(GamePadAxis.RightThumbY, reading.RightThumbstickY));
            }

            if (ChangeAxis(4, reading.LeftTrigger))
            {
                inputEvents.Add(CreateAxisEvent(GamePadAxis.LeftTrigger, reading.LeftTrigger));
            }
            if (ChangeAxis(5, reading.RightTrigger))
            {
                inputEvents.Add(CreateAxisEvent(GamePadAxis.RightTrigger, reading.RightTrigger));
            }
        }
Exemple #9
0
        public void SetButton(GamePadButton button, bool state)
        {
            // Check for only 1 bit
            if ((button & (button - 1)) != 0)
            {
                throw new InvalidOperationException("Can not set more than one button at a time");
            }

            var buttonEvent = InputEventPool <GamePadButtonEvent> .GetOrCreate(this);

            buttonEvent.Button = button;
            buttonEvent.IsDown = state;
            PendingEvents.Add(buttonEvent);
        }
Exemple #10
0
        protected void HandleButton(int index, bool state)
        {
            if (index < 0 || index > ButtonStates.Length)
            {
                throw new IndexOutOfRangeException();
            }

            if (ButtonStates[index] != state)
            {
                ButtonStates[index] = state;
                var buttonEvent = InputEventPool <GameControllerButtonEvent> .GetOrCreate(this);

                buttonEvent.IsDown = state;
                buttonEvent.Index  = index;
                events.Add(buttonEvent);
            }
        }
Exemple #11
0
        protected void HandleAxis(int index, float state)
        {
            if (index < 0 || index > AxisStates.Length)
            {
                throw new IndexOutOfRangeException();
            }

            if (AxisStates[index] != state)
            {
                AxisStates[index] = state;
                var axisEvent = InputEventPool <GameControllerAxisEvent> .GetOrCreate(this);

                axisEvent.Value = state;
                axisEvent.Index = index;
                events.Add(axisEvent);
            }
        }
Exemple #12
0
        protected void HandleDirection(int index, Direction state)
        {
            if (index < 0 || index > DirectionStates.Length)
            {
                throw new IndexOutOfRangeException();
            }

            if (DirectionStates[index] != state)
            {
                DirectionStates[index] = state;
                var directionEvent = InputEventPool <GameControllerDirectionEvent> .GetOrCreate(this);

                directionEvent.Index     = index;
                directionEvent.Direction = state;
                events.Add(directionEvent);
            }
        }
Exemple #13
0
        public void HandleKeyUp(Keys key)
        {
            // Prevent duplicate up events
            if (!KeyRepeats.ContainsKey(key))
            {
                return;
            }

            KeyRepeats.Remove(key);
            downKeys.Remove(key);
            var keyEvent = InputEventPool <KeyEvent> .GetOrCreate(this);

            keyEvent.IsDown      = false;
            keyEvent.Key         = key;
            keyEvent.RepeatCount = 0;
            Events.Add(keyEvent);
        }
Exemple #14
0
        /// <summary>
        /// Special move that generates pointer events with just delta
        /// </summary>
        /// <param name="delta">The movement delta</param>
        public void HandleMouseDelta(Vector2 delta)
        {
            if (delta == Vector2.Zero)
            {
                return;
            }

            // Normalize delta
            delta *= PointerState.InverseSurfaceSize;

            nextDelta += delta;

            var pointerEvent = InputEventPool <PointerEvent> .GetOrCreate(MouseDevice);

            pointerEvent.Position      = Position;
            pointerEvent.DeltaPosition = delta;
            pointerEvent.PointerId     = 0;
            pointerEvent.EventType     = PointerEventType.Moved;

            Events.Add(pointerEvent);
        }
Exemple #15
0
        public void HandleButtonUp(MouseButton button)
        {
            // Prevent duplicate events
            if (!downButtons.Contains(button))
            {
                return;
            }

            downButtons.Remove(button);

            var buttonEvent = InputEventPool <MouseButtonEvent> .GetOrCreate(MouseDevice);

            buttonEvent.Button = button;
            buttonEvent.IsDown = false;
            Events.Add(buttonEvent);

            // Simulate tap on primary mouse button
            if (button == MouseButton.Left)
            {
                HandlePointerUp();
            }
        }
Exemple #16
0
        public void HandleKeyDown(Keys key)
        {
            // Increment repeat count on subsequent down events
            int repeatCount;

            if (KeyRepeats.TryGetValue(key, out repeatCount))
            {
                KeyRepeats[key] = ++repeatCount;
            }
            else
            {
                KeyRepeats.Add(key, repeatCount);
                downKeys.Add(key);
            }

            var keyEvent = InputEventPool <KeyEvent> .GetOrCreate(this);

            keyEvent.IsDown      = true;
            keyEvent.Key         = key;
            keyEvent.RepeatCount = repeatCount;
            Events.Add(keyEvent);
        }
Exemple #17
0
        private void RichTextBoxOnTextChanged(object sender, EventArgs eventArgs)
        {
            // Take all text inserted into the text box and send it as a text event instead
            if (richTextBox.Text.Length > 0)
            {
                // Filter out characters that do not belong in text input
                string inputString = richTextBox.Text;
                inputString = inputString.Replace("\r", "").Replace("\n", "").Replace("\t", "");

                if (inputString.Length > 0)
                {
                    var compEvent = InputEventPool <TextInputEvent> .GetOrCreate(this);

                    compEvent.Type              = TextInputEventType.Input;
                    compEvent.Text              = inputString;
                    compEvent.CompositionStart  = richTextBox.SelectionStart;
                    compEvent.CompositionLength = richTextBox.SelectionLength;
                    textEvents.Add(compEvent);
                }
            }

            richTextBox.Text = "";
        }
Exemple #18
0
        /// <summary>
        /// Maps game controller events to gamepad events
        /// </summary>
        /// <returns>The equivalent gamepad event</returns>
        /// <param name="targetDevice">The gamepad that events are mapped to</param>
        /// <param name="sourceDevice">The game controller that is mapped to a gamepad</param>
        /// <param name="controllerEvent">The controller input event as a source</param>
        /// <param name="target">Target list</param>
        public virtual void MapInputEvent(IGamePadDevice targetDevice, IGameControllerDevice sourceDevice, InputEvent controllerEvent, List <InputEvent> target)
        {
            var buttonEvent = controllerEvent as GameControllerButtonEvent;

            if (buttonEvent != null)
            {
                if (buttonEvent.Index < buttonMap.Count &&
                    buttonMap[buttonEvent.Index] != GamePadButton.None)
                {
                    GamePadButtonEvent buttonEvent1 = InputEventPool <GamePadButtonEvent> .GetOrCreate(targetDevice);

                    buttonEvent1.Button = buttonMap[buttonEvent.Index];
                    buttonEvent1.IsDown = buttonEvent.IsDown;
                    target.Add(buttonEvent1);
                }

                if (buttonEvent.Index < buttonsToTriggerMap.Count &&
                    buttonsToTriggerMap[buttonEvent.Index].Axis != GamePadAxis.None)
                {
                    var mappedAxis = buttonsToTriggerMap[buttonEvent.Index];

                    GamePadAxisEvent axisEvent1 = InputEventPool <GamePadAxisEvent> .GetOrCreate(targetDevice);

                    axisEvent1.Axis  = mappedAxis.Axis;
                    axisEvent1.Value = buttonEvent.IsDown ? 1.0f : 0.0f;
                    if (mappedAxis.Invert)
                    {
                        axisEvent1.Value = -axisEvent1.Value;
                    }
                    target.Add(axisEvent1);
                }
            }
            else
            {
                var axisEvent = controllerEvent as GameControllerAxisEvent;
                if (axisEvent != null)
                {
                    if (axisEvent.Index < axisMap.Count &&
                        axisMap[axisEvent.Index].Axis != GamePadAxis.None)
                    {
                        var mappedAxis = axisMap[axisEvent.Index];

                        GamePadAxisEvent axisEvent1 = InputEventPool <GamePadAxisEvent> .GetOrCreate(targetDevice);

                        axisEvent1.Axis = mappedAxis.Axis;
                        if (mappedAxis.Invert)
                        {
                            axisEvent1.Value = -axisEvent.Value;
                        }
                        else
                        {
                            axisEvent1.Value = axisEvent.Value;
                        }
                        if (mappedAxis.Remap)
                        {
                            axisEvent1.Value = (axisEvent1.Value + 1.0f) * 0.5f;
                            if (axisEvent1.Value < 0.0001f)
                            {
                                axisEvent1.Value = 0.0f;
                            }
                        }

                        target.Add(axisEvent1);
                    }
                    if (axisEvent.Index < axisToButtonMap.Count &&
                        axisToButtonMap[axisEvent.Index] != GamePadButton.None)
                    {
                        GamePadButtonEvent buttonEvent1 = InputEventPool <GamePadButtonEvent> .GetOrCreate(targetDevice);

                        buttonEvent1.Button = axisToButtonMap[axisEvent.Index];
                        buttonEvent1.IsDown = axisEvent.Value > 0.5f;
                        target.Add(buttonEvent1);
                    }
                }
                else if (MapFirstPovToPad)
                {
                    var directionEvent = controllerEvent as GameControllerDirectionEvent;
                    if (directionEvent?.Index == 0)
                    {
                        GamePadButton targetButtons = GameControllerUtils.DirectionToButtons(directionEvent.Direction);

                        // Pad buttons down
                        for (int i = 0; i < 4; i++)
                        {
                            int mask = (1 << i);
                            if (((int)targetDevice.State.Buttons & mask) != ((int)targetButtons & mask))
                            {
                                GamePadButtonEvent buttonEvent1 = InputEventPool <GamePadButtonEvent> .GetOrCreate(targetDevice);

                                buttonEvent1.Button = (GamePadButton)mask;
                                buttonEvent1.IsDown = ((int)targetButtons & mask) != 0;
                                target.Add(buttonEvent1);
                            }
                        }
                    }
                }
            }
        }
Exemple #19
0
        public override void Update(List <InputEvent> inputEvents)
        {
            if ((int)controller.UserIndex != Index)
            {
                SetIndexInternal((int)controller.UserIndex);
            }

            ClearButtonStates();

            if (controller.GetState(out xinputState))
            {
                // DPad/Shoulder/Thumb/Option buttons
                for (int i = 0; i < 16; i++)
                {
                    int mask   = 1 << i;
                    var masked = (int)xinputState.Gamepad.Buttons & mask;
                    if (masked != ((int)state.Buttons & mask))
                    {
                        bool buttonState = (masked != 0);
                        GamePadButtonEvent buttonEvent = InputEventPool <GamePadButtonEvent> .GetOrCreate(this);

                        buttonEvent.IsDown = buttonState;
                        buttonEvent.Button = (GamePadButton)mask; // 1 to 1 mapping with XInput buttons
                        inputEvents.Add(buttonEvent);
                        if (state.Update(buttonEvent))
                        {
                            UpdateButtonState(buttonEvent);
                        }
                    }
                }

                // Axes
                if (ChangeAxis(0, xinputState.Gamepad.LeftThumbX))
                {
                    inputEvents.Add(CreateAxisEvent(GamePadAxis.LeftThumbX, xinputState.Gamepad.LeftThumbX / 32768.0f));
                }
                if (ChangeAxis(1, xinputState.Gamepad.LeftThumbY))
                {
                    inputEvents.Add(CreateAxisEvent(GamePadAxis.LeftThumbY, xinputState.Gamepad.LeftThumbY / 32768.0f));
                }

                if (ChangeAxis(2, xinputState.Gamepad.RightThumbX))
                {
                    inputEvents.Add(CreateAxisEvent(GamePadAxis.RightThumbX, xinputState.Gamepad.RightThumbX / 32768.0f));
                }
                if (ChangeAxis(3, xinputState.Gamepad.RightThumbY))
                {
                    inputEvents.Add(CreateAxisEvent(GamePadAxis.RightThumbY, xinputState.Gamepad.RightThumbY / 32768.0f));
                }

                if (ChangeAxis(4, xinputState.Gamepad.LeftTrigger))
                {
                    inputEvents.Add(CreateAxisEvent(GamePadAxis.LeftTrigger, xinputState.Gamepad.LeftTrigger / 255.0f));
                }
                if (ChangeAxis(5, xinputState.Gamepad.RightTrigger))
                {
                    inputEvents.Add(CreateAxisEvent(GamePadAxis.RightTrigger, xinputState.Gamepad.RightTrigger / 255.0f));
                }
            }

            if (!controller.IsConnected)
            {
                DisconnectAndDispose();
            }
        }