mirror of
https://github.com/ppy/osu.git
synced 2024-11-11 10:33:30 +08:00
Implement a BindableRulesetSelector
This commit is contained in:
parent
cecbcd21b2
commit
c95e3da3ec
@ -17,7 +17,7 @@ using System.Linq;
|
||||
|
||||
namespace osu.Game.Overlays.Toolbar
|
||||
{
|
||||
public class ToolbarRulesetSelector : RulesetSelector
|
||||
public class ToolbarRulesetSelector : BindableRulesetSelector
|
||||
{
|
||||
private const float padding = 10;
|
||||
private readonly Drawable modeButtonLine;
|
||||
|
37
osu.Game/Rulesets/BindableRulesetSelector.cs
Normal file
37
osu.Game/Rulesets/BindableRulesetSelector.cs
Normal file
@ -0,0 +1,37 @@
|
||||
// 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.Bindables;
|
||||
|
||||
namespace osu.Game.Rulesets
|
||||
{
|
||||
public abstract class BindableRulesetSelector : RulesetSelector
|
||||
{
|
||||
protected readonly Bindable<RulesetInfo> GlobalRuleset = new Bindable<RulesetInfo>();
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(Bindable<RulesetInfo> parentRuleset)
|
||||
{
|
||||
GlobalRuleset.BindTo(parentRuleset);
|
||||
|
||||
GlobalRuleset.BindValueChanged(globalRulesetChanged);
|
||||
Current.BindValueChanged(OnLocalRulesetChanged);
|
||||
}
|
||||
|
||||
private void globalRulesetChanged(ValueChangedEvent<RulesetInfo> e) => OnGlobalRulesetChanged(e);
|
||||
|
||||
protected virtual void OnGlobalRulesetChanged(ValueChangedEvent<RulesetInfo> e)
|
||||
{
|
||||
Current.Value = e.NewValue;
|
||||
}
|
||||
|
||||
protected virtual void OnLocalRulesetChanged(ValueChangedEvent<RulesetInfo> e)
|
||||
{
|
||||
if (!GlobalRuleset.Disabled)
|
||||
{
|
||||
GlobalRuleset.Value = e.NewValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@
|
||||
// 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
|
||||
@ -10,54 +9,18 @@ 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)
|
||||
private void load(RulesetStore rulesets)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user