From aa681fb77dcffae9b5fd4cdab8fe85f8e49aecf5 Mon Sep 17 00:00:00 2001 From: Ollie Charles Date: Sun, 24 Jan 2021 16:14:51 +0000 Subject: [PATCH] Wrap ImGui::BeginCombo(), EndCombo(), Selectable() --- Main.hs | 10 ++++++++++ src/DearImGui.hs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/Main.hs b/Main.hs index a610669..8bd8b9f 100644 --- a/Main.hs +++ b/Main.hs @@ -60,6 +60,11 @@ loop w checked = do progressBar 0.314 (Just "Pi") + beginCombo "Label" "Preview" >>= whenTrue do + selectable "Testing 1" + selectable "Testing 2" + endCombo + end render @@ -74,3 +79,8 @@ loop w checked = do Just Event{ eventPayload } -> case eventPayload of QuitEvent -> return () _ -> loop w checked + + +whenTrue :: IO () -> Bool -> IO () +whenTrue io True = io +whenTrue io False = return () diff --git a/src/DearImGui.hs b/src/DearImGui.hs index 4045860..c3a05aa 100644 --- a/src/DearImGui.hs +++ b/src/DearImGui.hs @@ -60,6 +60,13 @@ module DearImGui , progressBar , bullet + -- ** Combo Box + , beginCombo + , endCombo + + -- ** Selectables + , selectable + -- * Types , ImGuiDir , pattern ImGuiDirLeft @@ -331,6 +338,32 @@ bullet :: IO () bullet = [C.exp| void { Bullet() } |] +-- | Begin creating a combo box with a given label and preview value. +-- +-- Returns 'True' if the combo box is open. In this state, you should populate +-- the contents of the combo box - for example, by calling 'selectable'. +-- +-- Wraps @ImGui::BeginCombo()@. +beginCombo :: String -> String -> IO Bool +beginCombo label previewValue = + withCString label \labelPtr -> + withCString previewValue \previewValuePtr -> + (1 ==) <$> [C.exp| bool { BeginCombo($(char* labelPtr), $(char* previewValuePtr)) } |] + + +-- | Only call 'endCombo' if 'beginCombon' returns 'True'! +-- +-- Wraps @ImGui::EndCombo()@. +endCombo :: IO () +endCombo = [C.exp| void { EndCombo() } |] + + +-- | Wraps @ImGui::Selectable()@. +selectable :: String -> IO Bool +selectable label = withCString label \labelPtr -> + (1 == ) <$> [C.exp| bool { Selectable($(char* labelPtr)) } |] + + -- | A cardinal direction. newtype ImGuiDir = ImGuiDir CInt