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 osu.Framework.Graphics;
5using osu.Framework.Graphics.Containers;
6using osu.Framework.Graphics.Shapes;
7using osu.Framework.Input.Events;
8using osu.Framework.Tests.Visual.Containers;
9using osuTK;
10using osuTK.Graphics;
11
12namespace osu.Framework.Tests.Visual.Sprites
13{
14 public class TestSceneTriangles : FrameworkTestScene
15 {
16 private readonly Container testContainer;
17
18 public TestSceneTriangles()
19 {
20 Add(testContainer = new Container
21 {
22 RelativeSizeAxes = Axes.Both,
23 });
24
25 string[] testNames = { @"Bounding box / input" };
26
27 for (int i = 0; i < testNames.Length; i++)
28 {
29 int test = i;
30 AddStep(testNames[i], delegate { loadTest(test); });
31 }
32
33 loadTest(0);
34 addCrosshair();
35 }
36
37 private void addCrosshair()
38 {
39 Add(new Box
40 {
41 Colour = Color4.Black,
42 Size = new Vector2(22, 4),
43 Anchor = Anchor.Centre,
44 Origin = Anchor.Centre
45 });
46
47 Add(new Box
48 {
49 Colour = Color4.Black,
50 Size = new Vector2(4, 22),
51 Anchor = Anchor.Centre,
52 Origin = Anchor.Centre
53 });
54
55 Add(new Box
56 {
57 Colour = Color4.WhiteSmoke,
58 Size = new Vector2(20, 2),
59 Anchor = Anchor.Centre,
60 Origin = Anchor.Centre
61 });
62
63 Add(new Box
64 {
65 Colour = Color4.WhiteSmoke,
66 Size = new Vector2(2, 20),
67 Anchor = Anchor.Centre,
68 Origin = Anchor.Centre
69 });
70 }
71
72 private void loadTest(int testType)
73 {
74 testContainer.Clear();
75
76 Triangle triangle;
77
78 switch (testType)
79 {
80 case 0:
81 Container box;
82
83 testContainer.Add(box = new InfofulBoxAutoSize
84 {
85 Anchor = Anchor.Centre,
86 Origin = Anchor.Centre
87 });
88
89 addCornerMarkers(box);
90
91 box.AddRange(new[]
92 {
93 new DraggableTriangle
94 {
95 //chameleon = true,
96 Position = new Vector2(0, 0),
97 Size = new Vector2(25, 25),
98 Origin = Anchor.Centre,
99 Anchor = Anchor.Centre,
100 Colour = Color4.Blue,
101 },
102 triangle = new DraggableTriangle
103 {
104 Size = new Vector2(250, 250),
105 Alpha = 0.5f,
106 Origin = Anchor.Centre,
107 Anchor = Anchor.Centre,
108 Colour = Color4.DarkSeaGreen,
109 }
110 });
111
112 triangle.OnUpdate += delegate { triangle.Rotation += 0.05f; };
113 break;
114 }
115
116#if DEBUG
117 //if (toggleDebugAutosize.State)
118 // testContainer.Children.FindAll(c => c.HasAutosizeChildren).ForEach(c => c.AutoSizeDebug = true);
119#endif
120 }
121
122 private void addCornerMarkers(Container box, int size = 50, Color4? colour = null)
123 {
124 box.Add(new DraggableTriangle
125 {
126 //chameleon = true,
127 Size = new Vector2(size, size),
128 Origin = Anchor.TopLeft,
129 Anchor = Anchor.TopLeft,
130 AllowDrag = false,
131 Depth = -2,
132 Colour = colour ?? Color4.Red,
133 });
134
135 box.Add(new DraggableTriangle
136 {
137 //chameleon = true,
138 Size = new Vector2(size, size),
139 Origin = Anchor.TopRight,
140 Anchor = Anchor.TopRight,
141 AllowDrag = false,
142 Depth = -2,
143 Colour = colour ?? Color4.Red,
144 });
145
146 box.Add(new DraggableTriangle
147 {
148 //chameleon = true,
149 Size = new Vector2(size, size),
150 Origin = Anchor.BottomLeft,
151 Anchor = Anchor.BottomLeft,
152 AllowDrag = false,
153 Depth = -2,
154 Colour = colour ?? Color4.Red,
155 });
156
157 box.Add(new DraggableTriangle
158 {
159 //chameleon = true,
160 Size = new Vector2(size, size),
161 Origin = Anchor.BottomRight,
162 Anchor = Anchor.BottomRight,
163 AllowDrag = false,
164 Depth = -2,
165 Colour = colour ?? Color4.Red,
166 });
167 }
168 }
169
170 internal class DraggableTriangle : Triangle
171 {
172 public bool AllowDrag = true;
173
174 protected override void OnDrag(DragEvent e)
175 {
176 if (!AllowDrag) return;
177
178 Position += e.Delta;
179 }
180
181 protected override bool OnDragStart(DragStartEvent e) => AllowDrag;
182 }
183}