Add tournament chat display

This commit is contained in:
Dean Herbert 2018-11-16 14:22:42 +09:00
parent e6637532bc
commit 1ab4713ef6
3 changed files with 270 additions and 4 deletions

View File

@ -0,0 +1,103 @@
// 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 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<User> { redUser } }
},
Team2 =
{
Value = new TournamentTeam { Players = new List<User> { 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!"
}));
}
}
}

View File

@ -0,0 +1,167 @@
// 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.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> Channel = new Bindable<Channel>();
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<Message> 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;
}
}
}
}
}

View File

@ -13,10 +13,6 @@ public class Message : IComparable<Message>, IEquatable<Message>
[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;