mirror of
https://github.com/ppy/osu.git
synced 2026-05-17 01:43:01 +08:00
159 lines
5.1 KiB
C#
159 lines
5.1 KiB
C#
// 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.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using osu.Framework.Allocation;
|
|
using osu.Framework.Audio;
|
|
using osu.Framework.Audio.Sample;
|
|
using osu.Framework.Bindables;
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
|
using osu.Framework.Graphics;
|
|
using osu.Framework.Graphics.Containers;
|
|
using osu.Framework.Graphics.UserInterface;
|
|
using osu.Framework.Input.Events;
|
|
using osu.Framework.Localisation;
|
|
using osu.Game.Overlays;
|
|
|
|
namespace osu.Game.Graphics.UserInterfaceV2
|
|
{
|
|
public partial class FormCheckBox : CompositeDrawable, IHasCurrentValue<bool>, IFormControl
|
|
{
|
|
public Bindable<bool> Current
|
|
{
|
|
get => current.Current;
|
|
set => current.Current = value;
|
|
}
|
|
|
|
private readonly BindableWithCurrent<bool> current = new BindableWithCurrent<bool>();
|
|
|
|
/// <summary>
|
|
/// Caption describing this slider bar, displayed on top of the controls.
|
|
/// </summary>
|
|
public LocalisableString Caption { get; init; }
|
|
|
|
/// <summary>
|
|
/// Hint text containing an extended description of this slider bar, displayed in a tooltip when hovering the caption.
|
|
/// </summary>
|
|
public LocalisableString HintText { get; init; }
|
|
|
|
private FormControlBackground background = null!;
|
|
private FormFieldCaption caption = null!;
|
|
|
|
private Sample? sampleChecked;
|
|
private Sample? sampleUnchecked;
|
|
private Sample? sampleDisabled;
|
|
|
|
[Resolved]
|
|
private OverlayColourProvider colourProvider { get; set; } = null!;
|
|
|
|
[BackgroundDependencyLoader]
|
|
private void load(AudioManager audio)
|
|
{
|
|
RelativeSizeAxes = Axes.X;
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
InternalChildren = new Drawable[]
|
|
{
|
|
background = new FormControlBackground(),
|
|
new Container
|
|
{
|
|
RelativeSizeAxes = Axes.X,
|
|
AutoSizeAxes = Axes.Y,
|
|
Padding = new MarginPadding(9),
|
|
Children = new Drawable[]
|
|
{
|
|
new Container
|
|
{
|
|
Anchor = Anchor.CentreLeft,
|
|
Origin = Anchor.CentreLeft,
|
|
RelativeSizeAxes = Axes.X,
|
|
AutoSizeAxes = Axes.Y,
|
|
Padding = new MarginPadding { Right = SwitchButton.WIDTH + 5 },
|
|
Children = new Drawable[]
|
|
{
|
|
caption = new FormFieldCaption
|
|
{
|
|
Caption = Caption,
|
|
TooltipText = HintText,
|
|
},
|
|
},
|
|
},
|
|
new SwitchButton
|
|
{
|
|
Anchor = Anchor.CentreRight,
|
|
Origin = Anchor.CentreRight,
|
|
Current = Current,
|
|
},
|
|
},
|
|
},
|
|
};
|
|
sampleChecked = audio.Samples.Get(@"UI/check-on");
|
|
sampleUnchecked = audio.Samples.Get(@"UI/check-off");
|
|
sampleDisabled = audio.Samples.Get(@"UI/default-select-disabled");
|
|
}
|
|
|
|
protected override void LoadComplete()
|
|
{
|
|
base.LoadComplete();
|
|
|
|
current.BindValueChanged(_ =>
|
|
{
|
|
updateState();
|
|
playSamples();
|
|
background.Flash();
|
|
|
|
ValueChanged?.Invoke();
|
|
});
|
|
current.BindDisabledChanged(_ => updateState(), true);
|
|
}
|
|
|
|
private void playSamples()
|
|
{
|
|
if (Current.Value)
|
|
sampleChecked?.Play();
|
|
else
|
|
sampleUnchecked?.Play();
|
|
}
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
|
{
|
|
updateState();
|
|
return true;
|
|
}
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
|
{
|
|
base.OnHoverLost(e);
|
|
updateState();
|
|
}
|
|
|
|
protected override bool OnClick(ClickEvent e)
|
|
{
|
|
if (!Current.Disabled)
|
|
Current.Value = !Current.Value;
|
|
else
|
|
sampleDisabled?.Play();
|
|
|
|
return true;
|
|
}
|
|
|
|
private void updateState()
|
|
{
|
|
caption.Colour = Current.Disabled ? colourProvider.Background1 : colourProvider.Content2;
|
|
background.StyleHovered = IsHovered;
|
|
background.StyleDisabled = IsDisabled;
|
|
}
|
|
|
|
public IEnumerable<LocalisableString> FilterTerms => Caption.Yield();
|
|
|
|
public event Action? ValueChanged;
|
|
|
|
public bool IsDefault => Current.IsDefault;
|
|
|
|
public void SetDefault() => Current.SetDefault();
|
|
|
|
public bool IsDisabled => Current.Disabled;
|
|
}
|
|
}
|