Fix getBackgroundDrawList() and getForegroundDrawList(). Add methods to create draw list objects with constant pointer

This commit is contained in:
SpaiR committed 2020-06-06 16:01:02 +03:00
1 parent 7a0e9ec942
commit fbe0ed5a08
11 files changed
+69 -42

No files matched your search

@@ -1,9 +0,0 @@
package imgui;
/**
* While implementing this interface, class could be manually destroyed (free allocated memory on the JNI side).
* Read javadoc and "BINDING NOTICE" for specific cases.
*/
interface ImDestroyable {
void destroy();
}
@@ -11,7 +11,7 @@ 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 implements ImDestroyable { public final class ImDrawData implements ImGuiDestroyableStruct {
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;
@@ -11,18 +11,15 @@ 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 implements ImDestroyable { public final class ImDrawList {
final long ptr; // BINDING NOTICE:
// For proper usage in getWindowDrawList(), getBackgroundDrawList(), getForegroundDrawList() we should be able to change object pointer.
long ptr;
ImDrawList(final long ptr) { ImDrawList(final long ptr) {
this.ptr = ptr; this.ptr = ptr;
} }
@Override
public void destroy() {
nDestroy(ptr);
}
/*JNI /*JNI
#include <stdint.h> #include <stdint.h>
#include <imgui.h> #include <imgui.h>
@@ -38,10 +35,6 @@ public final class ImDrawList implements ImDestroyable {
imDrawListPtrID = env->GetFieldID(jImDrawListClass, "ptr", "J"); imDrawListPtrID = env->GetFieldID(jImDrawListClass, "ptr", "J");
*/ */
private native void nDestroy(long ptr); /*
delete (ImDrawList*)ptr;
*/
/** /**
* 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.
*/ */
@@ -4,7 +4,7 @@ package imgui;
* 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 ImDestroyable { public final class ImFont implements ImGuiDestroyableStruct {
final long ptr; final long ptr;
private ImFontGlyph fallbackGlyph = null; private ImFontGlyph fallbackGlyph = null;
@@ -22,7 +22,7 @@ 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 ImDestroyable { public final class ImFontAtlas implements ImGuiDestroyableStruct {
final long ptr; final long ptr;
/** /**
@@ -1,6 +1,6 @@
package imgui; package imgui;
public final class ImFontConfig implements ImDestroyable { public final class ImFontConfig implements ImGuiDestroyableStruct {
final long ptr; final long ptr;
private short[] glyphRanges; private short[] glyphRanges;
@@ -4,7 +4,7 @@ package imgui;
* 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 ImDestroyable { public final class ImFontGlyph implements ImGuiDestroyableStruct {
final long ptr; final long ptr;
/** /**
+50 -15
View File
@@ -17,13 +17,13 @@ public final class ImGui {
private static final String LIB_TMP_DIR_PREFIX = "imgui-java-bin_" + System.getProperty("user.name", "user"); private static final String LIB_TMP_DIR_PREFIX = "imgui-java-bin_" + System.getProperty("user.name", "user");
private static final ImGuiIO IMGUI_IO; private static final ImGuiIO IMGUI_IO;
private static final ImDrawList WINDOW_DRAW_LIST;
private static final ImDrawList BACKGROUND_DRAW_LIST;
private static final ImDrawList FOREGROUND_DRAW_LIST;
private static ImDrawData drawData; private static ImDrawData drawData;
private static ImFont font; private static ImFont font;
private static ImGuiStyle style; private static ImGuiStyle style;
private static ImDrawList windowDrawList;
private static ImDrawList backgroundDrawList;
private static ImDrawList foregroundDrawList;
static { static {
final String libPath = System.getProperty(LIB_PATH_PROP); final String libPath = System.getProperty(LIB_PATH_PROP);
@@ -41,6 +41,9 @@ public final class ImGui {
} }
IMGUI_IO = new ImGuiIO(); IMGUI_IO = new ImGuiIO();
WINDOW_DRAW_LIST = new ImDrawList(0);
BACKGROUND_DRAW_LIST = new ImDrawList(0);
FOREGROUND_DRAW_LIST = new ImDrawList(0);
nInitJni(); nInitJni();
ImDrawList.nInit(); ImDrawList.nInit();
@@ -467,12 +470,22 @@ public final class ImGui {
/** /**
* Get draw list associated to the current window, to append your own drawing primitives * Get draw list associated to the current window, to append your own drawing primitives
* <p>
* BINDING NOTICE: to minimize overhead, method ALWAYS returns the same object, but changes its underlying pointer.
* If you need to get an object with constant pointer (which will point to the same window all the time) use {@link #getWindowDrawListNew()}.
*/ */
public static ImDrawList getWindowDrawList() { public static ImDrawList getWindowDrawList() {
if (windowDrawList == null) { WINDOW_DRAW_LIST.ptr = nGetWindowDrawList();
windowDrawList = new ImDrawList(nGetWindowDrawList()); return WINDOW_DRAW_LIST;
} }
return windowDrawList;
/**
* Get draw list associated to the current window, to append your own drawing primitives
* <p>
* BINDING NOTICE: returns {@link ImDrawList} for current window with constant pointer to it. Prefer to use {@link #getWindowDrawList()}.
*/
public static ImDrawList getWindowDrawListNew() {
return new ImDrawList(nGetWindowDrawList());
} }
private static native long nGetWindowDrawList(); /* private static native long nGetWindowDrawList(); /*
@@ -4514,12 +4527,23 @@ public final class ImGui {
/** /**
* Get background draw list for the viewport associated to the current window. * Get background draw list for the viewport associated to the current window.
* This draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents. * This draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents.
* <p>
* BINDING NOTICE: to minimize overhead, method ALWAYS returns the same object, but changes its underlying pointer.
* If you need to get an object with constant pointer (which will point to the same background all the time) use {@link #getBackgroundDrawListNew()}.
*/ */
public static ImDrawList getBackgroundDrawList() { public static ImDrawList getBackgroundDrawList() {
if (backgroundDrawList == null) { BACKGROUND_DRAW_LIST.ptr = nGetBackgroundDrawList();
backgroundDrawList = new ImDrawList(nGetBackgroundDrawList()); return BACKGROUND_DRAW_LIST;
} }
return backgroundDrawList;
/**
* Get background draw list for the viewport associated to the current window.
* This draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents.
* <p>
* BINDING NOTICE: returns {@link ImDrawList} for current background with constant pointer to it. Prefer to use {@link #getBackgroundDrawList()}.
*/
public static ImDrawList getBackgroundDrawListNew() {
return new ImDrawList(nGetBackgroundDrawList());
} }
private static native long nGetBackgroundDrawList(); /* private static native long nGetBackgroundDrawList(); /*
@@ -4529,12 +4553,23 @@ public final class ImGui {
/** /**
* Get foreground draw list for the viewport associated to the current window. * Get foreground draw list for the viewport associated to the current window.
* This draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents. * This draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents.
* <p>
* BINDING NOTICE: to minimize overhead, method ALWAYS returns the same object, but changes its underlying pointer.
* If you need to get an object with constant pointer (which will point to the same foreground all the time) use {@link #getForegroundDrawListNew()}.
*/ */
public static ImDrawList getForegroundDrawList() { public static ImDrawList getForegroundDrawList() {
if (foregroundDrawList == null) { FOREGROUND_DRAW_LIST.ptr = nGetForegroundDrawList();
foregroundDrawList = new ImDrawList(nGetForegroundDrawList()); return FOREGROUND_DRAW_LIST;
} }
return foregroundDrawList;
/**
* Get foreground draw list for the viewport associated to the current window.
* This draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents.
* <p>
* BINDING NOTICE: returns {@link ImDrawList} for current foreground with constant pointer to it. Prefer to use {@link #getForegroundDrawList()}.
*/
public static ImDrawList getForegroundDrawListNew() {
return new ImDrawList(nGetForegroundDrawList());
} }
private static native long nGetForegroundDrawList(); /* private static native long nGetForegroundDrawList(); /*
@@ -0,0 +1,8 @@
package imgui;
/**
* When implement, class could be manually destroyed (free allocated memory on the JNI side).
*/
interface ImGuiDestroyableStruct {
void destroy();
}
@@ -5,7 +5,7 @@ package imgui;
* 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 ImDestroyable { public final class ImGuiStyle implements ImGuiDestroyableStruct {
final long ptr; final long ptr;
/** /**
@@ -9,7 +9,7 @@ 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 ImDestroyable { public final class ImGuiWindowClass implements ImGuiDestroyableStruct {
final long ptr; final long ptr;
/** /**