[API] Remove lazy-created sructs and add methods to verify struct pointer

This commit is contained in:
SpaiR committed 2021-02-27 16:15:11 +02:00
1 parent c33331e13b
commit 91d636a659
8 files changed
+60 -75

No files matched your search

+5 -15
View File
@@ -7,8 +7,8 @@ import imgui.binding.ImGuiStructDestroyable;
* ImFontAtlas automatically loads a default embedded font for you when you call GetTexDataAsAlpha8() or GetTexDataAsRGBA32(). * ImFontAtlas automatically loads a default embedded font for you when you call GetTexDataAsAlpha8() or GetTexDataAsRGBA32().
*/ */
public final class ImFont extends ImGuiStructDestroyable { public final class ImFont extends ImGuiStructDestroyable {
private ImFontGlyph fallbackGlyph = null; private final ImFontGlyph fallbackGlyph = new ImFontGlyph(0);
private ImFontGlyph foundGlyph = new ImFontGlyph(0); private final ImFontGlyph foundGlyph = new ImFontGlyph(0);
public ImFont() { public ImFont() {
} }
@@ -64,9 +64,7 @@ public final class ImFont extends ImGuiStructDestroyable {
* = FindGlyph(FontFallbackChar) * = FindGlyph(FontFallbackChar)
*/ */
public ImFontGlyph getFallbackGlyph() { public ImFontGlyph getFallbackGlyph() {
if (fallbackGlyph == null) { fallbackGlyph.ptr = nGetFallbackGlyphPtr();
fallbackGlyph = new ImFontGlyph(nGetFallbackGlyphPtr());
}
return fallbackGlyph; return fallbackGlyph;
} }
@@ -172,11 +170,7 @@ public final class ImFont extends ImGuiStructDestroyable {
// Methods // Methods
public ImFontGlyph findGlyph(final int c) { public ImFontGlyph findGlyph(final int c) {
final long ptr = nFindGlyph(c); foundGlyph.ptr = nFindGlyph(c);
if (ptr == 0) {
return null;
}
foundGlyph.ptr = ptr;
return foundGlyph; return foundGlyph;
} }
@@ -185,11 +179,7 @@ public final class ImFont extends ImGuiStructDestroyable {
*/ */
public ImFontGlyph findGlyphNoFallback(final int c) { public ImFontGlyph findGlyphNoFallback(final int c) {
final long ptr = nFindGlyphNoFallback(c); foundGlyph.ptr = nFindGlyphNoFallback(c);
if (ptr == 0) {
return null;
}
foundGlyph.ptr = ptr;
return foundGlyph; return foundGlyph;
} }
+21 -25
View File
@@ -32,11 +32,11 @@ public class ImGui {
private static final ImGuiViewport WINDOW_VIEWPORT; private static final ImGuiViewport WINDOW_VIEWPORT;
private static final ImGuiViewport FIND_VIEWPORT; private static final ImGuiViewport FIND_VIEWPORT;
private static ImDrawData drawData; private static final ImDrawData DRAW_DATA;
private static ImFont font; private static final ImFont FONT;
private static ImGuiStyle style; private static final ImGuiStyle STYLE;
private static ImGuiViewport mainViewport; private static final ImGuiViewport MAIN_VIEWPORT;
private static ImGuiPlatformIO platformIO; private static final ImGuiPlatformIO PLATFORM_IO;
static { static {
final String libPath = System.getProperty(LIB_PATH_PROP); final String libPath = System.getProperty(LIB_PATH_PROP);
@@ -61,6 +61,12 @@ public class ImGui {
WINDOW_VIEWPORT = new ImGuiViewport(0); WINDOW_VIEWPORT = new ImGuiViewport(0);
FIND_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(); nInitJni();
ImFontAtlas.nInit(); ImFontAtlas.nInit();
ImGuiPlatformIO.init(); ImGuiPlatformIO.init();
@@ -164,10 +170,8 @@ public class ImGui {
* Access the Style structure (colors, sizes). Always use PushStyleCol(), PushStyleVar() to modify style mid-frame! * Access the Style structure (colors, sizes). Always use PushStyleCol(), PushStyleVar() to modify style mid-frame!
*/ */
public static ImGuiStyle getStyle() { public static ImGuiStyle getStyle() {
if (style == null) { STYLE.ptr = nGetStyle();
style = new ImGuiStyle(nGetStyle()); return STYLE;
}
return style;
} }
private static native long nGetStyle(); /* 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. * Valid after Render() and until the next call to NewFrame(). this is what you have to render.
*/ */
public static ImDrawData getDrawData() { public static ImDrawData getDrawData() {
if (drawData == null) { DRAW_DATA.ptr = nGetDrawData();
drawData = new ImDrawData(nGetDrawData()); return DRAW_DATA;
}
return drawData;
} }
private static native long nGetDrawData(); /* private static native long nGetDrawData(); /*
@@ -1094,10 +1096,8 @@ public class ImGui {
* Get current font. * Get current font.
*/ */
public static ImFont getFont() { public static ImFont getFont() {
if (font == null) { FONT.ptr = nGetFont();
font = new ImFont(nGetFont()); return FONT;
}
return font;
} }
private static native long nGetFont(); /* private static native long nGetFont(); /*
@@ -5121,10 +5121,8 @@ public class ImGui {
* Return primary/default viewport. * Return primary/default viewport.
*/ */
public static ImGuiViewport getMainViewport() { public static ImGuiViewport getMainViewport() {
if (mainViewport == null) { MAIN_VIEWPORT.ptr = nGetMainViewport();
mainViewport = new ImGuiViewport(nGetMainViewport()); return MAIN_VIEWPORT;
}
return mainViewport;
} }
private static native long nGetMainViewport(); /* private static native long nGetMainViewport(); /*
@@ -5682,10 +5680,8 @@ public class ImGui {
* Platform/renderer functions, for backend to setup + viewports list. * Platform/renderer functions, for backend to setup + viewports list.
*/ */
public static ImGuiPlatformIO getPlatformIO() { public static ImGuiPlatformIO getPlatformIO() {
if (platformIO == null) { PLATFORM_IO.ptr = nGetPlatformIO();
platformIO = new ImGuiPlatformIO(nGetPlatformIO()); return PLATFORM_IO;
}
return platformIO;
} }
private static native long nGetPlatformIO(); /* private static native long nGetPlatformIO(); /*
@@ -8,7 +8,7 @@ import imgui.callback.ImStrSupplier;
* Access via ImGui::GetIO(). Read 'Programmer guide' section in .cpp file for general usage. * Access via ImGui::GetIO(). Read 'Programmer guide' section in .cpp file for general usage.
*/ */
public final class ImGuiIO { public final class ImGuiIO {
private ImFontAtlas imFontAtlas; private final ImFontAtlas imFontAtlas = new ImFontAtlas(0);
ImGuiIO() { ImGuiIO() {
} }
@@ -240,9 +240,7 @@ public final class ImGuiIO {
* Font atlas: load, rasterize and pack one or more fonts into a single texture. * Font atlas: load, rasterize and pack one or more fonts into a single texture.
*/ */
public ImFontAtlas getFonts() { public ImFontAtlas getFonts() {
if (imFontAtlas == null) { imFontAtlas.ptr = nGetFonts();
imFontAtlas = new ImFontAtlas(nGetFonts());
}
return imFontAtlas; return imFontAtlas;
} }
@@ -256,7 +254,6 @@ public final class ImGuiIO {
* BINDING NOTICE: You SHOULD manually destroy previously used ImFontAtlas. * BINDING NOTICE: You SHOULD manually destroy previously used ImFontAtlas.
*/ */
public void setFonts(final ImFontAtlas imFontAtlas) { public void setFonts(final ImFontAtlas imFontAtlas) {
this.imFontAtlas = imFontAtlas;
nSetFonts(imFontAtlas.ptr); nSetFonts(imFontAtlas.ptr);
} }
@@ -3,11 +3,13 @@ package imgui.binding;
import imgui.ImGui; 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 { 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; public long ptr;
@@ -15,4 +17,22 @@ public abstract class ImGuiStruct {
ImGui.init(); ImGui.init();
this.ptr = ptr; 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();
}
} }
@@ -12,7 +12,7 @@ import imgui.type.ImInt;
* Refer to the library's Github page for examples and support * Refer to the library's Github page for examples and support
*/ */
public final class ImNodes { public final class ImNodes {
private static ImNodesStyle style; private static final ImNodesStyle STYLE = new ImNodesStyle(0);
private ImNodes() { private ImNodes() {
} }
@@ -61,10 +61,8 @@ public final class ImNodes {
* Returns the global style struct. See the struct declaration for default values. * Returns the global style struct. See the struct declaration for default values.
*/ */
public static ImNodesStyle getStyle() { public static ImNodesStyle getStyle() {
if (style == null) { STYLE.ptr = nGetStyle();
style = new ImNodesStyle(nGetStyle()); return STYLE;
}
return style;
} }
private static native long nGetStyle(); /* private static native long nGetStyle(); /*
@@ -3,7 +3,6 @@ package imgui.extension.imnodes;
import imgui.binding.ImGuiStructDestroyable; import imgui.binding.ImGuiStructDestroyable;
public final class ImNodesContext extends ImGuiStructDestroyable { public final class ImNodesContext extends ImGuiStructDestroyable {
public ImNodesContext() { public ImNodesContext() {
} }
@@ -25,7 +25,7 @@ public final class NodeEditor {
private static final ImDrawList HINT_BACKGROUND_DRAW_LIST = new ImDrawList(0); private static final ImDrawList HINT_BACKGROUND_DRAW_LIST = new ImDrawList(0);
private static final ImDrawList NODE_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() { private NodeEditor() {
} }
@@ -128,10 +128,8 @@ public final class NodeEditor {
*/ */
public static NodeEditorStyle getStyle() { public static NodeEditorStyle getStyle() {
if (style == null) { STYLE.ptr = nGetStyle();
style = new NodeEditorStyle(nGetStyle()); return STYLE;
}
return style;
} }
private static native long nGetStyle(); /* private static native long nGetStyle(); /*
@@ -198,13 +196,8 @@ public final class NodeEditor {
} }
public static ImDrawList getNodeBackgroundDrawList(final long nodeId) { public static ImDrawList getNodeBackgroundDrawList(final long nodeId) {
final long ptr = nGetNodeBackgroundDrawList(nodeId); NODE_BACKGROUND_DRAW_LIST.ptr = nGetNodeBackgroundDrawList(nodeId);
if (ptr == 0) { return NODE_BACKGROUND_DRAW_LIST;
return null;
} else {
NODE_BACKGROUND_DRAW_LIST.ptr = ptr;
return NODE_BACKGROUND_DRAW_LIST;
}
} }
private static native long nGetHintForegroundDrawList(); /* private static native long nGetHintForegroundDrawList(); /*
@@ -51,11 +51,7 @@ public final class ImGui extends imgui.ImGui {
*/ */
public static ImGuiDockNode dockBuilderGetNode(final int nodeId) { public static ImGuiDockNode dockBuilderGetNode(final int nodeId) {
final long ptr = nDockBuilderGetNode(nodeId); DOCK_NODE.ptr = nDockBuilderGetNode(nodeId);
if (ptr == 0) {
return null;
}
DOCK_NODE.ptr = ptr;
return DOCK_NODE; return DOCK_NODE;
} }
@@ -64,11 +60,7 @@ public final class ImGui extends imgui.ImGui {
*/ */
public static ImGuiDockNode dockBuilderGetCentralNode(final int nodeId) { public static ImGuiDockNode dockBuilderGetCentralNode(final int nodeId) {
final long ptr = nDockBuilderGetCentralNode(nodeId); DOCK_NODE.ptr = nDockBuilderGetCentralNode(nodeId);
if (ptr == 0) {
return null;
}
DOCK_NODE.ptr = ptr;
return DOCK_NODE; return DOCK_NODE;
} }