Skip to content

Commit

Permalink
Tidy up GlobalActionContainer a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy committed Oct 13, 2023
1 parent c2e92cb commit 9289c47
Showing 1 changed file with 60 additions and 55 deletions.
115 changes: 60 additions & 55 deletions osu.Game/Input/Bindings/GlobalActionContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace osu.Game.Input.Bindings
{
public partial class GlobalActionContainer : DatabasedKeyBindingContainer<GlobalAction>, IHandleGlobalKeyboardInput, IKeyBindingHandler<GlobalAction>
{
protected override bool Prioritised => true;

private readonly IKeyBindingHandler<GlobalAction>? handler;

public GlobalActionContainer(OsuGameBase? game)
Expand All @@ -23,22 +25,62 @@ public GlobalActionContainer(OsuGameBase? game)
handler = h;
}

protected override bool Prioritised => true;

// IMPORTANT: Take care when changing order of the items in the enumerable.
// It is used to decide the order of precedence, with the earlier items having higher precedence.
public override IEnumerable<IKeyBinding> DefaultKeyBindings => GlobalKeyBindings
.Concat(EditorKeyBindings)
.Concat(InGameKeyBindings)
.Concat(ReplayKeyBindings)
.Concat(SongSelectKeyBindings)
.Concat(AudioControlKeyBindings)
/// <summary>
/// All default key bindings across all categories, ordered with highest priority first.
/// </summary>
/// <remarks>
/// IMPORTANT: Take care when changing order of the items in the enumerable.
/// It is used to decide the order of precedence, with the earlier items having higher precedence.
/// </remarks>
public override IEnumerable<IKeyBinding> DefaultKeyBindings => globalKeyBindings
.Concat(editorKeyBindings)
.Concat(inGameKeyBindings)
.Concat(replayKeyBindings)
.Concat(songSelectKeyBindings)
.Concat(audioControlKeyBindings)
// Overlay bindings may conflict with more local cases like the editor so they are checked last.
// It has generally been agreed on that local screens like the editor should have priority,
// based on such usages potentially requiring a lot more key bindings that may be "shared" with global ones.
.Concat(OverlayKeyBindings);
.Concat(overlayKeyBindings);

public static IEnumerable<KeyBinding> GetDefaultBindingsFor(GlobalActionCategory category)
{
switch (category)
{
case GlobalActionCategory.General:
return globalKeyBindings;

case GlobalActionCategory.Editor:
return editorKeyBindings;

case GlobalActionCategory.InGame:
return inGameKeyBindings;

case GlobalActionCategory.Replay:
return replayKeyBindings;

case GlobalActionCategory.SongSelect:
return songSelectKeyBindings;

case GlobalActionCategory.AudioControl:
return audioControlKeyBindings;

case GlobalActionCategory.Overlays:
return overlayKeyBindings;

default:
throw new ArgumentOutOfRangeException(nameof(category), category, $"Unexpected {nameof(GlobalActionCategory)}");
}
}

public static IEnumerable<GlobalAction> GetGlobalActionsFor(GlobalActionCategory category)
=> GetDefaultBindingsFor(category).Select(binding => binding.Action).Cast<GlobalAction>().Distinct();

public bool OnPressed(KeyBindingPressEvent<GlobalAction> e) => handler?.OnPressed(e) == true;

public static IEnumerable<KeyBinding> GlobalKeyBindings => new[]
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e) => handler?.OnReleased(e);

private static IEnumerable<KeyBinding> globalKeyBindings => new[]
{
new KeyBinding(InputKey.Up, GlobalAction.SelectPrevious),
new KeyBinding(InputKey.Down, GlobalAction.SelectNext),
Expand Down Expand Up @@ -68,7 +110,7 @@ public GlobalActionContainer(OsuGameBase? game)
new KeyBinding(InputKey.F12, GlobalAction.TakeScreenshot),
};

public static IEnumerable<KeyBinding> OverlayKeyBindings => new[]
private static IEnumerable<KeyBinding> overlayKeyBindings => new[]
{
new KeyBinding(InputKey.F8, GlobalAction.ToggleChat),
new KeyBinding(InputKey.F6, GlobalAction.ToggleNowPlaying),
Expand All @@ -78,7 +120,7 @@ public GlobalActionContainer(OsuGameBase? game)
new KeyBinding(new[] { InputKey.Control, InputKey.N }, GlobalAction.ToggleNotifications),
};

public static IEnumerable<KeyBinding> EditorKeyBindings => new[]
private static IEnumerable<KeyBinding> editorKeyBindings => new[]
{
new KeyBinding(new[] { InputKey.F1 }, GlobalAction.EditorComposeMode),
new KeyBinding(new[] { InputKey.F2 }, GlobalAction.EditorDesignMode),
Expand All @@ -102,7 +144,7 @@ public GlobalActionContainer(OsuGameBase? game)
new KeyBinding(new[] { InputKey.Control, InputKey.R }, GlobalAction.EditorToggleRotateControl),
};

public static IEnumerable<KeyBinding> InGameKeyBindings => new[]
private static IEnumerable<KeyBinding> inGameKeyBindings => new[]
{
new KeyBinding(InputKey.Space, GlobalAction.SkipCutscene),
new KeyBinding(InputKey.ExtraMouseButton2, GlobalAction.SkipCutscene),
Expand All @@ -119,7 +161,7 @@ public GlobalActionContainer(OsuGameBase? game)
new KeyBinding(InputKey.F2, GlobalAction.ExportReplay),
};

public static IEnumerable<KeyBinding> ReplayKeyBindings => new[]
private static IEnumerable<KeyBinding> replayKeyBindings => new[]
{
new KeyBinding(InputKey.Space, GlobalAction.TogglePauseReplay),
new KeyBinding(InputKey.MouseMiddle, GlobalAction.TogglePauseReplay),
Expand All @@ -128,7 +170,7 @@ public GlobalActionContainer(OsuGameBase? game)
new KeyBinding(new[] { InputKey.Control, InputKey.H }, GlobalAction.ToggleReplaySettings),
};

public static IEnumerable<KeyBinding> SongSelectKeyBindings => new[]
private static IEnumerable<KeyBinding> songSelectKeyBindings => new[]
{
new KeyBinding(InputKey.F1, GlobalAction.ToggleModSelection),
new KeyBinding(InputKey.F2, GlobalAction.SelectNextRandom),
Expand All @@ -137,7 +179,7 @@ public GlobalActionContainer(OsuGameBase? game)
new KeyBinding(InputKey.BackSpace, GlobalAction.DeselectAllMods),
};

public static IEnumerable<KeyBinding> AudioControlKeyBindings => new[]
private static IEnumerable<KeyBinding> audioControlKeyBindings => new[]
{
new KeyBinding(new[] { InputKey.Alt, InputKey.Up }, GlobalAction.IncreaseVolume),
new KeyBinding(new[] { InputKey.Alt, InputKey.Down }, GlobalAction.DecreaseVolume),
Expand All @@ -154,43 +196,6 @@ public GlobalActionContainer(OsuGameBase? game)
new KeyBinding(InputKey.PlayPause, GlobalAction.MusicPlay),
new KeyBinding(InputKey.F3, GlobalAction.MusicPlay)
};

public static IEnumerable<KeyBinding> GetDefaultBindingsFor(GlobalActionCategory category)
{
switch (category)
{
case GlobalActionCategory.General:
return GlobalKeyBindings;

case GlobalActionCategory.Editor:
return EditorKeyBindings;

case GlobalActionCategory.InGame:
return InGameKeyBindings;

case GlobalActionCategory.Replay:
return ReplayKeyBindings;

case GlobalActionCategory.SongSelect:
return SongSelectKeyBindings;

case GlobalActionCategory.AudioControl:
return AudioControlKeyBindings;

case GlobalActionCategory.Overlays:
return OverlayKeyBindings;

default:
throw new ArgumentOutOfRangeException(nameof(category), category, $"Unexpected {nameof(GlobalActionCategory)}");
}
}

public static IEnumerable<GlobalAction> GetGlobalActionsFor(GlobalActionCategory category)
=> GetDefaultBindingsFor(category).Select(binding => binding.Action).Cast<GlobalAction>().Distinct();

public bool OnPressed(KeyBindingPressEvent<GlobalAction> e) => handler?.OnPressed(e) == true;

public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e) => handler?.OnReleased(e);
}

public enum GlobalAction
Expand Down

0 comments on commit 9289c47

Please sign in to comment.