diff options
author | stuebinm | 2021-12-02 02:28:23 +0100 |
---|---|---|
committer | stuebinm | 2021-12-02 16:00:54 +0100 |
commit | 7d8c66b4c3ffd610ef0da98c3f2ff8626f1c8af6 (patch) | |
tree | 1fd37b223dea6001c421aa17471d5108d2eb4e0a /lib/Tiled2.hs | |
parent | c2a49d6ea46c38f107ac1a47a965e4777be2aecc (diff) |
collect badges from object layers
this includes a halfway-reasonable parsing of object layers, as well as
some monad plumbing to get them all in the right place.
Diffstat (limited to '')
-rw-r--r-- | lib/Tiled2.hs | 108 |
1 files changed, 73 insertions, 35 deletions
diff --git a/lib/Tiled2.hs b/lib/Tiled2.hs index 7924d3e..7e8f773 100644 --- a/lib/Tiled2.hs +++ b/lib/Tiled2.hs @@ -36,8 +36,9 @@ aesonOptions :: Int -> Options aesonOptions l = defaultOptions { omitNothingFields = True , rejectUnknownFields = True - -- can't be bothered to do a nixer prefix strip + -- can't be bothered to do a nicer prefix strip , fieldLabelModifier = drop l . map toLower + , sumEncoding = UntaggedValue } -- | A globally indexed identifier. @@ -97,8 +98,8 @@ instance ToJSON Property where , "name" .= name , "value" .= int] -data Point = Point { pointX :: Int - , pointY :: Int +data Point = Point { pointX :: Double + , pointY :: Double } deriving (Eq, Generic, Show) instance FromJSON Point where @@ -106,37 +107,74 @@ instance FromJSON Point where instance ToJSON Point where toJSON = genericToJSON (aesonOptions 5) -data Object = Object { objectId :: Int - -- ^ Incremental id - unique across all objects - , objectWidth :: Double - -- ^ Width in pixels. Ignored if using a gid. - , objectHeight :: Double - -- ^ Height in pixels. Ignored if using a gid. - , objectName :: Maybe String - -- ^ String assigned to name field in editor - , objectType :: String - -- ^ String assigned to type field in editor - , objectProperties :: Maybe Value - -- ^ String key-value pairs - , objectVisible :: Maybe Bool - -- ^ Whether object is shown in editor. - , objectX :: Double - -- ^ x coordinate in pixels - , objectY :: Double - -- ^ y coordinate in pixels - , objectRotation :: Float - -- ^ Angle in degrees clockwise - , objectGid :: Maybe GlobalId - -- ^ GID, only if object comes from a Tilemap - , objectEllipse :: Maybe Bool - -- ^ Used to mark an object as an ellipse - , objectPolygon :: Maybe (Vector Point) - -- ^ A list of x,y coordinates in pixels - , objectPolyline :: Maybe (Vector Point) - -- ^ A list of x,y coordinates in pixels - , objectText :: Maybe Value - -- ^ String key-value pairs - } deriving (Eq, Generic, Show) + +-- | all kinds of objects that can occur in object layers, even +-- | those that we don't want to allow. +data Object = ObjectPoint + { objectId :: Int + , objectName :: Maybe String + , objectProperties :: Maybe (Vector Property) + , objectVisible :: Maybe Bool + , objectX :: Double + , objectY :: Double + , objectHeight :: Double + , objectWidth :: Double + , objectRotation :: Double + , objectGid :: Maybe GlobalId + , objectText :: Maybe Text + , objectType :: Text + , objectPoint :: Bool + } + | ObjectRectangle + { objectId :: Int + , objectName :: Maybe String + , objectProperties :: Maybe (Vector Property) + , objectVisible :: Maybe Bool + , objectX :: Double + , objectY :: Double + , objectRotation :: Double + , objectGid :: Maybe GlobalId + , objectText :: Maybe Text + , objectWidth :: Double + , objectHeight :: Double + , objectEllipse :: Maybe Bool + , objectType :: Text + } + | ObjectPolygon + { objectId :: Int + , objectName :: Maybe String + , objectProperties :: Maybe (Vector Property) + , objectVisible :: Maybe Bool + , objectX :: Double + , objectY :: Double + , objectRotation :: Double + , objectGid :: Maybe GlobalId + , objectText :: Maybe Text + , objectWidth :: Double + , objectHeight :: Double + , objectType :: Text + , objectPolygon :: Vector Point + } + | ObjectPolyline + { objectId :: Int + , objectName :: Maybe String + , objectProperties :: Maybe (Vector Property) + , objectVisible :: Maybe Bool + , objectX :: Double + , objectY :: Double + , objectRotation :: Double + , objectGid :: Maybe GlobalId + , objectText :: Maybe Text + , objectWidth :: Double + , objectType :: Text + , objectHeight :: Double + , objectPolyline :: Vector Point + } deriving (Eq, Generic, Show) + + + + + instance FromJSON Object where parseJSON = genericParseJSON (aesonOptions 6) @@ -150,7 +188,7 @@ data Layer = Layer { layerWidth :: Maybe Double -- ^ Row count. Same as map height for fixed-size maps. , layerName :: Text -- ^ Name assigned to this layer - , layerType :: String + , layerType :: Text -- ^ “tilelayer”, “objectgroup”, or “imagelayer” , layerVisible :: Bool -- ^ Whether layer is shown or hidden in editor |