Skip to content

Commit 499d1c7

Browse files
authored
Refresh the code (#5)
* Update for recent Tiled format * Fix for new GHC, text and aeson
1 parent f8b247e commit 499d1c7

File tree

3 files changed

+66
-17
lines changed

3 files changed

+66
-17
lines changed

aeson-tiled.cabal

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: aeson-tiled
2-
version: 0.0.0.1
2+
version: 0.0.1.0
33
synopsis: Aeson instances for the Tiled map editor.
44
description: The mighty Tiled 2d map editor is an open source
55
app for creating tile based level maps. This package provides
@@ -20,9 +20,9 @@ library
2020
exposed-modules: Data.Aeson.Tiled
2121
build-depends: base >= 4.7 && < 5
2222
, bytestring >= 0.10 && < 1
23-
, aeson >= 1.0 && < 2
23+
, aeson >= 1.0 && < 3
2424
, containers >= 0.5 && < 1
25-
, text >= 1.2 && < 2
25+
, text >= 1.2 && < 3
2626
, vector >= 0.11 && < 1
2727
default-language: Haskell2010
2828

src/Data/Aeson/Tiled.hs

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE DeriveGeneric #-}
23
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
34
{-# LANGUAGE OverloadedStrings #-}
@@ -68,7 +69,11 @@ omitNulls (A.Object hs) = A.Object
6869
$ toList hs
6970
omitNulls x = x
7071

72+
#if MIN_VERSION_aeson(2,0,0)
73+
parseDefault :: FromJSON a => A.Object -> A.Key -> a -> Parser a
74+
#else
7175
parseDefault :: FromJSON a => A.Object -> Text -> a -> Parser a
76+
#endif
7277
parseDefault o s d = fromMaybe d <$> o .:? s
7378

7479

@@ -78,9 +83,9 @@ data Object = Object { objectId :: Int
7883
-- ^ Width in pixels. Ignored if using a gid.
7984
, objectHeight :: Double
8085
-- ^ Height in pixels. Ignored if using a gid.
81-
, objectName :: String
86+
, objectName :: Text
8287
-- ^ String assigned to name field in editor
83-
, objectType :: String
88+
, objectType :: Text
8489
-- ^ String assigned to type field in editor
8590
, objectProperties :: Map Text Text
8691
-- ^ String key-value pairs
@@ -146,9 +151,9 @@ data Layer = Layer { layerWidth :: Double
146151
-- ^ Column count. Same as map width for fixed-size maps.
147152
, layerHeight :: Double
148153
-- ^ Row count. Same as map height for fixed-size maps.
149-
, layerName :: String
154+
, layerName :: Text
150155
-- ^ Name assigned to this layer
151-
, layerType :: String
156+
, layerType :: Text -- TODO: LayerType
152157
-- ^ “tilelayer”, “objectgroup”, or “imagelayer”
153158
, layerVisible :: Bool
154159
-- ^ Whether layer is shown or hidden in editor
@@ -164,7 +169,7 @@ data Layer = Layer { layerWidth :: Double
164169
-- ^ string key-value pairs.
165170
, layerOpacity :: Float
166171
-- ^ Value between 0 and 1
167-
, layerDraworder :: String
172+
, layerDraworder :: Text -- TODO: DrawOrder
168173
-- ^ “topdown” (default) or “index”. objectgroup only.
169174
} deriving (Eq, Generic, Show)
170175

@@ -200,7 +205,7 @@ instance ToJSON Layer where
200205
]
201206

202207

203-
data Terrain = Terrain { terrainName :: String
208+
data Terrain = Terrain { terrainName :: Text
204209
-- ^ Name of terrain
205210
, terrainTile :: LocalId
206211
-- ^ Local ID of tile representing terrain
@@ -257,9 +262,9 @@ instance ToJSON Tile where
257262

258263
data Tileset = Tileset { tilesetFirstgid :: GlobalId
259264
-- ^ GID corresponding to the first tile in the set
260-
, tilesetImage :: String
265+
, tilesetImage :: FilePath
261266
-- ^ Image used for tiles in this set
262-
, tilesetName :: String
267+
, tilesetName :: Text
263268
-- ^ Name given to this tileset
264269
, tilesetTilewidth :: Int
265270
-- ^ Maximum width of tiles in this set
@@ -338,11 +343,47 @@ instance ToJSON Tileset where
338343
, "tiles" .= tilesetTiles
339344
]
340345

346+
data Version
347+
= VersionFloat Float
348+
| VersionText Text
349+
deriving (Eq, Ord, Show, Generic)
350+
351+
instance FromJSON Version where
352+
parseJSON v =
353+
fmap VersionFloat (parseJSON v) <|>
354+
fmap VersionText (parseJSON v)
355+
356+
instance ToJSON Version where
357+
toJSON version = case version of
358+
VersionFloat f -> toJSON f
359+
VersionText t -> toJSON t
360+
361+
data Orientation
362+
= Orthogonal
363+
| Isometric
364+
| Staggered
365+
| Orientation Text
366+
deriving (Eq, Ord, Show, Generic)
367+
368+
instance FromJSON Orientation where
369+
parseJSON = withText "Orientation" $ \t ->
370+
case t of
371+
"orthogonal" -> pure Orthogonal
372+
"isometric" -> pure Isometric
373+
"staggered" -> pure Staggered
374+
_otherwise -> pure $ Orientation t
375+
376+
instance ToJSON Orientation where
377+
toJSON o = toJSON $ case o of
378+
Orthogonal -> "orthogonal"
379+
Isometric -> "isometric"
380+
Staggered -> "staggered"
381+
Orientation t -> t
341382

342383
-- | The full monty.
343-
data Tiledmap = Tiledmap { tiledmapVersion :: Float
384+
data Tiledmap = Tiledmap { tiledmapVersion :: Version
344385
-- ^ The JSON format version
345-
, tiledmapTiledversion :: String
386+
, tiledmapTiledversion :: Version
346387
-- ^ The Tiled version used to save the file
347388
, tiledmapWidth :: Int
348389
-- ^ Number of tile columns
@@ -352,15 +393,15 @@ data Tiledmap = Tiledmap { tiledmapVersion :: Float
352393
-- ^ Map grid width.
353394
, tiledmapTileheight :: Double
354395
-- ^ Map grid height.
355-
, tiledmapOrientation :: String
396+
, tiledmapOrientation :: Orientation
356397
-- ^ Orthogonal, isometric, or staggered
357398
, tiledmapLayers :: Vector Layer
358399
-- ^ Array of Layers
359400
, tiledmapTilesets :: Vector Tileset
360401
-- ^ Array of Tilesets
361-
, tiledmapBackgroundcolor :: Maybe String
402+
, tiledmapBackgroundcolor :: Maybe Text
362403
-- ^ Hex-formatted color (#RRGGBB or #AARRGGBB) (optional)
363-
, tiledmapRenderorder :: String
404+
, tiledmapRenderorder :: Text -- TODO: RenderOrder
364405
-- ^ Rendering direction (orthogonal maps only)
365406
, tiledmapProperties :: Map Text Text
366407
-- ^ String key-value pairs

stack.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
# resolver:
1616
# name: custom-snapshot
1717
# location: "./custom-snapshot.yaml"
18-
resolver: lts-9.5
18+
resolver: lts-18.23
19+
system-ghc: true
1920

2021
# User packages to be built.
2122
# Various formats can be used as shown in the example below.
@@ -41,6 +42,13 @@ packages:
4142
# (e.g., acme-missiles-0.3)
4243
extra-deps: []
4344

45+
# XXX: GHC 9.2.1-capable set
46+
#- base-compat-0.12.1
47+
#- base-compat-batteries-0.12.1
48+
#- aeson-2.0.3.0
49+
#- text-2.0
50+
#- hashable-1.4.0.2
51+
4452
# Override default flag values for local packages and extra-deps
4553
flags: {}
4654

0 commit comments

Comments
 (0)