1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 10:52:53 +08:00

allow custom username color for chatLine

user's color (e.g. green for NAT) will be ignore
This commit is contained in:
cdwcgt 2023-06-12 21:47:13 +09:00
parent 226bb4f387
commit 430938fbb2
No known key found for this signature in database
GPG Key ID: 144396D01095C3A2
3 changed files with 36 additions and 4 deletions

View File

@ -38,10 +38,13 @@ namespace osu.Game.Tests.Visual.Online
private void clear() => AddStep("clear messages", textContainer.Clear); private void clear() => AddStep("clear messages", textContainer.Clear);
private void addMessageWithChecks(string text, bool isAction = false, bool isImportant = false, string username = null) private void addMessageWithChecks(string text, bool isAction = false, bool isImportant = false, string username = null, Colour4? color = null)
{ {
int index = textContainer.Count + 1; int index = textContainer.Count + 1;
var newLine = new ChatLine(new DummyMessage(text, isAction, isImportant, index, username)); var newLine = new ChatLine(new DummyMessage(text, isAction, isImportant, index, username))
{
UsernameColour = color
};
textContainer.Add(newLine); textContainer.Add(newLine);
} }
@ -51,6 +54,7 @@ namespace osu.Game.Tests.Visual.Online
addMessageWithChecks($"Wide {a} character username.", username: new string('w', a)); addMessageWithChecks($"Wide {a} character username.", username: new string('w', a));
addMessageWithChecks("Short name with spaces.", username: "sho rt name"); addMessageWithChecks("Short name with spaces.", username: "sho rt name");
addMessageWithChecks("Long name with spaces.", username: "long name with s p a c e s"); addMessageWithChecks("Long name with spaces.", username: "long name with s p a c e s");
addMessageWithChecks("message with custom color", username: "I have custom color", color: Colour4.Green);
} }
private class DummyMessage : Message private class DummyMessage : Message

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
@ -18,6 +19,7 @@ using osu.Game.Configuration;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Chat; using osu.Game.Online.Chat;
namespace osu.Game.Overlays.Chat namespace osu.Game.Overlays.Chat
@ -66,6 +68,24 @@ namespace osu.Game.Overlays.Chat
private Container? highlight; private Container? highlight;
private Colour4? usernameColour;
/// <summary>
/// if set, it will override <see cref="APIUser.Colour"/> or <see cref="DrawableChatUsername.default_colours"/>.
/// Must be set when constructor, otherwise throw <see cref="InvalidOperationException"/>.
/// </summary>
public Colour4? UsernameColour
{
get => usernameColour;
set
{
if (drawableUsername != null)
throw new InvalidOperationException("Can't change Username color after DrawableChatUsername created");
usernameColour = value;
}
}
public ChatLine(Message message) public ChatLine(Message message)
{ {
Message = message; Message = message;
@ -103,7 +123,7 @@ namespace osu.Game.Overlays.Chat
Font = OsuFont.GetFont(size: FontSize * 0.75f, weight: FontWeight.SemiBold, fixedWidth: true), Font = OsuFont.GetFont(size: FontSize * 0.75f, weight: FontWeight.SemiBold, fixedWidth: true),
AlwaysPresent = true, AlwaysPresent = true,
}, },
drawableUsername = new DrawableChatUsername(message.Sender) drawableUsername = new DrawableChatUsername(message.Sender, usernameColour)
{ {
Width = UsernameWidth, Width = UsernameWidth,
FontSize = FontSize, FontSize = FontSize,

View File

@ -77,7 +77,7 @@ namespace osu.Game.Overlays.Chat
private readonly Drawable colouredDrawable; private readonly Drawable colouredDrawable;
public DrawableChatUsername(APIUser user) public DrawableChatUsername(APIUser user, Color4? customColor = null)
{ {
this.user = user; this.user = user;
@ -92,6 +92,14 @@ namespace osu.Game.Overlays.Chat
Origin = Anchor.TopRight, Origin = Anchor.TopRight,
}; };
if (customColor != null)
{
AccentColour = customColor.Value;
Add(colouredDrawable = drawableText);
return;
}
if (string.IsNullOrWhiteSpace(user.Colour)) if (string.IsNullOrWhiteSpace(user.Colour))
{ {
AccentColour = default_colours[user.Id % default_colours.Length]; AccentColour = default_colours[user.Id % default_colours.Length];