diff --git a/addons/imgui-godot/ImGuiGodot/ImGuiExtensions.cs b/addons/imgui-godot/ImGuiGodot/ImGuiExtensions.cs index 5b00ae4..2e7120c 100644 --- a/addons/imgui-godot/ImGuiGodot/ImGuiExtensions.cs +++ b/addons/imgui-godot/ImGuiGodot/ImGuiExtensions.cs @@ -61,7 +61,8 @@ public static Color ToColor(this Vector4 vec) /// public static void SetIniFilename(this ImGuiIOPtr io, string fileName) { - Internal.State.Instance.SetIniFilename(io, fileName); + _ = io; + ImGuiGD.SetIniFilename(fileName); } } #endif diff --git a/addons/imgui-godot/ImGuiGodot/ImGuiGD.cs b/addons/imgui-godot/ImGuiGodot/ImGuiGD.cs index 6135d97..3a2c6b2 100644 --- a/addons/imgui-godot/ImGuiGodot/ImGuiGD.cs +++ b/addons/imgui-godot/ImGuiGodot/ImGuiGD.cs @@ -128,5 +128,10 @@ internal static bool SubViewportWidget(SubViewport svp) { return _backend.SubViewportWidget(svp); } + + public static void SetIniFilename(string filename) + { + _backend.SetIniFilename(filename); + } } #endif diff --git a/addons/imgui-godot/ImGuiGodot/Internal/BackendNative.cs b/addons/imgui-godot/ImGuiGodot/Internal/BackendNative.cs index 0cf9ced..e02b2f4 100644 --- a/addons/imgui-godot/ImGuiGodot/Internal/BackendNative.cs +++ b/addons/imgui-godot/ImGuiGodot/Internal/BackendNative.cs @@ -18,6 +18,7 @@ private sealed class MethodName public static readonly StringName SetMainViewport = "SetMainViewport"; public static readonly StringName SubViewport = "SubViewport"; public static readonly StringName ToolInit = "ToolInit"; + public static readonly StringName SetIniFilename = "SetIniFilename"; } private sealed class PropertyName @@ -94,5 +95,10 @@ public void ToolInit() _gd.Call(MethodName.ToolInit); ImGuiSync.SyncPtrs(); } + + public void SetIniFilename(string filename) + { + _gd.Call(MethodName.SetIniFilename, filename); + } } #endif diff --git a/addons/imgui-godot/ImGuiGodot/Internal/BackendNet.cs b/addons/imgui-godot/ImGuiGodot/Internal/BackendNet.cs index c64fa7b..713d2d5 100644 --- a/addons/imgui-godot/ImGuiGodot/Internal/BackendNet.cs +++ b/addons/imgui-godot/ImGuiGodot/Internal/BackendNet.cs @@ -57,6 +57,11 @@ public void ResetFonts() State.Instance.Fonts.ResetFonts(); } + public void SetIniFilename(string filename) + { + State.Instance.SetIniFilename(filename); + } + public void SetMainViewport(Viewport vp) { ImGuiController.Instance.SetMainViewport(vp); diff --git a/addons/imgui-godot/ImGuiGodot/Internal/IBackend.cs b/addons/imgui-godot/ImGuiGodot/Internal/IBackend.cs index 17d0351..f7f8ca6 100644 --- a/addons/imgui-godot/ImGuiGodot/Internal/IBackend.cs +++ b/addons/imgui-godot/ImGuiGodot/Internal/IBackend.cs @@ -16,5 +16,6 @@ internal interface IBackend public void Connect(Callable callable); public void SetMainViewport(Viewport vp); public bool SubViewportWidget(SubViewport svp); + public void SetIniFilename(string filename); } #endif diff --git a/addons/imgui-godot/ImGuiGodot/Internal/State.cs b/addons/imgui-godot/ImGuiGodot/Internal/State.cs index 4a8a37e..ece6355 100644 --- a/addons/imgui-godot/ImGuiGodot/Internal/State.cs +++ b/addons/imgui-godot/ImGuiGodot/Internal/State.cs @@ -149,8 +149,9 @@ public static void Init(Resource cfg) ImGuiGD.RebuildFontAtlas(); } - public unsafe void SetIniFilename(ImGuiIOPtr io, string fileName) + public unsafe void SetIniFilename(string fileName) { + var io = ImGui.GetIO(); io.NativePtr->IniFilename = null; if (_iniFilenameBuffer != IntPtr.Zero) diff --git a/doc/test/csharp/Main.cs b/doc/test/csharp/Main.cs index babe6cb..713e675 100644 --- a/doc/test/csharp/Main.cs +++ b/doc/test/csharp/Main.cs @@ -15,6 +15,9 @@ public override async void _Ready() { try { + var io = ImGui.GetIO(); + io.SetIniFilename(""); + await ToSignal(this, SignalName.WithinProcess); Assert.Equal(ImGuiGD.Scale, 2); @@ -27,6 +30,24 @@ public override async void _Ready() Assert.Equal(ImGuiGD.Scale, 4); Assert.Equal(ImGui.GetFontSize(), 52.0f); + // IniSavingRate + GetTree().CreateTimer(5.1).Timeout += OnTimeout; + } + catch (Exception e) + { + GD.Print(e); + GetTree().Quit(1); + } + } + + public async void OnTimeout() + { + try + { + await ToSignal(this, SignalName.WithinProcess); + + Assert.False(FileAccess.FileExists("user://imgui.ini")); + GD.Print("All tests passed."); GetTree().Quit(0); } diff --git a/doc/test/gdscript/main.gd b/doc/test/gdscript/main.gd index 45809e9..2e60a8d 100644 --- a/doc/test/gdscript/main.gd +++ b/doc/test/gdscript/main.gd @@ -3,6 +3,8 @@ extends "res://test_base.gd" signal within_process func _ready() -> void: + ImGuiGD.SetIniFilename("") + await within_process # ImGuiConfig loads properly @@ -17,6 +19,14 @@ func _ready() -> void: assert_equal(ImGuiGD.Scale, 4) assert_equal(ImGui.GetFontSize(), 52) + # IniSavingRate + get_tree().create_timer(5.1).timeout.connect(on_timeout) + +func on_timeout(): + await within_process + + assert_false(FileAccess.file_exists("user://imgui.ini")) + exit_with_status() func _process(_delta: float) -> void: diff --git a/gdext/include/imgui-godot.h b/gdext/include/imgui-godot.h index 14e02d0..51a9ac2 100644 --- a/gdext/include/imgui-godot.h +++ b/gdext/include/imgui-godot.h @@ -148,6 +148,13 @@ inline bool ToolInit() return detail::ImGuiGD->call(sn); } +inline void SetIniFilename(String fn) +{ + ERR_FAIL_COND(!detail::GET_IMGUIGD()); + static const StringName sn("SetIniFilename"); + detail::ImGuiGD->call(sn, fn); +} + inline void SyncImGuiPtrs() { Object* obj = ClassDB::instantiate("ImGuiSync"); diff --git a/gdext/src/Context.cpp b/gdext/src/Context.cpp index 3e01340..56307b0 100644 --- a/gdext/src/Context.cpp +++ b/gdext/src/Context.cpp @@ -103,8 +103,7 @@ void Init(const Ref& cfg) ctx->scale = cfg->get("Scale"); String iniFilename = cfg->get("IniFilename"); - if (iniFilename.length() > 0) - SetIniFilename(iniFilename); + SetIniFilename(iniFilename); Array fonts = cfg->get("Fonts"); for (int i = 0; i < fonts.size(); ++i) diff --git a/gdext/src/ImGuiGD.cpp b/gdext/src/ImGuiGD.cpp index 09615e9..d757a47 100644 --- a/gdext/src/ImGuiGD.cpp +++ b/gdext/src/ImGuiGD.cpp @@ -39,6 +39,7 @@ void ImGuiGD::_bind_methods() ClassDB::bind_method(D_METHOD("GetImGuiPtrs", "version", "ioSize", "vertSize", "idxSize", "charSize"), &ImGuiGD::GetImGuiPtrs); ClassDB::bind_method(D_METHOD("ToolInit"), &ImGuiGD::ToolInit); + ClassDB::bind_method(D_METHOD("SetIniFilename", "filename"), &ImGuiGD::SetIniFilename); ClassDB::bind_method(D_METHOD("GetFontPtrs"), &ImGuiGD::GetFontPtrs); } @@ -190,4 +191,9 @@ bool ImGuiGD::SubViewport(godot::SubViewport* svp) return ImGui::Godot::SubViewportWidget(svp); } +void ImGuiGD::SetIniFilename(String fn) +{ + ImGui::Godot::SetIniFilename(fn); +} + } // namespace ImGui::Godot diff --git a/gdext/src/ImGuiGD.h b/gdext/src/ImGuiGD.h index bbaedfb..53c2a86 100644 --- a/gdext/src/ImGuiGD.h +++ b/gdext/src/ImGuiGD.h @@ -38,6 +38,7 @@ class ImGuiGD : public Object float _GetScale(); void SetMainViewport(Viewport* vp); + void SetIniFilename(String fn); PackedInt64Array GetFontPtrs(); PackedInt64Array GetImGuiPtrs(String version, int ioSize, int vertSize, int idxSize, int charSize);