1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 15:17:27 +08:00

Implement component for displaying hotkeys

This commit is contained in:
Bartłomiej Dach 2024-07-18 10:21:04 +02:00
parent 3531f646f2
commit 3c6c49187a
No known key found for this signature in database
4 changed files with 192 additions and 0 deletions

View File

@ -0,0 +1,28 @@
// 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 osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
using osu.Game.Graphics.UserInterface;
using osu.Game.Input.Bindings;
namespace osu.Game.Tests.Visual.UserInterface
{
public partial class TestSceneHotkeyDisplay : ThemeComparisonTestScene
{
protected override Drawable CreateContent() => new FillFlowContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Direction = FillDirection.Vertical,
Children = new[]
{
new HotkeyDisplay { Hotkey = new Hotkey(new KeyCombination(InputKey.MouseLeft)) },
new HotkeyDisplay { Hotkey = new Hotkey(GlobalAction.EditorDecreaseDistanceSpacing) },
new HotkeyDisplay { Hotkey = new Hotkey(PlatformAction.Save) },
}
};
}
}

View File

@ -0,0 +1,53 @@
// 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 System.Linq;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
using osu.Framework.Platform;
using osu.Game.Input;
using osu.Game.Input.Bindings;
namespace osu.Game.Graphics.UserInterface
{
public struct Hotkey
{
public KeyCombination[]? KeyCombinations { get; }
public GlobalAction? GlobalAction { get; }
public PlatformAction? PlatformAction { get; }
public Hotkey(params KeyCombination[] keyCombinations)
{
KeyCombinations = keyCombinations;
}
public Hotkey(GlobalAction globalAction)
{
GlobalAction = globalAction;
}
public Hotkey(PlatformAction platformAction)
{
PlatformAction = platformAction;
}
public IEnumerable<string> ResolveKeyCombination(ReadableKeyCombinationProvider keyCombinationProvider, RealmKeyBindingStore keyBindingStore, GameHost gameHost)
{
if (KeyCombinations != null)
return KeyCombinations.Select(keyCombinationProvider.GetReadableString);
if (GlobalAction != null)
return keyBindingStore.GetReadableKeyCombinationsFor(GlobalAction.Value);
if (PlatformAction != null)
{
var action = PlatformAction.Value;
var bindings = gameHost.PlatformKeyBindings.Where(kb => (PlatformAction)kb.Action == action);
return bindings.Select(b => keyCombinationProvider.GetReadableString(b.KeyCombination));
}
return Enumerable.Empty<string>();
}
}
}

View File

@ -0,0 +1,110 @@
// 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.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input;
using osu.Framework.Platform;
using osu.Game.Graphics.Sprites;
using osu.Game.Input;
using osu.Game.Overlays;
using osuTK;
namespace osu.Game.Graphics.UserInterface
{
public partial class HotkeyDisplay : CompositeDrawable
{
private Hotkey hotkey;
public Hotkey Hotkey
{
get => hotkey;
set
{
if (EqualityComparer<Hotkey>.Default.Equals(hotkey, value))
return;
hotkey = value;
if (IsLoaded)
updateState();
}
}
private FillFlowContainer flow = null!;
[Resolved]
private ReadableKeyCombinationProvider readableKeyCombinationProvider { get; set; } = null!;
[Resolved]
private RealmKeyBindingStore realmKeyBindingStore { get; set; } = null!;
[Resolved]
private GameHost gameHost { get; set; } = null!;
[BackgroundDependencyLoader]
private void load()
{
AutoSizeAxes = Axes.Both;
InternalChild = flow = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(5)
};
updateState();
}
protected override void LoadComplete()
{
base.LoadComplete();
updateState();
}
private void updateState()
{
flow.Clear();
foreach (string h in hotkey.ResolveKeyCombination(readableKeyCombinationProvider, realmKeyBindingStore, gameHost))
flow.Add(new HotkeyBox(h));
}
private partial class HotkeyBox : CompositeDrawable
{
private readonly string hotkey;
public HotkeyBox(string hotkey)
{
this.hotkey = hotkey;
}
[BackgroundDependencyLoader]
private void load(OverlayColourProvider? colourProvider, OsuColour colours)
{
AutoSizeAxes = Axes.Both;
Masking = true;
CornerRadius = 3;
InternalChildren = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider?.Background6 ?? Colour4.Black.Opacity(0.7f),
},
new OsuSpriteText
{
Margin = new MarginPadding { Horizontal = 5, Bottom = 1, },
Text = hotkey.ToUpperInvariant(),
Font = OsuFont.Default.With(size: 12, weight: FontWeight.Bold),
Colour = colourProvider?.Light1 ?? colours.GrayA,
}
};
}
}
}
}

View File

@ -409,6 +409,7 @@ namespace osu.Game
KeyBindingStore = new RealmKeyBindingStore(realm, keyCombinationProvider);
KeyBindingStore.Register(globalBindings, RulesetStore.AvailableRulesets);
dependencies.Cache(KeyBindingStore);
dependencies.Cache(globalBindings);