From 6374ec92976834c8a9bc5f92ab122a6cf7984f0f Mon Sep 17 00:00:00 2001 From: Ollie Charles Date: Sun, 31 Jan 2021 15:32:08 +0000 Subject: [PATCH] Sketch for new argument handling --- Main.hs | 2 +- src/DearImGui.hs | 33 +++++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Main.hs b/Main.hs index 5485202..3dde086 100644 --- a/Main.hs +++ b/Main.hs @@ -46,7 +46,7 @@ loop w checked color slider = do -- showAboutWindow -- showUserGuide - begin "My Window" + begin Begin{ name = "My Window", isOpen = Nothing } text "Hello!" button "Click me" >>= \case diff --git a/src/DearImGui.hs b/src/DearImGui.hs index a796b22..0bc9eac 100644 --- a/src/DearImGui.hs +++ b/src/DearImGui.hs @@ -41,6 +41,7 @@ module DearImGui -- * Windows , begin + , Begin(..) , end -- * Cursor/Layout @@ -126,7 +127,7 @@ import qualified Language.C.Inline.Cpp as Cpp -- StateVar import Data.StateVar - ( HasGetter(get), HasSetter, ($=!) ) + ( HasGetter(get), HasSetter, ($=!), StateVar ) -- transformers import Control.Monad.IO.Class @@ -262,10 +263,21 @@ styleColorsClassic = liftIO do -- matching 'end' for each 'begin' call, regardless of its return value! -- -- Wraps @ImGui::Begin()@. -begin :: MonadIO m => String -> m Bool -begin name = liftIO do +begin :: MonadIO m => Begin -> m Bool +begin Begin{ name, isOpen } = liftIO $ withCString name \namePtr -> - (0 /=) <$> [C.exp| bool { ImGui::Begin($(char* namePtr)) } |] + withMaybeStateVar isOpen \isOpenPtr -> do + (0 /=) <$> [C.exp| bool { ImGui::Begin($(char* namePtr), $(bool* isOpenPtr)) } |] + + +data Begin = Begin + { name :: String + + , isOpen :: Maybe (StateVar CBool) + -- ^ When set shows a window-closing widget in the upper-right corner of the + -- window, which clicking will set this 'Bool' reference to 'False' when + -- clicked. + } -- | Pop window from the stack. @@ -582,3 +594,16 @@ pattern ImGuiDirDown = ImGuiDir 3 withCStringOrNull :: Maybe String -> (Ptr CChar -> IO a) -> IO a withCStringOrNull Nothing k = k nullPtr withCStringOrNull (Just s) k = withCString s k + + +withMaybeStateVar :: Storable x => Maybe (StateVar x) -> (Ptr x -> IO r) -> IO r +withMaybeStateVar Nothing k = k nullPtr +withMaybeStateVar (Just r) k = do + x <- get r + with x \xPtr -> do + y <- k xPtr + + x' <- peek xPtr + r $=! x' + + return y