Add imageButton (#97)

This commit is contained in:
Alexander Bondarenko 2021-09-12 16:20:47 +03:00 committed by GitHub
parent 88326420b8
commit e3f7fbfd6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 35 deletions

View File

@ -29,6 +29,7 @@ data Texture = Texture
, textureWidth :: GLsizei , textureWidth :: GLsizei
, textureHeight :: GLsizei , textureHeight :: GLsizei
} }
deriving (Show)
textureSize :: Texture -> ImVec2 textureSize :: Texture -> ImVec2
textureSize texture = textureSize texture =
@ -69,17 +70,20 @@ bindTexture texture dataPtr = do
GL_UNSIGNED_BYTE GL_UNSIGNED_BYTE
(castPtr dataPtr) (castPtr dataPtr)
fill :: Integral size => size -> size -> VS.Vector GLubyte fill :: Texture -> (GLubyte, GLubyte, GLubyte) -> VS.Vector GLubyte
fill width height = fill texture (r, g, b) =
VS.generate VS.generate
(3 * fromIntegral width * fromIntegral height) (3 * width * height)
(\i -> (\i ->
case i `mod` 3 of case i `mod` 3 of
0 -> 0x00 0 -> r
1 -> 0x7F 1 -> g
2 -> 0xFF 2 -> b
_ -> error "assert: 3-byte pitch" _ -> error "assert: 3-byte pitch"
) )
where
width = fromIntegral (textureWidth texture)
height = fromIntegral (textureHeight texture)
main :: IO () main :: IO ()
@ -109,34 +113,47 @@ main = do
openGL3Shutdown openGL3Shutdown
liftIO do liftIO do
let width = 320 blueish <- create2DTexture 320 240
height = 240 VS.unsafeWith (fill blueish (0x00, 0x7F, 0xFF)) $
texture <- create2DTexture width height bindTexture blueish
VS.unsafeWith (fill width height) $
bindTexture texture pinkish <- create2DTexture 240 320
VS.unsafeWith (fill pinkish (0xFF, 0x00, 0x7F)) $
bindTexture pinkish
err <- glGetError err <- glGetError
putStrLn $ "Error-code: " ++ show err putStrLn $ "Error-code: " ++ show err
mainLoop window texture print (blueish, pinkish)
mainLoop window (blueish, pinkish) False
mainLoop :: SDL.Window -> Texture -> IO () mainLoop :: SDL.Window -> (Texture, Texture) -> Bool -> IO ()
mainLoop window texture = unlessQuit do mainLoop window textures flag = unlessQuit do
-- Tell ImGui we're starting a new frame -- Tell ImGui we're starting a new frame
openGL3NewFrame openGL3NewFrame
sdl2NewFrame sdl2NewFrame
newFrame newFrame
-- Build the GUI -- Build the GUI
withWindowOpen "Image example" $ runManaged do clicked <- withWindow "Image example" \open ->
-- Drawing images require some backend-specific code. if open then do
-- Meanwhile, we have to deal with raw binding. text "That's an image, click it"
let openGLtextureID = intPtrToPtr $ fromIntegral $ textureID texture newLine
sizePtr <- managed $ Foreign.with (textureSize texture)
uv0Ptr <- managed $ Foreign.with (ImVec2 0 0) let texture = if flag then fst textures else snd textures
uv1Ptr <- managed $ Foreign.with (ImVec2 1 1)
tintColPtr <- managed $ Foreign.with (ImVec4 1 1 1 1) -- Drawing images require some backend-specific code.
borderColPtr <- managed $ Foreign.with (ImVec4 0 1 0 0) -- Meanwhile, we have to deal with raw binding.
Raw.image openGLtextureID sizePtr uv0Ptr uv1Ptr tintColPtr borderColPtr let openGLtextureID = intPtrToPtr $ fromIntegral $ textureID texture
Foreign.with (textureSize texture) \sizePtr ->
Foreign.with (ImVec2 0 0) \uv0Ptr ->
Foreign.with (ImVec2 1 1) \uv1Ptr ->
Foreign.with (ImVec4 1 1 1 1) \tintColPtr ->
Foreign.with (ImVec4 1 1 1 1) \bgColPtr ->
Raw.imageButton openGLtextureID sizePtr uv0Ptr uv1Ptr (-1) bgColPtr tintColPtr
else
pure False
-- Render -- Render
glClear GL_COLOR_BUFFER_BIT glClear GL_COLOR_BUFFER_BIT
@ -146,7 +163,7 @@ mainLoop window texture = unlessQuit do
SDL.glSwapWindow window SDL.glSwapWindow window
mainLoop window texture mainLoop window textures (flag /= clicked)
where where
unlessQuit action = do unlessQuit action = do

View File

@ -106,6 +106,7 @@ module DearImGui.Raw
, invisibleButton , invisibleButton
, arrowButton , arrowButton
, image , image
, imageButton
, checkbox , checkbox
, progressBar , progressBar
, bullet , bullet
@ -568,16 +569,39 @@ arrowButton strIdPtr dir = liftIO do
-- Wraps @ImGui::Image()@. -- Wraps @ImGui::Image()@.
image :: (MonadIO m) => Ptr () -> Ptr ImVec2 -> Ptr ImVec2 -> Ptr ImVec2 -> Ptr ImVec4 -> Ptr ImVec4 -> m () image :: (MonadIO m) => Ptr () -> Ptr ImVec2 -> Ptr ImVec2 -> Ptr ImVec2 -> Ptr ImVec4 -> Ptr ImVec4 -> m ()
image userTextureIDPtr sizePtr uv0Ptr uv1Ptr tintColPtr borderColPtr = liftIO do image userTextureIDPtr sizePtr uv0Ptr uv1Ptr tintColPtr borderColPtr = liftIO do
[C.exp| void { [C.exp|
Image( void {
$(void* userTextureIDPtr), Image(
*$(ImVec2* sizePtr), $(void* userTextureIDPtr),
*$(ImVec2* uv0Ptr), *$(ImVec2* sizePtr),
*$(ImVec2* uv1Ptr), *$(ImVec2* uv0Ptr),
*$(ImVec4* tintColPtr), *$(ImVec2* uv1Ptr),
*$(ImVec4* borderColPtr) *$(ImVec4* tintColPtr),
) *$(ImVec4* borderColPtr)
} |] )
}
|]
-- | Clickable Image Area.
--
-- Negative @frame_padding@ uses default frame padding settings. Set to 0 for no padding.
--
-- Wraps @ImGui::ImageButton()@.
imageButton :: (MonadIO m) => Ptr () -> Ptr ImVec2 -> Ptr ImVec2 -> Ptr ImVec2 -> CInt -> Ptr ImVec4 -> Ptr ImVec4 -> m Bool
imageButton userTextureIDPtr sizePtr uv0Ptr uv1Ptr framePadding bgColPtr tintColPtr = liftIO do
(0 /=) <$> [C.exp|
bool {
ImageButton(
$(void* userTextureIDPtr),
*$(ImVec2* sizePtr),
*$(ImVec2* uv0Ptr),
*$(ImVec2* uv1Ptr),
$(int framePadding),
*$(ImVec4* bgColPtr),
*$(ImVec4* tintColPtr)
)
}
|]
-- | Wraps @ImGui::Checkbox()@. -- | Wraps @ImGui::Checkbox()@.