2019-01-24 16:43:03 +08:00
|
|
|
// 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;
|
2017-04-18 17:40:02 +08:00
|
|
|
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;
|
2017-06-20 13:54:23 +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;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-04-18 17:40:02 +08:00
|
|
|
namespace osu.Game.Screens.Play
|
|
|
|
{
|
2019-02-27 16:01:44 +08:00
|
|
|
public partial class SquareGraph : Container
|
2017-04-18 17:40:02 +08:00
|
|
|
{
|
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
|
|
|
|
2017-04-18 17:40:02 +08:00
|
|
|
private int progress;
|
2019-02-27 20:07:17 +08:00
|
|
|
|
2017-04-18 17:40:02 +08:00
|
|
|
public int Progress
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
get => progress;
|
2017-04-18 17:40:02 +08:00
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == progress) return;
|
2019-02-27 20:07:17 +08:00
|
|
|
|
2017-04-18 17:40:02 +08:00
|
|
|
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
|
|
|
|
2017-04-18 17:40:02 +08:00
|
|
|
private int[] values;
|
2019-02-27 20:07:17 +08:00
|
|
|
|
2017-04-18 17:40:02 +08:00
|
|
|
public int[] Values
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
get => values;
|
2017-04-18 17:40:02 +08:00
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == values) return;
|
2019-02-27 20:07:17 +08:00
|
|
|
|
2017-04-18 17:40:02 +08:00
|
|
|
values = value;
|
2023-05-13 07:29:11 +08:00
|
|
|
haveValuesChanged = true;
|
|
|
|
layout.Invalidate();
|
2017-04-18 17:40:02 +08:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2017-04-18 17:40:02 +08:00
|
|
|
private Color4 fillColour;
|
2019-02-27 20:07:17 +08:00
|
|
|
|
2017-04-18 17:40:02 +08:00
|
|
|
public Color4 FillColour
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
get => fillColour;
|
2017-04-18 17:40:02 +08:00
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == fillColour) return;
|
2019-02-27 20:07:17 +08:00
|
|
|
|
2017-04-18 17:40:02 +08:00
|
|
|
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
|
|
|
{
|
2019-09-04 18:38:12 +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
|
|
|
}
|
|
|
|
|
2017-04-18 17:40:02 +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++)
|
2017-04-18 17:40:02 +08:00
|
|
|
columns[i].State = i <= progress ? ColumnState.Lit : ColumnState.Dimmed;
|
2019-02-27 16:01:44 +08:00
|
|
|
columns?.ForceRedraw();
|
2017-04-18 17:40:02 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-04-18 17:40:02 +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();
|
2017-04-18 17:40:02 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-04-18 17:40:02 +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)
|
2017-04-18 17:40:02 +08:00
|
|
|
{
|
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;
|
|
|
|
}
|
2017-04-18 17:40:02 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-04-18 17:40:02 +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
|
|
|
|
2017-04-18 18:22:45 +08:00
|
|
|
private float cubeCount => DrawHeight / WIDTH;
|
2017-04-18 17:40:02 +08:00
|
|
|
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
|
|
|
|
2017-04-18 17:40:02 +08:00
|
|
|
private readonly List<Box> drawableRows = new List<Box>();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-04-18 18:22:45 +08:00
|
|
|
private float filled;
|
2019-02-27 20:07:17 +08:00
|
|
|
|
2017-04-18 18:22:45 +08:00
|
|
|
public float Filled
|
2017-04-18 17:40:02 +08:00
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
get => filled;
|
2017-04-18 17:40:02 +08:00
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == filled) return;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2019-02-27 20:07:17 +08:00
|
|
|
filled = value;
|
2017-04-18 17:40:02 +08:00
|
|
|
fillActive();
|
|
|
|
}
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-04-18 17:40:02 +08:00
|
|
|
private ColumnState state;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-04-18 17:40:02 +08:00
|
|
|
public ColumnState State
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
get => state;
|
2017-04-18 17:40:02 +08:00
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == state) return;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2019-02-27 20:07:17 +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);
|
2017-04-18 17:40:02 +08:00
|
|
|
}
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2023-05-13 07:29:11 +08:00
|
|
|
public Column()
|
2017-04-18 17:40:02 +08:00
|
|
|
{
|
2017-04-18 18:22:45 +08:00
|
|
|
Width = WIDTH;
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2019-02-27 15:42:09 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
2017-04-18 18:22:45 +08:00
|
|
|
{
|
2019-02-27 15:42:09 +08:00
|
|
|
drawableRows.AddRange(Enumerable.Range(0, (int)cubeCount).Select(r => new Box
|
2017-04-18 17:40:02 +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
|
|
|
|
2017-04-18 17:40:02 +08:00
|
|
|
Children = drawableRows;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-04-18 17:40:02 +08:00
|
|
|
// Reverse drawableRows so when iterating through them they start at the bottom
|
|
|
|
drawableRows.Reverse();
|
2019-03-05 12:16:41 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2019-03-05 12:16:41 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
2017-04-18 18:22:45 +08:00
|
|
|
fillActive();
|
2017-04-18 17:40:02 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-04-18 17:40:02 +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
|
|
|
|
2019-11-20 20:19:49 +08:00
|
|
|
int countFilled = (int)Math.Clamp(filled * drawableRows.Count, 0, drawableRows.Count);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-04-18 17:40:02 +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;
|
2017-04-18 17:40:02 +08:00
|
|
|
}
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-04-18 17:40:02 +08:00
|
|
|
public enum ColumnState
|
|
|
|
{
|
|
|
|
Lit,
|
|
|
|
Dimmed
|
|
|
|
}
|
|
|
|
}
|
2017-04-18 18:22:45 +08:00
|
|
|
}
|