1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 19:17:25 +08:00
osu-lazer/osu.Game/Screens/Play/SquareGraph.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

270 lines
7.3 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2017-09-04 08:10:04 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
2019-02-27 16:01:44 +08:00
using System.Threading;
using osu.Framework;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
using osu.Framework.Graphics.Shapes;
2019-02-27 15:42:09 +08:00
using osu.Framework.Allocation;
2019-02-27 16:01:44 +08:00
using osu.Framework.Threading;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Play
{
2019-02-27 16:01:44 +08:00
public partial class SquareGraph : Container
{
2019-02-27 16:01:44 +08:00
private BufferedContainer<Column> columns;
2018-04-13 17:19:50 +08:00
2019-02-27 16:01:44 +08:00
public int ColumnCount => columns?.Children.Count ?? 0;
2018-04-13 17:19:50 +08:00
private int progress;
public int Progress
{
get => progress;
set
{
if (value == progress) return;
progress = value;
redrawProgress();
}
}
2018-04-13 17:19:50 +08:00
2019-11-28 21:41:29 +08:00
private float[] calculatedValues = Array.Empty<float>(); // values but adjusted to fit the amount of columns
2018-04-13 17:19:50 +08:00
private int[] values;
public int[] Values
{
get => values;
set
{
if (value == values) return;
values = value;
2022-09-17 21:40:36 +08:00
graphNeedsUpdate = true;
}
}
2018-04-13 17:19:50 +08:00
private Color4 fillColour;
public Color4 FillColour
{
get => fillColour;
set
{
if (value == fillColour) return;
fillColour = value;
redrawFilled();
}
}
2018-04-13 17:19:50 +08:00
2019-02-27 18:11:09 +08:00
private ScheduledDelegate scheduledCreate;
2018-04-13 17:19:50 +08:00
2022-09-17 22:05:25 +08:00
private bool graphNeedsUpdate;
2022-09-17 21:40:36 +08:00
2022-09-17 17:45:04 +08:00
private Vector2 previousDrawSize;
protected override void Update()
{
base.Update();
2018-04-13 17:19:50 +08:00
2022-09-17 21:40:36 +08:00
if (graphNeedsUpdate || (values != null && DrawSize != previousDrawSize))
{
2019-02-27 18:11:09 +08:00
columns?.FadeOut(500, Easing.OutQuint).Expire();
scheduledCreate?.Cancel();
scheduledCreate = Scheduler.AddDelayed(RecreateGraph, 500);
2022-09-17 17:45:04 +08:00
previousDrawSize = DrawSize;
2022-09-17 21:40:36 +08:00
graphNeedsUpdate = false;
}
}
2018-04-13 17:19:50 +08:00
2019-02-27 18:11:09 +08:00
private CancellationTokenSource cts;
2019-02-27 16:01:44 +08:00
/// <summary>
/// Recreates the entire graph.
/// </summary>
2019-02-27 18:11:09 +08:00
protected virtual void RecreateGraph()
2019-02-27 16:01:44 +08:00
{
var newColumns = new BufferedContainer<Column>(cachedFrameBuffer: true)
2019-02-27 16:01:44 +08:00
{
RedrawOnScale = false,
2019-02-27 16:01:44 +08:00
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();
LoadComponentAsync(newColumns, c =>
{
Child = columns = c;
columns.FadeInFromZero(500, Easing.OutQuint);
recalculateValues();
redrawFilled();
redrawProgress();
}, (cts = new CancellationTokenSource()).Token);
2019-02-27 16:01:44 +08:00
}
/// <summary>
/// Redraws all the columns to match their lit/dimmed state.
/// </summary>
private void redrawProgress()
{
2019-02-27 16:01:44 +08:00
for (int i = 0; i < ColumnCount; i++)
columns[i].State = i <= progress ? ColumnState.Lit : ColumnState.Dimmed;
2019-02-27 16:01:44 +08:00
columns?.ForceRedraw();
}
2018-04-13 17:19:50 +08:00
/// <summary>
/// Redraws the filled amount of all the columns.
/// </summary>
private void redrawFilled()
{
for (int i = 0; i < ColumnCount; i++)
columns[i].Filled = calculatedValues.ElementAtOrDefault(i);
2019-02-27 16:01:44 +08:00
columns?.ForceRedraw();
}
2018-04-13 17:19:50 +08:00
/// <summary>
/// Takes <see cref="Values"/> and adjusts it to fit the amount of columns.
/// </summary>
private void recalculateValues()
{
var newValues = new List<float>();
2018-04-13 17:19:50 +08:00
if (values == null)
{
for (float i = 0; i < ColumnCount; i++)
newValues.Add(0);
2018-04-13 17:19:50 +08:00
return;
}
2018-04-13 17:19:50 +08:00
int max = values.Max();
2018-04-13 17:19:50 +08:00
float step = values.Length / (float)ColumnCount;
2019-04-01 11:16:05 +08:00
for (float i = 0; i < values.Length; i += step)
{
newValues.Add((float)values[(int)i] / max);
}
2018-04-13 17:19:50 +08:00
calculatedValues = newValues.ToArray();
}
2018-04-13 17:19:50 +08:00
public partial class Column : Container, IStateful<ColumnState>
{
2017-04-18 19:23:03 +08:00
protected readonly Color4 EmptyColour = Color4.White.Opacity(20);
public Color4 LitColour = Color4.LightBlue;
protected readonly Color4 DimmedColour = Color4.White.Opacity(140);
2018-04-13 17:19:50 +08:00
private float cubeCount => DrawHeight / WIDTH;
private const float cube_size = 4;
private const float padding = 2;
public const float WIDTH = cube_size + padding;
2018-04-13 17:19:50 +08:00
2017-09-04 08:10:04 +08:00
public event Action<ColumnState> StateChanged;
2018-04-13 17:19:50 +08:00
private readonly List<Box> drawableRows = new List<Box>();
2018-04-13 17:19:50 +08:00
private float filled;
public float Filled
{
get => filled;
set
{
if (value == filled) return;
2018-04-13 17:19:50 +08:00
filled = value;
fillActive();
}
}
2018-04-13 17:19:50 +08:00
private ColumnState state;
2018-04-13 17:19:50 +08:00
public ColumnState State
{
get => state;
set
{
if (value == state) return;
2018-04-13 17:19:50 +08:00
state = value;
2017-04-18 19:23:03 +08:00
if (IsLoaded)
fillActive();
2018-04-13 17:19:50 +08:00
2017-09-04 08:10:04 +08:00
StateChanged?.Invoke(State);
}
}
2018-04-13 17:19:50 +08:00
2019-02-27 15:42:09 +08:00
public Column(float height)
{
Width = WIDTH;
2019-02-27 15:42:09 +08:00
Height = height;
}
2018-04-13 17:19:50 +08:00
2019-02-27 15:42:09 +08:00
[BackgroundDependencyLoader]
private void load()
{
2019-02-27 15:42:09 +08:00
drawableRows.AddRange(Enumerable.Range(0, (int)cubeCount).Select(r => new Box
{
2019-02-27 15:42:09 +08:00
Size = new Vector2(cube_size),
Position = new Vector2(0, r * WIDTH + padding),
}));
2018-04-13 17:19:50 +08:00
Children = drawableRows;
2018-04-13 17:19:50 +08:00
// Reverse drawableRows so when iterating through them they start at the bottom
drawableRows.Reverse();
}
2018-04-13 17:19:50 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
fillActive();
}
2018-04-13 17:19:50 +08:00
private void fillActive()
{
2017-04-18 19:23:03 +08:00
Color4 colour = State == ColumnState.Lit ? LitColour : DimmedColour;
2018-04-13 17:19:50 +08:00
int countFilled = (int)Math.Clamp(filled * drawableRows.Count, 0, drawableRows.Count);
2018-04-13 17:19:50 +08:00
for (int i = 0; i < drawableRows.Count; i++)
2017-04-18 19:23:03 +08:00
drawableRows[i].Colour = i < countFilled ? colour : EmptyColour;
}
}
2018-04-13 17:19:50 +08:00
public enum ColumnState
{
Lit,
Dimmed
}
}
}