Skip to content

Commit 19bd512

Browse files
committed
Refactor ImDrawList
1 parent d59b45a commit 19bd512

File tree

3 files changed

+44
-39
lines changed

3 files changed

+44
-39
lines changed

imgui-binding/src/main/java/imgui/ImDrawList.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,35 @@
1111
* 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)
1212
* 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.
1313
*/
14-
public final class ImDrawList {
15-
public static final int TYPE_WINDOW = 0;
16-
public static final int TYPE_BACKGROUND = 1;
17-
public static final int TYPE_FOREGROUND = 2;
14+
public final class ImDrawList implements ImDestroyable {
15+
final long ptr;
1816

19-
@SuppressWarnings({"FieldCanBeLocal", "unused"})
20-
private final int drawListType;
17+
ImDrawList(final long ptr) {
18+
this.ptr = ptr;
19+
}
2120

22-
ImDrawList(final int drawListType) {
23-
this.drawListType = drawListType;
21+
@Override
22+
public void destroy() {
23+
nDestroy(ptr);
2424
}
2525

2626
/*JNI
2727
#include <stdint.h>
2828
#include <imgui.h>
2929
#include "jni_common.h"
3030
31-
const static int DRAWLIST_TYPE_WINDOW = 0;
32-
const static int DRAWLIST_TYPE_BACKGROUND = 1;
33-
const static int DRAWLIST_TYPE_FOREGROUND = 2;
34-
35-
ImDrawList* getDrawList(int drawListType) {
36-
switch (drawListType) {
37-
case DRAWLIST_TYPE_WINDOW:
38-
return ImGui::GetWindowDrawList();
39-
case DRAWLIST_TYPE_BACKGROUND:
40-
return ImGui::GetBackgroundDrawList();
41-
case DRAWLIST_TYPE_FOREGROUND:
42-
return ImGui::GetForegroundDrawList();
43-
default:
44-
return NULL;
45-
}
46-
}
47-
48-
jfieldID drawListTypeID;
31+
jfieldID imDrawListPtrID;
4932
50-
#define IM_DRAW_LIST getDrawList(env->GetIntField(object, drawListTypeID))
33+
#define IM_DRAW_LIST ((ImDrawList*)env->GetLongField(object, imDrawListPtrID))
5134
*/
5235

5336
static native void nInit(); /*
5437
jclass jImDrawListClass = env->FindClass("imgui/ImDrawList");
55-
drawListTypeID = env->GetFieldID(jImDrawListClass, "drawListType", "I");
38+
imDrawListPtrID = env->GetFieldID(jImDrawListClass, "ptr", "J");
39+
*/
40+
41+
private native void nDestroy(long ptr); /*
42+
delete (ImDrawList*)ptr;
5643
*/
5744

5845
/**

imgui-binding/src/main/java/imgui/ImGui.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ public final class ImGui {
1818

1919
private static final ImGuiIO IMGUI_IO;
2020

21-
private static final ImDrawList IM_DRAW_LIST_WINDOW = new ImDrawList(ImDrawList.TYPE_WINDOW);
22-
private static final ImDrawList IM_DRAW_LIST_BACKGROUND = new ImDrawList(ImDrawList.TYPE_BACKGROUND);
23-
private static final ImDrawList IM_DRAW_LIST_FOREGROUND = new ImDrawList(ImDrawList.TYPE_FOREGROUND);
24-
2521
private static ImDrawData drawData;
2622
private static ImFont font;
2723
private static ImGuiStyle style;
24+
private static ImDrawList windowDrawList;
25+
private static ImDrawList backgroundDrawList;
26+
private static ImDrawList foregroundDrawList;
2827

2928
static {
3029
final String libPath = System.getProperty(LIB_PATH_PROP);
@@ -465,9 +464,16 @@ public static boolean begin(String title, ImBool pOpen, int imGuiWindowFlags) {
465464
* Get draw list associated to the current window, to append your own drawing primitives
466465
*/
467466
public static ImDrawList getWindowDrawList() {
468-
return IM_DRAW_LIST_WINDOW;
467+
if (windowDrawList == null) {
468+
windowDrawList = new ImDrawList(nGetWindowDrawList());
469+
}
470+
return windowDrawList;
469471
}
470472

473+
private static native long nGetWindowDrawList(); /*
474+
return (intptr_t)ImGui::GetWindowDrawList();
475+
*/
476+
471477
/**
472478
* Get current window position in screen space (useful if you want to do your own drawing via the DrawList API)
473479
*/
@@ -4412,16 +4418,30 @@ public static byte[] acceptDragDropPayload(String type, int imGuiDragDropFlags)
44124418
* This draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents.
44134419
*/
44144420
public static ImDrawList getBackgroundDrawList() {
4415-
return IM_DRAW_LIST_BACKGROUND;
4421+
if (backgroundDrawList == null) {
4422+
backgroundDrawList = new ImDrawList(nGetBackgroundDrawList());
4423+
}
4424+
return backgroundDrawList;
44164425
}
44174426

4427+
private static native long nGetBackgroundDrawList(); /*
4428+
return (intptr_t)ImGui::GetBackgroundDrawList();
4429+
*/
4430+
44184431
/**
44194432
* This draw list will be the last rendered one. Useful to quickly draw shapes/text over dear imgui contents.
44204433
*/
44214434
public static ImDrawList getForegroundDrawList() {
4422-
return IM_DRAW_LIST_FOREGROUND;
4435+
if (foregroundDrawList == null) {
4436+
foregroundDrawList = new ImDrawList(nGetForegroundDrawList());
4437+
}
4438+
return foregroundDrawList;
44234439
}
44244440

4441+
private static native long nGetForegroundDrawList(); /*
4442+
return (intptr_t)ImGui::GetForegroundDrawList();
4443+
*/
4444+
44254445
// TODO GetDrawListSharedData
44264446

44274447
/**

imgui-binding/src/main/java/imgui/ImGuiIO.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,13 @@ public final class ImGuiIO {
199199
* Font atlas: load, rasterize and pack one or more fonts into a single texture.
200200
*/
201201
public ImFontAtlas getFonts() {
202-
// on demand instantiation
203-
// will throw native exception if Dear ImGui context isn't created (like in the original library)
204202
if (imFontAtlas == null) {
205-
imFontAtlas = new ImFontAtlas(nGetFontsPtr());
203+
imFontAtlas = new ImFontAtlas(nGetFonts());
206204
}
207205
return imFontAtlas;
208206
}
209207

210-
private native long nGetFontsPtr(); /*
208+
private native long nGetFonts(); /*
211209
return (intptr_t)ImGui::GetIO().Fonts;
212210
*/
213211

0 commit comments

Comments
 (0)