osu/osu.Game/Overlays/Chat/ChatTabControl.cs

335 lines
12 KiB
C#
Raw Normal View History

2018-01-05 11:21:19 +00:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
2017-05-12 06:32:52 +00:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
2017-05-12 06:32:52 +00:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.Chat;
using OpenTK;
using OpenTK.Graphics;
using osu.Framework.Configuration;
2017-08-03 15:25:52 +00:00
using System;
using osu.Game.Graphics.Containers;
2017-05-12 06:32:52 +00:00
namespace osu.Game.Overlays.Chat
{
public class ChatTabControl : OsuTabControl<Channel>
{
private const float shear_width = 10;
2017-08-03 15:25:52 +00:00
public Action<Channel> OnRequestLeave;
2017-05-26 10:10:16 +00:00
public readonly Bindable<bool> ChannelSelectorActive = new Bindable<bool>();
2017-06-12 09:39:22 +00:00
private readonly ChannelTabItem.ChannelSelectorTabItem selectorTab;
2017-05-12 06:32:52 +00:00
public ChatTabControl()
{
TabContainer.Margin = new MarginPadding { Left = 50 };
TabContainer.Spacing = new Vector2(-shear_width, 0);
TabContainer.Masking = false;
AddInternal(new SpriteIcon
2017-05-12 06:32:52 +00:00
{
Icon = FontAwesome.fa_comments,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Size = new Vector2(20),
Margin = new MarginPadding(10),
2017-05-12 06:32:52 +00:00
});
2017-05-17 19:16:53 +00:00
AddTabItem(selectorTab = new ChannelTabItem.ChannelSelectorTabItem(new Channel { Name = "+" }));
2017-06-12 09:39:22 +00:00
ChannelSelectorActive.BindTo(selectorTab.Active);
}
protected override void AddTabItem(TabItem<Channel> item, bool addToDropdown = true)
{
if (selectorTab.Depth < float.MaxValue)
// performTabSort might've made selectorTab's position wonky, fix it
TabContainer.ChangeChildDepth(selectorTab, float.MaxValue);
base.AddTabItem(item, addToDropdown);
if (SelectedTab == null)
SelectTab(item);
}
2017-09-06 14:12:32 +00:00
protected override TabItem<Channel> CreateTabItem(Channel value) => new ChannelTabItem(value) { OnRequestClose = tabCloseRequested };
2017-08-03 15:25:52 +00:00
2017-06-12 09:39:22 +00:00
protected override void SelectTab(TabItem<Channel> tab)
{
if (tab is ChannelTabItem.ChannelSelectorTabItem)
{
tab.Active.Toggle();
return;
}
selectorTab.Active.Value = false;
base.SelectTab(tab);
2017-05-12 06:32:52 +00:00
}
2017-09-06 14:12:32 +00: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);
}
2017-05-12 06:32:52 +00:00
private class ChannelTabItem : TabItem<Channel>
{
private Color4 backgroundInactive;
private Color4 backgroundHover;
private Color4 backgroundActive;
public override bool IsRemovable => !Pinned;
2017-08-03 15:25:52 +00:00
2017-05-12 06:32:52 +00:00
private readonly SpriteText text;
2017-05-16 08:37:54 +00:00
private readonly SpriteText textBold;
2017-09-06 12:43:20 +00:00
private readonly ClickableContainer closeButton;
2017-05-12 06:32:52 +00:00
private readonly Box box;
private readonly Box highlightBox;
private readonly SpriteIcon icon;
2017-05-12 06:32:52 +00:00
2017-09-06 13:58:21 +00:00
public Action<ChannelTabItem> OnRequestClose;
2017-08-03 15:25:52 +00:00
2017-05-12 06:32:52 +00:00
private void updateState()
{
if (Active)
fadeActive();
else
fadeInactive();
}
private const float transition_length = 400;
private void fadeActive()
{
2017-07-22 18:50:25 +00:00
this.ResizeTo(new Vector2(Width, 1.1f), transition_length, Easing.OutQuint);
2017-05-12 06:32:52 +00:00
2017-07-22 18:50:25 +00:00
box.FadeColour(backgroundActive, transition_length, Easing.OutQuint);
highlightBox.FadeIn(transition_length, Easing.OutQuint);
2017-05-16 08:37:54 +00:00
2017-07-22 18:50:25 +00:00
text.FadeOut(transition_length, Easing.OutQuint);
textBold.FadeIn(transition_length, Easing.OutQuint);
2017-05-12 06:32:52 +00:00
}
private void fadeInactive()
{
2017-07-22 18:50:25 +00:00
this.ResizeTo(new Vector2(Width, 1), transition_length, Easing.OutQuint);
2017-05-12 06:32:52 +00:00
2017-07-22 18:50:25 +00:00
box.FadeColour(backgroundInactive, transition_length, Easing.OutQuint);
highlightBox.FadeOut(transition_length, Easing.OutQuint);
2017-05-16 08:37:54 +00:00
2017-07-22 18:50:25 +00:00
text.FadeIn(transition_length, Easing.OutQuint);
textBold.FadeOut(transition_length, Easing.OutQuint);
2017-05-12 06:32:52 +00:00
}
protected override bool OnHover(InputState state)
{
if (IsRemovable)
2017-09-06 12:43:20 +00:00
closeButton.FadeIn(200, Easing.OutQuint);
2017-05-12 06:32:52 +00:00
if (!Active)
2017-07-22 18:50:25 +00:00
box.FadeColour(backgroundHover, transition_length, Easing.OutQuint);
2017-05-12 06:32:52 +00:00
return true;
}
protected override void OnHoverLost(InputState state)
{
2017-09-06 12:43:20 +00:00
closeButton.FadeOut(200, Easing.OutQuint);
2017-05-12 06:32:52 +00:00
updateState();
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
backgroundActive = colours.ChatBlue;
backgroundInactive = colours.Gray4;
backgroundHover = colours.Gray7;
highlightBox.Colour = colours.Yellow;
2017-05-26 10:10:16 +00:00
}
protected override void LoadComplete()
{
base.LoadComplete();
2017-05-12 06:32:52 +00:00
updateState();
}
public ChannelTabItem(Channel value) : base(value)
{
Width = 150;
RelativeSizeAxes = Axes.Y;
Anchor = Anchor.BottomLeft;
Origin = Anchor.BottomLeft;
Shear = new Vector2(shear_width / ChatOverlay.TAB_AREA_HEIGHT, 0);
Masking = true;
2017-06-12 03:48:47 +00:00
EdgeEffect = new EdgeEffectParameters
2017-05-12 06:32:52 +00:00
{
Type = EdgeEffectType.Shadow,
Radius = 10,
Colour = Color4.Black.Opacity(0.2f),
};
Children = new Drawable[]
{
box = new Box
{
EdgeSmoothness = new Vector2(1, 0),
RelativeSizeAxes = Axes.Both,
},
highlightBox = new Box
{
Width = 5,
Alpha = 0,
Anchor = Anchor.BottomRight,
Origin = Anchor.BottomRight,
EdgeSmoothness = new Vector2(1, 0),
RelativeSizeAxes = Axes.Y,
},
new Container
{
Shear = new Vector2(-shear_width / ChatOverlay.TAB_AREA_HEIGHT, 0),
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
icon = new SpriteIcon
2017-05-12 06:32:52 +00:00
{
Icon = FontAwesome.fa_hashtag,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Colour = Color4.Black,
X = -10,
Alpha = 0.2f,
Size = new Vector2(ChatOverlay.TAB_AREA_HEIGHT),
2017-05-12 06:32:52 +00:00
},
text = new OsuSpriteText
{
Margin = new MarginPadding(5),
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
Text = value.ToString(),
TextSize = 18,
},
2017-05-16 08:37:54 +00:00
textBold = new OsuSpriteText
{
Alpha = 0,
Margin = new MarginPadding(5),
Origin = Anchor.CentreLeft,
Anchor = Anchor.CentreLeft,
Text = value.ToString(),
Font = @"Exo2.0-Bold",
TextSize = 18,
},
2017-09-06 12:43:20 +00:00
closeButton = new CloseButton
{
Alpha = 0,
2017-09-06 12:43:20 +00:00
Margin = new MarginPadding { Right = 20 },
Origin = Anchor.CentreRight,
Anchor = Anchor.CentreRight,
2017-09-06 12:43:20 +00:00
Action = delegate
{
2017-09-06 13:58:21 +00:00
if (IsRemovable) OnRequestClose?.Invoke(this);
2017-09-06 12:43:20 +00:00
},
},
2017-08-03 15:25:52 +00:00
},
},
2017-05-12 06:32:52 +00:00
};
}
2017-05-17 19:16:53 +00:00
public class CloseButton : OsuClickableContainer
2017-09-06 12:43:20 +00:00
{
2017-09-06 13:41:03 +00:00
private readonly SpriteIcon icon;
2017-09-06 12:43:20 +00:00
public CloseButton()
{
Size = new Vector2(20);
Child = icon = new SpriteIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Scale = new Vector2(0.75f),
Icon = FontAwesome.fa_close,
RelativeSizeAxes = Axes.Both,
};
}
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
{
icon.ScaleTo(0.5f, 1000, Easing.OutQuint);
return base.OnMouseDown(state, args);
}
protected override bool OnMouseUp(InputState state, MouseUpEventArgs args)
{
icon.ScaleTo(0.75f, 1000, Easing.OutElastic);
return base.OnMouseUp(state, args);
}
protected override bool OnHover(InputState state)
{
icon.FadeColour(Color4.Red, 200, Easing.OutQuint);
return base.OnHover(state);
}
protected override void OnHoverLost(InputState state)
{
icon.FadeColour(Color4.White, 200, Easing.OutQuint);
base.OnHoverLost(state);
}
}
2017-05-17 19:16:53 +00:00
public class ChannelSelectorTabItem : ChannelTabItem
{
public override bool IsRemovable => false;
2017-08-03 15:25:52 +00:00
2017-06-12 09:39:22 +00:00
public ChannelSelectorTabItem(Channel value) : base(value)
{
2017-05-17 19:16:53 +00:00
Depth = float.MaxValue;
2017-05-25 18:25:59 +00:00
Width = 45;
2017-05-17 19:16:53 +00:00
icon.Alpha = 0;
2017-05-25 18:25:59 +00:00
text.TextSize = 45;
textBold.TextSize = 45;
2017-05-17 19:16:53 +00:00
}
[BackgroundDependencyLoader]
private new void load(OsuColour colour)
{
2017-05-25 18:25:59 +00:00
backgroundInactive = colour.Gray2;
backgroundActive = colour.Gray3;
2017-05-17 19:16:53 +00:00
}
2017-06-12 09:39:22 +00:00
}
2017-06-12 09:39:22 +00:00
protected override void OnActivated() => updateState();
2017-06-12 09:39:22 +00:00
protected override void OnDeactivated() => updateState();
2017-05-12 06:32:52 +00:00
}
}
}