1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 00:27:25 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuCheckbox.cs

126 lines
3.7 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.
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.UserInterface;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Graphics.Containers;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Graphics.UserInterface
{
public class OsuCheckbox : Checkbox
{
2021-02-02 20:29:00 +08:00
/// <summary>
/// Whether to play sounds when the state changes as a result of user interaction.
/// </summary>
protected virtual bool PlaySoundsOnUserChange => true;
public LocalisableString LabelText
2018-04-13 17:19:50 +08:00
{
set
{
2019-06-09 16:07:23 +08:00
if (labelText != null)
labelText.Text = value;
2018-04-13 17:19:50 +08:00
}
}
public MarginPadding LabelPadding
{
2019-06-09 16:07:23 +08:00
get => labelText?.Padding ?? new MarginPadding();
2018-04-13 17:19:50 +08:00
set
{
2019-06-09 16:07:23 +08:00
if (labelText != null)
labelText.Padding = value;
2018-04-13 17:19:50 +08:00
}
}
protected readonly Nub Nub;
2019-06-09 16:07:23 +08:00
private readonly OsuTextFlowContainer labelText;
2021-01-19 16:11:40 +08:00
private Sample sampleChecked;
private Sample sampleUnchecked;
2018-04-13 17:19:50 +08:00
public OsuCheckbox(bool nubOnRight = true)
2018-04-13 17:19:50 +08:00
{
AutoSizeAxes = Axes.Y;
RelativeSizeAxes = Axes.X;
const float nub_padding = 5;
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
labelText = new OsuTextFlowContainer(ApplyLabelParameters)
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
2018-04-13 17:19:50 +08:00
},
Nub = new Nub(),
new HoverSounds()
2018-04-13 17:19:50 +08:00
};
if (nubOnRight)
{
Nub.Anchor = Anchor.CentreRight;
Nub.Origin = Anchor.CentreRight;
Nub.Margin = new MarginPadding { Right = nub_padding };
labelText.Padding = new MarginPadding { Right = Nub.EXPANDED_SIZE + nub_padding * 2 };
}
else
{
2021-02-04 21:53:41 +08:00
Nub.Anchor = Anchor.CentreLeft;
Nub.Origin = Anchor.CentreLeft;
Nub.Margin = new MarginPadding { Left = nub_padding };
labelText.Padding = new MarginPadding { Left = Nub.EXPANDED_SIZE + nub_padding * 2 };
}
2018-04-13 17:19:50 +08:00
Nub.Current.BindTo(Current);
2019-06-11 17:24:50 +08:00
Current.DisabledChanged += disabled => labelText.Alpha = Nub.Alpha = disabled ? 0.3f : 1;
2018-04-13 17:19:50 +08:00
}
/// <summary>
/// A function which can be overridden to change the parameters of the label's text.
/// </summary>
protected virtual void ApplyLabelParameters(SpriteText text)
{
}
2019-08-15 13:09:30 +08:00
[BackgroundDependencyLoader]
private void load(AudioManager audio)
2018-04-13 17:19:50 +08:00
{
2019-08-15 13:09:30 +08:00
sampleChecked = audio.Samples.Get(@"UI/check-on");
sampleUnchecked = audio.Samples.Get(@"UI/check-off");
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
Nub.Glowing = true;
2018-10-02 11:02:47 +08:00
return base.OnHover(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 17:19:50 +08:00
{
Nub.Glowing = false;
2018-10-02 11:02:47 +08:00
base.OnHoverLost(e);
2018-04-13 17:19:50 +08:00
}
2019-08-15 13:09:30 +08:00
protected override void OnUserChange(bool value)
2018-04-13 17:19:50 +08:00
{
2019-08-15 13:09:30 +08:00
base.OnUserChange(value);
2021-02-02 20:29:00 +08:00
if (PlaySoundsOnUserChange)
{
if (value)
sampleChecked?.Play();
else
sampleUnchecked?.Play();
}
2018-04-13 17:19:50 +08:00
}
}
}