A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System.Collections.Immutable;
5using osu.Framework.Extensions.EnumExtensions;
6using osu.Framework.Input.Handlers;
7using osu.Framework.Input.StateChanges;
8using osu.Framework.Input.StateChanges.Events;
9using osu.Framework.Platform;
10using osuTK;
11using osuTK.Input;
12
13namespace osu.Framework.Input
14{
15 public class UserInputManager : PassThroughInputManager
16 {
17 protected override ImmutableArray<InputHandler> InputHandlers => Host.AvailableInputHandlers;
18
19 public override bool HandleHoverEvents => Host.Window?.CursorInWindow.Value ?? true;
20
21 protected internal override bool ShouldBeAlive => true;
22
23 protected internal UserInputManager()
24 {
25 // UserInputManager is at the very top of the draw hierarchy, so it has no parent updating its IsAlive state
26 IsAlive = true;
27 UseParentInput = false;
28 }
29
30 public override void HandleInputStateChange(InputStateChangeEvent inputStateChange)
31 {
32 switch (inputStateChange)
33 {
34 case MousePositionChangeEvent mousePositionChange:
35 var mouse = mousePositionChange.State.Mouse;
36
37 // confine cursor
38 if (Host.Window != null && Host.Window.CursorState.HasFlagFast(CursorState.Confined))
39 {
40 var clientSize = Host.Window.ClientSize;
41 mouse.Position = Vector2.Clamp(mouse.Position, Vector2.Zero, new Vector2(clientSize.Width, clientSize.Height));
42 }
43
44 break;
45
46 case ButtonStateChangeEvent<MouseButton> buttonChange:
47 if (buttonChange.Kind == ButtonStateChangeKind.Pressed && Host.Window?.CursorInWindow.Value == false)
48 return;
49
50 break;
51
52 case MouseScrollChangeEvent _:
53 if (Host.Window?.CursorInWindow.Value == false)
54 return;
55
56 break;
57 }
58
59 base.HandleInputStateChange(inputStateChange);
60 }
61 }
62}