From 49f7bb245e808bca41066caa45ecae8859684d8d Mon Sep 17 00:00:00 2001 From: Jason Shipman Date: Tue, 20 Feb 2024 14:16:37 -0500 Subject: [PATCH] Add support for dispatching raw SDL events to Dear ImGui (#195) --- src/DearImGui/SDL.hs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/DearImGui/SDL.hs b/src/DearImGui/SDL.hs index 9e91041..b92f5da 100644 --- a/src/DearImGui/SDL.hs +++ b/src/DearImGui/SDL.hs @@ -21,12 +21,14 @@ module DearImGui.SDL ( , sdl2Shutdown , pollEventWithImGui , pollEventsWithImGui + -- *** Raw + , dispatchRawEvent ) where -- base import Control.Monad - ( when ) + ( void, when ) import Foreign.Marshal.Alloc ( alloca ) import Foreign.Ptr @@ -42,6 +44,7 @@ import qualified Language.C.Inline.Cpp as Cpp import SDL import SDL.Raw.Enum as Raw import qualified SDL.Raw.Event as Raw +import qualified SDL.Raw.Types as Raw -- transformers import Control.Monad.IO.Class @@ -77,11 +80,24 @@ pollEventWithImGui = liftIO do nEvents <- Raw.peepEvents evPtr 1 Raw.SDL_PEEKEVENT Raw.SDL_FIRSTEVENT Raw.SDL_LASTEVENT when (nEvents > 0) do - let evPtr' = castPtr evPtr :: Ptr () - [C.exp| void { ImGui_ImplSDL2_ProcessEvent((SDL_Event*) $(void* evPtr')) } |] + void $ dispatchRawEvent evPtr pollEvent +-- | Dispatch a raw 'Raw.Event' value to Dear ImGui. +-- +-- You may want this function instead of 'pollEventWithImGui' if you do not use +-- @sdl2@'s higher-level 'Event' type (e.g. your application has its own polling +-- mechanism). +-- +-- __It is your application's responsibility to both manage the input__ +-- __pointer's memory and to fill the memory location with a raw 'Raw.Event'__ +-- __value.__ +dispatchRawEvent :: MonadIO m => Ptr Raw.Event -> m Bool +dispatchRawEvent evPtr = liftIO do + let evPtr' = castPtr evPtr :: Ptr () + (0 /=) <$> [C.exp| bool { ImGui_ImplSDL2_ProcessEvent((const SDL_Event*) $(void* evPtr')) } |] + -- | Like the SDL2 'pollEvents' function, while also dispatching the events to -- Dear ImGui. See 'pollEventWithImGui'. pollEventsWithImGui :: MonadIO m => m [Event]