2017-02-07 04:59:30 +00:00
|
|
|
|
// 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 09:56:20 +00:00
|
|
|
|
|
|
|
|
|
using System;
|
2016-12-05 10:45:54 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Audio;
|
|
|
|
|
using osu.Framework.Audio.Sample;
|
2016-11-08 00:04:31 +00:00
|
|
|
|
using osu.Framework.Configuration;
|
2016-11-15 10:34:20 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Primitives;
|
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2016-11-08 00:04:31 +00:00
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2017-01-31 09:37:11 +00:00
|
|
|
|
using osu.Framework.Input;
|
2017-01-31 09:53:52 +00:00
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2016-11-15 10:34:20 +00:00
|
|
|
|
using OpenTK.Graphics;
|
2016-11-08 00:04:31 +00:00
|
|
|
|
|
2017-01-31 09:37:11 +00:00
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
2016-11-09 11:13:13 +00:00
|
|
|
|
{
|
2017-01-31 09:37:11 +00:00
|
|
|
|
public class OsuCheckbox : CheckBox
|
2016-11-09 11:13:13 +00:00
|
|
|
|
{
|
|
|
|
|
private Bindable<bool> bindable;
|
|
|
|
|
|
|
|
|
|
public Bindable<bool> Bindable
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (bindable != null)
|
|
|
|
|
bindable.ValueChanged -= bindableValueChanged;
|
|
|
|
|
bindable = value;
|
|
|
|
|
if (bindable != null)
|
2016-11-08 00:04:31 +00:00
|
|
|
|
{
|
|
|
|
|
bool state = State == CheckBoxState.Checked;
|
|
|
|
|
if (state != bindable.Value)
|
|
|
|
|
State = bindable.Value ? CheckBoxState.Checked : CheckBoxState.Unchecked;
|
2016-11-09 11:13:13 +00:00
|
|
|
|
bindable.ValueChanged += bindableValueChanged;
|
|
|
|
|
}
|
2017-02-13 15:35:24 +00:00
|
|
|
|
|
|
|
|
|
if (bindable?.Disabled ?? true)
|
|
|
|
|
Alpha = 0.3f;
|
2016-11-09 11:13:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-15 10:34:20 +00: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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-04 11:06:53 +00:00
|
|
|
|
private Nub nub;
|
2016-11-15 10:34:20 +00:00
|
|
|
|
private SpriteText labelSpriteText;
|
2017-02-18 08:35:04 +00:00
|
|
|
|
private SampleChannel sampleChecked;
|
|
|
|
|
private SampleChannel sampleUnchecked;
|
2016-11-15 10:34:20 +00:00
|
|
|
|
|
2017-01-31 09:37:11 +00:00
|
|
|
|
public OsuCheckbox()
|
2016-11-15 10:34:20 +00:00
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
|
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2017-01-31 09:53:52 +00:00
|
|
|
|
labelSpriteText = new OsuSpriteText(),
|
2017-02-04 11:06:53 +00:00
|
|
|
|
nub = new Nub
|
2016-11-15 10:34:20 +00:00
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.CentreRight,
|
|
|
|
|
Origin = Anchor.CentreRight,
|
|
|
|
|
Margin = new MarginPadding { Right = 5 },
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-09 11:13:13 +00:00
|
|
|
|
private void bindableValueChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
State = bindable.Value ? CheckBoxState.Checked : CheckBoxState.Unchecked;
|
2016-11-08 00:04:31 +00:00
|
|
|
|
}
|
2016-11-09 11:13:13 +00:00
|
|
|
|
|
2016-11-08 00:04:31 +00:00
|
|
|
|
protected override void Dispose(bool isDisposing)
|
2016-11-09 11:13:13 +00:00
|
|
|
|
{
|
|
|
|
|
if (bindable != null)
|
2016-11-08 00:04:31 +00:00
|
|
|
|
bindable.ValueChanged -= bindableValueChanged;
|
|
|
|
|
base.Dispose(isDisposing);
|
2016-11-09 11:13:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-15 10:34:20 +00:00
|
|
|
|
protected override bool OnHover(InputState state)
|
|
|
|
|
{
|
2017-02-04 08:50:58 +00:00
|
|
|
|
nub.Glowing = true;
|
|
|
|
|
nub.Expanded = true;
|
2016-11-15 10:34:20 +00:00
|
|
|
|
return base.OnHover(state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnHoverLost(InputState state)
|
|
|
|
|
{
|
2017-02-04 08:50:58 +00:00
|
|
|
|
nub.Glowing = false;
|
|
|
|
|
nub.Expanded = false;
|
2016-11-15 10:34:20 +00:00
|
|
|
|
base.OnHoverLost(state);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-05 10:45:54 +00:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(AudioManager audio)
|
|
|
|
|
{
|
|
|
|
|
sampleChecked = audio.Sample.Get(@"Checkbox/check-on");
|
|
|
|
|
sampleUnchecked = audio.Sample.Get(@"Checkbox/check-off");
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-08 00:04:31 +00:00
|
|
|
|
protected override void OnChecked()
|
2016-11-09 11:13:13 +00:00
|
|
|
|
{
|
2016-12-05 10:45:54 +00:00
|
|
|
|
sampleChecked?.Play();
|
2017-02-04 08:50:58 +00:00
|
|
|
|
nub.State = CheckBoxState.Checked;
|
2017-02-13 15:35:24 +00:00
|
|
|
|
|
|
|
|
|
if (bindable != null)
|
|
|
|
|
bindable.Value = true;
|
2016-11-09 11:13:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnUnchecked()
|
|
|
|
|
{
|
2016-12-05 10:45:54 +00:00
|
|
|
|
sampleUnchecked?.Play();
|
2017-02-04 08:50:58 +00:00
|
|
|
|
nub.State = CheckBoxState.Unchecked;
|
2017-02-13 15:35:24 +00:00
|
|
|
|
|
|
|
|
|
if (bindable != null)
|
|
|
|
|
bindable.Value = false;
|
2016-11-09 11:13:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|