Refactor Ruleset Selector in Direct

This commit is contained in:
Andrei Zavatski 2019-07-02 13:07:36 +03:00
parent 115dcc3147
commit 08dfe413c1
2 changed files with 99 additions and 80 deletions

View File

@ -0,0 +1,94 @@
// 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;
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<RulesetInfo> CreateTabItem(RulesetInfo value) => new DirectRulesetTabItem(value);
protected override TabFillFlowContainer CreateTabFlow() => new TabFillFlowContainer
{
Direction = FillDirection.Horizontal,
AutoSizeAxes = Axes.Both,
};
private class DirectRulesetTabItem : TabItem<RulesetInfo>
{
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);
}
}
}
}

View File

@ -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<DirectSortCriteria, BeatmapSearchCategory>
{
public readonly Bindable<RulesetInfo> Ruleset = new Bindable<RulesetInfo>();
private FillFlowContainer<RulesetToggleButton> modeButtons;
private DirectRulesetSelector rulesetSelector;
protected override Color4 BackgroundColour => OsuColour.FromHex(@"384552");
protected override DirectSortCriteria DefaultTab => DirectSortCriteria.Ranked;
protected override Drawable CreateSupplementaryControls()
{
modeButtons = new FillFlowContainer<RulesetToggleButton>
{
AutoSizeAxes = Axes.Both,
Spacing = new Vector2(10f, 0f),
};
protected override Drawable CreateSupplementaryControls() => rulesetSelector = new DirectRulesetSelector();
return modeButtons;
}
public Bindable<RulesetInfo> Ruleset => rulesetSelector.Current;
[BackgroundDependencyLoader(true)]
private void load(RulesetStore rulesets, OsuColour colours, Bindable<RulesetInfo> ruleset)
private void load(OsuColour colours, Bindable<RulesetInfo> 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<RulesetInfo> bindable;
private readonly ConstrainedIconContainer iconContainer;
private void Bindable_ValueChanged(ValueChangedEvent<RulesetInfo> 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<RulesetInfo> 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<RulesetInfo>(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);
}
}