예제 #1
0
        public override bool OnGenericMotionEvent(MotionEvent e)
        {
            if (e.Source == InputSourceType.Joystick)
            {
                Log.Debug(TAG, "OnGenericMotionEvent Joystick");
                var xrange = e.Device?.GetMotionRange(Axis.X, e.Source);
                var x      = e.GetAxisValue(Axis.X);

                var yrange = e.Device?.GetMotionRange(Axis.Y, e.Source);
                var y      = e.GetAxisValue(Axis.Y);

                var zrange = e.Device?.GetMotionRange(Axis.Z, e.Source);
                var z      = e.GetAxisValue(Axis.Z);

                var rzrange = e.Device?.GetMotionRange(Axis.Rz, e.Source);
                var rz      = e.GetAxisValue(Axis.Rz);

                x = (float)MathHelper.mapConstrained(x, xrange.Min, xrange.Max, -1, 1);

                y = (float)MathHelper.mapConstrained(y, yrange.Min, yrange.Max, -1, 1);

                z = (float)MathHelper.mapConstrained(z, zrange.Min, zrange.Max, -1, 1);

                rz = (float)MathHelper.mapConstrained(rz, rzrange.Min, rzrange.Max, -1, 1);

                Log.Debug(TAG, $"OnGenericMotionEvent Joystick  {x} {y} {z} {rz}");
            }

            if (e.Source == InputSourceType.Gamepad)
            {
                Log.Debug(TAG, "OnGenericMotionEvent Gamepad");
            }

            return(base.OnGenericMotionEvent(e));
        }
예제 #2
0
    private static float GetCenteredAxis(
        MotionEvent motionEvent,
        InputDevice device,
        Axis axis)
    {
        var range = device.GetMotionRange(axis, motionEvent.Source);

        // A joystick at rest does not always report an absolute position of
        // (0,0). Use the getFlat() method to determine the range of values
        // bounding the joystick axis center.
        if (range != null)
        {
            float flat  = range.Flat;
            float value = motionEvent.GetAxisValue(axis);

            // Ignore axis values that are within the 'flat' region of the
            // joystick axis center.
            if (Math.Abs(value) > flat)
            {
                return(value);
            }
        }
        else
        {
            return(motionEvent.GetAxisValue(axis));
        }

        return(0);
    }
예제 #3
0
        public override bool OnGenericMotionEvent(MotionEvent e)
        {
            AmazonFireGameController.SetDPad(e.GetAxisValue(Axis.HatX));
            AmazonFireGameController.SetLeftAnalogStick(e.GetAxisValue(Axis.X));

            return(true);
        }
예제 #4
0
        internal static bool OnGenericMotionEvent(MotionEvent e)
        {
            var gamePad = GetGamePad(e.Device);

            if (gamePad == null)
            {
                return(false);
            }

            if (e.Action != MotionEventActions.Move)
            {
                return(false);
            }

            gamePad._leftStick    = new Vector2(e.GetAxisValue(Axis.X), -e.GetAxisValue(Axis.Y));
            gamePad._rightStick   = new Vector2(e.GetAxisValue(Axis.Z), -e.GetAxisValue(Axis.Rz));
            gamePad._leftTrigger  = e.GetAxisValue(Axis.Ltrigger);
            gamePad._rightTrigger = e.GetAxisValue(Axis.Rtrigger);

            if (!gamePad.DPadButtons)
            {
                if (e.GetAxisValue(Axis.HatX) < 0)
                {
                    gamePad._buttons |= Buttons.DPadLeft;
                    gamePad._buttons &= ~Buttons.DPadRight;
                }
                else if (e.GetAxisValue(Axis.HatX) > 0)
                {
                    gamePad._buttons &= ~Buttons.DPadLeft;
                    gamePad._buttons |= Buttons.DPadRight;
                }
                else
                {
                    gamePad._buttons &= ~Buttons.DPadLeft;
                    gamePad._buttons &= ~Buttons.DPadRight;
                }

                if (e.GetAxisValue(Axis.HatY) < 0)
                {
                    gamePad._buttons |= Buttons.DPadUp;
                    gamePad._buttons &= ~Buttons.DPadDown;
                }
                else if (e.GetAxisValue(Axis.HatY) > 0)
                {
                    gamePad._buttons &= ~Buttons.DPadUp;
                    gamePad._buttons |= Buttons.DPadDown;
                }
                else
                {
                    gamePad._buttons &= ~Buttons.DPadUp;
                    gamePad._buttons &= ~Buttons.DPadDown;
                }
            }

            return(true);
        }
예제 #5
0
        public static bool OnGenericMotionEvent(MotionEvent e)
        {
            var gamePad = GetGamePad(e.Device);

            if (gamePad == null)
            {
                return(false);
            }

            if (e.Action != MotionEventActions.Move)
            {
                return(false);
            }

            gamePad._leftStick  = new Vector2(e.GetAxisValue(Axis.X), -e.GetAxisValue(Axis.Y));
            gamePad._rightStick = new Vector2(e.GetAxisValue(Axis.Z), -e.GetAxisValue(Axis.Rz));

            // The code I got from a gist from the monogame community used Ltrigger and Rtrigger,
            // but on my GameSir controller the Gas and Brake were the triggers, so I'm going to do
            // a math.max to pick up the max value:
            gamePad._leftTrigger  = Math.Max(e.GetAxisValue(Axis.Ltrigger), e.GetAxisValue(Axis.Brake));
            gamePad._rightTrigger = Math.Max(e.GetAxisValue(Axis.Rtrigger), e.GetAxisValue(Axis.Gas));

            return(true);
        }
예제 #6
0
        public override bool OnGenericMotionEvent(MotionEvent e)
        {
            if (e.Source != InputSourceType.Touchscreen)
            {
                AmazonFireGameController.SetDPad(e.GetAxisValue(Axis.HatX));
                AmazonFireGameController.SetLeftAnalogStick(e.GetAxisValue(Axis.X));

                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #7
0
 public static void DebugMotionEvent(MotionEvent motionEvent)
 {
     foreach (Axis axis in Enum.GetValues(typeof(Axis)))
     {
         Log.Info(TAG, string.Format("({0}) {1} = {2}", (int)axis, axis, motionEvent.GetAxisValue(axis)));
     }
 }
예제 #8
0
        public bool OnGenericMotionEvent(MotionEvent e)
        {
            if (e.Source == InputSourceType.Joystick && e.Action == MotionEventActions.Move)
            {
                var events = new Dictionary <(GameControllerEventType, string), float>();
                foreach (Axis axisCode in Enum.GetValues(typeof(Axis)))
                {
                    var axisValue = e.GetAxisValue(axisCode);

                    if ((axisCode == Axis.Rx || axisCode == Axis.Ry) &&
                        e.Device.VendorId == 1356 &&
                        (e.Device.ProductId == 2508 || e.Device.ProductId == 1476))
                    {
                        // DualShock 4 hack for the triggers
                        if (!_lastAxisValues.ContainsKey(axisCode) && axisValue == 0.0F)
                        {
                            continue;
                        }

                        axisValue = (axisValue + 1) / 2;
                    }

                    axisValue = AdjustControllerValue(axisValue);

                    if (_lastAxisValues.TryGetValue(axisCode, out float lastValue))
                    {
                        if (AreAlmostEqual(axisValue, lastValue))
                        {
                            // axisValue == lastValue
                            continue;
                        }
                    }

                    _lastAxisValues[axisCode] = axisValue;
                    events[(GameControllerEventType.Axis, axisCode.ToString())] = axisValue;
            public override bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
            {
                float xPosition = e1.GetAxisValue(Axis.X);
                float width     = mParent.mPreviewView.Width;
                float height    = mParent.mPreviewView.Height;

                float xPosNorm = xPosition / width;

                int step =
                    (distanceY <-Math.Abs(distanceX) ? -1 : distanceY> Math.Abs(distanceX) ? 1 : 0);

                // Even on left, odd on right
                Range exposureRange = mParent.mCameraInfo.ExposureState.ExposureCompensationRange;

                if (mParent.mRenderMode == ViewfinderProcessor.ModeNormal)
                {
                    mParent.mAutoExposure += step;
                    mParent.mAutoExposure  = Math.Max((exposureRange.Lower as Integer).IntValue(),
                                                      Math.Min((exposureRange.Upper as Integer).IntValue(), mParent.mAutoExposure));
                }
                else if (xPosNorm > 0.5)
                {
                    mParent.mOddExposure += step;
                    mParent.mOddExposure  = Math.Max(mParent.mEvenExposure,
                                                     Math.Min((exposureRange.Upper as Integer).IntValue(), mParent.mOddExposure));
                }
                else
                {
                    mParent.mEvenExposure += step;
                    mParent.mEvenExposure  = Math.Min(mParent.mOddExposure,
                                                      Math.Max((exposureRange.Lower as Integer).IntValue(), mParent.mEvenExposure));
                }

                return(true);
            }
예제 #10
0
            public override bool OnInterceptTouchEvent(MotionEvent ev)
            {
                if (ev.Action == MotionEventActions.Down)
                {
                    _initialDownY = ev.GetAxisValue(Axis.Y);
                }

                var isBeingDragged = base.OnInterceptTouchEvent(ev);

                if (!isBeingDragged && ev.Action == MotionEventActions.Move && _nestedScrollAccepted && !_nestedScrollCalled)
                {
                    var y  = ev.GetAxisValue(Axis.Y);
                    var dy = (y - _initialDownY) / 2;
                    isBeingDragged = dy > _touchSlop;
                }

                return(isBeingDragged);
            }
예제 #11
0
            private bool HandleAxisChange(MotionEvent e)
            {
                foreach (var axis in axisMap.Keys)
                {
                    axisMap[axis].Value = e.GetAxisValue(axis);
                }

                return(true);
            }
예제 #12
0
    public static GamepadButtons?GetDirectionPressed(InputEvent inputEvent)
    {
        if (!IsDpadDevice(inputEvent))
        {
            return(null);
        }

        GamepadButtons directionPressed = GamepadButtons.None;

        // If the input event is a MotionEvent, check its hat axis values.
        if (inputEvent is MotionEvent)
        {
            // Use the hat axis value to find the D-pad direction
            MotionEvent motionEvent = (MotionEvent)inputEvent;

            float xaxis = motionEvent.GetAxisValue(Axis.HatX);
            float yaxis = motionEvent.GetAxisValue(Axis.HatY);

            // Check if the AXIS_HAT_X value is -1 or 1, and set the D-pad
            // LEFT and RIGHT direction accordingly.
            if (Math.Abs(xaxis - (-1.0f)) < Epsilon)
            {
                directionPressed = directionPressed | GamepadButtons.DPadLeft;
            }
            else if (Math.Abs(xaxis - 1.0f) < Epsilon)
            {
                directionPressed = directionPressed | GamepadButtons.DPadRight;
            }
            // Check if the AXIS_HAT_Y value is -1 or 1, and set the D-pad
            // UP and DOWN direction accordingly.
            if (Math.Abs(yaxis - (-1.0f)) < Epsilon)
            {
                directionPressed = directionPressed | GamepadButtons.DPadUp;
            }
            else if (Math.Abs(yaxis - 1.0f) < Epsilon)
            {
                directionPressed = directionPressed | GamepadButtons.DPadDown;
            }
        }
        return(directionPressed);
    }
예제 #13
0
        public override bool OnGenericMotionEvent(MotionEvent motionEvent)
        {
            int playerNum = OuyaController.getPlayerNumByDeviceId(motionEvent.DeviceId);

            if (playerNum >= 0 && playerNum < 4 &&
                null != m_game &&
                null != m_game.Controllers &&
                playerNum < m_game.Controllers.Count)
            {
                m_game.Controllers[playerNum].Axis[OuyaController.AXIS_LS_X] =
                    motionEvent.GetAxisValue((Axis)OuyaController.AXIS_LS_X);
                m_game.Controllers[playerNum].Axis[OuyaController.AXIS_LS_Y] =
                    motionEvent.GetAxisValue((Axis)OuyaController.AXIS_LS_Y);
                m_game.Controllers[playerNum].Axis[OuyaController.AXIS_RS_X] =
                    motionEvent.GetAxisValue((Axis)OuyaController.AXIS_RS_X);
                m_game.Controllers[playerNum].Axis[OuyaController.AXIS_RS_Y] =
                    motionEvent.GetAxisValue((Axis)OuyaController.AXIS_RS_Y);
                m_game.Controllers[playerNum].Axis[OuyaController.AXIS_L2] =
                    motionEvent.GetAxisValue((Axis)OuyaController.AXIS_L2);
                m_game.Controllers[playerNum].Axis[OuyaController.AXIS_R2] =
                    motionEvent.GetAxisValue((Axis)OuyaController.AXIS_R2);
                base.OnGenericMotionEvent(motionEvent);
            }
            return(true);
        }
예제 #14
0
        //Good info
        //https://developer.android.com/training/game-controllers/controller-input.html#joystick

        /// <summary>
        /// Fire a generic motion event, it should be known to be a joystick event before calling this.
        /// </summary>
        /// <param name="ev"></param>
        /// <returns></returns>
        public void HandleJoystickEvent(MotionEvent ev)
        {
            //You should add history support, but right now joysticks are polling only anyway
            //when you add an event for them make sure to process the history

            var  hatx = ev.GetAxisValue(Axis.HatX);
            var  haty = ev.GetAxisValue(Axis.HatY);
            bool last, current;

            var key = GamepadButtonCode.XInput_DPadUp;

            last = keysDown[(int)key];
            keysDown[(int)key] = current = haty < -0.1f;
            process(last, current, key);

            key  = GamepadButtonCode.XInput_DPadDown;
            last = keysDown[(int)key];
            keysDown[(int)key] = current = haty > 0.1f;
            process(last, current, key);

            key  = GamepadButtonCode.XInput_DPadLeft;
            last = keysDown[(int)key];
            keysDown[(int)key] = current = hatx < -0.1f;
            process(last, current, key);

            key  = GamepadButtonCode.XInput_DPadRight;
            last = keysDown[(int)key];
            keysDown[(int)key] = current = hatx > 0.1f;
            process(last, current, key);

            const int historyPose = -1;

            Vector2 lStick = new Vector2(getCenteredAxis(ev, ev.Device, Axis.X, historyPose),
                                         getCenteredAxis(ev, ev.Device, Axis.Y, historyPose));

            Vector2 rStick = new Vector2(getCenteredAxis(ev, ev.Device, Axis.Z, historyPose),
                                         getCenteredAxis(ev, ev.Device, Axis.Rz, historyPose));

            this.fireMovement(lStick, rStick, ev.GetAxisValue(Axis.Ltrigger), ev.GetAxisValue(Axis.Rtrigger));
        }
예제 #15
0
        public bool OnGenericMotionEvent(MotionEvent e)
        {
            if (e.Source == InputSourceType.Joystick && e.Action == MotionEventActions.Move)
            {
                var events = new Dictionary <(GameControllerEventType, string), float>();
                foreach (Axis axisCode in Enum.GetValues(typeof(Axis)))
                {
                    var axisValue = e.GetAxisValue(axisCode);

                    if ((axisCode == Axis.Rx || axisCode == Axis.Ry) &&
                        e.Device.VendorId == 1356 &&
                        (e.Device.ProductId == 2508 || e.Device.ProductId == 1476))
                    {
                        // DualShock 4 hack for the triggers ([-1:1] -> [0:1])
                        if (!_lastAxisValues.ContainsKey(axisCode) && axisValue == 0.0F)
                        {
                            continue;
                        }

                        axisValue = (axisValue + 1) / 2;
                    }

                    if (e.Device.VendorId == 0x057e &&
                        (/*e.Device.ProductId == 0x2006 || e.Device.ProductId == 0x2007 ||*/ e.Device.ProductId == 0x2009))
                    {
                        // Nintendo Switch Pro controller hack ([-0.69:0.7] -> [-1:1])
                        // 2006 and 2007 are for the Nintendo Joy-Con controller (haven't reported issues with it)
                        axisValue = Math.Min(1, Math.Max(-1, axisValue / 0.69F));
                    }

                    if (e.Device.VendorId == 1118 && e.Device.ProductId == 765 &&
                        axisCode == Axis.Generic1)
                    {
                        // XBox One controller reports a constant value on Generic 1 - filter it out
                        continue;
                    }

                    axisValue = AdjustControllerValue(axisValue);

                    if (_lastAxisValues.TryGetValue(axisCode, out float lastValue))
                    {
                        if (AreAlmostEqual(axisValue, lastValue))
                        {
                            // axisValue == lastValue
                            continue;
                        }
                    }

                    _lastAxisValues[axisCode] = axisValue;
                    events[(GameControllerEventType.Axis, axisCode.ToString())] = axisValue;
예제 #16
0
 public static void DebugOuyaMotionEvent(MotionEvent motionEvent)
 {
     Log.Info(TAG, "OuyaController.AXIS_LS_X value=" + motionEvent.GetAxisValue((Axis)OuyaController.AXIS_LS_X));
     Log.Info(TAG, "OuyaController.AXIS_LS_Y value=" + motionEvent.GetAxisValue((Axis)OuyaController.AXIS_LS_Y));
     Log.Info(TAG, "OuyaController.AXIS_RS_X value=" + motionEvent.GetAxisValue((Axis)OuyaController.AXIS_RS_X));
     Log.Info(TAG, "OuyaController.AXIS_RS_Y value=" + motionEvent.GetAxisValue((Axis)OuyaController.AXIS_RS_Y));
     Log.Info(TAG, "OuyaController.AXIS_L2 value=" + motionEvent.GetAxisValue((Axis)OuyaController.AXIS_L2));
     Log.Info(TAG, "OuyaController.AXIS_R2 value=" + motionEvent.GetAxisValue((Axis)OuyaController.AXIS_R2));
 }
        //Get the centered position for the joystick axis
        private float GetCenteredAxis(MotionEvent e, InputDevice device, Axis axis)
        {
            InputDevice.MotionRange range = device.GetMotionRange(axis, e.Source);
            if (range != null)
            {
                float flat  = range.Flat;
                float value = e.GetAxisValue(axis);
                if (System.Math.Abs(value) > flat)
                {
                    return(value);
                }
            }

            return(0);
        }
예제 #18
0
        internal static void HandleMotionEvent(MotionEvent e)
        {
            int num = TranslateDeviceId(e.DeviceId);

            if (num >= 0)
            {
                m_states[num].Sticks[0]   = new Vector2(e.GetAxisValue(Axis.X), 0f - e.GetAxisValue(Axis.Y));
                m_states[num].Sticks[1]   = new Vector2(e.GetAxisValue(Axis.Z), 0f - e.GetAxisValue(Axis.Rz));
                m_states[num].Triggers[0] = MathUtils.Max(e.GetAxisValue(Axis.Ltrigger), e.GetAxisValue(Axis.Brake));
                m_states[num].Triggers[1] = MathUtils.Max(e.GetAxisValue(Axis.Rtrigger), e.GetAxisValue(Axis.Gas));
                float axisValue  = e.GetAxisValue(Axis.HatX);
                float axisValue2 = e.GetAxisValue(Axis.HatY);
                m_states[num].Buttons[10] = (axisValue < -0.5f);
                m_states[num].Buttons[12] = (axisValue > 0.5f);
                m_states[num].Buttons[11] = (axisValue2 < -0.5f);
                m_states[num].Buttons[13] = (axisValue2 > 0.5f);
            }
        }
예제 #19
0
        private static bool IsInContact(PointerDeviceType type, MotionEvent nativeEvent)
        {
            switch (type)
            {
            case PointerDeviceType.Mouse:
                return(nativeEvent.ButtonState != 0);

            case PointerDeviceType.Pen:
                return(nativeEvent.GetAxisValue(Axis.Distance, nativeEvent.ActionIndex) == 0);

            default:
            case PointerDeviceType.Touch:
                return(nativeEvent.Action.HasFlag(MotionEventActions.Down) ||
                       nativeEvent.Action.HasFlag(MotionEventActions.PointerDown) ||
                       nativeEvent.Action.HasFlag(MotionEventActions.Move));
            }
        }
        private static bool IsInContact(MotionEvent nativeEvent, PointerDeviceType pointerType, MotionEventActions action, MotionEventButtonState buttons)
        {
            switch (pointerType)
            {
            case PointerDeviceType.Mouse:
                // For mouse, we cannot only rely on action: We will get a "HoverExit" when we press the left button.
                return(buttons != 0);

            case PointerDeviceType.Pen:
                return(nativeEvent.GetAxisValue(Axis.Distance, nativeEvent.ActionIndex) == 0);

            default:
            case PointerDeviceType.Touch:
                // WARNING: MotionEventActions.Down == 0, so action.HasFlag(MotionEventActions.Up) is always true!
                return(!action.HasFlag(MotionEventActions.Up) &&
                       !action.HasFlag(MotionEventActions.PointerUp) &&
                       !action.HasFlag(MotionEventActions.Cancel));
            }
        }
예제 #21
0
        internal static bool OnGenericMotionEvent(MotionEvent e)
        {
            var gamePad = GetGamePad(e.Device);

            if (gamePad == null)
            {
                return(false);
            }

            if (e.Action != MotionEventActions.Move)
            {
                return(false);
            }

            gamePad._leftStick    = new Vector2(e.GetAxisValue(Axis.X), -e.GetAxisValue(Axis.Y));
            gamePad._rightStick   = new Vector2(e.GetAxisValue(Axis.Z), -e.GetAxisValue(Axis.Rz));
            gamePad._leftTrigger  = e.GetAxisValue(Axis.Ltrigger);
            gamePad._rightTrigger = e.GetAxisValue(Axis.Rtrigger);

            return(true);
        }
예제 #22
0
 private Vector2 getEventScroll(MotionEvent e) =>
 // Android reports horizontal scroll opposite of what framework expects.
 new Vector2(-e.GetAxisValue(Axis.Hscroll), e.GetAxisValue(Axis.Vscroll));
예제 #23
0
 private Vector2 getEventScroll(MotionEvent e) => new Vector2(e.GetAxisValue(Axis.Hscroll), e.GetAxisValue(Axis.Vscroll));
예제 #24
0
            bool OnTouchOrHoverEvent(MotionEvent e, bool isTouch)
            {
                MotionEventButtonState buttonState    = e.ButtonState;
                MotionEventButtonState pressedButtons = buttonState & ~mOldButtonState;

                mOldButtonState = buttonState;

                if ((pressedButtons & MotionEventButtonState.Secondary) != 0)
                {
                    // Advance color when the right mouse button or first stylus button
                    // is pressed.
                    AdvanceColor();
                }

                PaintMode mode;

                if ((buttonState & MotionEventButtonState.Tertiary) != 0)
                {
                    // Splat paint when the middle mouse button or second stylus button is pressed.
                    mode = PaintMode.Splat;
                }
                else if (isTouch || (buttonState & MotionEventButtonState.Primary) != 0)
                {
                    // Draw paint when touching or if the primary button is pressed.
                    mode = PaintMode.Draw;
                }
                else
                {
                    // Otherwise, do not paint anything.
                    return(false);
                }

                MotionEventActions action = e.ActionMasked;

                if (action == MotionEventActions.Down || action == MotionEventActions.Move ||
                    action == MotionEventActions.HoverMove)
                {
                    int N = e.HistorySize;
                    int P = e.PointerCount;
                    for (int i = 0; i < N; i++)
                    {
                        for (int j = 0; j < P; j++)
                        {
                            Paint(GetPaintModeForTool(e.GetToolType(j), mode),
                                  e.GetHistoricalX(j, i),
                                  e.GetHistoricalY(j, i),
                                  e.GetHistoricalPressure(j, i),
                                  e.GetHistoricalTouchMajor(j, i),
                                  e.GetHistoricalTouchMinor(j, i),
                                  e.GetHistoricalOrientation(j, i),
                                  e.GetHistoricalAxisValue(Axis.Distance, j, i),
                                  e.GetHistoricalAxisValue(Axis.Tilt, j, i));
                        }
                    }
                    for (int j = 0; j < P; j++)
                    {
                        Paint(GetPaintModeForTool(e.GetToolType(j), mode),
                              e.GetX(j),
                              e.GetY(j),
                              e.GetPressure(j),
                              e.GetTouchMajor(j),
                              e.GetTouchMinor(j),
                              e.GetOrientation(j),
                              e.GetAxisValue(Axis.Distance, j),
                              e.GetAxisValue(Axis.Tilt, j));
                    }
                    mCurX = e.GetX();
                    mCurY = e.GetY();
                }
                return(true);
            }