2016-12-13 10:08:49 +00:00
|
|
|
{-# LANGUAGE RecordWildCards, BangPatterns #-}
|
2016-12-08 17:22:29 +00:00
|
|
|
|
|
|
|
-- | Module for drawing primitives
|
|
|
|
module Affection.Draw
|
2016-12-23 13:18:39 +00:00
|
|
|
(
|
|
|
|
-- drawRect
|
|
|
|
drawRect'
|
2016-12-11 11:24:02 +00:00
|
|
|
-- , clear
|
2016-12-11 16:38:03 +00:00
|
|
|
, handleDrawRequest
|
2016-12-11 19:24:16 +00:00
|
|
|
, invalidateDrawRequest
|
2016-12-21 03:28:57 +00:00
|
|
|
-- , present
|
2016-12-11 11:24:02 +00:00
|
|
|
, clearArea
|
2016-12-08 17:22:29 +00:00
|
|
|
) where
|
|
|
|
|
2016-12-11 16:38:03 +00:00
|
|
|
import Affection.Types
|
2016-12-08 17:22:29 +00:00
|
|
|
|
2016-12-23 13:18:39 +00:00
|
|
|
import Foreign
|
2016-12-08 17:22:29 +00:00
|
|
|
import Foreign.C.Types
|
|
|
|
|
2016-12-11 16:38:03 +00:00
|
|
|
import Control.Monad.State (get, put)
|
2016-12-21 03:28:57 +00:00
|
|
|
import Control.Monad (when, unless)
|
2016-12-11 16:38:03 +00:00
|
|
|
|
2016-12-23 13:18:39 +00:00
|
|
|
import System.Glib.GObject
|
|
|
|
|
2016-12-11 16:38:03 +00:00
|
|
|
import qualified SDL
|
|
|
|
|
2016-12-08 17:22:29 +00:00
|
|
|
import qualified BABL as B
|
|
|
|
|
|
|
|
import qualified GEGL as G
|
|
|
|
|
2016-12-11 19:24:16 +00:00
|
|
|
import Debug.Trace
|
|
|
|
|
2016-12-08 17:22:29 +00:00
|
|
|
-- | Draw a rectangle on target buffer
|
2016-12-23 13:18:39 +00:00
|
|
|
-- drawRect
|
|
|
|
-- :: G.GeglBuffer -- ^ Target buffer
|
|
|
|
-- -> G.GeglNode -- ^ Target node
|
|
|
|
-- -> G.Color -- ^ Color to draw in
|
|
|
|
-- -> DrawType -- ^ Draw type
|
|
|
|
-- -> GeglRectangle -- ^ Dimensions of Rectangle
|
|
|
|
-- -> Affection us ()
|
|
|
|
-- drawRect buf node color (Fill) rect@G.GeglRectangle{..} = do
|
|
|
|
-- liftIO $ G.pixelPoke buf rect (B.PixelFormat B.RGBA B.CFdouble) G.GeglAccessReadWrite G.GeglAbyssNone $
|
|
|
|
-- (\(x, y) ->
|
|
|
|
-- let col = unsafeColorize color
|
|
|
|
-- in
|
|
|
|
-- G.Pixel x y col
|
|
|
|
-- )
|
|
|
|
-- ad@AffectionData{..} <- get
|
|
|
|
-- put $ ad
|
|
|
|
-- { drawStack = (DrawRequest rect buf False) : drawStack
|
|
|
|
-- }
|
|
|
|
-- drawRect buf node color (Line size) rect@G.GeglRectangle{..} = do
|
|
|
|
-- liftIO $ G.pixelPoke buf rect (B.PixelFormat B.RGBA B.CFdouble) G.GeglAccessReadWrite G.GeglAbyssNone $
|
|
|
|
-- (\(x, y) ->
|
|
|
|
-- let col = unsafeColorize color
|
|
|
|
-- in if not ((x >= rectangleX + size && x < rectangleX + rectangleWidth - size) &&
|
|
|
|
-- (y >= rectangleY + size && y < rectangleY + rectangleHeight - size))
|
|
|
|
-- then
|
|
|
|
-- G.Pixel x y col
|
|
|
|
-- else
|
|
|
|
-- G.Pixel x y $ unsafeColorize $ G.RGBA 0 0 0 0
|
|
|
|
-- )
|
|
|
|
-- ad@AffectionData{..} <- get
|
|
|
|
-- put $ ad
|
|
|
|
-- { drawStack = (DrawRequest rect buf False) : drawStack
|
|
|
|
-- }
|
|
|
|
-- -- drawRect buf node color Fill (G.GeglRectangle rectangleX rectangleY rectangleWidth size)
|
|
|
|
-- -- drawRect buf node color Fill (G.GeglRectangle rectangleX rectangleY size rectangleHeight)
|
|
|
|
-- -- drawRect buf node color Fill (G.GeglRectangle (rectangleX + rectangleWidth - size) rectangleY size rectangleHeight)
|
|
|
|
-- -- drawRect buf node color Fill (G.GeglRectangle rectangleX (rectangleY + rectangleHeight - size) rectangleWidth size)
|
2016-12-11 16:38:03 +00:00
|
|
|
|
2016-12-21 03:28:57 +00:00
|
|
|
drawRect'
|
|
|
|
:: G.GeglNode -- ^ Target Node
|
|
|
|
-> G.Color -- ^ Color to draw in
|
|
|
|
-> DrawType -- ^ Draw type
|
|
|
|
-> G.GeglRectangle -- ^ Dimensions of Rectangle
|
|
|
|
-> G.GeglBuffer -- ^ Final Buffer
|
2016-12-11 16:38:03 +00:00
|
|
|
-> Affection us ()
|
2016-12-21 03:28:57 +00:00
|
|
|
drawRect' node color Fill rect@GeglRectangle{..} buf = do
|
2016-12-18 18:01:18 +00:00
|
|
|
ad <- get
|
2016-12-23 13:18:39 +00:00
|
|
|
tempRoot <- liftIO $ G.gegl_node_new
|
|
|
|
opNode <- liftIO $ G.gegl_node_new_child tempRoot $ G.Operation "gegl:rectangle"
|
2016-12-21 03:28:57 +00:00
|
|
|
[ G.Property "x" $ G.PropertyDouble $ fromIntegral rectangleX
|
|
|
|
, G.Property "y" $ G.PropertyDouble $ fromIntegral rectangleY
|
|
|
|
, G.Property "width" $ G.PropertyDouble $ fromIntegral rectangleWidth
|
|
|
|
, G.Property "height" $ G.PropertyDouble $ fromIntegral rectangleHeight
|
|
|
|
, G.Property "color" $ G.PropertyColor color
|
|
|
|
]
|
|
|
|
diw <- liftIO $ G.gegl_node_connect_to opNode "output" node "input"
|
|
|
|
unless diw $ error "Affection.Draw.drawRect': connect failed"
|
2016-12-11 16:38:03 +00:00
|
|
|
put $ ad
|
2016-12-23 13:18:39 +00:00
|
|
|
{ drawStack = (DrawRequest rect buf (Kill tempRoot)) : drawStack ad
|
2016-12-11 16:38:03 +00:00
|
|
|
}
|
2016-12-21 03:28:57 +00:00
|
|
|
|
|
|
|
-- -- | force a blit of a specified area. Do not use often as it slows down the program
|
|
|
|
-- present
|
|
|
|
-- :: G.GeglNode -- ^ Node to blit
|
|
|
|
-- -> G.GeglBuffer -- ^ BUffer to draw
|
|
|
|
-- -> G.GeglRectangle -- ^ Area to blit
|
|
|
|
-- -> Bool -- ^ Shall the drawing be cleared in the next run?
|
|
|
|
-- -> Affection us ()
|
|
|
|
-- present node buf rect pers = do
|
|
|
|
-- ad <- get
|
|
|
|
-- put $ ad
|
|
|
|
-- { drawStack = (DrawRequest rect buf pers) : drawStack ad
|
|
|
|
-- }
|
|
|
|
-- ad <- get
|
|
|
|
-- format <- liftIO $ B.babl_format $ B.PixelFormat B.RGBA B.CFu8
|
|
|
|
-- liftIO $ SDL.lockSurface $ drawSurface ad
|
|
|
|
-- liftIO $ G.gegl_node_blit
|
|
|
|
-- rect
|
|
|
|
-- format
|
|
|
|
-- nullPtr
|
|
|
|
-- 0
|
|
|
|
-- [G.GeglBlitDefault]
|
|
|
|
-- liftIO $ SDL.unlockSurface $ drawSurface ad
|
|
|
|
-- liftIO $ SDL.updateWindowSurface $ drawWindow ad
|
2016-12-11 16:38:03 +00:00
|
|
|
|
|
|
|
-- | function for handling 'DrawRequest's and updating the output
|
|
|
|
handleDrawRequest
|
|
|
|
:: Ptr a -- ^ Pixel buffer to blit to
|
2016-12-21 03:28:57 +00:00
|
|
|
-- -> B.BablFormatPtr -- ^ format to blit in
|
2016-12-11 16:38:03 +00:00
|
|
|
-> Int -- ^ Stride
|
|
|
|
-> Int -- ^ Components per Pixel
|
|
|
|
-> DrawRequest -- ^ 'DrawRequest' to handle
|
2016-12-11 19:24:16 +00:00
|
|
|
-> Affection us (Maybe DrawRequest)
|
2016-12-21 03:28:57 +00:00
|
|
|
handleDrawRequest pixels stride cpp dr@DrawRequest{..} = do
|
2016-12-11 19:24:16 +00:00
|
|
|
ad <- get
|
2016-12-20 03:15:30 +00:00
|
|
|
let surf = drawSurface ad
|
2016-12-13 10:08:49 +00:00
|
|
|
liftIO $ SDL.lockSurface surf
|
2016-12-21 03:28:57 +00:00
|
|
|
liftIO $ G.gegl_buffer_get
|
|
|
|
requestBuffer
|
|
|
|
(Just requestArea)
|
2016-12-11 19:24:16 +00:00
|
|
|
1
|
2016-12-21 03:28:57 +00:00
|
|
|
(Just $ drawFormat ad)
|
2016-12-11 19:24:16 +00:00
|
|
|
(pixels `plusPtr`
|
|
|
|
(rectangleX requestArea * cpp + rectangleY requestArea * stride))
|
2016-12-13 21:18:09 +00:00
|
|
|
stride
|
2016-12-21 03:28:57 +00:00
|
|
|
G.GeglAbyssNone
|
2016-12-13 10:08:49 +00:00
|
|
|
liftIO $ SDL.unlockSurface surf
|
2016-12-20 03:15:30 +00:00
|
|
|
-- liftIO $ SDL.updateWindowSurface $ drawWindow ad
|
2016-12-23 13:18:39 +00:00
|
|
|
case requestPersist of
|
|
|
|
Yes ->
|
|
|
|
return Nothing
|
|
|
|
Kill _ ->
|
|
|
|
return $ Just dr
|
2016-12-11 19:24:16 +00:00
|
|
|
|
|
|
|
-- | clear a previously drawn area
|
|
|
|
invalidateDrawRequest
|
|
|
|
:: Ptr a -- ^ Pixel buffer to blit to
|
2016-12-21 03:28:57 +00:00
|
|
|
-- -> B.BablFormatPtr -- ^ format to blit in
|
2016-12-11 19:24:16 +00:00
|
|
|
-> Int -- ^ Stride
|
|
|
|
-> Int -- ^ Components per Pixel
|
|
|
|
-> DrawRequest -- ^ Drawrequest to invalidate
|
2016-12-11 16:38:03 +00:00
|
|
|
-> Affection us ()
|
2016-12-21 03:28:57 +00:00
|
|
|
invalidateDrawRequest pixels stride cpp dr@DrawRequest{..} = do
|
2016-12-11 16:38:03 +00:00
|
|
|
ad <- get
|
2016-12-23 13:18:39 +00:00
|
|
|
let surf = drawSurface ad
|
2016-12-11 19:24:16 +00:00
|
|
|
liftIO $ clearArea requestBuffer requestArea
|
2016-12-13 10:08:49 +00:00
|
|
|
liftIO $ SDL.lockSurface surf
|
2016-12-21 03:28:57 +00:00
|
|
|
liftIO $ G.gegl_buffer_get
|
|
|
|
requestBuffer
|
|
|
|
(Just requestArea)
|
2016-12-11 16:38:03 +00:00
|
|
|
1
|
2016-12-21 03:28:57 +00:00
|
|
|
(Just $ drawFormat ad)
|
2016-12-11 16:38:03 +00:00
|
|
|
(pixels `plusPtr`
|
|
|
|
(rectangleX requestArea * cpp + rectangleY requestArea * stride))
|
2016-12-13 21:18:09 +00:00
|
|
|
stride
|
2016-12-21 03:28:57 +00:00
|
|
|
G.GeglAbyssNone
|
2016-12-13 10:08:49 +00:00
|
|
|
liftIO $ SDL.unlockSurface surf
|
2016-12-23 13:18:39 +00:00
|
|
|
let Kill victim = requestPersist
|
|
|
|
liftIO $ G.gegl_node_drop victim
|
2016-12-20 03:15:30 +00:00
|
|
|
-- liftIO $ SDL.updateWindowSurface $ drawWindow ad
|
2016-12-08 17:22:29 +00:00
|
|
|
|
|
|
|
-- | compute color for a single pixel
|
|
|
|
colorize
|
|
|
|
:: (G.ComponentValue, G.ComponentValue, G.ComponentValue, G.ComponentValue) -- ^ Pixel information in buffer
|
|
|
|
-> G.Color -- ^ Color to draw over
|
|
|
|
-> (G.ComponentValue, G.ComponentValue, G.ComponentValue, G.ComponentValue) -- ^ Resulting colour
|
|
|
|
colorize (rr, rg, rb, ra) col =
|
|
|
|
let (G.CVdouble (CDouble br)) = rr
|
|
|
|
(G.CVdouble (CDouble bg)) = rg
|
|
|
|
(G.CVdouble (CDouble bb)) = rb
|
|
|
|
(G.CVdouble (CDouble ba)) = ra
|
|
|
|
(cr, cg, cb) = case col of
|
|
|
|
G.RGBA r g b _ -> (r, g, b)
|
|
|
|
G.RGB r g b -> (r, g, b)
|
|
|
|
ca = case col of
|
|
|
|
G.RGBA _ _ _ a -> a
|
|
|
|
G.RGB _ _ _ -> 1
|
|
|
|
alpha = ca
|
|
|
|
dst_a = ba
|
|
|
|
da = alpha + dst_a * (1 - alpha)
|
|
|
|
a_term = dst_a * (1 - alpha)
|
|
|
|
red = cr * alpha + br * a_term
|
|
|
|
gre = cg * alpha + bg * a_term
|
|
|
|
blu = cb * alpha + bb * a_term
|
|
|
|
in
|
|
|
|
( G.CVdouble $ CDouble $ red / da
|
|
|
|
, G.CVdouble $ CDouble $ gre / da
|
|
|
|
, G.CVdouble $ CDouble $ blu / da
|
|
|
|
, G.CVdouble $ CDouble $ ca
|
|
|
|
)
|
2016-12-10 22:49:51 +00:00
|
|
|
|
|
|
|
unsafeColorize col =
|
|
|
|
let
|
|
|
|
(r, g, b) = case col of
|
|
|
|
G.RGBA cr cg cb _ -> (cr, cg, cb)
|
|
|
|
G.RGB cr cg cb -> (cr, cg, cb)
|
|
|
|
a = case col of
|
|
|
|
G.RGBA _ _ _ ca -> ca
|
|
|
|
G.RGB _ _ _ -> 1
|
|
|
|
in
|
|
|
|
( G.CVdouble $ CDouble $ r
|
|
|
|
, G.CVdouble $ CDouble $ g
|
|
|
|
, G.CVdouble $ CDouble $ b
|
|
|
|
, G.CVdouble $ CDouble $ a
|
|
|
|
)
|
2016-12-11 11:24:02 +00:00
|
|
|
|
|
|
|
-- -- | Clear all data from a buffer
|
|
|
|
-- clear
|
|
|
|
-- :: G.GeglBuffer -- ^ Target buffer
|
|
|
|
-- -> IO ()
|
|
|
|
-- clear buf = clearArea buf =<< G.gegl_rectangle_infinite_plane
|
|
|
|
|
|
|
|
-- | Clear a specified area of a buffer from all data
|
|
|
|
clearArea
|
|
|
|
:: G.GeglBuffer -- ^ Target buffer
|
|
|
|
-> G.GeglRectangle -- ^ Area to clear
|
|
|
|
-> IO ()
|
|
|
|
clearArea = G.gegl_buffer_clear
|