Skip to content

Commit 533fc7f

Browse files
committed
[API] Refactor Drag & Drop api
In Java we can easily bind our payload to an object. This is a more flexible approach, than using byte arrays so the last one was removed.
1 parent 86aac35 commit 533fc7f

File tree

1 file changed

+91
-67
lines changed

1 file changed

+91
-67
lines changed

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

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

3+
import imgui.flag.ImGuiDragDropFlags;
34
import imgui.flag.ImGuiInputTextFlags;
45
import imgui.type.ImBoolean;
56
import imgui.type.ImDouble;
@@ -4842,8 +4843,8 @@ public static void setNextWindowClass(ImGuiWindowClass windowClass) {
48424843
// Drag and Drop
48434844
// - If you stop calling BeginDragDropSource() the payload is preserved however it won't have a preview tooltip (we currently display a fallback "..." tooltip as replacement)
48444845

4845-
private static WeakReference<Object> objectPayloadRef = null;
4846-
private static final byte[] OBJECT_PAYLOAD_PLACEHOLDER_DATA = new byte[1];
4846+
private static WeakReference<Object> payloadRef = null;
4847+
private static final byte[] PAYLOAD_PLACEHOLDER_DATA = new byte[1];
48474848

48484849
/**
48494850
* Call when the current item is active. If this return true, you can call SetDragDropPayload() + EndDragDropSource()
@@ -4862,46 +4863,44 @@ public static void setNextWindowClass(ImGuiWindowClass windowClass) {
48624863
/**
48634864
* Type is a user defined string of maximum 32 characters. Strings starting with '_' are reserved for dear imgui internal types.
48644865
* <p>
4865-
* BINDING NOTICE: Alternative for {@link #setDragDropPayload(String, byte[])}.
4866-
* Using this method any Java object can be used for payload.
4867-
* Binding layer stores a reference to the object in a form of {@link WeakReference}.
4866+
* BINDING NOTICE:
4867+
* Method adopted for Java, so objects are used instead of raw bytes.
4868+
* Binding stores a reference to the object in a form of {@link WeakReference}.
48684869
*/
4869-
public static boolean setDragDropPayloadObject(String type, Object payload) {
4870-
return setDragDropPayloadObject(type, payload, 0);
4870+
public static boolean setDragDropPayload(final String dataType, final Object payload) {
4871+
return setDragDropPayload(dataType, payload, ImGuiDragDropFlags.None);
48714872
}
48724873

48734874
/**
48744875
* Type is a user defined string of maximum 32 characters. Strings starting with '_' are reserved for dear imgui internal types.
48754876
* <p>
4876-
* BINDING NOTICE: Alternative for {@link #setDragDropPayload(String, byte[], int)}.
4877-
* Using this method any Java object can be used for payload.
4878-
* Binding layer stores a reference to the object in a form of {@link WeakReference}.
4877+
* BINDING NOTICE:
4878+
* Method adopted for Java, so objects are used instead of raw bytes.
4879+
* Binding stores a reference to the object in a form of {@link WeakReference}.
48794880
*/
4880-
public static boolean setDragDropPayloadObject(String type, Object payload, int imGuiCond) {
4881-
if (objectPayloadRef == null || objectPayloadRef.get() != payload) {
4882-
objectPayloadRef = new WeakReference<>(payload);
4881+
public static boolean setDragDropPayload(final String dataType, final Object payload, final int imGuiCond) {
4882+
if (payloadRef == null || payloadRef.get() != payload) {
4883+
payloadRef = new WeakReference<>(payload);
48834884
}
4884-
return setDragDropPayload(type, OBJECT_PAYLOAD_PLACEHOLDER_DATA, imGuiCond);
4885+
return nSetDragDropPayload(dataType, PAYLOAD_PLACEHOLDER_DATA, 1, imGuiCond);
48854886
}
48864887

48874888
/**
4888-
* Type is a user defined string of maximum 32 characters. Strings starting with '_' are reserved for dear imgui internal types.
4889-
* Data is copied and held by imgui.
4889+
* Binding alternative for {@link #setDragDropPayload(String, Object)}, but uses payload class as a unique identifier.
48904890
*/
4891-
public static boolean setDragDropPayload(String type, byte[] data) {
4892-
return nSetDragDropPayload(type, data, data.length, 0);
4891+
public static boolean setDragDropPayload(final Object payload) {
4892+
return setDragDropPayload(payload, ImGuiDragDropFlags.None);
48934893
}
48944894

48954895
/**
4896-
* Type is a user defined string of maximum 32 characters. Strings starting with '_' are reserved for dear imgui internal types.
4897-
* Data is copied and held by imgui.
4896+
* Binding alternative for {@link #setDragDropPayload(String, Object, int)}, but uses payload class as a unique identifier.
48984897
*/
4899-
public static boolean setDragDropPayload(String type, byte[] data, int imGuiCond) {
4900-
return nSetDragDropPayload(type, data, data.length, imGuiCond);
4898+
public static boolean setDragDropPayload(final Object payload, final int imGuiCond) {
4899+
return setDragDropPayload(String.valueOf(payload.getClass().hashCode()), payload, imGuiCond);
49014900
}
49024901

4903-
private static native boolean nSetDragDropPayload(String type, byte[] data, int sz, int imGuiCond); /*
4904-
return ImGui::SetDragDropPayload(type, &data[0], sz, imGuiCond);
4902+
private static native boolean nSetDragDropPayload(String dataType, byte[] data, int sz, int imGuiCond); /*
4903+
return ImGui::SetDragDropPayload(dataType, &data[0], sz, imGuiCond);
49054904
*/
49064905

49074906
/**
@@ -4920,49 +4919,57 @@ public static boolean setDragDropPayload(String type, byte[] data, int imGuiCond
49204919

49214920
/**
49224921
* Accept contents of a given type. If ImGuiDragDropFlags_AcceptBeforeDelivery is set you can peek into the payload before the mouse button is released.
4923-
* <p>
4924-
* BINDING NOTICE: Alternative for {@link #acceptDragDropPayload(String)}.
4925-
* Use in combination with {@link #setDragDropPayloadObject(String, Object)}.
49264922
*/
4927-
public static Object acceptDragDropPayloadObject(String type) {
4928-
return acceptDragDropPayloadObject(type, 0);
4923+
public static <T> T acceptDragDropPayload(final String dataType) {
4924+
return acceptDragDropPayload(dataType, ImGuiDragDropFlags.None);
4925+
}
4926+
4927+
/**
4928+
* Type safe alternative for {@link #acceptDragDropPayload(String)}, since it checks assignability of the accepted class.
4929+
*/
4930+
public static <T> T acceptDragDropPayload(final String dataType, final Class<T> aClass) {
4931+
return acceptDragDropPayload(dataType, ImGuiDragDropFlags.None, aClass);
49294932
}
49304933

49314934
/**
49324935
* Accept contents of a given type. If ImGuiDragDropFlags_AcceptBeforeDelivery is set you can peek into the payload before the mouse button is released.
4933-
* <p>
4934-
* BINDING NOTICE: Alternative for {@link #acceptDragDropPayload(String, int)}.
4935-
* Use in combination with {@link #setDragDropPayloadObject(String, Object)}.
49364936
*/
4937-
public static Object acceptDragDropPayloadObject(String type, int imGuiDragDropFlags) {
4938-
return nAcceptDragDropPayloadObject(type, imGuiDragDropFlags) ? objectPayloadRef.get() : null;
4937+
public static <T> T acceptDragDropPayload(final String dataType, final int imGuiDragDropFlags) {
4938+
return acceptDragDropPayload(dataType, imGuiDragDropFlags, null);
49394939
}
49404940

4941-
private static native boolean nAcceptDragDropPayloadObject(String type, int imGuiDragDropFlags); /*
4942-
return ImGui::AcceptDragDropPayload(type, imGuiDragDropFlags) != NULL;
4943-
*/
4941+
/**
4942+
* Type safe alternative for {@link #acceptDragDropPayload(String, int)}, since it checks assignability of the accepted class.
4943+
*/
4944+
@SuppressWarnings("unchecked")
4945+
public static <T> T acceptDragDropPayload(final String dataType, final int imGuiDragDropFlags, final Class<T> aClass) {
4946+
if (payloadRef != null && nAcceptDragDropPayload(dataType, imGuiDragDropFlags)) {
4947+
final Object rawPayload = payloadRef.get();
4948+
if (rawPayload != null) {
4949+
if (aClass == null || rawPayload.getClass().isAssignableFrom(aClass)) {
4950+
return (T) rawPayload;
4951+
}
4952+
}
4953+
}
4954+
return null;
4955+
}
49444956

49454957
/**
4946-
* Accept contents of a given type. If ImGuiDragDropFlags_AcceptBeforeDelivery is set you can peek into the payload before the mouse button is released.
4958+
* Binding alternative for {@link #acceptDragDropPayload(String)}, but uses payload class as a unique identifier.
49474959
*/
4948-
public static byte[] acceptDragDropPayload(String type) {
4949-
return nAcceptDragDropPayload(type, 0);
4960+
public static <T> T acceptDragDropPayload(final Class<T> aClass) {
4961+
return acceptDragDropPayload(String.valueOf(aClass.hashCode()), ImGuiDragDropFlags.None, aClass);
49504962
}
49514963

49524964
/**
4953-
* Accept contents of a given type. If ImGuiDragDropFlags_AcceptBeforeDelivery is set you can peek into the payload before the mouse button is released.
4965+
* Binding alternative for {@link #acceptDragDropPayload(String, int)}, but uses payload class as a unique identifier.
49544966
*/
4955-
public static byte[] acceptDragDropPayload(String type, int imGuiDragDropFlags) {
4956-
return nAcceptDragDropPayload(type, imGuiDragDropFlags);
4967+
public static <T> T acceptDragDropPayload(final Class<T> aClass, final int imGuiDragDropFlags) {
4968+
return acceptDragDropPayload(String.valueOf(aClass.hashCode()), imGuiDragDropFlags, aClass);
49574969
}
49584970

4959-
private static native byte[] nAcceptDragDropPayload(String type, int imGuiDragDropFlags); /*
4960-
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(type, imGuiDragDropFlags)) {
4961-
jbyteArray array = env->NewByteArray(payload->DataSize);
4962-
env->SetByteArrayRegion(array, 0, payload->DataSize, (jbyte*)payload->Data);
4963-
return array;
4964-
}
4965-
return NULL;
4971+
private static native boolean nAcceptDragDropPayload(String dataType, int imGuiDragDropFlags); /*
4972+
return ImGui::AcceptDragDropPayload(dataType, imGuiDragDropFlags) != NULL;
49664973
*/
49674974

49684975
/**
@@ -4973,29 +4980,46 @@ public static byte[] acceptDragDropPayload(String type, int imGuiDragDropFlags)
49734980
*/
49744981

49754982
/**
4976-
* Peek directly into the current payload from anywhere. May return NULL. use ImGuiPayload::IsDataType() to test for the payload type.
4977-
* <p>
4978-
* BINDING NOTICE: Binding alternative for {@link #getDragDropPayload()}.
4979-
* Use in combination with {@link #setDragDropPayloadObject(String, Object)}.
4983+
* Peek directly into the current payload from anywhere. May return NULL.
49804984
*/
4981-
public static Object getDragDropPayloadObject() {
4982-
return objectPayloadRef != null && nGetDragDropPayloadObjectObject() ? objectPayloadRef.get() : null;
4985+
@SuppressWarnings("unchecked")
4986+
public static <T> T getDragDropPayload() {
4987+
if (payloadRef != null && nHasDragDropPayload()) {
4988+
final Object rawPayload = payloadRef.get();
4989+
if (rawPayload != null) {
4990+
return (T) rawPayload;
4991+
}
4992+
}
4993+
return null;
49834994
}
49844995

4985-
private static native boolean nGetDragDropPayloadObjectObject(); /*
4986-
return ImGui::GetDragDropPayload() != NULL;
4987-
*/
4988-
49894996
/**
4990-
* Peek directly into the current payload from anywhere. May return NULL. use ImGuiPayload::IsDataType() to test for the payload type.
4997+
* Peek directly into the current payload from anywhere. May return NULL. Checks if payload has the same type as provided.
49914998
*/
4992-
public static native byte[] getDragDropPayload(); /*
4993-
if (const ImGuiPayload* payload = ImGui::GetDragDropPayload()) {
4994-
jbyteArray array = env->NewByteArray(payload->DataSize);
4995-
env->SetByteArrayRegion(array, 0, payload->DataSize, (jbyte*)payload->Data);
4996-
return array;
4999+
@SuppressWarnings("unchecked")
5000+
public static <T> T getDragDropPayload(final String dataType) {
5001+
if (payloadRef != null && nHasDragDropPayload(dataType)) {
5002+
final Object rawPayload = payloadRef.get();
5003+
if (rawPayload != null) {
5004+
return (T) rawPayload;
5005+
}
49975006
}
4998-
return NULL;
5007+
return null;
5008+
}
5009+
5010+
/**
5011+
* Binding alternative for {@link #getDragDropPayload(String)}, but uses payload class as a unique identifier.
5012+
*/
5013+
public static <T> T getDragDropPayload(final Class<T> aClass) {
5014+
return getDragDropPayload(String.valueOf(aClass.hashCode()));
5015+
}
5016+
5017+
private static native boolean nHasDragDropPayload(); /*
5018+
return ImGui::GetDragDropPayload()->Data != NULL;
5019+
*/
5020+
5021+
private static native boolean nHasDragDropPayload(String dataType); /*
5022+
return ImGui::GetDragDropPayload()->IsDataType(dataType);
49995023
*/
50005024

50015025
// Clipping

0 commit comments

Comments
 (0)