From fc307a4d6ea16c62c45bcc89ccc0b15c43c46e48 Mon Sep 17 00:00:00 2001 From: Alexander Bondarenko <486682+dpwiz@users.noreply.github.com> Date: Thu, 10 Mar 2022 11:34:13 +0300 Subject: [PATCH] Add remaining popup wrappers (#136) - BeginPopupContextItem - BeginPopupContextWindow - BeginPopupContextVoid For #132 --- examples/glfw/Main.hs | 15 +--------- src/DearImGui.hs | 70 ++++++++++++++++++++++++++++++++++++++++++- src/DearImGui/Raw.hs | 23 +++++++++++++- 3 files changed, 92 insertions(+), 16 deletions(-) diff --git a/examples/glfw/Main.hs b/examples/glfw/Main.hs index 2ea8e30..08148d8 100644 --- a/examples/glfw/Main.hs +++ b/examples/glfw/Main.hs @@ -67,25 +67,12 @@ mainLoop win = do clicking <- button "Clickety Click" when clicking $ putStrLn "Ow!" - - -- Attach a popup to the latest widget - let popupId = "pop-me" - openPopupOnItemClick popupId ImGuiPopupFlags_MouseButtonRight - - -- Put some content into a popup window - -- alternatively: withPopup (closes automatically) - withPopupModalOpen popupId do + itemContextPopup do text "pop!" button "ok" >>= \clicked -> when clicked $ closeCurrentPopup - -- Query popup status - popping <- isCurrentPopupOpen popupId - when popping do - sameLine - text "Popping right now." - -- Show the ImGui demo window showDemoWindow diff --git a/src/DearImGui.hs b/src/DearImGui.hs index 2f1ff6e..0f486ad 100644 --- a/src/DearImGui.hs +++ b/src/DearImGui.hs @@ -234,20 +234,42 @@ module DearImGui , Raw.endTooltip -- * Popups/Modals + + -- ** Generic , withPopup , withPopupOpen , beginPopup + , Raw.endPopup + -- ** Modal , withPopupModal , withPopupModalOpen , beginPopupModal - , Raw.endPopup + -- ** Item context + , itemContextPopup + , withPopupContextItemOpen + , withPopupContextItem + , beginPopupContextItem + -- ** Window context + , windowContextPopup + , withPopupContextWindowOpen + , withPopupContextWindow + , beginPopupContextWindow + + -- ** Void context + , voidContextPopup + , withPopupContextVoidOpen + , withPopupContextVoid + , beginPopupContextVoid + + -- ** Manual , openPopup , openPopupOnItemClick , Raw.closeCurrentPopup + -- ** Queries , isCurrentPopupOpen , isAnyPopupOpen , isAnyLevelPopupOpen @@ -1484,6 +1506,52 @@ withPopupModalOpen :: MonadUnliftIO m => String -> m () -> m () withPopupModalOpen popupId action = withPopupModal popupId (`when` action) +beginPopupContextItem :: MonadIO m => Maybe String -> ImGuiPopupFlags -> m Bool +beginPopupContextItem itemId flags = liftIO do + withCStringOrNull itemId \popupIdPtr -> + Raw.beginPopupContextItem popupIdPtr flags + +withPopupContextItem :: MonadUnliftIO m => Maybe String -> ImGuiPopupFlags -> (Bool -> m a) -> m a +withPopupContextItem popupId flags = bracket (beginPopupContextItem popupId flags) (`when` Raw.endPopup) + +withPopupContextItemOpen :: MonadUnliftIO m => Maybe String -> ImGuiPopupFlags -> m () -> m () +withPopupContextItemOpen popupId flags action = withPopupContextItem popupId flags (`when` action) + +-- | Attach item context popup to right mouse button click on a last item. +itemContextPopup :: MonadUnliftIO m => m () -> m () +itemContextPopup = withPopupContextItemOpen Nothing ImGuiPopupFlags_MouseButtonRight + +beginPopupContextWindow :: MonadIO m => Maybe String -> ImGuiPopupFlags -> m Bool +beginPopupContextWindow popupId flags = liftIO do + withCStringOrNull popupId \popupIdPtr -> + Raw.beginPopupContextWindow popupIdPtr flags + +withPopupContextWindow :: MonadUnliftIO m => Maybe String -> ImGuiPopupFlags -> (Bool -> m a) -> m a +withPopupContextWindow popupId flags = bracket (beginPopupContextWindow popupId flags) (`when` Raw.endPopup) + +withPopupContextWindowOpen :: MonadUnliftIO m => Maybe String -> ImGuiPopupFlags -> m () -> m () +withPopupContextWindowOpen popupId flags action = withPopupContextWindow popupId flags (`when` action) + +-- | Attach item context popup to right mouse button click on a current window. +windowContextPopup :: MonadUnliftIO m => m () -> m () +windowContextPopup = withPopupContextWindowOpen Nothing ImGuiPopupFlags_MouseButtonRight + +beginPopupContextVoid :: MonadIO m => Maybe String -> ImGuiPopupFlags -> m Bool +beginPopupContextVoid popupId flags = liftIO do + withCStringOrNull popupId \popupIdPtr -> + Raw.beginPopupContextVoid popupIdPtr flags + +withPopupContextVoid :: MonadUnliftIO m => Maybe String -> ImGuiPopupFlags -> (Bool -> m a) -> m a +withPopupContextVoid popupId flags = bracket (beginPopupContextVoid popupId flags) (`when` Raw.endPopup) + +withPopupContextVoidOpen :: MonadUnliftIO m => Maybe String -> ImGuiPopupFlags -> m () -> m () +withPopupContextVoidOpen popupId flags action = withPopupContextVoid popupId flags (`when` action) + +-- | Attach item context popup to right mouse button click outside of any windows. +voidContextPopup :: MonadUnliftIO m => m () -> m () +voidContextPopup = withPopupContextWindowOpen Nothing ImGuiPopupFlags_MouseButtonRight + + -- | Call to mark popup as open (don't call every frame!). -- -- Wraps @ImGui::OpenPopup()@ diff --git a/src/DearImGui/Raw.hs b/src/DearImGui/Raw.hs index cb3e2c9..11e1621 100644 --- a/src/DearImGui/Raw.hs +++ b/src/DearImGui/Raw.hs @@ -199,6 +199,9 @@ module DearImGui.Raw , openPopup , openPopupOnItemClick , closeCurrentPopup + , beginPopupContextItem + , beginPopupContextWindow + , beginPopupContextVoid , isPopupOpen -- * ID stack/scopes @@ -1255,7 +1258,7 @@ openPopup popupIdPtr = liftIO do [C.exp| void { OpenPopup($(char* popupIdPtr)) } |] --- | helper to open popup when clicked on last item. +-- | Open popup when clicked on last item. -- -- Note: actually triggers on the mouse _released_ event to be consistent with popup behaviors. -- @@ -1272,6 +1275,24 @@ closeCurrentPopup :: (MonadIO m) => m () closeCurrentPopup = liftIO do [C.exp| void { CloseCurrentPopup() } |] +-- | Open+begin popup when clicked on last item. +-- +-- Use str_id==NULL to associate the popup to previous item. +-- +-- If you want to use that on a non-interactive item such as 'text' you need to pass in an explicit ID here. +beginPopupContextItem :: (MonadIO m) => CString -> ImGuiPopupFlags-> m Bool +beginPopupContextItem popupIdPtr flags = liftIO do + (0 /=) <$> [C.exp| bool { BeginPopupContextItem($(char* popupIdPtr), $(ImGuiPopupFlags flags)) } |] + +-- | Open+begin popup when clicked on current window. +beginPopupContextWindow :: (MonadIO m) => CString -> ImGuiPopupFlags-> m Bool +beginPopupContextWindow popupIdPtr flags = liftIO do + (0 /=) <$> [C.exp| bool { BeginPopupContextWindow($(char* popupIdPtr), $(ImGuiPopupFlags flags)) } |] + +-- | Open+begin popup when clicked in void (where there are no windows). +beginPopupContextVoid :: (MonadIO m) => CString -> ImGuiPopupFlags-> m Bool +beginPopupContextVoid popupIdPtr flags = liftIO do + (0 /=) <$> [C.exp| bool { BeginPopupContextVoid($(char* popupIdPtr), $(ImGuiPopupFlags flags)) } |] -- | Query popup status --