From 1ab4713ef63a6764c78cb3063abe9476df3407ae Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 16 Nov 2018 14:22:42 +0900 Subject: [PATCH] Add tournament chat display --- .../TestCaseMatchChatDisplay.cs | 103 +++++++++++ .../Components/MatchChatDisplay.cs | 167 ++++++++++++++++++ osu.Game/Online/Chat/Message.cs | 4 - 3 files changed, 270 insertions(+), 4 deletions(-) create mode 100644 osu.Game.Tournament.Tests/TestCaseMatchChatDisplay.cs create mode 100644 osu.Game.Tournament/Components/MatchChatDisplay.cs diff --git a/osu.Game.Tournament.Tests/TestCaseMatchChatDisplay.cs b/osu.Game.Tournament.Tests/TestCaseMatchChatDisplay.cs new file mode 100644 index 0000000000..aab56d1714 --- /dev/null +++ b/osu.Game.Tournament.Tests/TestCaseMatchChatDisplay.cs @@ -0,0 +1,103 @@ +// Copyright (c) 2007-2018 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System.Collections.Generic; +using osu.Framework.Allocation; +using osu.Framework.Graphics; +using osu.Game.Online.Chat; +using osu.Game.Tests.Visual; +using osu.Game.Tournament.Components; +using osu.Game.Tournament.Screens.Ladder.Components; +using osu.Game.Users; +using OpenTK; + +namespace osu.Game.Tournament.Tests +{ + public class TestCaseMatchChatDisplay : OsuTestCase + { + private readonly Channel testChannel = new Channel(); + + private readonly User admin = new User + { + Username = "HappyStick", + Id = 2, + Colour = "f2ca34" + }; + + private readonly User redUser = new User + { + Username = "BanchoBot", + Id = 3, + }; + + private readonly User blueUser = new User + { + Username = "Zallius", + Id = 4, + }; + + [Cached] + private LadderInfo ladderInfo = new LadderInfo(); + + public TestCaseMatchChatDisplay() + { + MatchChatDisplay chatDisplay; + + Add(chatDisplay = new MatchChatDisplay + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Size = new Vector2(400, 80) + }); + + ladderInfo.CurrentMatch.Value = new MatchPairing + { + Team1 = + { + Value = new TournamentTeam { Players = new List { redUser } } + }, + Team2 = + { + Value = new TournamentTeam { Players = new List { blueUser } } + } + }; + + chatDisplay.Channel.Value = testChannel; + } + + protected override void LoadComplete() + { + base.LoadComplete(); + + AddStep("message from admin", () => testChannel.AddLocalEcho(new LocalEchoMessage + { + Sender = admin, + Content = "I am a wang!" + })); + + AddStep("message from team red", () => testChannel.AddLocalEcho(new LocalEchoMessage + { + Sender = redUser, + Content = "I am team red." + })); + + AddStep("message from team red", () => testChannel.AddLocalEcho(new LocalEchoMessage + { + Sender = redUser, + Content = "I plan to win!" + })); + + AddStep("message from team blue", () => testChannel.AddLocalEcho(new LocalEchoMessage + { + Sender = blueUser, + Content = "Not on my watch. Prepare to eat saaaaaaaaaand. Lots and lots of saaaaaaand." + })); + + AddStep("message from admin", () => testChannel.AddLocalEcho(new LocalEchoMessage + { + Sender = admin, + Content = "Okay okay, calm down guys. Let's do this!" + })); + } + } +} diff --git a/osu.Game.Tournament/Components/MatchChatDisplay.cs b/osu.Game.Tournament/Components/MatchChatDisplay.cs new file mode 100644 index 0000000000..a68507044d --- /dev/null +++ b/osu.Game.Tournament/Components/MatchChatDisplay.cs @@ -0,0 +1,167 @@ +// Copyright (c) 2007-2018 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System.Collections.Generic; +using System.Linq; +using osu.Framework.Allocation; +using osu.Framework.Configuration; +using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.Shapes; +using osu.Game.Graphics; +using osu.Game.Graphics.Containers; +using osu.Game.Graphics.Sprites; +using osu.Game.Online.Chat; +using OpenTK; +using OpenTK.Graphics; + +namespace osu.Game.Tournament.Components +{ + public class MatchChatDisplay : CompositeDrawable + { + private Channel lastChannel; + public readonly Bindable Channel = new Bindable(); + private readonly FillFlowContainer messagesFlow; + + public MatchChatDisplay() + { + CornerRadius = 5; + Masking = true; + + InternalChildren = new Drawable[] + { + new Box + { + Colour = Color4.Black, + Alpha = 0.8f, + RelativeSizeAxes = Axes.Both, + }, + messagesFlow = new FillFlowContainer + { + RelativeSizeAxes = Axes.X, + AutoSizeAxes = Axes.Y, + LayoutEasing = Easing.Out, + LayoutDuration = 500, + Anchor = Anchor.BottomLeft, + Origin = Anchor.BottomLeft, + Direction = FillDirection.Vertical, + }, + }; + + Channel.BindValueChanged(channelChanged); + } + + private void channelChanged(Channel channel) + { + if (lastChannel != null) + lastChannel.NewMessagesArrived -= newMessages; + + lastChannel = channel; + + channel.NewMessagesArrived += newMessages; + } + + private void newMessages(IEnumerable messages) + { + var excessChildren = messagesFlow.Children.Count - 10; + if (excessChildren > 0) + { + foreach (var c in messagesFlow.Children.Take(excessChildren)) + c.Expire(); + } + + foreach (var message in messages) + { + var formatted = MessageFormatter.FormatMessage(message); + messagesFlow.Add(new MatchMessage(formatted) { Y = messagesFlow.Height }); + } + } + + private class MatchMessage : CompositeDrawable + { + private readonly Message message; + + public MatchMessage(Message message) + { + this.message = message; + } + + private readonly Color4 red = new Color4(186, 0, 18, 255); + private readonly Color4 blue = new Color4(17, 136, 170, 255); + + [BackgroundDependencyLoader] + private void load(LadderInfo info) + { + Circle colourBox; + + Margin = new MarginPadding(3); + + RelativeSizeAxes = Axes.X; + AutoSizeAxes = Axes.Y; + + OsuSpriteText senderText; + InternalChildren = new Drawable[] + { + new FillFlowContainer + { + RelativeSizeAxes = Axes.X, + AutoSizeAxes = Axes.Y, + Direction = FillDirection.Horizontal, + Children = new Drawable[] + { + new Container + { + RelativeSizeAxes = Axes.X, + Width = 0.2f, + Children = new Drawable[] + { + senderText = new OsuSpriteText + { + Font = @"Exo2.0-Bold", + Anchor = Anchor.TopRight, + Origin = Anchor.TopRight, + Text = message.Sender.ToString() + } + } + }, + new Container + { + Size = new Vector2(8, OsuSpriteText.FONT_SIZE), + Margin = new MarginPadding { Horizontal = 3 }, + Children = new Drawable[] + { + colourBox = new Circle + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Size = new Vector2(8), + }, + } + }, + new OsuTextFlowContainer + { + RelativeSizeAxes = Axes.X, + AutoSizeAxes = Axes.Y, + Width = 0.5f, + Text = message.DisplayContent + } + } + }, + }; + + if (message.Sender.Colour != null) + { + senderText.Colour = colourBox.Colour = OsuColour.FromHex(message.Sender.Colour); + } + else if (info.CurrentMatch.Value.Team1.Value.Players.Any(u => u.Id == message.Sender.Id)) + { + colourBox.Colour = red; + } + else if (info.CurrentMatch.Value.Team2.Value.Players.Any(u => u.Id == message.Sender.Id)) + { + colourBox.Colour = blue; + } + } + } + } +} diff --git a/osu.Game/Online/Chat/Message.cs b/osu.Game/Online/Chat/Message.cs index 3f0f352ac7..2eb3ca70c2 100644 --- a/osu.Game/Online/Chat/Message.cs +++ b/osu.Game/Online/Chat/Message.cs @@ -13,10 +13,6 @@ namespace osu.Game.Online.Chat [JsonProperty(@"message_id")] public readonly long? Id; - //todo: this should be inside sender. - [JsonProperty(@"sender_id")] - public long UserId; - [JsonProperty(@"channel_id")] public long ChannelId;