Add combo to wrap ImGUI::Combo() (#28)

Co-authored-by: Oliver Charles <ollie@ocharles.org.uk>
This commit is contained in:
Shaun Sharples 2021-02-05 23:20:32 +02:00 committed by GitHub
parent 895f5c1926
commit 643d2ea5b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 4 deletions

18
Main.hs
View File

@ -30,13 +30,21 @@ main = do
color <- newIORef $ ImVec3 1 0 0
slider <- newIORef (0.42, 0, 0.314)
r <- newIORef 4
loop w checked color slider r
selected <- newIORef 4
loop w checked color slider r selected
openGL2Shutdown
loop :: Window -> IORef Bool -> IORef ImVec3 -> IORef (Float, Float, Float) -> IORef Int -> IO ()
loop w checked color slider r = do
loop
:: Window
-> IORef Bool
-> IORef ImVec3
-> IORef (Float, Float, Float)
-> IORef Int
-> IORef Int
-> IO ()
loop w checked color slider r selected = do
quit <- pollEvents
openGL2NewFrame
@ -89,6 +97,8 @@ loop w checked color slider r = do
selectable "Testing 2"
endCombo
combo "Simple" selected [ "1", "2", "3" ]
endChild
plotHistogram "A histogram" [ 10, 10, 20, 30, 90 ]
@ -124,7 +134,7 @@ loop w checked color slider r = do
glSwapWindow w
if quit then return () else loop w checked color slider r
if quit then return () else loop w checked color slider r selected
where

View File

@ -67,6 +67,7 @@ module DearImGui
-- ** Combo Box
, beginCombo
, endCombo
, combo
-- ** Drag Sliders
, dragFloat
@ -416,6 +417,27 @@ endCombo = liftIO do
[C.exp| void { EndCombo() } |]
-- Wraps @ImGui::Combo()@.
combo :: (MonadIO m, HasGetter ref Int, HasSetter ref Int) => String -> ref -> [String] -> m Bool
combo label selectedIndex items = liftIO $ Managed.with m return
where
m = do
i <- get selectedIndex
cStrings <- traverse (\str -> Managed.managed (withCString str)) items
labelPtr <- Managed.managed $ withCString label
iPtr <- Managed.managed $ with (fromIntegral i)
liftIO $ withArrayLen cStrings \len itemsPtr -> do
let len' = fromIntegral len
[C.exp| bool { Combo($(char* labelPtr), $(int* iPtr), $(char** itemsPtr), $(int len')) }|] >>= \case
0 -> return False
_ -> do
i' <- peek iPtr
selectedIndex $=! fromIntegral i'
return True
-- | Wraps @ImGui::DragFloat()@
dragFloat :: (MonadIO m, HasSetter ref Float, HasGetter ref Float) => String -> ref -> Float -> Float -> Float -> m Bool
dragFloat desc ref speed minValue maxValue = liftIO do