2022-02-24 06:11:12 +08:00
|
|
|
// 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.
|
|
|
|
|
2022-05-07 22:35:34 +08:00
|
|
|
#nullable enable
|
|
|
|
|
2022-02-20 20:40:52 +08:00
|
|
|
using System;
|
2022-02-24 06:11:12 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Audio;
|
|
|
|
using osu.Framework.Audio.Sample;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Colour;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
|
|
|
using osu.Framework.Input.Events;
|
|
|
|
using osu.Framework.Utils;
|
2022-05-07 22:35:34 +08:00
|
|
|
using osu.Game.Audio;
|
2022-02-24 06:11:12 +08:00
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Graphics.Containers;
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
using osu.Game.Rulesets.Mods;
|
|
|
|
using osu.Game.Rulesets.UI;
|
|
|
|
using osuTK;
|
|
|
|
using osuTK.Input;
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Mods
|
|
|
|
{
|
|
|
|
public class ModPanel : OsuClickableContainer
|
|
|
|
{
|
|
|
|
public Mod Mod { get; }
|
|
|
|
public BindableBool Active { get; } = new BindableBool();
|
2022-02-20 20:40:52 +08:00
|
|
|
public BindableBool Filtered { get; } = new BindableBool();
|
2022-02-24 06:11:12 +08:00
|
|
|
|
|
|
|
protected readonly Box Background;
|
|
|
|
protected readonly Container SwitchContainer;
|
|
|
|
protected readonly Container MainContentContainer;
|
|
|
|
protected readonly Box TextBackground;
|
|
|
|
protected readonly FillFlowContainer TextFlow;
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
protected OverlayColourProvider ColourProvider { get; private set; } = null!;
|
|
|
|
|
|
|
|
protected const double TRANSITION_DURATION = 150;
|
2022-02-24 13:55:35 +08:00
|
|
|
|
2022-02-20 00:52:16 +08:00
|
|
|
public const float CORNER_RADIUS = 7;
|
2022-02-24 06:11:12 +08:00
|
|
|
|
|
|
|
protected const float HEIGHT = 42;
|
|
|
|
protected const float IDLE_SWITCH_WIDTH = 54;
|
|
|
|
protected const float EXPANDED_SWITCH_WIDTH = 70;
|
|
|
|
|
|
|
|
private Colour4 activeColour;
|
|
|
|
|
2022-05-07 22:35:34 +08:00
|
|
|
private readonly Bindable<bool> samplePlaybackDisabled = new BindableBool();
|
2022-02-24 06:11:12 +08:00
|
|
|
private Sample? sampleOff;
|
|
|
|
private Sample? sampleOn;
|
|
|
|
|
|
|
|
public ModPanel(Mod mod)
|
|
|
|
{
|
|
|
|
Mod = mod;
|
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
Height = 42;
|
|
|
|
|
|
|
|
// all below properties are applied to `Content` rather than the `ModPanel` in its entirety
|
|
|
|
// to allow external components to set these properties on the panel without affecting
|
|
|
|
// its "internal" appearance.
|
|
|
|
Content.Masking = true;
|
|
|
|
Content.CornerRadius = CORNER_RADIUS;
|
|
|
|
Content.BorderThickness = 2;
|
2022-04-20 15:30:58 +08:00
|
|
|
Content.Shear = new Vector2(ShearedOverlayContainer.SHEAR, 0);
|
2022-02-24 06:11:12 +08:00
|
|
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
Background = new Box
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both
|
|
|
|
},
|
|
|
|
SwitchContainer = new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Y,
|
|
|
|
Child = new ModSwitchSmall(mod)
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Active = { BindTarget = Active },
|
2022-04-20 15:30:58 +08:00
|
|
|
Shear = new Vector2(-ShearedOverlayContainer.SHEAR, 0),
|
2022-02-24 06:11:12 +08:00
|
|
|
Scale = new Vector2(HEIGHT / ModSwitchSmall.DEFAULT_SIZE)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
MainContentContainer = new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Child = new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Masking = true,
|
|
|
|
CornerRadius = CORNER_RADIUS,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
TextBackground = new Box
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both
|
|
|
|
},
|
|
|
|
TextFlow = new FillFlowContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Padding = new MarginPadding
|
|
|
|
{
|
|
|
|
Horizontal = 17.5f,
|
|
|
|
Vertical = 4
|
|
|
|
},
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
Children = new[]
|
|
|
|
{
|
|
|
|
new OsuSpriteText
|
|
|
|
{
|
|
|
|
Text = mod.Name,
|
|
|
|
Font = OsuFont.TorusAlternate.With(size: 18, weight: FontWeight.SemiBold),
|
2022-04-20 15:30:58 +08:00
|
|
|
Shear = new Vector2(-ShearedOverlayContainer.SHEAR, 0),
|
2022-02-24 06:11:12 +08:00
|
|
|
Margin = new MarginPadding
|
|
|
|
{
|
2022-04-20 15:30:58 +08:00
|
|
|
Left = -18 * ShearedOverlayContainer.SHEAR
|
2022-02-24 06:11:12 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
new OsuSpriteText
|
|
|
|
{
|
|
|
|
Text = mod.Description,
|
|
|
|
Font = OsuFont.Default.With(size: 12),
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Truncate = true,
|
2022-04-20 15:30:58 +08:00
|
|
|
Shear = new Vector2(-ShearedOverlayContainer.SHEAR, 0)
|
2022-02-24 06:11:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Action = Active.Toggle;
|
|
|
|
}
|
|
|
|
|
2022-05-07 22:35:34 +08:00
|
|
|
[BackgroundDependencyLoader(true)]
|
|
|
|
private void load(AudioManager audio, OsuColour colours, ISamplePlaybackDisabler? samplePlaybackDisabler)
|
2022-02-24 06:11:12 +08:00
|
|
|
{
|
|
|
|
sampleOn = audio.Samples.Get(@"UI/check-on");
|
|
|
|
sampleOff = audio.Samples.Get(@"UI/check-off");
|
|
|
|
|
|
|
|
activeColour = colours.ForModType(Mod.Type);
|
2022-05-07 22:35:34 +08:00
|
|
|
|
|
|
|
if (samplePlaybackDisabler != null)
|
|
|
|
((IBindable<bool>)samplePlaybackDisabled).BindTo(samplePlaybackDisabler.SamplePlaybackDisabled);
|
2022-02-24 06:11:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override HoverSounds CreateHoverSounds(HoverSampleSet sampleSet) => new HoverSounds(sampleSet);
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
Active.BindValueChanged(_ =>
|
|
|
|
{
|
|
|
|
playStateChangeSamples();
|
|
|
|
UpdateState();
|
|
|
|
});
|
2022-04-18 04:30:02 +08:00
|
|
|
Filtered.BindValueChanged(_ => updateFilterState(), true);
|
2022-02-24 06:11:12 +08:00
|
|
|
|
|
|
|
UpdateState();
|
|
|
|
FinishTransforms(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void playStateChangeSamples()
|
|
|
|
{
|
2022-05-07 22:35:34 +08:00
|
|
|
if (samplePlaybackDisabled.Value)
|
|
|
|
return;
|
|
|
|
|
2022-02-24 06:11:12 +08:00
|
|
|
if (Active.Value)
|
|
|
|
sampleOn?.Play();
|
|
|
|
else
|
|
|
|
sampleOff?.Play();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
|
|
|
{
|
|
|
|
UpdateState();
|
|
|
|
return base.OnHover(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
|
|
|
{
|
|
|
|
UpdateState();
|
|
|
|
base.OnHoverLost(e);
|
|
|
|
}
|
|
|
|
|
2022-02-24 13:55:35 +08:00
|
|
|
private bool mouseDown;
|
2022-02-24 06:11:12 +08:00
|
|
|
|
|
|
|
protected override bool OnMouseDown(MouseDownEvent e)
|
|
|
|
{
|
|
|
|
if (e.Button == MouseButton.Left)
|
2022-02-24 13:55:35 +08:00
|
|
|
mouseDown = true;
|
|
|
|
|
|
|
|
UpdateState();
|
2022-02-28 13:32:50 +08:00
|
|
|
return false;
|
2022-02-24 06:11:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnMouseUp(MouseUpEvent e)
|
|
|
|
{
|
2022-02-24 13:55:35 +08:00
|
|
|
mouseDown = false;
|
|
|
|
|
|
|
|
UpdateState();
|
2022-02-24 06:11:12 +08:00
|
|
|
base.OnMouseUp(e);
|
|
|
|
}
|
|
|
|
|
2022-04-21 04:59:37 +08:00
|
|
|
protected virtual Colour4 BackgroundColour => Active.Value ? activeColour.Darken(0.3f) : (Colour4)ColourProvider.Background3;
|
|
|
|
protected virtual Colour4 ForegroundColour => Active.Value ? activeColour : (Colour4)ColourProvider.Background2;
|
|
|
|
protected virtual Colour4 TextColour => Active.Value ? (Colour4)ColourProvider.Background6 : Colour4.White;
|
|
|
|
|
2022-02-24 13:55:35 +08:00
|
|
|
protected virtual void UpdateState()
|
2022-02-24 06:11:12 +08:00
|
|
|
{
|
2022-02-24 13:55:35 +08:00
|
|
|
float targetWidth = Active.Value ? EXPANDED_SWITCH_WIDTH : IDLE_SWITCH_WIDTH;
|
|
|
|
double transitionDuration = TRANSITION_DURATION;
|
2022-02-24 06:11:12 +08:00
|
|
|
|
2022-04-21 04:59:37 +08:00
|
|
|
Colour4 backgroundColour = BackgroundColour;
|
|
|
|
Colour4 foregroundColour = ForegroundColour;
|
|
|
|
Colour4 textColour = TextColour;
|
2022-02-24 06:11:12 +08:00
|
|
|
|
2022-02-24 13:55:35 +08:00
|
|
|
// Hover affects colour of button background
|
|
|
|
if (IsHovered)
|
2022-02-24 06:11:12 +08:00
|
|
|
{
|
2022-04-21 04:59:37 +08:00
|
|
|
backgroundColour = backgroundColour.Lighten(0.1f);
|
|
|
|
foregroundColour = foregroundColour.Lighten(0.1f);
|
2022-02-24 06:11:12 +08:00
|
|
|
}
|
|
|
|
|
2022-02-24 13:55:35 +08:00
|
|
|
// Mouse down adds a halfway tween of the movement
|
|
|
|
if (mouseDown)
|
2022-02-24 06:11:12 +08:00
|
|
|
{
|
2022-02-24 13:55:35 +08:00
|
|
|
targetWidth = (float)Interpolation.Lerp(IDLE_SWITCH_WIDTH, EXPANDED_SWITCH_WIDTH, 0.5f);
|
|
|
|
transitionDuration *= 4;
|
2022-02-24 06:11:12 +08:00
|
|
|
}
|
2022-02-24 13:55:35 +08:00
|
|
|
|
2022-04-21 04:59:37 +08:00
|
|
|
Content.TransformTo(nameof(BorderColour), ColourInfo.GradientVertical(backgroundColour, foregroundColour), transitionDuration, Easing.OutQuint);
|
|
|
|
Background.FadeColour(backgroundColour, transitionDuration, Easing.OutQuint);
|
2022-02-24 13:55:35 +08:00
|
|
|
SwitchContainer.ResizeWidthTo(targetWidth, transitionDuration, Easing.OutQuint);
|
|
|
|
MainContentContainer.TransformTo(nameof(Padding), new MarginPadding
|
2022-02-24 06:11:12 +08:00
|
|
|
{
|
2022-02-24 13:55:35 +08:00
|
|
|
Left = targetWidth,
|
|
|
|
Right = CORNER_RADIUS
|
|
|
|
}, transitionDuration, Easing.OutQuint);
|
2022-04-21 04:59:37 +08:00
|
|
|
TextBackground.FadeColour(foregroundColour, transitionDuration, Easing.OutQuint);
|
2022-02-24 13:55:35 +08:00
|
|
|
TextFlow.FadeColour(textColour, transitionDuration, Easing.OutQuint);
|
2022-02-24 06:11:12 +08:00
|
|
|
}
|
2022-02-20 20:40:52 +08:00
|
|
|
|
|
|
|
#region Filtering support
|
|
|
|
|
|
|
|
public void ApplyFilter(Func<Mod, bool>? filter)
|
|
|
|
{
|
|
|
|
Filtered.Value = filter != null && !filter.Invoke(Mod);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateFilterState()
|
|
|
|
{
|
|
|
|
this.FadeTo(Filtered.Value ? 0 : 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2022-02-24 06:11:12 +08:00
|
|
|
}
|
|
|
|
}
|