fix formatting

This commit is contained in:
Infi
2024-04-18 22:07:16 +02:00
parent cf40aca8c0
commit 2d69af3f5c
2 changed files with 90 additions and 73 deletions
+1 -2
View File
@@ -88,9 +88,8 @@ internal sealed class Display : ISettingsTab {
ImGui.Spacing(); ImGui.Spacing();
if (ImGuiUtil.InputIntVertical(Language.Options_MaxLinesToShow_Name, Language.Options_MaxLinesToShow_Description, ref Mutable.MaxLinesToRender)) { if (ImGuiUtil.InputIntVertical(Language.Options_MaxLinesToShow_Name, Language.Options_MaxLinesToShow_Description, ref Mutable.MaxLinesToRender))
Mutable.MaxLinesToRender = Math.Clamp(Mutable.MaxLinesToRender, 1, 10_000); Mutable.MaxLinesToRender = Math.Clamp(Mutable.MaxLinesToRender, 1, 10_000);
}
ImGui.Spacing(); ImGui.Spacing();
ImGuiUtil.OptionCheckbox(ref Mutable.CanMove, Language.Options_CanMove_Name); ImGuiUtil.OptionCheckbox(ref Mutable.CanMove, Language.Options_CanMove_Name);
+89 -71
View File
@@ -9,124 +9,134 @@ using ImGuiNET;
namespace ChatTwo.Util; namespace ChatTwo.Util;
internal static class ImGuiUtil { internal static class ImGuiUtil
private static readonly ImGuiMouseButton[] Buttons = { {
private static readonly ImGuiMouseButton[] Buttons =
[
ImGuiMouseButton.Left, ImGuiMouseButton.Left,
ImGuiMouseButton.Middle, ImGuiMouseButton.Middle,
ImGuiMouseButton.Right, ImGuiMouseButton.Right
}; ];
private static Payload? _hovered; private static Payload? _hovered;
private static Payload? _lastLink; private static Payload? _lastLink;
private static readonly List<(Vector2, Vector2)> PayloadBounds = new(); private static readonly List<(Vector2, Vector2)> PayloadBounds = new();
internal static void PostPayload(Chunk chunk, PayloadHandler? handler) { internal static void PostPayload(Chunk chunk, PayloadHandler? handler)
{
var payload = chunk.Link; var payload = chunk.Link;
if (payload != null && ImGui.IsItemHovered()) { if (payload != null && ImGui.IsItemHovered())
{
_hovered = payload; _hovered = payload;
ImGui.SetMouseCursor(ImGuiMouseCursor.Hand); ImGui.SetMouseCursor(ImGuiMouseCursor.Hand);
handler?.Hover(payload); handler?.Hover(payload);
} else if (!ReferenceEquals(_hovered, payload)) { }
else if (!ReferenceEquals(_hovered, payload))
{
_hovered = null; _hovered = null;
} }
if (handler == null) { if (handler == null)
return; return;
}
foreach (var button in Buttons) { foreach (var button in Buttons)
if (ImGui.IsItemClicked(button)) { if (ImGui.IsItemClicked(button))
handler.Click(chunk, payload, button); handler.Click(chunk, payload, button);
}
}
} }
internal static unsafe void WrapText(string csText, Chunk chunk, PayloadHandler? handler, Vector4 defaultText, float lineWidth) { 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();
ImGuiNative.igTextUnformatted(text, textEnd); ImGuiNative.igTextUnformatted(text, textEnd);
PostPayload(chunk, handler); PostPayload(chunk, handler);
if (!ReferenceEquals(_lastLink, chunk.Link)) { if (!ReferenceEquals(_lastLink, chunk.Link))
PayloadBounds.Clear(); PayloadBounds.Clear();
}
_lastLink = chunk.Link; _lastLink = chunk.Link;
if (_hovered != null && ReferenceEquals(_hovered, chunk.Link)) { if (_hovered != null && ReferenceEquals(_hovered, chunk.Link))
{
defaultText.W = 0.25f; defaultText.W = 0.25f;
var actualCol = ColourUtil.Vector4ToAbgr(defaultText); var actualCol = ColourUtil.Vector4ToAbgr(defaultText);
ImGui.GetWindowDrawList().AddRectFilled(oldPos, oldPos + ImGui.GetItemRectSize(), actualCol); ImGui.GetWindowDrawList().AddRectFilled(oldPos, oldPos + ImGui.GetItemRectSize(), actualCol);
foreach (var (start, size) in PayloadBounds) { foreach (var (start, size) in PayloadBounds)
ImGui.GetWindowDrawList().AddRectFilled(start, start + size, actualCol); ImGui.GetWindowDrawList().AddRectFilled(start, start + size, actualCol);
}
PayloadBounds.Clear(); PayloadBounds.Clear();
} }
if (_hovered == null && chunk.Link != null) { if (_hovered == null && chunk.Link != null)
PayloadBounds.Add((oldPos, ImGui.GetItemRectSize())); PayloadBounds.Add((oldPos, ImGui.GetItemRectSize()));
}
} }
if (csText.Length == 0) { if (csText.Length == 0)
return; return;
}
foreach (var part in csText.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None)) { foreach (var part in csText.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None))
{
var bytes = Encoding.UTF8.GetBytes(part); var bytes = Encoding.UTF8.GetBytes(part);
fixed (byte* rawText = bytes) { fixed (byte* rawText = bytes)
{
var text = rawText; var text = rawText;
var textEnd = text + bytes.Length; var textEnd = text + bytes.Length;
// empty string // empty string
if (text == null) { if (text == null)
{
ImGui.TextUnformatted(""); ImGui.TextUnformatted("");
continue; continue;
} }
var widthLeft = ImGui.GetContentRegionAvail().X; var widthLeft = ImGui.GetContentRegionAvail().X;
var endPrevLine = ImGuiNative.ImFont_CalcWordWrapPositionA(ImGui.GetFont().NativePtr, ImGuiHelpers.GlobalScale, text, textEnd, widthLeft); var endPrevLine = ImGuiNative.ImFont_CalcWordWrapPositionA(ImGui.GetFont().NativePtr, ImGuiHelpers.GlobalScale, text, textEnd, widthLeft);
if (endPrevLine == null) { if (endPrevLine == null)
continue; continue;
}
var firstSpace = FindFirstSpace(text, textEnd); var firstSpace = FindFirstSpace(text, textEnd);
var properBreak = firstSpace <= endPrevLine; var properBreak = firstSpace <= endPrevLine;
if (properBreak) { if (properBreak)
{
Text(text, endPrevLine); Text(text, endPrevLine);
} else { }
if (lineWidth == 0f) { else
{
if (lineWidth == 0f)
{
ImGui.TextUnformatted(""); ImGui.TextUnformatted("");
} else { }
else
{
// check if the next bit is longer than the entire line width // 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); 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 // only go to next line is it's going to wrap at the space
if (wrapPos >= firstSpace)
ImGui.TextUnformatted(""); ImGui.TextUnformatted("");
}
} }
} }
widthLeft = ImGui.GetContentRegionAvail().X; widthLeft = ImGui.GetContentRegionAvail().X;
while (endPrevLine < textEnd) { while (endPrevLine < textEnd)
if (properBreak) { {
if (properBreak)
text = endPrevLine; text = endPrevLine;
}
if (*text == ' ') { // skip a space at start of line
if (*text == ' ')
++text; ++text;
} // 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 (properBreak && newEnd == endPrevLine) { if (properBreak && newEnd == endPrevLine)
break; break;
}
endPrevLine = newEnd; endPrevLine = newEnd;
if (endPrevLine == null) { if (endPrevLine == null)
{
ImGui.TextUnformatted(""); ImGui.TextUnformatted("");
ImGui.TextUnformatted(""); ImGui.TextUnformatted("");
break; break;
@@ -134,7 +144,8 @@ internal static class ImGuiUtil {
Text(text, endPrevLine); Text(text, endPrevLine);
if (!properBreak) { if (!properBreak)
{
properBreak = true; properBreak = true;
widthLeft = ImGui.GetContentRegionAvail().X; widthLeft = ImGui.GetContentRegionAvail().X;
} }
@@ -143,29 +154,29 @@ internal static class ImGuiUtil {
} }
} }
private static unsafe byte* FindFirstSpace(byte* text, byte* textEnd) { private static unsafe byte* FindFirstSpace(byte* text, byte* textEnd)
for (var i = text; i < textEnd; i++) { {
if (char.IsWhiteSpace((char) *i)) { for (var i = text; i < textEnd; i++)
if (char.IsWhiteSpace((char) *i))
return i; return i;
}
}
return textEnd; return textEnd;
} }
internal static bool IconButton(FontAwesomeIcon icon, string? id = null, string? tooltip = null) { internal static bool IconButton(FontAwesomeIcon icon, string? id = null, string? tooltip = null)
{
ImGui.PushFont(UiBuilder.IconFont); ImGui.PushFont(UiBuilder.IconFont);
var label = icon.ToIconString(); var label = icon.ToIconString();
if (id != null) { if (id != null)
label += $"##{id}"; label += $"##{id}";
}
var ret = ImGui.Button(label); var ret = ImGui.Button(label);
ImGui.PopFont(); ImGui.PopFont();
if (tooltip != null && ImGui.IsItemHovered()) { if (tooltip != null && ImGui.IsItemHovered())
{
ImGui.BeginTooltip(); ImGui.BeginTooltip();
ImGui.TextUnformatted(tooltip); ImGui.TextUnformatted(tooltip);
ImGui.EndTooltip(); ImGui.EndTooltip();
@@ -174,58 +185,63 @@ internal static class ImGuiUtil {
return ret; return ret;
} }
internal static bool OptionCheckbox(ref bool value, string label, string? description = null) { internal static bool OptionCheckbox(ref bool value, string label, string? description = null)
{
var ret = ImGui.Checkbox(label, ref value); var ret = ImGui.Checkbox(label, ref value);
if (description != null)
if (description != null) {
HelpText(description); HelpText(description);
}
return ret; return ret;
} }
internal static void HelpText(string text) { internal static void HelpText(string text)
{
var colour = ImGui.GetStyle().Colors[(int) ImGuiCol.TextDisabled]; var colour = ImGui.GetStyle().Colors[(int) ImGuiCol.TextDisabled];
ImGui.PushStyleColor(ImGuiCol.Text, colour); ImGui.PushStyleColor(ImGuiCol.Text, colour);
ImGui.PushTextWrapPos(); ImGui.PushTextWrapPos();
try { try
{
ImGui.TextUnformatted(text); ImGui.TextUnformatted(text);
} finally { }
finally
{
ImGui.PopTextWrapPos(); ImGui.PopTextWrapPos();
ImGui.PopStyleColor(); ImGui.PopStyleColor();
} }
} }
internal static void WarningText(string text, bool wrap = true) { internal static void WarningText(string text, bool wrap = true)
{
var style = StyleModel.GetConfiguredStyle() ?? StyleModel.GetFromCurrent(); var style = StyleModel.GetConfiguredStyle() ?? StyleModel.GetFromCurrent();
var dalamudOrange = style.BuiltInColors?.DalamudOrange; var dalamudOrange = style.BuiltInColors?.DalamudOrange;
if (dalamudOrange != null) { if (dalamudOrange != null)
ImGui.PushStyleColor(ImGuiCol.Text, dalamudOrange.Value); ImGui.PushStyleColor(ImGuiCol.Text, dalamudOrange.Value);
}
if (wrap) ImGui.PushTextWrapPos(); if (wrap) ImGui.PushTextWrapPos();
ImGui.TextUnformatted(text); ImGui.TextUnformatted(text);
if (wrap) ImGui.PopTextWrapPos(); if (wrap) ImGui.PopTextWrapPos();
if (dalamudOrange != null) { if (dalamudOrange != null)
ImGui.PopStyleColor(); ImGui.PopStyleColor();
}
} }
internal static bool BeginComboVertical(string label, string previewValue, ImGuiComboFlags flags = ImGuiComboFlags.None) { internal static bool BeginComboVertical(string label, string previewValue, ImGuiComboFlags flags = ImGuiComboFlags.None)
{
ImGui.TextUnformatted(label); ImGui.TextUnformatted(label);
ImGui.SetNextItemWidth(-1); ImGui.SetNextItemWidth(-1);
return ImGui.BeginCombo($"##{label}", previewValue, flags); return ImGui.BeginCombo($"##{label}", previewValue, flags);
} }
internal static bool DragFloatVertical(string label, ref float value, float vSpeed = 1.0f, float vMin = float.MinValue, float vMax = float.MaxValue, string? format = null, ImGuiSliderFlags flags = ImGuiSliderFlags.None) { internal static bool DragFloatVertical(string label, ref float value, float vSpeed = 1.0f, float vMin = float.MinValue, float vMax = float.MaxValue, string? format = null, ImGuiSliderFlags flags = ImGuiSliderFlags.None)
{
ImGui.TextUnformatted(label); ImGui.TextUnformatted(label);
ImGui.SetNextItemWidth(-1); ImGui.SetNextItemWidth(-1);
return ImGui.DragFloat($"##{label}", ref value, vSpeed, vMin, vMax, format, flags); return ImGui.DragFloat($"##{label}", ref value, vSpeed, vMin, vMax, format, flags);
} }
internal static bool DragFloatVertical(string label, string description, ref float value, float vSpeed = 1.0f, float vMin = float.MinValue, float vMax = float.MaxValue, string? format = null, ImGuiSliderFlags flags = ImGuiSliderFlags.None) { internal static bool DragFloatVertical(string label, string description, ref float value, float vSpeed = 1.0f, float vMin = float.MinValue, float vMax = float.MaxValue, string? format = null, ImGuiSliderFlags flags = ImGuiSliderFlags.None)
{
ImGui.TextUnformatted(label); ImGui.TextUnformatted(label);
ImGui.SetNextItemWidth(-1); ImGui.SetNextItemWidth(-1);
var r = ImGui.DragFloat($"##{label}", ref value, vSpeed, vMin, vMax, format, flags); var r = ImGui.DragFloat($"##{label}", ref value, vSpeed, vMin, vMax, format, flags);
@@ -234,7 +250,8 @@ internal static class ImGuiUtil {
return r; return r;
} }
internal static bool InputIntVertical(string label, string description, ref int value, int step = 1, int stepFast = 100, ImGuiInputTextFlags flags = ImGuiInputTextFlags.None) { internal static bool InputIntVertical(string label, string description, ref int value, int step = 1, int stepFast = 100, ImGuiInputTextFlags flags = ImGuiInputTextFlags.None)
{
ImGui.TextUnformatted(label); ImGui.TextUnformatted(label);
ImGui.SetNextItemWidth(-1); ImGui.SetNextItemWidth(-1);
var r = ImGui.InputInt($"##{label}", ref value, step, stepFast, flags); var r = ImGui.InputInt($"##{label}", ref value, step, stepFast, flags);
@@ -259,7 +276,8 @@ internal static class ImGuiUtil {
return ret; return ret;
} }
internal static bool TryToImGui(this VirtualKey key, out ImGuiKey result) { internal static bool TryToImGui(this VirtualKey key, out ImGuiKey result)
{
result = key switch { result = key switch {
VirtualKey.NO_KEY => ImGuiKey.None, VirtualKey.NO_KEY => ImGuiKey.None,
VirtualKey.BACK => ImGuiKey.Backspace, VirtualKey.BACK => ImGuiKey.Backspace,