2021-05-14 23:01:17 +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.
using osu.Framework.Allocation ;
using osu.Framework.Bindables ;
using osuTK.Graphics ;
using osu.Framework.Extensions.Color4Extensions ;
using osu.Framework.Graphics ;
2021-08-16 17:40:34 +08:00
using osu.Framework.Graphics.Colour ;
2021-05-14 23:01:17 +08:00
using osu.Framework.Graphics.Cursor ;
using osu.Framework.Graphics.Effects ;
2021-05-15 09:24:08 +08:00
using osu.Framework.Graphics.UserInterface ;
2021-05-14 23:01:17 +08:00
using osu.Framework.Input.Events ;
2021-06-26 01:10:04 +08:00
using osu.Framework.Localisation ;
2021-05-14 23:01:17 +08:00
using osu.Game.Graphics ;
2021-05-24 19:34:47 +08:00
using osu.Game.Graphics.UserInterface ;
2021-05-14 23:01:17 +08:00
namespace osu.Game.Overlays
{
2021-05-24 19:34:47 +08:00
public class RestoreDefaultValueButton < T > : OsuButton , IHasTooltip , IHasCurrentValue < T >
2021-05-14 23:01:17 +08:00
{
public override bool IsPresent = > base . IsPresent | | Scheduler . HasPendingTasks ;
2021-05-26 17:03:15 +08:00
// this is done to ensure a click on this button doesn't trigger focus on a parent element which contains the button.
public override bool AcceptsFocus = > true ;
2021-07-09 05:39:09 +08:00
// this is intentionally not using BindableWithCurrent, as it can use the wrong IsDefault implementation when passed a BindableNumber.
// using GetBoundCopy() ensures that the received bindable is of the exact same type as the source bindable and uses the proper IsDefault implementation.
2021-07-03 03:30:26 +08:00
private Bindable < T > current ;
2021-05-15 09:24:08 +08:00
public Bindable < T > Current
2021-05-14 23:01:17 +08:00
{
2021-07-03 03:30:26 +08:00
get = > current ;
set
{
2021-07-03 04:24:51 +08:00
current ? . UnbindAll ( ) ;
current = value . GetBoundCopy ( ) ;
2021-07-03 03:30:26 +08:00
2021-07-09 05:48:48 +08:00
current . ValueChanged + = _ = > UpdateState ( ) ;
current . DefaultChanged + = _ = > UpdateState ( ) ;
current . DisabledChanged + = _ = > UpdateState ( ) ;
2021-08-16 17:40:34 +08:00
if ( IsLoaded )
UpdateState ( ) ;
2021-07-03 03:30:26 +08:00
}
2021-05-14 23:01:17 +08:00
}
private bool hovering ;
public RestoreDefaultValueButton ( )
{
2021-05-24 19:34:47 +08:00
Height = 1 ;
2021-05-14 23:01:17 +08:00
RelativeSizeAxes = Axes . Y ;
Width = SettingsPanel . CONTENT_MARGINS ;
}
[BackgroundDependencyLoader]
private void load ( OsuColour colour )
{
2021-05-24 19:34:47 +08:00
BackgroundColour = colour . Yellow ;
Content . Width = 0.33f ;
Content . CornerRadius = 3 ;
Content . EdgeEffect = new EdgeEffectParameters
2021-05-14 23:01:17 +08:00
{
2021-08-17 11:48:30 +08:00
Colour = BackgroundColour . Opacity ( 0.1f ) ,
2021-05-24 19:34:47 +08:00
Type = EdgeEffectType . Glow ,
Radius = 2 ,
2021-05-14 23:01:17 +08:00
} ;
2021-05-24 19:34:47 +08:00
Padding = new MarginPadding { Vertical = 1.5f } ;
Alpha = 0f ;
2021-05-24 20:08:34 +08:00
Action + = ( ) = >
{
2021-07-03 03:30:26 +08:00
if ( ! current . Disabled )
current . SetDefault ( ) ;
2021-05-24 20:08:34 +08:00
} ;
2021-05-14 23:01:17 +08:00
}
2021-07-10 22:57:52 +08:00
protected override void LoadComplete ( )
{
base . LoadComplete ( ) ;
2021-08-16 17:40:34 +08:00
// avoid unnecessary transforms on first display.
Alpha = currentAlpha ;
2021-08-17 11:48:30 +08:00
Background . Colour = currentColour ;
2021-07-10 22:57:52 +08:00
}
2021-06-26 01:10:04 +08:00
public LocalisableString TooltipText = > "revert to default" ;
2021-05-14 23:01:17 +08:00
protected override bool OnHover ( HoverEvent e )
{
hovering = true ;
UpdateState ( ) ;
return false ;
}
protected override void OnHoverLost ( HoverLostEvent e )
{
hovering = false ;
UpdateState ( ) ;
}
public void UpdateState ( ) = > Scheduler . AddOnce ( updateState ) ;
2021-08-16 17:40:34 +08:00
private float currentAlpha = > current . IsDefault ? 0f : hovering & & ! current . Disabled ? 1f : 0.65f ;
2021-08-17 11:48:30 +08:00
private ColourInfo currentColour = > current . Disabled ? Color4 . Gray : BackgroundColour ;
2021-08-16 17:40:34 +08:00
2021-05-14 23:01:17 +08:00
private void updateState ( )
{
2021-07-03 02:06:57 +08:00
if ( current = = null )
2021-05-14 23:01:17 +08:00
return ;
2021-08-16 17:40:34 +08:00
this . FadeTo ( currentAlpha , 200 , Easing . OutQuint ) ;
2021-08-17 11:48:30 +08:00
Background . FadeColour ( currentColour , 200 , Easing . OutQuint ) ;
2021-05-14 23:01:17 +08:00
}
}
2021-05-18 02:44:47 +08:00
}