1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-19 10:52:55 +08:00

Refactor ChannelList to use new ChannelGroup class for each type of channel

This commit is contained in:
Jai Sharma 2022-05-27 19:46:53 +01:00
parent 320b6ca631
commit e950c8c1d0

View File

@ -26,18 +26,20 @@ namespace osu.Game.Overlays.Chat.ChannelList
public Action<Channel>? OnRequestSelect; public Action<Channel>? OnRequestSelect;
public Action<Channel>? OnRequestLeave; public Action<Channel>? OnRequestLeave;
public IEnumerable<Channel> Channels => public IEnumerable<Channel> Channels => groupFlow.Children.Where(child => child is ChannelGroup)
announceChannelFlow.Channels.Concat(publicChannelFlow.Channels).Concat(privateChannelFlow.Channels); .Cast<ChannelGroup>()
.SelectMany(channelGroup => channelGroup.ItemFlow)
.Select(item => item.Channel);
public readonly ChannelListing.ChannelListingChannel ChannelListingChannel = new ChannelListing.ChannelListingChannel(); public readonly ChannelListing.ChannelListingChannel ChannelListingChannel = new ChannelListing.ChannelListingChannel();
private readonly Dictionary<Channel, ChannelListItem> channelMap = new Dictionary<Channel, ChannelListItem>(); private readonly Dictionary<Channel, ChannelListItem> channelMap = new Dictionary<Channel, ChannelListItem>();
private OsuScrollContainer scroll = null!; private OsuScrollContainer scroll = null!;
private ChannelListLabel announceChannelLabel = null!; private FillFlowContainer groupFlow = null!;
private ChannelListItemFlow announceChannelFlow = null!; private ChannelGroup announceChannelGroup = null!;
private ChannelListItemFlow publicChannelFlow = null!; private ChannelGroup publicChannelGroup = null!;
private ChannelListItemFlow privateChannelFlow = null!; private ChannelGroup privateChannelGroup = null!;
private ChannelListItem selector = null!; private ChannelListItem selector = null!;
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
@ -55,20 +57,17 @@ namespace osu.Game.Overlays.Chat.ChannelList
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
ScrollbarAnchor = Anchor.TopRight, ScrollbarAnchor = Anchor.TopRight,
ScrollDistance = 35f, ScrollDistance = 35f,
Child = new FillFlowContainer Child = groupFlow = new FillFlowContainer
{ {
Direction = FillDirection.Vertical, Direction = FillDirection.Vertical,
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y, AutoSizeAxes = Axes.Y,
Children = new Drawable[] Children = new Drawable[]
{ {
announceChannelLabel = new ChannelListLabel(ChatStrings.ChannelsListTitleANNOUNCE.ToUpper()), announceChannelGroup = new ChannelGroup(ChatStrings.ChannelsListTitleANNOUNCE.ToUpper()),
announceChannelFlow = new ChannelListItemFlow(), publicChannelGroup = new ChannelGroup(ChatStrings.ChannelsListTitlePUBLIC.ToUpper()),
new ChannelListLabel(ChatStrings.ChannelsListTitlePUBLIC.ToUpper()),
publicChannelFlow = new ChannelListItemFlow(),
selector = new ChannelListItem(ChannelListingChannel), selector = new ChannelListItem(ChannelListingChannel),
new ChannelListLabel(ChatStrings.ChannelsListTitlePM.ToUpper()), privateChannelGroup = new ChannelGroup(ChatStrings.ChannelsListTitlePM.ToUpper()),
privateChannelFlow = new ChannelListItemFlow(),
}, },
}, },
}, },
@ -86,7 +85,7 @@ namespace osu.Game.Overlays.Chat.ChannelList
item.OnRequestSelect += chan => OnRequestSelect?.Invoke(chan); item.OnRequestSelect += chan => OnRequestSelect?.Invoke(chan);
item.OnRequestLeave += chan => OnRequestLeave?.Invoke(chan); item.OnRequestLeave += chan => OnRequestLeave?.Invoke(chan);
ChannelListItemFlow flow = getFlowForChannel(channel); FillFlowContainer<ChannelListItem> flow = getFlowForChannel(channel);
channelMap.Add(channel, item); channelMap.Add(channel, item);
flow.Add(item); flow.Add(item);
@ -99,7 +98,7 @@ namespace osu.Game.Overlays.Chat.ChannelList
return; return;
ChannelListItem item = channelMap[channel]; ChannelListItem item = channelMap[channel];
ChannelListItemFlow flow = getFlowForChannel(channel); FillFlowContainer<ChannelListItem> flow = getFlowForChannel(channel);
channelMap.Remove(channel); channelMap.Remove(channel);
flow.Remove(item); flow.Remove(item);
@ -117,57 +116,60 @@ namespace osu.Game.Overlays.Chat.ChannelList
public void ScrollChannelIntoView(Channel channel) => scroll.ScrollIntoView(GetItem(channel)); public void ScrollChannelIntoView(Channel channel) => scroll.ScrollIntoView(GetItem(channel));
private ChannelListItemFlow getFlowForChannel(Channel channel) private FillFlowContainer<ChannelListItem> getFlowForChannel(Channel channel)
{ {
switch (channel.Type) switch (channel.Type)
{ {
case ChannelType.Public: case ChannelType.Public:
return publicChannelFlow; return publicChannelGroup.ItemFlow;
case ChannelType.PM: case ChannelType.PM:
return privateChannelFlow; return privateChannelGroup.ItemFlow;
case ChannelType.Announce: case ChannelType.Announce:
return announceChannelFlow; return announceChannelGroup.ItemFlow;
default: default:
return publicChannelFlow; return publicChannelGroup.ItemFlow;
} }
} }
private void updateVisibility() private void updateVisibility()
{ {
if (announceChannelFlow.Channels.Count() == 0) if (announceChannelGroup.ItemFlow.Children.Count == 0)
{ announceChannelGroup.Hide();
announceChannelLabel.Hide();
announceChannelFlow.Hide();
}
else else
{ announceChannelGroup.Show();
announceChannelLabel.Show();
announceChannelFlow.Show();
}
} }
private class ChannelListLabel : OsuSpriteText private class ChannelGroup : FillFlowContainer
{ {
public ChannelListLabel(LocalisableString label) public FillFlowContainer<ChannelListItem> ItemFlow => flow;
{
Text = label;
Margin = new MarginPadding { Left = 18, Bottom = 5, Top = 8 };
Font = OsuFont.Torus.With(size: 12, weight: FontWeight.SemiBold);
}
}
private class ChannelListItemFlow : FillFlowContainer<ChannelListItem> private readonly FillFlowContainer<ChannelListItem> flow;
{
public IEnumerable<Channel> Channels => Children.Select(c => c.Channel);
public ChannelListItemFlow() public ChannelGroup(LocalisableString label)
{ {
Direction = FillDirection.Vertical; Direction = FillDirection.Vertical;
RelativeSizeAxes = Axes.X; RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y; AutoSizeAxes = Axes.Y;
Padding = new MarginPadding { Top = 8 };
Children = new Drawable[]
{
new OsuSpriteText
{
Text = label,
Margin = new MarginPadding { Left = 18, Bottom = 5 },
Font = OsuFont.Torus.With(size: 12, weight: FontWeight.SemiBold),
},
flow = new FillFlowContainer<ChannelListItem>
{
Direction = FillDirection.Vertical,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
},
};
} }
} }
} }