forked from SpaiR/imgui-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleDragAndDrop.java
More file actions
134 lines (109 loc) · 4.22 KB
/
ExampleDragAndDrop.java
File metadata and controls
134 lines (109 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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.pushID("any");
ImGui.button(data, 100, 50);
setupTarget();
ImGui.popID();
ImGui.separator();
ImGui.text("Drop here string payload only");
ImGui.pushID("string");
ImGui.button(data, 100, 50);
setupStringPayloadTarget();
ImGui.popID();
ImGui.separator();
ImGui.text("Drop here class specific payload only");
ImGui.pushID("class");
ImGui.button(data, 100, 50);
setupClassSpecificPayloadTarget();
ImGui.popID();
}
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();
}
}
}