[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);
}
}
}
+8 -7
View File
@@ -1,5 +1,6 @@
package imgui;
import imgui.flag.ImGuiCond;
import imgui.flag.ImGuiDragDropFlags;
import imgui.flag.ImGuiInputTextFlags;
import imgui.type.ImBoolean;
@@ -4868,7 +4869,7 @@ public class ImGui {
* Binding stores a reference to the object in a form of {@link WeakReference}.
*/
public static boolean setDragDropPayload(final String dataType, final Object payload) {
return setDragDropPayload(dataType, payload, ImGuiDragDropFlags.None);
return setDragDropPayload(dataType, payload, ImGuiCond.None);
}
/**
@@ -4886,14 +4887,14 @@ public class ImGui {
}
/**
* Binding alternative for {@link #setDragDropPayload(String, Object)}, but uses payload class as a unique identifier.
* Binding alternative for {@link #setDragDropPayload(String, Object)}, which uses payload class as a unique identifier.
*/
public static boolean setDragDropPayload(final Object payload) {
return setDragDropPayload(payload, ImGuiDragDropFlags.None);
return setDragDropPayload(payload, ImGuiCond.None);
}
/**
* Binding alternative for {@link #setDragDropPayload(String, Object, int)}, but uses payload class as a unique identifier.
* Binding alternative for {@link #setDragDropPayload(String, Object, int)}, which uses payload class as a unique identifier.
*/
public static boolean setDragDropPayload(final Object payload, final int imGuiCond) {
return setDragDropPayload(String.valueOf(payload.getClass().hashCode()), payload, imGuiCond);
@@ -4955,14 +4956,14 @@ public class ImGui {
}
/**
* Binding alternative for {@link #acceptDragDropPayload(String)}, but uses payload class as a unique identifier.
* Binding alternative for {@link #acceptDragDropPayload(String)}, which uses payload class as a unique identifier.
*/
public static <T> T acceptDragDropPayload(final Class<T> aClass) {
return acceptDragDropPayload(String.valueOf(aClass.hashCode()), ImGuiDragDropFlags.None, aClass);
}
/**
* Binding alternative for {@link #acceptDragDropPayload(String, int)}, but uses payload class as a unique identifier.
* Binding alternative for {@link #acceptDragDropPayload(String, int)}, which uses payload class as a unique identifier.
*/
public static <T> T acceptDragDropPayload(final Class<T> aClass, final int imGuiDragDropFlags) {
return acceptDragDropPayload(String.valueOf(aClass.hashCode()), imGuiDragDropFlags, aClass);
@@ -5008,7 +5009,7 @@ public class ImGui {
}
/**
* Binding alternative for {@link #getDragDropPayload(String)}, but uses payload class as a unique identifier.
* Binding alternative for {@link #getDragDropPayload(String)}, which uses payload class as a unique identifier.
*/
public static <T> T getDragDropPayload(final Class<T> aClass) {
return getDragDropPayload(String.valueOf(aClass.hashCode()));