Use bindings for DalamudIme

This commit is contained in:
goaaats 2025-04-08 22:21:36 +02:00
parent ad3b0f0194
commit 1bc216ccd6

View file

@ -11,6 +11,7 @@ using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Text.Unicode; using System.Text.Unicode;
using Dalamud.Bindings.ImGui;
using Dalamud.Configuration.Internal; using Dalamud.Configuration.Internal;
using Dalamud.Game.Text; using Dalamud.Game.Text;
using Dalamud.Hooking.WndProcHook; using Dalamud.Hooking.WndProcHook;
@ -19,8 +20,6 @@ using Dalamud.Interface.GameFonts;
using Dalamud.Interface.ManagedFontAtlas.Internals; using Dalamud.Interface.ManagedFontAtlas.Internals;
using Dalamud.Interface.Utility; using Dalamud.Interface.Utility;
using Dalamud.Bindings.ImGui;
#if IMEDEBUG #if IMEDEBUG
using Serilog; using Serilog;
#endif #endif
@ -170,7 +169,7 @@ internal sealed unsafe class DalamudIme : IInternalDisposableService
return true; return true;
if (!ImGui.GetIO().ConfigInputTextCursorBlink) if (!ImGui.GetIO().ConfigInputTextCursorBlink)
return true; return true;
var textState = CustomNativeFunctions.igCustom_GetInputTextState(); var textState = GetInputTextState();
if (textState->Id == 0 || (textState->Flags & ImGuiInputTextFlags.ReadOnly) != 0) if (textState->Id == 0 || (textState->Flags & ImGuiInputTextFlags.ReadOnly) != 0)
return true; return true;
if (textState->CursorAnim <= 0) if (textState->CursorAnim <= 0)
@ -228,6 +227,12 @@ internal sealed unsafe class DalamudIme : IInternalDisposableService
} }
} }
private static ImGuiInputTextStateWrapper* GetInputTextState()
{
var ctx = ImGui.GetCurrentContext();
return (ImGuiInputTextStateWrapper*)&ctx.Handle->InputTextState;
}
private static (string String, bool Supported) ToUcs2(char* data, int nc = -1) private static (string String, bool Supported) ToUcs2(char* data, int nc = -1)
{ {
if (nc == -1) if (nc == -1)
@ -326,7 +331,7 @@ internal sealed unsafe class DalamudIme : IInternalDisposableService
try try
{ {
var textState = CustomNativeFunctions.igCustom_GetInputTextState(); var textState = GetInputTextState();
var invalidTarget = textState->Id == 0 || (textState->Flags & ImGuiInputTextFlags.ReadOnly) != 0; var invalidTarget = textState->Id == 0 || (textState->Flags & ImGuiInputTextFlags.ReadOnly) != 0;
#if IMEDEBUG #if IMEDEBUG
@ -556,7 +561,7 @@ internal sealed unsafe class DalamudIme : IInternalDisposableService
this.ReflectCharacterEncounters(newString); this.ReflectCharacterEncounters(newString);
var textState = CustomNativeFunctions.igCustom_GetInputTextState(); var textState = GetInputTextState();
if (this.temporaryUndoSelection is not null) if (this.temporaryUndoSelection is not null)
{ {
textState->Undo(); textState->Undo();
@ -615,7 +620,7 @@ internal sealed unsafe class DalamudIme : IInternalDisposableService
this.compositionCursorOffset = 0; this.compositionCursorOffset = 0;
this.temporaryUndoSelection = null; this.temporaryUndoSelection = null;
var textState = CustomNativeFunctions.igCustom_GetInputTextState(); var textState = GetInputTextState();
textState->Stb.SelectStart = textState->Stb.Cursor = textState->Stb.SelectEnd; textState->Stb.SelectStart = textState->Stb.Cursor = textState->Stb.SelectEnd;
this.candidateStrings.Clear(); this.candidateStrings.Clear();
@ -967,7 +972,7 @@ internal sealed unsafe class DalamudIme : IInternalDisposableService
} }
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
private struct ImGuiInputTextState private struct ImGuiInputTextStateWrapper
{ {
public uint Id; public uint Id;
public int CurLenW; public int CurLenW;
@ -985,7 +990,7 @@ internal sealed unsafe class DalamudIme : IInternalDisposableService
public bool Edited; public bool Edited;
public ImGuiInputTextFlags Flags; public ImGuiInputTextFlags Flags;
public ImVectorWrapper<char> TextW => new((ImVector*)&this.ThisPtr->TextWRaw); public ImVectorWrapper<char> TextW => new((ImVector*)&this.ThisWrapperPtr->TextWRaw);
public (int Start, int End, int Cursor) SelectionTuple public (int Start, int End, int Cursor) SelectionTuple
{ {
@ -993,6 +998,8 @@ internal sealed unsafe class DalamudIme : IInternalDisposableService
set => (this.Stb.SelectStart, this.Stb.SelectEnd, this.Stb.Cursor) = value; set => (this.Stb.SelectStart, this.Stb.SelectEnd, this.Stb.Cursor) = value;
} }
private ImGuiInputTextStateWrapper* ThisWrapperPtr => (ImGuiInputTextStateWrapper*)Unsafe.AsPointer(ref this);
private ImGuiInputTextState* ThisPtr => (ImGuiInputTextState*)Unsafe.AsPointer(ref this); private ImGuiInputTextState* ThisPtr => (ImGuiInputTextState*)Unsafe.AsPointer(ref this);
public void SetSelectionRange(int offset, int length, int relativeCursorOffset) public void SetSelectionRange(int offset, int length, int relativeCursorOffset)
@ -1020,14 +1027,14 @@ internal sealed unsafe class DalamudIme : IInternalDisposableService
(s, e) = (e, s); (s, e) = (e, s);
} }
public void Undo() => CustomNativeFunctions.igCustom_StbTextUndo(this.ThisPtr); public void Undo() => ImGuiP.Custom_StbTextUndo(this.ThisPtr);
public bool MakeUndoReplace(int offset, int oldLength, int newLength) public bool MakeUndoReplace(int offset, int oldLength, int newLength)
{ {
if (oldLength == 0 && newLength == 0) if (oldLength == 0 && newLength == 0)
return false; return false;
CustomNativeFunctions.igCustom_StbTextMakeUndoReplace(this.ThisPtr, offset, oldLength, newLength); ImGuiP.Custom_StbTextMakeUndoReplace(this.ThisPtr, offset, oldLength, newLength);
return true; return true;
} }
@ -1103,20 +1110,6 @@ internal sealed unsafe class DalamudIme : IInternalDisposableService
} }
} }
private static class CustomNativeFunctions
{
#pragma warning disable SA1300
[DllImport("cimgui")]
public static extern ImGuiInputTextState* igCustom_GetInputTextState();
[DllImport("cimgui")]
public static extern void igCustom_StbTextMakeUndoReplace(ImGuiInputTextState* str, int where, int old_length, int new_length);
[DllImport("cimgui")]
public static extern void igCustom_StbTextUndo(ImGuiInputTextState* str);
#pragma warning restore SA1300
}
#if IMEDEBUG #if IMEDEBUG
private static class ImeDebug private static class ImeDebug
{ {