Skip to content

Commit

Permalink
Merge pull request #27630 from EVAST9919/editor-performance
Browse files Browse the repository at this point in the history
Improve editor performance for maps with many control points
  • Loading branch information
peppy committed Mar 20, 2024
2 parents 773daee + fd509c8 commit 1f70019
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 25 deletions.
@@ -1,10 +1,9 @@
// 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.

using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Caching;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Screens.Edit.Components.Timelines.Summary.Parts;

Expand All @@ -15,6 +14,16 @@ namespace osu.Game.Screens.Edit.Compose.Components.Timeline
/// </summary>
public partial class TimelineControlPointDisplay : TimelinePart<TimelineControlPointGroup>
{
[Resolved]
private Timeline timeline { get; set; } = null!;

/// <summary>
/// The visible time/position range of the timeline.
/// </summary>
private (float min, float max) visibleRange = (float.MinValue, float.MaxValue);

private readonly Cached groupCache = new Cached();

private readonly IBindableList<ControlPointGroup> controlPointGroups = new BindableList<ControlPointGroup>();

protected override void LoadBeatmap(EditorBeatmap beatmap)
Expand All @@ -23,34 +32,67 @@ protected override void LoadBeatmap(EditorBeatmap beatmap)

controlPointGroups.UnbindAll();
controlPointGroups.BindTo(beatmap.ControlPointInfo.Groups);
controlPointGroups.BindCollectionChanged((_, args) =>
controlPointGroups.BindCollectionChanged((_, _) => groupCache.Invalidate(), true);
}

protected override void Update()
{
base.Update();

if (DrawWidth <= 0) return;

(float, float) newRange = (
(ToLocalSpace(timeline.ScreenSpaceDrawQuad.TopLeft).X - TopPointPiece.WIDTH) / DrawWidth * Content.RelativeChildSize.X,
(ToLocalSpace(timeline.ScreenSpaceDrawQuad.TopRight).X) / DrawWidth * Content.RelativeChildSize.X);

if (visibleRange != newRange)
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Reset:
Clear();
break;
visibleRange = newRange;
groupCache.Invalidate();
}

case NotifyCollectionChangedAction.Add:
Debug.Assert(args.NewItems != null);
if (!groupCache.IsValid)
{
recreateDrawableGroups();
groupCache.Validate();
}
}

foreach (var group in args.NewItems.OfType<ControlPointGroup>())
Add(new TimelineControlPointGroup(group));
break;
private void recreateDrawableGroups()
{
// Remove groups outside the visible range
foreach (TimelineControlPointGroup drawableGroup in this)
{
if (!shouldBeVisible(drawableGroup.Group))
drawableGroup.Expire();
}

case NotifyCollectionChangedAction.Remove:
Debug.Assert(args.OldItems != null);
// Add remaining ones
for (int i = 0; i < controlPointGroups.Count; i++)
{
var group = controlPointGroups[i];

foreach (var group in args.OldItems.OfType<ControlPointGroup>())
{
var matching = Children.SingleOrDefault(gv => ReferenceEquals(gv.Group, group));
if (!shouldBeVisible(group))
continue;

matching?.Expire();
}
bool alreadyVisible = false;

foreach (var g in this)
{
if (ReferenceEquals(g.Group, group))
{
alreadyVisible = true;
break;
}
}
}, true);

if (alreadyVisible)
continue;

Add(new TimelineControlPointGroup(group));
}
}

private bool shouldBeVisible(ControlPointGroup group) => group.Time >= visibleRange.min && group.Time <= visibleRange.max;
}
}
Expand Up @@ -19,12 +19,12 @@ public partial class TopPointPiece : CompositeDrawable

protected OsuSpriteText Label { get; private set; } = null!;

private const float width = 80;
public const float WIDTH = 80;

public TopPointPiece(ControlPoint point)
{
Point = point;
Width = width;
Width = WIDTH;
Height = 16;
Margin = new MarginPadding { Vertical = 4 };

Expand Down Expand Up @@ -65,7 +65,7 @@ private void load(OsuColour colours)
new Container
{
RelativeSizeAxes = Axes.Y,
Width = width - triangle_portion,
Width = WIDTH - triangle_portion,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Colour = Point.GetRepresentingColour(colours),
Expand Down

0 comments on commit 1f70019

Please sign in to comment.