{-# LANGUAGE BlockArguments #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE TypeApplications #-} module MetaBrush.Document ( AABB(..) , Document(..), emptyDocument, currentDocument , Stroke(..) , PointData(..), BrushPointData(..) , FocusState(..) , _selection, _brush ) where -- base import GHC.Generics ( Generic ) -- containers import Data.Map.Strict ( Map ) import qualified Data.Map.Strict as Map ( lookup ) import Data.Sequence ( Seq ) -- generic-lens import Data.Generics.Product.Fields ( field' ) import Data.Generics.Product.Typed ( HasType(typed) ) -- lens import Control.Lens ( Lens' ) -- text import Data.Text ( Text ) -- stm import qualified Control.Concurrent.STM as STM ( atomically ) import qualified Control.Concurrent.STM.TVar as STM ( TVar, readTVar ) -- MetaBrush import Math.Bezier.Stroke ( StrokePoint(..) ) import Math.Vector2D ( Point2D(..) ) import MetaBrush.Unique ( Unique ) -------------------------------------------------------------------------------- data AABB = AABB { topLeft, botRight :: !( Point2D Double ) } deriving stock Show data Document = Document { displayName :: !Text , mbFilePath :: !( Maybe FilePath ) , unsavedChanges :: !Bool , viewportCenter :: !( Point2D Double ) , zoomFactor :: !Double , documentUnique :: Unique , strokes :: ![ Stroke ] } deriving stock ( Show, Generic ) data Stroke = Stroke { strokeName :: Text , strokeVisible :: !Bool , strokeUnique :: Unique , strokePoints :: !( Seq ( StrokePoint PointData ) ) } deriving stock ( Show, Generic ) data PointData = PointData { pointState :: FocusState , brushShape :: Seq ( StrokePoint BrushPointData ) } deriving stock ( Show, Generic ) data BrushPointData = BrushPointData { brushPointState :: FocusState } deriving stock ( Show, Generic ) data FocusState = Normal | Hover | Selected deriving stock ( Show, Eq ) _selection :: HasType FocusState pt => Lens' ( StrokePoint pt ) FocusState _selection = field' @"pointData" . typed @FocusState _brush :: Lens' ( StrokePoint PointData ) ( Seq ( StrokePoint BrushPointData ) ) _brush = field' @"pointData" . field' @"brushShape" currentDocument :: STM.TVar ( Maybe Unique ) -> STM.TVar ( Map Unique Document ) -> IO ( Maybe Document ) currentDocument activeDocumentTVar openDocumentsTVar = STM.atomically do mbActive <- STM.readTVar activeDocumentTVar case mbActive of Nothing -> pure Nothing Just unique -> do docs <- STM.readTVar openDocumentsTVar pure ( Map.lookup unique docs ) emptyDocument :: Text -> Unique -> Document emptyDocument docName unique = Document { displayName = docName , mbFilePath = Nothing , unsavedChanges = False , viewportCenter = Point2D 0 0 , zoomFactor = 1 , documentUnique = unique , strokes = [] }