2022-03-27 04:33:52 +08: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.
#nullable enable
using System ;
using osu.Framework.Allocation ;
using osu.Framework.Audio ;
using osu.Framework.Audio.Sample ;
using osu.Framework.Bindables ;
namespace osu.Game.Graphics.UserInterface
{
2022-04-26 16:31:55 +08:00
public class ShearedToggleButton : ShearedButton
2022-03-27 04:33:52 +08:00
{
2022-06-03 21:22:32 +08:00
private Sample ? sampleClick ;
2022-03-27 04:33:52 +08:00
private Sample ? sampleOff ;
private Sample ? sampleOn ;
2022-04-26 16:31:55 +08:00
/// <summary>
/// Whether this button is currently toggled to an active state.
/// </summary>
public BindableBool Active { get ; } = new BindableBool ( ) ;
2022-03-27 04:33:52 +08:00
2022-04-01 04:19:08 +08:00
/// <summary>
/// Creates a new <see cref="ShearedToggleButton"/>
/// </summary>
/// <param name="width">
/// The width of the button.
/// <list type="bullet">
/// <item>If a non-<see langword="null"/> value is provided, this button will have a fixed width equal to the provided value.</item>
/// <item>If a <see langword="null"/> value is provided (or the argument is omitted entirely), the button will autosize in width to fit the text.</item>
/// </list>
/// </param>
public ShearedToggleButton ( float? width = null )
2022-04-26 16:31:55 +08:00
: base ( width )
2022-03-27 04:33:52 +08:00
{
}
[BackgroundDependencyLoader]
private void load ( AudioManager audio )
{
2022-06-03 21:22:32 +08:00
sampleClick = audio . Samples . Get ( @"UI/default-select" ) ;
sampleOn = audio . Samples . Get ( @"UI/dropdown-open" ) ;
sampleOff = audio . Samples . Get ( @"UI/dropdown-close" ) ;
2022-03-27 04:33:52 +08:00
}
protected override HoverSounds CreateHoverSounds ( HoverSampleSet sampleSet ) = > new HoverSounds ( sampleSet ) ;
protected override void LoadComplete ( )
{
2022-04-26 17:21:46 +08:00
Active . BindDisabledChanged ( disabled = > Action = disabled ? ( Action ? ) null : Active . Toggle , true ) ;
2022-03-27 04:33:52 +08:00
Active . BindValueChanged ( _ = >
{
2022-04-26 16:31:55 +08:00
updateActiveState ( ) ;
2022-03-27 04:33:52 +08:00
playSample ( ) ;
} ) ;
2022-04-26 16:31:55 +08:00
updateActiveState ( ) ;
base . LoadComplete ( ) ;
2022-03-27 04:33:52 +08:00
}
2022-04-26 16:31:55 +08:00
private void updateActiveState ( )
2022-03-27 04:33:52 +08:00
{
2022-04-26 16:31:55 +08:00
DarkerColour = Active . Value ? ColourProvider . Highlight1 : ColourProvider . Background3 ;
LighterColour = Active . Value ? ColourProvider . Colour0 : ColourProvider . Background1 ;
TextColour = Active . Value ? ColourProvider . Background6 : ColourProvider . Content1 ;
2022-03-27 04:33:52 +08:00
}
private void playSample ( )
{
2022-06-03 21:22:32 +08:00
sampleClick ? . Play ( ) ;
2022-03-27 04:33:52 +08:00
if ( Active . Value )
sampleOn ? . Play ( ) ;
else
sampleOff ? . Play ( ) ;
}
}
}