1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 17:23:05 +08:00
osu-lazer/osu.Game/Screens/Play/SquareGraph.cs

275 lines
7.4 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
2018-04-13 17:19:50 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
2023-05-13 17:28:05 +08:00
using System.Threading;
2023-10-30 14:10:10 +08:00
using JetBrains.Annotations;
2018-04-13 17:19:50 +08:00
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;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.Shapes;
2019-02-27 15:42:09 +08:00
using osu.Framework.Allocation;
2023-05-13 07:29:11 +08:00
using osu.Framework.Layout;
2023-05-13 17:28:05 +08:00
using osu.Framework.Threading;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Play
{
2022-11-24 13:32:20 +08:00
public partial class SquareGraph : Container
2018-04-13 17:19:50 +08:00
{
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;
2018-04-13 17:19:50 +08:00
public int Progress
{
get => progress;
2018-04-13 17:19:50 +08:00
set
{
if (value == progress) return;
2018-04-13 17:19:50 +08:00
progress = value;
redrawProgress();
}
}
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;
2018-04-13 17:19:50 +08:00
public int[] Values
{
get => values;
2018-04-13 17:19:50 +08:00
set
{
if (value == values) return;
2018-04-13 17:19:50 +08:00
values = value;
2023-05-13 07:29:11 +08:00
layout.Invalidate();
2018-04-13 17:19:50 +08:00
}
}
private Color4 fillColour;
2018-04-13 17:19:50 +08:00
public Color4 FillColour
{
get => fillColour;
2018-04-13 17:19:50 +08:00
set
{
if (value == fillColour) return;
2018-04-13 17:19:50 +08:00
fillColour = value;
redrawFilled();
}
}
2023-05-13 17:28:05 +08:00
private ScheduledDelegate scheduledCreate;
2023-05-13 17:37:56 +08:00
private readonly LayoutValue layout = new LayoutValue(Invalidation.DrawSize | Invalidation.DrawInfo);
2023-05-13 17:28:05 +08:00
2023-05-13 07:29:11 +08:00
public SquareGraph()
{
AddLayout(layout);
}
2019-02-27 16:01:44 +08:00
2023-05-13 07:29:11 +08:00
protected override void Update()
{
base.Update();
2023-05-13 07:39:01 +08:00
2023-05-13 07:29:11 +08:00
if (!layout.IsValid)
{
2023-05-13 17:28:05 +08:00
columns?.FadeOut(500, Easing.OutQuint).Expire();
2023-05-13 07:29:11 +08:00
2023-05-13 17:28:05 +08:00
scheduledCreate?.Cancel();
scheduledCreate = Scheduler.AddDelayed(RecreateGraph, 500);
2023-05-13 07:29:11 +08:00
2023-05-13 17:28:05 +08:00
layout.Validate();
2023-05-13 07:29:11 +08:00
}
}
2023-05-13 17:28:05 +08:00
private CancellationTokenSource cts;
2023-05-13 16:12:46 +08:00
2023-05-13 17:28:05 +08:00
/// <summary>
/// Recreates the entire graph.
/// </summary>
protected virtual void RecreateGraph()
2023-05-13 07:29:11 +08:00
{
2023-05-13 17:28:05 +08:00
var newColumns = new BufferedContainer<Column>(cachedFrameBuffer: true)
2023-05-13 07:29:11 +08:00
{
2023-05-13 17:28:05 +08:00
RedrawOnScale = false,
RelativeSizeAxes = Axes.Both,
};
2023-05-13 07:47:44 +08:00
2023-05-13 17:28:05 +08:00
for (float x = 0; x < DrawWidth; x += Column.WIDTH)
2019-02-27 16:01:44 +08:00
{
2023-05-13 17:28:05 +08:00
newColumns.Add(new Column(DrawHeight)
2019-02-27 16:01:44 +08:00
{
LitColour = fillColour,
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Position = new Vector2(x, 0),
State = ColumnState.Dimmed,
2023-05-13 17:28:05 +08:00
});
}
2019-02-27 16:01:44 +08:00
2023-05-13 17:28:05 +08:00
cts?.Cancel();
2019-02-27 16:01:44 +08:00
2023-05-13 17:28:05 +08:00
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
}
2018-04-13 17:19:50 +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++)
2018-04-13 17:19:50 +08:00
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()
{
2023-05-13 17:28:05 +08:00
var newValues = new List<float>();
if (values == null)
2018-04-13 17:19:50 +08:00
{
2023-05-13 17:28:05 +08:00
for (float i = 0; i < ColumnCount; i++)
newValues.Add(0);
2023-05-13 16:13:09 +08:00
return;
2023-05-13 07:27:28 +08:00
}
2019-04-01 11:16:05 +08:00
2023-05-13 17:28:05 +08:00
int max = values.Max();
2023-05-13 16:13:09 +08:00
2023-05-13 17:28:05 +08:00
float step = values.Length / (float)ColumnCount;
2023-05-13 16:13:09 +08:00
2023-05-13 17:28:05 +08:00
for (float i = 0; i < values.Length; i += step)
2023-05-13 16:13:09 +08:00
{
2023-05-13 17:28:05 +08:00
newValues.Add((float)values[(int)i] / max);
2023-05-13 16:13:09 +08:00
}
2023-05-13 17:28:05 +08:00
calculatedValues = newValues.ToArray();
2018-04-13 17:19:50 +08:00
}
2022-11-24 13:32:20 +08:00
public partial class Column : Container, IStateful<ColumnState>
2018-04-13 17:19:50 +08:00
{
protected readonly Color4 EmptyColour = Color4.White.Opacity(20);
public Color4 LitColour = Color4.LightBlue;
protected readonly Color4 DimmedColour = Color4.White.Opacity(140);
private float cubeCount => DrawHeight / WIDTH;
private const float cube_size = 4;
private const float padding = 2;
public const float WIDTH = cube_size + padding;
2023-10-30 14:10:10 +08:00
[CanBeNull]
2018-04-13 17:19:50 +08:00
public event Action<ColumnState> StateChanged;
private readonly List<Box> drawableRows = new List<Box>();
private float filled;
2018-04-13 17:19:50 +08:00
public float Filled
{
get => filled;
2018-04-13 17:19:50 +08:00
set
{
if (value == filled) return;
filled = value;
2018-04-13 17:19:50 +08:00
fillActive();
}
}
private ColumnState state;
public ColumnState State
{
get => state;
2018-04-13 17:19:50 +08:00
set
{
if (value == state) return;
state = value;
2018-04-13 17:19:50 +08:00
if (IsLoaded)
fillActive();
StateChanged?.Invoke(State);
}
}
2023-05-13 17:28:05 +08:00
public Column(float height)
2018-04-13 17:19:50 +08:00
{
Width = WIDTH;
2023-05-13 17:28:05 +08:00
Height = height;
2018-04-13 17:19:50 +08:00
}
2019-02-27 15:42:09 +08:00
[BackgroundDependencyLoader]
private void load()
2018-04-13 17:19:50 +08:00
{
2019-02-27 15:42:09 +08:00
drawableRows.AddRange(Enumerable.Range(0, (int)cubeCount).Select(r => new Box
2018-04-13 17:19:50 +08:00
{
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;
// 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();
2018-04-13 17:19:50 +08:00
fillActive();
}
private void fillActive()
{
Color4 colour = State == ColumnState.Lit ? LitColour : DimmedColour;
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++)
drawableRows[i].Colour = i < countFilled ? colour : EmptyColour;
}
}
public enum ColumnState
{
Lit,
Dimmed
}
}
}