Skip to content

Commit 78bb5ec

Browse files
committed
Wrap ImGuiIO.iniFilename
1 parent 397cea7 commit 78bb5ec

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Main.hs

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
module Main (main) where
77

8+
import Data.StateVar
89
import Data.IORef
910
import DearImGui
1011
import DearImGui.OpenGL
@@ -23,6 +24,11 @@ main = do
2324
bracket createContext destroyContext \_imguiContext ->
2425
bracket_ (sdl2InitForOpenGL w glContext) sdl2Shutdown $
2526
bracket_ openGL2Init openGL2Shutdown do
27+
iniFilename $= Just "imgui_state.ini"
28+
29+
putStr "State stored in: "
30+
get iniFilename >>= print
31+
2632
checkVersion
2733
styleColorsLight
2834

dear-imgui.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ library
120120
executable test
121121
main-is: Main.hs
122122
default-language: Haskell2010
123-
build-depends: base, sdl2, gl, dear-imgui
123+
build-depends: base, sdl2, gl, dear-imgui, StateVar
124124
ghc-options: -Wall
125125

126126

src/DearImGui.hs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE BlockArguments #-}
22
{-# LANGUAGE DuplicateRecordFields #-}
33
{-# LANGUAGE FlexibleContexts #-}
4+
{-# LANGUAGE LambdaCase #-}
45
{-# LANGUAGE NamedFieldPuns #-}
56
{-# LANGUAGE OverloadedStrings #-}
67
{-# LANGUAGE PatternSynonyms #-}
@@ -27,6 +28,9 @@ module DearImGui
2728
, getDrawData
2829
, checkVersion
2930

31+
-- ** @ImGUIIO@
32+
, iniFilename
33+
3034
-- * Demo, Debug, Information
3135
, showDemoWindow
3236
, showMetricsWindow
@@ -126,7 +130,7 @@ import qualified Language.C.Inline.Cpp as Cpp
126130

127131
-- StateVar
128132
import Data.StateVar
129-
( HasGetter(get), HasSetter, ($=!) )
133+
( HasGetter(get), HasSetter, StateVar(..), ($=!) )
130134

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

198202

203+
-- | Path to @.ini@ file. Set to 'Nothing' to disable automatic .ini
204+
-- loading/saving, if e.g. you want to manually load/save from memory.
205+
iniFilename :: StateVar (Maybe String)
206+
iniFilename = StateVar getter setter
207+
where
208+
getter = do
209+
cStr <- [C.exp| const char* { GetIO().IniFilename } |]
210+
if cStr == nullPtr then return Nothing else Just <$> peekCString cStr
211+
212+
setter = \case
213+
Nothing ->
214+
[C.block| void { GetIO().IniFilename = $(char* nullPtr); } |]
215+
216+
Just str -> do
217+
strPtr <- newCString str
218+
[C.block| void { GetIO().IniFilename = $(char* strPtr); } |]
219+
220+
199221
-- | Create demo window. Demonstrate most ImGui features. Call this to learn
200222
-- about the library! Try to make it always available in your application!
201223
showDemoWindow :: MonadIO m => m ()

0 commit comments

Comments
 (0)