Beispiel #1
0
        public override bool Initialize(GameHost host)
        {
            base.Initialize(host);

            Enabled.BindValueChanged(e =>
            {
                if (e.NewValue)
                {
                    host.Window.MouseMove  += handleMouseEvent;
                    host.Window.MouseDown  += handleMouseEvent;
                    host.Window.MouseUp    += handleMouseEvent;
                    host.Window.MouseWheel += handleMouseEvent;

                    // polling is used to keep a valid mouse position when we aren't receiving events.
                    osuTK.Input.MouseState?lastCursorState   = null;
                    host.InputThread.Scheduler.Add(scheduled = new ScheduledDelegate(delegate
                    {
                        // we should be getting events if the mouse is inside the window.
                        if (MouseInWindow || !host.Window.Visible || host.Window.WindowState == WindowState.Minimized)
                        {
                            return;
                        }

                        var cursorState = osuTK.Input.Mouse.GetCursorState();

                        if (cursorState.Equals(lastCursorState))
                        {
                            return;
                        }

                        lastCursorState = cursorState;

                        var mapped = host.Window.PointToClient(new Point(cursorState.X, cursorState.Y));

                        var newState = new OsuTKPollMouseState(cursorState, host.IsActive.Value, new Vector2(mapped.X, mapped.Y));
                        HandleState(newState, lastPollState, true);
                        lastPollState = newState;
                    }, 0, 1000.0 / 60));
                }
                else
                {
                    scheduled?.Cancel();

                    host.Window.MouseMove  -= handleMouseEvent;
                    host.Window.MouseDown  -= handleMouseEvent;
                    host.Window.MouseUp    -= handleMouseEvent;
                    host.Window.MouseWheel -= handleMouseEvent;

                    lastPollState  = null;
                    lastEventState = null;
                }
            }, true);

            return(true);
        }
Beispiel #2
0
        public override bool Initialize(GameHost host)
        {
            base.Initialize(host);

            // Get the bindables we need to determine whether to confine the mouse to window or not
            if (host.Window is DesktopGameWindow desktopWindow)
            {
                confineMode.BindTo(desktopWindow.ConfineMouseMode);
                windowMode.BindTo(desktopWindow.WindowMode);
                mapAbsoluteInputToWindow.BindTo(desktopWindow.MapAbsoluteInputToWindow);
            }

            Enabled.BindValueChanged(e =>
            {
                if (e.NewValue)
                {
                    host.InputThread.Scheduler.Add(scheduled = new ScheduledDelegate(delegate
                    {
                        if (!host.Window.Visible || host.Window.WindowState == WindowState.Minimized)
                        {
                            return;
                        }

                        if ((MouseInWindow || lastEachDeviceStates.Any(s => s != null && s.Buttons.HasAnyButtonPressed)) && host.Window.Focused)
                        {
                            osuTK.Input.Mouse.GetStates(newRawStates);

                            while (lastEachDeviceStates.Count < newRawStates.Count)
                            {
                                lastEachDeviceStates.Add(null);
                            }

                            for (int i = 0; i < newRawStates.Count; i++)
                            {
                                if (newRawStates[i].IsConnected != true)
                                {
                                    lastEachDeviceStates[i] = null;
                                    continue;
                                }

                                var rawState  = newRawStates[i];
                                var lastState = lastEachDeviceStates[i];

                                if (lastState != null && rawState.Equals(lastState.RawState))
                                {
                                    continue;
                                }

                                var newState = new OsuTKPollMouseState(rawState, host.IsActive.Value, getUpdatedPosition(rawState, lastState));

                                HandleState(newState, lastState, rawState.Flags.HasFlag(MouseStateFlags.MoveAbsolute));

                                lastEachDeviceStates[i] = newState;
                                lastUnfocusedState      = null;
                            }
                        }
                        else
                        {
                            var state       = osuTK.Input.Mouse.GetCursorState();
                            var screenPoint = host.Window.PointToClient(new Point(state.X, state.Y));

                            var newState = new UnfocusedMouseState(new MouseState(), host.IsActive.Value, new Vector2(screenPoint.X, screenPoint.Y));

                            HandleState(newState, lastUnfocusedState, true);

                            lastUnfocusedState = newState;
                            lastEachDeviceStates.Clear();
                        }
                    }, 0, 0));
                }
                else
                {
                    scheduled?.Cancel();
                    lastEachDeviceStates.Clear();
                    lastUnfocusedState = null;
                }
            }, true);

            return(true);
        }