1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 21:02:55 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuCheckbox.cs

120 lines
3.2 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;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics.Sprites;
2018-11-20 15:51:59 +08:00
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Graphics.UserInterface
{
public class OsuCheckbox : Checkbox
{
private Bindable<bool> bindable;
public Bindable<bool> Bindable
{
set
{
bindable = value;
Current.BindTo(bindable);
}
}
public Color4 CheckedColor { get; set; } = Color4.Cyan;
public Color4 UncheckedColor { get; set; } = Color4.White;
public int FadeDuration { get; set; }
public string LabelText
{
get => labelSpriteText?.Text;
2018-04-13 17:19:50 +08:00
set
{
if (labelSpriteText != null)
labelSpriteText.Text = value;
}
}
public MarginPadding LabelPadding
{
get => labelSpriteText?.Padding ?? new MarginPadding();
2018-04-13 17:19:50 +08:00
set
{
if (labelSpriteText != null)
labelSpriteText.Padding = value;
}
}
protected readonly Nub Nub;
private readonly SpriteText labelSpriteText;
private SampleChannel sampleChecked;
private SampleChannel sampleUnchecked;
public OsuCheckbox()
{
AutoSizeAxes = Axes.Y;
RelativeSizeAxes = Axes.X;
Children = new Drawable[]
{
labelSpriteText = new OsuSpriteText(),
Nub = new Nub
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Margin = new MarginPadding { Right = 5 },
},
new HoverClickSounds()
};
Nub.Current.BindTo(Current);
Current.DisabledChanged += disabled =>
{
2019-05-26 23:55:12 +08:00
labelSpriteText.Alpha = Nub.Alpha = disabled ? 0.3f : 1;
2018-04-13 17:19:50 +08:00
};
}
protected override void LoadComplete()
{
base.LoadComplete();
Current.ValueChanged += enabled =>
2018-04-13 17:19:50 +08:00
{
if (enabled.NewValue)
2018-04-13 17:19:50 +08:00
sampleChecked?.Play();
else
sampleUnchecked?.Play();
};
}
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;
Nub.Expanded = 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;
Nub.Expanded = false;
2018-10-02 11:02:47 +08:00
base.OnHoverLost(e);
2018-04-13 17:19:50 +08:00
}
[BackgroundDependencyLoader]
2018-08-31 06:04:40 +08:00
private void load(AudioManager audio)
2018-04-13 17:19:50 +08:00
{
sampleChecked = audio.Samples.Get(@"UI/check-on");
sampleUnchecked = audio.Samples.Get(@"UI/check-off");
2018-04-13 17:19:50 +08:00
}
}
}