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

            Enabled.BindValueChanged(e =>
            {
                var tkWin = (OsuTKWindow)host.Window;

                if (e.NewValue)
                {
                    tkWin.MouseMove  += handleMouseEvent;
                    tkWin.MouseDown  += handleMouseEvent;
                    tkWin.MouseUp    += handleMouseEvent;
                    tkWin.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.Value || !tkWin.Visible || tkWin.WindowState.ToOsuTK() == osuTK.WindowState.Minimized)
                        {
                            return;
                        }

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

                        if (lastCursorState != null && cursorState.Equals(lastCursorState.Value))
                        {
                            return;
                        }

                        lastCursorState = cursorState;

                        var mapped = tkWin.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();

                    tkWin.MouseMove  -= handleMouseEvent;
                    tkWin.MouseDown  -= handleMouseEvent;
                    tkWin.MouseUp    -= handleMouseEvent;
                    tkWin.MouseWheel -= handleMouseEvent;

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

            return(true);
        }
示例#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(enabled =>
            {
                if (enabled)
                {
                    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, 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, 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);
        }