osu/osu.Game/Screens/Play/SongProgressGraph.cs

131 lines
3.5 KiB
C#
Raw Normal View History

2017-02-07 17:26:17 +00: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;
2017-02-07 05:49:41 +00:00
using System.Collections.Generic;
using osu.Framework.Graphics.Containers;
2017-02-03 19:22:02 +00:00
namespace osu.Game.Screens.Play
{
public class SongProgressGraph : BufferedContainer
2017-02-03 19:22:02 +00:00
{
2017-02-07 05:49:41 +00:00
private List<SongProgressGraphColumn> columns = new List<SongProgressGraphColumn>();
private float lastDrawWidth;
2017-02-07 05:49:41 +00:00
public override bool HandleInput => false;
public int ColumnCount => columns.Count;
2017-02-07 05:49:41 +00:00
private int progress;
public int Progress
2017-02-07 05:49:41 +00:00
{
get
{
return progress;
}
set
{
if (value == progress) return;
progress = value;
redrawProgress();
2017-02-07 05:49:41 +00:00
}
}
private List<int> calculatedValues = new List<int>(); // values but adjusted to fit the amount of columns
private List<int> values;
public List<int> Values
{
get
{
return values;
}
set
{
if (value == values) return;
values = value;
recreateGraph();
}
}
private void redrawProgress()
2017-02-07 05:49:41 +00:00
{
for (int i = 0; i < columns.Count; i++)
2017-02-07 05:49:41 +00:00
{
columns[i].State = i <= progress ? ColumnState.Lit : ColumnState.Dimmed;
}
ForceRedraw();
}
private void redrawFilled()
{
for (int i = 0; i < ColumnCount; i++)
{
columns[i].Filled = calculatedValues[i];
}
ForceRedraw();
}
private void recalculateValues()
{
// Resizes values to fit the amount of columns and stores it in calculatedValues
// Defaults to all zeros if values is null
calculatedValues.RemoveAll(delegate { return true; });
if (values == null)
{
for (float i = 0; i < ColumnCount; i++)
{
calculatedValues.Add(0);
}
return;
}
2017-03-22 11:59:44 +00:00
float step = values.Count / ColumnCount;
for (float i = 0; i < values.Count; i += step)
{
calculatedValues.Add(values[(int)i]);
}
}
private void recreateGraph()
{
RemoveAll(delegate { return true; });
columns.RemoveAll(delegate { return true; });
2017-02-07 05:49:41 +00:00
for (int x = 0; x < DrawWidth; x += 3)
2017-02-07 05:49:41 +00:00
{
columns.Add(new SongProgressGraphColumn
{
Position = new Vector2(x + 1, 0),
2017-03-22 11:59:44 +00:00
State = ColumnState.Dimmed,
2017-02-07 05:49:41 +00:00
});
Add(columns[columns.Count - 1]);
}
recalculateValues();
redrawFilled();
redrawProgress();
}
protected override void Update()
{
base.Update();
if (DrawWidth == lastDrawWidth) return;
recreateGraph();
lastDrawWidth = DrawWidth;
}
public SongProgressGraph()
{
CacheDrawnFrameBuffer = true;
PixelSnapping = true;
}
}
}