fix: handle more weird wrapping stuff

This commit is contained in:
Anna
2022-02-16 15:15:10 -05:00
parent 3e7ba56c17
commit 259000d92a
2 changed files with 25 additions and 11 deletions
+8 -6
View File
@@ -671,13 +671,15 @@ internal sealed class ChatLog : IUiComponent {
ImGui.TableNextColumn(); ImGui.TableNextColumn();
} }
var lineWidth = ImGui.GetContentRegionAvail().X;
var beforeDraw = ImGui.GetCursorScreenPos(); var beforeDraw = ImGui.GetCursorScreenPos();
if (message.Sender.Count > 0) { if (message.Sender.Count > 0) {
this.DrawChunks(message.Sender, true, this.PayloadHandler); this.DrawChunks(message.Sender, true, this.PayloadHandler, lineWidth);
ImGui.SameLine(); ImGui.SameLine();
} }
this.DrawChunks(message.Content, true, this.PayloadHandler); this.DrawChunks(message.Content, true, this.PayloadHandler, lineWidth);
var afterDraw = ImGui.GetCursorScreenPos(); var afterDraw = ImGui.GetCursorScreenPos();
message.Height = ImGui.GetCursorPosY() - lastPos; message.Height = ImGui.GetCursorPosY() - lastPos;
@@ -918,7 +920,7 @@ internal sealed class ChatLog : IUiComponent {
return 0; return 0;
} }
internal void DrawChunks(IReadOnlyList<Chunk> chunks, bool wrap = true, PayloadHandler? handler = null) { internal void DrawChunks(IReadOnlyList<Chunk> chunks, bool wrap = true, PayloadHandler? handler = null, float lineWidth = 0f) {
ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, Vector2.Zero); ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, Vector2.Zero);
try { try {
for (var i = 0; i < chunks.Count; i++) { for (var i = 0; i < chunks.Count; i++) {
@@ -926,7 +928,7 @@ internal sealed class ChatLog : IUiComponent {
continue; continue;
} }
this.DrawChunk(chunks[i], wrap, handler); this.DrawChunk(chunks[i], wrap, handler, lineWidth);
if (i < chunks.Count - 1) { if (i < chunks.Count - 1) {
ImGui.SameLine(); 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) { if (chunk is IconChunk icon && this._fontIcon != null) {
var bounds = IconUtil.GetBounds((byte) icon.Icon); var bounds = IconUtil.GetBounds((byte) icon.Icon);
if (bounds != null) { if (bounds != null) {
@@ -988,7 +990,7 @@ internal sealed class ChatLog : IUiComponent {
} }
if (wrap) { if (wrap) {
ImGuiUtil.WrapText(content, chunk, handler, this.Ui.DefaultText); ImGuiUtil.WrapText(content, chunk, handler, this.Ui.DefaultText, lineWidth);
} else { } else {
ImGui.TextUnformatted(content); ImGui.TextUnformatted(content);
ImGuiUtil.PostPayload(chunk, handler); ImGuiUtil.PostPayload(chunk, handler);
+17 -5
View File
@@ -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) { void Text(byte* text, byte* textEnd) {
var oldPos = ImGui.GetCursorScreenPos(); var oldPos = ImGui.GetCursorScreenPos();
@@ -96,7 +96,16 @@ internal static class ImGuiUtil {
if (properBreak) { if (properBreak) {
Text(text, endPrevLine); Text(text, endPrevLine);
} else { } 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; widthLeft = ImGui.GetContentRegionAvail().X;
@@ -105,14 +114,12 @@ internal static class ImGuiUtil {
text = endPrevLine; text = endPrevLine;
} }
properBreak = true;
if (*text == ' ') { if (*text == ' ') {
++text; ++text;
} // skip a space at start of line } // skip a space at start of line
var newEnd = ImGuiNative.ImFont_CalcWordWrapPositionA(ImGui.GetFont().NativePtr, ImGuiHelpers.GlobalScale, text, textEnd, widthLeft); var newEnd = ImGuiNative.ImFont_CalcWordWrapPositionA(ImGui.GetFont().NativePtr, ImGuiHelpers.GlobalScale, text, textEnd, widthLeft);
if (newEnd == endPrevLine) { if (properBreak && newEnd == endPrevLine) {
break; break;
} }
@@ -124,6 +131,11 @@ internal static class ImGuiUtil {
} }
Text(text, endPrevLine); Text(text, endPrevLine);
if (!properBreak) {
properBreak = true;
widthLeft = ImGui.GetContentRegionAvail().X;
}
} }
} }
} }