mirror of
				https://github.com/Drezil/dear-imgui.hs.git
				synced 2025-11-04 07:01:06 +01:00 
			
		
		
		
	Add combo to wrap ImGUI::Combo() (#28)
Co-authored-by: Oliver Charles <ollie@ocharles.org.uk>
This commit is contained in:
		
							
								
								
									
										18
									
								
								Main.hs
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								Main.hs
									
									
									
									
									
								
							@@ -30,13 +30,21 @@ main = do
 | 
				
			|||||||
      color <- newIORef $ ImVec3 1 0 0
 | 
					      color <- newIORef $ ImVec3 1 0 0
 | 
				
			||||||
      slider <- newIORef (0.42, 0, 0.314)
 | 
					      slider <- newIORef (0.42, 0, 0.314)
 | 
				
			||||||
      r <- newIORef 4
 | 
					      r <- newIORef 4
 | 
				
			||||||
      loop w checked color slider r
 | 
					      selected <- newIORef 4
 | 
				
			||||||
 | 
					      loop w checked color slider r selected
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      openGL2Shutdown
 | 
					      openGL2Shutdown
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
loop :: Window -> IORef Bool -> IORef ImVec3 -> IORef (Float, Float, Float) -> IORef Int -> IO ()
 | 
					loop 
 | 
				
			||||||
loop w checked color slider r = do
 | 
					  :: Window 
 | 
				
			||||||
 | 
					  -> IORef Bool 
 | 
				
			||||||
 | 
					  -> IORef ImVec3 
 | 
				
			||||||
 | 
					  -> IORef (Float, Float, Float) 
 | 
				
			||||||
 | 
					  -> IORef Int 
 | 
				
			||||||
 | 
					  -> IORef Int 
 | 
				
			||||||
 | 
					  -> IO ()
 | 
				
			||||||
 | 
					loop w checked color slider r selected = do
 | 
				
			||||||
  quit <- pollEvents
 | 
					  quit <- pollEvents
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  openGL2NewFrame
 | 
					  openGL2NewFrame
 | 
				
			||||||
@@ -89,6 +97,8 @@ loop w checked color slider r = do
 | 
				
			|||||||
    selectable "Testing 2"
 | 
					    selectable "Testing 2"
 | 
				
			||||||
    endCombo
 | 
					    endCombo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  combo "Simple" selected [ "1", "2", "3" ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  endChild
 | 
					  endChild
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  plotHistogram "A histogram" [ 10, 10, 20, 30, 90 ]
 | 
					  plotHistogram "A histogram" [ 10, 10, 20, 30, 90 ]
 | 
				
			||||||
@@ -124,7 +134,7 @@ loop w checked color slider r = do
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  glSwapWindow w
 | 
					  glSwapWindow w
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if quit then return () else loop w checked color slider r
 | 
					  if quit then return () else loop w checked color slider r selected
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  where
 | 
					  where
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -67,6 +67,7 @@ module DearImGui
 | 
				
			|||||||
    -- ** Combo Box
 | 
					    -- ** Combo Box
 | 
				
			||||||
  , beginCombo
 | 
					  , beginCombo
 | 
				
			||||||
  , endCombo
 | 
					  , endCombo
 | 
				
			||||||
 | 
					  , combo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    -- ** Drag Sliders
 | 
					    -- ** Drag Sliders
 | 
				
			||||||
  , dragFloat
 | 
					  , dragFloat
 | 
				
			||||||
@@ -416,6 +417,27 @@ endCombo = liftIO do
 | 
				
			|||||||
  [C.exp| void { EndCombo() } |]
 | 
					  [C.exp| void { EndCombo() } |]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-- Wraps @ImGui::Combo()@.
 | 
				
			||||||
 | 
					combo :: (MonadIO m, HasGetter ref Int, HasSetter ref Int) => String -> ref -> [String] -> m Bool
 | 
				
			||||||
 | 
					combo label selectedIndex items = liftIO $ Managed.with m return
 | 
				
			||||||
 | 
					  where
 | 
				
			||||||
 | 
					    m = do
 | 
				
			||||||
 | 
					      i <- get selectedIndex
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      cStrings <- traverse (\str -> Managed.managed (withCString str)) items
 | 
				
			||||||
 | 
					      labelPtr <- Managed.managed $ withCString label
 | 
				
			||||||
 | 
					      iPtr     <- Managed.managed $ with (fromIntegral i)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      liftIO $ withArrayLen cStrings \len itemsPtr -> do
 | 
				
			||||||
 | 
					        let len' = fromIntegral len
 | 
				
			||||||
 | 
					        [C.exp| bool { Combo($(char* labelPtr), $(int* iPtr), $(char** itemsPtr), $(int len')) }|] >>= \case
 | 
				
			||||||
 | 
					          0 -> return False
 | 
				
			||||||
 | 
					          _ -> do
 | 
				
			||||||
 | 
					            i' <- peek iPtr
 | 
				
			||||||
 | 
					            selectedIndex $=! fromIntegral i'
 | 
				
			||||||
 | 
					            return True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- | Wraps @ImGui::DragFloat()@
 | 
					-- | Wraps @ImGui::DragFloat()@
 | 
				
			||||||
dragFloat :: (MonadIO m, HasSetter ref Float, HasGetter ref Float) => String -> ref -> Float -> Float -> Float -> m Bool
 | 
					dragFloat :: (MonadIO m, HasSetter ref Float, HasGetter ref Float) => String -> ref -> Float -> Float -> Float -> m Bool
 | 
				
			||||||
dragFloat desc ref speed minValue maxValue = liftIO do
 | 
					dragFloat desc ref speed minValue maxValue = liftIO do
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user