1
1
package imgui ;
2
2
3
+ import imgui .flag .ImGuiDragDropFlags ;
3
4
import imgui .flag .ImGuiInputTextFlags ;
4
5
import imgui .type .ImBoolean ;
5
6
import imgui .type .ImDouble ;
@@ -4842,8 +4843,8 @@ public static void setNextWindowClass(ImGuiWindowClass windowClass) {
4842
4843
// Drag and Drop
4843
4844
// - 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)
4844
4845
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 ];
4847
4848
4848
4849
/**
4849
4850
* 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) {
4862
4863
/**
4863
4864
* Type is a user defined string of maximum 32 characters. Strings starting with '_' are reserved for dear imgui internal types.
4864
4865
* <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}.
4868
4869
*/
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 );
4871
4872
}
4872
4873
4873
4874
/**
4874
4875
* Type is a user defined string of maximum 32 characters. Strings starting with '_' are reserved for dear imgui internal types.
4875
4876
* <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}.
4879
4880
*/
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 );
4883
4884
}
4884
- return setDragDropPayload ( type , OBJECT_PAYLOAD_PLACEHOLDER_DATA , imGuiCond );
4885
+ return nSetDragDropPayload ( dataType , PAYLOAD_PLACEHOLDER_DATA , 1 , imGuiCond );
4885
4886
}
4886
4887
4887
4888
/**
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.
4890
4890
*/
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 );
4893
4893
}
4894
4894
4895
4895
/**
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.
4898
4897
*/
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 );
4901
4900
}
4902
4901
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);
4905
4904
*/
4906
4905
4907
4906
/**
@@ -4920,49 +4919,57 @@ public static boolean setDragDropPayload(String type, byte[] data, int imGuiCond
4920
4919
4921
4920
/**
4922
4921
* 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)}.
4926
4922
*/
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 );
4929
4932
}
4930
4933
4931
4934
/**
4932
4935
* 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)}.
4936
4936
*/
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 ) ;
4939
4939
}
4940
4940
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
+ }
4944
4956
4945
4957
/**
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 .
4947
4959
*/
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 );
4950
4962
}
4951
4963
4952
4964
/**
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 .
4954
4966
*/
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 );
4957
4969
}
4958
4970
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;
4966
4973
*/
4967
4974
4968
4975
/**
@@ -4973,29 +4980,46 @@ public static byte[] acceptDragDropPayload(String type, int imGuiDragDropFlags)
4973
4980
*/
4974
4981
4975
4982
/**
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.
4980
4984
*/
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 ;
4983
4994
}
4984
4995
4985
- private static native boolean nGetDragDropPayloadObjectObject (); /*
4986
- return ImGui::GetDragDropPayload() != NULL;
4987
- */
4988
-
4989
4996
/**
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 .
4991
4998
*/
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
+ }
4997
5006
}
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);
4999
5023
*/
5000
5024
5001
5025
// Clipping
0 commit comments