Implement an abstract RulesetSelector class

This commit is contained in:
EVAST9919 2019-06-12 23:23:01 +03:00
parent 8d8615773c
commit e5a6d920cd
2 changed files with 72 additions and 33 deletions

View File

@ -1,7 +1,6 @@
// 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.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<RulesetInfo>
public class ToolbarRulesetSelector : RulesetSelector
{
private const float padding = 10;
private readonly Drawable modeButtonLine;
private RulesetStore rulesets;
private readonly Bindable<RulesetInfo> globalRuleset = new Bindable<RulesetInfo>();
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<RulesetInfo> CreateDropdown() => null;
protected override TabItem<RulesetInfo> 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<RulesetInfo> 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<RulesetInfo> 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<RulesetInfo> e)
protected override void OnLocalRulesetChanged(ValueChangedEvent<RulesetInfo> e)
{
if (!globalRuleset.Disabled)
{
globalRuleset.Value = e.NewValue;
}
base.OnLocalRulesetChanged(e);
activeMode.Invalidate();
}

View File

@ -0,0 +1,63 @@
// 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.UserInterface;
using osu.Framework.Bindables;
using osu.Framework.Allocation;
namespace osu.Game.Rulesets
{
public abstract class RulesetSelector : TabControl<RulesetInfo>
{
protected RulesetStore AvaliableRulesets;
protected readonly Bindable<RulesetInfo> GlobalRuleset = new Bindable<RulesetInfo>();
protected override Dropdown<RulesetInfo> CreateDropdown() => null;
/// <summary>
/// Whether we want to change a global ruleset when local one is changed.
/// </summary>
protected virtual bool AllowGlobalRulesetChange => true;
/// <summary>
/// Whether we want to change a local ruleset when global one is changed.
/// /// </summary>
protected virtual bool AllowLocalRulesetChange => true;
[BackgroundDependencyLoader]
private void load(RulesetStore rulesets, Bindable<RulesetInfo> parentRuleset)
{
AvaliableRulesets = rulesets;
GlobalRuleset.BindTo(parentRuleset);
foreach (var r in rulesets.AvailableRulesets)
{
AddItem(r);
}
GlobalRuleset.BindValueChanged(globalRulesetChanged);
Current.BindValueChanged(OnLocalRulesetChanged);
}
private void globalRulesetChanged(ValueChangedEvent<RulesetInfo> e)
{
if (AllowLocalRulesetChange)
{
OnGlobalRulesetChanged(e);
}
}
protected virtual void OnGlobalRulesetChanged(ValueChangedEvent<RulesetInfo> e)
{
Current.Value = e.NewValue;
}
protected virtual void OnLocalRulesetChanged(ValueChangedEvent<RulesetInfo> e)
{
if (!GlobalRuleset.Disabled && AllowGlobalRulesetChange)
{
GlobalRuleset.Value = e.NewValue;
}
}
}
}