2023-01-10 04:35:27 +08: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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2023-01-11 05:49:35 +08:00
|
|
|
using osu.Framework.Allocation;
|
2023-01-10 04:35:27 +08:00
|
|
|
using osu.Framework.Graphics;
|
2023-01-11 05:49:35 +08:00
|
|
|
using osu.Framework.Graphics.Primitives;
|
|
|
|
using osu.Framework.Graphics.Rendering;
|
|
|
|
using osu.Framework.Graphics.Shaders;
|
|
|
|
using osu.Framework.Graphics.Textures;
|
2023-01-10 04:35:27 +08:00
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
{
|
2023-01-11 18:22:18 +08:00
|
|
|
public partial class SegmentedGraph<T> : Drawable
|
2023-01-10 04:35:27 +08:00
|
|
|
where T : struct, IComparable<T>, IConvertible, IEquatable<T>
|
|
|
|
{
|
|
|
|
private bool graphNeedsUpdate;
|
|
|
|
|
|
|
|
private T[]? values;
|
|
|
|
private int[] tiers = Array.Empty<int>();
|
|
|
|
private readonly SegmentManager segments;
|
|
|
|
|
2023-01-12 17:57:12 +08:00
|
|
|
private int tierCount;
|
2023-01-10 04:35:27 +08:00
|
|
|
|
2023-01-12 17:57:12 +08:00
|
|
|
public SegmentedGraph(int tierCount = 1)
|
2023-01-10 04:35:27 +08:00
|
|
|
{
|
|
|
|
this.tierCount = tierCount;
|
2023-01-12 17:57:12 +08:00
|
|
|
tierColours = new[]
|
|
|
|
{
|
|
|
|
new Colour4(0, 0, 0, 0)
|
|
|
|
};
|
2023-01-10 04:35:27 +08:00
|
|
|
segments = new SegmentManager(tierCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public T[] Values
|
|
|
|
{
|
|
|
|
get => values ?? Array.Empty<T>();
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == values) return;
|
|
|
|
|
|
|
|
values = value;
|
|
|
|
graphNeedsUpdate = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 17:13:16 +08:00
|
|
|
private Colour4[] tierColours;
|
|
|
|
|
|
|
|
public Colour4[] TierColours
|
|
|
|
{
|
|
|
|
get => tierColours;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value.Length == 0 || value == tierColours)
|
|
|
|
return;
|
|
|
|
|
2023-01-12 17:57:12 +08:00
|
|
|
tierCount = value.Length;
|
|
|
|
tierColours = value;
|
2023-01-12 17:13:16 +08:00
|
|
|
|
|
|
|
graphNeedsUpdate = true;
|
|
|
|
}
|
|
|
|
}
|
2023-01-10 04:35:27 +08:00
|
|
|
|
2023-01-11 05:49:35 +08:00
|
|
|
private Texture texture = null!;
|
|
|
|
private IShader shader = null!;
|
2023-01-10 04:35:27 +08:00
|
|
|
|
2023-01-11 05:49:35 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(IRenderer renderer, ShaderManager shaders)
|
2023-01-10 04:35:27 +08:00
|
|
|
{
|
2023-01-11 05:49:35 +08:00
|
|
|
texture = renderer.WhitePixel;
|
|
|
|
shader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE);
|
2023-01-10 04:35:27 +08:00
|
|
|
}
|
|
|
|
|
2023-01-11 05:49:35 +08:00
|
|
|
protected override void Update()
|
2023-01-10 04:35:27 +08:00
|
|
|
{
|
2023-01-11 05:49:35 +08:00
|
|
|
base.Update();
|
2023-01-10 04:35:27 +08:00
|
|
|
|
2023-01-11 05:49:35 +08:00
|
|
|
if (graphNeedsUpdate)
|
2023-01-10 04:35:27 +08:00
|
|
|
{
|
2023-01-12 09:36:35 +08:00
|
|
|
recalculateTiers(values);
|
2023-01-11 05:49:35 +08:00
|
|
|
recalculateSegments();
|
|
|
|
Invalidate(Invalidation.DrawNode);
|
2023-01-12 09:36:35 +08:00
|
|
|
graphNeedsUpdate = false;
|
2023-01-11 05:49:35 +08:00
|
|
|
}
|
2023-01-10 04:35:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void recalculateTiers(T[]? arr)
|
|
|
|
{
|
|
|
|
if (arr == null || arr.Length == 0)
|
|
|
|
{
|
|
|
|
tiers = Array.Empty<int>();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
float[] floatValues = arr.Select(v => Convert.ToSingle(v)).ToArray();
|
|
|
|
|
|
|
|
// Shift values to eliminate negative ones
|
|
|
|
float min = floatValues.Min();
|
|
|
|
|
|
|
|
if (min < 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < floatValues.Length; i++)
|
2023-01-11 21:03:36 +08:00
|
|
|
floatValues[i] += Math.Abs(min);
|
2023-01-10 04:35:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize values
|
|
|
|
float max = floatValues.Max();
|
|
|
|
|
|
|
|
for (int i = 0; i < floatValues.Length; i++)
|
|
|
|
floatValues[i] /= max;
|
|
|
|
|
|
|
|
// Deduce tiers from values
|
|
|
|
tiers = floatValues.Select(v => (int)Math.Floor(v * tierCount)).ToArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void recalculateSegments()
|
|
|
|
{
|
|
|
|
segments.Clear();
|
|
|
|
|
|
|
|
if (tiers.Length == 0)
|
|
|
|
{
|
|
|
|
segments.Add(0, 0, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < tiers.Length; i++)
|
|
|
|
{
|
|
|
|
for (int tier = 0; tier < tierCount; tier++)
|
|
|
|
{
|
|
|
|
if (tier < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// One tier covers itself and all tiers above it.
|
|
|
|
// By layering multiple transparent boxes, higher tiers will be brighter.
|
|
|
|
// If using opaque colors, higher tiers will be on front, covering lower tiers.
|
|
|
|
if (tiers[i] >= tier)
|
|
|
|
{
|
|
|
|
if (!segments.IsTierStarted(tier))
|
|
|
|
segments.StartSegment(tier, i * 1f / tiers.Length);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (segments.IsTierStarted(tier))
|
|
|
|
segments.EndSegment(tier, i * 1f / tiers.Length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
segments.EndAllPendingSegments();
|
|
|
|
segments.Sort();
|
|
|
|
}
|
|
|
|
|
2023-01-12 17:13:16 +08:00
|
|
|
private Colour4 getTierColour(int tier) => tier >= 0 ? tierColours[tier] : new Colour4(0, 0, 0, 0);
|
2023-01-10 04:35:27 +08:00
|
|
|
|
2023-01-11 05:49:35 +08:00
|
|
|
protected override DrawNode CreateDrawNode() => new SegmentedGraphDrawNode(this);
|
2023-01-10 04:35:27 +08:00
|
|
|
|
|
|
|
protected struct SegmentInfo
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The tier this segment is at.
|
|
|
|
/// </summary>
|
|
|
|
public int Tier;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The progress at which this segment starts.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// The value is a normalized float (from 0 to 1).
|
|
|
|
/// </remarks>
|
|
|
|
public float Start;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The progress at which this segment ends.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// The value is a normalized float (from 0 to 1).
|
|
|
|
/// </remarks>
|
|
|
|
public float End;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The length of this segment.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// The value is a normalized float (from 0 to 1).
|
|
|
|
/// </remarks>
|
|
|
|
public float Length => End - Start;
|
2023-01-12 09:36:35 +08:00
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return $"({Tier}, {Start * 100}%, {End * 100}%)";
|
|
|
|
}
|
2023-01-10 04:35:27 +08:00
|
|
|
}
|
|
|
|
|
2023-01-11 05:49:35 +08:00
|
|
|
private class SegmentedGraphDrawNode : DrawNode
|
|
|
|
{
|
|
|
|
public new SegmentedGraph<T> Source => (SegmentedGraph<T>)base.Source;
|
|
|
|
|
|
|
|
private Texture texture = null!;
|
|
|
|
private IShader shader = null!;
|
|
|
|
private readonly List<SegmentInfo> segments = new List<SegmentInfo>();
|
|
|
|
private Vector2 drawSize;
|
|
|
|
|
|
|
|
public SegmentedGraphDrawNode(SegmentedGraph<T> source)
|
|
|
|
: base(source)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void ApplyState()
|
|
|
|
{
|
|
|
|
base.ApplyState();
|
|
|
|
|
|
|
|
texture = Source.texture;
|
|
|
|
shader = Source.shader;
|
|
|
|
drawSize = Source.DrawSize;
|
|
|
|
segments.Clear();
|
2023-01-11 18:11:31 +08:00
|
|
|
segments.AddRange(Source.segments.Where(s => s.Length * drawSize.X > 1));
|
2023-01-11 05:49:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void Draw(IRenderer renderer)
|
|
|
|
{
|
|
|
|
base.Draw(renderer);
|
|
|
|
|
|
|
|
shader.Bind();
|
|
|
|
|
|
|
|
foreach (SegmentInfo segment in segments)
|
|
|
|
{
|
|
|
|
Vector2 topLeft = new Vector2(segment.Start * drawSize.X, 0);
|
|
|
|
Vector2 topRight = new Vector2(segment.End * drawSize.X, 0);
|
|
|
|
Vector2 bottomLeft = new Vector2(segment.Start * drawSize.X, drawSize.Y);
|
|
|
|
Vector2 bottomRight = new Vector2(segment.End * drawSize.X, drawSize.Y);
|
|
|
|
|
|
|
|
renderer.DrawQuad(
|
|
|
|
texture,
|
|
|
|
new Quad(
|
|
|
|
Vector2Extensions.Transform(topLeft, DrawInfo.Matrix),
|
|
|
|
Vector2Extensions.Transform(topRight, DrawInfo.Matrix),
|
|
|
|
Vector2Extensions.Transform(bottomLeft, DrawInfo.Matrix),
|
|
|
|
Vector2Extensions.Transform(bottomRight, DrawInfo.Matrix)),
|
2023-01-12 04:32:12 +08:00
|
|
|
Source.getTierColour(segment.Tier));
|
2023-01-11 05:49:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
shader.Unbind();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 04:35:27 +08:00
|
|
|
protected class SegmentManager : IEnumerable<SegmentInfo>
|
|
|
|
{
|
|
|
|
private readonly List<SegmentInfo> segments = new List<SegmentInfo>();
|
|
|
|
|
|
|
|
private readonly SegmentInfo?[] pendingSegments;
|
|
|
|
|
|
|
|
public SegmentManager(int tierCount)
|
|
|
|
{
|
|
|
|
pendingSegments = new SegmentInfo?[tierCount];
|
|
|
|
}
|
|
|
|
|
|
|
|
public void StartSegment(int tier, float start)
|
|
|
|
{
|
|
|
|
if (pendingSegments[tier] != null)
|
|
|
|
throw new InvalidOperationException($"Another {nameof(SegmentInfo)} of tier {tier.ToString()} has already been started.");
|
|
|
|
|
|
|
|
pendingSegments[tier] = new SegmentInfo
|
|
|
|
{
|
|
|
|
Tier = tier,
|
|
|
|
Start = Math.Clamp(start, 0, 1)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public void EndSegment(int tier, float end)
|
|
|
|
{
|
|
|
|
SegmentInfo? pendingSegment = pendingSegments[tier];
|
|
|
|
if (pendingSegment == null)
|
|
|
|
throw new InvalidOperationException($"Cannot end {nameof(SegmentInfo)} of tier {tier.ToString()} that has not been started.");
|
|
|
|
|
|
|
|
SegmentInfo segment = pendingSegment.Value;
|
|
|
|
segment.End = Math.Clamp(end, 0, 1);
|
|
|
|
segments.Add(segment);
|
|
|
|
pendingSegments[tier] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void EndAllPendingSegments()
|
|
|
|
{
|
|
|
|
foreach (SegmentInfo? pendingSegment in pendingSegments)
|
|
|
|
{
|
2023-01-12 04:32:12 +08:00
|
|
|
if (pendingSegment == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SegmentInfo finalizedSegment = pendingSegment.Value;
|
|
|
|
finalizedSegment.End = 1;
|
|
|
|
segments.Add(finalizedSegment);
|
2023-01-10 04:35:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Sort() =>
|
|
|
|
segments.Sort((a, b) =>
|
|
|
|
a.Tier != b.Tier
|
|
|
|
? a.Tier.CompareTo(b.Tier)
|
|
|
|
: a.Start.CompareTo(b.Start));
|
|
|
|
|
|
|
|
public void Add(SegmentInfo segment) => segments.Add(segment);
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
{
|
|
|
|
segments.Clear();
|
|
|
|
|
|
|
|
for (int i = 0; i < pendingSegments.Length; i++)
|
|
|
|
pendingSegments[i] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int Count => segments.Count;
|
|
|
|
|
|
|
|
public void Add(int tier, float start, float end)
|
|
|
|
{
|
|
|
|
SegmentInfo segment = new SegmentInfo
|
|
|
|
{
|
|
|
|
Tier = tier,
|
|
|
|
Start = Math.Clamp(start, 0, 1),
|
|
|
|
End = Math.Clamp(end, 0, 1)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (segment.Start > segment.End)
|
|
|
|
throw new InvalidOperationException("Segment start cannot be after segment end.");
|
|
|
|
|
|
|
|
Add(segment);
|
|
|
|
}
|
|
|
|
|
2023-01-12 04:32:12 +08:00
|
|
|
public bool IsTierStarted(int tier) => tier >= 0 && pendingSegments[tier].HasValue;
|
2023-01-10 04:35:27 +08:00
|
|
|
|
|
|
|
public IEnumerator<SegmentInfo> GetEnumerator() => segments.GetEnumerator();
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|