1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 02:57:26 +08:00
osu-lazer/osu.Game/Screens/OnlinePlay/FooterButtonFreeMods.cs

147 lines
5.0 KiB
C#
Raw Normal View History

2023-06-23 00:37:25 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2021-02-01 17:08:49 +08:00
// See the LICENCE file in the repository root for full licence text.
using System;
2021-02-01 17:08:49 +08:00
using System.Collections.Generic;
using System.Linq;
2021-02-01 17:08:49 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
2021-02-01 17:08:49 +08:00
using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
2021-02-01 17:08:49 +08:00
using osu.Game.Rulesets.Mods;
using osu.Game.Screens.Select;
using osuTK;
2021-02-02 20:46:22 +08:00
namespace osu.Game.Screens.OnlinePlay
2021-02-01 17:08:49 +08:00
{
2022-11-24 13:32:20 +08:00
public partial class FooterButtonFreeMods : FooterButton, IHasCurrentValue<IReadOnlyList<Mod>>
2021-02-01 17:08:49 +08:00
{
private readonly BindableWithCurrent<IReadOnlyList<Mod>> current = new BindableWithCurrent<IReadOnlyList<Mod>>(Array.Empty<Mod>());
public Bindable<IReadOnlyList<Mod>> Current
{
get => current.Current;
set
{
ArgumentNullException.ThrowIfNull(value);
current.Current = value;
}
}
private OsuSpriteText count = null!;
private Circle circle = null!;
private readonly FreeModSelectOverlay freeModSelectOverlay;
public FooterButtonFreeMods(FreeModSelectOverlay freeModSelectOverlay)
2021-02-01 17:08:49 +08:00
{
this.freeModSelectOverlay = freeModSelectOverlay;
2021-02-01 17:08:49 +08:00
}
[Resolved]
private OsuColour colours { get; set; } = null!;
2021-02-01 17:08:49 +08:00
[BackgroundDependencyLoader]
private void load()
2021-02-01 17:08:49 +08:00
{
ButtonContentContainer.AddRange(new[]
2021-02-01 17:08:49 +08:00
{
new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.Both,
Children = new Drawable[]
{
circle = new Circle
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Colour = colours.YellowDark,
RelativeSizeAxes = Axes.Both,
},
count = new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Padding = new MarginPadding(5),
UseFullGlyphHeight = false,
}
}
},
new IconButton
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Scale = new Vector2(0.8f),
Icon = FontAwesome.Solid.Bars,
Action = () => freeModSelectOverlay.ToggleVisibility()
}
2021-02-01 17:08:49 +08:00
});
SelectedColour = colours.Yellow;
DeselectedColour = SelectedColour.Opacity(0.5f);
Text = @"freemods";
}
protected override void LoadComplete()
{
base.LoadComplete();
Current.BindValueChanged(_ => updateModDisplay(), true);
// Overwrite any external behaviour as we delegate the main toggle action to a sub-button.
Action = toggleAllFreeMods;
}
/// <summary>
/// Immediately toggle all free mods on/off.
/// </summary>
private void toggleAllFreeMods()
{
var availableMods = allAvailableAndValidMods.ToArray();
Current.Value = Current.Value.Count == availableMods.Length
? Array.Empty<Mod>()
: availableMods;
2021-02-01 17:08:49 +08:00
}
private void updateModDisplay()
{
2023-10-06 20:01:23 +08:00
int currentCount = Current.Value.Count;
2023-10-06 20:01:23 +08:00
if (currentCount == allAvailableAndValidMods.Count())
{
count.Text = "all";
count.FadeColour(colours.Gray2, 200, Easing.OutQuint);
circle.FadeColour(colours.Yellow, 200, Easing.OutQuint);
}
2023-10-06 20:01:23 +08:00
else if (currentCount > 0)
{
2023-10-06 20:01:23 +08:00
count.Text = $"{currentCount} mods";
count.FadeColour(colours.Gray2, 200, Easing.OutQuint);
circle.FadeColour(colours.YellowDark, 200, Easing.OutQuint);
}
2021-02-01 17:08:49 +08:00
else
{
count.Text = "off";
count.FadeColour(colours.GrayF, 200, Easing.OutQuint);
circle.FadeColour(colours.Gray4, 200, Easing.OutQuint);
}
2021-02-01 17:08:49 +08:00
}
private IEnumerable<Mod> allAvailableAndValidMods => freeModSelectOverlay.AllAvailableMods
.Where(state => state.ValidForSelection.Value)
.Select(state => state.Mod);
2021-02-01 17:08:49 +08:00
}
}