1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 11:20:04 +08:00

Remove usage of key repeat helper method

This commit is contained in:
Dean Herbert 2021-11-18 12:38:20 +09:00
parent d7b178ea37
commit 66c307e0ee
6 changed files with 10 additions and 151 deletions

View File

@ -1,11 +1,8 @@
// 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.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Bindings;
using osu.Framework.Threading;
using osu.Game.Screens.Play.HUD;
using osu.Game.Skinning;
using osuTK;
@ -14,30 +11,6 @@ namespace osu.Game.Extensions
{
public static class DrawableExtensions
{
public const double REPEAT_INTERVAL = 70;
public const double INITIAL_DELAY = 250;
/// <summary>
/// Helper method that is used while <see cref="IKeyBindingHandler"/> doesn't support repetitions of <see cref="IKeyBindingHandler{T}.OnPressed"/>.
/// Simulates repetitions by continually invoking a delegate according to the default key repeat rate.
/// </summary>
/// <remarks>
/// The returned delegate can be cancelled to stop repeat events from firing (usually in <see cref="IKeyBindingHandler{T}.OnReleased"/>).
/// </remarks>
/// <param name="handler">The <see cref="IKeyBindingHandler{T}"/> which is handling the repeat.</param>
/// <param name="scheduler">The <see cref="Scheduler"/> to schedule repetitions on.</param>
/// <param name="action">The <see cref="Action"/> to be invoked once immediately and with every repetition.</param>
/// <param name="initialRepeatDelay">The delay imposed on the first repeat. Defaults to <see cref="INITIAL_DELAY"/>.</param>
/// <returns>A <see cref="ScheduledDelegate"/> which can be cancelled to stop the repeat events from firing.</returns>
public static ScheduledDelegate BeginKeyRepeat(this IKeyBindingHandler handler, Scheduler scheduler, Action action, double initialRepeatDelay = INITIAL_DELAY)
{
action();
ScheduledDelegate repeatDelegate = new ScheduledDelegate(action, handler.Time.Current + initialRepeatDelay, REPEAT_INTERVAL);
scheduler.Add(repeatDelegate);
return repeatDelegate;
}
/// <summary>
/// Shakes this drawable.
/// </summary>

View File

@ -6,8 +6,6 @@ using osu.Framework.Graphics.Containers;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Threading;
using osu.Game.Extensions;
using osu.Game.Input.Bindings;
namespace osu.Game.Overlays.Volume
@ -17,18 +15,12 @@ namespace osu.Game.Overlays.Volume
public Func<GlobalAction, bool> ActionRequested;
public Func<GlobalAction, float, bool, bool> ScrollActionRequested;
private ScheduledDelegate keyRepeat;
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
switch (e.Action)
{
case GlobalAction.DecreaseVolume:
case GlobalAction.IncreaseVolume:
keyRepeat?.Cancel();
keyRepeat = this.BeginKeyRepeat(Scheduler, () => ActionRequested?.Invoke(e.Action), 150);
return true;
case GlobalAction.ToggleMute:
case GlobalAction.NextVolumeMeter:
case GlobalAction.PreviousVolumeMeter:
@ -41,7 +33,6 @@ namespace osu.Game.Overlays.Volume
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
keyRepeat?.Cancel();
}
protected override bool OnScroll(ScrollEvent e)

View File

@ -14,7 +14,6 @@ using osu.Framework.Threading;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Configuration;
using osu.Game.Extensions;
using osu.Game.Input.Bindings;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Objects;
@ -204,11 +203,11 @@ namespace osu.Game.Rulesets.UI.Scrolling
switch (e.Action)
{
case GlobalAction.IncreaseScrollSpeed:
scheduleScrollSpeedAdjustment(1);
AdjustScrollSpeed(1);
return true;
case GlobalAction.DecreaseScrollSpeed:
scheduleScrollSpeedAdjustment(-1);
AdjustScrollSpeed(-1);
return true;
}
@ -223,12 +222,6 @@ namespace osu.Game.Rulesets.UI.Scrolling
scheduledScrollSpeedAdjustment = null;
}
private void scheduleScrollSpeedAdjustment(int amount)
{
scheduledScrollSpeedAdjustment?.Cancel();
scheduledScrollSpeedAdjustment = this.BeginKeyRepeat(Scheduler, () => AdjustScrollSpeed(amount));
}
private class LocalScrollingInfo : IScrollingInfo
{
public IBindable<ScrollingDirection> Direction { get; } = new Bindable<ScrollingDirection>();

View File

@ -12,7 +12,6 @@ using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Threading;
using osu.Game.Extensions;
using osu.Game.Graphics.Cursor;
using osu.Game.Input.Bindings;
@ -146,11 +145,11 @@ namespace osu.Game.Screens.OnlinePlay.Lounge.Components
switch (e.Action)
{
case GlobalAction.SelectNext:
beginRepeatSelection(() => selectNext(1), e.Action);
selectNext(1);
return true;
case GlobalAction.SelectPrevious:
beginRepeatSelection(() => selectNext(-1), e.Action);
selectNext(-1);
return true;
}
@ -159,40 +158,6 @@ namespace osu.Game.Screens.OnlinePlay.Lounge.Components
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
switch (e.Action)
{
case GlobalAction.SelectNext:
case GlobalAction.SelectPrevious:
endRepeatSelection(e.Action);
break;
}
}
private ScheduledDelegate repeatDelegate;
private object lastRepeatSource;
/// <summary>
/// Begin repeating the specified selection action.
/// </summary>
/// <param name="action">The action to perform.</param>
/// <param name="source">The source of the action. Used in conjunction with <see cref="endRepeatSelection"/> to only cancel the correct action (most recently pressed key).</param>
private void beginRepeatSelection(Action action, object source)
{
endRepeatSelection();
lastRepeatSource = source;
repeatDelegate = this.BeginKeyRepeat(Scheduler, action);
}
private void endRepeatSelection(object source = null)
{
// only the most recent source should be able to cancel the current action.
if (source != null && !EqualityComparer<object>.Default.Equals(lastRepeatSource, source))
return;
repeatDelegate?.Cancel();
repeatDelegate = null;
lastRepeatSource = null;
}
private void selectNext(int direction)

View File

@ -7,9 +7,7 @@ using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Threading;
using osu.Game.Beatmaps;
using osu.Game.Extensions;
using osu.Game.Input.Bindings;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Objects;
@ -48,8 +46,6 @@ namespace osu.Game.Screens.Play
protected override ResultsScreen CreateResults(ScoreInfo score) => new SoloResultsScreen(score, false);
private ScheduledDelegate keyboardSeekDelegate;
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
const double keyboard_seek_amount = 5000;
@ -57,13 +53,11 @@ namespace osu.Game.Screens.Play
switch (e.Action)
{
case GlobalAction.SeekReplayBackward:
keyboardSeekDelegate?.Cancel();
keyboardSeekDelegate = this.BeginKeyRepeat(Scheduler, () => keyboardSeek(-1));
keyboardSeek(-1);
return true;
case GlobalAction.SeekReplayForward:
keyboardSeekDelegate?.Cancel();
keyboardSeekDelegate = this.BeginKeyRepeat(Scheduler, () => keyboardSeek(1));
keyboardSeek(1);
return true;
case GlobalAction.TogglePauseReplay:
@ -86,13 +80,6 @@ namespace osu.Game.Screens.Play
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
switch (e.Action)
{
case GlobalAction.SeekReplayBackward:
case GlobalAction.SeekReplayForward:
keyboardSeekDelegate?.Cancel();
break;
}
}
}
}

View File

@ -18,7 +18,6 @@ using osu.Framework.Threading;
using osu.Framework.Utils;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Extensions;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Cursor;
using osu.Game.Input.Bindings;
@ -479,42 +478,27 @@ namespace osu.Game.Screens.Select
switch (e.Key)
{
case Key.Left:
if (!e.Repeat)
beginRepeatSelection(() => SelectNext(-1), e.Key);
SelectNext(-1);
return true;
case Key.Right:
if (!e.Repeat)
beginRepeatSelection(() => SelectNext(), e.Key);
SelectNext();
return true;
}
return false;
}
protected override void OnKeyUp(KeyUpEvent e)
{
switch (e.Key)
{
case Key.Left:
case Key.Right:
endRepeatSelection(e.Key);
break;
}
base.OnKeyUp(e);
}
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
switch (e.Action)
{
case GlobalAction.SelectNext:
beginRepeatSelection(() => SelectNext(1, false), e.Action);
SelectNext(1, false);
return true;
case GlobalAction.SelectPrevious:
beginRepeatSelection(() => SelectNext(-1, false), e.Action);
SelectNext(-1, false);
return true;
}
@ -523,40 +507,6 @@ namespace osu.Game.Screens.Select
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
switch (e.Action)
{
case GlobalAction.SelectNext:
case GlobalAction.SelectPrevious:
endRepeatSelection(e.Action);
break;
}
}
private ScheduledDelegate repeatDelegate;
private object lastRepeatSource;
/// <summary>
/// Begin repeating the specified selection action.
/// </summary>
/// <param name="action">The action to perform.</param>
/// <param name="source">The source of the action. Used in conjunction with <see cref="endRepeatSelection"/> to only cancel the correct action (most recently pressed key).</param>
private void beginRepeatSelection(Action action, object source)
{
endRepeatSelection();
lastRepeatSource = source;
repeatDelegate = this.BeginKeyRepeat(Scheduler, action);
}
private void endRepeatSelection(object source = null)
{
// only the most recent source should be able to cancel the current action.
if (source != null && !EqualityComparer<object>.Default.Equals(lastRepeatSource, source))
return;
repeatDelegate?.Cancel();
repeatDelegate = null;
lastRepeatSource = null;
}
#endregion