A game framework written with osu! in mind.
at master 8.7 kB view raw
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.Linq; 5using osu.Framework.Allocation; 6using osu.Framework.Configuration; 7using osu.Framework.Graphics; 8using osu.Framework.Graphics.Containers; 9using osu.Framework.Graphics.Shapes; 10using osu.Framework.Graphics.Sprites; 11using osu.Framework.Input; 12using osu.Framework.Input.Events; 13using osu.Framework.Platform; 14using osu.Framework.Input.Handlers.Mouse; 15using osuTK; 16using osuTK.Graphics; 17 18namespace osu.Framework.Tests.Visual.Input 19{ 20 public class TestSceneInputManager : FrameworkTestScene 21 { 22 public TestSceneInputManager() 23 { 24 Add(new Container 25 { 26 RelativeSizeAxes = Axes.Both, 27 Size = new Vector2(1), 28 Children = new Drawable[] 29 { 30 new Container 31 { 32 RelativeSizeAxes = Axes.Both, 33 RelativePositionAxes = Axes.Both, 34 Position = new Vector2(0, -0.1f), 35 Size = new Vector2(0.7f, 0.8f), 36 Anchor = Anchor.Centre, 37 Origin = Anchor.Centre, 38 Child = new ContainingInputManagerStatusText(), 39 }, 40 new Container 41 { 42 RelativeSizeAxes = Axes.Both, 43 Anchor = Anchor.Centre, 44 Size = new Vector2(0.7f, 0.1f), 45 }, 46 new PassThroughInputManager 47 { 48 RelativeSizeAxes = Axes.Both, 49 RelativePositionAxes = Axes.Both, 50 Position = new Vector2(0, 0.1f), 51 Size = new Vector2(0.7f, 0.5f), 52 Anchor = Anchor.Centre, 53 Origin = Anchor.Centre, 54 Child = new ContainingInputManagerStatusText(), 55 } 56 } 57 }); 58 } 59 60 public class SmallText : SpriteText 61 { 62 public SmallText() 63 { 64 Font = new FontUsage(size: 20); 65 } 66 } 67 68 public class ContainingInputManagerStatusText : Container 69 { 70 private readonly SpriteText inputManagerStatus, 71 mouseStatus, 72 keyboardStatus, 73 joystickStatus, 74 onMouseDownStatus, 75 onMouseUpStatus, 76 onMouseMoveStatus, 77 onScrollStatus, 78 onHoverStatus; 79 80 public ContainingInputManagerStatusText() 81 { 82 RelativeSizeAxes = Axes.Both; 83 Children = new Drawable[] 84 { 85 new Box 86 { 87 RelativeSizeAxes = Axes.Both, 88 Colour = new Color4(1, 1, 1, 0.2f), 89 }, 90 new FillFlowContainer 91 { 92 RelativeSizeAxes = Axes.Both, 93 Direction = FillDirection.Vertical, 94 Children = new Drawable[] 95 { 96 inputManagerStatus = new SmallText(), 97 mouseStatus = new SmallText(), 98 keyboardStatus = new SmallText(), 99 joystickStatus = new SmallText(), 100 onMouseDownStatus = new SmallText { Text = "OnMouseDown 0" }, 101 onMouseUpStatus = new SmallText { Text = "OnMouseUp 0" }, 102 onMouseMoveStatus = new SmallText { Text = "OnMouseMove 0" }, 103 onScrollStatus = new SmallText { Text = "OnScroll 0" }, 104 onHoverStatus = new SmallText { Text = "OnHover 0" }, 105 } 106 } 107 }; 108 } 109 110 protected override void Update() 111 { 112 var inputManager = GetContainingInputManager(); 113 var currentState = inputManager.CurrentState; 114 var mouse = currentState.Mouse; 115 inputManagerStatus.Text = $"{inputManager}"; 116 mouseStatus.Text = $"Mouse: {mouse.Position} {mouse.Scroll} " + string.Join(' ', mouse.Buttons); 117 keyboardStatus.Text = "Keyboard: " + string.Join(' ', currentState.Keyboard.Keys); 118 joystickStatus.Text = "Joystick: " + string.Join(' ', currentState.Joystick.Buttons); 119 base.Update(); 120 } 121 122 public int MouseDownCount; 123 124 protected override bool OnMouseDown(MouseDownEvent e) 125 { 126 ++MouseDownCount; 127 onMouseDownStatus.Text = $"OnMouseDown {MouseDownCount}: Position={e.MousePosition}"; 128 return true; 129 } 130 131 public int MouseUpCount; 132 133 protected override void OnMouseUp(MouseUpEvent e) 134 { 135 ++MouseUpCount; 136 onMouseUpStatus.Text = $"OnMouseUp {MouseUpCount}: Position={e.MousePosition}, MouseDownPosition={e.MouseDownPosition}"; 137 base.OnMouseUp(e); 138 } 139 140 public int MouseMoveCount; 141 142 protected override bool OnMouseMove(MouseMoveEvent e) 143 { 144 ++MouseMoveCount; 145 onMouseMoveStatus.Text = $"OnMouseMove {MouseMoveCount}: Position={e.MousePosition}, Delta={e.Delta}"; 146 return base.OnMouseMove(e); 147 } 148 149 public int ScrollCount; 150 151 protected override bool OnScroll(ScrollEvent e) 152 { 153 ++ScrollCount; 154 onScrollStatus.Text = $"OnScroll {ScrollCount}: ScrollDelta={e.ScrollDelta}, IsPrecise={e.IsPrecise}"; 155 return base.OnScroll(e); 156 } 157 158 public int HoverCount; 159 160 protected override bool OnHover(HoverEvent e) 161 { 162 ++HoverCount; 163 onHoverStatus.Text = $"OnHover {HoverCount}: Position={e.MousePosition}"; 164 return base.OnHover(e); 165 } 166 167 protected override bool OnClick(ClickEvent e) 168 { 169 this.MoveToOffset(new Vector2(100, 0)).Then().MoveToOffset(new Vector2(-100, 0), 1000, Easing.In); 170 return true; 171 } 172 } 173 174 [Resolved] 175 private FrameworkConfigManager config { get; set; } 176 177 [Resolved] 178 private GameHost host { get; set; } 179 180 [BackgroundDependencyLoader] 181 private void load() 182 { 183 AddSliderStep("Cursor sensivity", 0.5, 5, 1, setCursorSensivityConfig); 184 setCursorSensivityConfig(1); 185 AddToggleStep("Toggle relative mode", setRelativeMode); 186 AddToggleStep("Toggle ConfineMouseMode", setConfineMouseModeConfig); 187 AddToggleStep("Toggle cursor visibility", setCursorVisibility); 188 189 setRelativeMode(false); 190 setConfineMouseModeConfig(false); 191 AddStep("Reset handlers", () => host.ResetInputHandlers()); 192 } 193 194 private void setCursorSensivityConfig(double sensitivity) 195 { 196 var mouseHandler = getMouseHandler(); 197 198 if (mouseHandler == null) 199 return; 200 201 mouseHandler.Sensitivity.Value = sensitivity; 202 } 203 204 private void setRelativeMode(bool enabled) 205 { 206 var mouseHandler = getMouseHandler(); 207 208 if (mouseHandler == null) 209 return; 210 211 mouseHandler.UseRelativeMode.Value = enabled; 212 } 213 214 private MouseHandler getMouseHandler() 215 { 216 return host.AvailableInputHandlers.OfType<MouseHandler>().FirstOrDefault(); 217 } 218 219 private void setCursorVisibility(bool visible) 220 { 221 if (host.Window == null) 222 return; 223 224 if (visible) 225 host.Window.CursorState &= ~CursorState.Hidden; 226 else 227 host.Window.CursorState |= CursorState.Hidden; 228 } 229 230 private void setConfineMouseModeConfig(bool enabled) 231 { 232 config.SetValue(FrameworkSetting.ConfineMouseMode, enabled ? ConfineMouseMode.Always : ConfineMouseMode.Fullscreen); 233 } 234 } 235}