diff --git a/osu.Game.Tests/Visual/TestCaseClickableText.cs b/osu.Game.Tests/Visual/TestCaseClickableText.cs new file mode 100644 index 0000000000..8bf58d8bf4 --- /dev/null +++ b/osu.Game.Tests/Visual/TestCaseClickableText.cs @@ -0,0 +1,70 @@ +// Copyright (c) 2007-2018 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using OpenTK; +using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; +using osu.Game.Graphics.UserInterface; +using System; +using System.Collections.Generic; + +namespace osu.Game.Tests.Visual +{ + public class TestCaseClickableText : OsuTestCase + { + public override IReadOnlyList RequiredTypes => new[] { + typeof(ClickableText), + typeof(FillFlowContainer) + }; + + public TestCaseClickableText() + { + ClickableText text; + + Add(new FillFlowContainer + { + Spacing = new Vector2(10), + RelativeSizeAxes = Axes.Both, + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Children = new[] + { + new ClickableText + { + Text = "Default", + }, + new ClickableText + { + IsEnabled = false, + Text = "Disabled", + }, + new ClickableText + { + Text = "Without sounds", + IsMuted = true, + }, + new ClickableText + { + Text = "Without click sounds", + IsClickMuted = true, + }, + new ClickableText + { + Text = "Without hover sounds", + IsHoverMuted = true, + }, + text = new ClickableText + { + Text = "Disables after click (Action)", + }, + new ClickableText + { + Text = "Has tooltip", + TooltipText = "Yep", + }, + }, + }); + text.Action = () => text.IsEnabled = false; + } + } +} diff --git a/osu.Game/Graphics/UserInterface/ClickableText.cs b/osu.Game/Graphics/UserInterface/ClickableText.cs index 8d0e98d687..a97569c4c1 100644 --- a/osu.Game/Graphics/UserInterface/ClickableText.cs +++ b/osu.Game/Graphics/UserInterface/ClickableText.cs @@ -1,44 +1,81 @@ // Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE -using OpenTK.Graphics; using osu.Framework.Allocation; using osu.Framework.Audio; using osu.Framework.Audio.Sample; using osu.Framework.Graphics; using osu.Framework.Graphics.Cursor; -using osu.Framework.Graphics.Shapes; -using osu.Framework.Graphics.Sprites; using osu.Framework.Input; +using osu.Game.Graphics.Sprites; using System; namespace osu.Game.Graphics.UserInterface -{ - public class ClickableText : SpriteText, IHasTooltip +{/// +/// +/// + public class ClickableText : OsuSpriteText, IHasTooltip { private bool isEnabled; + private bool isMuted; private SampleChannel sampleHover; private SampleChannel sampleClick; + /// + /// An action that can be set to execute after click. + /// public Action Action; + /// + /// If set to true, a sound will be played on click. + /// + public bool IsClickMuted; + + /// + /// If set to true, a sound will be played on hover. + /// + public bool IsHoverMuted; + + /// + /// If disabled, no sounds will be played and wont execute. + /// True by default. + /// public bool IsEnabled { get { return isEnabled; } set { isEnabled = value; - Alpha = value ? 1 : 0.5f; + this.FadeTo(value ? 1 : 0.5f, 250); } } + /// + /// Whether to play sounds on hover and click. Automatically sets + /// and to the same value.> + /// + public bool IsMuted { + get { return isMuted; } + set + { + IsHoverMuted = value; + IsClickMuted = value; + isMuted = value; + } + } + + /// + /// A text with sounds on hover and click, + /// an action that can be set to execute on click, + /// and a tooltip. + /// public ClickableText() => isEnabled = true; public override bool HandleMouseInput => true; protected override bool OnHover(InputState state) { - if (isEnabled) + if (isEnabled && !IsHoverMuted) sampleHover?.Play(); return base.OnHover(state); } @@ -47,7 +84,8 @@ namespace osu.Game.Graphics.UserInterface { if (isEnabled) { - sampleClick?.Play(); + if (!IsClickMuted) + sampleClick?.Play(); Action?.Invoke(); } return base.OnClick(state); @@ -58,6 +96,7 @@ namespace osu.Game.Graphics.UserInterface [BackgroundDependencyLoader] private void load(AudioManager audio) { + sampleClick = audio.Sample.Get(@"UI/generic-select-soft"); sampleHover = audio.Sample.Get(@"UI/generic-hover-soft"); } }