mirror of
https://github.com/ppy/osu.git
synced 2024-11-11 17:47:29 +08:00
Merge pull request #647 from peppy/song-progress-fixes
Song progress refactor
This commit is contained in:
commit
437a4a5943
@ -16,6 +16,7 @@ namespace osu.Desktop.VisualTests.Tests
|
||||
public override string Description => @"With fake data";
|
||||
|
||||
private SongProgress progress;
|
||||
private SongProgressGraph graph;
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
@ -23,11 +24,20 @@ namespace osu.Desktop.VisualTests.Tests
|
||||
|
||||
Add(progress = new SongProgress
|
||||
{
|
||||
RelativeSizeAxes = Axes.X,
|
||||
AudioClock = new StopwatchClock(true),
|
||||
Anchor = Anchor.BottomLeft,
|
||||
Origin = Anchor.BottomLeft,
|
||||
});
|
||||
|
||||
Add(graph = new SongProgressGraph
|
||||
{
|
||||
RelativeSizeAxes = Axes.X,
|
||||
Height = 200,
|
||||
Anchor = Anchor.TopLeft,
|
||||
Origin = Anchor.TopLeft,
|
||||
});
|
||||
|
||||
AddStep("Toggle Bar", progress.ToggleBar);
|
||||
AddWaitStep(5);
|
||||
AddStep("Toggle Bar", progress.ToggleBar);
|
||||
@ -44,6 +54,7 @@ namespace osu.Desktop.VisualTests.Tests
|
||||
objects.Add(new HitObject { StartTime = i });
|
||||
|
||||
progress.Objects = objects;
|
||||
graph.Objects = objects;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ namespace osu.Game.Rulesets.UI
|
||||
Position = new Vector2(0, 30),
|
||||
};
|
||||
|
||||
protected override SongProgress CreateProgress() => new SongProgress()
|
||||
protected override SongProgress CreateProgress() => new SongProgress
|
||||
{
|
||||
Anchor = Anchor.BottomLeft,
|
||||
Origin = Anchor.BottomLeft,
|
||||
|
@ -4,7 +4,6 @@
|
||||
using OpenTK;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Graphics.Primitives;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using osu.Game.Graphics;
|
||||
@ -13,12 +12,13 @@ using System.Linq;
|
||||
using osu.Framework.Timing;
|
||||
using osu.Game.Rulesets.Objects;
|
||||
using osu.Game.Rulesets.Objects.Types;
|
||||
using osu.Framework.Graphics.Primitives;
|
||||
|
||||
namespace osu.Game.Screens.Play
|
||||
{
|
||||
public class SongProgress : OverlayContainer
|
||||
{
|
||||
private const int progress_height = 5;
|
||||
private const int bottom_bar_height = 5;
|
||||
|
||||
protected override bool HideOnEscape => false;
|
||||
|
||||
@ -41,25 +41,7 @@ namespace osu.Game.Screens.Play
|
||||
{
|
||||
set
|
||||
{
|
||||
objects = value;
|
||||
|
||||
const int granularity = 200;
|
||||
|
||||
var interval = lastHitTime / granularity;
|
||||
|
||||
var values = new int[granularity];
|
||||
|
||||
foreach (var h in objects)
|
||||
{
|
||||
IHasEndTime end = h as IHasEndTime;
|
||||
|
||||
int startRange = (int)(h.StartTime / interval);
|
||||
int endRange = (int)((end?.EndTime ?? h.StartTime) / interval);
|
||||
for (int i = startRange; i <= endRange; i++)
|
||||
values[i]++;
|
||||
}
|
||||
|
||||
graph.Values = values;
|
||||
graph.Objects = objects = value;
|
||||
}
|
||||
}
|
||||
|
||||
@ -71,9 +53,10 @@ namespace osu.Game.Screens.Play
|
||||
|
||||
public SongProgress()
|
||||
{
|
||||
RelativeSizeAxes = Axes.X;
|
||||
Height = progress_height + SongProgressGraph.Column.HEIGHT + handle_size.Y;
|
||||
Y = progress_height;
|
||||
const float graph_height = SquareGraph.Column.WIDTH * 6;
|
||||
|
||||
Height = bottom_bar_height + graph_height + handle_size.Y;
|
||||
Y = bottom_bar_height;
|
||||
|
||||
Children = new Drawable[]
|
||||
{
|
||||
@ -82,10 +65,10 @@ namespace osu.Game.Screens.Play
|
||||
RelativeSizeAxes = Axes.X,
|
||||
Origin = Anchor.BottomLeft,
|
||||
Anchor = Anchor.BottomLeft,
|
||||
Height = SongProgressGraph.Column.HEIGHT,
|
||||
Margin = new MarginPadding { Bottom = progress_height },
|
||||
Height = graph_height,
|
||||
Margin = new MarginPadding { Bottom = bottom_bar_height },
|
||||
},
|
||||
bar = new SongProgressBar(progress_height, SongProgressGraph.Column.HEIGHT, handle_size)
|
||||
bar = new SongProgressBar(bottom_bar_height, graph_height, handle_size)
|
||||
{
|
||||
Alpha = 0,
|
||||
Anchor = Anchor.BottomLeft,
|
||||
@ -114,7 +97,7 @@ namespace osu.Game.Screens.Play
|
||||
private void updateBarVisibility()
|
||||
{
|
||||
bar.FadeTo(barVisible ? 1 : 0, transition_duration, EasingTypes.In);
|
||||
MoveTo(new Vector2(0, barVisible ? 0 : progress_height), transition_duration, EasingTypes.In);
|
||||
MoveTo(new Vector2(0, barVisible ? 0 : bottom_bar_height), transition_duration, EasingTypes.In);
|
||||
}
|
||||
|
||||
protected override void PopIn()
|
||||
@ -132,11 +115,13 @@ namespace osu.Game.Screens.Play
|
||||
{
|
||||
base.Update();
|
||||
|
||||
if (objects == null)
|
||||
return;
|
||||
|
||||
double progress = (AudioClock?.CurrentTime ?? Time.Current) / lastHitTime;
|
||||
|
||||
bar.UpdatePosition((float)progress);
|
||||
graph.Progress = (int)(graph.ColumnCount * progress);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,235 +1,43 @@
|
||||
// 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;
|
||||
using System.Collections.Generic;
|
||||
using osu.Framework;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Sprites;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Extensions.Color4Extensions;
|
||||
using osu.Game.Rulesets.Objects.Types;
|
||||
using osu.Game.Rulesets.Objects;
|
||||
|
||||
namespace osu.Game.Screens.Play
|
||||
{
|
||||
public class SongProgressGraph : BufferedContainer
|
||||
public class SongProgressGraph : SquareGraph
|
||||
{
|
||||
private Column[] columns = { };
|
||||
private IEnumerable<HitObject> objects;
|
||||
|
||||
public int ColumnCount => columns.Length;
|
||||
|
||||
public override bool HandleInput => false;
|
||||
|
||||
private int progress;
|
||||
public int Progress
|
||||
public IEnumerable<HitObject> Objects
|
||||
{
|
||||
get { return progress; }
|
||||
set
|
||||
{
|
||||
if (value == progress) return;
|
||||
progress = value;
|
||||
objects = value;
|
||||
|
||||
redrawProgress();
|
||||
}
|
||||
}
|
||||
const int granularity = 200;
|
||||
|
||||
private int[] calculatedValues = { }; // values but adjusted to fit the amount of columns
|
||||
private int[] values;
|
||||
public int[] Values
|
||||
{
|
||||
get { return values; }
|
||||
set
|
||||
{
|
||||
if (value == values) return;
|
||||
values = value;
|
||||
recreateGraph();
|
||||
}
|
||||
}
|
||||
var lastHit = ((objects.Last() as IHasEndTime)?.EndTime ?? objects.Last().StartTime) + 1;
|
||||
|
||||
private Color4 fillColour;
|
||||
public Color4 FillColour
|
||||
{
|
||||
get { return fillColour; }
|
||||
set
|
||||
{
|
||||
if (value == fillColour) return;
|
||||
fillColour = value;
|
||||
var interval = lastHit / granularity;
|
||||
|
||||
redrawFilled();
|
||||
}
|
||||
}
|
||||
var values = new int[granularity];
|
||||
|
||||
public SongProgressGraph()
|
||||
{
|
||||
CacheDrawnFrameBuffer = true;
|
||||
PixelSnapping = true;
|
||||
}
|
||||
|
||||
private float lastDrawWidth;
|
||||
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()
|
||||
{
|
||||
for (int i = 0; i < columns.Length; i++)
|
||||
{
|
||||
columns[i].State = i <= progress ? ColumnState.Lit : ColumnState.Dimmed;
|
||||
}
|
||||
|
||||
ForceRedraw();
|
||||
}
|
||||
|
||||
/// <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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes <see cref="Values"/> and adjusts it to fit the amount of columns.
|
||||
/// </summary>
|
||||
private void recalculateValues()
|
||||
{
|
||||
var newValues = new List<int>();
|
||||
|
||||
if (values == null)
|
||||
{
|
||||
for (float i = 0; i < ColumnCount; i++)
|
||||
newValues.Add(0);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
float step = values.Length / (float)ColumnCount;
|
||||
for (float i = 0; i < values.Length; i += step)
|
||||
{
|
||||
newValues.Add(values[(int)i]);
|
||||
}
|
||||
|
||||
calculatedValues = newValues.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recreates the entire graph.
|
||||
/// </summary>
|
||||
private void recreateGraph()
|
||||
{
|
||||
var newColumns = new List<Column>();
|
||||
|
||||
for (float x = 0; x < DrawWidth; x += Column.WIDTH)
|
||||
{
|
||||
newColumns.Add(new Column(fillColour)
|
||||
foreach (var h in objects)
|
||||
{
|
||||
Anchor = Anchor.BottomLeft,
|
||||
Origin = Anchor.BottomLeft,
|
||||
Position = new Vector2(x, 0),
|
||||
State = ColumnState.Dimmed,
|
||||
});
|
||||
}
|
||||
IHasEndTime end = h as IHasEndTime;
|
||||
|
||||
columns = newColumns.ToArray();
|
||||
Children = columns;
|
||||
|
||||
recalculateValues();
|
||||
redrawFilled();
|
||||
redrawProgress();
|
||||
}
|
||||
|
||||
public class Column : Container, IStateful<ColumnState>
|
||||
{
|
||||
private readonly Color4 emptyColour = Color4.White.Opacity(100);
|
||||
private readonly Color4 litColour;
|
||||
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;
|
||||
public const float HEIGHT = cube_count * WIDTH + padding;
|
||||
|
||||
private readonly List<Box> drawableRows = new List<Box>();
|
||||
|
||||
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(Color4 litColour)
|
||||
{
|
||||
Size = new Vector2(WIDTH, HEIGHT);
|
||||
this.litColour = litColour;
|
||||
|
||||
for (int r = 0; r < cube_count; r++)
|
||||
{
|
||||
drawableRows.Add(new Box
|
||||
{
|
||||
EdgeSmoothness = new Vector2(padding / 4),
|
||||
Size = new Vector2(cube_size),
|
||||
Position = new Vector2(0, r * WIDTH + padding),
|
||||
});
|
||||
int startRange = (int)(h.StartTime / interval);
|
||||
int endRange = (int)((end?.EndTime ?? h.StartTime) / interval);
|
||||
for (int i = startRange; i <= endRange; i++)
|
||||
values[i]++;
|
||||
}
|
||||
|
||||
Children = drawableRows;
|
||||
|
||||
// Reverse drawableRows so when iterating through them they start at the bottom
|
||||
drawableRows.Reverse();
|
||||
Values = values;
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
Lit,
|
||||
Dimmed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
238
osu.Game/Screens/Play/SquareGraph.cs
Normal file
238
osu.Game/Screens/Play/SquareGraph.cs
Normal file
@ -0,0 +1,238 @@
|
||||
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using osu.Framework;
|
||||
using osu.Framework.Caching;
|
||||
using osu.Framework.Extensions.Color4Extensions;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Graphics.Sprites;
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics;
|
||||
|
||||
namespace osu.Game.Screens.Play
|
||||
{
|
||||
public class SquareGraph : BufferedContainer
|
||||
{
|
||||
private Column[] columns = { };
|
||||
|
||||
public int ColumnCount => columns.Length;
|
||||
|
||||
public override bool HandleInput => false;
|
||||
|
||||
private int progress;
|
||||
public int Progress
|
||||
{
|
||||
get { return progress; }
|
||||
set
|
||||
{
|
||||
if (value == progress) return;
|
||||
progress = value;
|
||||
redrawProgress();
|
||||
}
|
||||
}
|
||||
|
||||
private float[] calculatedValues = { }; // values but adjusted to fit the amount of columns
|
||||
|
||||
private int[] values;
|
||||
public int[] Values
|
||||
{
|
||||
get { return values; }
|
||||
set
|
||||
{
|
||||
if (value == values) return;
|
||||
values = value;
|
||||
layout.Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
private Color4 fillColour;
|
||||
public Color4 FillColour
|
||||
{
|
||||
get { return fillColour; }
|
||||
set
|
||||
{
|
||||
if (value == fillColour) return;
|
||||
fillColour = value;
|
||||
redrawFilled();
|
||||
}
|
||||
}
|
||||
|
||||
public SquareGraph()
|
||||
{
|
||||
CacheDrawnFrameBuffer = true;
|
||||
}
|
||||
|
||||
private Cached layout = new Cached();
|
||||
|
||||
public override bool Invalidate(Invalidation invalidation = Invalidation.All, Drawable source = null, bool shallPropagate = true)
|
||||
{
|
||||
if ((invalidation & Invalidation.SizeInParentSpace) > 0)
|
||||
layout.Invalidate();
|
||||
return base.Invalidate(invalidation, source, shallPropagate);
|
||||
}
|
||||
|
||||
protected override void Update()
|
||||
{
|
||||
base.Update();
|
||||
layout.Refresh(recreateGraph);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Redraws all the columns to match their lit/dimmed state.
|
||||
/// </summary>
|
||||
private void redrawProgress()
|
||||
{
|
||||
for (int i = 0; i < columns.Length; i++)
|
||||
columns[i].State = i <= progress ? ColumnState.Lit : ColumnState.Dimmed;
|
||||
ForceRedraw();
|
||||
}
|
||||
|
||||
/// <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);
|
||||
ForceRedraw();
|
||||
}
|
||||
|
||||
/// <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;
|
||||
for (float i = 0; i < values.Length; i += step)
|
||||
{
|
||||
newValues.Add((float)values[(int)i] / max);
|
||||
}
|
||||
|
||||
calculatedValues = newValues.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recreates the entire graph.
|
||||
/// </summary>
|
||||
private void recreateGraph()
|
||||
{
|
||||
var newColumns = new List<Column>();
|
||||
|
||||
for (float x = 0; x < DrawWidth; x += Column.WIDTH)
|
||||
{
|
||||
newColumns.Add(new Column
|
||||
{
|
||||
LitColour = fillColour,
|
||||
Anchor = Anchor.BottomLeft,
|
||||
Origin = Anchor.BottomLeft,
|
||||
Height = DrawHeight,
|
||||
Position = new Vector2(x, 0),
|
||||
State = ColumnState.Dimmed,
|
||||
});
|
||||
}
|
||||
|
||||
columns = newColumns.ToArray();
|
||||
Children = columns;
|
||||
|
||||
recalculateValues();
|
||||
redrawFilled();
|
||||
redrawProgress();
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
private readonly List<Box> drawableRows = new List<Box>();
|
||||
|
||||
private float filled;
|
||||
public float 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;
|
||||
|
||||
if (IsLoaded)
|
||||
fillActive();
|
||||
}
|
||||
}
|
||||
|
||||
public Column()
|
||||
{
|
||||
Width = WIDTH;
|
||||
}
|
||||
|
||||
protected override void LoadComplete()
|
||||
{
|
||||
for (int r = 0; r < cubeCount; r++)
|
||||
{
|
||||
drawableRows.Add(new Box
|
||||
{
|
||||
Size = new Vector2(cube_size),
|
||||
Position = new Vector2(0, r * WIDTH + padding),
|
||||
});
|
||||
}
|
||||
|
||||
Children = drawableRows;
|
||||
|
||||
// Reverse drawableRows so when iterating through them they start at the bottom
|
||||
drawableRows.Reverse();
|
||||
|
||||
fillActive();
|
||||
}
|
||||
|
||||
private void fillActive()
|
||||
{
|
||||
Color4 colour = State == ColumnState.Lit ? LitColour : DimmedColour;
|
||||
|
||||
int countFilled = (int)MathHelper.Clamp(filled * drawableRows.Count, 0, drawableRows.Count);
|
||||
|
||||
for (int i = 0; i < drawableRows.Count; i++)
|
||||
drawableRows[i].Colour = i < countFilled ? colour : EmptyColour;
|
||||
}
|
||||
}
|
||||
|
||||
public enum ColumnState
|
||||
{
|
||||
Lit,
|
||||
Dimmed
|
||||
}
|
||||
}
|
||||
}
|
@ -201,6 +201,7 @@
|
||||
<Compile Include="Screens\Charts\ChartInfo.cs" />
|
||||
<Compile Include="Screens\Edit\Editor.cs" />
|
||||
<Compile Include="Screens\Play\HotkeyRetryOverlay.cs" />
|
||||
<Compile Include="Screens\Play\SquareGraph.cs" />
|
||||
<Compile Include="Screens\ScreenWhiteBox.cs" />
|
||||
<Compile Include="Screens\Loader.cs" />
|
||||
<Compile Include="Screens\Menu\Button.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user