コード例 #1
0
        public override bool Initialize(GameHost host)
        {
            if (!base.Initialize(host))
            {
                return(false);
            }

            if (!(host.Window is AndroidGameWindow androidWindow))
            {
                return(false);
            }

            window = androidWindow;

            window.CursorStateChanged += updatePointerCapture;

            // it's possible that Android forcefully released capture if we were unfocused.
            // so we update here when we get focus again.
            View.FocusChange += (sender, args) =>
            {
                if (args.HasFocus)
                {
                    updatePointerCapture();
                }
            };

            UseRelativeMode.BindValueChanged(_ => updatePointerCapture());

            Enabled.BindValueChanged(enabled =>
            {
                if (enabled.NewValue)
                {
                    View.GenericMotion += HandleGenericMotion;
                    View.Hover         += HandleHover;
                    View.KeyDown       += HandleKeyDown;
                    View.KeyUp         += HandleKeyUp;
                    View.Touch         += HandleTouch;

                    // Pointer capture is only available on Android 8.0 and up
                    if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
                    {
                        View.CapturedPointer += HandleCapturedPointer;
                    }
                }
                else
                {
                    View.GenericMotion -= HandleGenericMotion;
                    View.Hover         -= HandleHover;
                    View.KeyDown       -= HandleKeyDown;
                    View.KeyUp         -= HandleKeyUp;
                    View.Touch         -= HandleTouch;

                    // Pointer capture is only available on Android 8.0 and up
                    if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
                    {
                        View.CapturedPointer -= HandleCapturedPointer;
                    }
                }

                updatePointerCapture();
            }, true);

            return(true);
        }
コード例 #2
0
        public override bool Initialize(GameHost host)
        {
            if (!base.Initialize(host))
            {
                return(false);
            }

            if (!(host.Window is SDL2DesktopWindow desktopWindow))
            {
                return(false);
            }

            window = desktopWindow;

            isActive = window.IsActive.GetBoundCopy();
            isActive.BindValueChanged(_ => updateRelativeMode());

            cursorInWindow = host.Window.CursorInWindow.GetBoundCopy();
            cursorInWindow.BindValueChanged(e =>
            {
                if (e.NewValue)
                {
                    // don't immediately update if the cursor has just entered the window
                    pendingUpdateRelativeMode = true;
                }
                else
                {
                    updateRelativeMode();
                }
            });

            cursorState = desktopWindow.CursorStateBindable.GetBoundCopy();
            cursorState.BindValueChanged(_ => updateRelativeMode());

            UseRelativeMode.BindValueChanged(e =>
            {
                window.MouseAutoCapture = !e.NewValue;
                updateRelativeMode();
            }, true);

            Enabled.BindValueChanged(enabled =>
            {
                updateRelativeMode();

                if (enabled.NewValue)
                {
                    window.MouseMove         += HandleMouseMove;
                    window.MouseMoveRelative += HandleMouseMoveRelative;
                    window.MouseDown         += handleMouseDown;
                    window.MouseUp           += handleMouseUp;
                    window.MouseWheel        += handleMouseWheel;
                }
                else
                {
                    window.MouseMove         -= HandleMouseMove;
                    window.MouseMoveRelative -= HandleMouseMoveRelative;
                    window.MouseDown         -= handleMouseDown;
                    window.MouseUp           -= handleMouseUp;
                    window.MouseWheel        -= handleMouseWheel;
                }
            }, true);

            window.Exited += () =>
            {
                if (window.RelativeMouseMode && cursorCaptured)
                {
                    window.RelativeMouseMode = false;
                    transferLastPositionToHostCursor();
                }
            };

            return(true);
        }