diff --git a/affection.cabal b/affection.cabal index cd3dd3f..49f955e 100644 --- a/affection.cabal +++ b/affection.cabal @@ -41,6 +41,7 @@ library , Affection.Types , Affection.StateMachine , Affection.MouseInteractable + , Affection.Property default-extensions: OverloadedStrings -- Modules included in this library but not exported. diff --git a/examples/example00.hs b/examples/example00.hs index 5e9f7a3..551c881 100644 --- a/examples/example00.hs +++ b/examples/example00.hs @@ -46,18 +46,18 @@ load _ = do traceM "loading" root <- G.gegl_node_new traceM "new root node" - checkerboard <- G.gegl_node_new_child root $ G.checkerboardOperation - [ G.Property "color1" $ G.PropertyColor $ G.RGBA 0.4 0.4 0.4 1 - , G.Property "color2" $ G.PropertyColor $ G.RGBA 0.6 0.6 0.6 1 - ] + checkerboard <- G.gegl_node_new_child root $ G.checkerboardOperation $ + props $ do + prop "color1" $ G.RGBA 0.4 0.4 0.4 1 + prop "color2" $ G.RGBA 0.6 0.6 0.6 1 traceM "checkerboard" over <- G.gegl_node_new_child root G.defaultOverOperation traceM "over" - text <- G.gegl_node_new_child root $ G.textOperation - [ G.Property "string" $ G.PropertyString "Hello world!" - , G.Property "color" $ G.PropertyColor $ G.RGBA 0 0 1 0.5 - , G.Property "size" $ G.PropertyDouble 40 - ] + text <- G.gegl_node_new_child root $ G.textOperation $ + props $ do + prop "string" ("Hello world!"::String) + prop "color" $ G.RGBA 0 0 1 0.5 + prop "size" (40::Int) traceM "text" G.gegl_node_link checkerboard over G.gegl_node_connect_to text "output" over "aux" diff --git a/examples/example01.hs b/examples/example01.hs index 0acefcc..3c64519 100644 --- a/examples/example01.hs +++ b/examples/example01.hs @@ -48,25 +48,25 @@ load _ = do traceM "loading" root <- G.gegl_node_new traceM "new root node" - checkerboard <- G.gegl_node_new_child root $ G.checkerboardOperation - [ G.Property "color1" $ G.PropertyColor $ G.RGBA 0.4 0.4 0.4 1 - , G.Property "color2" $ G.PropertyColor $ G.RGBA 0.6 0.6 0.6 1 - ] + checkerboard <- G.gegl_node_new_child root $ G.checkerboardOperation $ + props $ do + prop "color1" $ G.RGBA 0.4 0.4 0.4 1 + prop "color2" $ G.RGBA 0.6 0.6 0.6 1 traceM "checkerboard" over <- G.gegl_node_new_child root G.defaultOverOperation traceM "over" buffer <- G.gegl_buffer_new (Just $ G.GeglRectangle 0 0 800 600) =<< B.babl_format (B.PixelFormat B.RGBA B.CFfloat) - sink <- G.gegl_node_new_child root $ G.Operation "gegl:copy-buffer" - [ G.Property "buffer" $ G.PropertyBuffer buffer - ] + sink <- G.gegl_node_new_child root $ G.Operation "gegl:copy-buffer" $ + props $ + prop "buffer" buffer traceM "buffer-source" nop <- G.gegl_node_new_child root $ G.Operation "gegl:nop" [] traceM "nop" - crop <- G.gegl_node_new_child root $ G.Operation "gegl:crop" - [ G.Property "width" $ G.PropertyDouble 800 - , G.Property "height" $ G.PropertyDouble 600 - ] + crop <- G.gegl_node_new_child root $ G.Operation "gegl:crop" $ + props $ do + prop "width" (800::Double) + prop "height" (600.0::Double) G.gegl_node_link_many [checkerboard, over, crop, sink] G.gegl_node_connect_to nop "output" over "aux" traceM "connections made" diff --git a/examples/example02.hs b/examples/example02.hs index c04b50a..934d812 100644 --- a/examples/example02.hs +++ b/examples/example02.hs @@ -34,31 +34,31 @@ load _ = do traceM "loading" root <- G.gegl_node_new traceM "new root node" - checkerboard <- G.gegl_node_new_child root $ G.checkerboardOperation - [ G.Property "color1" $ G.PropertyColor $ G.RGBA 0.4 0.4 0.4 1 - , G.Property "color2" $ G.PropertyColor $ G.RGBA 0.6 0.6 0.6 1 - ] + checkerboard <- G.gegl_node_new_child root $ G.checkerboardOperation $ + props $ do + prop "color1" $ G.RGBA 0.4 0.4 0.4 1 + prop "color2" $ G.RGBA 0.6 0.6 0.6 1 traceM "checkerboard" over <- G.gegl_node_new_child root G.defaultOverOperation traceM "over" buffer <- G.gegl_buffer_new (Just $ G.GeglRectangle 0 0 800 600) =<< B.babl_format (B.PixelFormat B.RGBA B.CFfloat) - sink <- G.gegl_node_new_child root $ G.Operation "gegl:copy-buffer" - [ G.Property "buffer" $ G.PropertyBuffer buffer - ] + sink <- G.gegl_node_new_child root $ G.Operation "gegl:copy-buffer" $ + props $ + prop "buffer" buffer traceM "buffer-sink" - rect <- G.gegl_node_new_child root $ G.Operation "gegl:rectangle" - [ G.Property "x" $ G.PropertyDouble 0 - , G.Property "y" $ G.PropertyDouble 0 - , G.Property "width" $ G.PropertyDouble 20 - , G.Property "height" $ G.PropertyDouble 20 - , G.Property "color" $ G.PropertyColor $ G.RGBA 1 0 0 0.5 - ] + rect <- G.gegl_node_new_child root $ G.Operation "gegl:rectangle" $ + props $ do + prop "x" (0::Double) + prop "y" (0::Double) + prop "width" (20::Double) + prop "height" (20::Double) + prop "color" $ G.RGBA 1 0 0 0.5 traceM "rect" - crop <- G.gegl_node_new_child root $ G.Operation "gegl:crop" - [ G.Property "width" $ G.PropertyDouble 800 - , G.Property "height" $ G.PropertyDouble 600 - ] + crop <- G.gegl_node_new_child root $ G.Operation "gegl:crop" $ + props $ do + prop "width" (800::Double) + prop "height" (600::Double) G.gegl_node_link_many [checkerboard, over, crop, sink] _ <- G.gegl_node_connect_to rect "output" over "aux" traceM "connections made" @@ -103,10 +103,10 @@ update = do handle (SDL.MouseMotionEvent dat) = do let (SDL.P (SDL.V2 x y)) = SDL.mouseMotionEventPos dat ud <- getAffection - liftIO $ G.gegl_node_set (nodeGraph ud M.! "rect") $ G.Operation "" - [ G.Property "x" $ G.PropertyDouble $ fromIntegral (x - 10) - , G.Property "y" $ G.PropertyDouble $ fromIntegral (y - 10) - ] + liftIO $ G.gegl_node_set (nodeGraph ud M.! "rect") $ G.Operation "" $ + props $ do + prop "x" (fromIntegral (x - 10) :: Double) + prop "y" $ (fromIntegral (y - 10) :: Double) handle (SDL.WindowClosedEvent _) = do traceM "seeya!" diff --git a/src/Affection.hs b/src/Affection.hs index 9ae35b7..f42d519 100644 --- a/src/Affection.hs +++ b/src/Affection.hs @@ -13,6 +13,7 @@ module Affection , getDelta , quit , module A + , module Affection.Property ) where import qualified SDL @@ -38,6 +39,7 @@ import Affection.Draw as A import Affection.Particle as A import Affection.StateMachine as A import Affection.MouseInteractable as A +import Affection.Property import qualified BABL as B diff --git a/src/Affection/Property.hs b/src/Affection/Property.hs new file mode 100644 index 0000000..dfebc79 --- /dev/null +++ b/src/Affection/Property.hs @@ -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 Foreign.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