2019-01-24 08:43:03 +00: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.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2016-12-05 10:45:54 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Audio;
|
|
|
|
|
using osu.Framework.Audio.Sample;
|
2016-11-15 10:34:20 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
2021-02-04 07:54:17 +00:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2016-11-08 00:04:31 +00:00
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2018-10-02 03:02:47 +00:00
|
|
|
|
using osu.Framework.Input.Events;
|
2021-10-30 16:50:13 +00:00
|
|
|
|
using osu.Framework.Localisation;
|
2019-06-09 07:47:48 +00:00
|
|
|
|
using osu.Game.Graphics.Containers;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-01-31 09:37:11 +00:00
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
2016-11-09 11:13:13 +00:00
|
|
|
|
{
|
2017-03-31 08:38:33 +00:00
|
|
|
|
public partial class OsuCheckbox : Checkbox
|
2016-11-09 11:13:13 +00:00
|
|
|
|
{
|
2021-02-02 12:29:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether to play sounds when the state changes as a result of user interaction.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual bool PlaySoundsOnUserChange => true;
|
|
|
|
|
|
2021-10-30 16:50:13 +00:00
|
|
|
|
public LocalisableString LabelText
|
2016-11-15 10:34:20 +00:00
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
2022-07-11 21:12:41 +00:00
|
|
|
|
if (LabelTextFlowContainer != null)
|
|
|
|
|
LabelTextFlowContainer.Text = value;
|
2016-11-15 10:34:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2016-11-15 10:34:20 +00:00
|
|
|
|
public MarginPadding LabelPadding
|
|
|
|
|
{
|
2022-07-11 21:12:41 +00:00
|
|
|
|
get => LabelTextFlowContainer?.Padding ?? new MarginPadding();
|
2016-11-15 10:34:20 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
2022-07-11 21:12:41 +00:00
|
|
|
|
if (LabelTextFlowContainer != null)
|
|
|
|
|
LabelTextFlowContainer.Padding = value;
|
2016-11-15 10:34:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-05-30 07:40:35 +00:00
|
|
|
|
protected readonly Nub Nub;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-07-11 21:12:41 +00:00
|
|
|
|
protected readonly OsuTextFlowContainer LabelTextFlowContainer;
|
2021-01-19 08:11:40 +00:00
|
|
|
|
private Sample sampleChecked;
|
|
|
|
|
private Sample sampleUnchecked;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-02-04 07:53:55 +00:00
|
|
|
|
public OsuCheckbox(bool nubOnRight = true)
|
2016-11-15 10:34:20 +00:00
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
|
RelativeSizeAxes = Axes.X;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2019-06-09 07:47:48 +00:00
|
|
|
|
const float nub_padding = 5;
|
|
|
|
|
|
2016-11-15 10:34:20 +00:00
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2022-07-11 21:12:41 +00:00
|
|
|
|
LabelTextFlowContainer = new OsuTextFlowContainer(ApplyLabelParameters)
|
2019-06-09 07:47:48 +00:00
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
2017-11-25 17:41:18 +00:00
|
|
|
|
},
|
2021-02-04 07:53:55 +00:00
|
|
|
|
Nub = new Nub(),
|
2021-02-11 05:36:41 +00:00
|
|
|
|
new HoverSounds()
|
2016-11-15 10:34:20 +00:00
|
|
|
|
};
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-02-04 07:53:55 +00:00
|
|
|
|
if (nubOnRight)
|
|
|
|
|
{
|
|
|
|
|
Nub.Anchor = Anchor.CentreRight;
|
|
|
|
|
Nub.Origin = Anchor.CentreRight;
|
|
|
|
|
Nub.Margin = new MarginPadding { Right = nub_padding };
|
2022-07-11 21:12:41 +00:00
|
|
|
|
LabelTextFlowContainer.Padding = new MarginPadding { Right = Nub.EXPANDED_SIZE + nub_padding * 2 };
|
2021-02-04 07:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-02-04 13:53:41 +00:00
|
|
|
|
Nub.Anchor = Anchor.CentreLeft;
|
|
|
|
|
Nub.Origin = Anchor.CentreLeft;
|
2021-02-04 07:53:55 +00:00
|
|
|
|
Nub.Margin = new MarginPadding { Left = nub_padding };
|
2022-07-11 21:12:41 +00:00
|
|
|
|
LabelTextFlowContainer.Padding = new MarginPadding { Left = Nub.EXPANDED_SIZE + nub_padding * 2 };
|
2021-02-04 07:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 07:40:35 +00:00
|
|
|
|
Nub.Current.BindTo(Current);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-07-11 21:12:41 +00:00
|
|
|
|
Current.DisabledChanged += disabled => LabelTextFlowContainer.Alpha = Nub.Alpha = disabled ? 0.3f : 1;
|
2018-02-08 05:43:07 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-02-04 07:54:17 +00: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 05:09:30 +00:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(AudioManager audio)
|
2018-02-08 05:43:07 +00:00
|
|
|
|
{
|
2019-08-15 05:09:30 +00:00
|
|
|
|
sampleChecked = audio.Samples.Get(@"UI/check-on");
|
|
|
|
|
sampleUnchecked = audio.Samples.Get(@"UI/check-off");
|
2016-11-09 11:13:13 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
2016-11-15 10:34:20 +00:00
|
|
|
|
{
|
2017-05-30 07:40:35 +00:00
|
|
|
|
Nub.Glowing = true;
|
2018-10-02 03:02:47 +00:00
|
|
|
|
return base.OnHover(e);
|
2016-11-15 10:34:20 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
2016-11-15 10:34:20 +00:00
|
|
|
|
{
|
2017-05-30 07:40:35 +00:00
|
|
|
|
Nub.Glowing = false;
|
2018-10-02 03:02:47 +00:00
|
|
|
|
base.OnHoverLost(e);
|
2016-11-15 10:34:20 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2019-08-15 05:09:30 +00:00
|
|
|
|
protected override void OnUserChange(bool value)
|
2016-12-05 10:45:54 +00:00
|
|
|
|
{
|
2019-08-15 05:09:30 +00:00
|
|
|
|
base.OnUserChange(value);
|
2021-02-02 12:29:00 +00:00
|
|
|
|
|
|
|
|
|
if (PlaySoundsOnUserChange)
|
|
|
|
|
{
|
|
|
|
|
if (value)
|
|
|
|
|
sampleChecked?.Play();
|
|
|
|
|
else
|
|
|
|
|
sampleUnchecked?.Play();
|
|
|
|
|
}
|
2016-12-05 10:45:54 +00:00
|
|
|
|
}
|
2016-11-09 11:13:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|