Skip to content

Instantly share code, notes, and snippets.

@sheaf
Last active June 20, 2021 22:07
Show Gist options
  • Select an option

  • Save sheaf/995ee73e842b0a3887142e9887d5cd70 to your computer and use it in GitHub Desktop.

Select an option

Save sheaf/995ee73e842b0a3887142e9887d5cd70 to your computer and use it in GitHub Desktop.
data Controller = Slider | Toggle
data Ref = Value | Ref
type InputData :: Ref -> Type
type InputData ref =
Struct
'[ "mousePos" ':-> V 2 Float
, "scancodes" ':-> Array 512 Word32
-- , screensize ...
-- , time ...
, "imguiData" ':-> ImguiData ref
]
type family Apply ref a where
Apply 'Value a = a
Apply 'Ref a = ( String, Controller, IORef a )
type ImguiData :: Ref -> Type
type ImguiData ref =
Struct
'[ "slider1" ':-> Apply ref Float
, "slider2" ':-> Apply ref Float
]
type ReadRefs :: [ Symbol :-> Type ] -> [ Symbol :-> Type ] -> Constraint
class ReadRefs as bs | bs -> as, as -> bs where
readRefs :: Struct as -> IO ( Struct bs )
instance ReadRefs '[] '[] where
readRefs _ = pure End
instance ( ReadRefs as bs, k1 ~ k2, a ~ ( String, Controller, IORef b ) ) =>
ReadRefs ( (k1 ':-> a) ': as ) ( (k2 ':-> b) ': bs ) where
readRefs ( ( _, _, ref ) :& refs ) = do
val <- readIORef ref
vals <- readRefs @as @bs refs
pure ( val :& vals )
class CreateRefs as where
createRefs :: IO ( Struct as )
instance CreateRefs '[] where
createRefs = pure End
instance CreateRefs as => CreateRefs ( (k ':-> a) ': as ) where
createRefs = do
ref <- newIORef @a
refs <- createRefs
class CreateControllers as where
createControllers :: IO ( Struct as ) -> IO ()
instance CreateControllers '[] where
createControllers _ = pure ()
instance ( a ~ ( String, Controller, IORef Float ), CreateControllers as ) => CreateControllers ( ( k ':-> a ) ': as ) where
createControllers ( (controllerName, controllerType, ref) :& as ) = do
createController controllerName controllerType ref
createControllers as
createController :: String -> Controller -> IORef Float -> IO ()
createController controllerName controllerType ref =
case controllerType of
Slider -> do
-- include Dear ImGui code for creating a slider
Toggle -> do
-- toggle...
readImguiData :: ImguiData Ref -> IO ( ImguiData Val )
readImguiData = readRefs
zeroInputData :: InputData Val
zeroInputData = V2 0 0 :& Prelude.pure 0 :& imguiStruct :& End
where
imguiStruct = ...
{-# NOINLINE zeroInputRefs #-}
zeroInputRefs :: InputData Ref
zeroInputRefs = unsafePerformIO do
slider1Ref <- newIORef @Float 0
slider2Ref <- newIORef @Float 0
let
imguiStruct :: ImguiData Ref
imguiStruct
= ( "Slider 1", Slider, slider1Ref )
:& ( "Slider 2", Slider, slider2Ref )
:& End
pure $ V2 0 0 :& Prelude.pure 0 :& imguiStruct :& End
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment