Refactor ImDrawList

This commit is contained in:
SpaiR committed 2020-05-17 21:09:53 +03:00
1 parent d59b45a88f
commit 19bd512a22
3 files changed
+44 -39

No files matched your search

@@ -11,16 +11,16 @@ 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 implements ImDestroyable {
public static final int TYPE_WINDOW = 0; final long ptr;
public static final int TYPE_BACKGROUND = 1;
public static final int TYPE_FOREGROUND = 2;
@SuppressWarnings({"FieldCanBeLocal", "unused"}) ImDrawList(final long ptr) {
private final int drawListType; this.ptr = ptr;
}
ImDrawList(final int drawListType) { @Override
this.drawListType = drawListType; public void destroy() {
nDestroy(ptr);
} }
/*JNI /*JNI
@@ -28,31 +28,18 @@ public final class ImDrawList {
#include <imgui.h> #include <imgui.h>
#include "jni_common.h" #include "jni_common.h"
const static int DRAWLIST_TYPE_WINDOW = 0; jfieldID imDrawListPtrID;
const static int DRAWLIST_TYPE_BACKGROUND = 1;
const static int DRAWLIST_TYPE_FOREGROUND = 2;
ImDrawList* getDrawList(int drawListType) { #define IM_DRAW_LIST ((ImDrawList*)env->GetLongField(object, imDrawListPtrID))
switch (drawListType) {
case DRAWLIST_TYPE_WINDOW:
return ImGui::GetWindowDrawList();
case DRAWLIST_TYPE_BACKGROUND:
return ImGui::GetBackgroundDrawList();
case DRAWLIST_TYPE_FOREGROUND:
return ImGui::GetForegroundDrawList();
default:
return NULL;
}
}
jfieldID drawListTypeID;
#define IM_DRAW_LIST getDrawList(env->GetIntField(object, drawListTypeID))
*/ */
static native void nInit(); /* static native void nInit(); /*
jclass jImDrawListClass = env->FindClass("imgui/ImDrawList"); jclass jImDrawListClass = env->FindClass("imgui/ImDrawList");
drawListTypeID = env->GetFieldID(jImDrawListClass, "drawListType", "I"); imDrawListPtrID = env->GetFieldID(jImDrawListClass, "ptr", "J");
*/
private native void nDestroy(long ptr); /*
delete (ImDrawList*)ptr;
*/ */
/** /**
+27 -7
View File
@@ -18,13 +18,12 @@ public final class ImGui {
private static final ImGuiIO IMGUI_IO; private static final ImGuiIO IMGUI_IO;
private static final ImDrawList IM_DRAW_LIST_WINDOW = new ImDrawList(ImDrawList.TYPE_WINDOW);
private static final ImDrawList IM_DRAW_LIST_BACKGROUND = new ImDrawList(ImDrawList.TYPE_BACKGROUND);
private static final ImDrawList IM_DRAW_LIST_FOREGROUND = new ImDrawList(ImDrawList.TYPE_FOREGROUND);
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);
@@ -465,9 +464,16 @@ 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
*/ */
public static ImDrawList getWindowDrawList() { public static ImDrawList getWindowDrawList() {
return IM_DRAW_LIST_WINDOW; if (windowDrawList == null) {
windowDrawList = new ImDrawList(nGetWindowDrawList());
}
return windowDrawList;
} }
private static native long nGetWindowDrawList(); /*
return (intptr_t)ImGui::GetWindowDrawList();
*/
/** /**
* Get current window position in screen space (useful if you want to do your own drawing via the DrawList API) * Get current window position in screen space (useful if you want to do your own drawing via the DrawList API)
*/ */
@@ -4412,16 +4418,30 @@ public final class ImGui {
* 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.
*/ */
public static ImDrawList getBackgroundDrawList() { public static ImDrawList getBackgroundDrawList() {
return IM_DRAW_LIST_BACKGROUND; if (backgroundDrawList == null) {
backgroundDrawList = new ImDrawList(nGetBackgroundDrawList());
}
return backgroundDrawList;
} }
private static native long nGetBackgroundDrawList(); /*
return (intptr_t)ImGui::GetBackgroundDrawList();
*/
/** /**
* This draw list will be the last rendered one. Useful to quickly draw shapes/text over dear imgui contents. * This draw list will be the last rendered one. Useful to quickly draw shapes/text over dear imgui contents.
*/ */
public static ImDrawList getForegroundDrawList() { public static ImDrawList getForegroundDrawList() {
return IM_DRAW_LIST_FOREGROUND; if (foregroundDrawList == null) {
foregroundDrawList = new ImDrawList(nGetForegroundDrawList());
}
return foregroundDrawList;
} }
private static native long nGetForegroundDrawList(); /*
return (intptr_t)ImGui::GetForegroundDrawList();
*/
// TODO GetDrawListSharedData // TODO GetDrawListSharedData
/** /**
@@ -199,15 +199,13 @@ 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() {
// on demand instantiation
// will throw native exception if Dear ImGui context isn't created (like in the original library)
if (imFontAtlas == null) { if (imFontAtlas == null) {
imFontAtlas = new ImFontAtlas(nGetFontsPtr()); imFontAtlas = new ImFontAtlas(nGetFonts());
} }
return imFontAtlas; return imFontAtlas;
} }
private native long nGetFontsPtr(); /* private native long nGetFonts(); /*
return (intptr_t)ImGui::GetIO().Fonts; return (intptr_t)ImGui::GetIO().Fonts;
*/ */