Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap ImGuiIO #18

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

module Main (main) where

import Data.StateVar
import Data.IORef
import DearImGui
import DearImGui.OpenGL
Expand All @@ -23,6 +24,11 @@ main = do
bracket createContext destroyContext \_imguiContext ->
bracket_ (sdl2InitForOpenGL w glContext) sdl2Shutdown $
bracket_ openGL2Init openGL2Shutdown do
iniFilename $= Just "imgui_state.ini"

putStr "State stored in: "
get iniFilename >>= print

checkVersion
styleColorsLight

Expand Down
2 changes: 1 addition & 1 deletion dear-imgui.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ library
executable test
main-is: Main.hs
default-language: Haskell2010
build-depends: base, sdl2, gl, dear-imgui
build-depends: base, sdl2, gl, dear-imgui, StateVar
ghc-options: -Wall


Expand Down
24 changes: 23 additions & 1 deletion src/DearImGui.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
Expand All @@ -27,6 +28,9 @@ module DearImGui
, getDrawData
, checkVersion

-- ** @ImGUIIO@
, iniFilename

-- * Demo, Debug, Information
, showDemoWindow
, showMetricsWindow
Expand Down Expand Up @@ -126,7 +130,7 @@ import qualified Language.C.Inline.Cpp as Cpp

-- StateVar
import Data.StateVar
( HasGetter(get), HasSetter, ($=!) )
( HasGetter(get), HasSetter, StateVar(..), ($=!) )

-- transformers
import Control.Monad.IO.Class
Expand Down Expand Up @@ -196,6 +200,24 @@ checkVersion = liftIO do
[C.exp| void { IMGUI_CHECKVERSION(); } |]


-- | Path to @.ini@ file. Set to 'Nothing' to disable automatic .ini
-- loading/saving, if e.g. you want to manually load/save from memory.
iniFilename :: StateVar (Maybe String)
iniFilename = StateVar getter setter
where
getter = do
cStr <- [C.exp| const char* { GetIO().IniFilename } |]
if cStr == nullPtr then return Nothing else Just <$> peekCString cStr

setter = \case
Nothing ->
[C.block| void { GetIO().IniFilename = $(char* nullPtr); } |]

Just str -> do
strPtr <- newCString str
[C.block| void { GetIO().IniFilename = $(char* strPtr); } |]


-- | Create demo window. Demonstrate most ImGui features. Call this to learn
-- about the library! Try to make it always available in your application!
showDemoWindow :: MonadIO m => m ()
Expand Down