1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-06 02:33:20 +08:00

Use struct for bars description

This commit is contained in:
Andrei Zavatski 2022-11-19 10:16:58 +03:00
parent 7d02cbafed
commit 9b8f98735c

View File

@ -42,7 +42,7 @@ namespace osu.Game.Graphics.UserInterface
} }
} }
private IEnumerable<BarDescriptor> bars; private readonly List<BarStruct> bars = new List<BarStruct>();
/// <summary> /// <summary>
/// A list of floats that defines the length of each <see cref="Bar"/> /// A list of floats that defines the length of each <see cref="Bar"/>
@ -51,30 +51,31 @@ namespace osu.Game.Graphics.UserInterface
{ {
set set
{ {
List<BarDescriptor> newBars = bars?.ToList() ?? new List<BarDescriptor>();
int newCount = value.Count(); int newCount = value.Count();
float size = newCount; float size = newCount;
if (size != 0) if (size != 0)
size = 1.0f / size; size = 1.0f / size;
foreach (var bar in value.Select((length, index) => new { Value = length, Bar = newBars.Count > index ? newBars[index] : null })) float maxLength = MaxValue ?? value.Max();
{
float length = MaxValue ?? value.Max();
if (length != 0)
length = Math.Max(0f, bar.Value / length);
if (bar.Bar != null) foreach (var bar in value.Select((length, index) => new { Value = length, Index = index }))
{ {
bar.Bar.OldValue = bar.Bar.Value; float length = maxLength == 0 ? 0 : Math.Max(0f, bar.Value / maxLength);
bar.Bar.Value = length; if (bar.Index < bars.Count)
bar.Bar.ShortSide = size; {
BarStruct b = bars[bar.Index];
b.OldValue = b.Value;
b.Value = length;
b.ShortSide = size;
bars[bar.Index] = b;
} }
else else
{ {
newBars.Add(new BarDescriptor bars.Add(new BarStruct
{ {
Value = length, Value = length,
ShortSide = size ShortSide = size
@ -82,10 +83,8 @@ namespace osu.Game.Graphics.UserInterface
} }
} }
if (newBars.Count > newCount) if (bars.Count > newCount)
newBars.RemoveRange(newCount, newBars.Count - newCount); bars.RemoveRange(newCount, bars.Count - newCount);
bars = newBars;
animationStartTime = Clock.CurrentTime; animationStartTime = Clock.CurrentTime;
animationComplete = false; animationComplete = false;
@ -109,22 +108,30 @@ namespace osu.Game.Graphics.UserInterface
{ {
base.Update(); base.Update();
if (noBars) if (!bars.Any())
return; return;
double currentTime = Clock.CurrentTime; double currentTime = Clock.CurrentTime;
if (currentTime < animationStartTime + resize_duration) if (currentTime < animationStartTime + resize_duration)
{ {
foreach (var bar in bars) for (int i = 0; i < bars.Count(); i++)
{
BarStruct bar = bars[i];
bar.IntermediateValue = Interpolation.ValueAt(currentTime, bar.OldValue, bar.Value, animationStartTime, animationStartTime + resize_duration, easing); bar.IntermediateValue = Interpolation.ValueAt(currentTime, bar.OldValue, bar.Value, animationStartTime, animationStartTime + resize_duration, easing);
bars[i] = bar;
}
Invalidate(Invalidation.DrawNode); Invalidate(Invalidation.DrawNode);
} }
else if (!animationComplete) else if (!animationComplete)
{ {
foreach (var bar in bars) for (int i = 0; i < bars.Count(); i++)
{
BarStruct bar = bars[i];
bar.IntermediateValue = bar.Value; bar.IntermediateValue = bar.Value;
bars[i] = bar;
}
Invalidate(Invalidation.DrawNode); Invalidate(Invalidation.DrawNode);
@ -132,8 +139,6 @@ namespace osu.Game.Graphics.UserInterface
} }
} }
private bool noBars => bars?.Any() != true;
protected override DrawNode CreateDrawNode() => new BarGraphDrawNode(this); protected override DrawNode CreateDrawNode() => new BarGraphDrawNode(this);
private class BarGraphDrawNode : DrawNode private class BarGraphDrawNode : DrawNode
@ -150,7 +155,7 @@ namespace osu.Game.Graphics.UserInterface
private Vector2 drawSize; private Vector2 drawSize;
private BarDirection direction; private BarDirection direction;
private readonly List<BarDescriptor> bars = new List<BarDescriptor>(); private readonly List<BarStruct> bars = new List<BarStruct>();
public override void ApplyState() public override void ApplyState()
{ {
@ -162,10 +167,6 @@ namespace osu.Game.Graphics.UserInterface
direction = Source.direction; direction = Source.direction;
bars.Clear(); bars.Clear();
if (Source.noBars)
return;
bars.AddRange(Source.bars); bars.AddRange(Source.bars);
} }
@ -225,7 +226,7 @@ namespace osu.Game.Graphics.UserInterface
} }
} }
private class BarDescriptor private struct BarStruct
{ {
public float OldValue { get; set; } public float OldValue { get; set; }
public float Value { get; set; } public float Value { get; set; }