Skip to content

Commit 4586f30

Browse files
committed
Wrap @imgui::ColorPicker3()@
1 parent 774ef94 commit 4586f30

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

Main.hs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ main = do
2626
checkVersion
2727
styleColorsLight
2828

29-
newIORef False >>= loop w
29+
checked <- newIORef False
30+
color <- newIORef $ ImVec3 1 0 0
31+
loop w checked color
3032

3133
openGL2Shutdown
3234

33-
loop :: Window -> IORef Bool -> IO ()
34-
loop w checked = do
35+
loop :: Window -> IORef Bool -> IORef ImVec3 -> IO ()
36+
loop w checked color = do
3537
quit <- pollEvents
3638

3739
openGL2NewFrame
@@ -80,8 +82,7 @@ loop w checked = do
8082

8183
plotHistogram "A histogram" [ 10, 10, 20, 30, 90 ]
8284

83-
ref <- newIORef $ ImVec4 1 0 0 1
84-
colorButton "Test" ref
85+
colorPicker3 "Test" color
8586

8687
beginMainMenuBar >>= whenTrue do
8788
beginMenu "Hello" >>= whenTrue do
@@ -103,7 +104,7 @@ loop w checked = do
103104

104105
glSwapWindow w
105106

106-
if quit then return () else loop w checked
107+
if quit then return () else loop w checked color
107108

108109
where
109110

src/DearImGui.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE BlockArguments #-}
2+
{-# LANGUAGE DuplicateRecordFields #-}
23
{-# LANGUAGE FlexibleContexts #-}
34
{-# LANGUAGE NamedFieldPuns #-}
45
{-# LANGUAGE OverloadedStrings #-}
@@ -63,6 +64,7 @@ module DearImGui
6364
, endCombo
6465

6566
-- * Color Editor/Picker
67+
, colorPicker3
6668
, colorButton
6769

6870
-- ** Selectables
@@ -100,6 +102,7 @@ module DearImGui
100102
, pattern ImGuiDirRight
101103
, pattern ImGuiDirUp
102104
, pattern ImGuiDirDown
105+
, ImVec3(..)
103106
, ImVec4(..)
104107
)
105108
where
@@ -374,6 +377,20 @@ endCombo = liftIO do
374377
[C.exp| void { EndCombo() } |]
375378

376379

380+
-- | Wraps @ImGui::ColorPicker3()@.
381+
colorPicker3 :: (MonadIO m, HasSetter ref ImVec3, HasGetter ref ImVec3) => String -> ref -> m Bool
382+
colorPicker3 desc ref = liftIO do
383+
ImVec3{x, y, z} <- get ref
384+
withArray (realToFrac <$> [x, y, z]) \refPtr -> do
385+
changed <- withCString desc \descPtr ->
386+
(1 == ) <$> [C.exp| bool { ColorPicker3( $(char* descPtr), $(float *refPtr) ) } |]
387+
388+
[x', y', z'] <- peekArray 3 refPtr
389+
ref $=! ImVec3 (realToFrac x') (realToFrac y') (realToFrac z')
390+
391+
return changed
392+
393+
377394
-- | Display a color square/button, hover for details, return true when pressed.
378395
--
379396
-- | Wraps @ImGui::ColorButton()@.

src/DearImGui/Context.hs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# language DuplicateRecordFields #-}
12
{-# language NamedFieldPuns #-}
23
{-# language OverloadedStrings #-}
34
{-# language TemplateHaskell #-}
@@ -10,6 +11,26 @@ import qualified Data.Map.Strict as Map
1011
import Foreign
1112

1213

14+
data ImVec3 = ImVec3 { x, y, z :: {-# unpack #-} !Float }
15+
16+
17+
instance Storable ImVec3 where
18+
sizeOf ~ImVec3{x, y, z} = sizeOf x + sizeOf y + sizeOf z
19+
20+
alignment _ = 0
21+
22+
poke ptr ImVec3{ x, y, z } = do
23+
poke (castPtr ptr `plusPtr` (sizeOf x * 0)) x
24+
poke (castPtr ptr `plusPtr` (sizeOf x * 1)) y
25+
poke (castPtr ptr `plusPtr` (sizeOf x * 2)) z
26+
27+
peek ptr = do
28+
x <- peek (castPtr ptr )
29+
y <- peek (castPtr ptr `plusPtr` (sizeOf x * 1))
30+
z <- peek (castPtr ptr `plusPtr` (sizeOf x * 2))
31+
return ImVec3{ x, y, z }
32+
33+
1334
data ImVec4 = ImVec4 { x, y, z, w :: {-# unpack #-} !Float }
1435

1536

@@ -35,6 +56,7 @@ instance Storable ImVec4 where
3556
imguiContext :: Context
3657
imguiContext = mempty
3758
{ ctxTypesTable = Map.fromList
38-
[ ( TypeName "ImVec4", [t| ImVec4 |] )
59+
[ ( TypeName "ImVec3", [t| ImVec3 |] )
60+
, ( TypeName "ImVec4", [t| ImVec4 |] )
3961
]
4062
}

0 commit comments

Comments
 (0)