implement Affection.Property dsl

This commit is contained in:
Astro 2017-03-16 23:51:41 +01:00
parent ba454d341d
commit a737e2863c
5 changed files with 65 additions and 20 deletions

View file

@ -41,6 +41,7 @@ library
, Affection.Types , Affection.Types
, Affection.StateMachine , Affection.StateMachine
, Affection.MouseInteractable , Affection.MouseInteractable
, Affection.Property
default-extensions: OverloadedStrings default-extensions: OverloadedStrings
-- Modules included in this library but not exported. -- Modules included in this library but not exported.

View file

@ -46,18 +46,18 @@ load _ = do
traceM "loading" traceM "loading"
root <- G.gegl_node_new root <- G.gegl_node_new
traceM "new root node" traceM "new root node"
checkerboard <- G.gegl_node_new_child root $ G.checkerboardOperation checkerboard <- G.gegl_node_new_child root $ G.checkerboardOperation $
[ G.Property "color1" $ G.PropertyColor $ G.RGBA 0.4 0.4 0.4 1 props $ do
, G.Property "color2" $ G.PropertyColor $ G.RGBA 0.6 0.6 0.6 1 prop "color1" $ G.RGBA 0.4 0.4 0.4 1
] prop "color2" $ G.RGBA 0.6 0.6 0.6 1
traceM "checkerboard" traceM "checkerboard"
over <- G.gegl_node_new_child root G.defaultOverOperation over <- G.gegl_node_new_child root G.defaultOverOperation
traceM "over" traceM "over"
text <- G.gegl_node_new_child root $ G.textOperation text <- G.gegl_node_new_child root $ G.textOperation $
[ G.Property "string" $ G.PropertyString "Hello world!" props $ do
, G.Property "color" $ G.PropertyColor $ G.RGBA 0 0 1 0.5 prop "string" ("Hello world!"::String)
, G.Property "size" $ G.PropertyDouble 40 prop "color" $ G.RGBA 0 0 1 0.5
] prop "size" (40::Int)
traceM "text" traceM "text"
G.gegl_node_link checkerboard over G.gegl_node_link checkerboard over
G.gegl_node_connect_to text "output" over "aux" G.gegl_node_connect_to text "output" over "aux"

View file

@ -48,25 +48,25 @@ load _ = do
traceM "loading" traceM "loading"
root <- G.gegl_node_new root <- G.gegl_node_new
traceM "new root node" traceM "new root node"
checkerboard <- G.gegl_node_new_child root $ G.checkerboardOperation checkerboard <- G.gegl_node_new_child root $ G.checkerboardOperation $
[ G.Property "color1" $ G.PropertyColor $ G.RGBA 0.4 0.4 0.4 1 props $ do
, G.Property "color2" $ G.PropertyColor $ G.RGBA 0.6 0.6 0.6 1 prop "color1" $ G.RGBA 0.4 0.4 0.4 1
] prop "color2" $ G.RGBA 0.6 0.6 0.6 1
traceM "checkerboard" traceM "checkerboard"
over <- G.gegl_node_new_child root G.defaultOverOperation over <- G.gegl_node_new_child root G.defaultOverOperation
traceM "over" traceM "over"
buffer <- G.gegl_buffer_new (Just $ G.GeglRectangle 0 0 800 600) =<< buffer <- G.gegl_buffer_new (Just $ G.GeglRectangle 0 0 800 600) =<<
B.babl_format (B.PixelFormat B.RGBA B.CFfloat) B.babl_format (B.PixelFormat B.RGBA B.CFfloat)
sink <- G.gegl_node_new_child root $ G.Operation "gegl:copy-buffer" sink <- G.gegl_node_new_child root $ G.Operation "gegl:copy-buffer" $
[ G.Property "buffer" $ G.PropertyBuffer buffer props $
] prop "buffer" buffer
traceM "buffer-source" traceM "buffer-source"
nop <- G.gegl_node_new_child root $ G.Operation "gegl:nop" [] nop <- G.gegl_node_new_child root $ G.Operation "gegl:nop" []
traceM "nop" traceM "nop"
crop <- G.gegl_node_new_child root $ G.Operation "gegl:crop" crop <- G.gegl_node_new_child root $ G.Operation "gegl:crop" $
[ G.Property "width" $ G.PropertyDouble 800 props $ do
, G.Property "height" $ G.PropertyDouble 600 prop "width" (800::Double)
] prop "height" (600.0::Double)
G.gegl_node_link_many [checkerboard, over, crop, sink] G.gegl_node_link_many [checkerboard, over, crop, sink]
G.gegl_node_connect_to nop "output" over "aux" G.gegl_node_connect_to nop "output" over "aux"
traceM "connections made" traceM "connections made"

View file

@ -13,6 +13,7 @@ module Affection
, getDelta , getDelta
, quit , quit
, module A , module A
, module Affection.Property
) where ) where
import qualified SDL import qualified SDL
@ -38,6 +39,7 @@ import Affection.Draw as A
import Affection.Particle as A import Affection.Particle as A
import Affection.StateMachine as A import Affection.StateMachine as A
import Affection.MouseInteractable as A import Affection.MouseInteractable as A
import Affection.Property
import qualified BABL as B import qualified BABL as B

42
src/Affection/Property.hs Normal file
View file

@ -0,0 +1,42 @@
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
module Affection.Property where
import Control.Monad.State.Lazy
import qualified GEGL as G
import qualified BABL as B
import GHC.Ptr (Ptr)
type Props a = State [G.Property] a
props :: Props a -> [G.Property]
props = flip execState []
prop :: IsPropertyValue v => String -> v -> Props ()
prop k v = do
ps <- get
put $ (G.Property k (toPropertyValue v)) : ps
class IsPropertyValue v where
toPropertyValue :: v -> G.PropertyValue
instance IsPropertyValue Int where
toPropertyValue = G.PropertyInt
instance IsPropertyValue String where
toPropertyValue = G.PropertyString
instance IsPropertyValue Double where
toPropertyValue = G.PropertyDouble
instance IsPropertyValue G.Color where
toPropertyValue = G.PropertyColor
instance IsPropertyValue B.PixelFormat where
toPropertyValue = G.PropertyFormat
instance IsPropertyValue G.GeglBuffer where
toPropertyValue = G.PropertyBuffer
instance IsPropertyValue (Ptr ()) where
toPropertyValue = G.PropertyPointer