From 259000d92a9640ead36a67e8c75b15455ba2ad21 Mon Sep 17 00:00:00 2001 From: Anna Date: Wed, 16 Feb 2022 15:15:10 -0500 Subject: [PATCH] fix: handle more weird wrapping stuff --- ChatTwo/Ui/ChatLog.cs | 14 ++++++++------ ChatTwo/Util/ImGuiUtil.cs | 22 +++++++++++++++++----- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/ChatTwo/Ui/ChatLog.cs b/ChatTwo/Ui/ChatLog.cs index 6270872..3ba7a5f 100755 --- a/ChatTwo/Ui/ChatLog.cs +++ b/ChatTwo/Ui/ChatLog.cs @@ -671,13 +671,15 @@ internal sealed class ChatLog : IUiComponent { ImGui.TableNextColumn(); } + var lineWidth = ImGui.GetContentRegionAvail().X; + var beforeDraw = ImGui.GetCursorScreenPos(); if (message.Sender.Count > 0) { - this.DrawChunks(message.Sender, true, this.PayloadHandler); + this.DrawChunks(message.Sender, true, this.PayloadHandler, lineWidth); ImGui.SameLine(); } - this.DrawChunks(message.Content, true, this.PayloadHandler); + this.DrawChunks(message.Content, true, this.PayloadHandler, lineWidth); var afterDraw = ImGui.GetCursorScreenPos(); message.Height = ImGui.GetCursorPosY() - lastPos; @@ -918,7 +920,7 @@ internal sealed class ChatLog : IUiComponent { return 0; } - internal void DrawChunks(IReadOnlyList chunks, bool wrap = true, PayloadHandler? handler = null) { + internal void DrawChunks(IReadOnlyList chunks, bool wrap = true, PayloadHandler? handler = null, float lineWidth = 0f) { ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, Vector2.Zero); try { for (var i = 0; i < chunks.Count; i++) { @@ -926,7 +928,7 @@ internal sealed class ChatLog : IUiComponent { continue; } - this.DrawChunk(chunks[i], wrap, handler); + this.DrawChunk(chunks[i], wrap, handler, lineWidth); if (i < chunks.Count - 1) { ImGui.SameLine(); @@ -937,7 +939,7 @@ internal sealed class ChatLog : IUiComponent { } } - private void DrawChunk(Chunk chunk, bool wrap = true, PayloadHandler? handler = null) { + private void DrawChunk(Chunk chunk, bool wrap = true, PayloadHandler? handler = null, float lineWidth = 0f) { if (chunk is IconChunk icon && this._fontIcon != null) { var bounds = IconUtil.GetBounds((byte) icon.Icon); if (bounds != null) { @@ -988,7 +990,7 @@ internal sealed class ChatLog : IUiComponent { } if (wrap) { - ImGuiUtil.WrapText(content, chunk, handler, this.Ui.DefaultText); + ImGuiUtil.WrapText(content, chunk, handler, this.Ui.DefaultText, lineWidth); } else { ImGui.TextUnformatted(content); ImGuiUtil.PostPayload(chunk, handler); diff --git a/ChatTwo/Util/ImGuiUtil.cs b/ChatTwo/Util/ImGuiUtil.cs index 96a7067..7a3fa15 100755 --- a/ChatTwo/Util/ImGuiUtil.cs +++ b/ChatTwo/Util/ImGuiUtil.cs @@ -39,7 +39,7 @@ internal static class ImGuiUtil { } } - internal static unsafe void WrapText(string csText, Chunk chunk, PayloadHandler? handler, Vector4 defaultText) { + internal static unsafe void WrapText(string csText, Chunk chunk, PayloadHandler? handler, Vector4 defaultText, float lineWidth) { void Text(byte* text, byte* textEnd) { var oldPos = ImGui.GetCursorScreenPos(); @@ -96,7 +96,16 @@ internal static class ImGuiUtil { if (properBreak) { Text(text, endPrevLine); } else { - ImGui.TextUnformatted(""); + if (lineWidth == 0f) { + ImGui.TextUnformatted(""); + } else { + // check if the next bit is longer than the entire line width + var wrapPos = ImGuiNative.ImFont_CalcWordWrapPositionA(ImGui.GetFont().NativePtr, ImGuiHelpers.GlobalScale, text, firstSpace, lineWidth); + if (wrapPos >= firstSpace) { + // only go to next line is it's going to wrap at the space + ImGui.TextUnformatted(""); + } + } } widthLeft = ImGui.GetContentRegionAvail().X; @@ -105,14 +114,12 @@ internal static class ImGuiUtil { text = endPrevLine; } - properBreak = true; - if (*text == ' ') { ++text; } // skip a space at start of line var newEnd = ImGuiNative.ImFont_CalcWordWrapPositionA(ImGui.GetFont().NativePtr, ImGuiHelpers.GlobalScale, text, textEnd, widthLeft); - if (newEnd == endPrevLine) { + if (properBreak && newEnd == endPrevLine) { break; } @@ -124,6 +131,11 @@ internal static class ImGuiUtil { } Text(text, endPrevLine); + + if (!properBreak) { + properBreak = true; + widthLeft = ImGui.GetContentRegionAvail().X; + } } } }