1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-17 05:52:54 +08:00
osu-lazer/osu.Game/Screens/OnlinePlay/FreeModSelectOverlay.cs

158 lines
4.6 KiB
C#
Raw Normal View History

// 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.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays.Mods;
using osu.Game.Rulesets.Mods;
namespace osu.Game.Screens.OnlinePlay
{
/// <summary>
/// A <see cref="ModSelectOverlay"/> used for free-mod selection in online play.
/// </summary>
public class FreeModSelectOverlay : ModSelectOverlay
{
protected override bool Stacked => false;
2021-02-10 18:56:59 +08:00
protected override bool AllowConfiguration => false;
2021-02-02 20:29:08 +08:00
public new Func<Mod, bool> IsValidMod
{
get => base.IsValidMod;
set => base.IsValidMod = m => m.HasImplementation && m.UserPlayable && value(m);
2021-02-02 20:29:08 +08:00
}
public FreeModSelectOverlay()
{
2021-02-02 20:29:08 +08:00
IsValidMod = m => true;
DeselectAllButton.Alpha = 0;
Drawable selectAllButton;
Drawable deselectAllButton;
FooterContainer.AddRange(new[]
{
selectAllButton = new TriangleButton
{
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
Width = 180,
Text = "Select All",
Action = selectAll,
},
// Unlike the base mod select overlay, this button deselects mods instantaneously.
deselectAllButton = new TriangleButton
{
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
Width = 180,
Text = "Deselect All",
Action = deselectAll,
},
});
FooterContainer.SetLayoutPosition(selectAllButton, -2);
FooterContainer.SetLayoutPosition(deselectAllButton, -1);
}
private void selectAll()
{
foreach (var section in ModSectionsContainer.Children)
section.SelectAll();
}
private void deselectAll()
{
foreach (var section in ModSectionsContainer.Children)
2021-02-04 17:10:55 +08:00
section.DeselectAll();
}
2021-02-22 14:47:47 +08:00
protected override void OnAvailableModsChanged()
{
base.OnAvailableModsChanged();
foreach (var section in ModSectionsContainer.Children)
((FreeModSection)section).UpdateCheckboxState();
}
protected override ModSection CreateModSection(ModType type) => new FreeModSection(type);
private class FreeModSection : ModSection
{
private HeaderCheckbox checkbox;
public FreeModSection(ModType type)
: base(type)
{
}
protected override Drawable CreateHeader(string text) => new Container
{
AutoSizeAxes = Axes.Y,
2021-02-04 15:57:39 +08:00
RelativeSizeAxes = Axes.X,
Child = checkbox = new HeaderCheckbox
{
LabelText = text,
Changed = onCheckboxChanged
}
};
private void onCheckboxChanged(bool value)
{
2021-02-04 17:10:55 +08:00
if (value)
SelectAll();
else
DeselectAll();
}
2021-02-04 17:10:55 +08:00
protected override void ModButtonStateChanged(Mod mod)
{
2021-02-04 17:10:55 +08:00
base.ModButtonStateChanged(mod);
2021-02-22 14:47:47 +08:00
UpdateCheckboxState();
}
2021-02-22 14:47:47 +08:00
public void UpdateCheckboxState()
{
2021-02-04 17:10:55 +08:00
if (!SelectionAnimationRunning)
{
2021-02-22 14:47:47 +08:00
var validButtons = Buttons.Where(b => b.Mod.HasImplementation);
2021-02-04 17:10:55 +08:00
checkbox.Current.Value = validButtons.All(b => b.Selected);
}
}
}
private class HeaderCheckbox : OsuCheckbox
{
public Action<bool> Changed;
2021-02-02 20:29:00 +08:00
protected override bool PlaySoundsOnUserChange => false;
public HeaderCheckbox()
: base(false)
{
}
protected override void ApplyLabelParameters(SpriteText text)
{
base.ApplyLabelParameters(text);
text.Font = OsuFont.GetFont(weight: FontWeight.Bold);
}
protected override void OnUserChange(bool value)
{
base.OnUserChange(value);
Changed?.Invoke(value);
}
}
}
}