1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 21:52:55 +08:00

Refactor everything for sanity

This commit is contained in:
Dean Herbert 2023-12-13 17:33:39 +09:00
parent 9a982a9564
commit 5062c53e36
No known key found for this signature in database
3 changed files with 41 additions and 77 deletions

View File

@ -1,29 +1,30 @@
// 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.Collections.Generic;
using System;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics;
using osu.Framework.Utils;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osuTK;
namespace osu.Game.Overlays.Mods
{
public partial class AdjustedAttributesTooltip : VisibilityContainer, ITooltip
{
private readonly Dictionary<string, Bindable<OldNewPair>> attributes = new Dictionary<string, Bindable<OldNewPair>>();
private FillFlowContainer? attributesFillFlow;
private FillFlowContainer attributesFillFlow = null!;
private Container content = null!;
private BeatmapDifficulty? originalDifficulty;
private BeatmapDifficulty? adjustedDifficulty;
[Resolved]
private OsuColour colours { get; set; } = null!;
@ -69,36 +70,43 @@ namespace osu.Game.Overlays.Mods
},
};
foreach (var attribute in attributes)
attributesFillFlow?.Add(new AttributeDisplay(attribute.Key, attribute.Value.GetBoundCopy()));
updateVisibility();
updateDisplay();
}
public void AddAttribute(string name)
public void UpdateAttributes(BeatmapDifficulty original, BeatmapDifficulty adjusted)
{
Bindable<OldNewPair> newBindable = new Bindable<OldNewPair>();
newBindable.BindValueChanged(_ => updateVisibility());
attributes.Add(name, newBindable);
originalDifficulty = original;
adjustedDifficulty = adjusted;
attributesFillFlow?.Add(new AttributeDisplay(name, newBindable.GetBoundCopy()));
if (IsLoaded)
updateDisplay();
}
public void UpdateAttribute(string name, double oldValue, double newValue)
private void updateDisplay()
{
if (!attributes.ContainsKey(name)) return;
attributesFillFlow.Clear();
Bindable<OldNewPair> attribute = attributes[name];
if (originalDifficulty == null || adjustedDifficulty == null)
return;
OldNewPair attributeValue = attribute.Value;
attributeValue.OldValue = oldValue;
attributeValue.NewValue = newValue;
attemptAdd("AR", bd => bd.ApproachRate);
attemptAdd("OD", bd => bd.OverallDifficulty);
attemptAdd("CS", bd => bd.CircleSize);
attemptAdd("HP", bd => bd.DrainRate);
attribute.Value = attributeValue;
}
if (attributesFillFlow.Any())
content.Show();
else
content.Hide();
protected override void Update()
{
void attemptAdd(string name, Func<BeatmapDifficulty, double> lookup)
{
double a = lookup(originalDifficulty);
double b = lookup(adjustedDifficulty);
if (!Precision.AlmostEquals(a, b))
attributesFillFlow.Add(new AttributeDisplay(name, a, b));
}
}
public void SetContent(object content)
@ -110,55 +118,18 @@ namespace osu.Game.Overlays.Mods
public void Move(Vector2 pos) => Position = pos;
private void updateVisibility()
{
if (!IsLoaded)
return;
if (attributes.Any(attribute => !Precision.AlmostEquals(attribute.Value.Value.OldValue, attribute.Value.Value.NewValue)))
content.Show();
else
content.Hide();
}
private partial class AttributeDisplay : CompositeDrawable
{
public readonly Bindable<OldNewPair> AttributeValues;
public readonly string AttributeName;
private readonly OsuSpriteText text;
public AttributeDisplay(string name, Bindable<OldNewPair> values)
public AttributeDisplay(string name, double original, double adjusted)
{
AutoSizeAxes = Axes.Both;
AttributeName = name;
AttributeValues = values;
InternalChild = text = new OsuSpriteText
InternalChild = new OsuSpriteText
{
Font = OsuFont.Default.With(weight: FontWeight.Bold)
Font = OsuFont.Default.With(weight: FontWeight.Bold),
Text = $"{name}: {original:0.0#} → {adjusted:0.0#}"
};
AttributeValues.BindValueChanged(_ => update(), true);
}
private void update()
{
if (Precision.AlmostEquals(AttributeValues.Value.OldValue, AttributeValues.Value.NewValue))
{
Hide();
return;
}
Show();
text.Text = $"{AttributeName}: {(AttributeValues.Value.OldValue):0.0#} → {(AttributeValues.Value.NewValue):0.0#}";
}
}
private struct OldNewPair
{
public double OldValue, NewValue;
}
}
}

View File

@ -60,6 +60,7 @@ namespace osu.Game.Overlays.Mods
private AdjustedAttributesTooltip rateAdjustTooltip = null!;
public ITooltip GetCustomTooltip() => rateAdjustTooltip;
public object TooltipContent => this;
private const float transition_duration = 250;
@ -103,9 +104,6 @@ namespace osu.Game.Overlays.Mods
{
base.LoadComplete();
rateAdjustTooltip.AddAttribute("AR");
rateAdjustTooltip.AddAttribute("OD");
mods.BindValueChanged(_ =>
{
modSettingChangeTracker?.Dispose();
@ -184,8 +182,7 @@ namespace osu.Game.Overlays.Mods
Ruleset ruleset = gameRuleset.Value.CreateInstance();
BeatmapDifficulty adjustedDifficulty = ruleset.GetRateAdjustedDisplayDifficulty(originalDifficulty, rate);
rateAdjustTooltip.UpdateAttribute("AR", originalDifficulty.ApproachRate, adjustedDifficulty.ApproachRate);
rateAdjustTooltip.UpdateAttribute("OD", originalDifficulty.OverallDifficulty, adjustedDifficulty.OverallDifficulty);
rateAdjustTooltip.UpdateAttributes(originalDifficulty, adjustedDifficulty);
approachRateDisplay.AdjustType.Value = VerticalAttributeDisplay.CalculateEffect(originalDifficulty.ApproachRate, adjustedDifficulty.ApproachRate);
overallDifficultyDisplay.AdjustType.Value = VerticalAttributeDisplay.CalculateEffect(originalDifficulty.OverallDifficulty, adjustedDifficulty.OverallDifficulty);

View File

@ -101,9 +101,6 @@ namespace osu.Game.Screens.Select.Details
gameRuleset.BindValueChanged(_ => updateStatistics());
mods.BindValueChanged(modsChanged, true);
rateAdjustTooltip.AddAttribute("AR");
rateAdjustTooltip.AddAttribute("OD");
}
private ModSettingChangeTracker modSettingChangeTracker;
@ -148,8 +145,7 @@ namespace osu.Game.Screens.Select.Details
adjustedDifficulty = ruleset.CreateInstance().GetRateAdjustedDisplayDifficulty(originalDifficulty, rate);
rateAdjustTooltip.UpdateAttribute("AR", originalDifficulty.ApproachRate, adjustedDifficulty.ApproachRate);
rateAdjustTooltip.UpdateAttribute("OD", originalDifficulty.OverallDifficulty, adjustedDifficulty.OverallDifficulty);
rateAdjustTooltip.UpdateAttributes(originalDifficulty, adjustedDifficulty);
}
}