1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 08:07:24 +08:00
osu-lazer/osu.Game/Overlays/ChatOverlay.cs

430 lines
17 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +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 System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
2016-10-13 22:57:05 +08:00
using OpenTK;
2017-08-21 16:43:26 +08:00
using OpenTK.Graphics;
2016-11-09 07:13:20 +08:00
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Transforms;
2017-02-19 17:02:25 +08:00
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input;
using osu.Framework.MathUtils;
using osu.Game.Configuration;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
2017-08-21 16:43:26 +08:00
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.Chat;
using osu.Game.Overlays.Chat;
namespace osu.Game.Overlays
{
public class ChatOverlay : OsuFocusedOverlayContainer
{
2017-05-12 13:22:11 +08:00
private const float textbox_height = 60;
private const float channel_selection_min_height = 0.3f;
2017-02-22 12:38:22 +08:00
private ChatManager chatManager;
private readonly Container<DrawableChat> currentChannelContainer;
private readonly List<DrawableChat> loadedChannels = new List<DrawableChat>();
private readonly LoadingAnimation loading;
2017-08-21 16:43:26 +08:00
private readonly FocusedTextBox textbox;
2017-02-19 17:02:25 +08:00
2017-04-19 18:15:21 +08:00
private const int transition_length = 500;
public const float DEFAULT_HEIGHT = 0.4f;
2017-05-12 14:32:52 +08:00
public const float TAB_AREA_HEIGHT = 50;
2017-05-12 13:22:11 +08:00
2018-04-09 04:12:57 +08:00
private readonly ChannelTabControl channelTabs;
private readonly UserChatTabControl userTabs;
2017-05-12 14:32:52 +08:00
private readonly Container chatContainer;
private readonly Container tabsArea;
2017-05-12 14:32:52 +08:00
private readonly Box chatBackground;
private readonly Box tabBackground;
2017-11-21 11:11:29 +08:00
public Bindable<double> ChatHeight { get; set; }
private readonly Container channelSelectionContainer;
private readonly ChannelSelectionOverlay channelSelection;
public override bool Contains(Vector2 screenSpacePos) => chatContainer.ReceiveMouseInputAt(screenSpacePos) || channelSelection.State == Visibility.Visible && channelSelection.ReceiveMouseInputAt(screenSpacePos);
public ChatOverlay()
{
RelativeSizeAxes = Axes.Both;
RelativePositionAxes = Axes.Both;
Anchor = Anchor.BottomLeft;
Origin = Anchor.BottomLeft;
2017-05-12 13:22:11 +08:00
const float padding = 5;
Children = new Drawable[]
{
channelSelectionContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Height = 1f - DEFAULT_HEIGHT,
Masking = true,
Children = new[]
{
channelSelection = new ChannelSelectionOverlay
{
RelativeSizeAxes = Axes.Both,
},
},
},
chatContainer = new Container
2017-02-19 17:02:25 +08:00
{
Name = @"chat container",
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
2017-05-12 13:22:11 +08:00
RelativeSizeAxes = Axes.Both,
Height = DEFAULT_HEIGHT,
Children = new[]
2017-02-19 17:02:25 +08:00
{
new Container
2017-02-19 17:02:25 +08:00
{
Name = @"chat area",
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Top = TAB_AREA_HEIGHT },
Children = new Drawable[]
2017-05-12 13:22:11 +08:00
{
chatBackground = new Box
{
RelativeSizeAxes = Axes.Both,
},
currentChannelContainer = new Container<DrawableChat>
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding
{
Bottom = textbox_height
},
},
new Container
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.X,
Height = textbox_height,
Padding = new MarginPadding
{
Top = padding * 2,
Bottom = padding * 2,
2017-06-07 22:00:14 +08:00
Left = ChatLine.LEFT_PADDING + padding * 2,
Right = padding * 2,
},
Children = new Drawable[]
{
2017-08-21 16:43:26 +08:00
textbox = new FocusedTextBox
{
RelativeSizeAxes = Axes.Both,
Height = 1,
PlaceholderText = "type your message",
Exit = () => State = Visibility.Hidden,
OnCommit = postMessage,
ReleaseFocusOnCommit = false,
HoldFocus = true,
}
}
},
loading = new LoadingAnimation(),
}
2017-05-12 13:22:11 +08:00
},
tabsArea = new Container
2017-05-12 13:22:11 +08:00
{
Name = @"tabs area",
2017-05-12 13:22:11 +08:00
RelativeSizeAxes = Axes.X,
Height = TAB_AREA_HEIGHT,
2017-05-12 13:22:11 +08:00
Children = new Drawable[]
{
tabBackground = new Box
2017-05-12 13:22:11 +08:00
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black,
},
2018-04-09 04:12:57 +08:00
channelTabs = new ChannelTabControl
{
RelativeSizeAxes = Axes.Both,
OnRequestLeave = channel => chatManager.JoinedChannels.Remove(channel),
},
2018-04-09 04:12:57 +08:00
userTabs = new UserChatTabControl
{
RelativeSizeAxes = Axes.Both,
OnRequestLeave = privateChat => chatManager.OpenedUserChats.Remove(privateChat),
}
2017-05-12 13:22:11 +08:00
}
2017-05-12 14:32:52 +08:00
},
},
2017-05-12 14:32:52 +08:00
},
};
2018-04-09 04:12:57 +08:00
userTabs.Current.ValueChanged += user => chatManager.CurrentChat.Value = user;
channelTabs.Current.ValueChanged += newChannel => chatManager.CurrentChat.Value = newChannel;
2017-05-30 09:22:14 +08:00
channelTabs.ChannelSelectorActive.ValueChanged += value => channelSelection.State = value ? Visibility.Visible : Visibility.Hidden;
2017-09-04 08:10:04 +08:00
channelSelection.StateChanged += state =>
{
channelTabs.ChannelSelectorActive.Value = state == Visibility.Visible;
if (state == Visibility.Visible)
{
2017-08-21 16:43:26 +08:00
textbox.HoldFocus = false;
if (1f - ChatHeight.Value < channel_selection_min_height)
transformChatHeightTo(1f - channel_selection_min_height, 800, Easing.OutQuint);
}
else
2017-08-21 16:43:26 +08:00
textbox.HoldFocus = true;
};
channelSelection.OnRequestJoin = channel =>
{
if (!chatManager.JoinedChannels.Contains(channel))
chatManager.JoinedChannels.Add(channel);
};
channelSelection.OnRequestLeave = channel => chatManager.JoinedChannels.Remove(channel);
}
private void availableChannelsChanged(object sender, NotifyCollectionChangedEventArgs args)
{
channelSelection.Sections = new[]
{
new ChannelSection
{
Header = "All Channels",
Channels = chatManager.AvailableChannels,
},
};
}
private void joinedChannelsChanged(object sender, NotifyCollectionChangedEventArgs args)
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (ChannelChat newChannel in args.NewItems)
{
channelTabs.AddItem(newChannel);
newChannel.Joined.Value = true;
if (chatManager.CurrentChat.Value == null)
{
chatManager.CurrentChat.Value = newChannel;
}
2018-04-09 04:18:29 +08:00
}
break;
case NotifyCollectionChangedAction.Remove:
foreach (ChannelChat removedChannel in args.OldItems)
{
channelTabs.RemoveItem(removedChannel);
loadedChannels.Remove(loadedChannels.Find(c => c.Chat == removedChannel ));
removedChannel.Joined.Value = false;
if (chatManager.CurrentChat.Value == removedChannel)
chatManager.CurrentChat.Value = null;
}
break;
}
}
private void currentChatChanged(ChatBase chat)
{
if (chat == null)
{
textbox.Current.Disabled = true;
currentChannelContainer.Clear(false);
return;
}
textbox.Current.Disabled = chat.ReadOnly;
if (chat is ChannelChat channelChat)
2018-04-09 04:12:57 +08:00
{
channelTabs.Current.Value = channelChat;
2018-04-09 04:12:57 +08:00
userTabs.DeselectAll();
}
if (chat is UserChat userChat)
{
userTabs.Current.Value = userChat;
channelTabs.DeselectAll();
}
var loaded = loadedChannels.Find(d => d.Chat == chat);
if (loaded == null)
{
currentChannelContainer.FadeOut(500, Easing.OutQuint);
loading.Show();
loaded = new DrawableChat(chat);
loadedChannels.Add(loaded);
LoadComponentAsync(loaded, l =>
{
loading.Hide();
currentChannelContainer.Clear(false);
currentChannelContainer.Add(loaded);
currentChannelContainer.FadeIn(500, Easing.OutQuint);
});
}
else
{
currentChannelContainer.Clear(false);
currentChannelContainer.Add(loaded);
}
}
private double startDragChatHeight;
2017-07-27 21:33:10 +08:00
private bool isDragging;
protected override bool OnDragStart(InputState state)
{
2017-07-27 21:33:10 +08:00
isDragging = tabsArea.IsHovered;
2017-07-27 21:33:10 +08:00
if (!isDragging)
return base.OnDragStart(state);
startDragChatHeight = ChatHeight.Value;
return true;
}
protected override bool OnDrag(InputState state)
{
2017-07-27 21:33:10 +08:00
if (isDragging)
{
Trace.Assert(state.Mouse.PositionMouseDown != null);
// ReSharper disable once PossibleInvalidOperationException
double targetChatHeight = startDragChatHeight - (state.Mouse.Position.Y - state.Mouse.PositionMouseDown.Value.Y) / Parent.DrawSize.Y;
// If the channel selection screen is shown, mind its minimum height
if (channelSelection.State == Visibility.Visible && targetChatHeight > 1f - channel_selection_min_height)
targetChatHeight = 1f - channel_selection_min_height;
ChatHeight.Value = targetChatHeight;
}
2017-05-28 20:34:15 +08:00
return true;
}
protected override bool OnDragEnd(InputState state)
{
2017-07-27 21:33:10 +08:00
isDragging = false;
return base.OnDragEnd(state);
}
2017-05-30 15:33:26 +08:00
public override bool AcceptsFocus => true;
protected override void OnFocus(InputState state)
{
2017-08-21 16:43:26 +08:00
//this is necessary as textbox is masked away and therefore can't get focus :(
GetContainingInputManager().ChangeFocus(textbox);
2017-05-30 15:33:26 +08:00
base.OnFocus(state);
}
2017-04-19 18:15:21 +08:00
protected override void PopIn()
2017-02-19 17:02:25 +08:00
{
2017-07-23 02:50:25 +08:00
this.MoveToY(0, transition_length, Easing.OutQuint);
this.FadeIn(transition_length, Easing.OutQuint);
2017-08-21 16:43:26 +08:00
textbox.HoldFocus = true;
base.PopIn();
2017-04-19 18:15:21 +08:00
}
2017-04-19 17:46:26 +08:00
2017-04-19 18:15:21 +08:00
protected override void PopOut()
{
2017-07-23 02:50:25 +08:00
this.MoveToY(Height, transition_length, Easing.InSine);
this.FadeOut(transition_length, Easing.InSine);
2017-08-21 16:43:26 +08:00
textbox.HoldFocus = false;
base.PopOut();
2017-02-19 17:02:25 +08:00
}
[BackgroundDependencyLoader]
private void load(APIAccess api, OsuConfigManager config, OsuColour colours, ChatManager chatManager)
{
api.Register(chatManager);
ChatHeight = config.GetBindable<double>(OsuSetting.ChatDisplayHeight);
ChatHeight.ValueChanged += h =>
{
chatContainer.Height = (float)h;
channelSelectionContainer.Height = 1f - (float)h;
2017-06-01 19:16:53 +08:00
tabBackground.FadeTo(h == 1 ? 1 : 0.8f, 200);
};
ChatHeight.TriggerChange();
2017-05-12 14:32:52 +08:00
chatBackground.Colour = colours.ChatBlue;
loading.Show();
2017-04-19 18:15:21 +08:00
this.chatManager = chatManager;
chatManager.CurrentChat.ValueChanged += currentChatChanged;
chatManager.JoinedChannels.CollectionChanged += joinedChannelsChanged;
chatManager.AvailableChannels.CollectionChanged += availableChannelsChanged;
2018-04-09 04:12:57 +08:00
chatManager.OpenedUserChats.CollectionChanged += openedUserChatsChanged;
}
private void openedUserChatsChanged(object sender, NotifyCollectionChangedEventArgs args)
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
userTabs.AddItem(args.NewItems[0] as UserChat);
break;
case NotifyCollectionChangedAction.Remove:
userTabs.RemoveItem(args.OldItems[0] as UserChat);
break;
case NotifyCollectionChangedAction.Reset:
userTabs.Clear();
break;
}
}
2017-04-19 18:15:21 +08:00
private void postMessage(TextBox textbox, bool newText)
2016-10-13 22:57:05 +08:00
{
var text = textbox.Text.Trim();
2017-08-21 16:43:26 +08:00
if (string.IsNullOrWhiteSpace(text))
2017-05-16 18:55:45 +08:00
return;
if (text[0] == '/')
chatManager.PostCommand(text.Substring(1));
else
chatManager.PostMessage(text);
2016-11-30 16:07:09 +08:00
textbox.Text = string.Empty;
}
private void transformChatHeightTo(double newChatHeight, double duration = 0, Easing easing = Easing.None)
{
this.TransformTo(this.PopulateTransform(new TransformChatHeight(), newChatHeight, duration, easing));
}
private class TransformChatHeight : Transform<double, ChatOverlay>
{
private double valueAt(double time)
{
if (time < StartTime) return StartValue;
if (time >= EndTime) return EndValue;
return Interpolation.ValueAt(time, StartValue, EndValue, StartTime, EndTime, Easing);
}
public override string TargetMember => "ChatHeight.Value";
protected override void Apply(ChatOverlay d, double time) => d.ChatHeight.Value = valueAt(time);
protected override void ReadIntoStartValue(ChatOverlay d) => StartValue = d.ChatHeight.Value;
}
}
}