From 08dfe413c11d3112735e020dd146a9f0d63923da Mon Sep 17 00:00:00 2001 From: Andrei Zavatski Date: Tue, 2 Jul 2019 13:07:36 +0300 Subject: [PATCH 1/4] Refactor Ruleset Selector in Direct --- .../Overlays/Direct/DirectRulesetSelector.cs | 94 +++++++++++++++++++ osu.Game/Overlays/Direct/FilterControl.cs | 85 +---------------- 2 files changed, 99 insertions(+), 80 deletions(-) create mode 100644 osu.Game/Overlays/Direct/DirectRulesetSelector.cs diff --git a/osu.Game/Overlays/Direct/DirectRulesetSelector.cs b/osu.Game/Overlays/Direct/DirectRulesetSelector.cs new file mode 100644 index 0000000000..b42633eed3 --- /dev/null +++ b/osu.Game/Overlays/Direct/DirectRulesetSelector.cs @@ -0,0 +1,94 @@ +// 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; +using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.Sprites; +using osu.Framework.Graphics.UserInterface; +using osu.Framework.Input.Events; +using osu.Game.Graphics.UserInterface; +using osu.Game.Rulesets; +using osuTK; +using osuTK.Graphics; + +namespace osu.Game.Overlays.Direct +{ + public class DirectRulesetSelector : RulesetSelector + { + public override bool HandleNonPositionalInput => !Current.Disabled && base.HandleNonPositionalInput; + + public override bool HandlePositionalInput => !Current.Disabled && base.HandlePositionalInput; + + public override bool PropagatePositionalInputSubTree => !Current.Disabled && base.PropagatePositionalInputSubTree; + + public DirectRulesetSelector() + { + TabContainer.Masking = false; + TabContainer.Spacing = new Vector2(10, 0); + AutoSizeAxes = Axes.Both; + + Current.DisabledChanged += value => SelectedTab.FadeColour(value ? Color4.DarkGray : Color4.White, 200, Easing.OutQuint); + } + + protected override TabItem CreateTabItem(RulesetInfo value) => new DirectRulesetTabItem(value); + + protected override TabFillFlowContainer CreateTabFlow() => new TabFillFlowContainer + { + Direction = FillDirection.Horizontal, + AutoSizeAxes = Axes.Both, + }; + + private class DirectRulesetTabItem : TabItem + { + private readonly SpriteIcon icon; + + public DirectRulesetTabItem(RulesetInfo value) + : base(value) + { + AutoSizeAxes = Axes.Both; + + Children = new Drawable[] + { + icon = (SpriteIcon)value.CreateInstance().CreateIcon(), + new HoverClickSounds() + }; + + icon.Size = new Vector2(30); + } + + protected override void LoadComplete() + { + base.LoadComplete(); + + updateState(); + } + + protected override bool OnHover(HoverEvent e) + { + base.OnHover(e); + updateState(); + return true; + } + + protected override void OnHoverLost(HoverLostEvent e) + { + base.OnHoverLost(e); + updateState(); + } + + protected override void OnActivated() => updateState(); + + protected override void OnDeactivated() => updateState(); + + private void updateState() + { + if (IsHovered || Active.Value) + { + icon.FadeColour(Color4.White, 120, Easing.InQuad); + } + else + icon.FadeColour(Color4.Gray, 120, Easing.InQuad); + } + } + } +} diff --git a/osu.Game/Overlays/Direct/FilterControl.cs b/osu.Game/Overlays/Direct/FilterControl.cs index 268e011350..4b43542b43 100644 --- a/osu.Game/Overlays/Direct/FilterControl.cs +++ b/osu.Game/Overlays/Direct/FilterControl.cs @@ -4,105 +4,30 @@ using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Graphics; -using osu.Framework.Graphics.Containers; using osu.Game.Graphics; -using osu.Game.Graphics.Containers; using osu.Game.Online.API.Requests; using osu.Game.Overlays.SearchableList; using osu.Game.Rulesets; -using osuTK; using osuTK.Graphics; namespace osu.Game.Overlays.Direct { public class FilterControl : SearchableListFilterControl { - public readonly Bindable Ruleset = new Bindable(); - private FillFlowContainer modeButtons; + private DirectRulesetSelector rulesetSelector; protected override Color4 BackgroundColour => OsuColour.FromHex(@"384552"); protected override DirectSortCriteria DefaultTab => DirectSortCriteria.Ranked; - protected override Drawable CreateSupplementaryControls() - { - modeButtons = new FillFlowContainer - { - AutoSizeAxes = Axes.Both, - Spacing = new Vector2(10f, 0f), - }; + protected override Drawable CreateSupplementaryControls() => rulesetSelector = new DirectRulesetSelector(); - return modeButtons; - } + public Bindable Ruleset => rulesetSelector.Current; [BackgroundDependencyLoader(true)] - private void load(RulesetStore rulesets, OsuColour colours, Bindable ruleset) + private void load(OsuColour colours, Bindable ruleset) { DisplayStyleControl.Dropdown.AccentColour = colours.BlueDark; - - Ruleset.Value = ruleset.Value ?? rulesets.GetRuleset(0); - foreach (var r in rulesets.AvailableRulesets) - modeButtons.Add(new RulesetToggleButton(Ruleset, r)); - } - - private class RulesetToggleButton : OsuClickableContainer - { - private Drawable icon - { - get => iconContainer.Icon; - set => iconContainer.Icon = value; - } - - private RulesetInfo ruleset; - - public RulesetInfo Ruleset - { - get => ruleset; - set - { - ruleset = value; - icon = Ruleset.CreateInstance().CreateIcon(); - } - } - - private readonly Bindable bindable; - - private readonly ConstrainedIconContainer iconContainer; - - private void Bindable_ValueChanged(ValueChangedEvent e) - { - iconContainer.FadeTo(Ruleset.ID == e.NewValue?.ID ? 1f : 0.5f, 100); - } - - public override bool HandleNonPositionalInput => !bindable.Disabled && base.HandleNonPositionalInput; - public override bool HandlePositionalInput => !bindable.Disabled && base.HandlePositionalInput; - - public RulesetToggleButton(Bindable bindable, RulesetInfo ruleset) - { - this.bindable = bindable; - AutoSizeAxes = Axes.Both; - - Children = new[] - { - iconContainer = new ConstrainedIconContainer - { - Origin = Anchor.TopLeft, - Anchor = Anchor.TopLeft, - Size = new Vector2(32), - } - }; - - Ruleset = ruleset; - bindable.ValueChanged += Bindable_ValueChanged; - Bindable_ValueChanged(new ValueChangedEvent(bindable.Value, bindable.Value)); - Action = () => bindable.Value = Ruleset; - } - - protected override void Dispose(bool isDisposing) - { - if (bindable != null) - bindable.ValueChanged -= Bindable_ValueChanged; - base.Dispose(isDisposing); - } + rulesetSelector.Current.BindTo(ruleset); } } From 2971bd8cbcf3ed62c98d4e703963ac0bdcde35fb Mon Sep 17 00:00:00 2001 From: Andrei Zavatski Date: Tue, 2 Jul 2019 13:22:38 +0300 Subject: [PATCH 2/4] Add disable trigger to a testcase --- .../Visual/Online/TestSceneDirectOverlay.cs | 40 +++++++------------ .../Overlays/Direct/DirectRulesetSelector.cs | 2 +- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/osu.Game.Tests/Visual/Online/TestSceneDirectOverlay.cs b/osu.Game.Tests/Visual/Online/TestSceneDirectOverlay.cs index efc12c5fdd..75c2a2a6a1 100644 --- a/osu.Game.Tests/Visual/Online/TestSceneDirectOverlay.cs +++ b/osu.Game.Tests/Visual/Online/TestSceneDirectOverlay.cs @@ -3,10 +3,8 @@ using System.Collections.Generic; using NUnit.Framework; -using osu.Framework.Allocation; using osu.Game.Beatmaps; using osu.Game.Overlays; -using osu.Game.Rulesets; namespace osu.Game.Tests.Visual.Online { @@ -14,7 +12,6 @@ namespace osu.Game.Tests.Visual.Online public class TestSceneDirectOverlay : OsuTestScene { private DirectOverlay direct; - private RulesetStore rulesets; protected override void LoadComplete() { @@ -25,18 +22,11 @@ namespace osu.Game.Tests.Visual.Online AddStep(@"toggle", direct.ToggleVisibility); AddStep(@"result counts", () => direct.ResultAmounts = new DirectOverlay.ResultCounts(1, 4, 13)); - } - - [BackgroundDependencyLoader] - private void load(RulesetStore rulesets) - { - this.rulesets = rulesets; + AddStep(@"trigger disabled", () => Ruleset.Disabled = !Ruleset.Disabled); } private void newBeatmaps() { - var ruleset = rulesets.GetRuleset(0); - direct.BeatmapSets = new[] { new BeatmapSetInfo @@ -65,7 +55,7 @@ namespace osu.Game.Tests.Visual.Online { new BeatmapInfo { - Ruleset = ruleset, + Ruleset = Ruleset.Value, StarDifficulty = 5.35f, Metadata = new BeatmapMetadata(), }, @@ -97,7 +87,7 @@ namespace osu.Game.Tests.Visual.Online { new BeatmapInfo { - Ruleset = ruleset, + Ruleset = Ruleset.Value, StarDifficulty = 5.81f, Metadata = new BeatmapMetadata(), }, @@ -129,23 +119,23 @@ namespace osu.Game.Tests.Visual.Online { new BeatmapInfo { - Ruleset = ruleset, + Ruleset = Ruleset.Value, StarDifficulty = 0.9f, Metadata = new BeatmapMetadata(), }, new BeatmapInfo { - Ruleset = ruleset, + Ruleset = Ruleset.Value, StarDifficulty = 1.1f, }, new BeatmapInfo { - Ruleset = ruleset, + Ruleset = Ruleset.Value, StarDifficulty = 2.02f, }, new BeatmapInfo { - Ruleset = ruleset, + Ruleset = Ruleset.Value, StarDifficulty = 3.49f, }, }, @@ -176,43 +166,43 @@ namespace osu.Game.Tests.Visual.Online { new BeatmapInfo { - Ruleset = ruleset, + Ruleset = Ruleset.Value, StarDifficulty = 1.26f, Metadata = new BeatmapMetadata(), }, new BeatmapInfo { - Ruleset = ruleset, + Ruleset = Ruleset.Value, StarDifficulty = 2.01f, }, new BeatmapInfo { - Ruleset = ruleset, + Ruleset = Ruleset.Value, StarDifficulty = 2.87f, }, new BeatmapInfo { - Ruleset = ruleset, + Ruleset = Ruleset.Value, StarDifficulty = 3.76f, }, new BeatmapInfo { - Ruleset = ruleset, + Ruleset = Ruleset.Value, StarDifficulty = 3.93f, }, new BeatmapInfo { - Ruleset = ruleset, + Ruleset = Ruleset.Value, StarDifficulty = 4.37f, }, new BeatmapInfo { - Ruleset = ruleset, + Ruleset = Ruleset.Value, StarDifficulty = 5.13f, }, new BeatmapInfo { - Ruleset = ruleset, + Ruleset = Ruleset.Value, StarDifficulty = 5.42f, }, }, diff --git a/osu.Game/Overlays/Direct/DirectRulesetSelector.cs b/osu.Game/Overlays/Direct/DirectRulesetSelector.cs index b42633eed3..289c44a822 100644 --- a/osu.Game/Overlays/Direct/DirectRulesetSelector.cs +++ b/osu.Game/Overlays/Direct/DirectRulesetSelector.cs @@ -43,7 +43,7 @@ namespace osu.Game.Overlays.Direct private readonly SpriteIcon icon; public DirectRulesetTabItem(RulesetInfo value) - : base(value) + : base(value) { AutoSizeAxes = Axes.Both; From 082fa0d808c153c105f269ee9a017dfdb0ec83ef Mon Sep 17 00:00:00 2001 From: Andrei Zavatski Date: Tue, 2 Jul 2019 13:35:02 +0300 Subject: [PATCH 3/4] simplify updateState logic --- osu.Game/Overlays/Direct/DirectRulesetSelector.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/osu.Game/Overlays/Direct/DirectRulesetSelector.cs b/osu.Game/Overlays/Direct/DirectRulesetSelector.cs index 289c44a822..f2abca9ce6 100644 --- a/osu.Game/Overlays/Direct/DirectRulesetSelector.cs +++ b/osu.Game/Overlays/Direct/DirectRulesetSelector.cs @@ -80,15 +80,7 @@ namespace osu.Game.Overlays.Direct protected override void OnDeactivated() => updateState(); - private void updateState() - { - if (IsHovered || Active.Value) - { - icon.FadeColour(Color4.White, 120, Easing.InQuad); - } - else - icon.FadeColour(Color4.Gray, 120, Easing.InQuad); - } + private void updateState() => icon.FadeColour(IsHovered || Active.Value ? Color4.White : Color4.Gray, 120, Easing.InQuad); } } } From cfac90b228a382854242d6ce99fb33ccb8b0d255 Mon Sep 17 00:00:00 2001 From: Andrei Zavatski Date: Wed, 3 Jul 2019 14:34:24 +0300 Subject: [PATCH 4/4] Use ConstrainedIconContainer instead of SpriteIcon --- osu.Game/Overlays/Direct/DirectRulesetSelector.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/osu.Game/Overlays/Direct/DirectRulesetSelector.cs b/osu.Game/Overlays/Direct/DirectRulesetSelector.cs index f2abca9ce6..fdab9f1b90 100644 --- a/osu.Game/Overlays/Direct/DirectRulesetSelector.cs +++ b/osu.Game/Overlays/Direct/DirectRulesetSelector.cs @@ -3,9 +3,9 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; -using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.UserInterface; using osu.Framework.Input.Events; +using osu.Game.Graphics.Containers; using osu.Game.Graphics.UserInterface; using osu.Game.Rulesets; using osuTK; @@ -40,7 +40,7 @@ namespace osu.Game.Overlays.Direct private class DirectRulesetTabItem : TabItem { - private readonly SpriteIcon icon; + private readonly ConstrainedIconContainer iconContainer; public DirectRulesetTabItem(RulesetInfo value) : base(value) @@ -49,11 +49,13 @@ namespace osu.Game.Overlays.Direct Children = new Drawable[] { - icon = (SpriteIcon)value.CreateInstance().CreateIcon(), + iconContainer = new ConstrainedIconContainer + { + Icon = value.CreateInstance().CreateIcon(), + Size = new Vector2(32), + }, new HoverClickSounds() }; - - icon.Size = new Vector2(30); } protected override void LoadComplete() @@ -80,7 +82,7 @@ namespace osu.Game.Overlays.Direct protected override void OnDeactivated() => updateState(); - private void updateState() => icon.FadeColour(IsHovered || Active.Value ? Color4.White : Color4.Gray, 120, Easing.InQuad); + private void updateState() => iconContainer.FadeColour(IsHovered || Active.Value ? Color4.White : Color4.Gray, 120, Easing.InQuad); } } }