1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 04:07:24 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuCheckbox.cs

140 lines
4.2 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-12-06 17:56:20 +08:00
2016-12-05 18:45:54 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input;
using osu.Game.Graphics.Sprites;
using OpenTK.Graphics;
namespace osu.Game.Graphics.UserInterface
2016-11-09 19:13:13 +08:00
{
2017-03-31 16:38:33 +08:00
public class OsuCheckbox : Checkbox
2016-11-09 19:13:13 +08:00
{
private Bindable<bool> bindable;
public Bindable<bool> Bindable
{
set
{
if (bindable != null)
bindable.ValueChanged -= bindableValueChanged;
bindable = value;
if (bindable != null)
{
2017-03-31 16:38:33 +08:00
bool state = State == CheckboxState.Checked;
if (state != bindable.Value)
2017-03-31 16:38:33 +08:00
State = bindable.Value ? CheckboxState.Checked : CheckboxState.Unchecked;
2016-11-09 19:13:13 +08:00
bindable.ValueChanged += bindableValueChanged;
}
if (bindable?.Disabled ?? true)
Alpha = 0.3f;
2016-11-09 19:13:13 +08:00
}
}
public Color4 CheckedColor { get; set; } = Color4.Cyan;
public Color4 UncheckedColor { get; set; } = Color4.White;
public int FadeDuration { get; set; }
public string LabelText
{
get { return labelSpriteText?.Text; }
set
{
if (labelSpriteText != null)
labelSpriteText.Text = value;
}
}
public MarginPadding LabelPadding
{
get { return labelSpriteText?.Padding ?? new MarginPadding(); }
set
{
if (labelSpriteText != null)
labelSpriteText.Padding = value;
}
}
private 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(),
2017-02-04 19:06:53 +08:00
nub = new Nub
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Margin = new MarginPadding { Right = 5 },
}
};
}
private void bindableValueChanged(bool isChecked)
2016-11-09 19:13:13 +08:00
{
State = isChecked ? CheckboxState.Checked : CheckboxState.Unchecked;
}
2016-11-09 19:13:13 +08:00
protected override void Dispose(bool isDisposing)
2016-11-09 19:13:13 +08:00
{
if (bindable != null)
bindable.ValueChanged -= bindableValueChanged;
base.Dispose(isDisposing);
2016-11-09 19:13:13 +08:00
}
protected override bool OnHover(InputState state)
{
nub.Glowing = true;
nub.Expanded = true;
return base.OnHover(state);
}
protected override void OnHoverLost(InputState state)
{
nub.Glowing = false;
nub.Expanded = false;
base.OnHoverLost(state);
}
2016-12-05 18:45:54 +08:00
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
sampleChecked = audio.Sample.Get(@"Checkbox/check-on");
sampleUnchecked = audio.Sample.Get(@"Checkbox/check-off");
}
protected override void OnChecked()
2016-11-09 19:13:13 +08:00
{
2016-12-05 18:45:54 +08:00
sampleChecked?.Play();
2017-03-31 16:38:33 +08:00
nub.State = CheckboxState.Checked;
if (bindable != null)
bindable.Value = true;
2016-11-09 19:13:13 +08:00
}
protected override void OnUnchecked()
{
2016-12-05 18:45:54 +08:00
sampleUnchecked?.Play();
2017-03-31 16:38:33 +08:00
nub.State = CheckboxState.Unchecked;
if (bindable != null)
bindable.Value = false;
2016-11-09 19:13:13 +08:00
}
}
}