From 8494bd8361e6f4f3bfcd35e82467403cb71a8220 Mon Sep 17 00:00:00 2001 From: SpaiR Date: Sat, 29 Aug 2020 15:33:15 +0300 Subject: [PATCH] Add ImPlatformIO, ImGuiViewport --- .../src/main/java/imgui/ImDrawData.java | 14 + imgui-binding/src/main/java/imgui/ImGui.java | 113 ++++ .../src/main/java/imgui/ImGuiPlatformIO.java | 488 ++++++++++++++++++ .../src/main/java/imgui/ImGuiViewport.java | 14 +- .../callback/ImPlatformFuncViewport.java | 10 + .../callback/ImPlatformFuncViewportFloat.java | 10 + .../ImPlatformFuncViewportImVec2.java | 11 + .../ImPlatformFuncViewportString.java | 10 + .../ImPlatformFuncViewportSuppBoolean.java | 10 + .../ImPlatformFuncViewportSuppFloat.java | 10 + .../ImPlatformFuncViewportSuppImVec2.java | 11 + .../src/main/native/jni_binding_struct.cpp | 6 +- .../src/main/native/jni_callbacks.cpp | 57 ++ imgui-binding/src/main/native/jni_callbacks.h | 14 + 14 files changed, 773 insertions(+), 5 deletions(-) create mode 100644 imgui-binding/src/main/java/imgui/ImGuiPlatformIO.java create mode 100644 imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewport.java create mode 100644 imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportFloat.java create mode 100644 imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportImVec2.java create mode 100644 imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportString.java create mode 100644 imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportSuppBoolean.java create mode 100644 imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportSuppFloat.java create mode 100644 imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportSuppImVec2.java diff --git a/imgui-binding/src/main/java/imgui/ImDrawData.java b/imgui-binding/src/main/java/imgui/ImDrawData.java index a8f416e..ce18272 100644 --- a/imgui-binding/src/main/java/imgui/ImDrawData.java +++ b/imgui-binding/src/main/java/imgui/ImDrawData.java @@ -21,6 +21,8 @@ public final class ImDrawData extends ImGuiStruct { private static ByteBuffer dataBuffer = ByteBuffer.allocateDirect(25_000).order(ByteOrder.nativeOrder()); + private static final ImGuiViewport OWNER_VIEWPORT = new ImGuiViewport(0); + public ImDrawData(final long ptr) { super(ptr); } @@ -229,6 +231,18 @@ public final class ImDrawData extends ImGuiStruct { return IM_DRAW_DATA->FramebufferScale.y; */ + /** + * Viewport carrying the ImDrawData instance, might be of use to the renderer (generally not). + */ + public ImGuiViewport getOwnerViewport() { + OWNER_VIEWPORT.ptr = nGetOwnerViewport(); + return OWNER_VIEWPORT; + } + + private native long nGetOwnerViewport(); /* + return (intptr_t)IM_DRAW_DATA->OwnerViewport; + */ + // Functions /** diff --git a/imgui-binding/src/main/java/imgui/ImGui.java b/imgui-binding/src/main/java/imgui/ImGui.java index baa0eb4..7972532 100644 --- a/imgui-binding/src/main/java/imgui/ImGui.java +++ b/imgui-binding/src/main/java/imgui/ImGui.java @@ -29,10 +29,14 @@ public final class ImGui { private static final ImDrawList BACKGROUND_DRAW_LIST; private static final ImDrawList FOREGROUND_DRAW_LIST; private static final ImGuiStorage IMGUI_STORAGE; + private static final ImGuiViewport WINDOW_VIEWPORT; + private static final ImGuiViewport FIND_VIEWPORT; private static ImDrawData drawData; private static ImFont font; private static ImGuiStyle style; + private static ImGuiViewport mainViewport; + private static ImGuiPlatformIO platformIO; static { final String libPath = System.getProperty(LIB_PATH_PROP); @@ -54,9 +58,12 @@ public final class ImGui { BACKGROUND_DRAW_LIST = new ImDrawList(0); FOREGROUND_DRAW_LIST = new ImDrawList(0); IMGUI_STORAGE = new ImGuiStorage(0); + WINDOW_VIEWPORT = new ImGuiViewport(0); + FIND_VIEWPORT = new ImGuiViewport(0); nInitJni(); ImFontAtlas.nInit(); + ImGuiPlatformIO.init(); nInitInputTextData(); } @@ -494,6 +501,18 @@ public final class ImGui { return ImGui::GetWindowDpiScale(); */ + /** + * Get viewport currently associated to the current window. + */ + public static ImGuiViewport getWindowViewport() { + WINDOW_VIEWPORT.ptr = nGetWindowViewport(); + return WINDOW_VIEWPORT; + } + + private static native long nGetWindowViewport(); /* + return (intptr_t)ImGui::GetWindowViewport(); + */ + /** * Get current window position in screen space (useful if you want to do your own drawing via the DrawList API) */ @@ -4918,6 +4937,32 @@ public final class ImGui { return (intptr_t)ImGui::GetForegroundDrawList(); */ + /** + * Get background draw list for the given viewport. + * This draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents. + */ + public static ImDrawList getBackgroundDrawList(ImGuiViewport viewport) { + BACKGROUND_DRAW_LIST.ptr = nGetBackgroundDrawList(viewport.ptr); + return BACKGROUND_DRAW_LIST; + } + + private static native long nGetBackgroundDrawList(long viewportPtr); /* + return (intptr_t)ImGui::GetBackgroundDrawList((ImGuiViewport*)viewportPtr); + */ + + /** + * Get foreground draw list for the given viewport. + * This draw list will be the last rendered one. Useful to quickly draw shapes/text over dear imgui contents. + */ + public static ImDrawList getForegroundDrawList(ImGuiViewport viewport) { + BACKGROUND_DRAW_LIST.ptr = nGetForegroundDrawList(viewport.ptr); + return BACKGROUND_DRAW_LIST; + } + + private static native long nGetForegroundDrawList(long viewportPtr); /* + return (intptr_t)ImGui::GetBackgroundDrawList((ImGuiViewport*)viewportPtr); + */ + // TODO GetDrawListSharedData /** @@ -5378,4 +5423,72 @@ public final class ImGui { public static native String saveIniSettingsToMemory(long outIniSize); /* return env->NewStringUTF(ImGui::SaveIniSettingsToMemory((size_t*)&outIniSize)); */ + + // (Optional) Platform/OS interface for multi-viewport support + // Read comments around the ImGuiPlatformIO structure for more details. + // Note: You may use GetWindowViewport() to get the current viewport of the current window. + + /** + * Platform/renderer functions, for back-end to setup + viewports list. + */ + public static ImGuiPlatformIO getPlatformIO() { + if (platformIO == null) { + platformIO = new ImGuiPlatformIO(nGetPlatformIO()); + } + return platformIO; + } + + private static native long nGetPlatformIO(); /* + return (intptr_t)ImGui::GetMainViewport(); + */ + + /** + * Main viewport. Same as GetPlatformIO().MainViewport == GetPlatformIO().Viewports[0]. + */ + public static ImGuiViewport getMainViewport() { + if (mainViewport == null) { + mainViewport = new ImGuiViewport(nGetMainViewport()); + } + return mainViewport; + } + + private static native long nGetMainViewport(); /* + return (intptr_t)ImGui::GetMainViewport(); + */ + + /** + * Call in main loop. Will call CreateWindow/ResizeWindow/etc. Platform functions for each secondary viewport, and DestroyWindow for each inactive viewport. + */ + public static native void updatePlatformWindows(); /* + ImGui::UpdatePlatformWindows(); + */ + + /** + * Call in main loop. will call RenderWindow/SwapBuffers platform functions for each secondary viewport which doesn't have the ImGuiViewportFlags_Minimized flag set. + * May be reimplemented by user for custom rendering needs. + */ + public static native void renderPlatformWindowsDefault(); /* + ImGui::RenderPlatformWindowsDefault(); + */ + + /** + * Call DestroyWindow platform functions for all viewports. + * Call from back-end Shutdown() if you need to close platform windows before imgui shutdown. + * Otherwise will be called by DestroyContext(). + */ + public static native void destroyPlatformWindows(); /* + ImGui::DestroyPlatformWindows(); + */ + + /** + * This is a helper for back-ends. + */ + public static ImGuiViewport findViewportByID(int imGuiID) { + FIND_VIEWPORT.ptr = nFindViewportByID(imGuiID); + return FIND_VIEWPORT; + } + + private static native long nFindViewportByID(int imGuiID); /* + return (intptr_t)ImGui::FindViewportByID(imGuiID); + */ } diff --git a/imgui-binding/src/main/java/imgui/ImGuiPlatformIO.java b/imgui-binding/src/main/java/imgui/ImGuiPlatformIO.java new file mode 100644 index 0000000..16639fc --- /dev/null +++ b/imgui-binding/src/main/java/imgui/ImGuiPlatformIO.java @@ -0,0 +1,488 @@ +package imgui; + +import imgui.binding.ImGuiStruct; +import imgui.callback.ImPlatformFuncViewport; +import imgui.callback.ImPlatformFuncViewportFloat; +import imgui.callback.ImPlatformFuncViewportImVec2; +import imgui.callback.ImPlatformFuncViewportString; +import imgui.callback.ImPlatformFuncViewportSuppBoolean; +import imgui.callback.ImPlatformFuncViewportSuppFloat; +import imgui.callback.ImPlatformFuncViewportSuppImVec2; + +/** + * ----------------------------------------------------------------------------- + * [BETA] Platform interface for multi-viewport support + * ----------------------------------------------------------------------------- + * (Optional) This is completely optional, for advanced users! + * If you are new to Dear ImGui and trying to integrate it into your engine, you can probably ignore this for now. + * This feature allows you to seamlessly drag Dear ImGui windows outside of your application viewport. + * This is achieved by creating new Platform/OS windows on the fly, and rendering into them. + * Dear ImGui manages the viewport structures, and the back-end create and maintain one Platform/OS window for each of those viewports. + * See Glossary https://github.com/ocornut/imgui/wiki/Glossary for details about some of the terminology. + * See Thread https://github.com/ocornut/imgui/issues/1542 for gifs, news and questions about this evolving feature. + * About the coordinates system: + * - When multi-viewports are enabled, all Dear ImGui coordinates become absolute coordinates (same as OS coordinates!) + * - So e.g. ImGui::SetNextWindowPos(ImVec2(0,0)) will position a window relative to your primary monitor! + * - If you want to position windows relative to your main application viewport, use ImGui::GetMainViewport()->Pos as a base position. + * Steps to use multi-viewports in your application, when using a default back-end from the examples/ folder: + * - Application: Enable feature with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. + * - Back-end: The back-end initialization will setup all necessary ImGuiPlatformIO's functions and update monitors info every frame. + * - Application: In your main loop, call ImGui::UpdatePlatformWindows(), ImGui::RenderPlatformWindowsDefault() after EndFrame() or Render(). + * - Application: Fix absolute coordinates used in ImGui::SetWindowPos() or ImGui::SetNextWindowPos() calls. + * Steps to use multi-viewports in your application, when using a custom back-end: + * - Important: THIS IS NOT EASY TO DO and comes with many subtleties not described here! + * It's also an experimental feature, so some of the requirements may evolve. + * Consider using default back-ends if you can. Either way, carefully follow and refer to examples/ back-ends for details. + * - Application: Enable feature with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. + * - Back-end: Hook ImGuiPlatformIO's Platform_* and Renderer_* callbacks (see below). + * Set 'io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports' and 'io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports'. + * Update ImGuiPlatformIO's Monitors list every frame. + * Update MousePos every frame, in absolute coordinates. + * - Application: In your main loop, call ImGui::UpdatePlatformWindows(), ImGui::RenderPlatformWindowsDefault() after EndFrame() or Render(). + * You may skip calling RenderPlatformWindowsDefault() if its API is not convenient for your needs. Read comments below. + * - Application: Fix absolute coordinates used in ImGui::SetWindowPos() or ImGui::SetNextWindowPos() calls. + * About ImGui::RenderPlatformWindowsDefault(): + * - This function is a mostly a _helper_ for the common-most cases, and to facilitate using default back-ends. + * - You can check its simple source code to understand what it does. + * It basically iterates secondary viewports and call 4 functions that are setup in ImGuiPlatformIO, if available: + * Platform_RenderWindow(), Renderer_RenderWindow(), Platform_SwapBuffers(), Renderer_SwapBuffers() + * Those functions pointers exists only for the benefit of RenderPlatformWindowsDefault(). + * - If you have very specific rendering needs (e.g. flipping multiple swap-chain simultaneously, unusual sync/threading issues, etc.), + * you may be tempted to ignore RenderPlatformWindowsDefault() and write customized code to perform your renderingg. + * You may decide to setup the platform_io's *RenderWindow and *SwapBuffers pointers and call your functions through those pointers, + * or you may decide to never setup those pointers and call your code directly. They are a convenience, not an obligatory interface. + * ----------------------------------------------------------------------------- + * (Optional) Access via ImGui::GetPlatformIO() + * + * ------------------------------------------------------------------ + * Input - Back-end interface/functions + Monitor List + * ------------------------------------------------------------------ + * (Optional) Platform functions (e.g. Win32, GLFW, SDL2) + * For reference, the second column shows which function are generally calling the Platform Functions: + * N = ImGui::NewFrame() ~ beginning of the dear imgui frame: read info from platform/OS windows (latest size/position) + * F = ImGui::Begin(), ImGui::EndFrame() ~ during the dear imgui frame + * U = ImGui::UpdatePlatformWindows() ~ after the dear imgui frame: create and update all platform/OS windows + * R = ImGui::RenderPlatformWindowsDefault() ~ render + * D = ImGui::DestroyPlatformWindows() ~ shutdown + * The general idea is that NewFrame() we will read the current Platform/OS state, and UpdatePlatformWindows() will write to it. + * The functions are designed so we can mix and match 2 imgui_impl_xxxx files, one for the Platform (~window/input handling), one for Renderer. + * Custom engine back-ends will often provide both Platform and Renderer interfaces and so may not need to use all functions. + * Platform functions are typically called before their Renderer counterpart, apart from Destroy which are called the other way. + */ +public final class ImGuiPlatformIO extends ImGuiStruct { + private static final ImGuiViewport TMP_VIEWPORT = new ImGuiViewport(0); + private static final ImVec2 TMP_IM_VEC2 = new ImVec2(); + + public ImGuiPlatformIO(final long ptr) { + super(ptr); + } + + /*JNI + #include + #include + #include "jni_common.h" + #include "jni_callbacks.h" + #include "jni_binding_struct.h" + + #define IMGUI_PLATFORM_IO ((ImGuiPlatformIO*)STRUCT_PTR) + + jobject jTmpViewport = NULL; + jobject jTmpImVec2 = NULL; + */ + + static void init() { + nInit(TMP_VIEWPORT, TMP_IM_VEC2); + } + + private static native void nInit(ImGuiViewport tmpViewport, ImVec2 tmpImVec2); /* + jTmpViewport = env->NewGlobalRef(tmpViewport); + jTmpImVec2 = env->NewGlobalRef(tmpImVec2); + */ + + /*JNI + #define IM_PLATFORM_FUNC_VIEWPORT_TMPL(name)\ + jobject platformCallback##name = NULL;\ + void PlatformStub##name(ImGuiViewport* vp) {\ + if (platformCallback##name != NULL) {\ + JNIEnv* env = Jni::GetEnv();\ + env->SetLongField(jTmpViewport, Jni::GetBindingStructPtrID(), (intptr_t)vp);\ + Jni::CallImPlatformFuncViewport(env, platformCallback##name, jTmpViewport);\ + }\ + }\ + void PlatformStub##name(ImGuiViewport* vp, void*) {\ + PlatformStub##name(vp);\ + } + + IM_PLATFORM_FUNC_VIEWPORT_TMPL(CreateWindow) + + #define IM_PLATFORM_FUNC_VIEWPORT_METHOD_TMPL(name)\ + if (platformCallback##name != NULL) {\ + env->DeleteGlobalRef(platformCallback##name);\ + }\ + platformCallback##name = env->NewGlobalRef(func);\ + IMGUI_PLATFORM_IO->Platform_##name = PlatformStub##name; + */ + + /** + * // . . U . . // Create a new platform window for the given viewport. + */ + public native void setPlatformCreateWindow(ImPlatformFuncViewport func); /* + IM_PLATFORM_FUNC_VIEWPORT_METHOD_TMPL(CreateWindow) + */ + + /*JNI + IM_PLATFORM_FUNC_VIEWPORT_TMPL(DestroyWindow) + */ + + /** + * // N . U . D // + */ + public native void setPlatformDestroyWindow(ImPlatformFuncViewport func); /* + IM_PLATFORM_FUNC_VIEWPORT_METHOD_TMPL(DestroyWindow) + */ + + /*JNI + IM_PLATFORM_FUNC_VIEWPORT_TMPL(ShowWindow) + */ + + /** + * // . . U . . // Newly created windows are initially hidden so SetWindowPos/Size/Title can be called on them before showing the window. + */ + public native void setPlatformShowWindow(ImPlatformFuncViewport func); /* + IM_PLATFORM_FUNC_VIEWPORT_METHOD_TMPL(ShowWindow) + */ + + /*JNI + #define IM_PLATFORM_FUNC_VIEWPORT_IM_VEC2_TMPL(name)\ + jobject platformCallback##name = NULL;\ + void PlatformStub##name(ImGuiViewport* vp, ImVec2 pos) {\ + if (platformCallback##name != NULL) {\ + JNIEnv* env = Jni::GetEnv();\ + env->SetLongField(jTmpViewport, Jni::GetBindingStructPtrID(), (intptr_t)vp);\ + Jni::ImVec2Cpy(env, pos, jTmpImVec2);\ + Jni::CallImPlatformFuncViewportImVec2(env, platformCallback##name, jTmpViewport, jTmpImVec2);\ + }\ + } + + IM_PLATFORM_FUNC_VIEWPORT_IM_VEC2_TMPL(SetWindowPos) + + #define IM_PLATFORM_FUNC_VIEWPORT_IM_VEC2_METHOD_TMPL(name)\ + if (platformCallback##name != NULL) {\ + env->DeleteGlobalRef(platformCallback##name);\ + }\ + platformCallback##name = env->NewGlobalRef(func);\ + IMGUI_PLATFORM_IO->Platform_##name = PlatformStub##name; + */ + + /** + * // . . U . . // Set platform window position (given the upper-left corner of client area). + */ + public native void setPlatformSetWindowPos(ImPlatformFuncViewportImVec2 func); /* + IM_PLATFORM_FUNC_VIEWPORT_IM_VEC2_METHOD_TMPL(SetWindowPos) + */ + + /*JNI + #define IM_PLATFORM_FUNC_VIEWPORT_SUPP_IM_VEC2_TMPL(name)\ + jobject platformCallback##name = NULL;\ + ImVec2 PlatformStub##name(ImGuiViewport* vp) {\ + ImVec2 dst;\ + if (platformCallback##name != NULL) {\ + JNIEnv* env = Jni::GetEnv();\ + env->SetLongField(jTmpViewport, Jni::GetBindingStructPtrID(), (intptr_t)vp);\ + Jni::CallImPlatformFuncViewportSuppImVec2(env, platformCallback##name, jTmpViewport, jTmpImVec2);\ + Jni::ImVec2Cpy(env, jTmpImVec2, dst);\ + }\ + return dst;\ + } + + IM_PLATFORM_FUNC_VIEWPORT_SUPP_IM_VEC2_TMPL(GetWindowPos) + + #define IM_PLATFORM_FUNC_VIEWPORT_SUPP_IM_VEC2_METHOD_TMPL(name)\ + if (platformCallback##name != NULL) {\ + env->DeleteGlobalRef(platformCallback##name);\ + }\ + platformCallback##name = env->NewGlobalRef(func);\ + IMGUI_PLATFORM_IO->Platform_##name = PlatformStub##name; + */ + + /** + * // N . . . . // + */ + public native void setPlatformGetWindowPos(ImPlatformFuncViewportSuppImVec2 func); /* + IM_PLATFORM_FUNC_VIEWPORT_SUPP_IM_VEC2_METHOD_TMPL(GetWindowPos) + */ + + /*JNI + IM_PLATFORM_FUNC_VIEWPORT_IM_VEC2_TMPL(SetWindowSize) + */ + + /** + * // . . U . . // Set platform window client area size (ignoring OS decorations such as OS title bar etc.) + */ + public native void setPlatformSetWindowSize(ImPlatformFuncViewportImVec2 func); /* + IM_PLATFORM_FUNC_VIEWPORT_IM_VEC2_METHOD_TMPL(SetWindowSize) + */ + + /*JNI + IM_PLATFORM_FUNC_VIEWPORT_SUPP_IM_VEC2_TMPL(GetWindowSize) + */ + + /** + * // N . . . . // Get platform window client area size + */ + public native void setPlatformGetWindowSize(ImPlatformFuncViewportSuppImVec2 func); /* + IM_PLATFORM_FUNC_VIEWPORT_SUPP_IM_VEC2_METHOD_TMPL(GetWindowSize) + */ + + /*JNI + IM_PLATFORM_FUNC_VIEWPORT_TMPL(SetWindowFocus) + */ + + /** + * // N . . . . // Move window to front and set input focus. + */ + public native void setPlatformSetWindowFocus(ImPlatformFuncViewport func); /* + IM_PLATFORM_FUNC_VIEWPORT_METHOD_TMPL(SetWindowFocus) + */ + + /*JNI + #define IM_PLATFORM_FUNC_VIEWPORT_SUPP_BOOLEAN_TMPL(name)\ + jobject platformCallback##name = NULL;\ + bool PlatformStub##name(ImGuiViewport* vp) {\ + if (platformCallback##name != NULL) {\ + JNIEnv* env = Jni::GetEnv();\ + env->SetLongField(jTmpViewport, Jni::GetBindingStructPtrID(), (intptr_t)vp);\ + return Jni::CallImPlatformFuncViewportSuppBoolean(env, platformCallback##name, jTmpViewport);\ + }\ + return false;\ + } + + IM_PLATFORM_FUNC_VIEWPORT_SUPP_BOOLEAN_TMPL(GetWindowFocus) + + #define IM_PLATFORM_FUNC_VIEWPORT_SUPP_BOOLEAN_METHOD_TMPL(name)\ + if (platformCallback##name != NULL) {\ + env->DeleteGlobalRef(platformCallback##name);\ + }\ + platformCallback##name = env->NewGlobalRef(func);\ + IMGUI_PLATFORM_IO->Platform_##name = PlatformStub##name; + */ + + /** + * // . . U . . // + */ + public native void setPlatformGetWindowFocus(ImPlatformFuncViewportSuppBoolean func); /* + IM_PLATFORM_FUNC_VIEWPORT_SUPP_BOOLEAN_METHOD_TMPL(GetWindowFocus) + */ + + /*JNI + IM_PLATFORM_FUNC_VIEWPORT_SUPP_BOOLEAN_TMPL(GetWindowMinimized) + */ + + /** + * // N . . . . // Get platform window minimized state. When minimized, we generally won't attempt to get/set size and contents will be culled more easily. + */ + public native void setPlatformGetWindowMinimized(ImPlatformFuncViewportSuppBoolean func); /* + IM_PLATFORM_FUNC_VIEWPORT_SUPP_BOOLEAN_METHOD_TMPL(GetWindowMinimized) + */ + + /*JNI + jobject platformCallbackSetWindowTitle = NULL; + void PlatformStubSetWindowTitle(ImGuiViewport* vp, const char* str) { + if (platformCallbackSetWindowTitle != NULL) { + JNIEnv* env = Jni::GetEnv(); + env->SetLongField(jTmpViewport, Jni::GetBindingStructPtrID(), (intptr_t)vp); + Jni::CallImPlatformFuncViewportString(env, platformCallbackSetWindowTitle, jTmpViewport, str); + } + } + */ + + /** + * // . . U . . // Set platform window title (given an UTF-8 string) + */ + public native void setPlatformSetWindowTitle(ImPlatformFuncViewportString func); /* + if (platformCallbackSetWindowTitle != NULL) { + env->DeleteGlobalRef(platformCallbackSetWindowTitle); + } + platformCallbackSetWindowTitle = env->NewGlobalRef(func); + IMGUI_PLATFORM_IO->Platform_SetWindowTitle = PlatformStubSetWindowTitle; + */ + + /*JNI + jobject platformCallbackSetWindowAlpha = NULL; + void PlatformStubSetWindowAlpha(ImGuiViewport* vp, float alpha) { + if (platformCallbackSetWindowAlpha != NULL) { + JNIEnv* env = Jni::GetEnv(); + env->SetLongField(jTmpViewport, Jni::GetBindingStructPtrID(), (intptr_t)vp); + Jni::CallImPlatformFuncViewportFloat(env, platformCallbackSetWindowAlpha, jTmpViewport, alpha); + } + } + */ + + /** + * // . . U . . // (Optional) Setup window transparency + */ + public native void setPlatformSetWindowAlpha(ImPlatformFuncViewportFloat func); /* + if (platformCallbackSetWindowAlpha != NULL) { + env->DeleteGlobalRef(platformCallbackSetWindowAlpha); + } + platformCallbackSetWindowAlpha = env->NewGlobalRef(func); + IMGUI_PLATFORM_IO->Platform_SetWindowAlpha = PlatformStubSetWindowAlpha; + */ + + /*JNI + IM_PLATFORM_FUNC_VIEWPORT_TMPL(UpdateWindow) + */ + + /** + * // . . U . . // (Optional) Called by UpdatePlatformWindows(). Optional hook to allow the platform back-end from doing general book-keeping every frame. + */ + public native void setPlatformUpdateWindow(ImPlatformFuncViewport func); /* + IM_PLATFORM_FUNC_VIEWPORT_METHOD_TMPL(UpdateWindow) + */ + + /*JNI + IM_PLATFORM_FUNC_VIEWPORT_TMPL(RenderWindow) + */ + + /** + * // . . . R . // (Optional) Main rendering (platform side! This is often unused, or just setting a "current" context for OpenGL bindings). + */ + public native void setPlatformRenderWindow(ImPlatformFuncViewport func); /* + IM_PLATFORM_FUNC_VIEWPORT_METHOD_TMPL(RenderWindow) + */ + + /*JNI + IM_PLATFORM_FUNC_VIEWPORT_TMPL(SwapBuffers) + */ + + /** + * // . . . R . // (Optional) Call Present/SwapBuffers (platform side! This is often unused!). + */ + public native void setPlatformSwapBuffers(ImPlatformFuncViewport func); /* + IM_PLATFORM_FUNC_VIEWPORT_METHOD_TMPL(SwapBuffers) + */ + + /*JNI + jobject platformCallbackGetWindowDpiScale = NULL; + float PlatformStubGetWindowDpiScale(ImGuiViewport* vp) { + if (platformCallbackGetWindowDpiScale != NULL) { + JNIEnv* env = Jni::GetEnv(); + env->SetLongField(jTmpViewport, Jni::GetBindingStructPtrID(), (intptr_t)vp); + return Jni::CallImPlatformFuncViewportSuppFloat(env, platformCallbackGetWindowDpiScale, jTmpViewport); + } + return false; + } + */ + + /** + * // N . . . . // (Optional) [BETA] FIXME-DPI: DPI handling: Return DPI scale for this viewport. 1.0f = 96 DPI. + */ + public native void setPlatformGetWindowDpiScale(ImPlatformFuncViewportSuppFloat func); /* + if (platformCallbackGetWindowDpiScale != NULL) { + env->DeleteGlobalRef(platformCallbackGetWindowDpiScale); + } + platformCallbackGetWindowDpiScale = env->NewGlobalRef(func); + IMGUI_PLATFORM_IO->Platform_GetWindowDpiScale = PlatformStubGetWindowDpiScale; + */ + + /*JNI + IM_PLATFORM_FUNC_VIEWPORT_TMPL(OnChangedViewport) + */ + + /** + * // (Optional) [BETA] FIXME-DPI: DPI handling: Called during Begin() every time the viewport we are outputting into changes, so back-end has a chance to swap fonts to adjust style. + */ + public native void setPlatformOnChangedViewport(ImPlatformFuncViewport func); /* + IM_PLATFORM_FUNC_VIEWPORT_METHOD_TMPL(OnChangedViewport) + */ + + // (Optional) Renderer functions (e.g. DirectX, OpenGL, Vulkan) + + /*JNI + #define IM_RENDERER_FUNC_VIEWPORT_TMPL(name)\ + jobject rendererCallback##name = NULL;\ + void RendererStub##name(ImGuiViewport* vp) {\ + if (rendererCallback##name != NULL) {\ + JNIEnv* env = Jni::GetEnv();\ + env->SetLongField(jTmpViewport, Jni::GetBindingStructPtrID(), (intptr_t)vp);\ + Jni::CallImPlatformFuncViewport(env, rendererCallback##name, jTmpViewport);\ + }\ + }\ + void RendererStub##name(ImGuiViewport* vp, void*) {\ + RendererStub##name(vp);\ + } + + IM_RENDERER_FUNC_VIEWPORT_TMPL(CreateWindow) + + #define IM_RENDERER_FUNC_VIEWPORT_METHOD_TMPL(name)\ + if (rendererCallback##name != NULL) {\ + env->DeleteGlobalRef(rendererCallback##name);\ + }\ + rendererCallback##name = env->NewGlobalRef(func);\ + IMGUI_PLATFORM_IO->Renderer_##name = RendererStub##name; + */ + + /** + * // . . U . . // Create swap chain, frame buffers etc. (called after Platform_CreateWindow) + */ + public native void setRendererCreateWindow(ImPlatformFuncViewport func); /* + IM_RENDERER_FUNC_VIEWPORT_METHOD_TMPL(CreateWindow) + */ + + /*JNI + IM_RENDERER_FUNC_VIEWPORT_TMPL(DestroyWindow) + */ + + /** + * // N . U . D // Destroy swap chain, frame buffers etc. (called before Platform_DestroyWindow) + */ + public native void setRendererDestroyWindow(ImPlatformFuncViewport func); /* + IM_RENDERER_FUNC_VIEWPORT_METHOD_TMPL(DestroyWindow) + */ + + /*JNI + jobject rendererCallbackSetWindowSize = NULL; + void RendererStubSetWindowSize(ImGuiViewport* vp, ImVec2 pos) { + if (rendererCallbackSetWindowSize != NULL) { + JNIEnv* env = Jni::GetEnv(); + env->SetLongField(jTmpViewport, Jni::GetBindingStructPtrID(), (intptr_t)vp); + Jni::ImVec2Cpy(env, pos, jTmpImVec2); + Jni::CallImPlatformFuncViewportImVec2(env, rendererCallbackSetWindowSize, jTmpViewport, jTmpImVec2); + } + } + */ + + /** + * // . . U . . // Resize swap chain, frame buffers etc. (called after Platform_SetWindowSize) + */ + public native void setRendererSetWindowPos(ImPlatformFuncViewportImVec2 func); /* + if (rendererCallbackSetWindowSize != NULL) { + env->DeleteGlobalRef(rendererCallbackSetWindowSize); + } + rendererCallbackSetWindowSize = env->NewGlobalRef(func); + IMGUI_PLATFORM_IO->Renderer_SetWindowSize = RendererStubSetWindowSize; + */ + + /*JNI + IM_RENDERER_FUNC_VIEWPORT_TMPL(RenderWindow) + */ + + /** + * // . . . R . // (Optional) Clear framebuffer, setup render target, + * then render the viewport->DrawData. 'render_arg' is the value passed to RenderPlatformWindowsDefault(). + */ + public native void setRendererRenderWindow(ImPlatformFuncViewport func); /* + IM_RENDERER_FUNC_VIEWPORT_METHOD_TMPL(RenderWindow) + */ + + /*JNI + IM_RENDERER_FUNC_VIEWPORT_TMPL(SwapBuffers) + */ + + /** + * // . . . R . // (Optional) Call Present/SwapBuffers. 'render_arg' is the value passed to RenderPlatformWindowsDefault(). + */ + public native void setRendererSwapBuffers(ImPlatformFuncViewport func); /* + IM_RENDERER_FUNC_VIEWPORT_METHOD_TMPL(SwapBuffers) + */ +} diff --git a/imgui-binding/src/main/java/imgui/ImGuiViewport.java b/imgui-binding/src/main/java/imgui/ImGuiViewport.java index d093ee9..b1373a8 100644 --- a/imgui-binding/src/main/java/imgui/ImGuiViewport.java +++ b/imgui-binding/src/main/java/imgui/ImGuiViewport.java @@ -8,6 +8,8 @@ import imgui.binding.ImGuiStruct; * - 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 extends ImGuiStruct { + private static final ImDrawData DRAW_DATA = new ImDrawData(0); + public ImGuiViewport(final long ptr) { super(ptr); } @@ -179,7 +181,17 @@ public final class ImGuiViewport extends ImGuiStruct { IMGUI_VIEWPORT->DpiScale = dpiScale; */ - // TODO: DrawData + /** + * The ImDrawData corresponding to this viewport. Valid after Render() and until the next call to NewFrame(). + */ + public ImDrawData getDrawData() { + DRAW_DATA.ptr = nGetDrawData(); + return DRAW_DATA; + } + + private native long nGetDrawData(); /* + return (intptr_t)IMGUI_VIEWPORT->DrawData; + */ /** * (Advanced) 0: no parent. Instruct the platform back-end to setup a parent/child relationship between platform windows. diff --git a/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewport.java b/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewport.java new file mode 100644 index 0000000..5a6f35a --- /dev/null +++ b/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewport.java @@ -0,0 +1,10 @@ +package imgui.callback; + +import imgui.ImGuiViewport; + +/** + * Callback to represent ImGuiPlatformIO function with args: (ImGuiViewport*) + */ +public abstract class ImPlatformFuncViewport { + public abstract void accept(ImGuiViewport vp); +} diff --git a/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportFloat.java b/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportFloat.java new file mode 100644 index 0000000..1308447 --- /dev/null +++ b/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportFloat.java @@ -0,0 +1,10 @@ +package imgui.callback; + +import imgui.ImGuiViewport; + +/** + * Callback to represent ImGuiPlatformIO function with args: (ImGuiViewport*, String) + */ +public abstract class ImPlatformFuncViewportFloat { + public abstract void accept(ImGuiViewport vp, Float f); +} diff --git a/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportImVec2.java b/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportImVec2.java new file mode 100644 index 0000000..ee8b1f8 --- /dev/null +++ b/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportImVec2.java @@ -0,0 +1,11 @@ +package imgui.callback; + +import imgui.ImGuiViewport; +import imgui.ImVec2; + +/** + * Callback to represent ImGuiPlatformIO function with args: (ImGuiViewport*, ImVec2) + */ +public abstract class ImPlatformFuncViewportImVec2 { + public abstract void accept(ImGuiViewport vp, ImVec2 imVec2); +} diff --git a/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportString.java b/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportString.java new file mode 100644 index 0000000..d4bfe7a --- /dev/null +++ b/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportString.java @@ -0,0 +1,10 @@ +package imgui.callback; + +import imgui.ImGuiViewport; + +/** + * Callback to represent ImGuiPlatformIO function with args: (ImGuiViewport*, String) + */ +public abstract class ImPlatformFuncViewportString { + public abstract void accept(ImGuiViewport vp, String str); +} diff --git a/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportSuppBoolean.java b/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportSuppBoolean.java new file mode 100644 index 0000000..fd87715 --- /dev/null +++ b/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportSuppBoolean.java @@ -0,0 +1,10 @@ +package imgui.callback; + +import imgui.ImGuiViewport; + +/** + * Callback to represent ImGuiPlatformIO function with args: (ImGuiViewport*) -> Boolean + */ +public abstract class ImPlatformFuncViewportSuppBoolean { + public abstract boolean get(ImGuiViewport vp); +} diff --git a/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportSuppFloat.java b/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportSuppFloat.java new file mode 100644 index 0000000..781f81a --- /dev/null +++ b/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportSuppFloat.java @@ -0,0 +1,10 @@ +package imgui.callback; + +import imgui.ImGuiViewport; + +/** + * Callback to represent ImGuiPlatformIO function with args: (ImGuiViewport*) -> Boolean + */ +public abstract class ImPlatformFuncViewportSuppFloat { + public abstract float get(ImGuiViewport vp); +} diff --git a/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportSuppImVec2.java b/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportSuppImVec2.java new file mode 100644 index 0000000..33ecedc --- /dev/null +++ b/imgui-binding/src/main/java/imgui/callback/ImPlatformFuncViewportSuppImVec2.java @@ -0,0 +1,11 @@ +package imgui.callback; + +import imgui.ImGuiViewport; +import imgui.ImVec2; + +/** + * Callback to represent ImGuiPlatformIO function with args: (ImGuiViewport*) -> ImVec2 + */ +public abstract class ImPlatformFuncViewportSuppImVec2 { + public abstract void get(ImGuiViewport vp, ImVec2 dstImVec2); +} diff --git a/imgui-binding/src/main/native/jni_binding_struct.cpp b/imgui-binding/src/main/native/jni_binding_struct.cpp index 3553a0d..4eda649 100644 --- a/imgui-binding/src/main/native/jni_binding_struct.cpp +++ b/imgui-binding/src/main/native/jni_binding_struct.cpp @@ -4,14 +4,12 @@ jfieldID imGuiStructPtrID; namespace Jni { - void InitBindingStruct(JNIEnv* env) - { + void InitBindingStruct(JNIEnv* env) { jclass jImGuiStructClass = env->FindClass("imgui/binding/ImGuiStruct"); imGuiStructPtrID = env->GetFieldID(jImGuiStructClass, "ptr", "J"); } - jfieldID GetBindingStructPtrID() - { + jfieldID GetBindingStructPtrID() { return imGuiStructPtrID; } } diff --git a/imgui-binding/src/main/native/jni_callbacks.cpp b/imgui-binding/src/main/native/jni_callbacks.cpp index 68074e4..0b9016c 100644 --- a/imgui-binding/src/main/native/jni_callbacks.cpp +++ b/imgui-binding/src/main/native/jni_callbacks.cpp @@ -5,6 +5,14 @@ jmethodID jImListClipperCallbackAcceptMID; jmethodID jImStrConsumerAcceptMID; jmethodID jImStrSupplierGetMID; +jmethodID jImPlatformFuncViewportAcceptMID; +jmethodID jImPlatformFuncViewportFloatAcceptMID; +jmethodID jImPlatformFuncViewportImVec2AcceptMID; +jmethodID jImPlatformFuncViewportStringAcceptMID; +jmethodID jImPlatformFuncViewportSuppImVec2GetMID; +jmethodID jImPlatformFuncViewportSuppBooleanGetMID; +jmethodID jImPlatformFuncViewportSuppFloatGetMID; + namespace Jni { void InitCallbacks(JNIEnv* env) { @@ -16,6 +24,27 @@ namespace Jni jclass jImStrSupplier = env->FindClass("imgui/callback/ImStrSupplier"); jImStrSupplierGetMID = env->GetMethodID(jImStrSupplier, "get", "()Ljava/lang/String;"); + + jclass jImPlatformFuncViewport = env->FindClass("imgui/callback/ImPlatformFuncViewport"); + jImPlatformFuncViewportAcceptMID = env->GetMethodID(jImPlatformFuncViewport, "accept", "(Limgui/ImGuiViewport;)V"); + + jclass jImPlatformFuncViewportFloat = env->FindClass("imgui/callback/ImPlatformFuncViewportFloat"); + jImPlatformFuncViewportFloatAcceptMID = env->GetMethodID(jImPlatformFuncViewportFloat, "accept", "(Limgui/ImGuiViewport;F)V"); + + jclass jImPlatformFuncViewportImVec2 = env->FindClass("imgui/callback/ImPlatformFuncViewportImVec2"); + jImPlatformFuncViewportImVec2AcceptMID = env->GetMethodID(jImPlatformFuncViewportImVec2, "accept", "(Limgui/ImGuiViewport;Limgui/ImVec2;)V"); + + jclass jImPlatformFuncViewportString = env->FindClass("imgui/callback/ImPlatformFuncViewportString"); + jImPlatformFuncViewportStringAcceptMID = env->GetMethodID(jImPlatformFuncViewportString, "accept", "(Limgui/ImGuiViewport;Ljava/lang/String;)V"); + + jclass jImPlatformFuncViewportSuppImVec2 = env->FindClass("imgui/callback/ImPlatformFuncViewportSuppImVec2"); + jImPlatformFuncViewportSuppImVec2GetMID = env->GetMethodID(jImPlatformFuncViewportSuppImVec2, "get", "(Limgui/ImGuiViewport;Limgui/ImVec2;)V"); + + jclass jImPlatformFuncViewportSuppBoolean = env->FindClass("imgui/callback/ImPlatformFuncViewportSuppBoolean"); + jImPlatformFuncViewportSuppBooleanGetMID = env->GetMethodID(jImPlatformFuncViewportSuppBoolean, "get", "(Limgui/ImGuiViewport;)Z"); + + jclass jImPlatformFuncViewportSuppFloat = env->FindClass("imgui/callback/ImPlatformFuncViewportSuppFloat"); + jImPlatformFuncViewportSuppFloatGetMID = env->GetMethodID(jImPlatformFuncViewportSuppFloat, "get", "(Limgui/ImGuiViewport;)F"); } void CallImListClipperCallback(JNIEnv* env, jobject consumer, int index) { @@ -29,4 +58,32 @@ namespace Jni jstring CallImStrSupplier(JNIEnv* env, jobject supplier) { return (jstring)env->CallObjectMethod(supplier, jImStrSupplierGetMID); } + + void CallImPlatformFuncViewport(JNIEnv* env, jobject func, jobject vp) { + env->CallVoidMethod(func, jImPlatformFuncViewportAcceptMID, vp); + } + + void CallImPlatformFuncViewportFloat(JNIEnv* env, jobject func, jobject vp, float f) { + env->CallVoidMethod(func, jImPlatformFuncViewportFloatAcceptMID, vp, f); + } + + void CallImPlatformFuncViewportImVec2(JNIEnv* env, jobject func, jobject vp, jobject imVec2) { + env->CallVoidMethod(func, jImPlatformFuncViewportImVec2AcceptMID, vp, imVec2); + } + + void CallImPlatformFuncViewportString(JNIEnv* env, jobject func, jobject vp, const char* str) { + env->CallVoidMethod(func, jImPlatformFuncViewportStringAcceptMID, env->NewStringUTF(str)); + } + + void CallImPlatformFuncViewportSuppImVec2(JNIEnv* env, jobject func, jobject vp, jobject imVec2) { + env->CallVoidMethod(func, jImPlatformFuncViewportSuppImVec2GetMID, vp, imVec2); + } + + jboolean CallImPlatformFuncViewportSuppBoolean(JNIEnv* env, jobject func, jobject vp) { + return (jboolean)env->CallBooleanMethod(func, jImPlatformFuncViewportSuppBooleanGetMID, vp); + } + + jfloat CallImPlatformFuncViewportSuppFloat(JNIEnv* env, jobject func, jobject vp) { + return (jfloat)env->CallFloatMethod(func, jImPlatformFuncViewportSuppFloatGetMID, vp); + } } diff --git a/imgui-binding/src/main/native/jni_callbacks.h b/imgui-binding/src/main/native/jni_callbacks.h index 4307b86..d2b0940 100644 --- a/imgui-binding/src/main/native/jni_callbacks.h +++ b/imgui-binding/src/main/native/jni_callbacks.h @@ -12,6 +12,20 @@ namespace Jni void CallImStrConsumer(JNIEnv* env, jobject consumer, const char* str); jstring CallImStrSupplier(JNIEnv* env, jobject supplier); + + void CallImPlatformFuncViewport(JNIEnv* env, jobject func, jobject vp); + + void CallImPlatformFuncViewportFloat(JNIEnv* env, jobject func, jobject vp, float f); + + void CallImPlatformFuncViewportImVec2(JNIEnv* env, jobject func, jobject vp, jobject imVec2); + + void CallImPlatformFuncViewportString(JNIEnv* env, jobject func, jobject vp, const char* str); + + void CallImPlatformFuncViewportSuppImVec2(JNIEnv* env, jobject func, jobject vp, jobject imVec2); + + jboolean CallImPlatformFuncViewportSuppBoolean(JNIEnv* env, jobject func, jobject vp); + + jfloat CallImPlatformFuncViewportSuppFloat(JNIEnv* env, jobject func, jobject vp); } #endif