osu/osu.Game/Overlays/Toolbar/ToolbarOverlayToggleButton.cs

74 lines
2.1 KiB
C#
Raw Normal View History

// 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.
2018-04-13 09:19:50 +00:00
using osu.Framework.Bindables;
2018-04-13 09:19:50 +00:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics;
namespace osu.Game.Overlays.Toolbar
{
public class ToolbarOverlayToggleButton : ToolbarButton
{
private readonly Box stateBackground;
private OverlayContainer stateContainer;
private readonly Bindable<Visibility> overlayState = new Bindable<Visibility>();
2018-04-13 09:19:50 +00:00
public OverlayContainer StateContainer
{
get => stateContainer;
2018-04-13 09:19:50 +00:00
set
{
stateContainer = value;
2019-04-01 03:16:05 +00:00
overlayState.UnbindBindings();
2018-04-13 09:19:50 +00:00
if (stateContainer != null)
{
Action = stateContainer.ToggleVisibility;
overlayState.BindTo(stateContainer.State);
2018-04-13 09:19:50 +00:00
}
if (stateContainer is INamedOverlayComponent named)
{
TooltipMain = named.Title;
TooltipSub = named.Description;
2020-09-03 07:28:53 +00:00
SetIcon(named.IconTexture);
}
2018-04-13 09:19:50 +00:00
}
}
public ToolbarOverlayToggleButton()
{
Add(stateBackground = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = OsuColour.Gray(150).Opacity(180),
2019-08-21 04:29:50 +00:00
Blending = BlendingParameters.Additive,
2018-04-13 09:19:50 +00:00
Depth = 2,
Alpha = 0,
});
overlayState.ValueChanged += stateChanged;
2018-04-13 09:19:50 +00:00
}
private void stateChanged(ValueChangedEvent<Visibility> state)
2018-04-13 09:19:50 +00:00
{
switch (state.NewValue)
2018-04-13 09:19:50 +00:00
{
case Visibility.Hidden:
stateBackground.FadeOut(200);
break;
2019-04-01 03:16:05 +00:00
2018-04-13 09:19:50 +00:00
case Visibility.Visible:
stateBackground.FadeIn(200);
break;
}
}
}
}