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.Linq;
5using NUnit.Framework;
6using osu.Framework.Graphics;
7using osu.Framework.Graphics.Containers;
8using osu.Framework.Graphics.Shapes;
9using osu.Framework.Graphics.Sprites;
10using osu.Framework.Input.Events;
11using osu.Framework.Testing;
12using osuTK;
13using osuTK.Graphics;
14using osuTK.Input;
15
16namespace osu.Framework.Tests.Visual.Input
17{
18 public class TestSceneInputQueueChange : ManualInputManagerTestScene
19 {
20 private readonly HittableBox box1;
21 private readonly HittableBox box2;
22 private readonly HittableBox box3;
23
24 public TestSceneInputQueueChange()
25 {
26 RelativeSizeAxes = Axes.Both;
27 Children = new Drawable[]
28 {
29 box3 = new HittableBox(3),
30 box2 = new HittableBox(2),
31 box1 = new HittableBox(1),
32 };
33
34 // TODO: blocking event testing
35 }
36
37 [SetUp]
38 public new void SetUp() => Schedule(() =>
39 {
40 foreach (var b in Children.OfType<HittableBox>())
41 b.Reset();
42 });
43
44 [Test]
45 public void SeparateClicks()
46 {
47 AddStep("move", () => InputManager.MoveMouseTo(InputManager.Children.First().ScreenSpaceDrawQuad.Centre));
48 AddStep("press 1", () => InputManager.PressButton(MouseButton.Button1));
49 AddStep("press 2", () => InputManager.PressButton(MouseButton.Button2));
50 AddStep("release 1", () => InputManager.ReleaseButton(MouseButton.Button1));
51 AddStep("release 2", () => InputManager.ReleaseButton(MouseButton.Button2));
52 AddAssert("box 1 was pressed", () => box1.HitCount == 1);
53 AddAssert("box 2 was pressed", () => box2.HitCount == 1);
54 AddAssert("box 3 not pressed", () => box3.HitCount == 0);
55 }
56
57 [Test]
58 public void CombinedClicks()
59 {
60 AddStep("move", () => InputManager.MoveMouseTo(Children.First().ScreenSpaceDrawQuad.Centre));
61 AddStep("press 1+2", () =>
62 {
63 InputManager.PressButton(MouseButton.Button1);
64 InputManager.PressButton(MouseButton.Button2);
65 });
66 AddStep("release 1+2", () =>
67 {
68 InputManager.ReleaseButton(MouseButton.Button1);
69 InputManager.ReleaseButton(MouseButton.Button2);
70 });
71 AddAssert("box 1 was pressed", () => box1.HitCount == 1);
72 AddAssert("box 2 was pressed", () => box2.HitCount == 1);
73 AddAssert("box 3 not pressed", () => box3.HitCount == 0);
74 }
75
76 private class HittableBox : CompositeDrawable
77 {
78 private readonly int index;
79
80 public int HitCount;
81
82 private float xPos => index * 10;
83
84 public HittableBox(int index)
85 {
86 this.index = index;
87 Position = new Vector2(xPos);
88 Size = new Vector2(50);
89 Anchor = Anchor.Centre;
90 Origin = Anchor.Centre;
91
92 BorderColour = Color4.BlueViolet;
93 BorderThickness = 3;
94 Masking = true;
95
96 InternalChildren = new Drawable[]
97 {
98 new Box { RelativeSizeAxes = Axes.Both },
99 new SpriteText
100 {
101 Colour = Color4.Black,
102 Text = index.ToString(),
103 Anchor = Anchor.Centre,
104 Origin = Anchor.Centre,
105 }
106 };
107 }
108
109 protected override bool OnMouseDown(MouseDownEvent e)
110 {
111 HitCount++;
112 this.MoveToX(xPos + 100).Then().MoveToX(xPos, 1000, Easing.In);
113 return true;
114 }
115
116 public void Reset()
117 {
118 FinishTransforms();
119 HitCount = 0;
120 }
121 }
122 }
123}