2019-01-24 08:43:03 +00:00
|
|
|
|
// 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 09:19:50 +00:00
|
|
|
|
|
2018-11-20 07:51:59 +00:00
|
|
|
|
using osuTK;
|
2017-03-25 22:33:03 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
2017-04-03 17:28:05 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2022-11-18 21:19:49 +00:00
|
|
|
|
using osu.Framework.Graphics.Rendering;
|
|
|
|
|
using osu.Framework.Graphics.Shaders;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Graphics.Textures;
|
|
|
|
|
using osu.Framework.Graphics.Primitives;
|
|
|
|
|
using osu.Framework.Utils;
|
|
|
|
|
using System;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-04-03 17:28:05 +00:00
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
2017-03-25 22:33:03 +00:00
|
|
|
|
{
|
2022-11-18 21:19:49 +00:00
|
|
|
|
public partial class BarGraph : Drawable
|
2017-04-03 17:28:05 +00:00
|
|
|
|
{
|
2022-11-18 21:19:49 +00:00
|
|
|
|
private const int resize_duration = 250;
|
|
|
|
|
private const Easing easing = Easing.InOutCubic;
|
|
|
|
|
|
2017-04-10 14:42:23 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Manually sets the max value, if null <see cref="Enumerable.Max(IEnumerable{float})"/> is instead used
|
|
|
|
|
/// </summary>
|
|
|
|
|
public float? MaxValue { get; set; }
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-04-04 15:17:22 +00:00
|
|
|
|
private BarDirection direction = BarDirection.BottomToTop;
|
2019-02-28 04:31:40 +00:00
|
|
|
|
|
2022-11-18 21:19:49 +00:00
|
|
|
|
public BarDirection Direction
|
2017-04-04 15:17:22 +00:00
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => direction;
|
2017-04-04 15:17:22 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
2022-11-18 21:19:49 +00:00
|
|
|
|
if (direction == value)
|
|
|
|
|
return;
|
2019-04-01 03:16:05 +00:00
|
|
|
|
|
2022-11-18 21:19:49 +00:00
|
|
|
|
direction = value;
|
|
|
|
|
Invalidate(Invalidation.DrawNode);
|
2017-04-04 15:17:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-11-20 02:14:07 +00:00
|
|
|
|
private readonly BarsInfo bars = new BarsInfo();
|
2022-11-18 21:19:49 +00:00
|
|
|
|
|
2017-04-10 14:42:23 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A list of floats that defines the length of each <see cref="Bar"/>
|
|
|
|
|
/// </summary>
|
2017-04-03 17:28:05 +00:00
|
|
|
|
public IEnumerable<float> Values
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
2022-11-19 20:27:48 +00:00
|
|
|
|
if (!value.Any())
|
|
|
|
|
{
|
|
|
|
|
bars.Clear();
|
|
|
|
|
Invalidate(Invalidation.DrawNode);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float maxLength = MaxValue ?? value.Max();
|
2022-11-19 07:16:58 +00:00
|
|
|
|
|
2022-11-20 20:00:13 +00:00
|
|
|
|
bars.SetLengths(value.Select(v => maxLength == 0 ? 0 : Math.Max(0f, v / maxLength)).ToArray());
|
2022-11-18 21:19:49 +00:00
|
|
|
|
|
|
|
|
|
animationStartTime = Clock.CurrentTime;
|
|
|
|
|
animationComplete = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private double animationStartTime;
|
|
|
|
|
private bool animationComplete;
|
|
|
|
|
|
|
|
|
|
private IShader shader = null!;
|
|
|
|
|
private Texture texture = null!;
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(IRenderer renderer, ShaderManager shaders)
|
|
|
|
|
{
|
|
|
|
|
texture = renderer.WhitePixel;
|
|
|
|
|
shader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
|
2022-11-19 22:20:57 +00:00
|
|
|
|
if (!bars.Any)
|
2022-11-18 21:19:49 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
double currentTime = Clock.CurrentTime;
|
|
|
|
|
|
|
|
|
|
if (currentTime < animationStartTime + resize_duration)
|
|
|
|
|
{
|
2022-11-19 22:20:57 +00:00
|
|
|
|
bars.Animate(animationStartTime, currentTime);
|
2022-11-18 21:19:49 +00:00
|
|
|
|
Invalidate(Invalidation.DrawNode);
|
|
|
|
|
}
|
|
|
|
|
else if (!animationComplete)
|
|
|
|
|
{
|
2022-11-19 22:20:57 +00:00
|
|
|
|
bars.FinishAnimation();
|
2022-11-18 21:19:49 +00:00
|
|
|
|
Invalidate(Invalidation.DrawNode);
|
|
|
|
|
|
|
|
|
|
animationComplete = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DrawNode CreateDrawNode() => new BarGraphDrawNode(this);
|
|
|
|
|
|
|
|
|
|
private class BarGraphDrawNode : DrawNode
|
|
|
|
|
{
|
|
|
|
|
public new BarGraph Source => (BarGraph)base.Source;
|
|
|
|
|
|
|
|
|
|
public BarGraphDrawNode(BarGraph source)
|
|
|
|
|
: base(source)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IShader shader = null!;
|
|
|
|
|
private Texture texture = null!;
|
|
|
|
|
private Vector2 drawSize;
|
|
|
|
|
private BarDirection direction;
|
2022-11-19 20:40:02 +00:00
|
|
|
|
private float barBreadth;
|
2022-11-20 02:14:07 +00:00
|
|
|
|
|
|
|
|
|
private readonly List<float> lengths = new List<float>();
|
2022-11-18 21:19:49 +00:00
|
|
|
|
|
|
|
|
|
public override void ApplyState()
|
|
|
|
|
{
|
|
|
|
|
base.ApplyState();
|
|
|
|
|
|
|
|
|
|
shader = Source.shader;
|
|
|
|
|
texture = Source.texture;
|
|
|
|
|
drawSize = Source.DrawSize;
|
|
|
|
|
direction = Source.direction;
|
2022-11-20 02:14:07 +00:00
|
|
|
|
barBreadth = Source.bars.Breadth;
|
|
|
|
|
|
|
|
|
|
lengths.Clear();
|
|
|
|
|
lengths.AddRange(Source.bars.InstantaneousLengths);
|
2022-11-18 21:19:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Draw(IRenderer renderer)
|
|
|
|
|
{
|
|
|
|
|
base.Draw(renderer);
|
|
|
|
|
|
|
|
|
|
shader.Bind();
|
|
|
|
|
|
2022-11-20 02:14:07 +00:00
|
|
|
|
for (int i = 0; i < lengths.Count; i++)
|
2022-11-18 21:19:49 +00:00
|
|
|
|
{
|
2022-11-20 02:14:07 +00:00
|
|
|
|
float barHeight = drawSize.Y * ((direction == BarDirection.TopToBottom || direction == BarDirection.BottomToTop) ? lengths[i] : barBreadth);
|
|
|
|
|
float barWidth = drawSize.X * ((direction == BarDirection.LeftToRight || direction == BarDirection.RightToLeft) ? lengths[i] : barBreadth);
|
2022-11-18 21:19:49 +00:00
|
|
|
|
|
|
|
|
|
Vector2 topLeft;
|
|
|
|
|
|
|
|
|
|
switch (direction)
|
|
|
|
|
{
|
|
|
|
|
default:
|
|
|
|
|
case BarDirection.LeftToRight:
|
|
|
|
|
topLeft = new Vector2(0, i * barHeight);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case BarDirection.RightToLeft:
|
|
|
|
|
topLeft = new Vector2(drawSize.X - barWidth, i * barHeight);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case BarDirection.TopToBottom:
|
|
|
|
|
topLeft = new Vector2(i * barWidth, 0);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case BarDirection.BottomToTop:
|
|
|
|
|
topLeft = new Vector2(i * barWidth, drawSize.Y - barHeight);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-19 22:20:57 +00:00
|
|
|
|
renderer.DrawQuad(
|
|
|
|
|
texture,
|
|
|
|
|
new Quad(
|
|
|
|
|
Vector2Extensions.Transform(topLeft, DrawInfo.Matrix),
|
|
|
|
|
Vector2Extensions.Transform(topLeft + new Vector2(barWidth, 0), DrawInfo.Matrix),
|
|
|
|
|
Vector2Extensions.Transform(topLeft + new Vector2(0, barHeight), DrawInfo.Matrix),
|
|
|
|
|
Vector2Extensions.Transform(topLeft + new Vector2(barWidth, barHeight), DrawInfo.Matrix)
|
|
|
|
|
),
|
|
|
|
|
DrawColourInfo.Colour);
|
2022-11-18 21:19:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
shader.Unbind();
|
2017-04-03 17:28:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-18 21:19:49 +00:00
|
|
|
|
|
2022-11-20 02:14:07 +00:00
|
|
|
|
private class BarsInfo
|
2022-11-18 21:19:49 +00:00
|
|
|
|
{
|
2022-11-20 02:14:07 +00:00
|
|
|
|
public bool Any => Count > 0;
|
2022-11-19 22:20:57 +00:00
|
|
|
|
|
2022-11-20 02:14:07 +00:00
|
|
|
|
public int Count { get; private set; }
|
2022-11-19 22:20:57 +00:00
|
|
|
|
|
2022-11-20 20:00:13 +00:00
|
|
|
|
public float Breadth { get; private set; }
|
2022-11-19 22:20:57 +00:00
|
|
|
|
|
2022-11-20 02:14:07 +00:00
|
|
|
|
public List<float> InstantaneousLengths { get; } = new List<float>();
|
2022-11-19 22:20:57 +00:00
|
|
|
|
|
2022-11-20 02:14:07 +00:00
|
|
|
|
private readonly List<float> initialLengths = new List<float>();
|
|
|
|
|
private readonly List<float> finalLengths = new List<float>();
|
2022-11-19 22:20:57 +00:00
|
|
|
|
|
2022-11-20 20:00:13 +00:00
|
|
|
|
public void Clear() => SetLengths(Array.Empty<float>());
|
2022-11-19 22:20:57 +00:00
|
|
|
|
|
2022-11-20 20:00:13 +00:00
|
|
|
|
public void SetLengths(float[] newLengths)
|
2022-11-19 22:20:57 +00:00
|
|
|
|
{
|
2022-11-20 20:00:13 +00:00
|
|
|
|
int newCount = newLengths.Length;
|
2022-11-20 02:14:07 +00:00
|
|
|
|
|
2022-11-20 20:00:13 +00:00
|
|
|
|
for (int i = 0; i < newCount; i++)
|
|
|
|
|
{
|
|
|
|
|
// If we have an old bar at this index - change it's length
|
|
|
|
|
if (i < Count)
|
|
|
|
|
{
|
|
|
|
|
initialLengths[i] = finalLengths[i];
|
|
|
|
|
finalLengths[i] = newLengths[i];
|
2022-11-19 22:20:57 +00:00
|
|
|
|
|
2022-11-20 20:00:13 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-11-20 02:14:07 +00:00
|
|
|
|
|
2022-11-20 20:00:13 +00:00
|
|
|
|
// If exceeded old bars count - add new one
|
|
|
|
|
initialLengths.Add(0);
|
|
|
|
|
finalLengths.Add(newLengths[i]);
|
|
|
|
|
InstantaneousLengths.Add(0);
|
|
|
|
|
}
|
2022-11-19 22:20:57 +00:00
|
|
|
|
|
2022-11-20 20:00:13 +00:00
|
|
|
|
// Remove excessive bars
|
|
|
|
|
if (Count > newCount)
|
|
|
|
|
{
|
|
|
|
|
int barsToRemove = Count - newCount;
|
|
|
|
|
|
|
|
|
|
initialLengths.RemoveRange(newCount, barsToRemove);
|
|
|
|
|
finalLengths.RemoveRange(newCount, barsToRemove);
|
|
|
|
|
InstantaneousLengths.RemoveRange(newCount, barsToRemove);
|
|
|
|
|
}
|
2022-11-20 02:14:07 +00:00
|
|
|
|
|
2022-11-20 20:00:13 +00:00
|
|
|
|
Count = newCount;
|
|
|
|
|
Breadth = Count == 0 ? 0 : (1f / Count);
|
2022-11-19 22:20:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Animate(double animationStartTime, double currentTime)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < Count; i++)
|
2022-11-20 02:14:07 +00:00
|
|
|
|
InstantaneousLengths[i] = Interpolation.ValueAt(currentTime, initialLengths[i], finalLengths[i], animationStartTime, animationStartTime + resize_duration, easing);
|
2022-11-19 22:20:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void FinishAnimation()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < Count; i++)
|
2022-11-20 02:14:07 +00:00
|
|
|
|
InstantaneousLengths[i] = finalLengths[i];
|
2022-11-19 22:20:57 +00:00
|
|
|
|
}
|
2022-11-18 21:19:49 +00:00
|
|
|
|
}
|
2017-04-03 17:28:05 +00:00
|
|
|
|
}
|
2018-01-05 11:21:19 +00:00
|
|
|
|
}
|