From 6f449a583e57230e3af26dd8d40ab6051dc036b9 Mon Sep 17 00:00:00 2001 From: Andrei Zavatski Date: Sat, 19 Nov 2022 23:27:48 +0300 Subject: [PATCH] Handle empty values as a separate case --- osu.Game.Tests/Visual/Online/TestSceneGraph.cs | 2 ++ osu.Game/Graphics/UserInterface/BarGraph.cs | 13 +++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/osu.Game.Tests/Visual/Online/TestSceneGraph.cs b/osu.Game.Tests/Visual/Online/TestSceneGraph.cs index 2f3331b141..8b0536651d 100644 --- a/osu.Game.Tests/Visual/Online/TestSceneGraph.cs +++ b/osu.Game.Tests/Visual/Online/TestSceneGraph.cs @@ -3,6 +3,7 @@ #nullable disable +using System; using System.Linq; using NUnit.Framework; using osu.Framework.Graphics; @@ -32,6 +33,7 @@ namespace osu.Game.Tests.Visual.Online AddStep("values from 1-10", () => graph.Values = Enumerable.Range(1, 10).Select(i => (float)i)); AddStep("values from 1-100", () => graph.Values = Enumerable.Range(1, 100).Select(i => (float)i)); AddStep("reversed values from 1-10", () => graph.Values = Enumerable.Range(1, 10).Reverse().Select(i => (float)i)); + AddStep("empty values", () => graph.Values = Array.Empty()); AddStep("Bottom to top", () => graph.Direction = BarDirection.BottomToTop); AddStep("Top to bottom", () => graph.Direction = BarDirection.TopToBottom); AddStep("Left to right", () => graph.Direction = BarDirection.LeftToRight); diff --git a/osu.Game/Graphics/UserInterface/BarGraph.cs b/osu.Game/Graphics/UserInterface/BarGraph.cs index 56a2a4f579..c9bffc605e 100644 --- a/osu.Game/Graphics/UserInterface/BarGraph.cs +++ b/osu.Game/Graphics/UserInterface/BarGraph.cs @@ -51,13 +51,18 @@ namespace osu.Game.Graphics.UserInterface { set { + if (!value.Any()) + { + bars.Clear(); + Invalidate(Invalidation.DrawNode); + return; + } + int newCount = value.Count(); - float size = newCount; - if (size != 0) - size = 1.0f / size; + float size = 1.0f / newCount; - float maxLength = MaxValue ?? (newCount == 0 ? 0 : value.Max()); + float maxLength = MaxValue ?? value.Max(); foreach (var bar in value.Select((length, index) => new { Value = length, Index = index })) {