osu/osu.Game/Screens/Edit/Components/RadioButtons/DrawableRadioButton.cs

124 lines
3.6 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 System;
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
2018-04-13 09:19:50 +00:00
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
2018-11-20 07:51:59 +00:00
using osuTK;
using osuTK.Graphics;
2018-04-13 09:19:50 +00:00
2018-11-06 09:14:46 +00:00
namespace osu.Game.Screens.Edit.Components.RadioButtons
2018-04-13 09:19:50 +00:00
{
public class DrawableRadioButton : TriangleButton
{
/// <summary>
/// Invoked when this <see cref="DrawableRadioButton"/> has been selected.
/// </summary>
public Action<RadioButton> Selected;
private Color4 defaultBackgroundColour;
private Color4 defaultBubbleColour;
private Color4 selectedBackgroundColour;
private Color4 selectedBubbleColour;
private readonly Drawable bubble;
private readonly RadioButton button;
public DrawableRadioButton(RadioButton button)
{
this.button = button;
Text = button.Text;
Action = button.Action;
RelativeSizeAxes = Axes.X;
bubble = new CircularContainer
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
RelativeSizeAxes = Axes.Both,
FillMode = FillMode.Fit,
Scale = new Vector2(0.5f),
X = 10,
Masking = true,
Blending = BlendingMode.Additive,
Child = new Box { RelativeSizeAxes = Axes.Both }
};
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
defaultBackgroundColour = colours.Gray3;
defaultBubbleColour = defaultBackgroundColour.Darken(0.5f);
selectedBackgroundColour = colours.BlueDark;
selectedBubbleColour = selectedBackgroundColour.Lighten(0.5f);
Triangles.Alpha = 0;
Content.EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Shadow,
Radius = 2,
Offset = new Vector2(0, 1),
Colour = Color4.Black.Opacity(0.5f)
};
Add(bubble);
}
protected override void LoadComplete()
{
base.LoadComplete();
2019-02-21 09:56:34 +00:00
button.Selected.ValueChanged += e =>
2018-04-13 09:19:50 +00:00
{
updateSelectionState();
2019-02-21 09:56:34 +00:00
if (e.NewValue)
2018-04-13 09:19:50 +00:00
Selected?.Invoke(button);
};
updateSelectionState();
}
private void updateSelectionState()
{
if (!IsLoaded)
return;
2019-02-21 09:56:34 +00:00
BackgroundColour = button.Selected.Value ? selectedBackgroundColour : defaultBackgroundColour;
bubble.Colour = button.Selected.Value ? selectedBubbleColour : defaultBubbleColour;
2018-04-13 09:19:50 +00:00
}
2018-10-02 03:02:47 +00:00
protected override bool OnClick(ClickEvent e)
2018-04-13 09:19:50 +00:00
{
2019-02-21 09:56:34 +00:00
if (button.Selected.Value)
2018-04-13 09:19:50 +00:00
return true;
2019-02-21 09:56:34 +00:00
if (!Enabled.Value)
2018-04-13 09:19:50 +00:00
return true;
button.Selected.Value = true;
2018-10-02 03:02:47 +00:00
return base.OnClick(e);
2018-04-13 09:19:50 +00:00
}
protected override SpriteText CreateText() => new OsuSpriteText
{
Depth = -1,
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
X = 40f
};
}
}