Skip to content

Commit

Permalink
[API] Add ImGuiContext struct and ability to switch Dear ImGui contex…
Browse files Browse the repository at this point in the history
…ts (#65)

Struct itself has no fields or methods. It can be used for context switching though.

* Implement ImGuiContext

* Move ImGuiContext from imgui to imgui.internal, minor bugfixes

resolves #17
  • Loading branch information
perrymacmurray committed Jul 13, 2021
1 parent 0bcbb04 commit 78694a4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
46 changes: 40 additions & 6 deletions imgui-binding/src/main/java/imgui/ImGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import imgui.flag.ImGuiCond;
import imgui.flag.ImGuiDragDropFlags;
import imgui.flag.ImGuiInputTextFlags;
import imgui.internal.ImGuiContext;
import imgui.type.ImBoolean;
import imgui.type.ImDouble;
import imgui.type.ImFloat;
Expand All @@ -26,6 +27,7 @@ public class ImGui {
private static final String LIB_NAME_DEFAULT = System.getProperty("os.arch").contains("64") ? "imgui-java64" : "imgui-java";
private static final String LIB_TMP_DIR_PREFIX = "imgui-java-natives_" + System.currentTimeMillis();

private static final ImGuiContext IMGUI_CONTEXT;
private static final ImGuiIO IMGUI_IO;
private static final ImDrawList WINDOW_DRAW_LIST;
private static final ImDrawList BACKGROUND_DRAW_LIST;
Expand Down Expand Up @@ -55,6 +57,7 @@ public class ImGui {
System.loadLibrary(libName);
}

IMGUI_CONTEXT = new ImGuiContext(0);
IMGUI_IO = new ImGuiIO(0);
WINDOW_DRAW_LIST = new ImDrawList(0);
BACKGROUND_DRAW_LIST = new ImDrawList(0);
Expand Down Expand Up @@ -140,22 +143,53 @@ public static void init() {
//
// BINDING NOTICE: Getting of the current context is not implemented since it's a part of internal API which is not exposed here.

public static native void createContext(); /*
ImGui::CreateContext();
public static ImGuiContext createContext() {
IMGUI_CONTEXT.ptr = nCreateContext();
return IMGUI_CONTEXT;
}

private static native long nCreateContext(); /*
return (intptr_t)ImGui::CreateContext();
*/

public static void createContext(ImFontAtlas sharedFontAtlas) {
nCreateContext(sharedFontAtlas.ptr);
public static ImGuiContext createContext(ImFontAtlas sharedFontAtlas) {
IMGUI_CONTEXT.ptr = nCreateContext(sharedFontAtlas.ptr);
return IMGUI_CONTEXT;
}

private static native void nCreateContext(long sharedFontAtlasPtr); /*
ImGui::CreateContext((ImFontAtlas*)sharedFontAtlasPtr);
private static native long nCreateContext(long sharedFontAtlasPtr); /*
return (intptr_t)ImGui::CreateContext((ImFontAtlas*)sharedFontAtlasPtr);
*/

public static native void destroyContext(); /*
ImGui::DestroyContext();
*/

public static void destroyContext(ImGuiContext ctx) {
nDestroyContext(ctx.ptr);
}

private static native void nDestroyContext(long ptr); /*
ImGui::DestroyContext((ImGuiContext*) ptr);
*/

public static ImGuiContext getCurrentContext() {
IMGUI_CONTEXT.ptr = nGetCurrentContext();
return IMGUI_CONTEXT;
}

private static native long nGetCurrentContext(); /*
return (intptr_t)ImGui::GetCurrentContext();
*/

public static void setCurrentContext(ImGuiContext ctx) {
nSetCurrentContext(ctx.ptr);
}

private static native void nSetCurrentContext(long ptr); /*
ImGui::SetCurrentContext((ImGuiContext*) ptr);
*/

// Main

/**
Expand Down
9 changes: 9 additions & 0 deletions imgui-binding/src/main/java/imgui/internal/ImGuiContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package imgui.internal;

import imgui.binding.ImGuiStruct;

public class ImGuiContext extends ImGuiStruct {
public ImGuiContext(final long ptr) {
super(ptr);
}
}

0 comments on commit 78694a4

Please sign in to comment.