Quellcode durchsuchen

add demo about draggable objects

Falko Schindler vor 1 Jahr
Ursprung
Commit
0d8970d510
1 geänderte Dateien mit 21 neuen und 0 gelöschten Zeilen
  1. 21 0
      website/more_documentation/scene_documentation.py

+ 21 - 0
website/more_documentation/scene_documentation.py

@@ -55,3 +55,24 @@ def more() -> None:
         with ui.scene(width=285, height=220, on_click=handle_click) as scene:
             scene.sphere().move(x=-1, z=1).with_name('sphere')
             scene.box().move(x=1, z=1).with_name('box')
+
+    @text_demo('Draggable objects', '''
+        You can make objects draggable using the `.draggable` method.
+        There is an optional `on_drag_start` and `on_drag_end` argument to `ui.scene` to handle drag events.
+        The callbacks receive a `SceneDragEventArguments` object with the following attributes:
+        
+        - `type`: the type of drag event ("dragstart" or "dragend").
+        - `object_id`: the id of the object that was dragged.
+        - `object_name`: the name of the object that was dragged.
+        - `x`, `y`, `z`: the x, y and z coordinates of the dragged object.
+    ''')
+    def draggable_objects() -> None:
+        from nicegui import events
+
+        def handle_drag(e: events.SceneDragEventArguments):
+            ui.notify(f'You dropped the sphere at ({e.x:.2f}, {e.y:.2f}, {e.z:.2f})')
+
+        with ui.scene(width=285, height=220, on_drag_end=handle_drag) as scene:
+            sphere = scene.sphere()
+
+        ui.switch('draggable sphere', on_change=lambda e: sphere.draggable(e.value))