11
11
import imgui .enums .ImGuiInputTextFlags ;
12
12
import imgui .enums .ImGuiKey ;
13
13
import imgui .enums .ImGuiMouseCursor ;
14
- import imgui .enums .ImGuiWindowFlags ;
15
14
import imgui .gl3 .ImGuiImplGl3 ;
16
15
import org .lwjgl .glfw .GLFWErrorCallback ;
17
16
import org .lwjgl .glfw .GLFWVidMode ;
@@ -47,16 +46,16 @@ public final class ImGuiGlfwExample {
47
46
// Mouse cursors provided by GLFW
48
47
private final long [] mouseCursors = new long [ImGuiMouseCursor .COUNT ];
49
48
49
+ // LWJGL3 rendered itself (SHOULD be initialized)
50
50
private final ImGuiImplGl3 imGuiGl3 = new ImGuiImplGl3 ();
51
51
52
- // Local app variables go here
53
- private final String imguiDemoLink = "https://raw.githubusercontent.com/ocornut/imgui/v1.74/imgui_demo.cpp" ;
54
- private final byte [] testPayload = "Test Payload" .getBytes ();
52
+ // Local variables for application goes here
53
+ private final String imguiDemoLink = "https://raw.githubusercontent.com/ocornut/imgui/v1.74/imgui_demo.cpp" ; // Link to put into clipboard
54
+ private final byte [] testPayload = "Test Payload" .getBytes (); // Test data for payload. Should be represented as raw byt array.
55
55
private String dropTargetText = "Drop Here" ;
56
- private float [] backgroundColor = new float []{0.5f , 0 , 0 };
56
+ private float [] backgroundColor = new float []{0.5f , 0 , 0 }; // To modify background color dynamically
57
57
private int clickCount = 0 ;
58
58
private final ImString resizableStr = new ImString (5 );
59
-
60
59
private final ImBool showDemoWindow = new ImBool ();
61
60
62
61
public void run () {
@@ -67,7 +66,7 @@ public void run() {
67
66
destroyGlfw ();
68
67
}
69
68
70
- // Method initializes GLFW window. All code is mostly a copy-paste from the official LWJGL3 website.
69
+ // Method initializes GLFW window. All code is mostly a copy-paste from the official LWJGL3 "Get Started": https://www.lwjgl.org/guide
71
70
private void initGlfw () {
72
71
// Setup an error callback. The default implementation
73
72
// will print the error message in System.err.
@@ -85,7 +84,7 @@ private void initGlfw() {
85
84
glfwWindowHint (GLFW_MAXIMIZED , GLFW_TRUE ); // the window will be maximized
86
85
87
86
// Create the window
88
- window = glfwCreateWindow (DEFAULT_WIDTH , DEFAULT_HEIGHT , "ImGui+ GLFW+ LWJGL Example" , NULL , NULL );
87
+ window = glfwCreateWindow (DEFAULT_WIDTH , DEFAULT_HEIGHT , "Dear ImGui + GLFW + LWJGL Example" , NULL , NULL );
89
88
90
89
if (window == NULL ) {
91
90
throw new RuntimeException ("Failed to create the GLFW window" );
@@ -125,19 +124,18 @@ private void initImGui() {
125
124
// This line is critical for Dear ImGui to work.
126
125
ImGui .createContext ();
127
126
128
- // ImGui provides three different color schemas for styling. We will use the classic one here.
127
+ // ImGui provides 3 different color schemas for styling. We will use the classic one here.
128
+ // Try others with ImGui.styleColors*() methods.
129
129
ImGui .styleColorsClassic ();
130
- // ImGui.StyleColorsDark(); // This is a default style for ImGui
131
- // ImGui.StyleColorsLight();
132
130
133
131
// Initialize ImGuiIO config
134
132
final ImGuiIO io = ImGui .getIO ();
135
133
136
- io .setIniFilename (null );
137
- io .setConfigFlags (ImGuiConfigFlags .NavEnableKeyboard );
138
- io .setBackendFlags (ImGuiBackendFlags .HasMouseCursors );
139
- io .setBackendPlatformName ("imgui_java_impl_glfw" );
140
- io .setBackendRendererName ("imgui_java_impl_lwjgl" );
134
+ io .setIniFilename (null ); // We don't want to save .ini file
135
+ io .setConfigFlags (ImGuiConfigFlags .NavEnableKeyboard ); // Navigation with keyboard
136
+ io .setBackendFlags (ImGuiBackendFlags .HasMouseCursors ); // Mouse cursors to display while resizing windows etc.
137
+ io .setBackendPlatformName ("imgui_java_impl_glfw" ); // For debug purpose
138
+ io .setBackendRendererName ("imgui_java_impl_lwjgl" ); // For debug purpose
141
139
142
140
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
143
141
final int [] keyMap = new int [ImGuiKey .COUNT ];
@@ -175,7 +173,9 @@ private void initImGui() {
175
173
mouseCursors [ImGuiMouseCursor .ResizeNWSE ] = glfwCreateStandardCursor (GLFW_ARROW_CURSOR );
176
174
mouseCursors [ImGuiMouseCursor .Hand ] = glfwCreateStandardCursor (GLFW_HAND_CURSOR );
177
175
178
- // Here goes GLFW callbacks to update user input stuff in ImGui
176
+ // ------------------------------------------------------------
177
+ // Here goes GLFW callbacks to update user input in Dear ImGui
178
+
179
179
glfwSetKeyCallback (window , (w , key , scancode , action , mods ) -> {
180
180
if (action == GLFW_PRESS ) {
181
181
io .setKeysDown (key , true );
@@ -237,6 +237,7 @@ public String get() {
237
237
imGuiGl3 .init ();
238
238
}
239
239
240
+ // Main application loop
240
241
private void loop () {
241
242
double time = 0 ; // to track our frame delta value
242
243
@@ -247,8 +248,9 @@ private void loop() {
247
248
final double deltaTime = (time > 0 ) ? (currentTime - time ) : 1f / 60f ;
248
249
time = currentTime ;
249
250
250
- // Set the clear color
251
+ // Set the clear color and do clear itself
251
252
glClearColor (backgroundColor [0 ], backgroundColor [1 ], backgroundColor [2 ], 0.0f );
253
+ glClear (GL_COLOR_BUFFER_BIT );
252
254
253
255
// Get window size properties and mouse position
254
256
glfwGetWindowSize (window , winWidth , winHeight );
@@ -268,16 +270,13 @@ private void loop() {
268
270
glfwSetCursor (window , mouseCursors [imguiCursor ]);
269
271
glfwSetInputMode (window , GLFW_CURSOR , GLFW_CURSOR_NORMAL );
270
272
271
- // Clear the framebuffer
272
- glClear (GL_COLOR_BUFFER_BIT );
273
-
274
273
// IMPORTANT!!
275
- // Any ImGui code SHOULD go between NewFrame()/Render() methods
274
+ // Any Dear ImGui code SHOULD go between NewFrame()/Render() methods
276
275
ImGui .newFrame ();
277
- showUi (); // ImGui calls goes here
276
+ showUi ();
278
277
ImGui .render ();
279
278
280
- // After ImGui#render call we provide draw data into LWJGL3 render .
279
+ // After ImGui#render call we provide draw data into LWJGL3 renderer .
281
280
// At that moment ImGui will be rendered to the current OpenGL context.
282
281
imGuiGl3 .render (ImGui .getDrawData ());
283
282
@@ -292,9 +291,14 @@ private void showUi() {
292
291
ImGui .setNextWindowSize (600 , 300 , ImGuiCond .Once );
293
292
ImGui .setNextWindowPos (10 , 10 , ImGuiCond .Once );
294
293
295
- ImGui .begin ("Custom window" , ImGuiWindowFlags .NoDecoration ); // Start Custom window
296
- ImGui .text ("Hello from Java!" );
294
+ ImGui .begin ("Custom window" ); // Start Custom window
297
295
296
+ // Simple text label
297
+ ImGui .checkbox ("Show demo window" , showDemoWindow );
298
+
299
+ ImGui .separator ();
300
+
301
+ // Drag'n'Drop functionality
298
302
ImGui .button ("Drag me" );
299
303
if (ImGui .beginDragDropSource ()) {
300
304
ImGui .setDragDropPayload ("payload_type" , testPayload , testPayload .length );
@@ -311,11 +315,13 @@ private void showUi() {
311
315
ImGui .endDragDropTarget ();
312
316
}
313
317
318
+ // Color picker
314
319
ImGui .alignTextToFramePadding ();
315
320
ImGui .text ("Background color:" );
316
321
ImGui .sameLine ();
317
322
ImGui .colorEdit3 ("##click_counter_col" , backgroundColor , ImGuiColorEditFlags .NoInputs | ImGuiColorEditFlags .NoDragDrop );
318
323
324
+ // Simple click counter
319
325
if (ImGui .button ("Click" )) {
320
326
clickCount ++;
321
327
}
@@ -324,14 +330,24 @@ private void showUi() {
324
330
}
325
331
ImGui .sameLine ();
326
332
ImGui .text ("Count: " + clickCount );
327
- ImGui .checkbox ("Show demo window" , showDemoWindow );
328
- ImGui .newLine ();
329
333
334
+ ImGui .separator ();
335
+
336
+ // Input field with auto-resize ability
337
+ ImGui .text ("You can use text inputs with auto-resizable strings!" );
330
338
ImGui .inputText ("Resizable input" , resizableStr , ImGuiInputTextFlags .CallbackResize );
331
- ImGui .text (String .format ("text len: %d | buffer size: %d" , resizableStr .getLength (), resizableStr .getBufferSize ()));
332
- ImGui .newLine ();
339
+ ImGui .text ("text len:" );
340
+ ImGui .sameLine ();
341
+ ImGui .textColored (.12f , .6f , 1 , 1 , Integer .toString (resizableStr .getLength ()));
342
+ ImGui .sameLine ();
343
+ ImGui .text ("| buffer size:" );
344
+ ImGui .sameLine ();
345
+ ImGui .textColored (1 , .6f , 0 , 1 , Integer .toString (resizableStr .getBufferSize ()));
333
346
334
347
ImGui .separator ();
348
+ ImGui .newLine ();
349
+
350
+ // Link to the original demo file
335
351
ImGui .text ("Consider to look the original ImGui demo: " );
336
352
ImGui .setNextItemWidth (500 );
337
353
ImGui .textColored (0 , .8f , 0 , 1 , imguiDemoLink );
0 commit comments