1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 23:12:56 +08:00

Debounce SquareGraph some more

This commit is contained in:
Dean Herbert 2019-02-27 17:01:44 +09:00
parent 8c4a59e57e
commit 6f5d4ce2e2

View File

@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using osu.Framework;
using osu.Framework.Caching;
using osu.Framework.Extensions.Color4Extensions;
@ -12,18 +13,19 @@ using osu.Framework.Graphics.Containers;
using osuTK;
using osuTK.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Framework.MathUtils;
using osu.Framework.Allocation;
using osu.Framework.Threading;
namespace osu.Game.Screens.Play
{
public class SquareGraph : BufferedContainer
public class SquareGraph : Container
{
private Column[] columns = { };
private BufferedContainer<Column> columns;
public int ColumnCount => columns.Length;
public int ColumnCount => columns?.Children.Count ?? 0;
private int progress;
public int Progress
{
get { return progress; }
@ -38,6 +40,7 @@ namespace osu.Game.Screens.Play
private float[] calculatedValues = { }; // values but adjusted to fit the amount of columns
private int[] values;
public int[] Values
{
get { return values; }
@ -50,6 +53,7 @@ namespace osu.Game.Screens.Play
}
private Color4 fillColour;
public Color4 FillColour
{
get { return fillColour; }
@ -61,35 +65,83 @@ namespace osu.Game.Screens.Play
}
}
public SquareGraph()
public override bool Invalidate(Invalidation invalidation = Invalidation.All, Drawable source = null, bool shallPropagate = true)
{
CacheDrawnFrameBuffer = true;
if ((invalidation & Invalidation.DrawSize) > 0)
layout.Invalidate();
return base.Invalidate(invalidation, source, shallPropagate);
}
private Cached layout = new Cached();
private float lastDrawWidth;
protected override void Update()
{
base.Update();
if (values != null && (!layout.IsValid || !Precision.AlmostEquals(lastDrawWidth, DrawWidth, 5)))
if (values != null && !layout.IsValid)
{
recreateGraph();
lastDrawWidth = DrawWidth;
schedulerRecreateGraph();
layout.Validate();
}
}
private ScheduledDelegate scheduledCreate;
/// <summary>
/// Recreates the entire graph.
/// </summary>
private void schedulerRecreateGraph()
{
columns?.FadeOut(500, Easing.OutQuint).Expire();
scheduledCreate?.Cancel();
scheduledCreate = Scheduler.AddDelayed(recreateGraph, 500);
}
private CancellationTokenSource cts;
private void recreateGraph()
{
var newColumns = new BufferedContainer<Column>
{
CacheDrawnFrameBuffer = true,
RelativeSizeAxes = Axes.Both,
};
for (float x = 0; x < DrawWidth; x += Column.WIDTH)
{
newColumns.Add(new Column(DrawHeight)
{
LitColour = fillColour,
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Position = new Vector2(x, 0),
State = ColumnState.Dimmed,
});
}
cts?.Cancel();
cts = new CancellationTokenSource();
LoadComponentAsync(newColumns, c =>
{
Child = columns = c;
columns.FadeInFromZero(500, Easing.OutQuint);
recalculateValues();
redrawFilled();
redrawProgress();
}, cts.Token);
}
/// <summary>
/// Redraws all the columns to match their lit/dimmed state.
/// </summary>
private void redrawProgress()
{
for (int i = 0; i < columns.Length; i++)
for (int i = 0; i < ColumnCount; i++)
columns[i].State = i <= progress ? ColumnState.Lit : ColumnState.Dimmed;
ForceRedraw();
columns?.ForceRedraw();
}
/// <summary>
@ -99,7 +151,7 @@ namespace osu.Game.Screens.Play
{
for (int i = 0; i < ColumnCount; i++)
columns[i].Filled = calculatedValues.ElementAtOrDefault(i);
ForceRedraw();
columns?.ForceRedraw();
}
/// <summary>
@ -128,34 +180,6 @@ namespace osu.Game.Screens.Play
calculatedValues = newValues.ToArray();
}
/// <summary>
/// Recreates the entire graph.
/// </summary>
private void recreateGraph()
{
var newColumns = new List<Column>();
for (float x = 0; x < DrawWidth; x += Column.WIDTH)
{
newColumns.Add(new Column
{
LitColour = fillColour,
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Height = DrawHeight,
Position = new Vector2(x, 0),
State = ColumnState.Dimmed,
});
}
columns = newColumns.ToArray();
Children = columns;
recalculateValues();
redrawFilled();
redrawProgress();
}
public class Column : Container, IStateful<ColumnState>
{
protected readonly Color4 EmptyColour = Color4.White.Opacity(20);
@ -172,6 +196,7 @@ namespace osu.Game.Screens.Play
private readonly List<Box> drawableRows = new List<Box>();
private float filled;
public float Filled
{
get { return filled; }