1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 09:42:54 +08:00
osu-lazer/osu.Game/Overlays/Mods/AdjustedAttributesTooltip.cs
Bartłomiej Dach 9e5b6b97ff
Fix AdjustedAttributesTooltip being broken by design
Fixes issue described in the following comment:
https://github.com/ppy/osu/pull/25759#issuecomment-1855954637

That is just not how the tooltip system is supposed to be used.

To name the individual sins:

- Caching and returning a tooltip instance like the classes that used
  tooltips is incorrect. The lifetime of tooltip instances is managed by
  the tooltip container. `GetCustomTooltip()` is called by it
  exclusively. It should return a fresh instance every time.

- Not putting actual data in `IHasCustomTooltip.TooltipContent` is
  wrong.

- Having `Tooltip.SetContent()` be a no-op is *grossly and flagrantly*
  wrong.

I'm not even sure which particular combination of the above
transgressions caused the issue as it presented itself, but at this time
I frankly do not care.
2023-12-14 16:15:56 +01:00

143 lines
4.6 KiB
C#

// 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.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
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<AdjustedAttributesTooltip.Data?>
{
private FillFlowContainer attributesFillFlow = null!;
private Container content = null!;
private Data? data;
[Resolved]
private OsuColour colours { get; set; } = null!;
[BackgroundDependencyLoader]
private void load()
{
AutoSizeAxes = Axes.Both;
Masking = true;
CornerRadius = 5;
InternalChildren = new Drawable[]
{
content = new Container
{
AutoSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colours.Gray3,
},
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Padding = new MarginPadding { Vertical = 10, Horizontal = 15 },
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
new OsuSpriteText
{
Text = "One or more values are being adjusted by mods that change speed.",
},
attributesFillFlow = new FillFlowContainer
{
Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Both
}
}
}
}
},
};
updateDisplay();
}
private void updateDisplay()
{
attributesFillFlow.Clear();
if (data != null)
{
attemptAdd("CS", bd => bd.CircleSize);
attemptAdd("HP", bd => bd.DrainRate);
attemptAdd("OD", bd => bd.OverallDifficulty);
attemptAdd("AR", bd => bd.ApproachRate);
}
if (attributesFillFlow.Any())
content.Show();
else
content.Hide();
void attemptAdd(string name, Func<BeatmapDifficulty, double> lookup)
{
double originalValue = lookup(data.OriginalDifficulty);
double adjustedValue = lookup(data.AdjustedDifficulty);
if (!Precision.AlmostEquals(originalValue, adjustedValue))
attributesFillFlow.Add(new AttributeDisplay(name, originalValue, adjustedValue));
}
}
public void SetContent(Data? data)
{
if (this.data == data)
return;
this.data = data;
updateDisplay();
}
protected override void PopIn() => this.FadeIn(200, Easing.OutQuint);
protected override void PopOut() => this.FadeOut(200, Easing.OutQuint);
public void Move(Vector2 pos) => Position = pos;
public class Data
{
public BeatmapDifficulty OriginalDifficulty { get; }
public BeatmapDifficulty AdjustedDifficulty { get; }
public Data(BeatmapDifficulty originalDifficulty, BeatmapDifficulty adjustedDifficulty)
{
OriginalDifficulty = originalDifficulty;
AdjustedDifficulty = adjustedDifficulty;
}
}
private partial class AttributeDisplay : CompositeDrawable
{
public AttributeDisplay(string name, double original, double adjusted)
{
AutoSizeAxes = Axes.Both;
InternalChild = new OsuSpriteText
{
Font = OsuFont.Default.With(weight: FontWeight.Bold),
Text = $"{name}: {original:0.0#} → {adjusted:0.0#}"
};
}
}
}
}