Enum constraint for enum dropdown.

This commit is contained in:
Huo Yaoyuan
2019-12-07 19:56:56 +08:00
parent 40a5c1fd96
commit c3518a2b94
7 changed files with 26 additions and 21 deletions

View File

@ -6,12 +6,10 @@ using System;
namespace osu.Game.Graphics.UserInterface namespace osu.Game.Graphics.UserInterface
{ {
public class OsuEnumDropdown<T> : OsuDropdown<T> public class OsuEnumDropdown<T> : OsuDropdown<T>
where T : struct, Enum
{ {
public OsuEnumDropdown() public OsuEnumDropdown()
{ {
if (!typeof(T).IsEnum)
throw new InvalidOperationException("OsuEnumDropdown only supports enums as the generic type argument");
Items = (T[])Enum.GetValues(typeof(T)); Items = (T[])Enum.GetValues(typeof(T));
} }
} }

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osuTK; using osuTK;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -11,6 +12,7 @@ using osu.Game.Graphics.Containers;
namespace osu.Game.Overlays.SearchableList namespace osu.Game.Overlays.SearchableList
{ {
public class DisplayStyleControl<T> : Container public class DisplayStyleControl<T> : Container
where T : struct, Enum
{ {
public readonly SlimEnumDropdown<T> Dropdown; public readonly SlimEnumDropdown<T> Dropdown;
public readonly Bindable<PanelDisplayStyle> DisplayStyle = new Bindable<PanelDisplayStyle>(); public readonly Bindable<PanelDisplayStyle> DisplayStyle = new Bindable<PanelDisplayStyle>();

View File

@ -13,7 +13,9 @@ using osu.Framework.Graphics.Shapes;
namespace osu.Game.Overlays.SearchableList namespace osu.Game.Overlays.SearchableList
{ {
public abstract class SearchableListFilterControl<T, U> : Container public abstract class SearchableListFilterControl<TTab, TCategory> : Container
where TTab : struct, Enum
where TCategory : struct, Enum
{ {
private const float padding = 10; private const float padding = 10;
@ -21,12 +23,12 @@ namespace osu.Game.Overlays.SearchableList
private readonly Box tabStrip; private readonly Box tabStrip;
public readonly SearchTextBox Search; public readonly SearchTextBox Search;
public readonly PageTabControl<T> Tabs; public readonly PageTabControl<TTab> Tabs;
public readonly DisplayStyleControl<U> DisplayStyleControl; public readonly DisplayStyleControl<TCategory> DisplayStyleControl;
protected abstract Color4 BackgroundColour { get; } protected abstract Color4 BackgroundColour { get; }
protected abstract T DefaultTab { get; } protected abstract TTab DefaultTab { get; }
protected abstract U DefaultCategory { get; } protected abstract TCategory DefaultCategory { get; }
protected virtual Drawable CreateSupplementaryControls() => null; protected virtual Drawable CreateSupplementaryControls() => null;
/// <summary> /// <summary>
@ -36,9 +38,6 @@ namespace osu.Game.Overlays.SearchableList
protected SearchableListFilterControl() protected SearchableListFilterControl()
{ {
if (!typeof(T).IsEnum)
throw new InvalidOperationException("SearchableListFilterControl's sort tabs only support enums as the generic type argument");
RelativeSizeAxes = Axes.X; RelativeSizeAxes = Axes.X;
var controls = CreateSupplementaryControls(); var controls = CreateSupplementaryControls();
@ -90,7 +89,7 @@ namespace osu.Game.Overlays.SearchableList
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y, AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Right = 225 }, Padding = new MarginPadding { Right = 225 },
Child = Tabs = new PageTabControl<T> Child = Tabs = new PageTabControl<TTab>
{ {
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
}, },
@ -105,7 +104,7 @@ namespace osu.Game.Overlays.SearchableList
}, },
}, },
}, },
DisplayStyleControl = new DisplayStyleControl<U> DisplayStyleControl = new DisplayStyleControl<TCategory>
{ {
Anchor = Anchor.TopRight, Anchor = Anchor.TopRight,
Origin = Anchor.TopRight, Origin = Anchor.TopRight,

View File

@ -14,6 +14,7 @@ using osu.Framework.Graphics.Sprites;
namespace osu.Game.Overlays.SearchableList namespace osu.Game.Overlays.SearchableList
{ {
public abstract class SearchableListHeader<T> : Container public abstract class SearchableListHeader<T> : Container
where T : struct, Enum
{ {
public readonly HeaderTabControl<T> Tabs; public readonly HeaderTabControl<T> Tabs;
@ -24,9 +25,6 @@ namespace osu.Game.Overlays.SearchableList
protected SearchableListHeader() protected SearchableListHeader()
{ {
if (!typeof(T).IsEnum)
throw new InvalidOperationException("BrowseHeader only supports enums as the generic type argument");
RelativeSizeAxes = Axes.X; RelativeSizeAxes = Axes.X;
Height = 90; Height = 90;

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using osuTK.Graphics; using osuTK.Graphics;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
@ -16,19 +17,22 @@ namespace osu.Game.Overlays.SearchableList
public const float WIDTH_PADDING = 80; public const float WIDTH_PADDING = 80;
} }
public abstract class SearchableListOverlay<T, U, S> : SearchableListOverlay public abstract class SearchableListOverlay<THeader, TTab, TCategory> : SearchableListOverlay
where THeader : struct, Enum
where TTab : struct, Enum
where TCategory : struct, Enum
{ {
private readonly Container scrollContainer; private readonly Container scrollContainer;
protected readonly SearchableListHeader<T> Header; protected readonly SearchableListHeader<THeader> Header;
protected readonly SearchableListFilterControl<U, S> Filter; protected readonly SearchableListFilterControl<TTab, TCategory> Filter;
protected readonly FillFlowContainer ScrollFlow; protected readonly FillFlowContainer ScrollFlow;
protected abstract Color4 BackgroundColour { get; } protected abstract Color4 BackgroundColour { get; }
protected abstract Color4 TrianglesColourLight { get; } protected abstract Color4 TrianglesColourLight { get; }
protected abstract Color4 TrianglesColourDark { get; } protected abstract Color4 TrianglesColourDark { get; }
protected abstract SearchableListHeader<T> CreateHeader(); protected abstract SearchableListHeader<THeader> CreateHeader();
protected abstract SearchableListFilterControl<U, S> CreateFilterControl(); protected abstract SearchableListFilterControl<TTab, TCategory> CreateFilterControl();
protected SearchableListOverlay() protected SearchableListOverlay()
{ {

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using osuTK.Graphics; using osuTK.Graphics;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -11,6 +12,7 @@ using osuTK;
namespace osu.Game.Overlays.SearchableList namespace osu.Game.Overlays.SearchableList
{ {
public class SlimEnumDropdown<T> : OsuEnumDropdown<T> public class SlimEnumDropdown<T> : OsuEnumDropdown<T>
where T : struct, Enum
{ {
protected override DropdownHeader CreateHeader() => new SlimDropdownHeader(); protected override DropdownHeader CreateHeader() => new SlimDropdownHeader();

View File

@ -1,12 +1,14 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
namespace osu.Game.Overlays.Settings namespace osu.Game.Overlays.Settings
{ {
public class SettingsEnumDropdown<T> : SettingsDropdown<T> public class SettingsEnumDropdown<T> : SettingsDropdown<T>
where T : struct, Enum
{ {
protected override OsuDropdown<T> CreateDropdown() => new DropdownControl(); protected override OsuDropdown<T> CreateDropdown() => new DropdownControl();