From 5aa90df8195ffc8407a6d6fa63d84e48301d320d Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 21 Apr 2017 16:03:59 +0900 Subject: [PATCH] Allow OsuScreens to decide whether ruleset switching should be allowed. Tidies up ToolbarModeSelector a lot by using DI. --- osu.Game/OsuGame.cs | 8 +------ osu.Game/Overlays/Toolbar/Toolbar.cs | 12 +--------- .../Overlays/Toolbar/ToolbarModeSelector.cs | 24 ++++++++++++------- osu.Game/Screens/OsuScreen.cs | 19 ++++++++++++++- osu.Game/Screens/Play/Player.cs | 2 ++ osu.Game/Screens/Play/PlayerLoader.cs | 2 ++ osu.Game/Screens/Ranking/Results.cs | 2 ++ 7 files changed, 41 insertions(+), 28 deletions(-) diff --git a/osu.Game/OsuGame.cs b/osu.Game/OsuGame.cs index a139c4d6ce..c9f41de5f2 100644 --- a/osu.Game/OsuGame.cs +++ b/osu.Game/OsuGame.cs @@ -203,13 +203,7 @@ namespace osu.Game { Depth = -3, OnHome = delegate { intro?.ChildScreen?.MakeCurrent(); }, - OnRulesetChange = r => Ruleset.Value = r, - }, t => - { - Ruleset.ValueChanged += delegate { Toolbar.SetRuleset(Ruleset.Value); }; - Ruleset.TriggerChange(); - overlayContent.Add(Toolbar); - }); + }, overlayContent.Add); options.StateChanged += delegate { diff --git a/osu.Game/Overlays/Toolbar/Toolbar.cs b/osu.Game/Overlays/Toolbar/Toolbar.cs index a5074100c7..8c4e9e2ff8 100644 --- a/osu.Game/Overlays/Toolbar/Toolbar.cs +++ b/osu.Game/Overlays/Toolbar/Toolbar.cs @@ -8,7 +8,6 @@ using osu.Framework.Graphics.Colour; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Sprites; using osu.Framework.Input; -using osu.Game.Database; using osu.Game.Graphics; using OpenTK; @@ -20,7 +19,6 @@ namespace osu.Game.Overlays.Toolbar public const float TOOLTIP_HEIGHT = 30; public Action OnHome; - public Action OnRulesetChange; private readonly ToolbarModeSelector modeSelector; private readonly ToolbarUserArea userArea; @@ -53,13 +51,7 @@ namespace osu.Game.Overlays.Toolbar { Action = () => OnHome?.Invoke() }, - modeSelector = new ToolbarModeSelector - { - OnRulesetChange = mode => - { - OnRulesetChange?.Invoke(mode); - } - } + modeSelector = new ToolbarModeSelector() } }, new FillFlowContainer @@ -130,8 +122,6 @@ namespace osu.Game.Overlays.Toolbar } } - public void SetRuleset(RulesetInfo ruleset) => modeSelector.SetRuleset(ruleset); - protected override void PopIn() { MoveToY(0, transition_time, EasingTypes.OutQuint); diff --git a/osu.Game/Overlays/Toolbar/ToolbarModeSelector.cs b/osu.Game/Overlays/Toolbar/ToolbarModeSelector.cs index 1d3260bb23..209b64e709 100644 --- a/osu.Game/Overlays/Toolbar/ToolbarModeSelector.cs +++ b/osu.Game/Overlays/Toolbar/ToolbarModeSelector.cs @@ -1,7 +1,6 @@ // Copyright (c) 2007-2017 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE -using System; using System.Linq; using osu.Framework.Allocation; using osu.Framework.Caching; @@ -12,6 +11,7 @@ using osu.Framework.Graphics.Sprites; using osu.Game.Database; using OpenTK; using OpenTK.Graphics; +using osu.Framework.Configuration; namespace osu.Game.Overlays.Toolbar { @@ -23,7 +23,7 @@ namespace osu.Game.Overlays.Toolbar private readonly Drawable modeButtonLine; private ToolbarModeButton activeButton; - public Action OnRulesetChange; + private readonly Bindable ruleset = new Bindable(); public ToolbarModeSelector() { @@ -66,30 +66,36 @@ namespace osu.Game.Overlays.Toolbar } [BackgroundDependencyLoader] - private void load(RulesetDatabase rulesets) + private void load(RulesetDatabase rulesets, OsuGame game) { - foreach (var ruleset in rulesets.AllRulesets) + foreach (var r in rulesets.AllRulesets) { modeButtons.Add(new ToolbarModeButton { - Ruleset = ruleset, + Ruleset = r, Action = delegate { - SetRuleset(ruleset); - OnRulesetChange?.Invoke(ruleset); + ruleset.Value = r; } }); } + + ruleset.ValueChanged += rulesetChanged; + ruleset.DisabledChanged += disabledChanged; + ruleset.BindTo(game.Ruleset); } + public override bool HandleInput => !ruleset.Disabled; + + private void disabledChanged(bool isDisabled) => FadeColour(isDisabled ? Color4.Gray : Color4.White, 300); + protected override void Update() { base.Update(); - Size = new Vector2(modeButtons.DrawSize.X, 1); } - public void SetRuleset(RulesetInfo ruleset) + private void rulesetChanged(RulesetInfo ruleset) { foreach (ToolbarModeButton m in modeButtons.Children.Cast()) { diff --git a/osu.Game/Screens/OsuScreen.cs b/osu.Game/Screens/OsuScreen.cs index d0856bfe3e..0b3ecb4f5a 100644 --- a/osu.Game/Screens/OsuScreen.cs +++ b/osu.Game/Screens/OsuScreen.cs @@ -5,6 +5,7 @@ using osu.Framework.Allocation; using osu.Framework.Configuration; using osu.Framework.Screens; using osu.Game.Beatmaps; +using osu.Game.Database; using osu.Game.Graphics.Containers; namespace osu.Game.Screens @@ -25,8 +26,12 @@ namespace osu.Game.Screens internal virtual bool HasLocalCursorDisplayed => false; + internal virtual bool AllowRulesetChange => true; + private readonly Bindable beatmap = new Bindable(); + private readonly Bindable ruleset = new Bindable(); + public WorkingBeatmap Beatmap { get @@ -40,7 +45,7 @@ namespace osu.Game.Screens } [BackgroundDependencyLoader(permitNulls: true)] - private void load(OsuGameBase game) + private void load(OsuGameBase game, OsuGame osuGame) { if (game != null) { @@ -52,11 +57,23 @@ namespace osu.Game.Screens } beatmap.ValueChanged += OnBeatmapChanged; + + if (osuGame != null) + ruleset.BindTo(osuGame.Ruleset); } + /// + /// The global Beatmap was changed. + /// protected virtual void OnBeatmapChanged(WorkingBeatmap beatmap) { + } + protected override void Update() + { + if (!IsCurrentScreen) return; + + ruleset.Disabled = !AllowRulesetChange; } protected override void OnEntering(Screen last) diff --git a/osu.Game/Screens/Play/Player.cs b/osu.Game/Screens/Play/Player.cs index 5502723c0d..70cf82af80 100644 --- a/osu.Game/Screens/Play/Player.cs +++ b/osu.Game/Screens/Play/Player.cs @@ -39,6 +39,8 @@ namespace osu.Game.Screens.Play public bool IsPaused => !interpolatedSourceClock.IsRunning; + internal override bool AllowRulesetChange => false; + public bool HasFailed { get; private set; } public int RestartCount; diff --git a/osu.Game/Screens/Play/PlayerLoader.cs b/osu.Game/Screens/Play/PlayerLoader.cs index 615b138ead..765abd9873 100644 --- a/osu.Game/Screens/Play/PlayerLoader.cs +++ b/osu.Game/Screens/Play/PlayerLoader.cs @@ -27,6 +27,8 @@ namespace osu.Game.Screens.Play private bool showOverlays = true; internal override bool ShowOverlays => showOverlays; + internal override bool AllowRulesetChange => false; + protected override BackgroundScreen CreateBackground() => new BackgroundScreenBeatmap(Beatmap); public PlayerLoader(Player player) diff --git a/osu.Game/Screens/Ranking/Results.cs b/osu.Game/Screens/Ranking/Results.cs index f4edc11436..ffe72966f5 100644 --- a/osu.Game/Screens/Ranking/Results.cs +++ b/osu.Game/Screens/Ranking/Results.cs @@ -31,6 +31,8 @@ namespace osu.Game.Screens.Ranking private ResultModeTabControl modeChangeButtons; + internal override bool AllowRulesetChange => false; + private Container currentPage; private static readonly Vector2 background_blur = new Vector2(20);