dear-imgui.hs/Main.hs

114 lines
2.3 KiB
Haskell
Raw Normal View History

2021-01-24 15:27:03 +00:00
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
2021-01-24 15:56:14 +00:00
import Data.IORef
2021-01-24 15:27:03 +00:00
import DearImGui
import Control.Exception
import Graphics.GL
import SDL
main :: IO ()
main = do
initializeAll
bracket (createWindow "Hello, Dear ImGui!" defaultWindow { windowGraphicsContext = OpenGLContext defaultOpenGL }) destroyWindow \w ->
bracket (glCreateContext w) glDeleteContext \glContext ->
bracket createContext destroyContext \_imguiContext ->
bracket_ (sdl2InitForOpenGL w glContext) sdl2Shutdown $
bracket_ openGL2Init openGL2Shutdown do
checkVersion
styleColorsLight
openGL2Init
2021-01-24 15:56:14 +00:00
newIORef False >>= loop w
2021-01-24 15:27:03 +00:00
openGL2Shutdown
2021-01-24 15:56:14 +00:00
loop :: Window -> IORef Bool -> IO ()
loop w checked = do
2021-01-24 16:49:38 +00:00
quit <- pollEvents
2021-01-24 15:27:03 +00:00
openGL2NewFrame
sdl2NewFrame w
newFrame
-- showDemoWindow
-- showMetricsWindow
-- showAboutWindow
-- showUserGuide
begin "My Window"
text "Hello!"
button "Click me" >>= \case
2021-01-24 17:35:00 +00:00
True -> openPopup "Button Popup"
2021-01-24 15:27:03 +00:00
False -> return ()
2021-01-24 17:35:00 +00:00
beginPopupModal "Button Popup" >>= whenTrue do
button "Close" >>= whenTrue closeCurrentPopup
endPopup
2021-01-24 16:58:52 +00:00
sameLine >> smallButton "Click me" >>= \case
2021-01-24 15:27:03 +00:00
True -> putStrLn "Oh hi Mark"
False -> return ()
2021-01-24 16:58:52 +00:00
sameLine >> arrowButton "Arrow" ImGuiDirUp
2021-01-24 15:54:39 +00:00
2021-01-24 16:58:52 +00:00
sameLine >> checkbox "Check!" checked >>= \case
2021-01-24 15:56:14 +00:00
True -> readIORef checked >>= print
False -> return ()
2021-01-24 17:00:25 +00:00
separator
2021-01-24 16:03:18 +00:00
progressBar 0.314 (Just "Pi")
beginCombo "Label" "Preview" >>= whenTrue do
selectable "Testing 1"
selectable "Testing 2"
endCombo
2021-01-24 16:49:28 +00:00
beginMainMenuBar >>= whenTrue do
beginMenu "Hello" >>= whenTrue do
menuItem "Hello"
endMenu
beginMenu "World" >>= whenTrue do
menuItem "World"
endMenu
endMainMenuBar
2021-01-24 15:27:03 +00:00
end
render
glClear GL_COLOR_BUFFER_BIT
openGL2RenderDrawData =<< getDrawData
glSwapWindow w
2021-01-24 16:49:38 +00:00
if quit then return () else loop w checked
where
pollEvents = do
ev <- pollEventWithImGui
case ev of
Nothing -> return False
Just Event{ eventPayload } -> do
let isQuit = case eventPayload of
QuitEvent -> True
_ -> False
(isQuit ||) <$> pollEvents
whenTrue :: IO () -> Bool -> IO ()
whenTrue io True = io
whenTrue io False = return ()