1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 21:02:55 +08:00

Implement BeatmapSearchFilter component

This commit is contained in:
Andrei Zavatski 2020-02-16 14:19:49 +03:00
parent 80462014d9
commit 3888911eee
3 changed files with 195 additions and 0 deletions

View File

@ -0,0 +1,55 @@
// 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 System;
using System.Collections.Generic;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Online.API.Requests;
using osu.Game.Overlays;
using osu.Game.Overlays.BeatmapListing;
using osuTK;
namespace osu.Game.Tests.Visual.UserInterface
{
public class TestSceneBeatmapSearchFilter : OsuTestScene
{
public override IReadOnlyList<Type> RequiredTypes => new[]
{
typeof(BeatmapSearchFilter<>),
typeof(BeatmapSearchRulesetFilter)
};
[Cached]
private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Blue);
private readonly FillFlowContainer resizableContainer;
public TestSceneBeatmapSearchFilter()
{
Add(resizableContainer = new FillFlowContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.Y,
Width = 600,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 10),
Children = new Drawable[]
{
new BeatmapSearchRulesetFilter(),
new BeatmapSearchFilter<BeatmapSearchCategory>(),
}
});
}
[Test]
public void TestResize()
{
AddStep("Resize to 100px", () => resizableContainer.ResizeWidthTo(100, 1000));
AddStep("Resize to 600px", () => resizableContainer.ResizeWidthTo(600, 1000));
}
}
}

View File

@ -0,0 +1,104 @@
// 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 System;
using osu.Framework.Allocation;
using osu.Framework.Extensions;
using osu.Framework.Graphics;
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 osuTK;
using osuTK.Graphics;
using osu.Framework.Graphics.Containers;
namespace osu.Game.Overlays.BeatmapListing
{
public class BeatmapSearchFilter<T> : TabControl<T>
{
public BeatmapSearchFilter()
{
AutoSizeAxes = Axes.Y;
RelativeSizeAxes = Axes.X;
if (typeof(T).IsEnum)
{
foreach (var val in (T[])Enum.GetValues(typeof(T)))
AddItem(val);
}
}
protected override Dropdown<T> CreateDropdown() => null;
protected override TabItem<T> CreateTabItem(T value) => new FilterTabItem(value);
protected override TabFillFlowContainer CreateTabFlow() => new TabFillFlowContainer
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Direction = FillDirection.Full,
Spacing = new Vector2(10),
AllowMultiline = true,
};
protected class FilterTabItem : TabItem<T>
{
[Resolved]
private OverlayColourProvider colourProvider { get; set; }
private readonly OsuSpriteText text;
public FilterTabItem(T value)
: base(value)
{
AutoSizeAxes = Axes.Both;
AddRangeInternal(new Drawable[]
{
text = new OsuSpriteText
{
Font = OsuFont.GetFont(size: 13, weight: FontWeight.Regular),
Text = GetText(value)
},
new HoverClickSounds()
});
Enabled.Value = true;
}
[BackgroundDependencyLoader]
private void load()
{
updateState();
}
protected virtual string GetText(T value) => (value as Enum)?.GetDescription() ?? value.ToString();
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()
{
text.Font = text.Font.With(weight: Active.Value ? FontWeight.Medium : FontWeight.Regular);
text.FadeColour(Active.Value ? Color4.White : getStateColour(), 200, Easing.OutQuint);
}
private Color4 getStateColour() => IsHovered ? colourProvider.Light1 : colourProvider.Light3;
}
}
}

View File

@ -0,0 +1,36 @@
// 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.Allocation;
using osu.Game.Rulesets;
using osu.Framework.Graphics.UserInterface;
namespace osu.Game.Overlays.BeatmapListing
{
public class BeatmapSearchRulesetFilter : BeatmapSearchFilter<RulesetInfo>
{
[BackgroundDependencyLoader]
private void load(RulesetStore rulesets)
{
AddItem(new RulesetInfo
{
Name = @"Any"
});
foreach (var r in rulesets.AvailableRulesets)
AddItem(r);
}
protected override TabItem<RulesetInfo> CreateTabItem(RulesetInfo value) => new RulesetTabItem(value);
private class RulesetTabItem : FilterTabItem
{
public RulesetTabItem(RulesetInfo value)
: base(value)
{
}
protected override string GetText(RulesetInfo value) => value.Name;
}
}
}