diff --git a/Main.hs b/Main.hs index fe81714..305adb0 100644 --- a/Main.hs +++ b/Main.hs @@ -5,6 +5,7 @@ module Main (main) where +import Data.IORef import DearImGui import Control.Exception import Graphics.GL @@ -23,12 +24,12 @@ main = do styleColorsLight openGL2Init - loop w + newIORef False >>= loop w openGL2Shutdown -loop :: Window -> IO () -loop w = do +loop :: Window -> IORef Bool -> IO () +loop w checked = do ev <- pollEventWithImGui openGL2NewFrame @@ -53,6 +54,10 @@ loop w = do arrowButton "Arrow" ImGuiDirUp + checkbox "Check!" checked >>= \case + True -> readIORef checked >>= print + False -> return () + end render @@ -63,7 +68,7 @@ loop w = do glSwapWindow w case ev of - Nothing -> loop w + Nothing -> loop w checked Just Event{ eventPayload } -> case eventPayload of QuitEvent -> return () - _ -> loop w + _ -> loop w checked diff --git a/hs-dear-imgui.cabal b/hs-dear-imgui.cabal index 27f4b32..a6ce3b3 100644 --- a/hs-dear-imgui.cabal +++ b/hs-dear-imgui.cabal @@ -20,7 +20,7 @@ library extra-libraries: stdc++ pkgconfig-depends: sdl2 include-dirs: imgui - build-depends: base, inline-c, inline-c-cpp, sdl2 + build-depends: base, inline-c, inline-c-cpp, sdl2, StateVar extra-libraries: GL diff --git a/src/DearImGui.hs b/src/DearImGui.hs index 84a1e23..1c5e202 100644 --- a/src/DearImGui.hs +++ b/src/DearImGui.hs @@ -1,4 +1,5 @@ {-# LANGUAGE BlockArguments #-} +{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE PatternSynonyms #-} @@ -55,6 +56,7 @@ module DearImGui , button , smallButton , arrowButton + , checkbox -- * Types , ImGuiDir @@ -65,9 +67,11 @@ module DearImGui ) where +import Data.Bool +import Data.StateVar import Control.Monad ( when ) import Foreign -import Foreign.C.String +import Foreign.C import qualified Language.C.Inline as C import qualified Language.C.Inline.Cpp as Cpp import SDL @@ -297,6 +301,20 @@ arrowButton strId (ImGuiDir dir) = withCString strId \strIdPtr -> (1 ==) <$> [C.exp| bool { ArrowButton($(char* strIdPtr), $(int dir)) } |] +-- | Wraps @ImGui::Checkbox()@. +checkbox :: (HasSetter ref Bool, HasGetter ref Bool) => String -> ref -> IO Bool +checkbox label ref = do + currentValue <- get ref + with (bool 0 1 currentValue :: CBool) \boolPtr -> do + changed <- withCString label \labelPtr -> + (1 ==) <$> [C.exp| bool { Checkbox($(char* labelPtr), $(bool* boolPtr)) } |] + + newValue <- peek boolPtr + ref $=! (newValue == 1) + + return changed + + -- | A cardinal direction. newtype ImGuiDir = ImGuiDir CInt