Example #1
0
        public override bool OnTouchEvent(MotionEvent e)
        {
            if (virtualButtons != null)
            {
                showVirtualButtons = true;

                MotionEventActions action = e.ActionMasked;
                if (action == MotionEventActions.Down || action == MotionEventActions.PointerDown)
                {
                    int   pointerIndex = e.ActionIndex;
                    float x            = e.GetX(pointerIndex);
                    float y            = e.GetY(pointerIndex);
                    float w            = e.GetTouchMajor(pointerIndex) * 0.5f;
                    float h            = e.GetTouchMinor(pointerIndex) * 0.5f;

                    bool vibrated = false;
                    for (int i = 0; i < virtualButtons.Length; i++)
                    {
                        VirtualButton button = virtualButtons[i];
                        if (button.KeyCode != Key.Unknown)
                        {
                            if (button.CurrentPointerId == -1 && IsOnButton(button, x, y, w, h))
                            {
                                int pointerId = e.GetPointerId(pointerIndex);

                                button.CurrentPointerId             = pointerId;
                                pressedButtons[(int)button.KeyCode] = true;

                                if (allowVibrations && !vibrated)
                                {
                                    vibrator.Vibrate(16);
                                    vibrated = true;
                                }
                            }
                        }
                    }
                }
                else if (action == MotionEventActions.Move)
                {
                    int pointerCount = e.PointerCount;

                    for (int i = 0; i < virtualButtons.Length; i++)
                    {
                        VirtualButton button = virtualButtons[i];
                        if (button.KeyCode != Key.Unknown)
                        {
                            if (button.CurrentPointerId != -1)
                            {
                                int pointerIndex = e.FindPointerIndex(button.CurrentPointerId);

                                if (!IsOnButton(button, e.GetX(pointerIndex), e.GetY(pointerIndex), e.GetTouchMajor(pointerIndex) * 0.5f, e.GetTouchMinor(pointerIndex) * 0.5f))
                                {
                                    button.CurrentPointerId             = -1;
                                    pressedButtons[(int)button.KeyCode] = false;
                                }
                            }
                            else
                            {
                                for (int j = 0; j < pointerCount; j++)
                                {
                                    if (IsOnButton(button, e.GetX(j), e.GetY(j), e.GetTouchMajor(j) * 0.5f, e.GetTouchMinor(j) * 0.5f))
                                    {
                                        int pointerId = e.GetPointerId(j);

                                        button.CurrentPointerId             = pointerId;
                                        pressedButtons[(int)button.KeyCode] = true;

                                        if (allowVibrations)
                                        {
                                            vibrator.Vibrate(11);
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                else if (action == MotionEventActions.Up || action == MotionEventActions.Cancel)
                {
                    for (int i = 0; i < virtualButtons.Length; i++)
                    {
                        VirtualButton button = virtualButtons[i];
                        if (button.CurrentPointerId != -1)
                        {
                            button.CurrentPointerId             = -1;
                            pressedButtons[(int)button.KeyCode] = false;
                        }
                    }
                }
                else if (action == MotionEventActions.PointerUp)
                {
                    int pointerId = e.GetPointerId(e.ActionIndex);

                    for (int i = 0; i < virtualButtons.Length; i++)
                    {
                        VirtualButton button = virtualButtons[i];

                        if (button.CurrentPointerId == pointerId)
                        {
                            button.CurrentPointerId             = -1;
                            pressedButtons[(int)button.KeyCode] = false;
                        }
                    }
                }
            }

            //return base.OnTouchEvent(e);
            return(true);
        }
Example #2
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);
            }