Skip to content

Commit 90d96ad

Browse files
committed
Wrap ImGui::Checkbox()
1 parent 0fb690d commit 90d96ad

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

Main.hs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
module Main (main) where
77

8+
import Data.IORef
89
import DearImGui
910
import Control.Exception
1011
import Graphics.GL
@@ -23,12 +24,12 @@ main = do
2324
styleColorsLight
2425
openGL2Init
2526

26-
loop w
27+
newIORef False >>= loop w
2728

2829
openGL2Shutdown
2930

30-
loop :: Window -> IO ()
31-
loop w = do
31+
loop :: Window -> IORef Bool -> IO ()
32+
loop w checked = do
3233
ev <- pollEventWithImGui
3334

3435
openGL2NewFrame
@@ -53,6 +54,10 @@ loop w = do
5354

5455
arrowButton "Arrow" ImGuiDirUp
5556

57+
checkbox "Check!" checked >>= \case
58+
True -> readIORef checked >>= print
59+
False -> return ()
60+
5661
end
5762

5863
render
@@ -63,7 +68,7 @@ loop w = do
6368
glSwapWindow w
6469

6570
case ev of
66-
Nothing -> loop w
71+
Nothing -> loop w checked
6772
Just Event{ eventPayload } -> case eventPayload of
6873
QuitEvent -> return ()
69-
_ -> loop w
74+
_ -> loop w checked

hs-dear-imgui.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ library
2020
extra-libraries: stdc++
2121
pkgconfig-depends: sdl2
2222
include-dirs: imgui
23-
build-depends: base, inline-c, inline-c-cpp, sdl2
23+
build-depends: base, inline-c, inline-c-cpp, sdl2, StateVar
2424
extra-libraries: GL
2525

2626

src/DearImGui.hs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE BlockArguments #-}
2+
{-# LANGUAGE FlexibleContexts #-}
23
{-# LANGUAGE NamedFieldPuns #-}
34
{-# LANGUAGE OverloadedStrings #-}
45
{-# LANGUAGE PatternSynonyms #-}
@@ -55,6 +56,7 @@ module DearImGui
5556
, button
5657
, smallButton
5758
, arrowButton
59+
, checkbox
5860

5961
-- * Types
6062
, ImGuiDir
@@ -65,9 +67,11 @@ module DearImGui
6567
)
6668
where
6769

70+
import Data.Bool
71+
import Data.StateVar
6872
import Control.Monad ( when )
6973
import Foreign
70-
import Foreign.C.String
74+
import Foreign.C
7175
import qualified Language.C.Inline as C
7276
import qualified Language.C.Inline.Cpp as Cpp
7377
import SDL
@@ -297,6 +301,20 @@ arrowButton strId (ImGuiDir dir) = withCString strId \strIdPtr ->
297301
(1 ==) <$> [C.exp| bool { ArrowButton($(char* strIdPtr), $(int dir)) } |]
298302

299303

304+
-- | Wraps @ImGui::Checkbox()@.
305+
checkbox :: (HasSetter ref Bool, HasGetter ref Bool) => String -> ref -> IO Bool
306+
checkbox label ref = do
307+
currentValue <- get ref
308+
with (bool 0 1 currentValue :: CBool) \boolPtr -> do
309+
changed <- withCString label \labelPtr ->
310+
(1 ==) <$> [C.exp| bool { Checkbox($(char* labelPtr), $(bool* boolPtr)) } |]
311+
312+
newValue <- peek boolPtr
313+
ref $=! (newValue == 1)
314+
315+
return changed
316+
317+
300318
-- | A cardinal direction.
301319
newtype ImGuiDir = ImGuiDir CInt
302320

0 commit comments

Comments
 (0)