From f24a4b78ab684571d1c86fba24875d6cedcb04c5 Mon Sep 17 00:00:00 2001 From: Ollie Charles Date: Thu, 28 Jan 2021 23:02:04 +0000 Subject: [PATCH] Implement ImGui::SliderFloat2,3,4 (#22) --- Main.hs | 6 ++-- src/DearImGui.hs | 91 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 78 insertions(+), 19 deletions(-) diff --git a/Main.hs b/Main.hs index f8651af..6c59c7c 100644 --- a/Main.hs +++ b/Main.hs @@ -28,12 +28,12 @@ main = do checked <- newIORef False color <- newIORef $ ImVec3 1 0 0 - slider <- newIORef 0.42 + slider <- newIORef (0.42, 0, 0.314) loop w checked color slider openGL2Shutdown -loop :: Window -> IORef Bool -> IORef ImVec3 -> IORef Float -> IO () +loop :: Window -> IORef Bool -> IORef ImVec3 -> IORef (Float, Float, Float) -> IO () loop w checked color slider = do quit <- pollEvents @@ -74,7 +74,7 @@ loop w checked color slider = do separator - sliderFloat "Slider" slider 0.0 1.0 + sliderFloat3 "Slider" slider 0.0 1.0 progressBar 0.314 (Just "Pi") diff --git a/src/DearImGui.hs b/src/DearImGui.hs index b3fe43a..b434f70 100644 --- a/src/DearImGui.hs +++ b/src/DearImGui.hs @@ -63,13 +63,16 @@ module DearImGui , progressBar , bullet - -- ** Slider - , sliderFloat - -- ** Combo Box , beginCombo , endCombo + -- ** Slider + , sliderFloat + , sliderFloat2 + , sliderFloat3 + , sliderFloat4 + -- * Color Editor/Picker , colorPicker3 , colorButton @@ -397,19 +400,6 @@ endCombo = liftIO do [C.exp| void { EndCombo() } |] --- | Wraps @ImGui::ColorPicker3()@. -colorPicker3 :: (MonadIO m, HasSetter ref ImVec3, HasGetter ref ImVec3) => String -> ref -> m Bool -colorPicker3 desc ref = liftIO do - ImVec3{x, y, z} <- get ref - withArray (realToFrac <$> [x, y, z]) \refPtr -> do - changed <- withCString desc \descPtr -> - (0 /= ) <$> [C.exp| bool { ColorPicker3( $(char* descPtr), $(float *refPtr) ) } |] - - [x', y', z'] <- peekArray 3 refPtr - ref $=! ImVec3 (realToFrac x') (realToFrac y') (realToFrac z') - - return changed - -- | Wraps @ImGui::SliderFloat()@ sliderFloat :: (MonadIO m, HasSetter ref Float, HasGetter ref Float) => String -> ref -> Float -> Float -> m Bool sliderFloat desc ref minValue maxValue = liftIO do @@ -427,6 +417,75 @@ sliderFloat desc ref minValue maxValue = liftIO do min' = realToFrac minValue max' = realToFrac maxValue + +-- | Wraps @ImGui::SliderFloat2()@ +sliderFloat2 :: (MonadIO m, HasSetter ref (Float, Float), HasGetter ref (Float, Float)) => String -> ref -> Float -> Float -> m Bool +sliderFloat2 desc ref minValue maxValue = liftIO do + (x, y) <- get ref + withArray [ realToFrac x, realToFrac y ] \floatPtr -> do + changed <- withCString desc \descPtr -> + (0 /=) <$> [C.exp| bool { SliderFloat2( $(char* descPtr), $(float *floatPtr), $(float min'), $(float max')) } |] + + [x', y'] <- peekArray 2 floatPtr + ref $=! (realToFrac x', realToFrac y') + + return changed + where + min', max' :: CFloat + min' = realToFrac minValue + max' = realToFrac maxValue + + +-- | Wraps @ImGui::SliderFloat3()@ +sliderFloat3 :: (MonadIO m, HasSetter ref (Float, Float, Float), HasGetter ref (Float, Float, Float)) => String -> ref -> Float -> Float -> m Bool +sliderFloat3 desc ref minValue maxValue = liftIO do + (x, y, z) <- get ref + withArray [ realToFrac x, realToFrac y, realToFrac z ] \floatPtr -> do + changed <- withCString desc \descPtr -> + (0 /=) <$> [C.exp| bool { SliderFloat3( $(char* descPtr), $(float *floatPtr), $(float min'), $(float max')) } |] + + [x', y', z'] <- peekArray 3 floatPtr + ref $=! (realToFrac x', realToFrac y', realToFrac z') + + return changed + where + min', max' :: CFloat + min' = realToFrac minValue + max' = realToFrac maxValue + + +-- | Wraps @ImGui::SliderFloat4()@ +sliderFloat4 :: (MonadIO m, HasSetter ref (Float, Float, Float, Float), HasGetter ref (Float, Float, Float, Float)) => String -> ref -> Float -> Float -> m Bool +sliderFloat4 desc ref minValue maxValue = liftIO do + (x, y, z, u) <- get ref + withArray [ realToFrac x, realToFrac y, realToFrac z, realToFrac u ] \floatPtr -> do + changed <- withCString desc \descPtr -> + (0 /=) <$> [C.exp| bool { SliderFloat4( $(char* descPtr), $(float *floatPtr), $(float min'), $(float max')) } |] + + [x', y', z', u'] <- peekArray 4 floatPtr + ref $=! (realToFrac x', realToFrac y', realToFrac z', realToFrac u') + + return changed + where + min', max' :: CFloat + min' = realToFrac minValue + max' = realToFrac maxValue + + +-- | Wraps @ImGui::ColorPicker3()@. +colorPicker3 :: (MonadIO m, HasSetter ref ImVec3, HasGetter ref ImVec3) => String -> ref -> m Bool +colorPicker3 desc ref = liftIO do + ImVec3{x, y, z} <- get ref + withArray (realToFrac <$> [x, y, z]) \refPtr -> do + changed <- withCString desc \descPtr -> + (0 /= ) <$> [C.exp| bool { ColorPicker3( $(char* descPtr), $(float *refPtr) ) } |] + + [x', y', z'] <- peekArray 3 refPtr + ref $=! ImVec3 (realToFrac x') (realToFrac y') (realToFrac z') + + return changed + + -- | Display a color square/button, hover for details, return true when pressed. -- -- Wraps @ImGui::ColorButton()@.