Skip to content

Commit c8931af

Browse files
committed
[fonts] Example class comments
1 parent 01b7bec commit c8931af

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

imgui-lwjgl3/src/test/java/ImGuiGlfwExample.java

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import imgui.enums.ImGuiInputTextFlags;
1212
import imgui.enums.ImGuiKey;
1313
import imgui.enums.ImGuiMouseCursor;
14-
import imgui.enums.ImGuiWindowFlags;
1514
import imgui.gl3.ImGuiImplGl3;
1615
import org.lwjgl.glfw.GLFWErrorCallback;
1716
import org.lwjgl.glfw.GLFWVidMode;
@@ -47,16 +46,16 @@ public final class ImGuiGlfwExample {
4746
// Mouse cursors provided by GLFW
4847
private final long[] mouseCursors = new long[ImGuiMouseCursor.COUNT];
4948

49+
// LWJGL3 rendered itself (SHOULD be initialized)
5050
private final ImGuiImplGl3 imGuiGl3 = new ImGuiImplGl3();
5151

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.
5555
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
5757
private int clickCount = 0;
5858
private final ImString resizableStr = new ImString(5);
59-
6059
private final ImBool showDemoWindow = new ImBool();
6160

6261
public void run() {
@@ -67,7 +66,7 @@ public void run() {
6766
destroyGlfw();
6867
}
6968

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
7170
private void initGlfw() {
7271
// Setup an error callback. The default implementation
7372
// will print the error message in System.err.
@@ -85,7 +84,7 @@ private void initGlfw() {
8584
glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE); // the window will be maximized
8685

8786
// 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);
8988

9089
if (window == NULL) {
9190
throw new RuntimeException("Failed to create the GLFW window");
@@ -125,19 +124,18 @@ private void initImGui() {
125124
// This line is critical for Dear ImGui to work.
126125
ImGui.createContext();
127126

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.
129129
ImGui.styleColorsClassic();
130-
// ImGui.StyleColorsDark(); // This is a default style for ImGui
131-
// ImGui.StyleColorsLight();
132130

133131
// Initialize ImGuiIO config
134132
final ImGuiIO io = ImGui.getIO();
135133

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
141139

142140
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
143141
final int[] keyMap = new int[ImGuiKey.COUNT];
@@ -175,7 +173,9 @@ private void initImGui() {
175173
mouseCursors[ImGuiMouseCursor.ResizeNWSE] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
176174
mouseCursors[ImGuiMouseCursor.Hand] = glfwCreateStandardCursor(GLFW_HAND_CURSOR);
177175

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+
179179
glfwSetKeyCallback(window, (w, key, scancode, action, mods) -> {
180180
if (action == GLFW_PRESS) {
181181
io.setKeysDown(key, true);
@@ -237,6 +237,7 @@ public String get() {
237237
imGuiGl3.init();
238238
}
239239

240+
// Main application loop
240241
private void loop() {
241242
double time = 0; // to track our frame delta value
242243

@@ -247,8 +248,9 @@ private void loop() {
247248
final double deltaTime = (time > 0) ? (currentTime - time) : 1f / 60f;
248249
time = currentTime;
249250

250-
// Set the clear color
251+
// Set the clear color and do clear itself
251252
glClearColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], 0.0f);
253+
glClear(GL_COLOR_BUFFER_BIT);
252254

253255
// Get window size properties and mouse position
254256
glfwGetWindowSize(window, winWidth, winHeight);
@@ -268,16 +270,13 @@ private void loop() {
268270
glfwSetCursor(window, mouseCursors[imguiCursor]);
269271
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
270272

271-
// Clear the framebuffer
272-
glClear(GL_COLOR_BUFFER_BIT);
273-
274273
// IMPORTANT!!
275-
// Any ImGui code SHOULD go between NewFrame()/Render() methods
274+
// Any Dear ImGui code SHOULD go between NewFrame()/Render() methods
276275
ImGui.newFrame();
277-
showUi(); // ImGui calls goes here
276+
showUi();
278277
ImGui.render();
279278

280-
// After ImGui#render call we provide draw data into LWJGL3 render.
279+
// After ImGui#render call we provide draw data into LWJGL3 renderer.
281280
// At that moment ImGui will be rendered to the current OpenGL context.
282281
imGuiGl3.render(ImGui.getDrawData());
283282

@@ -292,9 +291,14 @@ private void showUi() {
292291
ImGui.setNextWindowSize(600, 300, ImGuiCond.Once);
293292
ImGui.setNextWindowPos(10, 10, ImGuiCond.Once);
294293

295-
ImGui.begin("Custom window", ImGuiWindowFlags.NoDecoration); // Start Custom window
296-
ImGui.text("Hello from Java!");
294+
ImGui.begin("Custom window"); // Start Custom window
297295

296+
// Simple text label
297+
ImGui.checkbox("Show demo window", showDemoWindow);
298+
299+
ImGui.separator();
300+
301+
// Drag'n'Drop functionality
298302
ImGui.button("Drag me");
299303
if (ImGui.beginDragDropSource()) {
300304
ImGui.setDragDropPayload("payload_type", testPayload, testPayload.length);
@@ -311,11 +315,13 @@ private void showUi() {
311315
ImGui.endDragDropTarget();
312316
}
313317

318+
// Color picker
314319
ImGui.alignTextToFramePadding();
315320
ImGui.text("Background color:");
316321
ImGui.sameLine();
317322
ImGui.colorEdit3("##click_counter_col", backgroundColor, ImGuiColorEditFlags.NoInputs | ImGuiColorEditFlags.NoDragDrop);
318323

324+
// Simple click counter
319325
if (ImGui.button("Click")) {
320326
clickCount++;
321327
}
@@ -324,14 +330,24 @@ private void showUi() {
324330
}
325331
ImGui.sameLine();
326332
ImGui.text("Count: " + clickCount);
327-
ImGui.checkbox("Show demo window", showDemoWindow);
328-
ImGui.newLine();
329333

334+
ImGui.separator();
335+
336+
// Input field with auto-resize ability
337+
ImGui.text("You can use text inputs with auto-resizable strings!");
330338
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()));
333346

334347
ImGui.separator();
348+
ImGui.newLine();
349+
350+
// Link to the original demo file
335351
ImGui.text("Consider to look the original ImGui demo: ");
336352
ImGui.setNextItemWidth(500);
337353
ImGui.textColored(0, .8f, 0, 1, imguiDemoLink);

0 commit comments

Comments
 (0)