Control colour via Current bindable

This commit is contained in:
ansel
2022-08-29 22:48:27 +03:00
parent 7faeed88b0
commit 5343c26452

View File

@ -1,10 +1,13 @@
// 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.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;
@ -13,11 +16,10 @@ using osuTK;
namespace osu.Game.Overlays.Mods
{
/// <inheritdoc />
/// <summary>
/// Base class for displays of mods effects.
/// </summary>
public abstract class ModsEffectDisplay : Container
public abstract class ModsEffectDisplay<TValue> : Container, IHasCurrentValue<TValue>
{
public const float HEIGHT = 42;
private const float transition_duration = 200;
@ -26,6 +28,14 @@ namespace osu.Game.Overlays.Mods
private readonly Box labelBackground;
private readonly Container content;
public Bindable<TValue> Current
{
get => current.Current;
set => current.Current = value;
}
private readonly BindableWithCurrent<TValue> current = new BindableWithCurrent<TValue>();
[Resolved]
private OsuColour colours { get; set; } = null!;
@ -37,6 +47,13 @@ namespace osu.Game.Overlays.Mods
/// </summary>
protected abstract LocalisableString Label { get; }
/// <summary>
/// Calculates, does current value increase difficulty or decrease it.
/// </summary>
/// <param name="value">Value to calculate for. Will arrive from <see cref="Current"/> bindable once it changes.</param>
/// <returns>Effect of the value.</returns>
protected abstract ModEffect CalculateEffect(TValue value);
protected virtual float ValueAreaWidth => 56;
protected override Container<Drawable> Content => content;
@ -118,28 +135,67 @@ namespace osu.Game.Overlays.Mods
labelBackground.Colour = colourProvider.Background4;
}
protected override void LoadComplete()
{
Current.BindValueChanged(e =>
{
var effect = CalculateEffect(e.NewValue);
setColours(effect);
}, true);
}
/// <summary>
/// Fades colours of text and its background according to displayed value.
/// </summary>
/// <param name="difference">Difference between actual value and default value.
/// Negative leads to green color, positive to red. Can be obtained via CompareTo(defaultValue).</param>
protected void SetColours(int difference)
/// <param name="effect">Effect of the value.</param>
private void setColours(ModEffect effect)
{
if (difference == 0)
switch (effect)
{
contentBackground.FadeColour(colourProvider.Background3, transition_duration, Easing.OutQuint);
content.FadeColour(Colour4.White, transition_duration, Easing.OutQuint);
}
else if (difference < 0)
{
contentBackground.FadeColour(colours.ForModType(ModType.DifficultyReduction), transition_duration, Easing.OutQuint);
content.FadeColour(colourProvider.Background5, transition_duration, Easing.OutQuint);
}
else
{
contentBackground.FadeColour(colours.ForModType(ModType.DifficultyIncrease), transition_duration, Easing.OutQuint);
content.FadeColour(colourProvider.Background5, transition_duration, Easing.OutQuint);
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));
}
}
#region Quick implementations for CalculateEffect
/// <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. Can be obtained from CompareTo.</param>
/// <returns>Effect.</returns>
protected ModEffect CalculateForSign(int comparison)
{
if (comparison == 0)
return ModEffect.NotChanged;
if (comparison < 0)
return ModEffect.DifficultyReduction;
return ModEffect.DifficultyIncrease;
}
#endregion
protected enum ModEffect
{
NotChanged,
DifficultyReduction,
DifficultyIncrease
}
}
}