From 93b729dae38b7402359683e729e02fe16bafe57c Mon Sep 17 00:00:00 2001 From: Ollie Charles Date: Sun, 24 Jan 2021 17:35:00 +0000 Subject: [PATCH] Wrap ImGui popups --- Main.hs | 6 +++++- src/DearImGui.hs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/Main.hs b/Main.hs index bf443b6..563fbd2 100644 --- a/Main.hs +++ b/Main.hs @@ -45,9 +45,13 @@ loop w checked = do text "Hello!" button "Click me" >>= \case - True -> putStrLn "Oh hi Mark" + True -> openPopup "Button Popup" False -> return () + beginPopupModal "Button Popup" >>= whenTrue do + button "Close" >>= whenTrue closeCurrentPopup + endPopup + sameLine >> smallButton "Click me" >>= \case True -> putStrLn "Oh hi Mark" False -> return () diff --git a/src/DearImGui.hs b/src/DearImGui.hs index d9805ae..e6c4cf6 100644 --- a/src/DearImGui.hs +++ b/src/DearImGui.hs @@ -80,6 +80,13 @@ module DearImGui , endMenu , menuItem + -- * Popups/Modals + , beginPopup + , beginPopupModal + , endPopup + , openPopup + , closeCurrentPopup + -- * Types , ImGuiDir , pattern ImGuiDirLeft @@ -483,6 +490,49 @@ menuItem label = liftIO do (1 ==) <$> [C.exp| bool { MenuItem($(char* labelPtr)) } |] +-- | Returns 'True' if the popup is open, and you can start outputting to it. +-- +-- Wraps @ImGui::BeginPopup()@ +beginPopup :: MonadIO m => String -> m Bool +beginPopup popupId = liftIO do + withCString popupId \popupIdPtr -> + (1 ==) <$> [C.exp| bool { BeginPopup($(char* popupIdPtr)) } |] + + +-- | Returns 'True' if the modal is open, and you can start outputting to it. +-- +-- Wraps @ImGui::BeginPopupModal()@ +beginPopupModal :: MonadIO m => String -> m Bool +beginPopupModal popupId = liftIO do + withCString popupId \popupIdPtr -> + (1 ==) <$> [C.exp| bool { BeginPopupModal($(char* popupIdPtr)) } |] + + +-- | Only call 'endPopup' if 'beginPopup' or 'beginPopupModal' returns 'True'! +-- +-- Wraps @ImGui::BeginPopupModal()@ +endPopup :: MonadIO m => m () +endPopup = liftIO do + [C.exp| void { EndPopup() } |] + + +-- | Call to mark popup as open (don't call every frame!). +-- +-- Wraps @ImGui::OpenPopup()@ +openPopup :: MonadIO m => String -> m () +openPopup popupId = liftIO do + withCString popupId \popupIdPtr -> + [C.exp| void { OpenPopup($(char* popupIdPtr)) } |] + + +-- | Manually close the popup we have begin-ed into. +-- +-- Wraps @ImGui::ClosePopup()@ +closeCurrentPopup :: MonadIO m => m () +closeCurrentPopup = liftIO do + [C.exp| void { CloseCurrentPopup() } |] + + -- | A cardinal direction. newtype ImGuiDir = ImGuiDir CInt