Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global key bindings for changing current ruleset #2532

Merged
merged 10 commits into from May 21, 2018
27 changes: 23 additions & 4 deletions osu.Game/Overlays/Toolbar/ToolbarModeSelector.cs
Expand Up @@ -6,7 +6,9 @@
using osu.Framework.Caching;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input;
using OpenTK;
using OpenTK.Input;
using OpenTK.Graphics;
using osu.Framework.Configuration;
using osu.Framework.Graphics.Shapes;
Expand All @@ -22,6 +24,7 @@ public class ToolbarModeSelector : Container
private readonly Drawable modeButtonLine;
private ToolbarModeButton activeButton;

private RulesetStore rulesets;
private readonly Bindable<RulesetInfo> ruleset = new Bindable<RulesetInfo>();

public ToolbarModeSelector()
Expand Down Expand Up @@ -67,26 +70,42 @@ public ToolbarModeSelector()
[BackgroundDependencyLoader(true)]
private void load(RulesetStore rulesets, OsuGame game)
{
this.rulesets = rulesets;
foreach (var r in rulesets.AvailableRulesets)
{
modeButtons.Add(new ToolbarModeButton
{
Ruleset = r,
Action = delegate
{
ruleset.Value = r;
}
Action = delegate { ruleset.Value = r; }
});
}

ruleset.ValueChanged += rulesetChanged;
ruleset.DisabledChanged += disabledChanged;

if (game != null)
ruleset.BindTo(game.Ruleset);
else
ruleset.Value = rulesets.AvailableRulesets.FirstOrDefault();
}

protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
base.OnKeyDown(state, args);

if (state.Keyboard.ControlPressed && !args.Repeat && args.Key >= Key.Number1 && args.Key <= Key.Number9)
{
int requested = args.Key - Key.Number1;

RulesetInfo found = rulesets.AvailableRulesets.Skip(requested).FirstOrDefault();
if (found != null)
ruleset.Value = found;
return true;
}

return false;
}

public override bool HandleKeyboardInput => !ruleset.Disabled && base.HandleKeyboardInput;
public override bool HandleMouseInput => !ruleset.Disabled && base.HandleMouseInput;

Expand Down