Skip to content

Commit

Permalink
Merge pull request #29137 from normalid-awa/feature/visual/chatline-b…
Browse files Browse the repository at this point in the history
…ackground-altering

Alternate background colour of chat lines to better visually distinguish wrapped lines
  • Loading branch information
peppy authored Jul 30, 2024
2 parents c80f338 + fc78dc9 commit 71acb7e
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 34 deletions.
36 changes: 36 additions & 0 deletions osu.Game.Tests/Visual/Online/TestSceneDrawableChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void TestDaySeparators()
Id = 3,
Username = "LocalUser"
};

string uuid = Guid.NewGuid().ToString();
AddStep("add local echo message", () => channel.AddLocalEcho(new LocalEchoMessage
{
Expand Down Expand Up @@ -83,5 +84,40 @@ public void TestDaySeparators()
AddUntilStep("three day separators present", () => drawableChannel.ChildrenOfType<DaySeparator>().Count() == 3);
AddAssert("last day separator is from correct day", () => drawableChannel.ChildrenOfType<DaySeparator>().Last().Date.Date == new DateTime(2022, 11, 22));
}

[Test]
public void TestBackgroundAlternating()
{
var localUser = new APIUser
{
Id = 3,
Username = "LocalUser"
};

int messageCount = 1;

AddRepeatStep("add messages", () =>
{
channel.AddNewMessages(new Message(messageCount)
{
Sender = localUser,
Content = "Hi there all!",
Timestamp = new DateTimeOffset(2022, 11, 21, 20, messageCount, 13, TimeSpan.Zero),
Uuid = Guid.NewGuid().ToString(),
});
messageCount++;
}, 10);

AddUntilStep("10 message present", () => drawableChannel.ChildrenOfType<ChatLine>().Count() == 10);

int checkCount = 0;

AddRepeatStep("check background", () =>
{
// +1 because the day separator take one index
Assert.AreEqual((checkCount + 1) % 2 == 0, drawableChannel.ChildrenOfType<ChatLine>().ToList()[checkCount].AlternatingBackground);
checkCount++;
}, 10);
}
}
}
115 changes: 81 additions & 34 deletions osu.Game/Overlays/Chat/ChatLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Chat;
using osuTK.Graphics;
using Message = osu.Game.Online.Chat.Message;

namespace osu.Game.Overlays.Chat
{
Expand Down Expand Up @@ -69,6 +68,23 @@ public Message Message

private Container? highlight;

private Drawable? background;

private bool alternatingBackground;

public bool AlternatingBackground
{
get => alternatingBackground;
set
{
if (alternatingBackground == value)
return;

alternatingBackground = value;
updateBackground();
}
}

/// <summary>
/// The colour used to paint the author's username.
/// </summary>
Expand Down Expand Up @@ -102,48 +118,73 @@ private void load(OsuConfigManager configManager)
configManager.BindWith(OsuSetting.Prefer24HourTime, prefer24HourTime);
prefer24HourTime.BindValueChanged(_ => updateTimestamp());

InternalChild = new GridContainer
Padding = new MarginPadding { Right = 5 };

InternalChildren = new[]
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
RowDimensions = new[] { new Dimension(GridSizeMode.AutoSize) },
ColumnDimensions = new[]
background = new Container
{
new Dimension(GridSizeMode.AutoSize),
new Dimension(GridSizeMode.Absolute, Spacing + UsernameWidth + Spacing),
new Dimension(),
Masking = true,
CornerRadius = 4,
Alpha = 0,
RelativeSizeAxes = Axes.Both,
Blending = BlendingParameters.Additive,
Child = new Box
{
Colour = Colour4.FromHex("#3b3234"),
RelativeSizeAxes = Axes.Both,
},
},
Content = new[]
new GridContainer
{
new Drawable[]
Padding = new MarginPadding
{
drawableTimestamp = new OsuSpriteText
{
Shadow = false,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.GetFont(size: FontSize * 0.75f, weight: FontWeight.SemiBold, fixedWidth: true),
AlwaysPresent = true,
},
drawableUsername = new DrawableChatUsername(message.Sender)
Horizontal = 2,
Vertical = 2,
},
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
RowDimensions = new[] { new Dimension(GridSizeMode.AutoSize) },
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.AutoSize),
new Dimension(GridSizeMode.Absolute, Spacing + UsernameWidth + Spacing),
new Dimension(),
},
Content = new[]
{
new Drawable[]
{
Width = UsernameWidth,
FontSize = FontSize,
AutoSizeAxes = Axes.Y,
Origin = Anchor.TopRight,
Anchor = Anchor.TopRight,
Margin = new MarginPadding { Horizontal = Spacing },
AccentColour = UsernameColour,
Inverted = !string.IsNullOrEmpty(message.Sender.Colour),
drawableTimestamp = new OsuSpriteText
{
Shadow = false,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.GetFont(size: FontSize * 0.75f, weight: FontWeight.SemiBold, fixedWidth: true),
AlwaysPresent = true,
},
drawableUsername = new DrawableChatUsername(message.Sender)
{
Width = UsernameWidth,
FontSize = FontSize,
AutoSizeAxes = Axes.Y,
Origin = Anchor.TopRight,
Anchor = Anchor.TopRight,
Margin = new MarginPadding { Horizontal = Spacing },
AccentColour = UsernameColour,
Inverted = !string.IsNullOrEmpty(message.Sender.Colour),
},
drawableContentFlow = new LinkFlowContainer(styleMessageContent)
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
}
},
drawableContentFlow = new LinkFlowContainer(styleMessageContent)
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
}
},
}
}
};

updateBackground();
}

protected override void LoadComplete()
Expand Down Expand Up @@ -258,5 +299,11 @@ private void updateTimestamp()
Color4Extensions.FromHex("812a96"),
Color4Extensions.FromHex("992861"),
};

private void updateBackground()
{
if (background != null)
background.Alpha = alternatingBackground ? 0.2f : 0;
}
}
}
11 changes: 11 additions & 0 deletions osu.Game/Overlays/Chat/DrawableChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ protected override void LoadComplete()
highlightedMessage.BindValueChanged(_ => processMessageHighlighting(), true);
}

protected override void Update()
{
base.Update();

for (int i = 0; i < ChatLineFlow.Count; i++)
{
if (ChatLineFlow[i] is ChatLine chatline)
chatline.AlternatingBackground = i % 2 == 0;
}
}

/// <summary>
/// Processes any pending message in <see cref="highlightedMessage"/>.
/// </summary>
Expand Down

0 comments on commit 71acb7e

Please sign in to comment.