From 06dfa42a5ad09ca8ca31f128d433e56a0dc900cb Mon Sep 17 00:00:00 2001 From: EVAST9919 Date: Sat, 8 Jun 2019 18:27:40 +0300 Subject: [PATCH] Refactor --- .../Overlays/Toolbar/ToolbarRulesetButton.cs | 73 +++++++------ .../Toolbar/ToolbarRulesetSelector.cs | 101 +++++++++--------- 2 files changed, 90 insertions(+), 84 deletions(-) diff --git a/osu.Game/Overlays/Toolbar/ToolbarRulesetButton.cs b/osu.Game/Overlays/Toolbar/ToolbarRulesetButton.cs index 87b18ba9f4..efb540cd45 100644 --- a/osu.Game/Overlays/Toolbar/ToolbarRulesetButton.cs +++ b/osu.Game/Overlays/Toolbar/ToolbarRulesetButton.cs @@ -2,57 +2,68 @@ // See the LICENCE file in the repository root for full licence text. using osu.Framework.Graphics.Effects; +using osu.Framework.Graphics.UserInterface; using osu.Game.Rulesets; using osuTK.Graphics; +using osu.Framework.Graphics; namespace osu.Game.Overlays.Toolbar { - public class ToolbarRulesetButton : ToolbarButton + public class ToolbarRulesetButton : TabItem { - private RulesetInfo ruleset; + private readonly DrawableRuleset ruleset; - public RulesetInfo Ruleset + public ToolbarRulesetButton(RulesetInfo value) + : base(value) { - get => ruleset; - set + AutoSizeAxes = Axes.X; + RelativeSizeAxes = Axes.Y; + Child = ruleset = new DrawableRuleset { - ruleset = value; + Active = false, + }; - var rInstance = ruleset.CreateInstance(); + var rInstance = value.CreateInstance(); - TooltipMain = rInstance.Description; - TooltipSub = $"Play some {rInstance.Description}"; - SetIcon(rInstance.CreateIcon()); - } + ruleset.TooltipMain = rInstance.Description; + ruleset.TooltipSub = $"Play some {rInstance.Description}"; + ruleset.SetIcon(rInstance.CreateIcon()); } - public bool Active + protected override void OnActivated() => ruleset.Active = true; + + protected override void OnDeactivated() => ruleset.Active = false; + + private class DrawableRuleset : ToolbarButton { - set + public bool Active { - if (value) + set { - IconContainer.Colour = Color4.White; - IconContainer.EdgeEffect = new EdgeEffectParameters + if (value) { - Type = EdgeEffectType.Glow, - Colour = new Color4(255, 194, 224, 100), - Radius = 15, - Roundness = 15, - }; - } - else - { - IconContainer.Colour = new Color4(255, 194, 224, 255); - IconContainer.EdgeEffect = new EdgeEffectParameters(); + IconContainer.Colour = Color4.White; + IconContainer.EdgeEffect = new EdgeEffectParameters + { + Type = EdgeEffectType.Glow, + Colour = new Color4(255, 194, 224, 100), + Radius = 15, + Roundness = 15, + }; + } + else + { + IconContainer.Colour = new Color4(255, 194, 224, 255); + IconContainer.EdgeEffect = new EdgeEffectParameters(); + } } } - } - protected override void LoadComplete() - { - base.LoadComplete(); - IconContainer.Scale *= 1.4f; + protected override void LoadComplete() + { + base.LoadComplete(); + IconContainer.Scale *= 1.4f; + } } } } diff --git a/osu.Game/Overlays/Toolbar/ToolbarRulesetSelector.cs b/osu.Game/Overlays/Toolbar/ToolbarRulesetSelector.cs index 90412ec1d1..abea9b217d 100644 --- a/osu.Game/Overlays/Toolbar/ToolbarRulesetSelector.cs +++ b/osu.Game/Overlays/Toolbar/ToolbarRulesetSelector.cs @@ -1,53 +1,54 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using System.Linq; using osu.Framework.Allocation; -using osu.Framework.Bindables; using osu.Framework.Caching; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Effects; using osuTK; -using osuTK.Input; using osuTK.Graphics; using osu.Framework.Graphics.Shapes; -using osu.Framework.Input.Events; using osu.Game.Rulesets; +using osu.Framework.Graphics.UserInterface; +using osu.Framework.Bindables; +using osu.Framework.Input.Events; +using osuTK.Input; +using System.Linq; namespace osu.Game.Overlays.Toolbar { - public class ToolbarRulesetSelector : Container + public class ToolbarRulesetSelector : TabControl { private const float padding = 10; - - private readonly FillFlowContainer modeButtons; private readonly Drawable modeButtonLine; - private ToolbarRulesetButton activeButton; - private RulesetStore rulesets; - private readonly Bindable ruleset = new Bindable(); + + public override bool HandleNonPositionalInput => !Current.Disabled && base.HandleNonPositionalInput; + public override bool HandlePositionalInput => !Current.Disabled && base.HandlePositionalInput; + + public override bool PropagatePositionalInputSubTree => !Current.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 ToolbarRulesetButton(value); public ToolbarRulesetSelector() { RelativeSizeAxes = Axes.Y; AutoSizeAxes = Axes.X; - Children = new[] + AddRangeInternal(new Drawable[] { - new OpaqueBackground(), - modeButtons = new FillFlowContainer + new OpaqueBackground { - RelativeSizeAxes = Axes.Y, - AutoSizeAxes = Axes.X, - Direction = FillDirection.Horizontal, - Anchor = Anchor.TopCentre, - Origin = Anchor.TopCentre, - Padding = new MarginPadding { Left = padding, Right = padding }, + Depth = 1, }, modeButtonLine = new Container { - Size = new Vector2(padding * 2 + ToolbarButton.WIDTH, 3), + Size = new Vector2(padding* 2 + ToolbarButton.WIDTH, 3), Anchor = Anchor.BottomLeft, Origin = Anchor.TopLeft, Masking = true, @@ -58,17 +59,22 @@ public ToolbarRulesetSelector() Radius = 15, Roundness = 15, }, - Children = new[] + Child = new Box { - new Box - { - RelativeSizeAxes = Axes.Both, - } + RelativeSizeAxes = Axes.Both, } } - }; + }); } + protected override TabFillFlowContainer CreateTabFlow() => new TabFillFlowContainer + { + RelativeSizeAxes = Axes.Y, + AutoSizeAxes = Axes.X, + Direction = FillDirection.Horizontal, + Padding = new MarginPadding { Left = padding, Right = padding }, + }; + [BackgroundDependencyLoader] private void load(RulesetStore rulesets, Bindable parentRuleset) { @@ -76,16 +82,13 @@ private void load(RulesetStore rulesets, Bindable parentRuleset) foreach (var r in rulesets.AvailableRulesets) { - modeButtons.Add(new ToolbarRulesetButton - { - Ruleset = r, - Action = delegate { ruleset.Value = r; } - }); + AddItem(r); } - ruleset.ValueChanged += rulesetChanged; - ruleset.DisabledChanged += disabledChanged; - ruleset.BindTo(parentRuleset); + Current.BindTo(parentRuleset); + Current.Disabled = false; + Current.DisabledChanged += disabledChanged; + Current.BindValueChanged(rulesetChanged); } protected override bool OnKeyDown(KeyDownEvent e) @@ -98,43 +101,35 @@ protected override bool OnKeyDown(KeyDownEvent e) RulesetInfo found = rulesets.AvailableRulesets.Skip(requested).FirstOrDefault(); if (found != null) - ruleset.Value = found; + Current.Value = found; return true; } return false; } - public override bool HandleNonPositionalInput => !ruleset.Disabled && base.HandleNonPositionalInput; - public override bool HandlePositionalInput => !ruleset.Disabled && base.HandlePositionalInput; - - public override bool PropagatePositionalInputSubTree => !ruleset.Disabled && base.PropagatePositionalInputSubTree; - - private void disabledChanged(bool isDisabled) => this.FadeColour(isDisabled ? Color4.Gray : Color4.White, 300); + private readonly Cached activeMode = new Cached(); private void rulesetChanged(ValueChangedEvent e) { - foreach (ToolbarRulesetButton m in modeButtons.Children.Cast()) - { - bool isActive = m.Ruleset.ID == e.NewValue.ID; - m.Active = isActive; - if (isActive) - activeButton = m; - } - activeMode.Invalidate(); } - private Cached activeMode = new Cached(); - protected override void UpdateAfterChildren() { base.UpdateAfterChildren(); if (!activeMode.IsValid) { - modeButtonLine.MoveToX(activeButton.DrawPosition.X, 200, Easing.OutQuint); - activeMode.Validate(); + foreach (TabItem tabItem in TabContainer) + { + if (tabItem.Value == Current.Value) + { + modeButtonLine.MoveToX(tabItem.DrawPosition.X, 200, Easing.OutQuint); + activeMode.Validate(); + return; + } + } } } }