From 91eab7985bb2a3fd83bd1202c81c16847dacf6af Mon Sep 17 00:00:00 2001 From: tsrk Date: Mon, 9 Jan 2023 21:35:27 +0100 Subject: [PATCH 01/12] feat(ui): Implement a segmented graph --- .../Graphics/UserInterface/SegmentedGraph.cs | 314 ++++++++++++++++++ 1 file changed, 314 insertions(+) create mode 100644 osu.Game/Graphics/UserInterface/SegmentedGraph.cs diff --git a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs new file mode 100644 index 0000000000..261e535fbc --- /dev/null +++ b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs @@ -0,0 +1,314 @@ +// Copyright (c) ppy Pty Ltd . 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; +using System.Threading; +using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.Shapes; +using osu.Framework.Threading; +using osuTK; + +namespace osu.Game.Graphics.UserInterface +{ + public abstract partial class SegmentedGraph : Container + where T : struct, IComparable, IConvertible, IEquatable + { + private BufferedContainer? rectSegments; + private float previousDrawWidth; + private bool graphNeedsUpdate; + + private T[]? values; + private int[] tiers = Array.Empty(); + private readonly SegmentManager segments; + + private readonly int tierCount; + + protected SegmentedGraph(int tierCount) + { + this.tierCount = tierCount; + TierColours = new Colour4[tierCount]; + segments = new SegmentManager(tierCount); + } + + public T[] Values + { + get => values ?? Array.Empty(); + set + { + if (value == values) return; + + values = value; + recalculateTiers(values); + graphNeedsUpdate = true; + } + } + + public readonly Colour4[] TierColours; + + private CancellationTokenSource? cts; + private ScheduledDelegate? scheduledCreate; + + protected override void Update() + { + base.Update(); + + if (graphNeedsUpdate || (values != null && DrawWidth != previousDrawWidth)) + { + rectSegments?.FadeOut(150, Easing.OutQuint).Expire(); + + scheduledCreate?.Cancel(); + scheduledCreate = Scheduler.AddDelayed(RecreateGraph, 150); + + previousDrawWidth = DrawWidth; + graphNeedsUpdate = false; + } + } + + protected virtual void RecreateGraph() + { + var newSegments = new BufferedContainer(cachedFrameBuffer: true) + { + RedrawOnScale = false, + RelativeSizeAxes = Axes.Both + }; + + cts?.Cancel(); + recalculateSegments(); + redrawSegments(newSegments); + + LoadComponentAsync(newSegments, s => + { + Children = new Drawable[] + { + rectSegments = s + }; + + s.FadeInFromZero(100); + }, (cts = new CancellationTokenSource()).Token); + } + + private void recalculateTiers(T[]? arr) + { + if (arr == null || arr.Length == 0) + { + tiers = Array.Empty(); + 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++) + floatValues[i] += min; + } + + // 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(); + } + + private Colour4 tierToColour(int tier) => tier >= 0 ? TierColours[tier] : new Colour4(0, 0, 0, 0); + + // Base implementation, could be drawn with draw node if preferred + private void redrawSegments(BufferedContainer container) + { + if (segments.Count == 0) + return; + + foreach (SegmentInfo segment in segments) // Lower tiers will be drawn first, putting them in the back + { + float width = segment.Length * DrawWidth; + + // If the segment width exceeds the DrawWidth, just fill the rest + if (width >= DrawWidth) + width = DrawWidth; + + container.Add(new Box + { + Name = $"Tier {segment.Tier} segment", + Origin = Anchor.BottomLeft, + Anchor = Anchor.BottomLeft, + RelativeSizeAxes = Axes.Y, + Position = new Vector2(segment.Start * DrawWidth, 0), + Width = width, + Colour = tierToColour(segment.Tier) + }); + } + } + + protected struct SegmentInfo + { + /// + /// The tier this segment is at. + /// + public int Tier; + + /// + /// The progress at which this segment starts. + /// + /// + /// The value is a normalized float (from 0 to 1). + /// + public float Start; + + /// + /// The progress at which this segment ends. + /// + /// + /// The value is a normalized float (from 0 to 1). + /// + public float End; + + /// + /// The length of this segment. + /// + /// + /// The value is a normalized float (from 0 to 1). + /// + public float Length => End - Start; + } + + protected class SegmentManager : IEnumerable + { + private readonly List segments = new List(); + + 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) + { + if (pendingSegment != null) + { + SegmentInfo finalizedSegment = pendingSegment.Value; + finalizedSegment.End = 1; + segments.Add(finalizedSegment); + } + } + } + + 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); + } + + public bool IsTierStarted(int tier) + { + if (tier < 0) + return false; + + return pendingSegments[tier].HasValue; + } + + public IEnumerator GetEnumerator() => segments.GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } + } +} From 21073f36012ede15d245f2813520ba5b4267c406 Mon Sep 17 00:00:00 2001 From: tsrk Date: Tue, 10 Jan 2023 22:49:35 +0100 Subject: [PATCH 02/12] reafactor: use DrawNode to draw SegmenteddGraph --- .../Graphics/UserInterface/SegmentedGraph.cs | 138 +++++++++--------- 1 file changed, 72 insertions(+), 66 deletions(-) diff --git a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs index 261e535fbc..65c2146889 100644 --- a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs +++ b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs @@ -5,20 +5,19 @@ using System; using System.Collections; using System.Collections.Generic; using System.Linq; -using System.Threading; +using osu.Framework.Allocation; using osu.Framework.Graphics; -using osu.Framework.Graphics.Containers; -using osu.Framework.Graphics.Shapes; -using osu.Framework.Threading; +using osu.Framework.Graphics.Primitives; +using osu.Framework.Graphics.Rendering; +using osu.Framework.Graphics.Shaders; +using osu.Framework.Graphics.Textures; using osuTK; namespace osu.Game.Graphics.UserInterface { - public abstract partial class SegmentedGraph : Container + public abstract partial class SegmentedGraph : Drawable where T : struct, IComparable, IConvertible, IEquatable { - private BufferedContainer? rectSegments; - private float previousDrawWidth; private bool graphNeedsUpdate; private T[]? values; @@ -44,53 +43,33 @@ namespace osu.Game.Graphics.UserInterface values = value; recalculateTiers(values); graphNeedsUpdate = true; + Invalidate(Invalidation.DrawNode); } } public readonly Colour4[] TierColours; - private CancellationTokenSource? cts; - private ScheduledDelegate? scheduledCreate; + private Texture texture = null!; + private IShader shader = 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(); - if (graphNeedsUpdate || (values != null && DrawWidth != previousDrawWidth)) + if (graphNeedsUpdate) { - rectSegments?.FadeOut(150, Easing.OutQuint).Expire(); - - scheduledCreate?.Cancel(); - scheduledCreate = Scheduler.AddDelayed(RecreateGraph, 150); - - previousDrawWidth = DrawWidth; - graphNeedsUpdate = false; + recalculateSegments(); + Invalidate(Invalidation.DrawNode); } } - protected virtual void RecreateGraph() - { - var newSegments = new BufferedContainer(cachedFrameBuffer: true) - { - RedrawOnScale = false, - RelativeSizeAxes = Axes.Both - }; - - cts?.Cancel(); - recalculateSegments(); - redrawSegments(newSegments); - - LoadComponentAsync(newSegments, s => - { - Children = new Drawable[] - { - rectSegments = s - }; - - s.FadeInFromZero(100); - }, (cts = new CancellationTokenSource()).Token); - } - private void recalculateTiers(T[]? arr) { if (arr == null || arr.Length == 0) @@ -159,32 +138,7 @@ namespace osu.Game.Graphics.UserInterface private Colour4 tierToColour(int tier) => tier >= 0 ? TierColours[tier] : new Colour4(0, 0, 0, 0); - // Base implementation, could be drawn with draw node if preferred - private void redrawSegments(BufferedContainer container) - { - if (segments.Count == 0) - return; - - foreach (SegmentInfo segment in segments) // Lower tiers will be drawn first, putting them in the back - { - float width = segment.Length * DrawWidth; - - // If the segment width exceeds the DrawWidth, just fill the rest - if (width >= DrawWidth) - width = DrawWidth; - - container.Add(new Box - { - Name = $"Tier {segment.Tier} segment", - Origin = Anchor.BottomLeft, - Anchor = Anchor.BottomLeft, - RelativeSizeAxes = Axes.Y, - Position = new Vector2(segment.Start * DrawWidth, 0), - Width = width, - Colour = tierToColour(segment.Tier) - }); - } - } + protected override DrawNode CreateDrawNode() => new SegmentedGraphDrawNode(this); protected struct SegmentInfo { @@ -218,6 +172,58 @@ namespace osu.Game.Graphics.UserInterface public float Length => End - Start; } + private class SegmentedGraphDrawNode : DrawNode + { + public new SegmentedGraph Source => (SegmentedGraph)base.Source; + + private Texture texture = null!; + private IShader shader = null!; + private readonly List segments = new List(); + private Vector2 drawSize; + + public SegmentedGraphDrawNode(SegmentedGraph source) + : base(source) + { + } + + public override void ApplyState() + { + base.ApplyState(); + + texture = Source.texture; + shader = Source.shader; + drawSize = Source.DrawSize; + segments.Clear(); + segments.AddRange(Source.segments); + } + + 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)), + Source.tierToColour(segment.Tier)); + } + + shader.Unbind(); + } + } + protected class SegmentManager : IEnumerable { private readonly List segments = new List(); From 578d16f2bce9f21fa4b5c01ba5ea78e76d31f3cb Mon Sep 17 00:00:00 2001 From: tsrk Date: Wed, 11 Jan 2023 11:11:31 +0100 Subject: [PATCH 03/12] perf: Do not draw sectoins that are less than 1px --- osu.Game/Graphics/UserInterface/SegmentedGraph.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs index 65c2146889..9a8fd1c4c5 100644 --- a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs +++ b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs @@ -194,7 +194,7 @@ namespace osu.Game.Graphics.UserInterface shader = Source.shader; drawSize = Source.DrawSize; segments.Clear(); - segments.AddRange(Source.segments); + segments.AddRange(Source.segments.Where(s => s.Length * drawSize.X > 1)); } public override void Draw(IRenderer renderer) From 42ff8c75fa9bb211f6e1478a49447a3d1f74f558 Mon Sep 17 00:00:00 2001 From: tsrk Date: Wed, 11 Jan 2023 11:22:18 +0100 Subject: [PATCH 04/12] refactor: make class not abstract --- osu.Game/Graphics/UserInterface/SegmentedGraph.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs index 9a8fd1c4c5..977594d9b0 100644 --- a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs +++ b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs @@ -15,7 +15,7 @@ using osuTK; namespace osu.Game.Graphics.UserInterface { - public abstract partial class SegmentedGraph : Drawable + public partial class SegmentedGraph : Drawable where T : struct, IComparable, IConvertible, IEquatable { private bool graphNeedsUpdate; From e128b9ee5cb75e9522493d09866d92bb653632dc Mon Sep 17 00:00:00 2001 From: tsrk Date: Wed, 11 Jan 2023 14:02:18 +0100 Subject: [PATCH 05/12] fix(SegmentedGraph): make ctor public --- osu.Game/Graphics/UserInterface/SegmentedGraph.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs index 977594d9b0..2a63d53870 100644 --- a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs +++ b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs @@ -26,7 +26,7 @@ namespace osu.Game.Graphics.UserInterface private readonly int tierCount; - protected SegmentedGraph(int tierCount) + public SegmentedGraph(int tierCount) { this.tierCount = tierCount; TierColours = new Colour4[tierCount]; From 6249322a32bf6b3314a2d69add184eb131269fed Mon Sep 17 00:00:00 2001 From: tsrk Date: Wed, 11 Jan 2023 14:03:36 +0100 Subject: [PATCH 06/12] fix(SegmentedGraph): solve issue for negatives values --- osu.Game/Graphics/UserInterface/SegmentedGraph.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs index 2a63d53870..f8c5c08bbf 100644 --- a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs +++ b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs @@ -86,7 +86,7 @@ namespace osu.Game.Graphics.UserInterface if (min < 0) { for (int i = 0; i < floatValues.Length; i++) - floatValues[i] += min; + floatValues[i] += Math.Abs(min); } // Normalize values From 845cdb3097ce2781c2575f0c8277900852fcccf5 Mon Sep 17 00:00:00 2001 From: tsrk Date: Wed, 11 Jan 2023 14:03:49 +0100 Subject: [PATCH 07/12] test: add visual tests for SegmentedGraph class --- .../UserInterface/TestSceneSegmentedGraph.cs | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs diff --git a/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs b/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs new file mode 100644 index 0000000000..ab7078d1b7 --- /dev/null +++ b/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs @@ -0,0 +1,96 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using System.Linq; +using osu.Framework.Graphics; +using osu.Game.Graphics.UserInterface; +using osuTK; +using Vector4 = System.Numerics.Vector4; + +namespace osu.Game.Tests.Visual.UserInterface +{ + public partial class TestSceneSegmentedGraph : OsuTestScene + { + private readonly SegmentedGraph graph; + + public TestSceneSegmentedGraph() + { + Children = new Drawable[] + { + graph = new SegmentedGraph(10) + { + RelativeSizeAxes = Axes.Both, + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Size = new Vector2(1, 0.5f), + }, + }; + + for (int i = 0; i < graph.TierColours.Length; i++) + { + graph.TierColours[i] = + new Colour4(Vector4.Lerp(Colour4.Blue.Vector, Colour4.White.Vector, i * 1f / (graph.TierColours.Length - 1))); + } + + AddStep("values from 1-10", () => graph.Values = Enumerable.Range(1, 10).ToArray()); + AddStep("values from 1-100", () => graph.Values = Enumerable.Range(1, 100).ToArray()); + AddStep("values from 1-500", () => graph.Values = Enumerable.Range(1, 500).ToArray()); + AddStep("sin() function of size 100", () => sinFunction()); + AddStep("sin() function of size 500", () => sinFunction(500)); + AddStep("bumps of size 100", () => bumps()); + AddStep("bumps of size 500", () => bumps(500)); + AddStep("100 random values", () => randomValues()); + AddStep("500 random values", () => randomValues(500)); + AddStep("reversed values from 1-10", () => graph.Values = Enumerable.Range(1, 10).Reverse().ToArray()); + } + + private void sinFunction(int size = 100) + { + const int max_value = 255; + int[] values = new int[size]; + + float step = 2 * MathF.PI / size; + float x = 0; + + for (int i = 0; i < size; i++) + { + values[i] = (int)(max_value * MathF.Sin(x)); + x += step; + } + + graph.Values = values; + } + + private void bumps(int size = 100) + { + const int max_value = 255; + int[] values = new int[size]; + + float step = 2 * MathF.PI / size; + float x = 0; + + for (int i = 0; i < size; i++) + { + values[i] = (int)(max_value * Math.Abs(MathF.Sin(x))); + x += step; + } + + graph.Values = values; + } + + private void randomValues(int size = 100) + { + Random rng = new Random(); + + int[] values = new int[size]; + + for (int i = 0; i < size; i++) + { + values[i] = rng.Next(255); + } + + graph.Values = values; + } + } +} From 442b2238b8cdf0f04d0054cb12c73571b51cba59 Mon Sep 17 00:00:00 2001 From: tsrk Date: Wed, 11 Jan 2023 14:37:57 +0100 Subject: [PATCH 08/12] test(SegmentedGraph): add concrete application test with beatmap density --- .../UserInterface/TestSceneSegmentedGraph.cs | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs b/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs index ab7078d1b7..f8df0528d6 100644 --- a/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs +++ b/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs @@ -2,9 +2,13 @@ // See the LICENCE file in the repository root for full licence text. using System; +using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using osu.Framework.Graphics; using osu.Game.Graphics.UserInterface; +using osu.Game.Rulesets.Objects; +using osu.Game.Rulesets.Osu; using osuTK; using Vector4 = System.Numerics.Vector4; @@ -42,6 +46,8 @@ namespace osu.Game.Tests.Visual.UserInterface AddStep("bumps of size 500", () => bumps(500)); AddStep("100 random values", () => randomValues()); AddStep("500 random values", () => randomValues(500)); + AddStep("beatmap density with granularity of 200", () => beatmapDensity()); + AddStep("beatmap density with granularity of 300", () => beatmapDensity(300)); AddStep("reversed values from 1-10", () => graph.Values = Enumerable.Range(1, 10).Reverse().ToArray()); } @@ -92,5 +98,40 @@ namespace osu.Game.Tests.Visual.UserInterface graph.Values = values; } + + private void beatmapDensity(int granularity = 200) + { + var ruleset = new OsuRuleset(); + var beatmap = CreateBeatmap(ruleset.RulesetInfo); + IEnumerable objects = beatmap.HitObjects; + + // Taken from SongProgressGraph + int[] values = new int[granularity]; + + if (!objects.Any()) + return; + + double firstHit = objects.First().StartTime; + double lastHit = objects.Max(o => o.GetEndTime()); + + if (lastHit == 0) + lastHit = objects.Last().StartTime; + + double interval = (lastHit - firstHit + 1) / granularity; + + foreach (var h in objects) + { + double endTime = h.GetEndTime(); + + Debug.Assert(endTime >= h.StartTime); + + int startRange = (int)((h.StartTime - firstHit) / interval); + int endRange = (int)((endTime - firstHit) / interval); + for (int i = startRange; i <= endRange; i++) + values[i]++; + } + + graph.Values = values; + } } } From 624e90b213f4e328add5723206555fd1b56e0374 Mon Sep 17 00:00:00 2001 From: tsrk Date: Wed, 11 Jan 2023 21:32:12 +0100 Subject: [PATCH 09/12] style: nitpicks --- .../Graphics/UserInterface/SegmentedGraph.cs | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs index f8c5c08bbf..d0356e6327 100644 --- a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs +++ b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs @@ -136,7 +136,7 @@ namespace osu.Game.Graphics.UserInterface segments.Sort(); } - private Colour4 tierToColour(int tier) => tier >= 0 ? TierColours[tier] : new Colour4(0, 0, 0, 0); + private Colour4 getTierColour(int tier) => tier >= 0 ? TierColours[tier] : new Colour4(0, 0, 0, 0); protected override DrawNode CreateDrawNode() => new SegmentedGraphDrawNode(this); @@ -217,7 +217,7 @@ namespace osu.Game.Graphics.UserInterface Vector2Extensions.Transform(topRight, DrawInfo.Matrix), Vector2Extensions.Transform(bottomLeft, DrawInfo.Matrix), Vector2Extensions.Transform(bottomRight, DrawInfo.Matrix)), - Source.tierToColour(segment.Tier)); + Source.getTierColour(segment.Tier)); } shader.Unbind(); @@ -263,12 +263,12 @@ namespace osu.Game.Graphics.UserInterface { foreach (SegmentInfo? pendingSegment in pendingSegments) { - if (pendingSegment != null) - { - SegmentInfo finalizedSegment = pendingSegment.Value; - finalizedSegment.End = 1; - segments.Add(finalizedSegment); - } + if (pendingSegment == null) + continue; + + SegmentInfo finalizedSegment = pendingSegment.Value; + finalizedSegment.End = 1; + segments.Add(finalizedSegment); } } @@ -305,13 +305,7 @@ namespace osu.Game.Graphics.UserInterface Add(segment); } - public bool IsTierStarted(int tier) - { - if (tier < 0) - return false; - - return pendingSegments[tier].HasValue; - } + public bool IsTierStarted(int tier) => tier >= 0 && pendingSegments[tier].HasValue; public IEnumerator GetEnumerator() => segments.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); From 5694487a7bfbdee738a464daa4b5ae6a9b380577 Mon Sep 17 00:00:00 2001 From: tsrk Date: Thu, 12 Jan 2023 02:36:35 +0100 Subject: [PATCH 10/12] fix(SegmentedGraph): update `graphNeedsUpdate` variable during `Update()` loop --- .../UserInterface/TestSceneSegmentedGraph.cs | 24 +++++++------------ .../Graphics/UserInterface/SegmentedGraph.cs | 9 +++++-- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs b/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs index f8df0528d6..a3426b36b4 100644 --- a/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs +++ b/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs @@ -54,49 +54,43 @@ namespace osu.Game.Tests.Visual.UserInterface private void sinFunction(int size = 100) { const int max_value = 255; - int[] values = new int[size]; + graph.Values = new int[size]; float step = 2 * MathF.PI / size; float x = 0; for (int i = 0; i < size; i++) { - values[i] = (int)(max_value * MathF.Sin(x)); + graph.Values[i] = (int)(max_value * MathF.Sin(x)); x += step; } - - graph.Values = values; } private void bumps(int size = 100) { const int max_value = 255; - int[] values = new int[size]; + graph.Values = new int[size]; float step = 2 * MathF.PI / size; float x = 0; for (int i = 0; i < size; i++) { - values[i] = (int)(max_value * Math.Abs(MathF.Sin(x))); + graph.Values[i] = (int)(max_value * Math.Abs(MathF.Sin(x))); x += step; } - - graph.Values = values; } private void randomValues(int size = 100) { Random rng = new Random(); - int[] values = new int[size]; + graph.Values = new int[size]; for (int i = 0; i < size; i++) { - values[i] = rng.Next(255); + graph.Values[i] = rng.Next(255); } - - graph.Values = values; } private void beatmapDensity(int granularity = 200) @@ -106,7 +100,7 @@ namespace osu.Game.Tests.Visual.UserInterface IEnumerable objects = beatmap.HitObjects; // Taken from SongProgressGraph - int[] values = new int[granularity]; + graph.Values = new int[granularity]; if (!objects.Any()) return; @@ -128,10 +122,8 @@ namespace osu.Game.Tests.Visual.UserInterface int startRange = (int)((h.StartTime - firstHit) / interval); int endRange = (int)((endTime - firstHit) / interval); for (int i = startRange; i <= endRange; i++) - values[i]++; + graph.Values[i]++; } - - graph.Values = values; } } } diff --git a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs index d0356e6327..af457edc11 100644 --- a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs +++ b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs @@ -41,9 +41,7 @@ namespace osu.Game.Graphics.UserInterface if (value == values) return; values = value; - recalculateTiers(values); graphNeedsUpdate = true; - Invalidate(Invalidation.DrawNode); } } @@ -65,8 +63,10 @@ namespace osu.Game.Graphics.UserInterface if (graphNeedsUpdate) { + recalculateTiers(values); recalculateSegments(); Invalidate(Invalidation.DrawNode); + graphNeedsUpdate = false; } } @@ -170,6 +170,11 @@ namespace osu.Game.Graphics.UserInterface /// The value is a normalized float (from 0 to 1). /// public float Length => End - Start; + + public override string ToString() + { + return $"({Tier}, {Start * 100}%, {End * 100}%)"; + } } private class SegmentedGraphDrawNode : DrawNode From 7cbc03dce6703b863d2c0349fe7454c247f36e5c Mon Sep 17 00:00:00 2001 From: tsrk Date: Thu, 12 Jan 2023 10:13:16 +0100 Subject: [PATCH 11/12] refactor(SegmentedGraph): use (get/set)ters to expose TierColour --- .../UserInterface/TestSceneSegmentedGraph.cs | 15 +++++--- .../Graphics/UserInterface/SegmentedGraph.cs | 38 +++++++++++++++++-- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs b/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs index a3426b36b4..5c0b3e0a20 100644 --- a/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs +++ b/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs @@ -10,7 +10,6 @@ using osu.Game.Graphics.UserInterface; using osu.Game.Rulesets.Objects; using osu.Game.Rulesets.Osu; using osuTK; -using Vector4 = System.Numerics.Vector4; namespace osu.Game.Tests.Visual.UserInterface { @@ -22,7 +21,7 @@ namespace osu.Game.Tests.Visual.UserInterface { Children = new Drawable[] { - graph = new SegmentedGraph(10) + graph = new SegmentedGraph(6) { RelativeSizeAxes = Axes.Both, Anchor = Anchor.Centre, @@ -31,11 +30,15 @@ namespace osu.Game.Tests.Visual.UserInterface }, }; - for (int i = 0; i < graph.TierColours.Length; i++) + graph.TierColours = new[] { - graph.TierColours[i] = - new Colour4(Vector4.Lerp(Colour4.Blue.Vector, Colour4.White.Vector, i * 1f / (graph.TierColours.Length - 1))); - } + Colour4.Red, + Colour4.OrangeRed, + Colour4.Orange, + Colour4.Yellow, + Colour4.YellowGreen, + Colour4.Green + }; AddStep("values from 1-10", () => graph.Values = Enumerable.Range(1, 10).ToArray()); AddStep("values from 1-100", () => graph.Values = Enumerable.Range(1, 100).ToArray()); diff --git a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs index af457edc11..456274e559 100644 --- a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs +++ b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs @@ -29,7 +29,7 @@ namespace osu.Game.Graphics.UserInterface public SegmentedGraph(int tierCount) { this.tierCount = tierCount; - TierColours = new Colour4[tierCount]; + tierColours = new Colour4[tierCount]; segments = new SegmentManager(tierCount); } @@ -45,7 +45,39 @@ namespace osu.Game.Graphics.UserInterface } } - public readonly Colour4[] TierColours; + private Colour4[] tierColours; + + public Colour4[] TierColours + { + get => tierColours; + set + { + if (value.Length == 0 || value == tierColours) + return; + + if (value.Length == tierCount) + { + tierColours = value; + } + else if (value.Length < tierCount) + { + var colourList = new List(value); + + for (int i = value.Length; i < tierCount; i++) + { + colourList.Add(value.Last()); + } + + tierColours = colourList.ToArray(); + } + else + { + tierColours = value[..tierCount]; + } + + graphNeedsUpdate = true; + } + } private Texture texture = null!; private IShader shader = null!; @@ -136,7 +168,7 @@ namespace osu.Game.Graphics.UserInterface segments.Sort(); } - private Colour4 getTierColour(int tier) => tier >= 0 ? TierColours[tier] : new Colour4(0, 0, 0, 0); + private Colour4 getTierColour(int tier) => tier >= 0 ? tierColours[tier] : new Colour4(0, 0, 0, 0); protected override DrawNode CreateDrawNode() => new SegmentedGraphDrawNode(this); From bb2ece5c71bed3c8ebc0f68d24e4f30ef02023c6 Mon Sep 17 00:00:00 2001 From: tsrk Date: Thu, 12 Jan 2023 10:57:12 +0100 Subject: [PATCH 12/12] refactor(SegmentedGraph): adjust tierCount based on passed Colours --- .../UserInterface/TestSceneSegmentedGraph.cs | 22 ++++++++++++++ .../Graphics/UserInterface/SegmentedGraph.cs | 30 +++++-------------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs b/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs index 5c0b3e0a20..80941569af 100644 --- a/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs +++ b/osu.Game.Tests/Visual/UserInterface/TestSceneSegmentedGraph.cs @@ -52,6 +52,28 @@ namespace osu.Game.Tests.Visual.UserInterface AddStep("beatmap density with granularity of 200", () => beatmapDensity()); AddStep("beatmap density with granularity of 300", () => beatmapDensity(300)); AddStep("reversed values from 1-10", () => graph.Values = Enumerable.Range(1, 10).Reverse().ToArray()); + AddStep("change colour", () => + { + graph.TierColours = new[] + { + Colour4.White, + Colour4.LightBlue, + Colour4.Aqua, + Colour4.Blue + }; + }); + AddStep("reset colour", () => + { + graph.TierColours = new[] + { + Colour4.Red, + Colour4.OrangeRed, + Colour4.Orange, + Colour4.Yellow, + Colour4.YellowGreen, + Colour4.Green + }; + }); } private void sinFunction(int size = 100) diff --git a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs index 456274e559..8ccba710b8 100644 --- a/osu.Game/Graphics/UserInterface/SegmentedGraph.cs +++ b/osu.Game/Graphics/UserInterface/SegmentedGraph.cs @@ -24,12 +24,15 @@ namespace osu.Game.Graphics.UserInterface private int[] tiers = Array.Empty(); private readonly SegmentManager segments; - private readonly int tierCount; + private int tierCount; - public SegmentedGraph(int tierCount) + public SegmentedGraph(int tierCount = 1) { this.tierCount = tierCount; - tierColours = new Colour4[tierCount]; + tierColours = new[] + { + new Colour4(0, 0, 0, 0) + }; segments = new SegmentManager(tierCount); } @@ -55,25 +58,8 @@ namespace osu.Game.Graphics.UserInterface if (value.Length == 0 || value == tierColours) return; - if (value.Length == tierCount) - { - tierColours = value; - } - else if (value.Length < tierCount) - { - var colourList = new List(value); - - for (int i = value.Length; i < tierCount; i++) - { - colourList.Add(value.Last()); - } - - tierColours = colourList.ToArray(); - } - else - { - tierColours = value[..tierCount]; - } + tierCount = value.Length; + tierColours = value; graphNeedsUpdate = true; }