@@ -421,6 +421,14 @@ module DearImGui
421
421
, Raw. setKeyboardFocusHere
422
422
, Raw. setNextItemAllowOverlap
423
423
424
+ -- ** Drag and drop
425
+ , withDragDropSource
426
+ , withDragDropTarget
427
+ , withDragDropSource_
428
+ , withDragDropTarget_
429
+ , withDragDropSourceData
430
+ , withDragDropTargetData
431
+
424
432
-- ** ListClipper
425
433
, withListClipper
426
434
, ClipItems (.. )
@@ -446,7 +454,7 @@ import Control.Monad
446
454
( when )
447
455
import Data.Bool
448
456
import Data.Foldable
449
- ( foldl' )
457
+ ( foldl' , for_ , traverse_ )
450
458
import Foreign
451
459
import Foreign.C
452
460
@@ -456,6 +464,7 @@ import DearImGui.Internal.Text (Text)
456
464
import DearImGui.Structs
457
465
import qualified DearImGui.Internal.Text as Text
458
466
import qualified DearImGui.Raw as Raw
467
+ import qualified DearImGui.Raw.DragDrop as Raw.DragDrop
459
468
import qualified DearImGui.Raw.Font as Raw.Font
460
469
import qualified DearImGui.Raw.ListClipper as Raw.ListClipper
461
470
@@ -471,7 +480,7 @@ import Control.Monad.IO.Class
471
480
( MonadIO , liftIO )
472
481
473
482
-- unliftio
474
- import UnliftIO (MonadUnliftIO )
483
+ import UnliftIO (MonadUnliftIO ( .. ) )
475
484
import UnliftIO.Exception (bracket , bracket_ )
476
485
477
486
-- vector
@@ -2609,6 +2618,86 @@ popStyleVar n = liftIO do
2609
2618
withFont :: MonadUnliftIO m => Raw.Font. Font -> m a -> m a
2610
2619
withFont font = bracket_ (Raw.Font. pushFont font) Raw.Font. popFont
2611
2620
2621
+ -- | Attach drag-n-drop source with a payload to a preceding item.
2622
+ --
2623
+ -- A valid target should have a matching payload type.
2624
+ --
2625
+ -- Data is copied and retained by DearImGui.
2626
+ -- Action is executed when the payload is accepted.
2627
+ withDragDropSource :: (MonadUnliftIO m , Storable a ) => ImGuiDragDropFlags -> Text -> a -> (Bool -> m () ) -> m ()
2628
+ withDragDropSource flags payloadType payload action =
2629
+ withRunInIO \ run ->
2630
+ with payload \ payloadPtr ->
2631
+ run $ withDragDropSourceData flags payloadType (castPtr payloadPtr, Foreign. sizeOf payload) action
2632
+
2633
+ -- | Attach drag-n-drop target to a preceding item.
2634
+ --
2635
+ -- A valid source should have a matching payload type.
2636
+ --
2637
+ -- Data is fetched from DearImGui copy and cleared on delivery.
2638
+ -- Action is executed when the payload is accepted and not empty.
2639
+ withDragDropTarget :: (MonadUnliftIO m , Storable a ) => ImGuiDragDropFlags -> Text -> (a -> m () ) -> m ()
2640
+ withDragDropTarget flags payloadType action =
2641
+ withRunInIO \ run ->
2642
+ Raw.DragDrop. beginTarget >>= flip when do
2643
+ Text. withCString payloadType \ typePtr -> do
2644
+ payload_ <- Raw.DragDrop. acceptPayload typePtr flags
2645
+ for_ payload_ \ payload -> do
2646
+ dataPtr <- Raw.DragDrop. getData payload
2647
+ Foreign. maybePeek peek (castPtr dataPtr) >>= traverse_ (run . action)
2648
+ Raw.DragDrop. endTarget
2649
+
2650
+ -- | Like 'withDragDropSource', but only set payload type.
2651
+ withDragDropSource_ :: (MonadUnliftIO m ) => ImGuiDragDropFlags -> Text -> (Bool -> m () ) -> m ()
2652
+ withDragDropSource_ flags payloadType action =
2653
+ withRunInIO \ run ->
2654
+ Raw.DragDrop. beginSource flags >>= flip when do
2655
+ accepted <-
2656
+ Text. withCString payloadType \ typePtr ->
2657
+ Raw.DragDrop. setPayload typePtr nullPtr 0 ImGuiCond_Once
2658
+ run $ action accepted
2659
+ Raw.DragDrop. endSource
2660
+
2661
+ -- | Like 'withDragDropTarget', but only set payload type.
2662
+ --
2663
+ -- Payload data is ignored.
2664
+ withDragDropTarget_ :: (MonadUnliftIO m ) => ImGuiDragDropFlags -> Text -> m () -> m ()
2665
+ withDragDropTarget_ flags payloadType action =
2666
+ withRunInIO \ run ->
2667
+ Raw.DragDrop. beginTarget >>= flip when do
2668
+ Text. withCString payloadType \ typePtr -> do
2669
+ payload_ <- Raw.DragDrop. acceptPayload typePtr flags
2670
+ for_ payload_ (\ _dataPtr -> run action)
2671
+ Raw.DragDrop. endTarget
2672
+
2673
+ -- | Like 'withDragDropSource', explicitly setting data ptr and size.
2674
+ --
2675
+ -- Suitable for data with dynamic lengths via @withCStringLen@-like functions.
2676
+ withDragDropSourceData :: (MonadUnliftIO m , Integral len ) => ImGuiDragDropFlags -> Text -> (Ptr a , len ) -> (Bool -> m () ) -> m ()
2677
+ withDragDropSourceData flags payloadType (dataPtr, dataSize) action =
2678
+ withRunInIO \ run ->
2679
+ Raw.DragDrop. beginSource flags >>= flip when do
2680
+ accepted <-
2681
+ Text. withCString payloadType \ typePtr ->
2682
+ Raw.DragDrop. setPayload typePtr dataPtr (fromIntegral dataSize) ImGuiCond_Once
2683
+ run $ action accepted
2684
+ Raw.DragDrop. endSource
2685
+
2686
+ -- | Like 'withDragDropTarget', getting raw data ptr and size.
2687
+ --
2688
+ -- Check the size, and pointer for NULLs etc.!
2689
+ withDragDropTargetData :: (MonadUnliftIO m , Integral len ) => ImGuiDragDropFlags -> Text -> ((Ptr a , len ) -> m () ) -> m ()
2690
+ withDragDropTargetData flags payloadType action =
2691
+ withRunInIO \ run ->
2692
+ Raw.DragDrop. beginTarget >>= flip when do
2693
+ Text. withCString payloadType \ typePtr -> do
2694
+ payload_ <- Raw.DragDrop. acceptPayload typePtr flags
2695
+ for_ payload_ \ payload -> do
2696
+ dataPtr <- Raw.DragDrop. getData payload
2697
+ dataSize <- Raw.DragDrop. getDataSize payload
2698
+ run $ action (castPtr dataPtr, fromIntegral dataSize)
2699
+ Raw.DragDrop. endTarget
2700
+
2612
2701
-- | Clips a large list of items
2613
2702
--
2614
2703
-- The requirements on @a@ are that they are all of the same height.
0 commit comments