Add isPopupOpen and wrappers (#134)

This commit is contained in:
Alexander Bondarenko 2022-03-09 21:08:54 +03:00 committed by GitHub
parent b837d583a5
commit 4517af8123
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 5 deletions

View File

@ -64,9 +64,27 @@ mainLoop win = do
text "Hello, ImGui!" text "Hello, ImGui!"
-- Add a button widget, and call 'putStrLn' when it's clicked -- Add a button widget, and call 'putStrLn' when it's clicked
button "Clickety Click" >>= \case clicking <- button "Clickety Click"
False -> return () when clicking $
True -> putStrLn "Ow!" 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
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 -- Show the ImGui demo window
showDemoWindow showDemoWindow

View File

@ -248,6 +248,10 @@ module DearImGui
, openPopupOnItemClick , openPopupOnItemClick
, Raw.closeCurrentPopup , Raw.closeCurrentPopup
, isCurrentPopupOpen
, isAnyPopupOpen
, isAnyLevelPopupOpen
-- * Item/Widgets Utilities -- * Item/Widgets Utilities
, Raw.isItemHovered , Raw.isItemHovered
, Raw.wantCaptureMouse , Raw.wantCaptureMouse
@ -1496,7 +1500,27 @@ openPopup popupId = liftIO do
-- Wraps @ImGui::OpenPopup()@ -- Wraps @ImGui::OpenPopup()@
openPopupOnItemClick :: MonadIO m => String -> ImGuiPopupFlags -> m () openPopupOnItemClick :: MonadIO m => String -> ImGuiPopupFlags -> m ()
openPopupOnItemClick popupId flags = liftIO do openPopupOnItemClick popupId flags = liftIO do
withCString popupId $ \popupId' -> Raw.openPopupOnItemClick popupId' flags withCString popupId $ \idPtr ->
Raw.openPopupOnItemClick idPtr flags
-- | Check if the popup is open at the current 'beginPopup' level of the popup stack.
isCurrentPopupOpen :: MonadIO m => String -> m Bool
isCurrentPopupOpen popupId = liftIO do
withCString popupId $ \idPtr ->
Raw.isPopupOpen idPtr ImGuiPopupFlags_None
-- | Check if *any* popup is open at the current 'beginPopup' level of the popup stack.
isAnyPopupOpen :: MonadIO m => String -> m Bool
isAnyPopupOpen popupId = liftIO do
withCString popupId $ \idPtr ->
Raw.isPopupOpen idPtr ImGuiPopupFlags_AnyPopupId
-- | Check if *any* popup is open at any level of the popup stack.
isAnyLevelPopupOpen :: MonadIO m => String -> m Bool
isAnyLevelPopupOpen popupId = liftIO do
withCString popupId $ \idPtr ->
Raw.isPopupOpen idPtr $
ImGuiPopupFlags_AnyPopupId .|. ImGuiPopupFlags_AnyPopupLevel
withCStringOrNull :: Maybe String -> (Ptr CChar -> IO a) -> IO a withCStringOrNull :: Maybe String -> (Ptr CChar -> IO a) -> IO a

View File

@ -199,6 +199,7 @@ module DearImGui.Raw
, openPopup , openPopup
, openPopupOnItemClick , openPopupOnItemClick
, closeCurrentPopup , closeCurrentPopup
, isPopupOpen
-- * ID stack/scopes -- * ID stack/scopes
, pushIDInt , pushIDInt
@ -1253,7 +1254,10 @@ openPopup :: (MonadIO m) => CString -> m ()
openPopup popupIdPtr = liftIO do openPopup popupIdPtr = liftIO do
[C.exp| void { OpenPopup($(char* popupIdPtr)) } |] [C.exp| void { OpenPopup($(char* popupIdPtr)) } |]
-- | Call to mark popup as open (don't call every frame!).
-- | helper to open popup when clicked on last item.
--
-- Note: actually triggers on the mouse _released_ event to be consistent with popup behaviors.
-- --
-- Wraps @ImGui::OpenPopupOnItemClick()@ -- Wraps @ImGui::OpenPopupOnItemClick()@
openPopupOnItemClick :: (MonadIO m) => CString -> ImGuiPopupFlags-> m () openPopupOnItemClick :: (MonadIO m) => CString -> ImGuiPopupFlags-> m ()
@ -1269,6 +1273,18 @@ closeCurrentPopup = liftIO do
[C.exp| void { CloseCurrentPopup() } |] [C.exp| void { CloseCurrentPopup() } |]
-- | Query popup status
--
-- - return 'True' if the popup is open at the current 'beginPopup' level of the popup stack.
-- - with 'ImGuiPopupFlags_AnyPopupId': return 'True' if any popup is open at the current 'beginPopup' level of the popup stack.
-- - with 'ImGuiPopupFlags_AnyPopupId' | 'ImGuiPopupFlags_AnyPopupLevel': return 'True' if any popup is open.
--
-- Wraps @ImGui::IsPopupOpen()@
isPopupOpen :: (MonadIO m) => CString -> ImGuiPopupFlags-> m Bool
isPopupOpen popupIdPtr flags = liftIO do
(0 /=) <$> [C.exp| bool { IsPopupOpen($(char* popupIdPtr), $(ImGuiPopupFlags flags)) } |]
-- | Is the last item hovered? (and usable, aka not blocked by a popup, etc.). -- | Is the last item hovered? (and usable, aka not blocked by a popup, etc.).
-- --
-- Wraps @ImGui::IsItemHovered()@ -- Wraps @ImGui::IsItemHovered()@