forked from ppy/osu-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestSceneScrollContainer.cs
587 lines (497 loc) · 23.8 KB
/
TestSceneScrollContainer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable disable
using System;
using System.Diagnostics;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Framework.Utils;
using osu.Framework.Testing;
using osuTK;
using osuTK.Graphics;
using osuTK.Input;
namespace osu.Framework.Tests.Visual.Containers
{
public partial class TestSceneScrollContainer : ManualInputManagerTestScene
{
private ScrollContainer<Drawable> scrollContainer;
[SetUp]
public void Setup() => Schedule(Clear);
[TestCase(0)]
[TestCase(100)]
public void TestScrollTo(float clampExtension)
{
const float container_height = 100;
const float box_height = 400;
AddStep("Create scroll container", () =>
{
Add(scrollContainer = new BasicScrollContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(container_height),
ClampExtension = clampExtension,
Child = new Box { Size = new Vector2(100, box_height) }
});
});
scrollTo(-100, box_height - container_height, clampExtension);
checkPosition(0);
scrollTo(100, box_height - container_height, clampExtension);
checkPosition(100);
scrollTo(300, box_height - container_height, clampExtension);
checkPosition(300);
scrollTo(400, box_height - container_height, clampExtension);
checkPosition(300);
scrollTo(500, box_height - container_height, clampExtension);
checkPosition(300);
}
private FillFlowContainer fill;
[Test]
public void TestScrollIntoView()
{
const float item_height = 25;
AddStep("Create scroll container", () =>
{
Add(scrollContainer = new BasicScrollContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(item_height * 4),
Child = fill = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
},
});
for (int i = 0; i < 8; i++)
{
fill.Add(new Box
{
Colour = new Color4(RNG.NextSingle(1), RNG.NextSingle(1), RNG.NextSingle(1), 1),
RelativeSizeAxes = Axes.X,
Height = item_height,
});
}
});
// simple last item (hits bottom of view)
scrollIntoView(7, item_height * 4);
// position doesn't change when item in view
scrollIntoView(6, item_height * 4);
// scroll in reverse without overscrolling
scrollIntoView(1, item_height);
// scroll forwards with small (non-zero) view
// current position will change on restore size
scrollIntoView(7, item_height * 7, heightAdjust: 15, expectedPostAdjustPosition: 100);
// scroll backwards with small (non-zero) view
// current position won't change on restore size
scrollIntoView(2, item_height * 2, heightAdjust: 15, expectedPostAdjustPosition: item_height * 2);
// test forwards scroll with zero container height
scrollIntoView(7, item_height * 7, heightAdjust: 0, expectedPostAdjustPosition: item_height * 4);
// test backwards scroll with zero container height
scrollIntoView(2, item_height * 2, heightAdjust: 0, expectedPostAdjustPosition: item_height * 2);
}
[TestCase(false)]
[TestCase(true)]
public void TestDraggingScroll(bool withClampExtension)
{
AddStep("Create scroll container", () =>
{
Add(scrollContainer = new BasicScrollContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(200),
ClampExtension = withClampExtension ? 100 : 0,
Child = new Box { Size = new Vector2(200, 300) }
});
});
AddStep("Click and drag scrollcontainer", () =>
{
InputManager.MoveMouseTo(scrollContainer);
InputManager.PressButton(MouseButton.Left);
// Required for the dragging state to be set correctly.
InputManager.MoveMouseTo(scrollContainer.ToScreenSpace(scrollContainer.LayoutRectangle.Centre + new Vector2(10f)));
});
AddStep("Move mouse up", () => InputManager.MoveMouseTo(scrollContainer.ScreenSpaceDrawQuad.Centre - new Vector2(0, 1000)));
checkPosition(withClampExtension ? 200 : 100);
AddStep("Move mouse down", () => InputManager.MoveMouseTo(scrollContainer.ScreenSpaceDrawQuad.Centre + new Vector2(0, 1000)));
checkPosition(withClampExtension ? -100 : 0);
AddStep("Release mouse button", () => InputManager.ReleaseButton(MouseButton.Left));
checkPosition(0);
}
[Test]
public void TestContentAnchors()
{
AddStep("Create scroll container with centre-left content", () =>
{
Add(scrollContainer = new BasicScrollContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(300),
ScrollContent =
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
Child = new Box { Size = new Vector2(300, 400) }
});
});
AddStep("Scroll to 0", () => scrollContainer.ScrollTo(0, false));
AddAssert("Content position at top", () => Precision.AlmostEquals(scrollContainer.ScreenSpaceDrawQuad.TopLeft, scrollContainer.ScrollContent.ScreenSpaceDrawQuad.TopLeft));
}
[Test]
public void TestClampedScrollbar()
{
AddStep("Create scroll container", () =>
{
Add(scrollContainer = new ClampedScrollbarScrollContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(500),
Child = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Children = new[]
{
new Box { Size = new Vector2(500) },
new Box { Size = new Vector2(500) },
new Box { Size = new Vector2(500) },
}
}
});
});
AddStep("scroll to end", () => scrollContainer.ScrollToEnd(false));
checkScrollbarPosition(250);
AddStep("scroll to start", () => scrollContainer.ScrollToStart(false));
checkScrollbarPosition(0);
}
[Test]
public void TestClampedScrollbarDrag()
{
ClampedScrollbarScrollContainer clampedContainer = null;
AddStep("Create scroll container", () =>
{
Add(scrollContainer = clampedContainer = new ClampedScrollbarScrollContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(500),
Child = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Children = new[]
{
new Box { Size = new Vector2(500) },
new Box { Size = new Vector2(500) },
new Box { Size = new Vector2(500) },
}
}
});
});
AddStep("Click scroll bar", () =>
{
InputManager.MoveMouseTo(clampedContainer.Scrollbar);
InputManager.PressButton(MouseButton.Left);
});
// Position at mouse down
checkScrollbarPosition(0);
AddStep("begin drag", () =>
{
// Required for the dragging state to be set correctly.
InputManager.MoveMouseTo(clampedContainer.Scrollbar.ToScreenSpace(clampedContainer.Scrollbar.LayoutRectangle.Centre + new Vector2(0, -10f)));
});
AddStep("Move mouse up", () => InputManager.MoveMouseTo(scrollContainer.ScreenSpaceDrawQuad.TopRight - new Vector2(0, 20)));
checkScrollbarPosition(0);
AddStep("Move mouse down", () => InputManager.MoveMouseTo(scrollContainer.ScreenSpaceDrawQuad.BottomRight + new Vector2(0, 20)));
checkScrollbarPosition(250);
AddStep("Release mouse button", () => InputManager.ReleaseButton(MouseButton.Left));
checkScrollbarPosition(250);
}
[Test]
public void TestHandleKeyboardRepeatAfterRemoval()
{
AddStep("create scroll container", () =>
{
Add(scrollContainer = new RepeatCountingScrollContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(500),
Child = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Children = new[]
{
new Box { Size = new Vector2(500) },
new Box { Size = new Vector2(500) },
new Box { Size = new Vector2(500) },
}
}
});
});
AddStep("move mouse to scroll container", () => InputManager.MoveMouseTo(scrollContainer));
AddStep("press page down and remove scroll container", () => InputManager.PressKey(Key.PageDown));
AddStep("remove scroll container", () =>
{
Remove(scrollContainer, false);
((RepeatCountingScrollContainer)scrollContainer).RepeatCount = 0;
});
AddWaitStep("wait for repeats", 5);
}
[Test]
public void TestEmptyScrollContainerDoesNotHandleScrollAndDrag()
{
AddStep("create scroll container", () =>
{
Add(scrollContainer = new InputHandlingScrollContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(500),
});
});
AddStep("Perform scroll", () =>
{
InputManager.MoveMouseTo(scrollContainer);
InputManager.ScrollVerticalBy(50);
});
AddAssert("Scroll was not handled", () =>
{
var inputHandlingScrollContainer = (InputHandlingScrollContainer)scrollContainer;
return inputHandlingScrollContainer.ScrollHandled.HasValue && !inputHandlingScrollContainer.ScrollHandled.Value;
});
AddStep("Perform drag", () =>
{
InputManager.MoveMouseTo(scrollContainer);
InputManager.PressButton(MouseButton.Left);
InputManager.MoveMouseTo(scrollContainer, new Vector2(50));
});
AddAssert("Drag was not handled", () =>
{
var inputHandlingScrollContainer = (InputHandlingScrollContainer)scrollContainer;
return inputHandlingScrollContainer.DragHandled.HasValue && !inputHandlingScrollContainer.DragHandled.Value;
});
}
[Test]
public void TestScrollbarRespectsPadding()
{
const float container_height = 100;
const float box_height = 400;
const float padding = 10;
AddStep("Create scroll container", () =>
{
Add(new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(container_height),
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Colour4.Red,
},
scrollContainer = new BasicScrollContainer
{
Padding = new MarginPadding { Vertical = padding },
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Child = new Box { Size = new Vector2(100, box_height) }
},
}
});
});
AddStep("Scroll to start", () => { scrollContainer.ScrollToStart(false); });
checkScrollbarPosition(0);
AddStep("Scroll to end", () => scrollContainer.ScrollToEnd(false));
checkScrollbarPosition(64);
}
/// <summary>
/// Ensures that initiating a drag with horizontal delta on a singular vertical <see cref="ScrollContainer{T}"/> doesn't prevent from continuing with vertical drags.
/// </summary>
/// <remarks>
/// If the vertical scroll container is nested inside of a horizontal one, then it should prevent it, as covered in <see cref="TestSceneScrollContainerNested.TestDragging"/>.
/// </remarks>
[TestCase(false)]
[TestCase(true)]
public void TestSingularVerticalScrollWithHorizontalDelta(bool withClampExtension)
{
AddStep("Create scroll container", () =>
{
Add(scrollContainer = new BasicScrollContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(200),
ClampExtension = withClampExtension ? 100 : 0,
Child = new Box { Size = new Vector2(200, 300) }
});
});
AddStep("Click and drag horizontally", () =>
{
InputManager.MoveMouseTo(scrollContainer);
InputManager.PressButton(MouseButton.Left);
// Required for the dragging state to be set correctly.
InputManager.MoveMouseTo(scrollContainer.ToScreenSpace(scrollContainer.LayoutRectangle.Centre + new Vector2(20f, 0f)));
});
AddStep("Move mouse up", () => InputManager.MoveMouseTo(scrollContainer.ScreenSpaceDrawQuad.Centre - new Vector2(0, 1000)));
checkPosition(withClampExtension ? 200 : 100);
AddStep("Move mouse down", () => InputManager.MoveMouseTo(scrollContainer.ScreenSpaceDrawQuad.Centre + new Vector2(0, 1000)));
checkPosition(withClampExtension ? -100 : 0);
AddStep("Move mouse diagonally up", () => InputManager.MoveMouseTo(scrollContainer.ScreenSpaceDrawQuad.Centre - new Vector2(2000, 1000)));
checkPosition(withClampExtension ? 200 : 100);
AddStep("Move mouse diagonally down", () => InputManager.MoveMouseTo(scrollContainer.ScreenSpaceDrawQuad.Centre + new Vector2(2000, 1000)));
checkPosition(withClampExtension ? -100 : 0);
AddStep("Release mouse button", () => InputManager.ReleaseButton(MouseButton.Left));
checkPosition(0);
AddStep("Hover over scroll container", () => InputManager.MoveMouseTo(scrollContainer));
AddStep("Scroll diagonally down", () => InputManager.ScrollBy(new Vector2(-20, -10)));
checkPosition(100);
AddStep("Scroll diagonally up", () => InputManager.ScrollBy(new Vector2(20, 10)));
checkPosition(0);
}
[Test]
public void TestDragHandlingUpdatesOnParentChanges()
{
BasicScrollContainer horizontalScrollContainer = null;
AddStep("Create scroll container", () =>
{
Add(scrollContainer = new BasicScrollContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(200),
ClampExtension = 0,
Child = new Box { Size = new Vector2(200, 300) }
});
});
AddStep("Click and drag horizontally", () =>
{
InputManager.MoveMouseTo(scrollContainer);
InputManager.PressButton(MouseButton.Left);
// Required for the dragging state to be set correctly.
InputManager.MoveMouseTo(scrollContainer.ToScreenSpace(scrollContainer.LayoutRectangle.Centre + new Vector2(20f, 0f)));
});
AddStep("Move mouse diagonally up", () => InputManager.MoveMouseTo(scrollContainer.ScreenSpaceDrawQuad.Centre - new Vector2(1000, 1000)));
checkPosition(100);
AddStep("Move mouse diagonally down", () => InputManager.MoveMouseTo(scrollContainer.ScreenSpaceDrawQuad.Centre + new Vector2(1000, 1000)));
checkPosition(0);
AddStep("Release mouse button", () => InputManager.ReleaseButton(MouseButton.Left));
checkPosition(0);
AddStep("Nest vertical scroll inside of horizontal", () =>
{
Remove(scrollContainer, false);
Add(horizontalScrollContainer = new BasicScrollContainer(Direction.Horizontal)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(400),
ClampExtension = 0,
Children = new Drawable[]
{
new Box
{
Size = new Vector2(500, 400),
Colour = FrameworkColour.Yellow,
},
scrollContainer
},
});
});
AddStep("Click and drag horizontally", () =>
{
InputManager.MoveMouseTo(scrollContainer);
InputManager.PressButton(MouseButton.Left);
// Required for the dragging state to be set correctly.
InputManager.MoveMouseTo(scrollContainer.ToScreenSpace(scrollContainer.LayoutRectangle.Centre + new Vector2(20f, 0f)));
});
AddStep("Move mouse diagonally up", () => InputManager.MoveMouseTo(scrollContainer.ScreenSpaceDrawQuad.Centre - new Vector2(1000, 1000)));
AddUntilStep("horizontal position at 100", () => Precision.AlmostEquals(100, horizontalScrollContainer.Current, 1));
AddUntilStep("vertical position at 0", () => Precision.AlmostEquals(0, scrollContainer.Current, 1));
AddStep("Move mouse diagonally down", () => InputManager.MoveMouseTo(scrollContainer.ScreenSpaceDrawQuad.Centre + new Vector2(1000, 1000)));
AddUntilStep("horizontal position at 0", () => Precision.AlmostEquals(0, horizontalScrollContainer.Current, 1));
AddUntilStep("vertical position at 0", () => Precision.AlmostEquals(0, scrollContainer.Current, 1));
AddStep("Release mouse button", () => InputManager.ReleaseButton(MouseButton.Left));
AddUntilStep("horizontal position at 0", () => Precision.AlmostEquals(0, horizontalScrollContainer.Current, 1));
AddUntilStep("vertical position at 0", () => Precision.AlmostEquals(0, scrollContainer.Current, 1));
}
private void scrollIntoView(int index, float expectedPosition, float? heightAdjust = null, float? expectedPostAdjustPosition = null)
{
if (heightAdjust != null)
AddStep("set container height zero", () => scrollContainer.Height = heightAdjust.Value);
AddStep($"scroll {index} into view", () => scrollContainer.ScrollIntoView(fill.Skip(index).First()));
AddUntilStep($"{index} is visible", () => !fill.Skip(index).First().IsMaskedAway);
checkPosition(expectedPosition);
if (heightAdjust != null)
{
Debug.Assert(expectedPostAdjustPosition != null, nameof(expectedPostAdjustPosition) + " != null");
AddStep("restore height", () => scrollContainer.Height = 100);
checkPosition(expectedPostAdjustPosition.Value);
}
}
private void scrollTo(float position, float scrollContentHeight, float extension)
{
float clampedTarget = Math.Clamp(position, -extension, scrollContentHeight + extension);
float immediateScrollPosition = 0;
AddStep($"scroll to {position}", () =>
{
scrollContainer.ScrollTo(position, false);
immediateScrollPosition = (float)scrollContainer.Current;
});
AddAssert($"immediately scrolled to {clampedTarget}", () => Precision.AlmostEquals(clampedTarget, immediateScrollPosition, 1));
}
private void checkPosition(float expected, ScrollContainer<Drawable> scroll = null) => AddUntilStep($"position at {expected}", () => Precision.AlmostEquals(expected, (scroll ?? scrollContainer).Current, 1));
private void checkScrollbarPosition(float expected) =>
AddUntilStep($"scrollbar position at {expected}", () => Precision.AlmostEquals(expected, scrollContainer.InternalChildren[1].DrawPosition.Y, 1));
private partial class RepeatCountingScrollContainer : BasicScrollContainer
{
public int RepeatCount { get; set; }
protected override bool OnKeyDown(KeyDownEvent e)
{
if (e.Repeat)
RepeatCount++;
return base.OnKeyDown(e);
}
}
private partial class ClampedScrollbarScrollContainer : BasicScrollContainer
{
public new ScrollbarContainer Scrollbar => base.Scrollbar;
protected override ScrollbarContainer CreateScrollbar(Direction direction) => new ClampedScrollbar(direction);
private partial class ClampedScrollbar : BasicScrollbar
{
protected internal override float MinimumDimSize => 250;
public ClampedScrollbar(Direction direction)
: base(direction)
{
}
}
}
private partial class InputHandlingScrollContainer : BasicScrollContainer
{
public bool? ScrollHandled { get; private set; }
public bool? DragHandled { get; private set; }
protected override bool OnScroll(ScrollEvent e)
{
ScrollHandled = base.OnScroll(e);
return ScrollHandled.Value;
}
protected override bool OnDragStart(DragStartEvent e)
{
DragHandled = base.OnDragStart(e);
return DragHandled.Value;
}
}
}
}