Skip to content

Commit

Permalink
Experiment: make StateRef wrappers optional
Browse files Browse the repository at this point in the history
  • Loading branch information
dpwiz committed Mar 23, 2022
1 parent 7d4f3a8 commit 4d47030
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/DearImGui.hs
Original file line number Diff line number Diff line change
Expand Up @@ -570,21 +570,36 @@ arrowButton strId dir = liftIO do
withCString strId \strIdPtr ->
Raw.arrowButton strIdPtr dir


-- | Wraps @ImGui::Checkbox()@.
checkbox :: (HasSetter ref Bool, HasGetter ref Bool, MonadIO m) => String -> ref -> m Bool
checkbox label ref = liftIO do
currentValue <- get ref
checkbox label ref = stateful ref $ checkboxM label

{-# INLINEABLE stateful #-}
stateful
:: (HasGetter t a, MonadIO m, HasSetter t a)
=> t -> (a -> m (Maybe a)) -> m Bool
stateful ref action = get ref >>= action >>= maybeSet ref

{-# INLINEABLE maybeSet #-}
maybeSet :: (HasSetter t a, MonadIO f) => t -> Maybe a -> f Bool
maybeSet ref = \case
Nothing ->
pure False
Just val -> do
ref $=! val
pure True

-- | Wraps @ImGui::Checkbox()@.
checkboxM :: (MonadIO m) => String -> Bool -> m (Maybe Bool)
checkboxM label currentValue = liftIO do
with (bool 0 1 currentValue) \boolPtr -> do
changed <- withCString label \labelPtr ->
Raw.checkbox labelPtr boolPtr

when changed do
newValue <- peek boolPtr
ref $=! (newValue == 1)

return changed

if changed then
(Just . (/=) 0) <$> peek boolPtr
else
pure Nothing

progressBar :: MonadIO m => Float -> Maybe String -> m ()
progressBar progress overlay = liftIO do
Expand Down

0 comments on commit 4d47030

Please sign in to comment.