Skip to content

Commit

Permalink
Add Ugly options
Browse files Browse the repository at this point in the history
  • Loading branch information
Romans Potasovs committed Mar 6, 2018
1 parent fe4a7cb commit 484a118
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 64 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#elm-tiled-decode

A library for building decoders for [Tiled](http://www.mapeditor.org/) levels.

## Motivation
Expand Down
122 changes: 60 additions & 62 deletions src/Tiled/Decode.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
module Tiled.Decode exposing (DefaultProps, Layer, LayerWith(..), Level, LevelWith, Object(..), Options, Properties(..), Tileset(..), decode, decodeWith, defaultOptions, init, initWith)
module Tiled.Decode
exposing
( DefaultProps
, Layer
, LayerWith(..)
, Level
, LevelWith
, Object(..)
, Properties(..)
, Tileset(..)
, decode
, decodeWith
, defaultOptions
, init
, initWith
)

{-| Use the `decode` to get default Level or `decodeWith` to get custom decoding version
Expand All @@ -15,7 +30,7 @@ module Tiled.Decode exposing (DefaultProps, Layer, LayerWith(..), Level, LevelWi
# Definition
@docs Level, Layer, Tileset, DefaultProps, Object, Properties, Options, LayerWith, LevelWith
@docs Level, Layer, Tileset, DefaultProps, Object, Properties, LayerWith, LevelWith
-}

Expand Down Expand Up @@ -91,20 +106,10 @@ initWith props =
{-| -}
decodeWith : OptionsWith a b c d e f -> Json.Decode.Decoder (LevelWith f d e)
decodeWith opts =
let
layerOpts =
{ imageLayerCustomPropertiesDecode = opts.imageLayerCustomPropertiesDecode
, imageLayerDefaultCustomProperties = opts.imageLayerDefaultCustomProperties
, tileLayerCustomPropertiesDecode = opts.tileLayerCustomPropertiesDecode
, tileLayerDefaultCustomProperties = opts.tileLayerDefaultCustomProperties
, objectLayerCustomPropertiesDecode = opts.objectLayerCustomPropertiesDecode
, objectLayerDefaultCustomProperties = opts.objectLayerDefaultCustomProperties
}
in
Json.Decode.Pipeline.decode LevelWith
|> required "height" Json.Decode.float
|> required "infinite" Json.Decode.bool
|> required "layers" (opts.decodeLayer layerOpts |> Json.Decode.list)
|> required "layers" (opts.decodeLayer opts.layer |> Json.Decode.list)
|> required "nextobjectid" Json.Decode.int
|> required "orientation" Json.Decode.string
|> required "renderorder" Json.Decode.string
Expand All @@ -130,74 +135,67 @@ type Properties

{-| -}
type alias LayerOptionsWith a b c =
{ imageLayerCustomPropertiesDecode : a -> Json.Decode.Decoder a
, imageLayerDefaultCustomProperties : a
, tileLayerCustomPropertiesDecode : b -> Json.Decode.Decoder b
, tileLayerDefaultCustomProperties : b
, objectLayerCustomPropertiesDecode : c -> Json.Decode.Decoder c
, objectLayerDefaultCustomProperties : c
{ imageLayerPropDecode : a -> Json.Decode.Decoder a
, imageLayerProps : a
, tileLayerPropDecode : b -> Json.Decode.Decoder b
, tileLayerProps : b
, objectLayerPropDecode : c -> Json.Decode.Decoder c
, objectLayerProps : c
}


type alias LayerOptions =
LayerOptionsWith DefaultProps DefaultProps DefaultProps


{-|
type alias Options =
{ decodeLayer : LayerOptions -> Json.Decode.Decoder Layer
, decodeTileset : Json.Decode.Decoder Tileset
, defaultCustomProperties : DefaultProps
, imageLayerCustomPropertiesDecode : DefaultProps -> Json.Decode.Decoder DefaultProps
, imageLayerDefaultCustomProperties : DefaultProps
, objectLayerCustomPropertiesDecode : DefaultProps -> Json.Decode.Decoder DefaultProps
, objectLayerDefaultCustomProperties : DefaultProps
, properties : DefaultProps -> Json.Decode.Decoder DefaultProps
, tileLayerCustomPropertiesDecode : DefaultProps -> Json.Decode.Decoder DefaultProps
, tileLayerDefaultCustomProperties : DefaultProps
}
-}
type alias Options =
OptionsWith DefaultProps DefaultProps DefaultProps Layer Tileset DefaultProps


{-| -}
type alias OptionsWith a b c d e f =
{ decodeLayer : LayerOptionsWith a b c -> Json.Decode.Decoder d
, decodeTileset : Json.Decode.Decoder e
, defaultCustomProperties : f
, properties : f -> Json.Decode.Decoder f
, imageLayerCustomPropertiesDecode : a -> Json.Decode.Decoder a
, imageLayerDefaultCustomProperties : a
, tileLayerCustomPropertiesDecode : b -> Json.Decode.Decoder b
, tileLayerDefaultCustomProperties : b
, objectLayerCustomPropertiesDecode : c -> Json.Decode.Decoder c
, objectLayerDefaultCustomProperties : c
, layer : LayerOptionsWith a b c
}


type alias Options a b c d e f g k k1 k2 k3 v v1 v2 v3 =
{ decodeLayer : LayerOptionsWith a b c -> Json.Decode.Decoder (LayerWith a b c)
, decodeTileset : Json.Decode.Decoder Tileset
, defaultCustomProperties : Dict k v
, layer :
{ imageLayerPropDecode : d -> Json.Decode.Decoder (Dict String Properties)
, imageLayerProps : Dict k1 v1
, objectLayerProps : Dict k2 v2
, objectLayerPropDecode : e -> Json.Decode.Decoder (Dict String Properties)
, tileLayerProps : Dict k3 v3
, tileLayerPropDecode : f -> Json.Decode.Decoder (Dict String Properties)
}
, properties : g -> Json.Decode.Decoder (Dict String Properties)
}


{-| -}
defaultOptions : Options
defaultOptions : Options a b c d e f g k k1 k2 k3 v v1 v2 v3
defaultOptions =
let
properties =
propDecode =
\_ -> decodeCustomProperties

defaultCustomProperties =
defaultProps =
Dict.empty
in
{ decodeLayer = \layer -> decodeLayerDefault layer
{ decodeLayer = \layer -> decodeLayer layer
, decodeTileset = decodeTilesetDefault
, properties = properties
, defaultCustomProperties = defaultCustomProperties
, imageLayerCustomPropertiesDecode = properties
, imageLayerDefaultCustomProperties = defaultCustomProperties
, tileLayerCustomPropertiesDecode = properties
, tileLayerDefaultCustomProperties = defaultCustomProperties
, objectLayerCustomPropertiesDecode = properties
, objectLayerDefaultCustomProperties = defaultCustomProperties
, properties = propDecode
, defaultCustomProperties = defaultProps
, layer =
{ imageLayerPropDecode = propDecode
, imageLayerProps = defaultProps
, tileLayerPropDecode = propDecode
, tileLayerProps = defaultProps
, objectLayerPropDecode = propDecode
, objectLayerProps = defaultProps
}
}


Expand Down Expand Up @@ -487,28 +485,28 @@ type alias ObjectLayerDataWith customProperties =


{-| -}
decodeLayerDefault : LayerOptions -> Json.Decode.Decoder Layer
decodeLayerDefault { imageLayerCustomPropertiesDecode, imageLayerDefaultCustomProperties, tileLayerCustomPropertiesDecode, tileLayerDefaultCustomProperties, objectLayerCustomPropertiesDecode, objectLayerDefaultCustomProperties } =
decodeLayer : LayerOptionsWith a b c -> Json.Decode.Decoder (LayerWith a b c)
decodeLayer { imageLayerPropDecode, imageLayerProps, tileLayerPropDecode, tileLayerProps, objectLayerPropDecode, objectLayerProps } =
field "type" Json.Decode.string
|> Json.Decode.andThen
(\string ->
case string of
"tilelayer" ->
Json.Decode.map TileLayer
(decodeTileLayer
|> optional "properties" (tileLayerCustomPropertiesDecode tileLayerDefaultCustomProperties) tileLayerDefaultCustomProperties
|> optional "properties" (tileLayerPropDecode tileLayerProps) tileLayerProps
)

"imagelayer" ->
Json.Decode.map ImageLayer
(decodeImageLayer
|> optional "properties" (imageLayerCustomPropertiesDecode imageLayerDefaultCustomProperties) imageLayerDefaultCustomProperties
|> optional "properties" (imageLayerPropDecode imageLayerProps) imageLayerProps
)

"objectgroup" ->
Json.Decode.map ObjectLayer
(decodeObjectLayer
|> optional "properties" (objectLayerCustomPropertiesDecode objectLayerDefaultCustomProperties) objectLayerDefaultCustomProperties
|> optional "properties" (objectLayerPropDecode objectLayerProps) objectLayerProps
)

_ ->
Expand Down

0 comments on commit 484a118

Please sign in to comment.