1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 07:27:25 +08:00

Merge pull request #19991 from Feodor0090/mods-effect-displays

Move `DifficultyMultiplierDisplay`'s layout and colour logic to a base class
This commit is contained in:
Dan Balasescu 2022-09-16 10:30:30 +09:00 committed by GitHub
commit 49d2c84646
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 306 additions and 159 deletions

View File

@ -0,0 +1,68 @@
// 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.Linq;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Localisation;
using osu.Framework.Testing;
using osu.Game.Graphics;
using osu.Game.Overlays;
using osu.Game.Overlays.Mods;
using osu.Game.Rulesets.Mods;
using osuTK.Graphics;
namespace osu.Game.Tests.Visual.UserInterface
{
[TestFixture]
public class TestSceneModsEffectDisplay : OsuTestScene
{
[Cached]
private OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Green);
[Resolved]
private OsuColour colours { get; set; } = null!;
[Test]
public void TestModsEffectDisplay()
{
TestDisplay testDisplay = null!;
Box background = null!;
AddStep("add display", () =>
{
Add(testDisplay = new TestDisplay
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre
});
var boxes = testDisplay.ChildrenOfType<Box>();
background = boxes.First();
});
AddStep("set value to default", () => testDisplay.Current.Value = 50);
AddUntilStep("colours are correct", () => testDisplay.Container.Colour == Color4.White && background.Colour == colourProvider.Background3);
AddStep("set value to less", () => testDisplay.Current.Value = 40);
AddUntilStep("colours are correct", () => testDisplay.Container.Colour == colourProvider.Background5 && background.Colour == colours.ForModType(ModType.DifficultyReduction));
AddStep("set value to bigger", () => testDisplay.Current.Value = 60);
AddUntilStep("colours are correct", () => testDisplay.Container.Colour == colourProvider.Background5 && background.Colour == colours.ForModType(ModType.DifficultyIncrease));
}
private class TestDisplay : ModsEffectDisplay
{
public Container<Drawable> Container => Content;
protected override LocalisableString Label => "Test display";
public TestDisplay()
{
Current.Default = 50;
}
}
}
}

View File

@ -3,185 +3,41 @@
#nullable disable
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.LocalisationExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Localisation;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Rulesets.Mods;
using osuTK;
using osu.Game.Localisation;
namespace osu.Game.Overlays.Mods
{
public class DifficultyMultiplierDisplay : CompositeDrawable, IHasCurrentValue<double>
public sealed class DifficultyMultiplierDisplay : ModsEffectDisplay
{
public const float HEIGHT = 42;
protected override LocalisableString Label => DifficultyMultiplierDisplayStrings.DifficultyMultiplier;
public Bindable<double> Current
{
get => current.Current;
set => current.Current = value;
}
private readonly BindableNumberWithCurrent<double> current = new BindableNumberWithCurrent<double>(1);
private readonly Box underlayBackground;
private readonly Box contentBackground;
private readonly FillFlowContainer multiplierFlow;
private readonly MultiplierCounter multiplierCounter;
[Resolved]
private OsuColour colours { get; set; }
[Resolved]
private OverlayColourProvider colourProvider { get; set; }
private const float multiplier_value_area_width = 56;
private const float transition_duration = 200;
protected override string CounterFormat => @"N2";
public DifficultyMultiplierDisplay()
{
Height = HEIGHT;
AutoSizeAxes = Axes.X;
InternalChild = new InputBlockingContainer
Current.Default = 1d;
Current.Value = 1d;
Add(new SpriteIcon
{
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
Masking = true,
CornerRadius = ModSelectPanel.CORNER_RADIUS,
Shear = new Vector2(ShearedOverlayContainer.SHEAR, 0),
Children = new Drawable[]
{
underlayBackground = new Box
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
RelativeSizeAxes = Axes.Y,
Width = multiplier_value_area_width + ModSelectPanel.CORNER_RADIUS
},
new GridContainer
{
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.AutoSize),
new Dimension(GridSizeMode.Absolute, multiplier_value_area_width)
},
Content = new[]
{
new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
Masking = true,
CornerRadius = ModSelectPanel.CORNER_RADIUS,
Children = new Drawable[]
{
contentBackground = new Box
{
RelativeSizeAxes = Axes.Both
},
new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Margin = new MarginPadding { Horizontal = 18 },
Shear = new Vector2(-ShearedOverlayContainer.SHEAR, 0),
Text = DifficultyMultiplierDisplayStrings.DifficultyMultiplier,
Font = OsuFont.Default.With(size: 17, weight: FontWeight.SemiBold)
}
}
},
multiplierFlow = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Shear = new Vector2(-ShearedOverlayContainer.SHEAR, 0),
Direction = FillDirection.Horizontal,
Spacing = new Vector2(2, 0),
Children = new Drawable[]
{
multiplierCounter = new MultiplierCounter
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Current = { BindTarget = Current }
},
new SpriteIcon
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Icon = FontAwesome.Solid.Times,
Size = new Vector2(7),
Margin = new MarginPadding { Top = 1 }
}
}
}
}
}
}
}
};
}
[BackgroundDependencyLoader]
private void load()
{
contentBackground.Colour = colourProvider.Background4;
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Icon = FontAwesome.Solid.Times,
Size = new Vector2(7),
Margin = new MarginPadding { Top = 1 }
});
}
protected override void LoadComplete()
{
base.LoadComplete();
current.BindValueChanged(_ => updateState(), true);
// required to prevent the counter initially rolling up from 0 to 1
// due to `Current.Value` having a nonstandard default value of 1.
multiplierCounter.SetCountWithoutRolling(Current.Value);
}
private void updateState()
{
if (Current.IsDefault)
{
underlayBackground.FadeColour(colourProvider.Background3, transition_duration, Easing.OutQuint);
multiplierFlow.FadeColour(Colour4.White, transition_duration, Easing.OutQuint);
}
else
{
var backgroundColour = Current.Value < 1
? colours.ForModType(ModType.DifficultyReduction)
: colours.ForModType(ModType.DifficultyIncrease);
underlayBackground.FadeColour(backgroundColour, transition_duration, Easing.OutQuint);
multiplierFlow.FadeColour(colourProvider.Background5, transition_duration, Easing.OutQuint);
}
}
private class MultiplierCounter : RollingCounter<double>
{
protected override double RollingDuration => 500;
protected override LocalisableString FormatCount(double count) => count.ToLocalisableString(@"N2");
protected override OsuSpriteText CreateSpriteText() => new OsuSpriteText
{
Font = OsuFont.Default.With(size: 17, weight: FontWeight.SemiBold)
};
Counter.SetCountWithoutRolling(Current.Value);
}
}
}

View File

@ -153,7 +153,7 @@ namespace osu.Game.Overlays.Mods
{
Padding = new MarginPadding
{
Top = (ShowTotalMultiplier ? DifficultyMultiplierDisplay.HEIGHT : 0) + PADDING,
Top = (ShowTotalMultiplier ? ModsEffectDisplay.HEIGHT : 0) + PADDING,
Bottom = PADDING
},
RelativeSizeAxes = Axes.Both,
@ -191,7 +191,7 @@ namespace osu.Game.Overlays.Mods
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
AutoSizeAxes = Axes.X,
Height = DifficultyMultiplierDisplay.HEIGHT,
Height = ModsEffectDisplay.HEIGHT,
Margin = new MarginPadding { Horizontal = 100 },
Child = multiplierDisplay = new DifficultyMultiplierDisplay
{

View File

@ -0,0 +1,223 @@
// 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 osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.LocalisationExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Localisation;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Rulesets.Mods;
using osuTK;
namespace osu.Game.Overlays.Mods
{
/// <summary>
/// Base class for displays of mods effects.
/// </summary>
public abstract class ModsEffectDisplay : Container, IHasCurrentValue<double>
{
public const float HEIGHT = 42;
private const float transition_duration = 200;
private readonly Box contentBackground;
private readonly Box labelBackground;
private readonly FillFlowContainer content;
public Bindable<double> Current
{
get => current.Current;
set => current.Current = value;
}
private readonly BindableWithCurrent<double> current = new BindableWithCurrent<double>();
[Resolved]
private OsuColour colours { get; set; } = null!;
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
/// <summary>
/// Text to display in the left area of the display.
/// </summary>
protected abstract LocalisableString Label { get; }
protected virtual float ValueAreaWidth => 56;
protected virtual string CounterFormat => @"N0";
protected override Container<Drawable> Content => content;
protected readonly RollingCounter<double> Counter;
protected ModsEffectDisplay()
{
Height = HEIGHT;
AutoSizeAxes = Axes.X;
InternalChild = new InputBlockingContainer
{
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
Masking = true,
CornerRadius = ModSelectPanel.CORNER_RADIUS,
Shear = new Vector2(ShearedOverlayContainer.SHEAR, 0),
Children = new Drawable[]
{
contentBackground = new Box
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
RelativeSizeAxes = Axes.Y,
Width = ValueAreaWidth + ModSelectPanel.CORNER_RADIUS
},
new GridContainer
{
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.AutoSize),
new Dimension(GridSizeMode.Absolute, ValueAreaWidth)
},
Content = new[]
{
new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
Masking = true,
CornerRadius = ModSelectPanel.CORNER_RADIUS,
Children = new Drawable[]
{
labelBackground = new Box
{
RelativeSizeAxes = Axes.Both
},
new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Margin = new MarginPadding { Horizontal = 18 },
Shear = new Vector2(-ShearedOverlayContainer.SHEAR, 0),
Text = Label,
Font = OsuFont.Default.With(size: 17, weight: FontWeight.SemiBold)
}
}
},
content = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Direction = FillDirection.Horizontal,
Shear = new Vector2(-ShearedOverlayContainer.SHEAR, 0),
Spacing = new Vector2(2, 0),
Child = Counter = new EffectCounter(CounterFormat)
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Current = { BindTarget = Current }
}
}
}
}
}
}
};
}
[BackgroundDependencyLoader]
private void load()
{
labelBackground.Colour = colourProvider.Background4;
}
protected override void LoadComplete()
{
Current.BindValueChanged(e =>
{
var effect = CalculateEffectForComparison(e.NewValue.CompareTo(Current.Default));
setColours(effect);
}, true);
}
/// <summary>
/// Fades colours of text and its background according to displayed value.
/// </summary>
/// <param name="effect">Effect of the value.</param>
private void setColours(ModEffect effect)
{
switch (effect)
{
case ModEffect.NotChanged:
contentBackground.FadeColour(colourProvider.Background3, transition_duration, Easing.OutQuint);
content.FadeColour(Colour4.White, transition_duration, Easing.OutQuint);
break;
case ModEffect.DifficultyReduction:
contentBackground.FadeColour(colours.ForModType(ModType.DifficultyReduction), transition_duration, Easing.OutQuint);
content.FadeColour(colourProvider.Background5, transition_duration, Easing.OutQuint);
break;
case ModEffect.DifficultyIncrease:
contentBackground.FadeColour(colours.ForModType(ModType.DifficultyIncrease), transition_duration, Easing.OutQuint);
content.FadeColour(colourProvider.Background5, transition_duration, Easing.OutQuint);
break;
default:
throw new ArgumentOutOfRangeException(nameof(effect));
}
}
/// <summary>
/// Converts signed integer into <see cref="ModEffect"/>. Negative values are counted as difficulty reduction, positive as increase.
/// </summary>
/// <param name="comparison">Value to convert. Will arrive from comparison between <see cref="Current"/> bindable once it changes and it's <see cref="Bindable{T}.Default"/>.</param>
/// <returns>Effect of the value.</returns>
protected virtual ModEffect CalculateEffectForComparison(int comparison)
{
if (comparison == 0)
return ModEffect.NotChanged;
if (comparison < 0)
return ModEffect.DifficultyReduction;
return ModEffect.DifficultyIncrease;
}
protected enum ModEffect
{
NotChanged,
DifficultyReduction,
DifficultyIncrease
}
private class EffectCounter : RollingCounter<double>
{
private readonly string? format;
public EffectCounter(string? format)
{
this.format = format;
}
protected override double RollingDuration => 500;
protected override LocalisableString FormatCount(double count) => count.ToLocalisableString(format);
protected override OsuSpriteText CreateSpriteText() => new OsuSpriteText
{
Font = OsuFont.Default.With(size: 17, weight: FontWeight.SemiBold)
};
}
}
}