Skip to content

Commit

Permalink
Fix ini filename issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Jul 10, 2024
1 parent f6a4b8d commit c1c2978
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 4 deletions.
3 changes: 2 additions & 1 deletion addons/imgui-godot/ImGuiGodot/ImGuiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public static Color ToColor(this Vector4 vec)
/// </summary>
public static void SetIniFilename(this ImGuiIOPtr io, string fileName)
{
Internal.State.Instance.SetIniFilename(io, fileName);
_ = io;
ImGuiGD.SetIniFilename(fileName);
}
}
#endif
5 changes: 5 additions & 0 deletions addons/imgui-godot/ImGuiGodot/ImGuiGD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,10 @@ internal static bool SubViewportWidget(SubViewport svp)
{
return _backend.SubViewportWidget(svp);
}

public static void SetIniFilename(string filename)
{
_backend.SetIniFilename(filename);
}
}
#endif
6 changes: 6 additions & 0 deletions addons/imgui-godot/ImGuiGodot/Internal/BackendNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -94,5 +95,10 @@ public void ToolInit()
_gd.Call(MethodName.ToolInit);
ImGuiSync.SyncPtrs();
}

public void SetIniFilename(string filename)
{
_gd.Call(MethodName.SetIniFilename, filename);
}
}
#endif
5 changes: 5 additions & 0 deletions addons/imgui-godot/ImGuiGodot/Internal/BackendNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions addons/imgui-godot/ImGuiGodot/Internal/IBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion addons/imgui-godot/ImGuiGodot/Internal/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions doc/test/csharp/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions doc/test/gdscript/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ extends "res://test_base.gd"
signal within_process

func _ready() -> void:
ImGuiGD.SetIniFilename("")

await within_process

# ImGuiConfig loads properly
Expand All @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions gdext/include/imgui-godot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
3 changes: 1 addition & 2 deletions gdext/src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ void Init(const Ref<Resource>& 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)
Expand Down
6 changes: 6 additions & 0 deletions gdext/src/ImGuiGD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions gdext/src/ImGuiGD.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c1c2978

Please sign in to comment.