You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
4.2 KiB

  1. {-# LANGUAGE OverloadedStrings #-}
  2. module Board where
  3. import Raylib.Types (Vector2(..), Color(..))
  4. import Data.Aeson
  5. import Data.Aeson.Types
  6. newtype BoardColor = BoardColor { unBoardColor :: Color } deriving Show
  7. newtype Point = Point { unPoint :: Vector2 } deriving Show
  8. data Path = Path { path'points :: [Point]
  9. , path'thickness :: Float
  10. , path'color :: BoardColor
  11. } deriving Show
  12. data BackgroundPattern = BGPatternPlain { bg'fillColor :: BoardColor }
  13. | BGPatternDots { bg'fillColor :: BoardColor
  14. , bg'drawColor :: BoardColor
  15. , bg'hSpacing :: Int
  16. , bg'vSpacing :: Int
  17. , bg'thickness :: Float
  18. }
  19. | BGPatternGrid { bg'fillColor :: BoardColor
  20. , bg'drawColor :: BoardColor
  21. , bg'hSpacing :: Int
  22. , bg'vSpacing :: Int
  23. , bg'thickness :: Float
  24. }
  25. deriving Show
  26. data Board = Board { boardTitle :: String
  27. , boardBackground :: BackgroundPattern
  28. , boardPaths :: [Path]
  29. } deriving Show
  30. instance ToJSON BoardColor where
  31. toJSON (BoardColor c) =
  32. object [ "r" .= color'r c, "g" .= color'g c, "b" .= color'b c, "a" .= color'a c ]
  33. instance FromJSON BoardColor where
  34. parseJSON = withObject "color" $ \o -> do
  35. c <- Color <$> o .: "r"
  36. <*> o .: "g"
  37. <*> o .: "b"
  38. <*> o .: "a"
  39. return $ BoardColor c
  40. instance ToJSON Point where
  41. toJSON (Point v) =
  42. object [ "x" .= vector2'x v, "y" .= vector2'y v ]
  43. instance FromJSON Point where
  44. parseJSON = withObject "point" $ \o -> do
  45. v2 <- Vector2 <$> o .: "x"
  46. <*> o .: "y"
  47. return $ Point v2
  48. instance ToJSON Path where
  49. toJSON (Path pts thk clr) =
  50. object [ "points" .= pts
  51. , "thickness" .= thk
  52. , "color" .= clr
  53. ]
  54. instance FromJSON Path where
  55. parseJSON = withObject "path" $ \o ->
  56. Path <$> o .: "thickness"
  57. <*> o .: "color"
  58. <*> o .: "points"
  59. instance ToJSON BackgroundPattern where
  60. toJSON (BGPatternPlain fc) =
  61. object [ "type" .= ("plain" :: String), "fillColor" .= fc ]
  62. toJSON (BGPatternDots fc dc hs vs th) =
  63. object [ "type" .= ("dots" :: String)
  64. , "fillColor" .= fc
  65. , "drawColor" .= dc
  66. , "hSpacing" .= hs
  67. , "vSpacing" .= vs
  68. , "thickness" .= th
  69. ]
  70. toJSON (BGPatternGrid fc dc hs vs th) =
  71. object [ "type" .= ("grid" :: String)
  72. , "fillColor" .= fc
  73. , "drawColor" .= dc
  74. , "hSpacing" .= hs
  75. , "vSpacing" .= vs
  76. , "thickness" .= th
  77. ]
  78. instance FromJSON BackgroundPattern where
  79. parseJSON = withObject "backgroundPattern" $ \o -> do
  80. pType <- (o .: "type" :: Parser String)
  81. case pType of
  82. "plain" -> BGPatternPlain <$> o .: "fillColor"
  83. "grid" -> BGPatternGrid <$> o .: "fillColor"
  84. <*> o .: "drawColor"
  85. <*> o .: "hSpacing"
  86. <*> o .: "vSpacing"
  87. <*> o .: "thickness"
  88. "dots" -> BGPatternDots <$> o .: "fillColor"
  89. <*> o .: "drawColor"
  90. <*> o .: "hSpacing"
  91. <*> o .: "vSpacing"
  92. <*> o .: "thickness"
  93. x -> error $ "Unknown background type: " ++ x
  94. instance ToJSON Board where
  95. toJSON (Board t b p) = object ["title" .= t , "background" .= b, "paths" .= p]
  96. instance FromJSON Board where
  97. parseJSON = withObject "board" $ \o ->
  98. Board <$> o .: "title"
  99. <*> o .: "background"
  100. <*> o .: "paths"