Refactor mapping to the native structs

This commit is contained in:
SpaiR committed 2020-08-23 16:49:16 +03:00
1 parent 1209caa518
commit 1dcf2328a4
17 files changed
+194 -251

No files matched your search

+4
View File
@@ -241,6 +241,10 @@ Dear ImGui by default uses a stb_strutype library to render a fonts atlas. It's
* To get an input/output to/from Dear ImGui - use primitive wrappers: `ImBool`, `ImInt` etc. * To get an input/output to/from Dear ImGui - use primitive wrappers: `ImBool`, `ImInt` etc.
* Due to the Java and JNI restrictions we can't provide a fully fledged callbacks to the `ImGui::InputText` methods. * Due to the Java and JNI restrictions we can't provide a fully fledged callbacks to the `ImGui::InputText` methods.
To replace some features use an ImGuiInputTextData class. To replace some features use an ImGuiInputTextData class.
* All classes which are represent Dear ImGui structs have a field with a pointer to those structs.
Field is public and could be modified without restrictions. So by changing the pointer it's possible to use the same java class to modify different
native structs. Commonly you don't need to mind about that.
Just keep in mind that you are able to do advanced stuff with it like: save pointers to the structs to modify them later.
* Read [javadoc](https://javadoc.io/doc/io.imgui.java/binding) and sources comments to get more info. * Read [javadoc](https://javadoc.io/doc/io.imgui.java/binding) and sources comments to get more info.
## How to Build ## How to Build
@@ -1,5 +1,7 @@
package imgui; package imgui;
import imgui.binding.ImGuiStruct;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
@@ -11,36 +13,28 @@ import java.nio.ByteOrder;
* BINDING NOTICE: Since it's impossible to do a 1:1 mapping with JNI, current class provides "getCmdList*()" methods. * BINDING NOTICE: Since it's impossible to do a 1:1 mapping with JNI, current class provides "getCmdList*()" methods.
* Those are used to get the data needed to do a rendering. * Those are used to get the data needed to do a rendering.
*/ */
public final class ImDrawData { public final class ImDrawData extends ImGuiStruct {
public static final int SIZEOF_IM_DRAW_IDX = 2; public static final int SIZEOF_IM_DRAW_IDX = 2;
public static final int SIZEOF_IM_DRAW_VERT = (4 + 1) * 4; public static final int SIZEOF_IM_DRAW_VERT = (4 + 1) * 4;
private static final int FACTOR = 5_000; private static final int FACTOR = 5_000;
final long ptr;
private ByteBuffer idxBuffer = ByteBuffer.allocateDirect(SIZEOF_IM_DRAW_IDX * FACTOR).order(ByteOrder.nativeOrder()); private ByteBuffer idxBuffer = ByteBuffer.allocateDirect(SIZEOF_IM_DRAW_IDX * FACTOR).order(ByteOrder.nativeOrder());
private ByteBuffer vtxBuffer = ByteBuffer.allocateDirect(SIZEOF_IM_DRAW_VERT * FACTOR).order(ByteOrder.nativeOrder()); private ByteBuffer vtxBuffer = ByteBuffer.allocateDirect(SIZEOF_IM_DRAW_VERT * FACTOR).order(ByteOrder.nativeOrder());
ImDrawData(final long ptr) { public ImDrawData(final long ptr) {
this.ptr = ptr; super(ptr);
} }
/*JNI /*JNI
#include <stdint.h> #include <stdint.h>
#include <imgui.h> #include <imgui.h>
#include "jni_common.h" #include "jni_common.h"
#include "jni_binding_struct.h"
jfieldID imDrawDataPtrID; #define IM_DRAW_DATA ((ImDrawData*)STRUCT_PTR)
#define IM_DRAW_DATA ((ImDrawData*)env->GetLongField(object, imDrawDataPtrID))
*/ */
static native void nInit(); /*
jclass jImDrawDataClass = env->FindClass("imgui/ImDrawData");
imDrawDataPtrID = env->GetFieldID(jImDrawDataClass, "ptr", "J");
*/
///////// Start of Render Methods | Binding ///////// Start of Render Methods | Binding
/** /**
@@ -1,5 +1,7 @@
package imgui; package imgui;
import imgui.binding.ImGuiStruct;
/** /**
* Draw command list * Draw command list
* This is the low-level list of polygons that ImGui:: functions are filling. At the end of the frame, * This is the low-level list of polygons that ImGui:: functions are filling. At the end of the frame,
@@ -11,28 +13,20 @@ package imgui;
* but you are totally free to apply whatever transformation matrix to want to the data (if you apply such transformation you'll want to apply it to ClipRect as well) * but you are totally free to apply whatever transformation matrix to want to the data (if you apply such transformation you'll want to apply it to ClipRect as well)
* Important: Primitives are always added to the list and not culled (culling is done at higher-level by ImGui:: functions), if you use this API a lot consider coarse culling your drawn objects. * Important: Primitives are always added to the list and not culled (culling is done at higher-level by ImGui:: functions), if you use this API a lot consider coarse culling your drawn objects.
*/ */
public final class ImDrawList { public final class ImDrawList extends ImGuiStruct {
long ptr; public ImDrawList(final long ptr) {
super(ptr);
ImDrawList(final long ptr) {
this.ptr = ptr;
} }
/*JNI /*JNI
#include <stdint.h> #include <stdint.h>
#include <imgui.h> #include <imgui.h>
#include "jni_common.h" #include "jni_common.h"
#include "jni_binding_struct.h"
jfieldID imDrawListPtrID; #define IM_DRAW_LIST ((ImDrawList*)STRUCT_PTR)
#define IM_DRAW_LIST ((ImDrawList*)env->GetLongField(object, imDrawListPtrID))
*/ */
static native void nInit(); /*
jclass jImDrawListClass = env->FindClass("imgui/ImDrawList");
imDrawListPtrID = env->GetFieldID(jImDrawListClass, "ptr", "J");
*/
/** /**
* Flags, you may poke into these to adjust anti-aliasing settings per-primitive. * Flags, you may poke into these to adjust anti-aliasing settings per-primitive.
*/ */
+12 -29
View File
@@ -1,54 +1,37 @@
package imgui; package imgui;
import imgui.binding.ImGuiStructDestroyable;
/** /**
* Font runtime data and rendering * Font runtime data and rendering
* 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 implements ImGuiDestroyableStruct { public final class ImFont extends ImGuiStructDestroyable {
final long ptr;
private ImFontGlyph fallbackGlyph = null; private ImFontGlyph fallbackGlyph = null;
/**
* This class will create a native structure.
* Call {@link #destroy()} method to manually free used memory.
*/
public ImFont() { public ImFont() {
ImGui.touch();
ptr = nCreate();
} }
ImFont(final long ptr) { public ImFont(final long ptr) {
this.ptr = ptr; super(ptr);
}
@Override
public void destroy() {
nDestroy(ptr);
} }
/*JNI /*JNI
#include <stdint.h> #include <stdint.h>
#include <imgui.h> #include <imgui.h>
#include "jni_common.h" #include "jni_common.h"
#include "jni_binding_struct.h"
jfieldID imFontPtrID; #define IM_FONT ((ImFont*)STRUCT_PTR)
#define IM_FONT ((ImFont*)env->GetLongField(object, imFontPtrID))
*/ */
static native void nInit(); /* @Override
jclass jImFontClass = env->FindClass("imgui/ImFont"); protected long create() {
imFontPtrID = env->GetFieldID(jImFontClass, "ptr", "J"); return nCreate();
*/ }
private native long nCreate(); /* private native long nCreate(); /*
ImFont* imFont = new ImFont(); return (intptr_t)(new ImFont());
return (intptr_t)imFont;
*/
private native void nDestroy(long ptr); /*
delete (ImFont*)ptr;
*/ */
// TODO IndexAdvanceX // TODO IndexAdvanceX
@@ -1,5 +1,6 @@
package imgui; package imgui;
import imgui.binding.ImGuiStructDestroyable;
import imgui.type.ImInt; import imgui.type.ImInt;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@@ -24,37 +25,23 @@ import java.nio.ByteOrder;
* - Even though many functions are suffixed with "TTF", OTF data is supported just as well. * - Even though many functions are suffixed with "TTF", OTF data is supported just as well.
* - This is an old API and it is currently awkward for those and and various other reasons! We will address them in the future! * - This is an old API and it is currently awkward for those and and various other reasons! We will address them in the future!
*/ */
public final class ImFontAtlas implements ImGuiDestroyableStruct { public final class ImFontAtlas extends ImGuiStructDestroyable {
final long ptr;
private ByteBuffer alpha8pixels = null; private ByteBuffer alpha8pixels = null;
private ByteBuffer rgba32pixels = null; private ByteBuffer rgba32pixels = null;
/**
* This class will create a native structure.
* Call {@link #destroy()} method to manually free used memory.
*/
public ImFontAtlas() { public ImFontAtlas() {
ImGui.touch();
ptr = nCreate();
} }
ImFontAtlas(final long ptr) { public ImFontAtlas(final long ptr) {
this.ptr = ptr; super(ptr);
}
@Override
public void destroy() {
nDestroy(ptr);
} }
/*JNI /*JNI
#include <stdint.h> #include <stdint.h>
#include <imgui.h> #include <imgui.h>
#include "jni_binding_struct.h"
jfieldID imFontAtlasPtrID; #define IM_FONT_ATLAS ((ImFontAtlas*)STRUCT_PTR)
#define IM_FONT_ATLAS ((ImFontAtlas*)env->GetLongField(object, imFontAtlasPtrID))
jmethodID jImFontAtlasCreateAlpha8PixelsMID; jmethodID jImFontAtlasCreateAlpha8PixelsMID;
jmethodID jImFontAtlasCreateRgba32PixelsMID; jmethodID jImFontAtlasCreateRgba32PixelsMID;
@@ -62,19 +49,18 @@ public final class ImFontAtlas implements ImGuiDestroyableStruct {
static native void nInit(); /* static native void nInit(); /*
jclass jImFontAtlasClass = env->FindClass("imgui/ImFontAtlas"); jclass jImFontAtlasClass = env->FindClass("imgui/ImFontAtlas");
imFontAtlasPtrID = env->GetFieldID(jImFontAtlasClass, "ptr", "J");
jImFontAtlasCreateAlpha8PixelsMID = env->GetMethodID(jImFontAtlasClass, "createAlpha8Pixels", "(I)Ljava/nio/ByteBuffer;"); jImFontAtlasCreateAlpha8PixelsMID = env->GetMethodID(jImFontAtlasClass, "createAlpha8Pixels", "(I)Ljava/nio/ByteBuffer;");
jImFontAtlasCreateRgba32PixelsMID = env->GetMethodID(jImFontAtlasClass, "createRgba32Pixels", "(I)Ljava/nio/ByteBuffer;"); jImFontAtlasCreateRgba32PixelsMID = env->GetMethodID(jImFontAtlasClass, "createRgba32Pixels", "(I)Ljava/nio/ByteBuffer;");
*/ */
private native long nCreate(); /* @Override
ImFontAtlas* imFontAtlas = new ImFontAtlas(); protected long create() {
return (intptr_t)imFontAtlas; return nCreate();
*/ }
private native void nDestroy(long ptr); /* private native long nCreate(); /*
delete (ImFontAtlas*)ptr; return (intptr_t)(new ImFontConfig());
*/ */
public ImFont addFont(final ImFontConfig imFontConfig) { public ImFont addFont(final ImFontConfig imFontConfig) {
@@ -1,8 +1,8 @@
package imgui; package imgui;
public final class ImFontConfig implements ImGuiDestroyableStruct { import imgui.binding.ImGuiStructDestroyable;
final long ptr;
public final class ImFontConfig extends ImGuiStructDestroyable {
private short[] glyphRanges; private short[] glyphRanges;
/** /**
@@ -10,43 +10,31 @@ public final class ImFontConfig implements ImGuiDestroyableStruct {
* Call {@link #destroy()} method to manually free used memory. * Call {@link #destroy()} method to manually free used memory.
*/ */
public ImFontConfig() { public ImFontConfig() {
ImGui.touch(); super();
ptr = nCreate();
setFontDataOwnedByAtlas(false); // Read method javadoc setFontDataOwnedByAtlas(false); // Read method javadoc
} }
ImFontConfig(final long ptr) { ImFontConfig(final long ptr) {
this.ptr = ptr; super(ptr);
setFontDataOwnedByAtlas(false); // Read method javadoc setFontDataOwnedByAtlas(false); // Read method javadoc
} }
@Override
public void destroy() {
nDestroy(ptr);
}
/*JNI /*JNI
#include <stdint.h> #include <stdint.h>
#include <imgui.h> #include <imgui.h>
#include "jni_common.h" #include "jni_common.h"
#include "jni_binding_struct.h"
jfieldID imFontConfigPtrID; #define IM_FONT_CONFIG ((ImFontConfig*)STRUCT_PTR)
#define IM_FONT_CONFIG ((ImFontConfig*)env->GetLongField(object, imFontConfigPtrID))
*/ */
static native void nInit(); /* @Override
jclass jImFontConfigClass = env->FindClass("imgui/ImFontConfig"); protected long create() {
imFontConfigPtrID = env->GetFieldID(jImFontConfigClass, "ptr", "J"); return nCreate();
*/ }
private native long nCreate(); /* private native long nCreate(); /*
ImFontConfig* imFontConfig = new ImFontConfig(); return (intptr_t)(new ImFontConfig());
return (intptr_t)imFontConfig;
*/
private native void nDestroy(long ptr); /*
delete (ImFontConfig*)ptr;
*/ */
/** /**
@@ -1,51 +1,34 @@
package imgui; package imgui;
import imgui.binding.ImGuiStructDestroyable;
/** /**
* Hold rendering data for one glyph. * Hold rendering data for one glyph.
* (Note: some language parsers may fail to convert the 31+1 bitfield members, in this case maybe drop store a single u32 or we can rework this) * (Note: some language parsers may fail to convert the 31+1 bitfield members, in this case maybe drop store a single u32 or we can rework this)
*/ */
public final class ImFontGlyph implements ImGuiDestroyableStruct { public final class ImFontGlyph extends ImGuiStructDestroyable {
final long ptr;
/**
* This class will create a native structure.
* Call {@link #destroy()} method to manually free used memory.
*/
public ImFontGlyph() { public ImFontGlyph() {
ImGui.touch();
ptr = nCreate();
} }
ImFontGlyph(final long ptr) { public ImFontGlyph(final long ptr) {
this.ptr = ptr; super(ptr);
}
@Override
public void destroy() {
nDestroy(ptr);
} }
/*JNI /*JNI
#include <stdint.h> #include <stdint.h>
#include <imgui.h> #include <imgui.h>
#include "jni_binding_struct.h"
jfieldID imFontGlyphPtrID; #define IM_FONT_GLYPH ((ImFontGlyph*)STRUCT_PTR)
#define IM_FONT_GLYPH ((ImFontGlyph*)env->GetLongField(object, imFontGlyphPtrID))
*/ */
static native void nInit(); /* @Override
jclass jImFontGlyphClass = env->FindClass("imgui/ImFontGlyph"); protected long create() {
imFontGlyphPtrID = env->GetFieldID(jImFontGlyphClass, "ptr", "J"); return nCreate();
*/ }
private native long nCreate(); /* private native long nCreate(); /*
ImFontConfig* imFontConfig = new ImFontConfig(); return (intptr_t)(new ImFontGlyph());
return (intptr_t)imFontConfig;
*/
private native void nDestroy(long ptr); /*
delete (ImFontConfig*)ptr;
*/ */
/** /**
+3 -10
View File
@@ -56,16 +56,7 @@ public final class ImGui {
IMGUI_STORAGE = new ImGuiStorage(0); IMGUI_STORAGE = new ImGuiStorage(0);
nInitJni(); nInitJni();
ImDrawList.nInit();
ImDrawData.nInit();
ImFontAtlas.nInit(); ImFontAtlas.nInit();
ImFontConfig.nInit();
ImFontGlyph.nInit();
ImFont.nInit();
ImGuiStyle.nInit();
ImGuiWindowClass.nInit();
ImGuiStorage.nInit();
ImGuiViewport.nInit();
nInitInputTextData(); nInitInputTextData();
} }
@@ -113,7 +104,7 @@ public final class ImGui {
* Method is used to initiate static instantiation (loading of the native libraries etc.). * Method is used to initiate static instantiation (loading of the native libraries etc.).
* Otherwise native libraries will be loaded on demand and natively mapped objects won't work. * Otherwise native libraries will be loaded on demand and natively mapped objects won't work.
*/ */
static void touch() { public static void touch() {
} }
private ImGui() { private ImGui() {
@@ -124,11 +115,13 @@ public final class ImGui {
#include <imgui.h> #include <imgui.h>
#include "jni_common.h" #include "jni_common.h"
#include "jni_callbacks.h" #include "jni_callbacks.h"
#include "jni_binding_struct.h"
*/ */
private static native void nInitJni(); /* private static native void nInitJni(); /*
Jni::InitCommon(env); Jni::InitCommon(env);
Jni::InitCallbacks(env); Jni::InitCallbacks(env);
Jni::InitBindingStruct(env);
*/ */
// Context creation and access // Context creation and access
@@ -1,8 +0,0 @@
package imgui;
/**
* When implement, class could be manually destroyed (free allocated memory on the JNI side).
*/
interface ImGuiDestroyableStruct {
void destroy();
}
@@ -1,5 +1,7 @@
package imgui; package imgui;
import imgui.binding.ImGuiStructDestroyable;
/** /**
* Helper: Key-Value storage * Helper: Key-Value storage
* Typically you don't have to worry about this since a storage is held within each Window. * Typically you don't have to worry about this since a storage is held within each Window.
@@ -10,48 +12,29 @@ package imgui;
* - You want to store custom debug data easily without adding or editing structures in your code (probably not efficient, but convenient) * - You want to store custom debug data easily without adding or editing structures in your code (probably not efficient, but convenient)
* Types are NOT stored, so it is up to you to make sure your Key don't collide with different types. * Types are NOT stored, so it is up to you to make sure your Key don't collide with different types.
*/ */
public final class ImGuiStorage implements ImGuiDestroyableStruct { public final class ImGuiStorage extends ImGuiStructDestroyable {
long ptr;
/**
* This class will create a native structure.
* Call {@link #destroy()} method to manually free used memory.
*/
public ImGuiStorage() { public ImGuiStorage() {
ImGui.touch();
ptr = nCreate();
} }
ImGuiStorage(final long ptr) { public ImGuiStorage(final long ptr) {
this.ptr = ptr; super(ptr);
}
@Override
public void destroy() {
nDestroy(ptr);
} }
/*JNI /*JNI
#include <stdint.h> #include <stdint.h>
#include <imgui.h> #include <imgui.h>
#include "jni_binding_struct.h"
jfieldID imGuiStoragePtrID; #define IMGUI_STORAGE ((ImGuiStorage*)STRUCT_PTR)
#define IMGUI_STORAGE ((ImGuiStorage*)env->GetLongField(object, imGuiStoragePtrID))
*/ */
static native void nInit(); /* @Override
jclass jImGuiStorageClass = env->FindClass("imgui/ImGuiStorage"); protected long create() {
imGuiStoragePtrID = env->GetFieldID(jImGuiStorageClass, "ptr", "J"); return nCreate();
*/ }
private native long nCreate(); /* private native long nCreate(); /*
ImGuiStorage* imGuiStorage = new ImGuiStorage(); return (intptr_t)(new ImGuiStorage());
return (intptr_t)imGuiStorage;
*/
private native void nDestroy(long ptr); /*
delete (ImGuiStorage*)ptr;
*/ */
// - Get***() functions find pair, never add/allocate. Pairs are sorted so a query is O(log N) // - Get***() functions find pair, never add/allocate. Pairs are sorted so a query is O(log N)
@@ -1,53 +1,36 @@
package imgui; package imgui;
import imgui.binding.ImGuiStructDestroyable;
/** /**
* You may modify the ImGui::GetStyle() main instance during initialization and before NewFrame(). * You may modify the ImGui::GetStyle() main instance during initialization and before NewFrame().
* During the frame, use ImGui::PushStyleVar(ImGuiStyleVar_XXXX)/PopStyleVar() to alter the main style values, * During the frame, use ImGui::PushStyleVar(ImGuiStyleVar_XXXX)/PopStyleVar() to alter the main style values,
* and ImGui::PushStyleColor(ImGuiCol_XXX)/PopStyleColor() for colors. * and ImGui::PushStyleColor(ImGuiCol_XXX)/PopStyleColor() for colors.
*/ */
public final class ImGuiStyle implements ImGuiDestroyableStruct { public final class ImGuiStyle extends ImGuiStructDestroyable {
final long ptr;
/**
* This class will create a native structure.
* Call {@link #destroy()} method to manually free used memory.
*/
public ImGuiStyle() { public ImGuiStyle() {
ImGui.touch();
ptr = nCreate();
} }
ImGuiStyle(final long ptr) { public ImGuiStyle(final long ptr) {
this.ptr = ptr; super(ptr);
}
@Override
public void destroy() {
nDestroy(ptr);
} }
/*JNI /*JNI
#include <imgui.h> #include <imgui.h>
#include <stdint.h> #include <stdint.h>
#include "jni_common.h" #include "jni_common.h"
#include "jni_binding_struct.h"
jfieldID imGuiStylePtrID; #define IMGUI_STYLE ((ImGuiStyle*)STRUCT_PTR)
#define IMGUI_STYLE ((ImGuiStyle*)env->GetLongField(object, imGuiStylePtrID))
*/ */
static native void nInit(); /* @Override
jclass jImGuiStyleClass = env->FindClass("imgui/ImGuiStyle"); protected long create() {
imGuiStylePtrID = env->GetFieldID(jImGuiStyleClass, "ptr", "J"); return nCreate();
*/ }
private native long nCreate(); /* private native long nCreate(); /*
ImGuiStyle* imGuiStyle = new ImGuiStyle(); return (intptr_t)(new ImGuiStyle());
return (intptr_t)imGuiStyle;
*/
private native void nDestroy(long ptr); /*
delete (ImGuiStyle*)ptr;
*/ */
/** /**
@@ -1,32 +1,26 @@
package imgui; package imgui;
import imgui.binding.ImGuiStruct;
/** /**
* The viewports created and managed by Dear ImGui. The role of the platform back-end is to create the platform/OS windows corresponding to each viewport. * The viewports created and managed by Dear ImGui. The role of the platform back-end is to create the platform/OS windows corresponding to each viewport.
* - Main Area = entire viewport. * - Main Area = entire viewport.
* - Work Area = entire viewport minus sections optionally used by menu bars, status bars. Some positioning code will prefer to use this. Window are also trying to stay within this area. * - Work Area = entire viewport minus sections optionally used by menu bars, status bars. Some positioning code will prefer to use this. Window are also trying to stay within this area.
*/ */
public final class ImGuiViewport { public final class ImGuiViewport extends ImGuiStruct {
final long ptr; public ImGuiViewport(final long ptr) {
super(ptr);
ImGuiViewport(final long ptr) {
this.ptr = ptr;
} }
/*JNI /*JNI
#include <stdint.h> #include <stdint.h>
#include <imgui.h> #include <imgui.h>
#include "jni_common.h" #include "jni_common.h"
#include "jni_binding_struct.h"
jfieldID imGuiViewportPtrID; #define IMGUI_VIEWPORT ((ImGuiViewport*)STRUCT_PTR)
#define IMGUI_VIEWPORT ((ImGuiViewport*)env->GetLongField(object, imGuiViewportPtrID))
*/ */
static native void nInit(); /*
jclass jImGuiViewportClass = env->FindClass("imgui/ImGuiViewport");
imGuiViewportPtrID = env->GetFieldID(jImGuiViewportClass, "ptr", "J");
*/
/** /**
* Unique identifier for the viewport. * Unique identifier for the viewport.
*/ */
@@ -1,5 +1,7 @@
package imgui; package imgui;
import imgui.binding.ImGuiStructDestroyable;
/** /**
* [ALPHA] Rarely used / very advanced uses only. Use with SetNextWindowClass() and DockSpace() functions. * [ALPHA] Rarely used / very advanced uses only. Use with SetNextWindowClass() and DockSpace() functions.
* Important: the content of this class is still highly WIP and likely to change and be refactored * Important: the content of this class is still highly WIP and likely to change and be refactored
@@ -9,48 +11,29 @@ package imgui;
* - To the platform back-end for OS level parent/child relationships of viewport. * - To the platform back-end for OS level parent/child relationships of viewport.
* - To the docking system for various options and filtering. * - To the docking system for various options and filtering.
*/ */
public final class ImGuiWindowClass implements ImGuiDestroyableStruct { public final class ImGuiWindowClass extends ImGuiStructDestroyable {
final long ptr;
/**
* This class will create a native structure.
* Call {@link #destroy()} method to manually free used memory.
*/
public ImGuiWindowClass() { public ImGuiWindowClass() {
ImGui.touch();
ptr = nCreate();
} }
ImGuiWindowClass(final long ptr) { public ImGuiWindowClass(final long ptr) {
this.ptr = ptr; super(ptr);
}
@Override
public void destroy() {
nDestroy(ptr);
} }
/*JNI /*JNI
#include <stdint.h> #include <stdint.h>
#include <imgui.h> #include <imgui.h>
#include "jni_binding_struct.h"
jfieldID imGuiWindowClassPtrID; #define IMGUI_WINDOW_CLASS ((ImGuiWindowClass*)STRUCT_PTR)
#define IMGUI_WINDOW_CLASS ((ImGuiWindowClass*)env->GetLongField(object, imGuiWindowClassPtrID))
*/ */
static native void nInit(); /* @Override
jclass jImGuiWindowClassClass = env->FindClass("imgui/ImGuiWindowClass"); protected long create() {
imGuiWindowClassPtrID = env->GetFieldID(jImGuiWindowClassClass, "ptr", "J"); return nCreate();
*/ }
private native long nCreate(); /* private native long nCreate(); /*
ImGuiWindowClass* imGuiWindowClass = new ImGuiWindowClass(); return (intptr_t)(new ImGuiWindowClass());
return (intptr_t)imGuiWindowClass;
*/
private native void nDestroy(long ptr); /*
delete (ImGuiWindowClass*)ptr;
*/ */
/** /**
@@ -0,0 +1,18 @@
package imgui.binding;
import imgui.ImGui;
/**
* Class provides a communication layer between binding and Dear ImGui library though the "pointer" field.
*/
public abstract class ImGuiStruct {
/**
* Pointer to the natively allocated object,
*/
public long ptr;
public ImGuiStruct(final long ptr) {
ImGui.touch();
this.ptr = ptr;
}
}
@@ -0,0 +1,31 @@
package imgui.binding;
/**
* Class extends {@link ImGuiStruct} and provides additional API to allocated and de-allocate native objects.
*/
public abstract class ImGuiStructDestroyable extends ImGuiStruct {
/**
* Constructor, which creates natively allocated struct. Those structs should be manually disposed with {@link #destroy()} method.
*/
public ImGuiStructDestroyable() {
this(0);
ptr = create();
}
public ImGuiStructDestroyable(final long ptr) {
super(ptr);
}
protected abstract long create();
/**
* Method to free natively allocated memory for the struct mapped with pointer.
*/
public void destroy() {
nDestroy(ptr);
}
private native void nDestroy(long ptr); /*
delete (void*)ptr;
*/
}
@@ -0,0 +1,17 @@
#include "jni_binding_struct.h"
jfieldID imGuiStructPtrID;
namespace Jni
{
void InitBindingStruct(JNIEnv* env)
{
jclass jImGuiStructClass = env->FindClass("imgui/binding/ImGuiStruct");
imGuiStructPtrID = env->GetFieldID(jImGuiStructClass, "ptr", "J");
}
jfieldID GetBindingStructPtrID()
{
return imGuiStructPtrID;
}
}
@@ -0,0 +1,17 @@
#include <jni.h>
#ifndef JNI_BINDING_STRUCT_H
#define JNI_BINDING_STRUCT_H
#ifndef STRUCT_PTR
#define STRUCT_PTR env->GetLongField(object, Jni::GetBindingStructPtrID())
#endif
namespace Jni
{
void InitBindingStruct(JNIEnv* env);
jfieldID GetBindingStructPtrID();
}
#endif