Handle other button clicks in HoverClickSounds

As suggested in review, remove previously introduced HoverMouseUpSounds
and instead change effect playing logic in HoverClickSounds by moving it
out of OnClick() to OnMouseUp().

Users of the class can either use the existing constructor to play
the effect only on left click or use the newly introduced constructor
with the MouseButton[] parameter to specify which button clicks should
trigger the sound.
This commit is contained in:
Bartłomiej Dach 2019-08-31 20:16:16 +02:00
parent a1c72db5f6
commit 658e0edc3e
3 changed files with 29 additions and 48 deletions

View File

@ -1,11 +1,13 @@
// 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 System;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Extensions;
using osu.Framework.Input.Events;
using osuTK.Input;
namespace osu.Game.Graphics.UserInterface
{
@ -16,16 +18,38 @@ namespace osu.Game.Graphics.UserInterface
public class HoverClickSounds : HoverSounds
{
private SampleChannel sampleClick;
private readonly MouseButton[] buttons;
/// <summary>
/// Creates an instance that adds sounds on hover and left click only.
/// </summary>
/// <param name="sampleSet">Set of click samples to play.</param>
public HoverClickSounds(HoverSampleSet sampleSet = HoverSampleSet.Normal)
: base(sampleSet)
: this(new[] { MouseButton.Left }, sampleSet)
{
}
protected override bool OnClick(ClickEvent e)
/// <summary>
/// Creates an instance that adds sounds on hover and on click for any of the buttons specified.
/// </summary>
/// <param name="buttons">Array of button codes which should trigger the click sound.</param>
/// <param name="sampleSet">Set of click samples to play.</param>
public HoverClickSounds(MouseButton[] buttons, HoverSampleSet sampleSet = HoverSampleSet.Normal)
: base(sampleSet)
{
sampleClick?.Play();
return base.OnClick(e);
this.buttons = buttons;
}
protected override bool OnMouseUp(MouseUpEvent e)
{
var index = Array.IndexOf(buttons, e.Button);
bool shouldPlayEffect = index > -1 && index < buttons.Length;
// examine the button pressed first for short-circuiting
// in most usages it is more likely that another button was pressed than that the cursor left the drawable bounds
if (shouldPlayEffect && Contains(e.ScreenSpaceMousePosition))
sampleClick?.Play();
return base.OnMouseUp(e);
}
[BackgroundDependencyLoader]

View File

@ -1,42 +0,0 @@
// 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 System.Collections.Generic;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Extensions;
using osu.Framework.Input.Events;
using osuTK.Input;
namespace osu.Game.Graphics.UserInterface
{
/// <summary>
/// Adds hover sounds to a drawable, as well as click sounds upon MouseUp events for selected mouse buttons.
/// Intended to be used for controls that can respond to clicks of buttons other than the left mouse button in place of <see cref="HoverClickSounds" />.
/// </summary>
public class HoverMouseUpSounds : HoverSounds
{
private SampleChannel sampleClick;
private readonly List<MouseButton> buttons;
public HoverMouseUpSounds(List<MouseButton> buttons, HoverSampleSet sampleSet = HoverSampleSet.Normal)
: base(sampleSet)
{
this.buttons = buttons;
}
protected override bool OnMouseUp(MouseUpEvent e)
{
if (Contains(e.ScreenSpaceMousePosition) && buttons.Contains(e.Button))
sampleClick?.Play();
return base.OnMouseUp(e);
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
sampleClick = audio.Samples.Get($@"UI/generic-select{SampleSet.GetDescription()}");
}
}
}

View File

@ -11,7 +11,6 @@
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Input.Events;
@ -284,7 +283,7 @@ public ModButton(Mod mod)
Anchor = Anchor.TopCentre,
Font = OsuFont.GetFont(size: 18)
},
new HoverMouseUpSounds(new List<MouseButton> { MouseButton.Left, MouseButton.Right })
new HoverClickSounds(new[] { MouseButton.Left, MouseButton.Right })
};
Mod = mod;