Make standalone chat reuse more code from main chat overlay

This commit is contained in:
Dean Herbert
2018-12-21 17:54:12 +09:00
parent a54951b72d
commit 81104f4803
4 changed files with 113 additions and 186 deletions

View File

@ -17,7 +17,7 @@ namespace osu.Game.Overlays.Chat
public class DrawableChannel : Container
{
public readonly Channel Channel;
private readonly ChatLineContainer flow;
protected readonly ChatLineContainer ChatLineFlow;
private readonly ScrollContainer scroll;
public DrawableChannel(Channel channel)
@ -38,7 +38,7 @@ namespace osu.Game.Overlays.Chat
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Child = flow = new ChatLineContainer
Child = ChatLineFlow = new ChatLineContainer
{
Padding = new MarginPadding { Left = 20, Right = 20 },
RelativeSizeAxes = Axes.X,
@ -72,17 +72,19 @@ namespace osu.Game.Overlays.Chat
Channel.PendingMessageResolved -= pendingMessageResolved;
}
protected virtual ChatLine CreateChatLine(Message m) => new ChatLine(m);
private void newMessagesArrived(IEnumerable<Message> newMessages)
{
// Add up to last Channel.MAX_HISTORY messages
var displayMessages = newMessages.Skip(Math.Max(0, newMessages.Count() - Channel.MaxHistory));
flow.AddRange(displayMessages.Select(m => new ChatLine(m)));
ChatLineFlow.AddRange(displayMessages.Select(CreateChatLine));
if (scroll.IsScrolledToEnd(10) || !flow.Children.Any() || newMessages.Any(m => m is LocalMessage))
if (scroll.IsScrolledToEnd(10) || !ChatLineFlow.Children.Any() || newMessages.Any(m => m is LocalMessage))
scrollToEnd();
var staleMessages = flow.Children.Where(c => c.LifetimeEnd == double.MaxValue).ToArray();
var staleMessages = ChatLineFlow.Children.Where(c => c.LifetimeEnd == double.MaxValue).ToArray();
int count = staleMessages.Length - Channel.MaxHistory;
for (int i = 0; i < count; i++)
@ -96,25 +98,25 @@ namespace osu.Game.Overlays.Chat
private void pendingMessageResolved(Message existing, Message updated)
{
var found = flow.Children.LastOrDefault(c => c.Message == existing);
var found = ChatLineFlow.Children.LastOrDefault(c => c.Message == existing);
if (found != null)
{
Trace.Assert(updated.Id.HasValue, "An updated message was returned with no ID.");
flow.Remove(found);
ChatLineFlow.Remove(found);
found.Message = updated;
flow.Add(found);
ChatLineFlow.Add(found);
}
}
private void messageRemoved(Message removed)
{
flow.Children.FirstOrDefault(c => c.Message == removed)?.FadeColour(Color4.Red, 400).FadeOut(600).Expire();
ChatLineFlow.Children.FirstOrDefault(c => c.Message == removed)?.FadeColour(Color4.Red, 400).FadeOut(600).Expire();
}
private void scrollToEnd() => ScheduleAfterChildren(() => scroll.ScrollToEnd());
private class ChatLineContainer : FillFlowContainer<ChatLine>
protected class ChatLineContainer : FillFlowContainer<ChatLine>
{
protected override int Compare(Drawable x, Drawable y)
{