1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-26 00:42:53 +08:00
osu-lazer/osu.Game/Screens/Play/SquareGraph.cs

271 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
using System;
using System.Collections.Generic;
using System.Linq;
2019-02-27 16:01:44 +08:00
using System.Threading;
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;
2020-02-24 19:52:15 +08:00
using osu.Framework.Layout;
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 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
2020-02-24 19:52:15 +08:00
public SquareGraph()
{
AddLayout(layout);
}
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;
layout.Invalidate();
}
}
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();
}
}
2020-02-24 19:52:15 +08:00
private readonly LayoutValue layout = new LayoutValue(Invalidation.DrawSize);
2019-02-27 18:11:09 +08:00
private ScheduledDelegate scheduledCreate;
2018-04-13 17:19:50 +08:00
protected override void Update()
{
base.Update();
2019-02-27 16:01:44 +08:00
if (values != null && !layout.IsValid)
2018-04-13 17:19:50 +08:00
{
2019-02-27 18:11:09 +08:00
columns?.FadeOut(500, Easing.OutQuint).Expire();
scheduledCreate?.Cancel();
scheduledCreate = Scheduler.AddDelayed(RecreateGraph, 500);
2018-04-13 17:19:50 +08:00
layout.Validate();
}
}
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>
{
CacheDrawnFrameBuffer = true,
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
}
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()
{
var newValues = new List<float>();
if (values == null)
{
for (float i = 0; i < ColumnCount; i++)
newValues.Add(0);
return;
}
var max = values.Max();
float step = values.Length / (float)ColumnCount;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
for (float i = 0; i < values.Length; i += step)
{
newValues.Add((float)values[(int)i] / max);
}
calculatedValues = newValues.ToArray();
}
public class Column : Container, IStateful<ColumnState>
{
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;
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);
}
}
2019-02-27 15:42:09 +08:00
public Column(float height)
2018-04-13 17:19:50 +08:00
{
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()
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
}
}
}