Skip to content

Commit 49f7bb2

Browse files
authored
Add support for dispatching raw SDL events to Dear ImGui (#195)
1 parent 47402c1 commit 49f7bb2

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/DearImGui/SDL.hs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ module DearImGui.SDL (
2121
, sdl2Shutdown
2222
, pollEventWithImGui
2323
, pollEventsWithImGui
24+
-- *** Raw
25+
, dispatchRawEvent
2426
)
2527
where
2628

2729
-- base
2830
import Control.Monad
29-
( when )
31+
( void, when )
3032
import Foreign.Marshal.Alloc
3133
( alloca )
3234
import Foreign.Ptr
@@ -42,6 +44,7 @@ import qualified Language.C.Inline.Cpp as Cpp
4244
import SDL
4345
import SDL.Raw.Enum as Raw
4446
import qualified SDL.Raw.Event as Raw
47+
import qualified SDL.Raw.Types as Raw
4548

4649
-- transformers
4750
import Control.Monad.IO.Class
@@ -77,11 +80,24 @@ pollEventWithImGui = liftIO do
7780
nEvents <- Raw.peepEvents evPtr 1 Raw.SDL_PEEKEVENT Raw.SDL_FIRSTEVENT Raw.SDL_LASTEVENT
7881

7982
when (nEvents > 0) do
80-
let evPtr' = castPtr evPtr :: Ptr ()
81-
[C.exp| void { ImGui_ImplSDL2_ProcessEvent((SDL_Event*) $(void* evPtr')) } |]
83+
void $ dispatchRawEvent evPtr
8284

8385
pollEvent
8486

87+
-- | Dispatch a raw 'Raw.Event' value to Dear ImGui.
88+
--
89+
-- You may want this function instead of 'pollEventWithImGui' if you do not use
90+
-- @sdl2@'s higher-level 'Event' type (e.g. your application has its own polling
91+
-- mechanism).
92+
--
93+
-- __It is your application's responsibility to both manage the input__
94+
-- __pointer's memory and to fill the memory location with a raw 'Raw.Event'__
95+
-- __value.__
96+
dispatchRawEvent :: MonadIO m => Ptr Raw.Event -> m Bool
97+
dispatchRawEvent evPtr = liftIO do
98+
let evPtr' = castPtr evPtr :: Ptr ()
99+
(0 /=) <$> [C.exp| bool { ImGui_ImplSDL2_ProcessEvent((const SDL_Event*) $(void* evPtr')) } |]
100+
85101
-- | Like the SDL2 'pollEvents' function, while also dispatching the events to
86102
-- Dear ImGui. See 'pollEventWithImGui'.
87103
pollEventsWithImGui :: MonadIO m => m [Event]

0 commit comments

Comments
 (0)