1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 19:27:26 +08:00
osu-lazer/osu.Game/Screens/Play/SongProgressGraph.cs

222 lines
6.4 KiB
C#
Raw Normal View History

2017-02-08 01:26:17 +08:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using OpenTK;
using OpenTK.Graphics;
using System.Linq;
2017-02-07 13:49:41 +08:00
using System.Collections.Generic;
2017-03-23 17:37:12 +08:00
using osu.Framework;
2017-03-23 19:13:03 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Containers;
2017-03-23 17:37:12 +08:00
using osu.Framework.Extensions.Color4Extensions;
2017-02-04 03:22:02 +08:00
namespace osu.Game.Screens.Play
{
public class SongProgressGraph : BufferedContainer
2017-02-04 03:22:02 +08:00
{
private Column[] columns = { };
2017-02-07 13:49:41 +08:00
2017-03-23 17:37:12 +08:00
public int ColumnCount => columns.Length;
2017-02-07 13:49:41 +08:00
public override bool HandleInput => false;
private int progress;
public int Progress
2017-02-07 13:49:41 +08:00
{
2017-03-23 17:37:12 +08:00
get { return progress; }
2017-02-07 13:49:41 +08:00
set
{
if (value == progress) return;
progress = value;
redrawProgress();
2017-02-07 13:49:41 +08:00
}
}
2017-03-23 17:37:12 +08:00
private int[] calculatedValues = { }; // values but adjusted to fit the amount of columns
2017-03-22 20:27:04 +08:00
private int[] values;
public int[] Values
{
2017-03-23 17:37:12 +08:00
get { return values; }
set
{
if (value == values) return;
values = value;
recreateGraph();
}
}
2017-03-23 17:37:12 +08:00
public SongProgressGraph()
{
CacheDrawnFrameBuffer = true;
PixelSnapping = true;
}
2017-03-24 10:38:23 +08:00
private float lastDrawWidth;
2017-03-23 17:37:12 +08:00
protected override void Update()
{
base.Update();
// todo: Recreating in update is probably not the best idea
if (DrawWidth == lastDrawWidth) return;
recreateGraph();
lastDrawWidth = DrawWidth;
}
/// <summary>
/// Redraws all the columns to match their lit/dimmed state.
/// </summary>
private void redrawProgress()
2017-02-07 13:49:41 +08:00
{
2017-03-23 17:37:12 +08:00
for (int i = 0; i < columns.Length; i++)
2017-02-07 13:49:41 +08:00
{
columns[i].State = i <= progress ? ColumnState.Lit : ColumnState.Dimmed;
}
ForceRedraw();
}
2017-03-23 17:37:12 +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);
}
}
2017-03-23 17:37:12 +08:00
/// <summary>
2017-03-23 18:24:43 +08:00
/// Takes <see cref="Values"/> and adjusts it to fit the amount of columns.
2017-03-23 17:37:12 +08:00
/// </summary>
private void recalculateValues()
{
2017-03-23 17:37:12 +08:00
var newValues = new List<int>();
if (values == null)
{
for (float i = 0; i < ColumnCount; i++)
2017-03-23 17:37:12 +08:00
newValues.Add(0);
return;
}
2017-03-22 20:27:04 +08:00
float step = values.Length / (float)ColumnCount;
for (float i = 0; i < values.Length; i += step)
{
2017-03-23 17:37:12 +08:00
newValues.Add(values[(int)i]);
}
2017-03-23 17:37:12 +08:00
calculatedValues = newValues.ToArray();
}
2017-03-23 17:37:12 +08:00
/// <summary>
/// Recreates the entire graph.
/// </summary>
private void recreateGraph()
{
2017-03-23 17:37:12 +08:00
var newColumns = new List<Column>();
2017-02-07 13:49:41 +08:00
for (float x = 0; x < DrawWidth; x += Column.WIDTH)
2017-02-07 13:49:41 +08:00
{
2017-03-23 17:37:12 +08:00
newColumns.Add(new Column
2017-02-07 13:49:41 +08:00
{
2017-03-23 19:13:03 +08:00
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
2017-03-23 17:20:00 +08:00
Position = new Vector2(x, 0),
2017-03-22 19:59:44 +08:00
State = ColumnState.Dimmed,
2017-02-07 13:49:41 +08:00
});
}
2017-03-23 17:37:12 +08:00
columns = newColumns.ToArray();
Children = columns;
recalculateValues();
redrawFilled();
redrawProgress();
}
public class Column : Container, IStateful<ColumnState>
{
2017-03-23 17:37:12 +08:00
private readonly Color4 emptyColour = Color4.White.Opacity(100);
private readonly Color4 litColour = new Color4(221, 255, 255, 255);
private readonly Color4 dimmedColour = Color4.White.Opacity(175);
private const float cube_count = 6;
private const float cube_size = 4;
private const float padding = 2;
public const float WIDTH = cube_size + padding;
2017-03-23 20:05:53 +08:00
public const float HEIGHT = cube_count * WIDTH + padding;
private readonly List<Box> drawableRows = new List<Box>();
2017-03-23 17:37:12 +08:00
private int filled;
public int Filled
{
get { return filled; }
set
{
if (value == filled) return;
filled = value;
fillActive();
}
}
private ColumnState state;
public ColumnState State
{
get { return state; }
set
{
if (value == state) return;
state = value;
fillActive();
}
}
public Column()
{
Size = new Vector2(WIDTH, HEIGHT);
2017-03-23 17:37:12 +08:00
2017-03-23 19:13:03 +08:00
for (int r = 0; r < cube_count; r++)
2017-03-23 17:37:12 +08:00
{
drawableRows.Add(new Box
{
EdgeSmoothness = new Vector2(padding / 4),
Size = new Vector2(cube_size),
2017-03-23 19:32:24 +08:00
Position = new Vector2(0, r * WIDTH + padding),
2017-03-23 17:37:12 +08:00
});
}
2017-03-24 10:38:23 +08:00
Children = drawableRows;
2017-03-23 17:37:12 +08:00
// Reverse drawableRows so when iterating through them they start at the bottom
drawableRows.Reverse();
}
private void fillActive()
{
Color4 colour = State == ColumnState.Lit ? litColour : dimmedColour;
for (int i = 0; i < drawableRows.Count; i++)
{
if (Filled == 0) // i <= Filled doesn't work for zero fill
drawableRows[i].Colour = emptyColour;
else
drawableRows[i].Colour = i <= Filled ? colour : emptyColour;
}
}
}
public enum ColumnState
{
2017-03-23 17:37:12 +08:00
Lit,
Dimmed
}
}
}