2019-01-24 16:43:03 +08:00
|
|
|
|
// 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.
|
2018-07-10 01:42:57 +08:00
|
|
|
|
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
|
using osu.Game.Online.Chat;
|
2018-11-20 15:51:59 +08:00
|
|
|
|
using osuTK;
|
2018-07-10 01:42:57 +08:00
|
|
|
|
using System;
|
2018-07-30 03:18:37 +08:00
|
|
|
|
using System.Linq;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2020-01-26 05:16:21 +08:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2018-07-10 01:42:57 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Chat.Tabs
|
|
|
|
|
{
|
|
|
|
|
public class ChannelTabControl : OsuTabControl<Channel>
|
|
|
|
|
{
|
2019-11-12 20:07:01 +08:00
|
|
|
|
public const float SHEAR_WIDTH = 10;
|
2018-07-10 01:42:57 +08:00
|
|
|
|
|
|
|
|
|
public Action<Channel> OnRequestLeave;
|
|
|
|
|
|
|
|
|
|
public readonly Bindable<bool> ChannelSelectorActive = new Bindable<bool>();
|
|
|
|
|
|
|
|
|
|
private readonly ChannelSelectorTabItem selectorTab;
|
|
|
|
|
|
|
|
|
|
public ChannelTabControl()
|
|
|
|
|
{
|
2019-10-07 01:22:55 +08:00
|
|
|
|
Padding = new MarginPadding { Left = 50 };
|
|
|
|
|
|
2018-07-10 04:59:29 +08:00
|
|
|
|
TabContainer.Spacing = new Vector2(-SHEAR_WIDTH, 0);
|
2018-07-10 01:42:57 +08:00
|
|
|
|
TabContainer.Masking = false;
|
|
|
|
|
|
2019-05-12 18:26:03 +08:00
|
|
|
|
AddTabItem(selectorTab = new ChannelSelectorTabItem());
|
2018-07-10 01:42:57 +08:00
|
|
|
|
|
|
|
|
|
ChannelSelectorActive.BindTo(selectorTab.Active);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void AddTabItem(TabItem<Channel> item, bool addToDropdown = true)
|
|
|
|
|
{
|
|
|
|
|
if (item != selectorTab && TabContainer.GetLayoutPosition(selectorTab) < float.MaxValue)
|
|
|
|
|
// performTabSort might've made selectorTab's position wonky, fix it
|
|
|
|
|
TabContainer.SetLayoutPosition(selectorTab, float.MaxValue);
|
|
|
|
|
|
2020-03-14 01:33:06 +08:00
|
|
|
|
((ChannelTabItem)item).OnRequestClose += channelItem => OnRequestLeave?.Invoke(channelItem.Value);
|
2019-06-26 18:25:54 +08:00
|
|
|
|
|
2018-07-10 01:42:57 +08:00
|
|
|
|
base.AddTabItem(item, addToDropdown);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override TabItem<Channel> CreateTabItem(Channel value)
|
|
|
|
|
{
|
2018-11-13 14:20:40 +08:00
|
|
|
|
switch (value.Type)
|
2018-07-10 01:42:57 +08:00
|
|
|
|
{
|
2018-11-20 14:03:55 +08:00
|
|
|
|
default:
|
2019-06-26 18:25:54 +08:00
|
|
|
|
return new ChannelTabItem(value);
|
2019-04-01 11:44:46 +08:00
|
|
|
|
|
2018-11-13 14:20:40 +08:00
|
|
|
|
case ChannelType.PM:
|
2019-06-26 18:25:54 +08:00
|
|
|
|
return new PrivateChannelTabItem(value);
|
2018-07-10 01:42:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-30 03:18:37 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a channel to the ChannelTabControl.
|
|
|
|
|
/// The first channel added will automaticly selected.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="channel">The channel that is going to be added.</param>
|
|
|
|
|
public void AddChannel(Channel channel)
|
|
|
|
|
{
|
|
|
|
|
if (!Items.Contains(channel))
|
|
|
|
|
AddItem(channel);
|
|
|
|
|
|
2020-06-03 15:48:44 +08:00
|
|
|
|
Current.Value ??= channel;
|
2018-07-30 03:18:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes a channel from the ChannelTabControl.
|
2020-03-14 01:33:06 +08:00
|
|
|
|
/// If the selected channel is the one that is being removed, the next available channel will be selected.
|
2018-07-30 03:18:37 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="channel">The channel that is going to be removed.</param>
|
|
|
|
|
public void RemoveChannel(Channel channel)
|
|
|
|
|
{
|
|
|
|
|
if (Current.Value == channel)
|
2020-01-16 17:34:56 +08:00
|
|
|
|
{
|
2020-03-14 01:33:06 +08:00
|
|
|
|
var allChannels = TabContainer.AllTabItems.Select(tab => tab.Value).ToList();
|
|
|
|
|
var isNextTabSelector = allChannels[allChannels.IndexOf(channel) + 1] == selectorTab.Value;
|
|
|
|
|
|
|
|
|
|
// selectorTab is not switchable, so we have to explicitly select it if it's the only tab left
|
|
|
|
|
if (isNextTabSelector && allChannels.Count == 2)
|
|
|
|
|
SelectTab(selectorTab);
|
|
|
|
|
else
|
|
|
|
|
SwitchTab(isNextTabSelector ? -1 : 1);
|
2020-01-16 17:34:56 +08:00
|
|
|
|
}
|
2020-03-14 01:33:06 +08:00
|
|
|
|
|
|
|
|
|
RemoveItem(channel);
|
2018-07-30 03:18:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 01:42:57 +08:00
|
|
|
|
protected override void SelectTab(TabItem<Channel> tab)
|
|
|
|
|
{
|
|
|
|
|
if (tab is ChannelSelectorTabItem)
|
|
|
|
|
{
|
2018-12-06 19:56:33 +08:00
|
|
|
|
tab.Active.Value = true;
|
2018-07-10 01:42:57 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.SelectTab(tab);
|
2018-12-06 19:56:33 +08:00
|
|
|
|
selectorTab.Active.Value = false;
|
2018-07-10 01:42:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-26 05:16:21 +08:00
|
|
|
|
protected override TabFillFlowContainer CreateTabFlow() => new ChannelTabFillFlowContainer
|
|
|
|
|
{
|
|
|
|
|
Direction = FillDirection.Full,
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Depth = -1,
|
|
|
|
|
Masking = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private class ChannelTabFillFlowContainer : TabFillFlowContainer
|
|
|
|
|
{
|
|
|
|
|
protected override int Compare(Drawable x, Drawable y) => CompareReverseChildID(x, y);
|
|
|
|
|
}
|
2018-07-10 01:42:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|