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
4#if NET5_0
5using System.Linq;
6using osu.Framework.Allocation;
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.Input.Handlers.Tablet;
14using osu.Framework.Platform;
15using osuTK;
16using osuTK.Graphics;
17
18namespace osu.Framework.Tests.Visual.Input
19{
20 public class TestSceneTabletInput : FrameworkTestScene
21 {
22 public TestSceneTabletInput()
23 {
24 var penButtonFlow = new FillFlowContainer
25 {
26 RelativeSizeAxes = Axes.X,
27 AutoSizeAxes = Axes.Y,
28 };
29
30 var auxButtonFlow = new FillFlowContainer
31 {
32 RelativeSizeAxes = Axes.X,
33 AutoSizeAxes = Axes.Y
34 };
35
36 for (int i = 0; i < 8; i++)
37 penButtonFlow.Add(new PenButtonHandler(i));
38
39 for (int i = 0; i < 16; i++)
40 auxButtonFlow.Add(new AuxiliaryButtonHandler(i));
41
42 Child = new FillFlowContainer
43 {
44 RelativeSizeAxes = Axes.Both,
45 Direction = FillDirection.Vertical,
46 Children = new[] { penButtonFlow, auxButtonFlow }
47 };
48 }
49
50 [Resolved]
51 private GameHost host { get; set; }
52
53 protected override void LoadComplete()
54 {
55 base.LoadComplete();
56
57 var tabletHandler = host.AvailableInputHandlers.OfType<OpenTabletDriverHandler>().FirstOrDefault();
58
59 if (tabletHandler != null)
60 AddToggleStep("toggle tablet handling", t => tabletHandler.Enabled.Value = t);
61 }
62
63 private class PenButtonHandler : CompositeDrawable
64 {
65 private readonly TabletPenButton button;
66 private readonly Drawable background;
67
68 public PenButtonHandler(int buttonIndex)
69 {
70 button = TabletPenButton.Primary + buttonIndex;
71
72 Size = new Vector2(50);
73
74 InternalChildren = new[]
75 {
76 background = new Container
77 {
78 RelativeSizeAxes = Axes.Both,
79 Colour = Color4.DarkGreen,
80 Alpha = 0,
81 Child = new Box { RelativeSizeAxes = Axes.Both }
82 },
83 new SpriteText
84 {
85 Anchor = Anchor.Centre,
86 Origin = Anchor.Centre,
87 Text = $"B{buttonIndex + 1}"
88 }
89 };
90 }
91
92 protected override bool OnTabletPenButtonPress(TabletPenButtonPressEvent e)
93 {
94 if (e.Button != button)
95 return base.OnTabletPenButtonPress(e);
96
97 background.FadeIn(100, Easing.OutQuint);
98 return true;
99 }
100
101 protected override void OnTabletPenButtonRelease(TabletPenButtonReleaseEvent e)
102 {
103 if (e.Button != button)
104 {
105 base.OnTabletPenButtonRelease(e);
106 return;
107 }
108
109 background.FadeOut(100);
110 }
111 }
112
113 private class AuxiliaryButtonHandler : CompositeDrawable
114 {
115 private readonly TabletAuxiliaryButton button;
116 private readonly Drawable background;
117
118 public AuxiliaryButtonHandler(int buttonIndex)
119 {
120 button = TabletAuxiliaryButton.Button1 + buttonIndex;
121
122 Size = new Vector2(50);
123
124 InternalChildren = new[]
125 {
126 background = new Container
127 {
128 RelativeSizeAxes = Axes.Both,
129 Colour = Color4.DarkGreen,
130 Alpha = 0,
131 Child = new Box { RelativeSizeAxes = Axes.Both }
132 },
133 new SpriteText
134 {
135 Anchor = Anchor.Centre,
136 Origin = Anchor.Centre,
137 Text = $"B{buttonIndex + 1}"
138 }
139 };
140 }
141
142 protected override bool OnTabletAuxiliaryButtonPress(TabletAuxiliaryButtonPressEvent e)
143 {
144 if (e.Button != button)
145 return base.OnTabletAuxiliaryButtonPress(e);
146
147 background.FadeIn(100, Easing.OutQuint);
148 return true;
149 }
150
151 protected override void OnTabletAuxiliaryButtonRelease(TabletAuxiliaryButtonReleaseEvent e)
152 {
153 if (e.Button != button)
154 {
155 base.OnTabletAuxiliaryButtonRelease(e);
156 return;
157 }
158
159 background.FadeOut(100);
160 }
161 }
162 }
163}
164#endif