2018-07-10 01:42:57 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
|
|
|
|
using osu.Game.Graphics;
|
|
|
|
|
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 osu.Framework.Configuration;
|
|
|
|
|
using System;
|
2018-07-30 03:18:37 +08:00
|
|
|
|
using System.Linq;
|
2018-07-10 01:42:57 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Chat.Tabs
|
|
|
|
|
{
|
|
|
|
|
public class ChannelTabControl : OsuTabControl<Channel>
|
|
|
|
|
{
|
2018-07-10 04:59:29 +08:00
|
|
|
|
public static readonly 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()
|
|
|
|
|
{
|
|
|
|
|
TabContainer.Margin = 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;
|
|
|
|
|
|
|
|
|
|
AddInternal(new SpriteIcon
|
|
|
|
|
{
|
|
|
|
|
Icon = FontAwesome.fa_comments,
|
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
|
Origin = Anchor.CentreLeft,
|
|
|
|
|
Size = new Vector2(20),
|
|
|
|
|
Margin = new MarginPadding(10),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
AddTabItem(selectorTab = new ChannelSelectorTabItem(new Channel { Name = "+" }));
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
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:
|
2018-07-10 01:42:57 +08:00
|
|
|
|
return new ChannelTabItem(value) { OnRequestClose = tabCloseRequested };
|
2018-11-13 14:20:40 +08:00
|
|
|
|
case ChannelType.PM:
|
2018-07-24 11:17:57 +08:00
|
|
|
|
return new PrivateChannelTabItem(value) { OnRequestClose = tabCloseRequested };
|
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);
|
|
|
|
|
|
|
|
|
|
if (Current.Value == null)
|
|
|
|
|
Current.Value = channel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes a channel from the ChannelTabControl.
|
|
|
|
|
/// If the selected channel is the one that is beeing removed, the next available channel will be selected.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="channel">The channel that is going to be removed.</param>
|
|
|
|
|
public void RemoveChannel(Channel channel)
|
|
|
|
|
{
|
|
|
|
|
RemoveItem(channel);
|
|
|
|
|
|
|
|
|
|
if (Current.Value == channel)
|
|
|
|
|
Current.Value = Items.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void tabCloseRequested(TabItem<Channel> tab)
|
|
|
|
|
{
|
|
|
|
|
int totalTabs = TabContainer.Count - 1; // account for selectorTab
|
|
|
|
|
int currentIndex = MathHelper.Clamp(TabContainer.IndexOf(tab), 1, totalTabs);
|
|
|
|
|
|
|
|
|
|
if (tab == SelectedTab && totalTabs > 1)
|
|
|
|
|
// Select the tab after tab-to-be-removed's index, or the tab before if current == last
|
|
|
|
|
SelectTab(TabContainer[currentIndex == totalTabs ? currentIndex - 1 : currentIndex + 1]);
|
|
|
|
|
else if (totalTabs == 1 && !selectorTab.Active)
|
|
|
|
|
// Open channel selection overlay if all channel tabs will be closed after removing this tab
|
|
|
|
|
SelectTab(selectorTab);
|
|
|
|
|
|
|
|
|
|
OnRequestLeave?.Invoke(tab.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|