Skip to content

Commit d382b64

Browse files
committed
Stubbing out ImGui::ColorButton() and ImVec4
1 parent ecab9d3 commit d382b64

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

Main.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ loop w checked = do
8080

8181
plotHistogram "A histogram" [ 10, 10, 20, 30, 90 ]
8282

83+
ref <- newIORef $ ImVec4 1 0 0 1
84+
colorButton "Test" ref
85+
8386
beginMainMenuBar >>= whenTrue do
8487
beginMenu "Hello" >>= whenTrue do
8588
menuItem "Hello"

hs-dear-imgui.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ flag sdl
2222
library
2323
exposed-modules:
2424
DearImGui
25+
DearImGui.Context
2526
hs-source-dirs:
2627
src
2728
default-language:
@@ -42,6 +43,7 @@ library
4243
imgui
4344
build-depends:
4445
base
46+
, containers
4547
, inline-c
4648
, inline-c-cpp
4749
, StateVar

src/DearImGui.hs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ module DearImGui
6262
, beginCombo
6363
, endCombo
6464

65+
-- * Color Editor/Picker
66+
, colorButton
67+
6568
-- ** Selectables
6669
, selectable
6770

@@ -97,6 +100,7 @@ module DearImGui
97100
, pattern ImGuiDirRight
98101
, pattern ImGuiDirUp
99102
, pattern ImGuiDirDown
103+
, ImVec4(..)
100104
)
101105
where
102106

@@ -105,6 +109,9 @@ import Data.Bool
105109
import Foreign
106110
import Foreign.C
107111

112+
-- dear-imgui
113+
import DearImGui.Context
114+
108115
-- inline-c
109116
import qualified Language.C.Inline as C
110117

@@ -120,7 +127,7 @@ import Control.Monad.IO.Class
120127
( MonadIO, liftIO )
121128

122129

123-
C.context (Cpp.cppCtx <> C.bsCtx)
130+
C.context (Cpp.cppCtx <> C.bsCtx <> imguiContext)
124131
C.include "imgui.h"
125132
Cpp.using "namespace ImGui"
126133

@@ -367,6 +374,22 @@ endCombo = liftIO do
367374
[C.exp| void { EndCombo() } |]
368375

369376

377+
-- | Display a color square/button, hover for details, return true when pressed.
378+
--
379+
-- | Wraps @ImGui::ColorButton()@.
380+
colorButton :: (MonadIO m, HasSetter ref ImVec4, HasGetter ref ImVec4) => String -> ref -> m Bool
381+
colorButton desc ref = liftIO do
382+
currentValue <- get ref
383+
with currentValue \refPtr -> do
384+
changed <- withCString desc \descPtr ->
385+
(1 == ) <$> [C.exp| bool { ColorButton( $(char* descPtr), *$(ImVec4 *refPtr) ) } |]
386+
387+
newValue <- peek refPtr
388+
ref $=! newValue
389+
390+
return changed
391+
392+
370393
-- | Wraps @ImGui::Selectable()@.
371394
selectable :: MonadIO m => String -> m Bool
372395
selectable label = liftIO do

src/DearImGui/Context.hs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{-# language NamedFieldPuns #-}
2+
{-# language OverloadedStrings #-}
3+
{-# language TemplateHaskell #-}
4+
5+
module DearImGui.Context where
6+
7+
import Language.C.Types
8+
import Language.C.Inline.Context
9+
import qualified Data.Map.Strict as Map
10+
import Foreign
11+
12+
13+
data ImVec4 = ImVec4 { x, y, z, w :: {-# unpack #-} Float }
14+
15+
16+
instance Storable ImVec4 where
17+
sizeOf ~ImVec4{x, y, z, w} = sizeOf x + sizeOf y + sizeOf z + sizeOf w
18+
19+
alignment _ = 0
20+
21+
poke ptr ImVec4{ x, y, z, w } = do
22+
poke (castPtr ptr `plusPtr` (sizeOf x * 0)) x
23+
poke (castPtr ptr `plusPtr` (sizeOf x * 1)) y
24+
poke (castPtr ptr `plusPtr` (sizeOf x * 2)) z
25+
poke (castPtr ptr `plusPtr` (sizeOf x * 3)) w
26+
27+
peek ptr = do
28+
x <- peek (castPtr ptr `plusPtr` )
29+
y <- peek (castPtr ptr `plusPtr` (sizeOf x * 1))
30+
z <- peek (castPtr ptr `plusPtr` (sizeOf x * 2))
31+
w <- peek (castPtr ptr `plusPtr` (sizeOf x * 3))
32+
return ImVec4{ x, y, z, w }
33+
34+
35+
imguiContext :: Context
36+
imguiContext = mempty
37+
{ ctxTypesTable = Map.fromList
38+
[ ( TypeName "ImVec4", [t| ImVec4 |] )
39+
]
40+
}

0 commit comments

Comments
 (0)