// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using osu.Framework.Bindables; using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Graphics.Sprites; using osuTK.Graphics; using osu.Framework.Graphics.Shapes; using osu.Framework.Input.Events; using osu.Framework.Localisation; namespace osu.Framework.Graphics.UserInterface { public class BasicButton : Button { public LocalisableString Text { get => SpriteText?.Text ?? default; set { if (SpriteText != null) SpriteText.Text = value; } } public Color4 BackgroundColour { get => Background.Colour; set => Background.FadeColour(value); } private Color4? flashColour; /// /// The colour the background will flash with when this button is clicked. /// public Color4 FlashColour { get => flashColour ?? BackgroundColour; set => flashColour = value; } /// /// The additive colour that is applied to the background when hovered. /// public Color4 HoverColour { get => Hover.Colour; set => Hover.FadeColour(value); } private Color4 disabledColour = Color4.Gray; /// /// The additive colour that is applied to this button when disabled. /// public Color4 DisabledColour { get => disabledColour; set { if (disabledColour == value) return; disabledColour = value; Enabled.TriggerChange(); } } /// /// The duration of the transition when hovering. /// public double HoverFadeDuration { get; set; } = 200; /// /// The duration of the flash when this button is clicked. /// public double FlashDuration { get; set; } = 200; /// /// The duration of the transition when toggling the Enabled state. /// public double DisabledFadeDuration { get; set; } = 200; protected Box Hover; protected Box Background; protected SpriteText SpriteText; public BasicButton() { AddRange(new Drawable[] { Background = new Box { Anchor = Anchor.Centre, Origin = Anchor.Centre, RelativeSizeAxes = Axes.Both, Colour = FrameworkColour.BlueGreen }, Hover = new Box { Alpha = 0, Anchor = Anchor.Centre, Origin = Anchor.Centre, RelativeSizeAxes = Axes.Both, Colour = Color4.White.Opacity(.1f), Blending = BlendingParameters.Additive }, SpriteText = CreateText() }); Enabled.BindValueChanged(enabledChanged, true); } protected virtual SpriteText CreateText() => new SpriteText { Depth = -1, Origin = Anchor.Centre, Anchor = Anchor.Centre, Font = FrameworkFont.Regular, Colour = FrameworkColour.Yellow }; protected override bool OnClick(ClickEvent e) { if (Enabled.Value) Background.FlashColour(FlashColour, FlashDuration); return base.OnClick(e); } protected override bool OnHover(HoverEvent e) { if (Enabled.Value) Hover.FadeIn(HoverFadeDuration); return base.OnHover(e); } protected override void OnHoverLost(HoverLostEvent e) { base.OnHoverLost(e); Hover.FadeOut(HoverFadeDuration); } private void enabledChanged(ValueChangedEvent e) { this.FadeColour(e.NewValue ? Color4.White : DisabledColour, DisabledFadeDuration, Easing.OutQuint); } } }