Skip to content

Commit d59b45a

Browse files
committed
Refactor ImGuiGlfwExample
1 parent 41d7f4e commit d59b45a

File tree

2 files changed

+222
-197
lines changed

2 files changed

+222
-197
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import imgui.ImBool;
2+
import imgui.ImGui;
3+
import imgui.ImString;
4+
import imgui.ImVec2;
5+
import imgui.enums.ImGuiColorEditFlags;
6+
import imgui.enums.ImGuiCond;
7+
import imgui.enums.ImGuiInputTextFlags;
8+
import org.lwjgl.BufferUtils;
9+
10+
import javax.imageio.ImageIO;
11+
import java.awt.image.BufferedImage;
12+
import java.io.File;
13+
import java.nio.ByteBuffer;
14+
15+
import static org.lwjgl.opengl.GL32.*;
16+
17+
@SuppressWarnings({"MagicNumber", "VisibilityModifier"})
18+
final class ExampleUi {
19+
private static final String IMGUI_DEMO_LINK = "https://raw.githubusercontent.com/ocornut/imgui/v1.76/imgui_demo.cpp";
20+
21+
// Test data for payload
22+
private final byte[] testPayload = "Test Payload".getBytes();
23+
private String dropTargetText = "Drop Here";
24+
25+
// To modify background color dynamically
26+
final float[] backgroundColor = new float[]{0.5f, 0, 0};
27+
28+
// Resizable input example
29+
private final ImString resizableStr = new ImString(5);
30+
private final ImBool showDemoWindow = new ImBool();
31+
32+
// Attach image example
33+
private int dukeTexture;
34+
final ImVec2 windowSize = new ImVec2(); // Vector to store "Custom Window" size
35+
final ImVec2 windowPos = new ImVec2(); // Vector to store "Custom Window" position
36+
37+
void init() throws Exception {
38+
dukeTexture = loadTexture(ImageIO.read(new File("src/test/resources/Duke_waving.png")));
39+
}
40+
41+
void render() {
42+
ImGui.setNextWindowSize(600, 300, ImGuiCond.Once);
43+
ImGui.setNextWindowPos(10, 10, ImGuiCond.Once);
44+
45+
ImGui.begin("Custom window"); // Start Custom window
46+
47+
// Draw an image in the bottom-right corner of the window
48+
ImGui.getWindowSize(windowSize);
49+
ImGui.getWindowPos(windowPos);
50+
final float xPoint = windowPos.x + windowSize.x - 100;
51+
final float yPoint = windowPos.y + windowSize.y;
52+
ImGui.getWindowDrawList().addImage(dukeTexture, xPoint, yPoint - 180, xPoint + 100, yPoint);
53+
54+
// Checkbox to show demo window
55+
ImGui.checkbox("Show demo window", showDemoWindow);
56+
57+
ImGui.separator();
58+
59+
// Drag'n'Drop functionality
60+
ImGui.button("Drag me");
61+
if (ImGui.beginDragDropSource()) {
62+
ImGui.setDragDropPayload("payload_type", testPayload, testPayload.length);
63+
ImGui.text("Drag started");
64+
ImGui.endDragDropSource();
65+
}
66+
ImGui.sameLine();
67+
ImGui.text(dropTargetText);
68+
if (ImGui.beginDragDropTarget()) {
69+
final byte[] payload = ImGui.acceptDragDropPayload("payload_type");
70+
if (payload != null) {
71+
dropTargetText = new String(payload);
72+
}
73+
ImGui.endDragDropTarget();
74+
}
75+
76+
// Color picker
77+
ImGui.alignTextToFramePadding();
78+
ImGui.text("Background color:");
79+
ImGui.sameLine();
80+
ImGui.colorEdit3("##click_counter_col", backgroundColor, ImGuiColorEditFlags.NoInputs | ImGuiColorEditFlags.NoDragDrop);
81+
82+
ImGui.separator();
83+
84+
// Input field with auto-resize ability
85+
ImGui.text("You can use text inputs with auto-resizable strings!");
86+
ImGui.inputText("Resizable input", resizableStr, ImGuiInputTextFlags.CallbackResize);
87+
ImGui.text("text len:");
88+
ImGui.sameLine();
89+
ImGui.textColored(.12f, .6f, 1, 1, Integer.toString(resizableStr.getLength()));
90+
ImGui.sameLine();
91+
ImGui.text("| buffer size:");
92+
ImGui.sameLine();
93+
ImGui.textColored(1, .6f, 0, 1, Integer.toString(resizableStr.getBufferSize()));
94+
95+
ImGui.separator();
96+
ImGui.newLine();
97+
98+
// Link to the original demo file
99+
ImGui.text("Consider to look the original ImGui demo: ");
100+
ImGui.setNextItemWidth(500);
101+
ImGui.textColored(0, .8f, 0, 1, IMGUI_DEMO_LINK);
102+
ImGui.sameLine();
103+
if (ImGui.button("Copy")) {
104+
ImGui.setClipboardText(IMGUI_DEMO_LINK);
105+
}
106+
107+
ImGui.end(); // End Custom window
108+
109+
if (showDemoWindow.get()) {
110+
ImGui.showDemoWindow(showDemoWindow);
111+
}
112+
}
113+
114+
private int loadTexture(final BufferedImage image) {
115+
final int[] pixels = new int[image.getWidth() * image.getHeight()];
116+
image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
117+
118+
final ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4); // 4 for RGBA, 3 for RGB
119+
for (int y = 0; y < image.getHeight(); y++) {
120+
for (int x = 0; x < image.getWidth(); x++) {
121+
final int pixel = pixels[y * image.getWidth() + x];
122+
buffer.put((byte) ((pixel >> 16) & 0xFF));
123+
buffer.put((byte) ((pixel >> 8) & 0xFF));
124+
buffer.put((byte) (pixel & 0xFF));
125+
buffer.put((byte) ((pixel >> 24) & 0xFF));
126+
}
127+
}
128+
buffer.flip();
129+
130+
final int textureID = glGenTextures();
131+
glBindTexture(GL_TEXTURE_2D, textureID);
132+
133+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
134+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
135+
136+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
137+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
138+
139+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
140+
141+
return textureID;
142+
}
143+
}

0 commit comments

Comments
 (0)