Undo stupid changes

This commit is contained in:
timiimit 2023-05-13 11:28:05 +02:00
parent 4b544903cb
commit d81cdacb0d
2 changed files with 51 additions and 83 deletions

View File

@ -63,11 +63,9 @@ namespace osu.Game.Tests.Visual.Gameplay
{ {
public int CreationCount { get; private set; } public int CreationCount { get; private set; }
protected override void UpdateGraph() protected override void RecreateGraph()
{ {
base.UpdateGraph(); base.RecreateGraph();
if (ColumnCount > 0)
CreationCount++; CreationCount++;
} }
} }

View File

@ -6,6 +6,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
using osu.Framework; using osu.Framework;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -15,6 +16,7 @@ using osuTK.Graphics;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Layout; using osu.Framework.Layout;
using osu.Framework.Threading;
namespace osu.Game.Screens.Play namespace osu.Game.Screens.Play
{ {
@ -22,8 +24,6 @@ namespace osu.Game.Screens.Play
{ {
private BufferedContainer<Column> columns; private BufferedContainer<Column> columns;
private readonly LayoutValue layout = new LayoutValue(Invalidation.DrawSize | Invalidation.DrawInfo);
public int ColumnCount => columns?.Children.Count ?? 0; public int ColumnCount => columns?.Children.Count ?? 0;
private int progress; private int progress;
@ -52,13 +52,10 @@ namespace osu.Game.Screens.Play
if (value == values) return; if (value == values) return;
values = value; values = value;
haveValuesChanged = true;
layout.Invalidate(); layout.Invalidate();
} }
} }
private bool haveValuesChanged;
private Color4 fillColour; private Color4 fillColour;
public Color4 FillColour public Color4 FillColour
@ -73,96 +70,66 @@ namespace osu.Game.Screens.Play
} }
} }
private ScheduledDelegate scheduledCreate;
private LayoutValue layout = new LayoutValue(Invalidation.DrawSize | Invalidation.DrawInfo);
public SquareGraph() public SquareGraph()
{ {
AddLayout(layout); AddLayout(layout);
} }
[BackgroundDependencyLoader]
private void load()
{
Child = columns = new BufferedContainer<Column>(cachedFrameBuffer: true)
{
RedrawOnScale = false,
RelativeSizeAxes = Axes.Both
};
}
protected override void Update() protected override void Update()
{ {
base.Update(); base.Update();
if (!layout.IsValid) if (!layout.IsValid)
{ {
UpdateGraph(); columns?.FadeOut(500, Easing.OutQuint).Expire();
scheduledCreate?.Cancel();
scheduledCreate = Scheduler.AddDelayed(RecreateGraph, 500);
layout.Validate(); layout.Validate();
} }
} }
private CancellationTokenSource cts;
/// <summary> /// <summary>
/// Updates the graph by either adding or removing columns based on DrawWidth. /// Recreates the entire graph.
/// Does nothing if correct number of columns already exists and/or if <see cref="SquareGraph.values"/> haven't changed.
/// </summary> /// </summary>
protected virtual void UpdateGraph() protected virtual void RecreateGraph()
{ {
int targetColumnCount = values == null ? 0 : (int)(DrawWidth / Column.WIDTH); var newColumns = new BufferedContainer<Column>(cachedFrameBuffer: true)
// early exit the most frequent case
if (!haveValuesChanged && targetColumnCount == ColumnCount)
{ {
updateColumnHeight(); RedrawOnScale = false,
columns.ForceRedraw(); RelativeSizeAxes = Axes.Both,
return; };
}
ensureColumnCount(targetColumnCount); for (float x = 0; x < DrawWidth; x += Column.WIDTH)
// fill graph data
recalculateValues();
redrawFilled();
redrawProgress();
haveValuesChanged = false;
}
private void updateColumnHeight()
{ {
foreach (var column in columns) newColumns.Add(new Column(DrawHeight)
{ {
column.Height = DrawHeight;
}
}
private void ensureColumnCount(int targetColumnCount)
{
// remove excess columns
while (targetColumnCount < ColumnCount)
{
columns.Remove(columns.Children[ColumnCount - 1], true);
}
updateColumnHeight();
// add missing columns
float x = ColumnCount * Column.WIDTH;
while (targetColumnCount > ColumnCount)
{
var column = new Column
{
Height = DrawHeight,
LitColour = fillColour, LitColour = fillColour,
Anchor = Anchor.BottomLeft, Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft, Origin = Anchor.BottomLeft,
Position = new Vector2(x, 0), Position = new Vector2(x, 0),
State = ColumnState.Dimmed, State = ColumnState.Dimmed,
}; });
LoadComponentAsync(column);
columns.Add(column);
x += Column.WIDTH;
} }
cts?.Cancel();
LoadComponentAsync(newColumns, c =>
{
Child = columns = c;
columns.FadeInFromZero(500, Easing.OutQuint);
recalculateValues();
redrawFilled();
redrawProgress();
}, (cts = new CancellationTokenSource()).Token);
} }
/// <summary> /// <summary>
@ -190,24 +157,26 @@ namespace osu.Game.Screens.Play
/// </summary> /// </summary>
private void recalculateValues() private void recalculateValues()
{ {
int columnCount = ColumnCount; var newValues = new List<float>();
if (values == null || values.Length == 0 || columnCount == 0)
if (values == null)
{ {
calculatedValues = new float[0]; for (float i = 0; i < ColumnCount; i++)
newValues.Add(0);
return; return;
} }
float ratio = values.Length / (float)columnCount; int max = values.Max();
if (calculatedValues.Length != columnCount) float step = values.Length / (float)ColumnCount;
calculatedValues = new float[columnCount];
float max = (float)values.Max(); for (float i = 0; i < values.Length; i += step)
for (int i = 0; i < calculatedValues.Length; i++)
{ {
calculatedValues[i] = values[(int)(i * ratio)] / max; newValues.Add((float)values[(int)i] / max);
} }
calculatedValues = newValues.ToArray();
} }
public partial class Column : Container, IStateful<ColumnState> public partial class Column : Container, IStateful<ColumnState>
@ -256,9 +225,10 @@ namespace osu.Game.Screens.Play
} }
} }
public Column() public Column(float height)
{ {
Width = WIDTH; Width = WIDTH;
Height = height;
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]