-
Notifications
You must be signed in to change notification settings - Fork 424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use double
in ScrollContainer
for scroll tracking
#6467
Merged
smoogipoo
merged 7 commits into
ppy:master
from
peppy:scroll-container-double-precision
Jan 14, 2025
+193
−32
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
17ef9dc
Use `double` in `ScrollContainer` for scroll tracking
peppy 26c808f
Allow implementing a custom application of scroll container's current…
peppy 7ed84be
Add test scene showing how to make a scroll container for very long c…
peppy cc595be
Merge branch 'master' into scroll-container-double-precision
peppy 870cecd
Add missing xmldoc to new method
peppy 862a68d
Appease codefactor for good measure
peppy 0e558e2
Fix grammar
bdach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
osu.Framework.Tests/Visual/Containers/TestSceneScrollContainerDoublePrecision.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
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.Testing; | ||
using osu.Framework.Utils; | ||
using osuTK; | ||
using osuTK.Graphics; | ||
|
||
namespace osu.Framework.Tests.Visual.Containers | ||
{ | ||
public partial class TestSceneScrollContainerDoublePrecision : ManualInputManagerTestScene | ||
{ | ||
private const float item_height = 5000; | ||
private const int item_count = 8000; | ||
|
||
private ScrollContainer<Drawable> scrollContainer = null!; | ||
|
||
[SetUp] | ||
public void Setup() => Schedule(Clear); | ||
|
||
[Test] | ||
public void TestStandard() | ||
{ | ||
AddStep("Create scroll container", () => | ||
{ | ||
Add(scrollContainer = new BasicScrollContainer | ||
{ | ||
Anchor = Anchor.Centre, | ||
Origin = Anchor.Centre, | ||
ScrollbarVisible = true, | ||
RelativeSizeAxes = Axes.Both, | ||
Size = new Vector2(0.7f, 0.9f), | ||
}); | ||
|
||
for (int i = 0; i < item_count; i++) | ||
{ | ||
scrollContainer.Add(new BoxWithDouble | ||
{ | ||
Colour = new Color4(RNG.NextSingle(1), RNG.NextSingle(1), RNG.NextSingle(1), 1), | ||
RelativeSizeAxes = Axes.X, | ||
Height = item_height, | ||
Y = i * item_height, | ||
}); | ||
} | ||
}); | ||
|
||
scrollIntoView(item_count - 2); | ||
scrollIntoView(item_count - 1); | ||
} | ||
|
||
[Test] | ||
public void TestDoublePrecision() | ||
{ | ||
AddStep("Create scroll container", () => | ||
{ | ||
Add(scrollContainer = new DoubleScrollContainer | ||
{ | ||
Anchor = Anchor.Centre, | ||
Origin = Anchor.Centre, | ||
ScrollbarVisible = true, | ||
RelativeSizeAxes = Axes.Both, | ||
Size = new Vector2(0.7f, 0.9f), | ||
}); | ||
|
||
for (int i = 0; i < item_count; i++) | ||
{ | ||
scrollContainer.Add(new BoxWithDouble | ||
{ | ||
Colour = new Color4(RNG.NextSingle(1), RNG.NextSingle(1), RNG.NextSingle(1), 1), | ||
RelativeSizeAxes = Axes.X, | ||
Height = item_height, | ||
DoubleLocation = i * item_height, | ||
}); | ||
} | ||
}); | ||
|
||
scrollIntoView(item_count - 2); | ||
scrollIntoView(item_count - 1); | ||
} | ||
|
||
private void scrollIntoView(int index) | ||
{ | ||
AddStep($"scroll {index} into view", () => scrollContainer.ScrollIntoView(scrollContainer.ChildrenOfType<BoxWithDouble>().Skip(index).First())); | ||
AddUntilStep($"{index} is visible", () => !scrollContainer.ChildrenOfType<BoxWithDouble>().Skip(index).First().IsMaskedAway); | ||
} | ||
|
||
public partial class DoubleScrollContainer : BasicScrollContainer | ||
{ | ||
private readonly Container<BoxWithDouble> layoutContent; | ||
|
||
public override void Add(Drawable drawable) | ||
{ | ||
if (drawable is not BoxWithDouble boxWithDouble) | ||
throw new InvalidOperationException(); | ||
|
||
Add(boxWithDouble); | ||
} | ||
|
||
public void Add(BoxWithDouble drawable) | ||
{ | ||
if (drawable is not BoxWithDouble boxWithDouble) | ||
throw new InvalidOperationException(); | ||
|
||
layoutContent.Height = (float)Math.Max(layoutContent.Height, boxWithDouble.DoubleLocation + boxWithDouble.DrawHeight); | ||
layoutContent.Add(drawable); | ||
} | ||
|
||
public DoubleScrollContainer() | ||
{ | ||
// Managing our own custom layout within ScrollContent causes feedback with internal ScrollContainer calculations, | ||
// so we must maintain one level of separation from ScrollContent. | ||
base.Add(layoutContent = new Container<BoxWithDouble> | ||
{ | ||
RelativeSizeAxes = Axes.X, | ||
}); | ||
} | ||
|
||
public override double GetChildPosInContent(Drawable d, Vector2 offset) | ||
{ | ||
if (d is not BoxWithDouble boxWithDouble) | ||
return base.GetChildPosInContent(d, offset); | ||
|
||
return boxWithDouble.DoubleLocation + offset.X; | ||
} | ||
|
||
protected override void ApplyCurrentToContent() | ||
{ | ||
Debug.Assert(ScrollDirection == Direction.Vertical); | ||
|
||
double scrollableExtent = -Current + ScrollableExtent * ScrollContent.RelativeAnchorPosition.Y; | ||
|
||
foreach (var d in layoutContent) | ||
d.Y = (float)(d.DoubleLocation + scrollableExtent); | ||
} | ||
} | ||
|
||
public partial class BoxWithDouble : Box | ||
{ | ||
public double DoubleLocation { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just to confirm my understanding: this is virtual because positions everywhere else in framework are floats and without overriding this there is no way to recover precision lost on coordinates, correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's basically allowing the few places which require
double
to bedouble
. This one is important for accurate scrolling.