Skip to content

Commit 3fe7732

Browse files
committed
[Example] Add example of Drag & Drop
1 parent 533fc7f commit 3fe7732

File tree

3 files changed

+142
-7
lines changed

3 files changed

+142
-7
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import imgui.ImGui;
2+
import imgui.flag.ImGuiDragDropFlags;
3+
import imgui.flag.ImGuiInputTextFlags;
4+
import imgui.flag.ImGuiWindowFlags;
5+
import imgui.type.ImBoolean;
6+
import imgui.type.ImInt;
7+
import imgui.type.ImString;
8+
9+
import java.util.concurrent.atomic.AtomicInteger;
10+
11+
public class ExampleDragAndDrop {
12+
private static final String PAYLOAD_TYPE = "PAYLOAD";
13+
14+
private static final StringPayload STRING_PAYLOAD = new StringPayload();
15+
private static final IntegerPayload INTEGER_PAYLOAD = new IntegerPayload();
16+
17+
private static final AtomicInteger CLASS_SPECIFIC_PAYLOAD = new AtomicInteger(23);
18+
19+
private static String data = "No Data...";
20+
21+
public static void show(final ImBoolean showDragNDropWindow) {
22+
if (ImGui.begin("Drag'N'Drop Demo", showDragNDropWindow, ImGuiWindowFlags.AlwaysAutoResize)) {
23+
ImGui.text("Drag from here:");
24+
25+
ImGui.inputText("String payload", STRING_PAYLOAD.input, ImGuiInputTextFlags.CallbackResize);
26+
setupPayload(STRING_PAYLOAD);
27+
28+
ImGui.inputInt("Integer payload", INTEGER_PAYLOAD.input);
29+
setupPayload(INTEGER_PAYLOAD);
30+
31+
ImGui.button("Class specific payload");
32+
setupClassSpecificPayload();
33+
34+
ImGui.separator();
35+
36+
ImGui.text("Drop here any:");
37+
ImGui.button(data, 100, 50);
38+
setupTarget();
39+
40+
ImGui.separator();
41+
42+
ImGui.text("Drop here string payload only");
43+
ImGui.button(data, 100, 50);
44+
setupStringPayloadTarget();
45+
46+
ImGui.separator();
47+
48+
ImGui.text("Drop here class specific payload only");
49+
ImGui.button(data, 100, 50);
50+
setupClassSpecificPayloadTarget();
51+
}
52+
ImGui.end();
53+
}
54+
55+
private static void setupPayload(final Payload<?> payload) {
56+
if (ImGui.beginDragDropSource(ImGuiDragDropFlags.SourceAllowNullID)) {
57+
ImGui.setDragDropPayload(PAYLOAD_TYPE, payload);
58+
ImGui.text("Dragging: " + payload.getData());
59+
ImGui.endDragDropSource();
60+
}
61+
}
62+
63+
private static void setupClassSpecificPayload() {
64+
if (ImGui.beginDragDropSource()) {
65+
ImGui.setDragDropPayload(CLASS_SPECIFIC_PAYLOAD);
66+
ImGui.text("Dragging class specific payload");
67+
ImGui.endDragDropSource();
68+
}
69+
}
70+
71+
private static void setupTarget() {
72+
if (ImGui.beginDragDropTarget()) {
73+
Payload<?> payload = ImGui.acceptDragDropPayload(PAYLOAD_TYPE);
74+
if (payload != null) {
75+
data = String.valueOf(payload.getData());
76+
}
77+
ImGui.endDragDropTarget();
78+
}
79+
}
80+
81+
/**
82+
* Type safe example. ImGui will show that it can accept payload, but payload itself will be null.
83+
*/
84+
private static void setupStringPayloadTarget() {
85+
if (ImGui.beginDragDropTarget()) {
86+
StringPayload payload = ImGui.acceptDragDropPayload(PAYLOAD_TYPE, StringPayload.class);
87+
if (payload != null) {
88+
data = payload.getData();
89+
}
90+
ImGui.endDragDropTarget();
91+
}
92+
}
93+
94+
/**
95+
* Class specific example. We can bind our payload to a specific class, so it will be 100% type safe.
96+
*/
97+
private static void setupClassSpecificPayloadTarget() {
98+
if (ImGui.beginDragDropTarget()) {
99+
AtomicInteger payload = ImGui.acceptDragDropPayload(AtomicInteger.class);
100+
if (payload != null) {
101+
data = String.valueOf(payload.get());
102+
}
103+
ImGui.endDragDropTarget();
104+
}
105+
}
106+
107+
private interface Payload<T> {
108+
T getData();
109+
}
110+
111+
private static final class StringPayload implements Payload<String> {
112+
ImString input = new ImString("You can drag inputs as well");
113+
114+
@Override
115+
public String getData() {
116+
return input.get();
117+
}
118+
}
119+
120+
private static final class IntegerPayload implements Payload<Integer> {
121+
ImInt input = new ImInt();
122+
123+
@Override
124+
public Integer getData() {
125+
return input.get();
126+
}
127+
}
128+
}

example/src/main/java/Extra.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class Extra {
66
private static final ImBoolean SHOW_DEMO_WINDOW = new ImBoolean(false);
77
private static final ImBoolean SHOW_IMNODES_DEMO_WINDOW = new ImBoolean(false);
88
private static final ImBoolean SHOW_IMGUI_NODE_EDITOR_DEMO_WINDOW = new ImBoolean(false);
9+
private static final ImBoolean SHOW_DRAG_N_DROP_WINDOW = new ImBoolean(false);
910

1011
private static final Graph GRAPH = new Graph();
1112

@@ -14,6 +15,7 @@ public static void show(final Application app) {
1415
ImGui.checkbox("Show Demo Window", SHOW_DEMO_WINDOW);
1516
ImGui.checkbox("Show ImNodes Demo Window", SHOW_IMNODES_DEMO_WINDOW);
1617
ImGui.checkbox("Show imgui-node-editor Demo Window", SHOW_IMGUI_NODE_EDITOR_DEMO_WINDOW);
18+
ImGui.checkbox("Show Drag'N'Drop Demo Window", SHOW_DRAG_N_DROP_WINDOW);
1719

1820
if (SHOW_DEMO_WINDOW.get()) {
1921
ImGui.showDemoWindow(SHOW_DEMO_WINDOW);
@@ -26,5 +28,9 @@ public static void show(final Application app) {
2628
if (SHOW_IMGUI_NODE_EDITOR_DEMO_WINDOW.get()) {
2729
ExampleImGuiNodeEditor.show(SHOW_IMGUI_NODE_EDITOR_DEMO_WINDOW, GRAPH);
2830
}
31+
32+
if (SHOW_DRAG_N_DROP_WINDOW.get()) {
33+
ExampleDragAndDrop.show(SHOW_DRAG_N_DROP_WINDOW);
34+
}
2935
}
3036
}

imgui-binding/src/main/java/imgui/ImGui.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package imgui;
22

3+
import imgui.flag.ImGuiCond;
34
import imgui.flag.ImGuiDragDropFlags;
45
import imgui.flag.ImGuiInputTextFlags;
56
import imgui.type.ImBoolean;
@@ -4868,7 +4869,7 @@ public static void setNextWindowClass(ImGuiWindowClass windowClass) {
48684869
* Binding stores a reference to the object in a form of {@link WeakReference}.
48694870
*/
48704871
public static boolean setDragDropPayload(final String dataType, final Object payload) {
4871-
return setDragDropPayload(dataType, payload, ImGuiDragDropFlags.None);
4872+
return setDragDropPayload(dataType, payload, ImGuiCond.None);
48724873
}
48734874

48744875
/**
@@ -4886,14 +4887,14 @@ public static boolean setDragDropPayload(final String dataType, final Object pay
48864887
}
48874888

48884889
/**
4889-
* Binding alternative for {@link #setDragDropPayload(String, Object)}, but uses payload class as a unique identifier.
4890+
* Binding alternative for {@link #setDragDropPayload(String, Object)}, which uses payload class as a unique identifier.
48904891
*/
48914892
public static boolean setDragDropPayload(final Object payload) {
4892-
return setDragDropPayload(payload, ImGuiDragDropFlags.None);
4893+
return setDragDropPayload(payload, ImGuiCond.None);
48934894
}
48944895

48954896
/**
4896-
* Binding alternative for {@link #setDragDropPayload(String, Object, int)}, but uses payload class as a unique identifier.
4897+
* Binding alternative for {@link #setDragDropPayload(String, Object, int)}, which uses payload class as a unique identifier.
48974898
*/
48984899
public static boolean setDragDropPayload(final Object payload, final int imGuiCond) {
48994900
return setDragDropPayload(String.valueOf(payload.getClass().hashCode()), payload, imGuiCond);
@@ -4955,14 +4956,14 @@ public static <T> T acceptDragDropPayload(final String dataType, final int imGui
49554956
}
49564957

49574958
/**
4958-
* Binding alternative for {@link #acceptDragDropPayload(String)}, but uses payload class as a unique identifier.
4959+
* Binding alternative for {@link #acceptDragDropPayload(String)}, which uses payload class as a unique identifier.
49594960
*/
49604961
public static <T> T acceptDragDropPayload(final Class<T> aClass) {
49614962
return acceptDragDropPayload(String.valueOf(aClass.hashCode()), ImGuiDragDropFlags.None, aClass);
49624963
}
49634964

49644965
/**
4965-
* Binding alternative for {@link #acceptDragDropPayload(String, int)}, but uses payload class as a unique identifier.
4966+
* Binding alternative for {@link #acceptDragDropPayload(String, int)}, which uses payload class as a unique identifier.
49664967
*/
49674968
public static <T> T acceptDragDropPayload(final Class<T> aClass, final int imGuiDragDropFlags) {
49684969
return acceptDragDropPayload(String.valueOf(aClass.hashCode()), imGuiDragDropFlags, aClass);
@@ -5008,7 +5009,7 @@ public static <T> T getDragDropPayload(final String dataType) {
50085009
}
50095010

50105011
/**
5011-
* Binding alternative for {@link #getDragDropPayload(String)}, but uses payload class as a unique identifier.
5012+
* Binding alternative for {@link #getDragDropPayload(String)}, which uses payload class as a unique identifier.
50125013
*/
50135014
public static <T> T getDragDropPayload(final Class<T> aClass) {
50145015
return getDragDropPayload(String.valueOf(aClass.hashCode()));

0 commit comments

Comments
 (0)