From f6e9712b38a578a8fb05a05d7f617b7ee32d95f5 Mon Sep 17 00:00:00 2001 From: SpaiR Date: Sat, 21 Dec 2019 01:54:45 +0300 Subject: [PATCH] Example tweaks --- imgui-binding/src/main/java/imgui/ImGui.java | 8 ++++ imgui-binding/src/main/java/imgui/Native.java | 10 ++++ .../java/imgui/enums/ImGuiMouseCursor.java | 9 ++++ .../src/main/java/imgui/gl3/ImGuiImplGl3.java | 24 +++------- .../src/test/java/ImGuiGlfwExample.java | 47 +++++++++++++++++-- 5 files changed, 76 insertions(+), 22 deletions(-) diff --git a/imgui-binding/src/main/java/imgui/ImGui.java b/imgui-binding/src/main/java/imgui/ImGui.java index bd9f7ff..0ee7a7e 100644 --- a/imgui-binding/src/main/java/imgui/ImGui.java +++ b/imgui-binding/src/main/java/imgui/ImGui.java @@ -1568,6 +1568,14 @@ public class ImGui { return Native.IsMouseHoveringRect(minX, minY, maxX, maxY, clip); } + public static ImGuiMouseCursor GetMouseCursor() { + return ImGuiMouseCursor.valueOf(Native.GetMouseCursor()); + } + + public static void SetMouseCursor(ImGuiMouseCursor type) { + Native.SetMouseCursor(type.getValue()); + } + // Internal methods public static void PushMultiItemsWidths(int components, float w_full) { diff --git a/imgui-binding/src/main/java/imgui/Native.java b/imgui-binding/src/main/java/imgui/Native.java index cdbf764..2417348 100644 --- a/imgui-binding/src/main/java/imgui/Native.java +++ b/imgui-binding/src/main/java/imgui/Native.java @@ -1,5 +1,7 @@ package imgui; +import imgui.enums.ImGuiMouseCursor; + import java.nio.Buffer; /** @@ -2148,5 +2150,13 @@ class Native { return ImGui::IsMouseHoveringRect(ImVec2(minX, minY), ImVec2(maxX, maxY), clip); */ + static native int GetMouseCursor(); /* + return (int)ImGui::GetMouseCursor(); + */ + + static native void SetMouseCursor(int type); /* + ImGui::SetMouseCursor(type); + */ + // @formatter:on } diff --git a/imgui-binding/src/main/java/imgui/enums/ImGuiMouseCursor.java b/imgui-binding/src/main/java/imgui/enums/ImGuiMouseCursor.java index 0c94608..75e28a4 100644 --- a/imgui-binding/src/main/java/imgui/enums/ImGuiMouseCursor.java +++ b/imgui-binding/src/main/java/imgui/enums/ImGuiMouseCursor.java @@ -25,4 +25,13 @@ public enum ImGuiMouseCursor { public int getValue() { return value; } + + public static ImGuiMouseCursor valueOf(int value) { + for (ImGuiMouseCursor cursor : values()) { + if (cursor.value == value) { + return cursor; + } + } + throw new IllegalArgumentException("Invalid cursor value: " + value); + } } diff --git a/imgui-lwjgl3/src/main/java/imgui/gl3/ImGuiImplGl3.java b/imgui-lwjgl3/src/main/java/imgui/gl3/ImGuiImplGl3.java index 6f7c9e2..ab9159e 100644 --- a/imgui-lwjgl3/src/main/java/imgui/gl3/ImGuiImplGl3.java +++ b/imgui-lwjgl3/src/main/java/imgui/gl3/ImGuiImplGl3.java @@ -20,8 +20,7 @@ import static org.lwjgl.opengl.GL30.*; * It do support a backup of the current GL state before rendering and restoring of its initial state after. * But some specific state variables may be missed (all which are hidden under '#ifdef' macros in the original 'imgui_impl_opengl3.cpp' file). *

- * By default this implementation uses shaders with 130 version of GLSL (OpenGL 3.0). - * You can provide your own shaders into the {@link ImGuiImplGl3#init(CharSequence, CharSequence)} method. + * This implementation uses shaders with 130 version of GLSL (OpenGL 3.0). */ @SuppressWarnings("MagicNumber") public final class ImGuiImplGl3 { @@ -61,23 +60,9 @@ public final class ImGuiImplGl3 { /** * This method SHOULD be called before calling of {@link ImGuiImplGl3#render(DrawData)} method. - * By using this method shaders with 130 version of GLSL (OpenGL 3.0) will be used for render. */ public void init() { - final CharSequence fragShaderSource = readFromResources("default.frag"); - final CharSequence vertShaderSource = readFromResources("default.vert"); - prepareShader(fragShaderSource, vertShaderSource); - prepareFont(); - } - - /** - * This method SHOULD be called before calling of {@link ImGuiImplGl3#render(DrawData)} method. - * - * @param fragShaderSource fragment shader to use for render - * @param vertShaderSource vertex shader to use for render - */ - public void init(final CharSequence fragShaderSource, final CharSequence vertShaderSource) { - prepareShader(fragShaderSource, vertShaderSource); + prepareShader(); prepareFont(); } @@ -187,7 +172,10 @@ public final class ImGuiImplGl3 { glDeleteProgram(programId); } - private void prepareShader(final CharSequence fragShaderSource, final CharSequence vertShaderSource) { + private void prepareShader() { + final CharSequence fragShaderSource = readFromResources("default.frag"); + final CharSequence vertShaderSource = readFromResources("default.vert"); + fragmentShaderId = loadAndCompileShader(GL_FRAGMENT_SHADER, fragShaderSource); vertexShaderId = loadAndCompileShader(GL_VERTEX_SHADER, vertShaderSource); diff --git a/imgui-lwjgl3/src/test/java/ImGuiGlfwExample.java b/imgui-lwjgl3/src/test/java/ImGuiGlfwExample.java index 16eadce..78f8116 100644 --- a/imgui-lwjgl3/src/test/java/ImGuiGlfwExample.java +++ b/imgui-lwjgl3/src/test/java/ImGuiGlfwExample.java @@ -3,6 +3,7 @@ import imgui.ImGuiString; import imgui.enums.ImGuiCond; import imgui.enums.ImGuiInputTextFlags; import imgui.enums.ImGuiKey; +import imgui.enums.ImGuiMouseCursor; import imgui.enums.ImGuiTreeNodeFlags; import imgui.gl3.ImGuiImplGl3; import org.lwjgl.glfw.GLFWErrorCallback; @@ -24,8 +25,6 @@ public final class ImGuiGlfwExample { private static final int DEFAULT_WIDTH = 1024; private static final int DEFAULT_HEIGHT = 768; - private static final ImGuiString IMGUI_DEMO_LINK = new ImGuiString("https://raw.githubusercontent.com/ocornut/imgui/v1.74/imgui_demo.cpp"); - private long window; // current GLFW window ID // Those are used to track window size properties @@ -44,8 +43,14 @@ public final class ImGuiGlfwExample { private boolean altKeyDown; private boolean superKeyDown; + private final long[] mouseCursors = new long[ImGuiMouseCursor.COUNT.getValue()]; + private final ImGuiImplGl3 imGuiGl3 = new ImGuiImplGl3(); + // Local app variables go here + private final ImGuiString imguiDemoLink = new ImGuiString("https://raw.githubusercontent.com/ocornut/imgui/v1.74/imgui_demo.cpp"); + private int clickCount = 0; + public void run() { initGlfw(); initImGui(); @@ -70,6 +75,7 @@ public final class ImGuiGlfwExample { glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE); // the window will be maximized + glfwWindowHint(GLFW_DECORATED, GLFW_TRUE); // the window will be decorated // Create the window window = glfwCreateWindow(DEFAULT_WIDTH, DEFAULT_HEIGHT, "ImGui+GLFW+LWJGL Example", NULL, NULL); @@ -97,6 +103,7 @@ public final class ImGuiGlfwExample { glfwSwapInterval(GLFW_TRUE); // Enable v-sync glfwShowWindow(window); // Make the window visible + // IMPORTANT!! // This line is critical for LWJGL's interoperation with GLFW's // OpenGL context, or any context that is managed externally. // LWJGL detects the context that is current in the current thread, @@ -107,6 +114,7 @@ public final class ImGuiGlfwExample { // Here we will initialize ImGui stuff. private void initImGui() { + // IMPORTANT!! // This line is critical for ImGui to work. // It loads native library, creates ImGui context and does other necessary stuff. ImGui.init(); @@ -142,6 +150,16 @@ public final class ImGuiGlfwExample { keys[ImGuiKey.Z.code] = GLFW_KEY_Z; ImGui.initKeyMap(keys); + // Mouse cursors mapping + mouseCursors[ImGuiMouseCursor.Arrow.getValue()] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); + mouseCursors[ImGuiMouseCursor.TextInput.getValue()] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR); + mouseCursors[ImGuiMouseCursor.ResizeAll.getValue()] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); + mouseCursors[ImGuiMouseCursor.ResizeNS.getValue()] = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR); + mouseCursors[ImGuiMouseCursor.ResizeEW.getValue()] = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR); + mouseCursors[ImGuiMouseCursor.ResizeNESW.getValue()] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); + mouseCursors[ImGuiMouseCursor.ResizeNWSE.getValue()] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); + mouseCursors[ImGuiMouseCursor.Hand.getValue()] = glfwCreateStandardCursor(GLFW_HAND_CURSOR); + // Here goes GLFW callbacks to update user input stuff in ImGui glfwSetKeyCallback(window, (w, key, scancode, action, mods) -> { final boolean isKeyDown = action == GLFW_PRESS; @@ -210,9 +228,15 @@ public final class ImGuiGlfwExample { ImGui.UpdateMousePos((float) mousePosX[0], (float) mousePosY[0]); ImGui.UpdateDeltaTime((float) deltaTime); + // Update mouse cursor + final ImGuiMouseCursor imguiCursor = ImGui.GetMouseCursor(); + glfwSetCursor(window, mouseCursors[imguiCursor.getValue()]); + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + // Render itself starts here glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer + // IMPORTANT!! // Any ImGui code SHOULD go between NewFrame()/Render() methods ImGui.NewFrame(); @@ -221,6 +245,14 @@ public final class ImGuiGlfwExample { ImGui.Begin("Custom window"); ImGui.Text("Hello from Java!"); + if (ImGui.Button("Click")) { + clickCount++; + } + if (ImGui.IsItemHovered()) { + ImGui.SetMouseCursor(ImGuiMouseCursor.Hand); + } + ImGui.SameLine(); + ImGui.Text("Count: " + clickCount); ImGui.Separator(); ImGui.BeginChild("##custom_window_child", 200, 50); @@ -233,11 +265,14 @@ public final class ImGuiGlfwExample { ImGui.Separator(); ImGui.Text("Consider to look the original ImGui demo: "); ImGui.SetNextItemWidth(500); - ImGui.InputText("##input_to_copy_link", IMGUI_DEMO_LINK, ImGuiInputTextFlags.ReadOnly); + ImGui.InputText("##input_to_copy_link", imguiDemoLink, ImGuiInputTextFlags.ReadOnly); + if (ImGui.IsItemHovered()) { + ImGui.SetMouseCursor(ImGuiMouseCursor.TextInput); + } ImGui.SameLine(); ImGui.Text("(?)"); if (ImGui.IsItemHovered()) { - ImGui.SetTooltip("You can copy and paste this link"); + ImGui.SetTooltip("You can copy and paste this link to browser"); } ImGui.End(); @@ -260,6 +295,10 @@ public final class ImGuiGlfwExample { } private void destroyGlfw() { + for (long mouseCursor : mouseCursors) { + glfwDestroyCursor(mouseCursor); + } + glfwFreeCallbacks(window); glfwDestroyWindow(window); glfwTerminate();