diff --git a/osu.Game/Overlays/Toolbar/ToolbarRulesetSelector.cs b/osu.Game/Overlays/Toolbar/ToolbarRulesetSelector.cs index 3baf9a1e49..bd7ac13c9e 100644 --- a/osu.Game/Overlays/Toolbar/ToolbarRulesetSelector.cs +++ b/osu.Game/Overlays/Toolbar/ToolbarRulesetSelector.cs @@ -1,7 +1,6 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using osu.Framework.Allocation; using osu.Framework.Caching; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; @@ -18,22 +17,18 @@ namespace osu.Game.Overlays.Toolbar { - public class ToolbarRulesetSelector : TabControl + public class ToolbarRulesetSelector : RulesetSelector { private const float padding = 10; private readonly Drawable modeButtonLine; - private RulesetStore rulesets; - private readonly Bindable globalRuleset = new Bindable(); - public override bool HandleNonPositionalInput => !globalRuleset.Disabled && base.HandleNonPositionalInput; - public override bool HandlePositionalInput => !globalRuleset.Disabled && base.HandlePositionalInput; + public override bool HandleNonPositionalInput => !GlobalRuleset.Disabled && base.HandleNonPositionalInput; + public override bool HandlePositionalInput => !GlobalRuleset.Disabled && base.HandlePositionalInput; - public override bool PropagatePositionalInputSubTree => !globalRuleset.Disabled && base.PropagatePositionalInputSubTree; + public override bool PropagatePositionalInputSubTree => !GlobalRuleset.Disabled && base.PropagatePositionalInputSubTree; private void disabledChanged(bool isDisabled) => this.FadeColour(isDisabled ? Color4.Gray : Color4.White, 300); - protected override Dropdown CreateDropdown() => null; - protected override TabItem CreateTabItem(RulesetInfo value) => new ToolbarRulesetTabButton(value); public ToolbarRulesetSelector() @@ -66,6 +61,8 @@ public ToolbarRulesetSelector() } } }); + + GlobalRuleset.DisabledChanged += disabledChanged; } protected override TabFillFlowContainer CreateTabFlow() => new TabFillFlowContainer @@ -76,24 +73,6 @@ public ToolbarRulesetSelector() Padding = new MarginPadding { Left = padding, Right = padding }, }; - [BackgroundDependencyLoader] - private void load(RulesetStore rulesets, Bindable parentRuleset) - { - this.rulesets = rulesets; - globalRuleset.BindTo(parentRuleset); - - foreach (var r in rulesets.AvailableRulesets) - { - AddItem(r); - } - - globalRuleset.BindValueChanged(globalRulesetChanged); - globalRuleset.DisabledChanged += disabledChanged; - Current.BindValueChanged(localRulesetChanged); - } - - private void globalRulesetChanged(ValueChangedEvent e) => Current.Value = e.NewValue; - protected override bool OnKeyDown(KeyDownEvent e) { base.OnKeyDown(e); @@ -102,7 +81,7 @@ protected override bool OnKeyDown(KeyDownEvent e) { int requested = e.Key - Key.Number1; - RulesetInfo found = rulesets.AvailableRulesets.Skip(requested).FirstOrDefault(); + RulesetInfo found = AvaliableRulesets.AvailableRulesets.Skip(requested).FirstOrDefault(); if (found != null) Current.Value = found; return true; @@ -113,12 +92,9 @@ protected override bool OnKeyDown(KeyDownEvent e) private readonly Cached activeMode = new Cached(); - private void localRulesetChanged(ValueChangedEvent e) + protected override void OnLocalRulesetChanged(ValueChangedEvent e) { - if (!globalRuleset.Disabled) - { - globalRuleset.Value = e.NewValue; - } + base.OnLocalRulesetChanged(e); activeMode.Invalidate(); } diff --git a/osu.Game/Rulesets/RulesetSelector.cs b/osu.Game/Rulesets/RulesetSelector.cs new file mode 100644 index 0000000000..e646c2676b --- /dev/null +++ b/osu.Game/Rulesets/RulesetSelector.cs @@ -0,0 +1,63 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using osu.Framework.Graphics.UserInterface; +using osu.Framework.Bindables; +using osu.Framework.Allocation; + +namespace osu.Game.Rulesets +{ + public abstract class RulesetSelector : TabControl + { + protected RulesetStore AvaliableRulesets; + protected readonly Bindable GlobalRuleset = new Bindable(); + + protected override Dropdown CreateDropdown() => null; + + /// + /// Whether we want to change a global ruleset when local one is changed. + /// + protected virtual bool AllowGlobalRulesetChange => true; + + /// + /// Whether we want to change a local ruleset when global one is changed. + /// /// + protected virtual bool AllowLocalRulesetChange => true; + + [BackgroundDependencyLoader] + private void load(RulesetStore rulesets, Bindable parentRuleset) + { + AvaliableRulesets = rulesets; + GlobalRuleset.BindTo(parentRuleset); + + foreach (var r in rulesets.AvailableRulesets) + { + AddItem(r); + } + + GlobalRuleset.BindValueChanged(globalRulesetChanged); + Current.BindValueChanged(OnLocalRulesetChanged); + } + + private void globalRulesetChanged(ValueChangedEvent e) + { + if (AllowLocalRulesetChange) + { + OnGlobalRulesetChanged(e); + } + } + + protected virtual void OnGlobalRulesetChanged(ValueChangedEvent e) + { + Current.Value = e.NewValue; + } + + protected virtual void OnLocalRulesetChanged(ValueChangedEvent e) + { + if (!GlobalRuleset.Disabled && AllowGlobalRulesetChange) + { + GlobalRuleset.Value = e.NewValue; + } + } + } +}