1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-13 22:47:26 +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.

303 lines
8.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;
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;
2023-05-13 07:29:11 +08:00
using osu.Framework.Layout;
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
2023-05-13 07:29:11 +08:00
private readonly LayoutValue layout = new LayoutValue(Invalidation.DrawSize | Invalidation.DrawInfo);
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;
2023-05-13 07:29:11 +08:00
haveValuesChanged = true;
layout.Invalidate();
}
}
2018-04-13 17:19:50 +08:00
2023-05-13 07:39:01 +08:00
private bool haveValuesChanged;
2023-05-13 07:29:11 +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
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
[BackgroundDependencyLoader]
private void load()
2019-02-27 16:01:44 +08:00
{
2023-05-13 07:29:11 +08:00
Child = columns = new BufferedContainer<Column>(cachedFrameBuffer: true)
2019-02-27 16:01:44 +08:00
{
RedrawOnScale = false,
2023-05-13 07:29:11 +08:00
RelativeSizeAxes = Axes.Both
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)
{
UpdateGraph();
layout.Validate();
}
}
/// <summary>
/// Updates the graph by either adding or removing columns based on DrawWidth.
/// Does nothing if correct number of columns already exists and/or if <see cref="SquareGraph.values"/> haven't changed.
/// </summary>
protected virtual void UpdateGraph()
{
int targetColumnCount = values == null ? 0 : (int)(DrawWidth / Column.WIDTH);
// early exit the most frequent case
if (!haveValuesChanged && targetColumnCount == ColumnCount)
{
2023-05-13 16:12:46 +08:00
updateColumnHeight();
2023-05-13 07:29:11 +08:00
columns.ForceRedraw();
return;
}
2019-02-27 16:01:44 +08:00
2023-05-13 07:29:11 +08:00
ensureColumnCount(targetColumnCount);
// fill graph data
recalculateValues();
redrawFilled();
redrawProgress();
haveValuesChanged = false;
}
2023-05-13 16:12:46 +08:00
private void updateColumnHeight()
{
foreach (var column in columns)
{
column.Height = DrawHeight;
}
}
2023-05-13 07:29:11 +08:00
private void ensureColumnCount(int targetColumnCount)
{
// remove excess columns
while (targetColumnCount < ColumnCount)
{
columns.Remove(columns.Children[ColumnCount - 1], true);
}
2023-05-13 16:12:46 +08:00
updateColumnHeight();
2023-05-13 07:29:11 +08:00
// add missing columns
float x = ColumnCount * Column.WIDTH;
2023-05-13 07:47:44 +08:00
2023-05-13 07:29:11 +08:00
while (targetColumnCount > ColumnCount)
2019-02-27 16:01:44 +08:00
{
2023-05-13 07:47:44 +08:00
var column = new Column
2019-02-27 16:01:44 +08:00
{
2023-05-13 07:29:11 +08:00
Height = 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 07:29:11 +08:00
};
2019-02-27 16:01:44 +08:00
2023-05-13 07:29:11 +08:00
LoadComponentAsync(column);
columns.Add(column);
2019-02-27 16:01:44 +08:00
2023-05-13 07:29:11 +08:00
x += Column.WIDTH;
}
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()
{
2023-05-13 16:13:09 +08:00
int columnCount = ColumnCount;
if (values == null || values.Length == 0 || columnCount == 0)
{
2023-05-13 16:13:09 +08:00
calculatedValues = new float[0];
return;
2023-05-13 07:27:28 +08:00
}
2019-04-01 11:16:05 +08:00
2023-05-13 16:13:09 +08:00
float ratio = values.Length / (float)columnCount;
if (calculatedValues.Length != columnCount)
calculatedValues = new float[columnCount];
float max = (float)values.Max();
2018-04-13 17:19:50 +08:00
2023-05-13 16:13:09 +08:00
for (int i = 0; i < calculatedValues.Length; i++)
{
calculatedValues[i] = values[(int)(i * ratio)] / max;
}
}
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
2023-05-13 07:29:11 +08:00
public Column()
{
Width = WIDTH;
}
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
}
}
}