Skip to content

Commit

Permalink
docs: Update item-callbacks.rst - drag and drop callback (#2430)
Browse files Browse the repository at this point in the history
* docs: Update item-callbacks.rst - drag and drop callback

Add docs for drag and drop callback (and drag payload)

* docs: Update item-callbacks.rst - drag and drop callback

Add docs for drag and drop callback (and drag payload)

* Update item-callbacks.rst - payload_type being optional

Modify a point about payload_type being optional.
  • Loading branch information
nvglucifer authored Dec 13, 2024
1 parent 65c2620 commit 27dd00c
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions docs/source/documentation/item-callbacks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,58 @@ User data can be any python object.
dpg.start_dearpygui()
dpg.destroy_context()
Drag and Drop callback (and drag payload)
-----------------------------------------

Drag/Drop callback receive its **app_data** from drag_payload's **drag_data**.

**user_data** (optional) can be specified in both **drag payload** and **target drop item** for further use in Drag/Drop callback.

**payload_type** (optional) can be any string you like. If not specified, **payload_type**'s default value is '$$DPG_PAYLOAD'.

For drop callback to work, **payload_type** must be specified the same in both **drag payload** and **target drop item**.

.. code-block:: python
import dearpygui.dearpygui as dpg
dpg.create_context()
def drag_cb(sender, app_data, user_data):
# sender is btn_drag
# app_data is btn_drag (value from drag_data)
# do some configure(drawing_item), animation
...
def drop_cb(sender, app_data, user_data):
# sender is group, app_data is btn_drag
dpg.move_item(app_data, parent=sender)
with dpg.window():
with dpg.group(horizontal=True):
with dpg.group(width=300, drop_callback=drop_cb, payload_type="int"): # user_data=??
dpg.add_text("Group left")
dpg.add_button(label="not drag this")
with dpg.group(width=300, drop_callback=drop_cb, payload_type="int"):
dpg.add_text("Group right")
dpg.add_button(label="not drag this")
btn_drag = dpg.add_button(label="drag me to another group then drop", drag_callback=drag_cb)
with dpg.drag_payload(parent=btn_drag, drag_data=btn_drag, payload_type="int"):
dpg.add_text("dragging a button")
# parent=btn_drag --> this playload will appear if dragged from the btn_drag
# drag_data=btn_drag --> btn_drag will be app_data in the above drag_cb and drop_cb
# payload_type="int" --> btn_drag is an int, specified in this playload and drop target - two group above
dpg.create_viewport()
dpg.setup_dearpygui()
dpg.show_viewport()
while dpg.is_dearpygui_running():
dpg.render_dearpygui_frame()
dpg.destroy_context()
Debugging Callbacks (new in 1.2)
--------------------------------

Expand Down

0 comments on commit 27dd00c

Please sign in to comment.