[Example] Add example of Drag & Drop

This commit is contained in:
SpaiR committed 2021-03-09 17:58:21 +02:00
1 parent 533fc7f979
commit 3fe77320f3
3 files changed
+142 -7

No files matched your search

@@ -0,0 +1,128 @@
import imgui.ImGui;
import imgui.flag.ImGuiDragDropFlags;
import imgui.flag.ImGuiInputTextFlags;
import imgui.flag.ImGuiWindowFlags;
import imgui.type.ImBoolean;
import imgui.type.ImInt;
import imgui.type.ImString;
import java.util.concurrent.atomic.AtomicInteger;
public class ExampleDragAndDrop {
private static final String PAYLOAD_TYPE = "PAYLOAD";
private static final StringPayload STRING_PAYLOAD = new StringPayload();
private static final IntegerPayload INTEGER_PAYLOAD = new IntegerPayload();
private static final AtomicInteger CLASS_SPECIFIC_PAYLOAD = new AtomicInteger(23);
private static String data = "No Data...";
public static void show(final ImBoolean showDragNDropWindow) {
if (ImGui.begin("Drag'N'Drop Demo", showDragNDropWindow, ImGuiWindowFlags.AlwaysAutoResize)) {
ImGui.text("Drag from here:");
ImGui.inputText("String payload", STRING_PAYLOAD.input, ImGuiInputTextFlags.CallbackResize);
setupPayload(STRING_PAYLOAD);
ImGui.inputInt("Integer payload", INTEGER_PAYLOAD.input);
setupPayload(INTEGER_PAYLOAD);
ImGui.button("Class specific payload");
setupClassSpecificPayload();
ImGui.separator();
ImGui.text("Drop here any:");
ImGui.button(data, 100, 50);
setupTarget();
ImGui.separator();
ImGui.text("Drop here string payload only");
ImGui.button(data, 100, 50);
setupStringPayloadTarget();
ImGui.separator();
ImGui.text("Drop here class specific payload only");
ImGui.button(data, 100, 50);
setupClassSpecificPayloadTarget();
}
ImGui.end();
}
private static void setupPayload(final Payload<?> payload) {
if (ImGui.beginDragDropSource(ImGuiDragDropFlags.SourceAllowNullID)) {
ImGui.setDragDropPayload(PAYLOAD_TYPE, payload);
ImGui.text("Dragging: " + payload.getData());
ImGui.endDragDropSource();
}
}
private static void setupClassSpecificPayload() {
if (ImGui.beginDragDropSource()) {
ImGui.setDragDropPayload(CLASS_SPECIFIC_PAYLOAD);
ImGui.text("Dragging class specific payload");
ImGui.endDragDropSource();
}
}
private static void setupTarget() {
if (ImGui.beginDragDropTarget()) {
Payload<?> payload = ImGui.acceptDragDropPayload(PAYLOAD_TYPE);
if (payload != null) {
data = String.valueOf(payload.getData());
}
ImGui.endDragDropTarget();
}
}
/**
* Type safe example. ImGui will show that it can accept payload, but payload itself will be null.
*/
private static void setupStringPayloadTarget() {
if (ImGui.beginDragDropTarget()) {
StringPayload payload = ImGui.acceptDragDropPayload(PAYLOAD_TYPE, StringPayload.class);
if (payload != null) {
data = payload.getData();
}
ImGui.endDragDropTarget();
}
}
/**
* Class specific example. We can bind our payload to a specific class, so it will be 100% type safe.
*/
private static void setupClassSpecificPayloadTarget() {
if (ImGui.beginDragDropTarget()) {
AtomicInteger payload = ImGui.acceptDragDropPayload(AtomicInteger.class);
if (payload != null) {
data = String.valueOf(payload.get());
}
ImGui.endDragDropTarget();
}
}
private interface Payload<T> {
T getData();
}
private static final class StringPayload implements Payload<String> {
ImString input = new ImString("You can drag inputs as well");
@Override
public String getData() {
return input.get();
}
}
private static final class IntegerPayload implements Payload<Integer> {
ImInt input = new ImInt();
@Override
public Integer getData() {
return input.get();
}
}
}
+6
View File
@@ -6,6 +6,7 @@ public class Extra {
private static final ImBoolean SHOW_DEMO_WINDOW = new ImBoolean(false);
private static final ImBoolean SHOW_IMNODES_DEMO_WINDOW = new ImBoolean(false);
private static final ImBoolean SHOW_IMGUI_NODE_EDITOR_DEMO_WINDOW = new ImBoolean(false);
private static final ImBoolean SHOW_DRAG_N_DROP_WINDOW = new ImBoolean(false);
private static final Graph GRAPH = new Graph();
@@ -14,6 +15,7 @@ public class Extra {
ImGui.checkbox("Show Demo Window", SHOW_DEMO_WINDOW);
ImGui.checkbox("Show ImNodes Demo Window", SHOW_IMNODES_DEMO_WINDOW);
ImGui.checkbox("Show imgui-node-editor Demo Window", SHOW_IMGUI_NODE_EDITOR_DEMO_WINDOW);
ImGui.checkbox("Show Drag'N'Drop Demo Window", SHOW_DRAG_N_DROP_WINDOW);
if (SHOW_DEMO_WINDOW.get()) {
ImGui.showDemoWindow(SHOW_DEMO_WINDOW);
@@ -26,5 +28,9 @@ public class Extra {
if (SHOW_IMGUI_NODE_EDITOR_DEMO_WINDOW.get()) {
ExampleImGuiNodeEditor.show(SHOW_IMGUI_NODE_EDITOR_DEMO_WINDOW, GRAPH);
}
if (SHOW_DRAG_N_DROP_WINDOW.get()) {
ExampleDragAndDrop.show(SHOW_DRAG_N_DROP_WINDOW);
}
}
}