1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 15:07:44 +08:00

Refactor with including headers

This commit is contained in:
Andrei Zavatski 2020-02-17 00:44:55 +03:00
parent 7207548e47
commit ea285fd005
7 changed files with 246 additions and 162 deletions

View File

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
@ -17,29 +18,40 @@ namespace osu.Game.Tests.Visual.UserInterface
{
public override IReadOnlyList<Type> RequiredTypes => new[]
{
typeof(BeatmapSearchFilter<>),
typeof(BeatmapSearchRulesetFilter)
typeof(BeatmapSearchFilterRow<>),
typeof(BeatmapSearchRulesetFilterRow),
typeof(BeatmapSearchSmallFilterRow<>),
};
[Cached]
private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Blue);
private readonly FillFlowContainer resizableContainer;
public TestSceneBeatmapSearchFilter()
{
Add(new FillFlowContainer
Add(resizableContainer = new FillFlowContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.Both,
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 10),
Children = new Drawable[]
{
new BeatmapSearchRulesetFilter(),
new BeatmapSearchFilter<BeatmapSearchCategory>(),
new SmallBeatmapSearchFilter<BeatmapSearchCategory>()
new BeatmapSearchRulesetFilterRow(),
new BeatmapSearchFilterRow<BeatmapSearchCategory>("Categories"),
new BeatmapSearchSmallFilterRow<BeatmapSearchCategory>("Header Name")
}
});
}
[Test]
public void TestResize()
{
AddStep("Resize to 0.3", () => resizableContainer.ResizeWidthTo(0.3f, 1000));
AddStep("Resize to 1", () => resizableContainer.ResizeWidthTo(1, 1000));
}
}
}

View File

@ -1,97 +0,0 @@
// 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;
namespace osu.Game.Overlays.BeatmapListing
{
public class BeatmapSearchFilter<T> : TabControl<T>
{
public BeatmapSearchFilter()
{
AutoSizeAxes = Axes.Both;
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.Both,
Spacing = new Vector2(10),
};
protected class FilterTabItem : TabItem<T>
{
protected virtual float TextSize => 13;
[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: TextSize, 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.FadeColour(Active.Value ? Color4.White : getStateColour(), 200, Easing.OutQuint);
private Color4 getStateColour() => IsHovered ? colourProvider.Light1 : colourProvider.Light3;
}
}
}

View File

@ -0,0 +1,149 @@
// 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 JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
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 osuTK;
using osuTK.Graphics;
namespace osu.Game.Overlays.BeatmapListing
{
public class BeatmapSearchFilterRow<T> : CompositeDrawable, IHasCurrentValue<T>
{
private readonly BindableWithCurrent<T> current = new BindableWithCurrent<T>();
public Bindable<T> Current
{
get => current.Current;
set => current.Current = value;
}
public BeatmapSearchFilterRow(string headerName)
{
AutoSizeAxes = Axes.Y;
RelativeSizeAxes = Axes.X;
AddInternal(new FillFlowContainer
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Spacing = new Vector2(0, 7),
Children = new Drawable[]
{
new Container
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
AutoSizeAxes = Axes.Y,
Width = 100,
Child = new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.GetFont(size: 10),
Text = headerName.ToUpper()
}
},
CreateFilter().With(f =>
{
f.Anchor = Anchor.CentreLeft;
f.Origin = Anchor.CentreLeft;
f.Current = current;
})
}
});
}
[NotNull]
protected virtual BeatmapSearchFilter CreateFilter() => new BeatmapSearchFilter();
protected class BeatmapSearchFilter : TabControl<T>
{
public BeatmapSearchFilter()
{
AutoSizeAxes = Axes.Both;
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.Both,
Spacing = new Vector2(10),
};
protected class FilterTabItem : TabItem<T>
{
protected virtual float TextSize => 13;
[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: TextSize, 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.FadeColour(Active.Value ? Color4.White : getStateColour(), 200, Easing.OutQuint);
private Color4 getStateColour() => IsHovered ? colourProvider.Light1 : colourProvider.Light3;
}
}
}
}

View File

@ -1,36 +0,0 @@
// 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;
}
}
}

View File

@ -0,0 +1,46 @@
// 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.Framework.Graphics.UserInterface;
using osu.Game.Rulesets;
namespace osu.Game.Overlays.BeatmapListing
{
public class BeatmapSearchRulesetFilterRow : BeatmapSearchFilterRow<RulesetInfo>
{
public BeatmapSearchRulesetFilterRow()
: base(@"Mode")
{
}
protected override BeatmapSearchFilter CreateFilter() => new RulesetFilter();
private class RulesetFilter : BeatmapSearchFilter
{
[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;
}
}
}
}

View File

@ -0,0 +1,32 @@
// 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.UserInterface;
namespace osu.Game.Overlays.BeatmapListing
{
public class BeatmapSearchSmallFilterRow<T> : BeatmapSearchFilterRow<T>
{
public BeatmapSearchSmallFilterRow(string headerName)
: base(headerName)
{
}
protected override BeatmapSearchFilter CreateFilter() => new SmallBeatmapSearchFilter();
private class SmallBeatmapSearchFilter : BeatmapSearchFilter
{
protected override TabItem<T> CreateTabItem(T value) => new SmallTabItem(value);
private class SmallTabItem : FilterTabItem
{
public SmallTabItem(T value)
: base(value)
{
}
protected override float TextSize => 10;
}
}
}
}

View File

@ -1,22 +0,0 @@
// 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.UserInterface;
namespace osu.Game.Overlays.BeatmapListing
{
public class SmallBeatmapSearchFilter<T> : BeatmapSearchFilter<T>
{
protected override TabItem<T> CreateTabItem(T value) => new SmallTabItem(value);
protected class SmallTabItem : FilterTabItem
{
public SmallTabItem(T value)
: base(value)
{
}
protected override float TextSize => 10;
}
}
}