From 91d636a659da5a3e26d39d1ba6daa75b3beb1fd2 Mon Sep 17 00:00:00 2001 From: SpaiR Date: Sat, 27 Feb 2021 10:13:49 +0200 Subject: [PATCH] [API] Remove lazy-created sructs and add methods to verify struct pointer --- imgui-binding/src/main/java/imgui/ImFont.java | 20 ++------ imgui-binding/src/main/java/imgui/ImGui.java | 46 +++++++++---------- .../src/main/java/imgui/ImGuiIO.java | 7 +-- .../main/java/imgui/binding/ImGuiStruct.java | 24 +++++++++- .../java/imgui/extension/imnodes/ImNodes.java | 8 ++-- .../extension/imnodes/ImNodesContext.java | 1 - .../imgui/extension/nodeditor/NodeEditor.java | 17 ++----- .../src/main/java/imgui/internal/ImGui.java | 12 +---- 8 files changed, 60 insertions(+), 75 deletions(-) diff --git a/imgui-binding/src/main/java/imgui/ImFont.java b/imgui-binding/src/main/java/imgui/ImFont.java index 1ca77f1..15bec71 100644 --- a/imgui-binding/src/main/java/imgui/ImFont.java +++ b/imgui-binding/src/main/java/imgui/ImFont.java @@ -7,8 +7,8 @@ import imgui.binding.ImGuiStructDestroyable; * ImFontAtlas automatically loads a default embedded font for you when you call GetTexDataAsAlpha8() or GetTexDataAsRGBA32(). */ public final class ImFont extends ImGuiStructDestroyable { - private ImFontGlyph fallbackGlyph = null; - private ImFontGlyph foundGlyph = new ImFontGlyph(0); + private final ImFontGlyph fallbackGlyph = new ImFontGlyph(0); + private final ImFontGlyph foundGlyph = new ImFontGlyph(0); public ImFont() { } @@ -64,9 +64,7 @@ public final class ImFont extends ImGuiStructDestroyable { * = FindGlyph(FontFallbackChar) */ public ImFontGlyph getFallbackGlyph() { - if (fallbackGlyph == null) { - fallbackGlyph = new ImFontGlyph(nGetFallbackGlyphPtr()); - } + fallbackGlyph.ptr = nGetFallbackGlyphPtr(); return fallbackGlyph; } @@ -172,11 +170,7 @@ public final class ImFont extends ImGuiStructDestroyable { // Methods public ImFontGlyph findGlyph(final int c) { - final long ptr = nFindGlyph(c); - if (ptr == 0) { - return null; - } - foundGlyph.ptr = ptr; + foundGlyph.ptr = nFindGlyph(c); return foundGlyph; } @@ -185,11 +179,7 @@ public final class ImFont extends ImGuiStructDestroyable { */ public ImFontGlyph findGlyphNoFallback(final int c) { - final long ptr = nFindGlyphNoFallback(c); - if (ptr == 0) { - return null; - } - foundGlyph.ptr = ptr; + foundGlyph.ptr = nFindGlyphNoFallback(c); return foundGlyph; } diff --git a/imgui-binding/src/main/java/imgui/ImGui.java b/imgui-binding/src/main/java/imgui/ImGui.java index 1fa368a..e49085e 100644 --- a/imgui-binding/src/main/java/imgui/ImGui.java +++ b/imgui-binding/src/main/java/imgui/ImGui.java @@ -32,11 +32,11 @@ public class ImGui { private static final ImGuiViewport WINDOW_VIEWPORT; private static final ImGuiViewport FIND_VIEWPORT; - private static ImDrawData drawData; - private static ImFont font; - private static ImGuiStyle style; - private static ImGuiViewport mainViewport; - private static ImGuiPlatformIO platformIO; + private static final ImDrawData DRAW_DATA; + private static final ImFont FONT; + private static final ImGuiStyle STYLE; + private static final ImGuiViewport MAIN_VIEWPORT; + private static final ImGuiPlatformIO PLATFORM_IO; static { final String libPath = System.getProperty(LIB_PATH_PROP); @@ -61,6 +61,12 @@ public class ImGui { WINDOW_VIEWPORT = new ImGuiViewport(0); FIND_VIEWPORT = new ImGuiViewport(0); + DRAW_DATA = new ImDrawData(0); + FONT = new ImFont(0); + STYLE = new ImGuiStyle(0); + MAIN_VIEWPORT = new ImGuiViewport(0); + PLATFORM_IO = new ImGuiPlatformIO(0); + nInitJni(); ImFontAtlas.nInit(); ImGuiPlatformIO.init(); @@ -164,10 +170,8 @@ public class ImGui { * Access the Style structure (colors, sizes). Always use PushStyleCol(), PushStyleVar() to modify style mid-frame! */ public static ImGuiStyle getStyle() { - if (style == null) { - style = new ImGuiStyle(nGetStyle()); - } - return style; + STYLE.ptr = nGetStyle(); + return STYLE; } private static native long nGetStyle(); /* @@ -200,10 +204,8 @@ public class ImGui { * Valid after Render() and until the next call to NewFrame(). this is what you have to render. */ public static ImDrawData getDrawData() { - if (drawData == null) { - drawData = new ImDrawData(nGetDrawData()); - } - return drawData; + DRAW_DATA.ptr = nGetDrawData(); + return DRAW_DATA; } private static native long nGetDrawData(); /* @@ -1094,10 +1096,8 @@ public class ImGui { * Get current font. */ public static ImFont getFont() { - if (font == null) { - font = new ImFont(nGetFont()); - } - return font; + FONT.ptr = nGetFont(); + return FONT; } private static native long nGetFont(); /* @@ -5121,10 +5121,8 @@ public class ImGui { * Return primary/default viewport. */ public static ImGuiViewport getMainViewport() { - if (mainViewport == null) { - mainViewport = new ImGuiViewport(nGetMainViewport()); - } - return mainViewport; + MAIN_VIEWPORT.ptr = nGetMainViewport(); + return MAIN_VIEWPORT; } private static native long nGetMainViewport(); /* @@ -5682,10 +5680,8 @@ public class ImGui { * Platform/renderer functions, for backend to setup + viewports list. */ public static ImGuiPlatformIO getPlatformIO() { - if (platformIO == null) { - platformIO = new ImGuiPlatformIO(nGetPlatformIO()); - } - return platformIO; + PLATFORM_IO.ptr = nGetPlatformIO(); + return PLATFORM_IO; } private static native long nGetPlatformIO(); /* diff --git a/imgui-binding/src/main/java/imgui/ImGuiIO.java b/imgui-binding/src/main/java/imgui/ImGuiIO.java index 33f16d8..27f5f06 100644 --- a/imgui-binding/src/main/java/imgui/ImGuiIO.java +++ b/imgui-binding/src/main/java/imgui/ImGuiIO.java @@ -8,7 +8,7 @@ import imgui.callback.ImStrSupplier; * Access via ImGui::GetIO(). Read 'Programmer guide' section in .cpp file for general usage. */ public final class ImGuiIO { - private ImFontAtlas imFontAtlas; + private final ImFontAtlas imFontAtlas = new ImFontAtlas(0); ImGuiIO() { } @@ -240,9 +240,7 @@ public final class ImGuiIO { * Font atlas: load, rasterize and pack one or more fonts into a single texture. */ public ImFontAtlas getFonts() { - if (imFontAtlas == null) { - imFontAtlas = new ImFontAtlas(nGetFonts()); - } + imFontAtlas.ptr = nGetFonts(); return imFontAtlas; } @@ -256,7 +254,6 @@ public final class ImGuiIO { * BINDING NOTICE: You SHOULD manually destroy previously used ImFontAtlas. */ public void setFonts(final ImFontAtlas imFontAtlas) { - this.imFontAtlas = imFontAtlas; nSetFonts(imFontAtlas.ptr); } diff --git a/imgui-binding/src/main/java/imgui/binding/ImGuiStruct.java b/imgui-binding/src/main/java/imgui/binding/ImGuiStruct.java index 67701f1..5f28f30 100644 --- a/imgui-binding/src/main/java/imgui/binding/ImGuiStruct.java +++ b/imgui-binding/src/main/java/imgui/binding/ImGuiStruct.java @@ -3,11 +3,13 @@ package imgui.binding; import imgui.ImGui; /** - * Class provides a communication layer between binding and Dear ImGui library though the "pointer" field. + * Class provides a communication layer between binding and Dear ImGui library through the "pointer" field. */ public abstract class ImGuiStruct { /** - * Pointer to the natively allocated object, + * Pointer to the natively allocated object. + * Could be zero if JNI returned NULL instead of the real object. + * For such cases methods {@link #isValidPtr()} and {@link #isNotValidPtr()} could be used to verify that object points to the real object. */ public long ptr; @@ -15,4 +17,22 @@ public abstract class ImGuiStruct { ImGui.init(); this.ptr = ptr; } + + /** + * Shows if object pointer is valid and does it point to the existing object. + * + * @return true, if pointer is valid + */ + public final boolean isValidPtr() { + return ptr != 0; + } + + /** + * Shows if object pointer is valid and does it point to the existing object. + * + * @return true, if pointer is invalid + */ + public final boolean isNotValidPtr() { + return !isValidPtr(); + } } diff --git a/imgui-binding/src/main/java/imgui/extension/imnodes/ImNodes.java b/imgui-binding/src/main/java/imgui/extension/imnodes/ImNodes.java index ce51cc7..3b012ca 100644 --- a/imgui-binding/src/main/java/imgui/extension/imnodes/ImNodes.java +++ b/imgui-binding/src/main/java/imgui/extension/imnodes/ImNodes.java @@ -12,7 +12,7 @@ import imgui.type.ImInt; * Refer to the library's Github page for examples and support */ public final class ImNodes { - private static ImNodesStyle style; + private static final ImNodesStyle STYLE = new ImNodesStyle(0); private ImNodes() { } @@ -61,10 +61,8 @@ public final class ImNodes { * Returns the global style struct. See the struct declaration for default values. */ public static ImNodesStyle getStyle() { - if (style == null) { - style = new ImNodesStyle(nGetStyle()); - } - return style; + STYLE.ptr = nGetStyle(); + return STYLE; } private static native long nGetStyle(); /* diff --git a/imgui-binding/src/main/java/imgui/extension/imnodes/ImNodesContext.java b/imgui-binding/src/main/java/imgui/extension/imnodes/ImNodesContext.java index 122bcf8..b948744 100644 --- a/imgui-binding/src/main/java/imgui/extension/imnodes/ImNodesContext.java +++ b/imgui-binding/src/main/java/imgui/extension/imnodes/ImNodesContext.java @@ -3,7 +3,6 @@ package imgui.extension.imnodes; import imgui.binding.ImGuiStructDestroyable; public final class ImNodesContext extends ImGuiStructDestroyable { - public ImNodesContext() { } diff --git a/imgui-binding/src/main/java/imgui/extension/nodeditor/NodeEditor.java b/imgui-binding/src/main/java/imgui/extension/nodeditor/NodeEditor.java index 52f9de1..2f9bb33 100644 --- a/imgui-binding/src/main/java/imgui/extension/nodeditor/NodeEditor.java +++ b/imgui-binding/src/main/java/imgui/extension/nodeditor/NodeEditor.java @@ -25,7 +25,7 @@ public final class NodeEditor { private static final ImDrawList HINT_BACKGROUND_DRAW_LIST = new ImDrawList(0); private static final ImDrawList NODE_BACKGROUND_DRAW_LIST = new ImDrawList(0); - private static NodeEditorStyle style; + private static final NodeEditorStyle STYLE = new NodeEditorStyle(0); private NodeEditor() { } @@ -128,10 +128,8 @@ public final class NodeEditor { */ public static NodeEditorStyle getStyle() { - if (style == null) { - style = new NodeEditorStyle(nGetStyle()); - } - return style; + STYLE.ptr = nGetStyle(); + return STYLE; } private static native long nGetStyle(); /* @@ -198,13 +196,8 @@ public final class NodeEditor { } public static ImDrawList getNodeBackgroundDrawList(final long nodeId) { - final long ptr = nGetNodeBackgroundDrawList(nodeId); - if (ptr == 0) { - return null; - } else { - NODE_BACKGROUND_DRAW_LIST.ptr = ptr; - return NODE_BACKGROUND_DRAW_LIST; - } + NODE_BACKGROUND_DRAW_LIST.ptr = nGetNodeBackgroundDrawList(nodeId); + return NODE_BACKGROUND_DRAW_LIST; } private static native long nGetHintForegroundDrawList(); /* diff --git a/imgui-binding/src/main/java/imgui/internal/ImGui.java b/imgui-binding/src/main/java/imgui/internal/ImGui.java index 93d9a09..0e62340 100644 --- a/imgui-binding/src/main/java/imgui/internal/ImGui.java +++ b/imgui-binding/src/main/java/imgui/internal/ImGui.java @@ -51,11 +51,7 @@ public final class ImGui extends imgui.ImGui { */ public static ImGuiDockNode dockBuilderGetNode(final int nodeId) { - final long ptr = nDockBuilderGetNode(nodeId); - if (ptr == 0) { - return null; - } - DOCK_NODE.ptr = ptr; + DOCK_NODE.ptr = nDockBuilderGetNode(nodeId); return DOCK_NODE; } @@ -64,11 +60,7 @@ public final class ImGui extends imgui.ImGui { */ public static ImGuiDockNode dockBuilderGetCentralNode(final int nodeId) { - final long ptr = nDockBuilderGetCentralNode(nodeId); - if (ptr == 0) { - return null; - } - DOCK_NODE.ptr = ptr; + DOCK_NODE.ptr = nDockBuilderGetCentralNode(nodeId); return DOCK_NODE; }