RoleplayersToolbox/RoleplayersToolbox/Util.cs

60 lines
1.6 KiB
C#
Raw Permalink Normal View History

2021-05-30 20:22:26 +00:00
using System;
using System.Collections.Generic;
using System.Text;
using Dalamud.Game.Text.SeStringHandling;
2021-06-27 22:56:44 +00:00
using Dalamud.Interface;
using ImGuiNET;
2021-05-30 20:22:26 +00:00
namespace RoleplayersToolbox {
internal static class Util {
2021-08-28 19:10:30 +00:00
public static SeString ReadSeString(IntPtr ptr) {
2021-05-30 20:22:26 +00:00
var bytes = ReadTerminatedBytes(ptr);
2021-08-28 19:10:30 +00:00
return SeString.Parse(bytes);
2021-05-30 20:22:26 +00:00
}
public static string ReadString(IntPtr ptr) {
var bytes = ReadTerminatedBytes(ptr);
return Encoding.UTF8.GetString(bytes);
}
private static unsafe byte[] ReadTerminatedBytes(IntPtr ptr) {
if (ptr == IntPtr.Zero) {
2021-11-09 23:13:03 +00:00
return Array.Empty<byte>();
2021-05-30 20:22:26 +00:00
}
var bytes = new List<byte>();
var bytePtr = (byte*) ptr;
while (*bytePtr != 0) {
bytes.Add(*bytePtr);
bytePtr += 1;
}
return bytes.ToArray();
}
2021-06-27 22:56:44 +00:00
internal static bool IconButton(FontAwesomeIcon icon, string? id = null) {
var label = icon.ToIconString();
if (id != null) {
label += $"##{id}";
}
ImGui.PushFont(UiBuilder.IconFont);
var ret = ImGui.Button(label);
ImGui.PopFont();
return ret;
}
internal static void Tooltip(string tooltip) {
if (!ImGui.IsItemHovered()) {
return;
}
ImGui.BeginTooltip();
ImGui.TextUnformatted(tooltip);
ImGui.EndTooltip();
}
2021-05-30 20:22:26 +00:00
}
}