Implement OverlaySortTabControl component

This commit is contained in:
Andrei Zavatski 2020-02-17 02:20:53 +03:00
parent e80c310ab2
commit a9f366fda3
5 changed files with 159 additions and 135 deletions

View File

@ -24,7 +24,7 @@ public class TestSceneCommentsContainer : OsuTestScene
typeof(CommentsHeader),
typeof(DrawableComment),
typeof(HeaderButton),
typeof(SortTabControl),
typeof(OverlaySortTabControl<>),
typeof(ShowChildrenButton),
typeof(DeletedCommentsCounter),
typeof(VotePill),

View File

@ -18,7 +18,7 @@ public class TestSceneCommentsHeader : OsuTestScene
{
typeof(CommentsHeader),
typeof(HeaderButton),
typeof(SortTabControl),
typeof(OverlaySortTabControl<>),
};
[Cached]

View File

@ -16,8 +16,6 @@ namespace osu.Game.Overlays.Comments
{
public class CommentsHeader : CompositeDrawable
{
private const int font_size = 14;
public readonly Bindable<CommentsSortCriteria> Sort = new Bindable<CommentsSortCriteria>();
public readonly BindableBool ShowDeleted = new BindableBool();
@ -40,29 +38,11 @@ public CommentsHeader()
Padding = new MarginPadding { Horizontal = 50 },
Children = new Drawable[]
{
new FillFlowContainer
new OverlaySortTabControl<CommentsSortCriteria>
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(10, 0),
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Children = new Drawable[]
{
new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.GetFont(size: font_size),
Text = @"Sort by"
},
new SortTabControl
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Current = Sort
}
}
Current = Sort
},
new ShowDeletedButton
{
@ -106,7 +86,7 @@ public ShowDeletedButton()
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.GetFont(size: font_size),
Font = OsuFont.GetFont(size: 12),
Text = @"Show deleted"
}
},
@ -126,4 +106,11 @@ protected override bool OnClick(ClickEvent e)
}
}
}
public enum CommentsSortCriteria
{
New,
Old,
Top
}
}

View File

@ -1,110 +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.Containers;
using osu.Framework.Graphics;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics;
using osuTK;
using osu.Game.Graphics.UserInterface;
using osu.Framework.Input.Events;
using osu.Framework.Bindables;
using osu.Framework.Allocation;
using osu.Game.Graphics.Sprites;
using osuTK.Graphics;
namespace osu.Game.Overlays.Comments
{
public class SortTabControl : OsuTabControl<CommentsSortCriteria>
{
protected override Dropdown<CommentsSortCriteria> CreateDropdown() => null;
protected override TabItem<CommentsSortCriteria> CreateTabItem(CommentsSortCriteria value) => new SortTabItem(value);
protected override TabFillFlowContainer CreateTabFlow() => new TabFillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(5, 0),
};
public SortTabControl()
{
AutoSizeAxes = Axes.Both;
}
private class SortTabItem : TabItem<CommentsSortCriteria>
{
public SortTabItem(CommentsSortCriteria value)
: base(value)
{
AutoSizeAxes = Axes.Both;
Child = new TabButton(value) { Active = { BindTarget = Active } };
}
protected override void OnActivated()
{
}
protected override void OnDeactivated()
{
}
private class TabButton : HeaderButton
{
public readonly BindableBool Active = new BindableBool();
[Resolved]
private OverlayColourProvider colourProvider { get; set; }
private readonly SpriteText text;
public TabButton(CommentsSortCriteria value)
{
Add(text = new OsuSpriteText
{
Font = OsuFont.GetFont(size: 14),
Text = value.ToString()
});
}
protected override void LoadComplete()
{
base.LoadComplete();
Active.BindValueChanged(active =>
{
updateBackgroundState();
text.Font = text.Font.With(weight: active.NewValue ? FontWeight.Bold : FontWeight.Medium);
text.Colour = active.NewValue ? colourProvider.Light1 : Color4.White;
}, true);
}
protected override bool OnHover(HoverEvent e)
{
updateBackgroundState();
return true;
}
protected override void OnHoverLost(HoverLostEvent e) => updateBackgroundState();
private void updateBackgroundState()
{
if (Active.Value || IsHovered)
ShowBackground();
else
HideBackground();
}
}
}
}
public enum CommentsSortCriteria
{
New,
Old,
Top
}
}

View File

@ -0,0 +1,147 @@
// 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.Containers;
using osu.Framework.Graphics;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics;
using osuTK;
using osu.Game.Graphics.UserInterface;
using osu.Framework.Input.Events;
using osu.Framework.Bindables;
using osu.Framework.Allocation;
using osu.Game.Graphics.Sprites;
using osuTK.Graphics;
using osu.Game.Overlays.Comments;
using JetBrains.Annotations;
namespace osu.Game.Overlays
{
public class OverlaySortTabControl<T> : CompositeDrawable, IHasCurrentValue<T>
{
private readonly BindableWithCurrent<T> current = new BindableWithCurrent<T>();
public Bindable<T> Current
{
get => current.Current;
set => current.Current = value;
}
public OverlaySortTabControl()
{
AutoSizeAxes = Axes.Both;
AddInternal(new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(10, 0),
Children = new Drawable[]
{
new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.GetFont(size: 12),
Text = @"Sort by"
},
CreateControl().With(c =>
{
c.Anchor = Anchor.CentreLeft;
c.Origin = Anchor.CentreLeft;
c.Current = current;
})
}
});
}
[NotNull]
protected virtual SortTabControl CreateControl() => new SortTabControl();
protected class SortTabControl : OsuTabControl<T>
{
protected override Dropdown<T> CreateDropdown() => null;
protected override TabItem<T> CreateTabItem(T value) => new SortTabItem(value);
protected override TabFillFlowContainer CreateTabFlow() => new TabFillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(5, 0),
};
public SortTabControl()
{
AutoSizeAxes = Axes.Both;
}
protected class SortTabItem : TabItem<T>
{
public SortTabItem(T value)
: base(value)
{
AutoSizeAxes = Axes.Both;
Child = new TabButton(value) { Active = { BindTarget = Active } };
}
protected override void OnActivated()
{
}
protected override void OnDeactivated()
{
}
private class TabButton : HeaderButton
{
public readonly BindableBool Active = new BindableBool();
[Resolved]
private OverlayColourProvider colourProvider { get; set; }
private readonly SpriteText text;
public TabButton(T value)
{
Add(text = new OsuSpriteText
{
Font = OsuFont.GetFont(size: 12),
Text = value.ToString()
});
}
protected override void LoadComplete()
{
base.LoadComplete();
Active.BindValueChanged(_ => updateState(), true);
}
protected override bool OnHover(HoverEvent e)
{
updateHoverState();
return true;
}
protected override void OnHoverLost(HoverLostEvent e) => updateHoverState();
private void updateState()
{
updateHoverState();
text.Font = text.Font.With(weight: Active.Value ? FontWeight.Bold : FontWeight.Medium);
}
private void updateHoverState()
{
if (Active.Value || IsHovered)
ShowBackground();
else
HideBackground();
text.Colour = Active.Value && !IsHovered ? colourProvider.Light1 : Color4.White;
}
}
}
}
}
}