1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 13:37:25 +08:00

Implement OverlayRulesetSelector

This commit is contained in:
Andrei Zavatski 2020-01-01 22:49:04 +03:00
parent beb8e72e15
commit af248457b0
3 changed files with 210 additions and 0 deletions

View File

@ -0,0 +1,68 @@
// 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 System;
using System.Collections.Generic;
using osu.Game.Rulesets.Catch;
using osu.Game.Rulesets.Mania;
using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Taiko;
using osu.Framework.Bindables;
using osu.Game.Overlays;
using osu.Game.Rulesets;
using NUnit.Framework;
using osu.Game.Graphics;
using osu.Framework.Allocation;
namespace osu.Game.Tests.Visual.UserInterface
{
public class TestSceneOverlayRulesetSelector : OsuTestScene
{
public override IReadOnlyList<Type> RequiredTypes => new[]
{
typeof(OverlayRulesetSelector),
typeof(OverlayRulesetTabItem),
};
private readonly OverlayRulesetSelector selector;
private readonly Bindable<RulesetInfo> ruleset = new Bindable<RulesetInfo>();
public TestSceneOverlayRulesetSelector()
{
Add(selector = new OverlayRulesetSelector
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Current = ruleset,
});
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
selector.AccentColour = colours.Lime;
}
[Test]
public void TestSelection()
{
var osuRuleset = new OsuRuleset().RulesetInfo;
var maniaRuleset = new ManiaRuleset().RulesetInfo;
var taikoRuleset = new TaikoRuleset().RulesetInfo;
var catchRuleset = new CatchRuleset().RulesetInfo;
AddStep("Select osu!", () => ruleset.Value = osuRuleset);
AddAssert("Check osu! selected", () => selector.Current.Value == osuRuleset);
AddStep("Select mania", () => ruleset.Value = maniaRuleset);
AddAssert("Check mania selected", () => selector.Current.Value == maniaRuleset);
AddStep("Select taiko", () => ruleset.Value = taikoRuleset);
AddAssert("Check taiko selected", () => selector.Current.Value == taikoRuleset);
AddStep("Select catch", () => ruleset.Value = catchRuleset);
AddAssert("Check catch selected", () => selector.Current.Value == catchRuleset);
}
}
}

View File

@ -0,0 +1,44 @@
// 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.UserInterface;
using osu.Game.Graphics;
using osu.Game.Rulesets;
using osuTK;
using osuTK.Graphics;
using System.Linq;
namespace osu.Game.Overlays
{
public class OverlayRulesetSelector : RulesetSelector
{
private Color4 accentColour;
public Color4 AccentColour
{
get => accentColour;
set
{
accentColour = value;
foreach (var i in TabContainer.Children.OfType<IHasAccentColour>())
i.AccentColour = value;
}
}
public OverlayRulesetSelector()
{
AutoSizeAxes = Axes.Both;
}
protected override TabItem<RulesetInfo> CreateTabItem(RulesetInfo value) => new OverlayRulesetTabItem(value);
protected override TabFillFlowContainer CreateTabFlow() => new TabFillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(25, 0),
};
}
}

View File

@ -0,0 +1,98 @@
// 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.UserInterface;
using osu.Framework.Input.Events;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Rulesets;
using osuTK.Graphics;
using osuTK;
namespace osu.Game.Overlays
{
public class OverlayRulesetTabItem : TabItem<RulesetInfo>, IHasAccentColour
{
protected readonly OsuSpriteText Text;
private readonly FillFlowContainer content;
public override bool PropagatePositionalInputSubTree => Enabled.Value && !Active.Value && base.PropagatePositionalInputSubTree;
private Color4 accentColour;
public Color4 AccentColour
{
get => accentColour;
set
{
if (accentColour == value)
return;
accentColour = value;
UpdateState();
}
}
protected override Container<Drawable> Content => content;
public OverlayRulesetTabItem(RulesetInfo value)
: base(value)
{
AutoSizeAxes = Axes.Both;
AddRangeInternal(new Drawable[]
{
content = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(3, 0),
Child = Text = new OsuSpriteText
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Text = value.Name,
}
},
new HoverClickSounds()
});
Enabled.Value = true;
}
protected override void LoadComplete()
{
base.LoadComplete();
Enabled.BindValueChanged(_ => UpdateState(), true);
}
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();
protected virtual void UpdateState()
{
Text.Font = Text.Font.With(weight: Active.Value ? FontWeight.Bold : FontWeight.Medium);
Text.FadeColour(GetColour(), 120, Easing.OutQuint);
}
protected Color4 GetColour() => IsHovered || Active.Value ? Color4.White : Enabled.Value ? AccentColour : Color4.DimGray;
}
}