From ce869d13dd5cb8febf948d1e33c530ba98174ca1 Mon Sep 17 00:00:00 2001 From: Metalblueberry Date: Wed, 14 Oct 2020 21:20:20 +0200 Subject: [PATCH 1/6] Functions to load/save tileset --- assets/tilesets/testLoadTileset.tsx | 5 + assets/tilesets/testLoadTilesetTile.tsx | 13 ++ tiled.go | 24 ++++ tiled_test.go | 2 +- tmx_group.go | 2 +- tmx_image.go | 12 +- tmx_layer.go | 2 +- tmx_map.go | 2 +- tmx_object.go | 74 +++++----- tmx_property.go | 14 +- tmx_property_test.go | 41 +++--- tmx_tileset.go | 57 +++++--- tmx_tileset_test.go | 179 ++++++++++++++++++++++++ 13 files changed, 332 insertions(+), 95 deletions(-) create mode 100644 assets/tilesets/testLoadTileset.tsx create mode 100644 assets/tilesets/testLoadTilesetTile.tsx diff --git a/assets/tilesets/testLoadTileset.tsx b/assets/tilesets/testLoadTileset.tsx new file mode 100644 index 0000000..6d4eeb9 --- /dev/null +++ b/assets/tilesets/testLoadTileset.tsx @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/assets/tilesets/testLoadTilesetTile.tsx b/assets/tilesets/testLoadTilesetTile.tsx new file mode 100644 index 0000000..cdac077 --- /dev/null +++ b/assets/tilesets/testLoadTilesetTile.tsx @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/tiled.go b/tiled.go index 1f0d277..99053dc 100644 --- a/tiled.go +++ b/tiled.go @@ -90,3 +90,27 @@ func (l *Loader) LoadFromFile(fileName string) (*Map, error) { return l.LoadFromReader(dir, f) } + +func LoadTilesetFromReader(baseDir string, r io.Reader) (*Tileset, error) { + d := xml.NewDecoder(r) + + m := &Tileset{ + baseDir: baseDir, + SourceLoaded: true, + } + if err := d.Decode(m); err != nil { + return nil, err + } + + return m, nil +} + +func SaveTilesetToWriter(tileset *Tileset, w io.Writer) error { + encoder := xml.NewEncoder(w) + encoder.Indent("", " ") + return encoder.Encode(tileset) +} + +func b(v bool) *bool { + return &v +} diff --git a/tiled_test.go b/tiled_test.go index dc8a6a9..ed1340e 100644 --- a/tiled_test.go +++ b/tiled_test.go @@ -184,7 +184,7 @@ func TestFont(t *testing.T) { assert.Equal(t, false, text.Italic) assert.Equal(t, false, text.Underline) assert.Equal(t, false, text.Strikethrough) - assert.Equal(t, true, text.Kerning) + assert.Equal(t, true, *text.Kerning) assert.Equal(t, "left", text.HAlign) assert.Equal(t, "top", text.VAlign) } diff --git a/tmx_group.go b/tmx_group.go index 1f72b44..28d974d 100644 --- a/tmx_group.go +++ b/tmx_group.go @@ -43,7 +43,7 @@ type Group struct { // Whether the layer is shown (1) or hidden (0). Defaults to 1. Visible bool `xml:"visible,attr"` // Custom properties - Properties Properties `xml:"properties>property"` + Properties *Properties `xml:"properties"` // Map layers Layers []*Layer `xml:"layer"` // Map object groups diff --git a/tmx_image.go b/tmx_image.go index 2d60325..d086996 100644 --- a/tmx_image.go +++ b/tmx_image.go @@ -47,7 +47,7 @@ type ImageLayer struct { // Whether the layer is shown (1) or hidden (0). Defaults to 1. Visible bool `xml:"visible,attr"` // Custom properties - Properties Properties `xml:"properties>property"` + Properties *Properties `xml:"properties"` // The group image Image *Image `xml:"image"` } @@ -73,16 +73,16 @@ func (l *ImageLayer) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error // Image source type Image struct { // Used for embedded images, in combination with a data child element. Valid values are file extensions like png, gif, jpg, bmp, etc. - Format string `xml:"format,attr"` + Format string `xml:"format,attr,omitempty"` // The reference to the tileset image file Source string `xml:"source,attr"` // Defines a specific color that is treated as transparent (example value: "#FF00FF" for magenta). // Up until Tiled 0.12, this value is written out without a # but this is planned to change. - Trans *HexColor `xml:"trans,attr"` + Trans *HexColor `xml:"trans,attr,omitempty"` // The image width in pixels (optional, used for tile index correction when the image changes) - Width int `xml:"width,attr"` + Width int `xml:"width,attr,omitempty"` // The image height in pixels (optional) - Height int `xml:"height,attr"` + Height int `xml:"height,attr,omitempty"` // Embedded image content - Data *Data `xml:"data,attr"` + Data *Data `xml:"data,attr,omitempty"` } diff --git a/tmx_layer.go b/tmx_layer.go index ae55c30..c4736db 100644 --- a/tmx_layer.go +++ b/tmx_layer.go @@ -79,7 +79,7 @@ type Layer struct { // Rendering offset for this layer in pixels. Defaults to 0. (since 0.14) OffsetY int `xml:"offsety,attr"` // Custom properties - Properties Properties `xml:"properties>property"` + Properties *Properties `xml:"properties"` // This is the attribute you'd like to use, not Data. Tile entry at (x,y) is obtained using l.DecodedTiles[y*map.Width+x]. Tiles []*LayerTile // Data diff --git a/tmx_map.go b/tmx_map.go index 200a830..d27cb66 100644 --- a/tmx_map.go +++ b/tmx_map.go @@ -79,7 +79,7 @@ type Map struct { // Stores the next available ID for new objects. This number is stored to prevent reuse of the same ID after objects have been removed. (since 0.11) NextObjectID uint32 `xml:"nextobjectid,attr"` // Custom properties - Properties *Properties `xml:"properties>property"` + Properties *Properties `xml:"properties"` // Map tilesets Tilesets []*Tileset `xml:"tileset"` // Map layers diff --git a/tmx_object.go b/tmx_object.go index fdea33d..b1b348d 100644 --- a/tmx_object.go +++ b/tmx_object.go @@ -39,60 +39,60 @@ type ObjectGroup struct { // Unique ID of the layer. // Each layer that added to a map gets a unique id. Even if a layer is deleted, // no layer ever gets the same ID. Can not be changed in Tiled. (since Tiled 1.2) - ID uint32 `xml:"id,attr"` + ID uint32 `xml:"id,attr,omitempty"` // The name of the object group. - Name string `xml:"name,attr"` + Name string `xml:"name,attr,omitempty"` // The color used to display the objects in this group. - Color *HexColor `xml:"color,attr"` + Color *HexColor `xml:"color,attr,omitempty"` // The opacity of the layer as a value from 0 to 1. Defaults to 1. - Opacity float32 `xml:"opacity,attr"` + Opacity float32 `xml:"opacity,attr,omitempty"` // Whether the layer is shown (1) or hidden (0). Defaults to 1. - Visible bool `xml:"visible,attr"` + Visible *bool `xml:"visible,attr"` // Rendering offset for this layer in pixels. Defaults to 0. (since 0.14) - OffsetX int `xml:"offsetx,attr"` + OffsetX int `xml:"offsetx,attr,omitempty"` // Rendering offset for this layer in pixels. Defaults to 0. (since 0.14) - OffsetY int `xml:"offsety,attr"` + OffsetY int `xml:"offsety,attr,omitempty"` // Whether the objects are drawn according to the order of appearance ("index") or sorted by their y-coordinate ("topdown"). Defaults to "topdown". - DrawOrder string `xml:"draworder,attr"` + DrawOrder string `xml:"draworder,attr,omitempty"` // Custom properties - Properties Properties `xml:"properties>property"` + Properties *Properties `xml:"properties,omitempty"` // Group objects - Objects []*Object `xml:"object"` + Objects []*Object `xml:"object,omitempty"` } // Object is used to add custom information to your tile map, such as spawn points, warps, exits, etc. type Object struct { // Unique ID of the object. Each object that is placed on a map gets a unique id. Even if an object was deleted, no object gets the same ID. // Can not be changed in Tiled Qt. (since Tiled 0.11) - ID uint32 `xml:"id,attr"` + ID uint32 `xml:"id,attr,omitempty"` // The name of the object. An arbitrary string. - Name string `xml:"name,attr"` + Name string `xml:"name,attr,omitempty"` // The type of the object. An arbitrary string. - Type string `xml:"type,attr"` + Type string `xml:"type,attr,omitempty"` // The x coordinate of the object. - X float64 `xml:"x,attr"` + X float64 `xml:"x,attr,omitempty"` // The y coordinate of the object. - Y float64 `xml:"y,attr"` + Y float64 `xml:"y,attr,omitempty"` // The width of the object (defaults to 0). - Width float64 `xml:"width,attr"` + Width float64 `xml:"width,attr,omitempty"` // The height of the object (defaults to 0). - Height float64 `xml:"height,attr"` + Height float64 `xml:"height,attr,omitempty"` // The rotation of the object in degrees clockwise (defaults to 0). (since 0.10) - Rotation float64 `xml:"rotation,attr"` + Rotation float64 `xml:"rotation,attr,omitempty"` // An reference to a tile (optional). - GID uint32 `xml:"gid,attr"` + GID uint32 `xml:"gid,attr,omitempty"` // Whether the object is shown (1) or hidden (0). Defaults to 1. (since 0.9) - Visible bool `xml:"visible,attr"` + Visible *bool `xml:"visible,attr"` // Custom properties - Properties Properties `xml:"properties>property"` + Properties *Properties `xml:"properties,omitempty"` // Used to mark an object as an ellipse. The existing x, y, width and height attributes are used to determine the size of the ellipse. - Ellipses []*Ellipse `xml:"ellipse"` + Ellipses []*Ellipse `xml:"ellipse,omitempty"` // Polygons - Polygons []*Polygon `xml:"polygon"` + Polygons []*Polygon `xml:"polygon,omitempty"` // Poly lines - PolyLines []*PolyLine `xml:"polyline"` + PolyLines []*PolyLine `xml:"polyline,omitempty"` // Text - Text *Text `xml:"text"` + Text *Text `xml:"text,omitempty"` } // Ellipse is used to mark an object as an ellipse. @@ -162,27 +162,27 @@ type Text struct { // The actual text Text string `xml:",chardata"` // The font family used (default: "sans-serif") - FontFamily string `xml:"fontfamily,attr"` + FontFamily string `xml:"fontfamily,attr,omitempty"` // The size of the font in pixels (not using points, because other sizes in the TMX format are also using pixels) (default: 16) - Size int `xml:"pixelsize,attr"` + Size int `xml:"pixelsize,attr,omitempty"` // Whether word wrapping is enabled (1) or disabled (0). Defaults to 0. - Wrap bool `xml:"wrap,attr"` + Wrap bool `xml:"wrap,attr,omitempty"` // Color of the text in #AARRGGBB or #RRGGBB format (default: #000000) - Color *HexColor `xml:"color,attr"` + Color *HexColor `xml:"color,attr,omitempty"` // Whether the font is bold (1) or not (0). Defaults to 0. - Bold bool `xml:"bold,attr"` + Bold bool `xml:"bold,attr,omitempty"` // Whether the font is italic (1) or not (0). Defaults to 0. - Italic bool `xml:"italic,attr"` + Italic bool `xml:"italic,attr,omitempty"` // Whether a line should be drawn below the text (1) or not (0). Defaults to 0. - Underline bool `xml:"underline,attr"` + Underline bool `xml:"underline,attr,omitempty"` // Whether a line should be drawn through the text (1) or not (0). Defaults to 0. - Strikethrough bool `xml:"strikeout,attr"` + Strikethrough bool `xml:"strikeout,attr,omitempty"` // Whether kerning should be used while rendering the text (1) or not (0). Default to 1. - Kerning bool `xml:"kerning,attr"` + Kerning *bool `xml:"kerning,attr,omitempty"` // Horizontal alignment of the text within the object (left (default), center, right or justify (since Tiled 1.2.1)) - HAlign string `xml:"halign,attr"` + HAlign string `xml:"halign,attr,omitempty"` // Vertical alignment of the text within the object (top (default), center or bottom) - VAlign string `xml:"valign,attr"` + VAlign string `xml:"valign,attr,omitempty"` } // UnmarshalXML decodes a single XML element beginning with the given start element. @@ -192,7 +192,7 @@ func (t *Text) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { item := Alias{ FontFamily: "sans-serif", Size: 16, - Kerning: true, + Kerning: b(true), HAlign: "left", VAlign: "top", Color: &HexColor{}, diff --git a/tmx_property.go b/tmx_property.go index 836c930..e4a8aef 100644 --- a/tmx_property.go +++ b/tmx_property.go @@ -25,7 +25,9 @@ package tiled import "strconv" // Properties wraps any number of custom properties -type Properties []*Property +type Properties struct { + Property []*Property `xml:"property"` +} // Property is used for custom properties type Property struct { @@ -43,7 +45,7 @@ type Property struct { // Get finds all properties by specified name func (p Properties) Get(name string) []string { var values []string - for _, property := range p { + for _, property := range p.Property { if property.Name == name { values = append(values, property.Value) } @@ -54,7 +56,7 @@ func (p Properties) Get(name string) []string { // GetString finds first string property by specified name func (p Properties) GetString(name string) string { var v string - for _, property := range p { + for _, property := range p.Property { if property.Name == name { if property.Type == "" { return property.Value @@ -68,7 +70,7 @@ func (p Properties) GetString(name string) string { // GetBool finds first bool property by specified name func (p Properties) GetBool(name string) bool { - for _, property := range p { + for _, property := range p.Property { if property.Name == name && property.Type == "boolean" { return property.Value == "true" } @@ -78,7 +80,7 @@ func (p Properties) GetBool(name string) bool { // GetInt finds first int property by specified name func (p Properties) GetInt(name string) int { - for _, property := range p { + for _, property := range p.Property { if property.Name == name && property.Type == "int" { v, err := strconv.Atoi(property.Value) if err != nil { @@ -92,7 +94,7 @@ func (p Properties) GetInt(name string) int { // GetFloat finds first float property by specified name func (p Properties) GetFloat(name string) float64 { - for _, property := range p { + for _, property := range p.Property { if property.Name == name && property.Type == "float" { v, err := strconv.ParseFloat(property.Value, 64) if err != nil { diff --git a/tmx_property_test.go b/tmx_property_test.go index 074246f..4271ba4 100644 --- a/tmx_property_test.go +++ b/tmx_property_test.go @@ -31,25 +31,27 @@ import ( func TestGetProperty(t *testing.T) { props := Properties{ - { - Name: "string-name", - Type: "string", - Value: "string-value", - }, - { - Name: "int-name", - Type: "int", - Value: "123", - }, - { - Name: "float-name", - Type: "float", - Value: "1.23", - }, - { - Name: "bool-name", - Type: "boolean", - Value: "true", + Property: []*Property{ + { + Name: "string-name", + Type: "string", + Value: "string-value", + }, + { + Name: "int-name", + Type: "int", + Value: "123", + }, + { + Name: "float-name", + Type: "float", + Value: "1.23", + }, + { + Name: "bool-name", + Type: "boolean", + Value: "true", + }, }, } @@ -57,4 +59,5 @@ func TestGetProperty(t *testing.T) { assert.Equal(t, 123, props.GetInt("int-name")) assert.Equal(t, 1.23, props.GetFloat("float-name")) assert.Equal(t, true, props.GetBool("bool-name")) + } diff --git a/tmx_tileset.go b/tmx_tileset.go index 1cfb21a..6bcfe01 100644 --- a/tmx_tileset.go +++ b/tmx_tileset.go @@ -10,43 +10,46 @@ type Tileset struct { // Base directory baseDir string + // XMLName holds the xml name for this struct + XMLName struct{} `xml:"tileset"` + // The TMX format version, generally 1.0. - Version string `xml:"version,attr"` + Version string `xml:"version,attr,omitempty"` // The Tiled version used to generate this file - TiledVersion string `xml:"tiledversion,attr"` + TiledVersion string `xml:"tiledversion,attr,omitempty"` // The first global tile ID of this tileset (this global ID maps to the first tile in this tileset). - FirstGID uint32 `xml:"firstgid,attr"` + FirstGID uint32 `xml:"firstgid,attr,omitempty"` // If this tileset is stored in an external TSX (Tile Set XML) file, this attribute refers to that file. // That TSX file has the same structure as the element described here. (There is the firstgid // attribute missing and this source attribute is also not there. These two attributes are kept in the // TMX map, since they are map specific.) - Source string `xml:"source,attr"` + Source string `xml:"source,attr,omitempty"` // External TSX source loaded. SourceLoaded bool `xml:"-"` // The name of this tileset. - Name string `xml:"name,attr"` + Name string `xml:"name,attr,omitempty"` // The (maximum) width of the tiles in this tileset. - TileWidth int `xml:"tilewidth,attr"` + TileWidth int `xml:"tilewidth,attr,omitempty"` // The (maximum) height of the tiles in this tileset. - TileHeight int `xml:"tileheight,attr"` + TileHeight int `xml:"tileheight,attr,omitempty"` // The spacing in pixels between the tiles in this tileset (applies to the tileset image). - Spacing int `xml:"spacing,attr"` + Spacing int `xml:"spacing,attr,omitempty"` // The margin around the tiles in this tileset (applies to the tileset image). - Margin int `xml:"margin,attr"` + Margin int `xml:"margin,attr,omitempty"` // The number of tiles in this tileset (since 0.13) - TileCount int `xml:"tilecount,attr"` + TileCount int `xml:"tilecount,attr,omitempty"` // The number of tile columns in the tileset. For image collection tilesets it is editable and is used when displaying the tileset. (since 0.15) - Columns int `xml:"columns,attr"` + Columns int `xml:"columns,attr,omitempty"` // Offset in pixels, to be applied when drawing a tile from the related tileset. When not present, no offset is applied. - TileOffset *TilesetTileOffset `xml:"tileoffset"` + TileOffset *TilesetTileOffset `xml:"tileoffset,omitempty"` // Custom properties - Properties Properties `xml:"properties>property"` + Properties *Properties `xml:"properties,omitempty"` // Embedded image - Image *Image `xml:"image"` + Image *Image `xml:"image,omitempty"` // Defines an array of terrain types, which can be referenced from the terrain of the tile element. - TerrainTypes []*Terrain `xml:"terraintypes>terrain"` + TerrainTypes *TerrainTypes `xml:"terraintypes,omitempty"` // Tiles in tileset - Tiles []*TilesetTile `xml:"tile"` + Tiles []*TilesetTile `xml:"tile,omitempty"` } // GetFileFullPath returns path to file relative to tileset file @@ -62,6 +65,10 @@ type TilesetTileOffset struct { Y int `xml:"y,attr"` } +type TerrainTypes struct { + Terrain []*Terrain +} + // Terrain type type Terrain struct { // The name of the terrain type. @@ -69,7 +76,7 @@ type Terrain struct { // The local tile-id of the tile that represents the terrain visually. Tile uint32 `xml:"tile,attr"` // Custom properties - Properties Properties `xml:"properties>property"` + Properties *Properties `xml:"properties"` } // TilesetTile information @@ -77,21 +84,25 @@ type TilesetTile struct { // The local tile ID within its tileset. ID uint32 `xml:"id,attr"` // The type of the tile. Refers to an object type and is used by tile objects. (optional) (since 1.0) - Type string `xml:"type,attr"` + Type string `xml:"type,attr,omitempty"` // Defines the terrain type of each corner of the tile, given as comma-separated indexes in the terrain types // array in the order top-left, top-right, bottom-left, bottom-right. // Leaving out a value means that corner has no terrain. (optional) (since 0.9) - Terrain string `xml:"terrain,attr"` + Terrain string `xml:"terrain,attr,omitempty"` // A percentage indicating the probability that this tile is chosen when it competes with others while editing with the terrain tool. (optional) (since 0.9) - Probability int `xml:"probability,attr"` + Probability int `xml:"probability,attr,omitempty"` // Custom properties - Properties Properties `xml:"properties>property"` + Properties *Properties `xml:"properties,omitempty"` // Embedded image Image *Image `xml:"image"` // Tile object groups - ObjectGroups []*ObjectGroup `xml:"objectgroup"` + ObjectGroups []*ObjectGroup `xml:"objectgroup,omitempty"` // List of animation frames - Animation []*AnimationFrame `xml:"animation>frame"` + Animation *Animation `xml:"animation,omitempty"` +} + +type Animation struct { + Frame []*AnimationFrame `xml:"frame"` } // AnimationFrame is single frame of animation diff --git a/tmx_tileset_test.go b/tmx_tileset_test.go index 2a4ea68..66b4d75 100644 --- a/tmx_tileset_test.go +++ b/tmx_tileset_test.go @@ -23,7 +23,12 @@ SOFTWARE. package tiled import ( + "bytes" + "encoding/xml" "image" + "io" + "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -165,5 +170,179 @@ func TestGetTileRect(t *testing.T) { } }) } +} + +var testLoadTilesetFile = &Tileset{ + baseDir: ".", + Columns: 64, + FirstGID: 0, + Image: &Image{ + Format: "", + Data: nil, + Height: 3040, + Width: 2048, + Source: "ProjectUtumno_full.png", + Trans: nil, + }, + Margin: 0, + Name: "ProjectUtumno_full", + Properties: nil, + Source: "", + SourceLoaded: true, + Spacing: 0, + TerrainTypes: nil, + TileCount: 6080, + TileHeight: 32, + TileOffset: nil, + TileWidth: 32, + TiledVersion: "1.2.3", + Tiles: []*TilesetTile{ + { + ID: 116, + Type: "door", + Animation: nil, + Image: nil, + ObjectGroups: nil, + Probability: 0, + Properties: nil, + Terrain: "", + }, + }, + Version: "1.2", +} + +var testLoadTilesetTileFile = &TilesetTile{ + ID: 464, + Animation: &Animation{ + Frame: []*AnimationFrame{ + { + Duration: 500, + TileID: 75, + }, + { + Duration: 500, + TileID: 76, + }, + }, + }, + Image: nil, + ObjectGroups: []*ObjectGroup{ + { + ID: 0, + Color: nil, + DrawOrder: "index", + Name: "", + Objects: []*Object{ + { + GID: 0, + Ellipses: nil, + Height: 6.125, + ID: 1, + Name: "", + PolyLines: nil, + Polygons: nil, + Properties: nil, + Rotation: 0, + Text: nil, + Type: "", + Visible: nil, + Width: 32.375, + X: -0.25, + Y: 17.75, + }, + }, + OffsetX: 0, + OffsetY: 0, + Opacity: 0, + Properties: nil, + Visible: nil, + }, + }, +} + +func TestLoadTileset(t *testing.T) { + tsxFile, err := os.Open(filepath.Join(GetAssetsDirectory(), "tilesets/testLoadTileset.tsx")) + assert.Nil(t, err) + defer tsxFile.Close() + + tsx, err := LoadTilesetFromReader(".", tsxFile) + assert.Nil(t, err) + assert.Equal(t, testLoadTilesetFile, tsx) +} + +func TestSaveTileset(t *testing.T) { + tsxFile, err := os.Open(filepath.Join(GetAssetsDirectory(), "tilesets/testLoadTileset.tsx")) + assert.Nil(t, err) + defer tsxFile.Close() + + buffer := &bytes.Buffer{} + err = SaveTilesetToWriter(testLoadTilesetFile, buffer) + assert.Nil(t, err) + + assertXMLEqual(t, tsxFile, buffer) +} + +func TestLoadTile(t *testing.T) { + tsxFile, err := os.Open(filepath.Join(GetAssetsDirectory(), "tilesets/testLoadTilesetTile.tsx")) + assert.Nil(t, err) + defer tsxFile.Close() + + tsx, err := LoadTilesetFromReader(".", tsxFile) + assert.Nil(t, err) + assert.Len(t, tsx.Tiles, 1) + + tile := tsx.Tiles[0] + assert.Equal(t, testLoadTilesetTileFile, tile) +} + +func TestSaveTile(t *testing.T) { + tsxFile, err := os.Open(filepath.Join(GetAssetsDirectory(), "tilesets/testLoadTilesetTile.tsx")) + assert.Nil(t, err) + defer tsxFile.Close() + + tsx, err := LoadTilesetFromReader(".", tsxFile) + assert.Nil(t, err) + + buffer := &bytes.Buffer{} + xml.NewEncoder(buffer).Encode(tsx) + + tsxFile.Seek(0, 0) + assertXMLEqual(t, tsxFile, buffer) + +} + +func assertXMLEqual(t *testing.T, expected io.Reader, obtained io.Reader) { + var expec node + var obt node + var err error + + err = xml.NewDecoder(expected).Decode(&expec) + assert.Nil(t, err) + err = xml.NewDecoder(obtained).Decode(&obt) + assert.Nil(t, err) + + assert.Equal(t, expec, obt) +} + +type node struct { + XMLName xml.Name + Attrs []xml.Attr `xml:",any,attr"` + Content string `xml:",innerxml"` + Nodes []node `xml:",any"` +} + +func (n *node) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + type inNode node + + err := d.DecodeElement((*inNode)(n), &start) + if err != nil { + return err + } + + //Discard content if there are child nodes + if len(n.Nodes) > 0 { + n.Content = "" + } + return nil } From 97545e33b593341c4c2bc1047bb02d3f2a8a213b Mon Sep 17 00:00:00 2001 From: Metalblueberry Date: Sat, 17 Oct 2020 14:10:23 +0200 Subject: [PATCH 2/6] docs --- tiled.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tiled.go b/tiled.go index 99053dc..9ae6e0e 100644 --- a/tiled.go +++ b/tiled.go @@ -91,6 +91,7 @@ func (l *Loader) LoadFromFile(fileName string) (*Map, error) { return l.LoadFromReader(dir, f) } +// LoadTilesetFromReader loads a .tsx file into a Tileset structure func LoadTilesetFromReader(baseDir string, r io.Reader) (*Tileset, error) { d := xml.NewDecoder(r) @@ -105,6 +106,7 @@ func LoadTilesetFromReader(baseDir string, r io.Reader) (*Tileset, error) { return m, nil } +// SaveTilesetToWriter saves a Tileset structure into a given writer func SaveTilesetToWriter(tileset *Tileset, w io.Writer) error { encoder := xml.NewEncoder(w) encoder.Indent("", " ") From 67d41bffacb3be15721cd9d7f19ed32bc7d2855a Mon Sep 17 00:00:00 2001 From: Metalblueberry Date: Sat, 17 Oct 2020 14:12:36 +0200 Subject: [PATCH 3/6] docs --- tmx_tileset.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tmx_tileset.go b/tmx_tileset.go index 6bcfe01..d1d6fac 100644 --- a/tmx_tileset.go +++ b/tmx_tileset.go @@ -65,6 +65,7 @@ type TilesetTileOffset struct { Y int `xml:"y,attr"` } +// TerrainTypes represent a list of Terrains type TerrainTypes struct { Terrain []*Terrain } @@ -101,6 +102,7 @@ type TilesetTile struct { Animation *Animation `xml:"animation,omitempty"` } +// Animation represents a list of AnimationFrames type Animation struct { Frame []*AnimationFrame `xml:"frame"` } From 214d64ca38b67c0903051ffdd1ed479d20b8dce5 Mon Sep 17 00:00:00 2001 From: Jonathan Pearson Date: Sat, 7 Dec 2024 11:13:57 -0500 Subject: [PATCH 4/6] Add support for loading tilesets directly from TSX files, without maps. --- assets/tilesets/testLoadTileset.tsx | 5 + assets/tilesets/testLoadTilesetTile.tsx | 13 +++ tiled.go | 43 +++++++ tmx_tileset_test.go | 149 ++++++++++++++++++++++++ 4 files changed, 210 insertions(+) create mode 100644 assets/tilesets/testLoadTileset.tsx create mode 100644 assets/tilesets/testLoadTilesetTile.tsx diff --git a/assets/tilesets/testLoadTileset.tsx b/assets/tilesets/testLoadTileset.tsx new file mode 100644 index 0000000..5698392 --- /dev/null +++ b/assets/tilesets/testLoadTileset.tsx @@ -0,0 +1,5 @@ + + + + + diff --git a/assets/tilesets/testLoadTilesetTile.tsx b/assets/tilesets/testLoadTilesetTile.tsx new file mode 100644 index 0000000..564171e --- /dev/null +++ b/assets/tilesets/testLoadTilesetTile.tsx @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/tiled.go b/tiled.go index dbfc924..da5a495 100644 --- a/tiled.go +++ b/tiled.go @@ -43,6 +43,20 @@ func LoadFile(fileName string, options ...LoaderOption) (*Map, error) { return l.LoadFile(fileName) } +// LoadTilesetFromReader loads a tileset from an io.Reader. +// baseDir is used to locate relative paths to additional tileset data; default +// is currend directory if empty. +func LoadTilesetFromReader(baseDir string, r io.Reader, options ...LoaderOption) (*Tileset, error) { + l := newLoader(options...) + return l.LoadTilesetFromReader(baseDir, r) +} + +// LoadTilesetFile loads a tiled map in TSX format from a file. +func LoadTilesetFile(fileName string, options ...LoaderOption) (*Tileset, error) { + l := newLoader(options...) + return l.LoadTilesetFile(fileName) +} + // loader provides configuration on how TMX maps and resources are loaded. type loader struct { // A FileSystem that is used for loading TMX files and any external @@ -106,3 +120,32 @@ func (l *loader) LoadFile(fileName string) (*Map, error) { dir := filepath.Dir(fileName) return l.LoadReader(dir, f) } + +// LoadTilesetFromReader loads a tileset in TSX format from an io.Reader. +// baseDir is used to locate relative pats to additional tile data; default is +// the current directory if empty. +func (l *loader) LoadTilesetFromReader(baseDir string, r io.Reader) (*Tileset, error) { + d := xml.NewDecoder(r) + + t := &Tileset{ + baseDir: baseDir, + } + if err := d.Decode(t); err != nil { + return nil, err + } + + t.SourceLoaded = true + return t, nil +} + +// LoadTilesetFile loads a tileset in TSX format from a file. +func (l *loader) LoadTilesetFile(fileName string) (*Tileset, error) { + f, err := l.open(fileName) + if err != nil { + return nil, err + } + defer f.Close() + + dir := filepath.Dir(fileName) + return l.LoadTilesetFromReader(dir, f) +} diff --git a/tmx_tileset_test.go b/tmx_tileset_test.go index 2a4ea68..3946cad 100644 --- a/tmx_tileset_test.go +++ b/tmx_tileset_test.go @@ -23,7 +23,11 @@ SOFTWARE. package tiled import ( + "encoding/xml" "image" + "io" + "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -167,3 +171,148 @@ func TestGetTileRect(t *testing.T) { } } + +var testLoadTilesetFile = &Tileset{ + baseDir: ".", + Columns: 64, + FirstGID: 0, + Image: &Image{ + Format: "", + Data: nil, + Height: 3040, + Width: 2048, + Source: "ProjectUtumno_full.png", + Trans: nil, + }, + Margin: 0, + Name: "ProjectUtumno_full", + Properties: nil, + Source: "", + SourceLoaded: true, + Spacing: 0, + TerrainTypes: nil, + TileCount: 6080, + TileHeight: 32, + TileOffset: nil, + TileWidth: 32, + TiledVersion: "1.2.3", + Tiles: []*TilesetTile{ + { + ID: 116, + Type: "door", + Animation: nil, + Image: nil, + ObjectGroups: nil, + Probability: 0, + Properties: nil, + Terrain: "", + }, + }, + Version: "1.2", +} + +var testLoadTilesetTileFile = &TilesetTile{ + ID: 464, + Animation: []*AnimationFrame{ + { + Duration: 500, + TileID: 75, + }, + { + Duration: 500, + TileID: 76, + }, + }, + Image: nil, + ObjectGroups: []*ObjectGroup{ + { + ID: 0, + Color: nil, + DrawOrder: "index", + Name: "", + Objects: []*Object{ + { + GID: 0, + Ellipses: nil, + Height: 6.125, + ID: 1, + Name: "", + PolyLines: nil, + Polygons: nil, + Properties: nil, + Rotation: 0, + Text: nil, + Type: "", + Visible: true, + Width: 32.375, + X: -0.25, + Y: 17.75, + }, + }, + OffsetX: 0, + OffsetY: 0, + Opacity: 1, + Properties: nil, + Visible: true, + }, + }, +} + +func TestLoadTileset(t *testing.T) { + tsxFile, err := os.Open(filepath.Join(GetAssetsDirectory(), "tilesets/testLoadTileset.tsx")) + assert.Nil(t, err) + defer tsxFile.Close() + + tsx, err := LoadTilesetFromReader(".", tsxFile) + assert.Nil(t, err) + + assert.Equal(t, testLoadTilesetFile, tsx) +} + +func TestLoadTile(t *testing.T) { + tsxFile, err := os.Open(filepath.Join(GetAssetsDirectory(), "tilesets/testLoadTilesetTile.tsx")) + assert.Nil(t, err) + defer tsxFile.Close() + + tsx, err := LoadTilesetFromReader(".", tsxFile) + assert.Nil(t, err) + assert.Len(t, tsx.Tiles, 1) + + tile := tsx.Tiles[0] + assert.Equal(t, testLoadTilesetTileFile, tile) +} + +func assertXMLEqual(t *testing.T, expected io.Reader, obtained io.Reader) { + var expec node + var obt node + var err error + + err = xml.NewDecoder(expected).Decode(&expec) + assert.Nil(t, err) + err = xml.NewDecoder(obtained).Decode(&obt) + assert.Nil(t, err) + + assert.Equal(t, expec, obt) +} + +type node struct { + XMLName xml.Name + Attrs []xml.Attr `xml:",any,attr"` + Content string `xml:",innerxml"` + Nodes []node `xml:",any"` +} + +func (n *node) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + type inNode node + + err := d.DecodeElement((*inNode)(n), &start) + if err != nil { + return err + } + + //Discard content if there are child nodes + if len(n.Nodes) > 0 { + n.Content = "" + } + return nil +} From f535a776cee7650791eabe9fd3975545e71497c1 Mon Sep 17 00:00:00 2001 From: Jonathan Pearson Date: Sat, 7 Dec 2024 11:13:57 -0500 Subject: [PATCH 5/6] Address https://github.com/lafriks/go-tiled/pull/35 by removing Save feature. Concerns were around using pointer-to-bool to support omitting default-true bools during save. Removed the Save feature to get past that issue, hoping to get at least part of this PR submitted after 4 years. --- assets/tilesets/testLoadTileset.tsx | 9 ++++- tiled.go | 40 +++----------------- tiled_test.go | 6 +-- tmx_defaults.go | 6 +-- tmx_group.go | 2 +- tmx_image.go | 12 +++--- tmx_layer.go | 2 +- tmx_map.go | 2 +- tmx_property.go | 14 +++---- tmx_property_test.go | 41 ++++++++++---------- tmx_tileset_test.go | 58 ++++++++--------------------- 11 files changed, 70 insertions(+), 122 deletions(-) diff --git a/assets/tilesets/testLoadTileset.tsx b/assets/tilesets/testLoadTileset.tsx index 5698392..7184af4 100644 --- a/assets/tilesets/testLoadTileset.tsx +++ b/assets/tilesets/testLoadTileset.tsx @@ -1,5 +1,12 @@ - + + + + + + + + diff --git a/tiled.go b/tiled.go index 69d441a..fb0195b 100644 --- a/tiled.go +++ b/tiled.go @@ -121,23 +121,6 @@ func (l *loader) LoadFile(fileName string) (*Map, error) { return l.LoadReader(dir, f) } -// LoadTilesetFromReader loads a tileset in TSX format from an io.Reader. -// baseDir is used to locate relative pats to additional tile data; default is -// the current directory if empty. -func (l *loader) LoadTilesetFromReader(baseDir string, r io.Reader) (*Tileset, error) { - d := xml.NewDecoder(r) - - t := &Tileset{ - baseDir: baseDir, - } - if err := d.Decode(t); err != nil { - return nil, err - } - - t.SourceLoaded = true - return t, nil -} - // LoadTilesetFile loads a tileset in TSX format from a file. func (l *loader) LoadTilesetFile(fileName string) (*Tileset, error) { f, err := l.open(fileName) @@ -151,27 +134,16 @@ func (l *loader) LoadTilesetFile(fileName string) (*Tileset, error) { } // LoadTilesetFromReader loads a .tsx file into a Tileset structure -func LoadTilesetFromReader(baseDir string, r io.Reader) (*Tileset, error) { +func (l *loader) LoadTilesetFromReader(baseDir string, r io.Reader) (*Tileset, error) { d := xml.NewDecoder(r) - m := &Tileset{ - baseDir: baseDir, - SourceLoaded: true, + t := &Tileset{ + baseDir: baseDir, } - if err := d.Decode(m); err != nil { + if err := d.Decode(t); err != nil { return nil, err } - return m, nil -} - -// SaveTilesetToWriter saves a Tileset structure into a given writer -func SaveTilesetToWriter(tileset *Tileset, w io.Writer) error { - encoder := xml.NewEncoder(w) - encoder.Indent("", " ") - return encoder.Encode(tileset) -} - -func b(v bool) *bool { - return &v + t.SourceLoaded = true + return t, nil } diff --git a/tiled_test.go b/tiled_test.go index 3d11d33..1a36769 100644 --- a/tiled_test.go +++ b/tiled_test.go @@ -96,12 +96,12 @@ func TestLoadFile(t *testing.T) { // Test ObjectGroups.Visible defaults to true assert.Len(t, m.ObjectGroups, 1) assert.Equal(t, uint32(2), m.ObjectGroups[0].ID) - assert.Equal(t, true, *m.ObjectGroups[0].Visible) + assert.Equal(t, true, m.ObjectGroups[0].Visible) // Test Object.Visible defaults to true assert.Len(t, m.ObjectGroups[0].Objects, 1) assert.Equal(t, uint32(2), m.ObjectGroups[0].Objects[0].ID) - assert.Equal(t, true, *m.ObjectGroups[0].Objects[0].Visible) + assert.Equal(t, true, m.ObjectGroups[0].Objects[0].Visible) } func TestLoadFileError(t *testing.T) { @@ -208,7 +208,7 @@ func TestFont(t *testing.T) { assert.Equal(t, false, text.Italic) assert.Equal(t, false, text.Underline) assert.Equal(t, false, text.Strikethrough) - assert.Equal(t, true, *text.Kerning) + assert.Equal(t, true, text.Kerning) assert.Equal(t, "left", text.HAlign) assert.Equal(t, "top", text.VAlign) } diff --git a/tmx_defaults.go b/tmx_defaults.go index ef6c684..cf20e5a 100644 --- a/tmx_defaults.go +++ b/tmx_defaults.go @@ -57,12 +57,12 @@ func (a *aliasMap) SetDefaults() { // SetDefaults provides default values for Object. func (a *aliasObject) SetDefaults() { - a.Visible = b(true) + a.Visible = true } // SetDefaults provides default values for ObjectGroup. func (a *aliasObjectGroup) SetDefaults() { - a.Visible = b(true) + a.Visible = true a.Opacity = 1 } @@ -70,7 +70,7 @@ func (a *aliasObjectGroup) SetDefaults() { func (a *aliasText) SetDefaults() { a.FontFamily = "sans-serif" a.Size = 16 - a.Kerning = b(true) + a.Kerning = true a.HAlign = "left" a.VAlign = "top" a.Color = &HexColor{} diff --git a/tmx_group.go b/tmx_group.go index e9665c0..0bbf739 100644 --- a/tmx_group.go +++ b/tmx_group.go @@ -49,7 +49,7 @@ type Group struct { // The parallax y factor of the layer 0 - 1.0 ParallaxY float32 `xml:"parallaxy,attr"` // Custom properties - Properties *Properties `xml:"properties"` + Properties Properties `xml:"properties>property"` // Map layers Layers []*Layer `xml:"layer"` // Map object groups diff --git a/tmx_image.go b/tmx_image.go index 3796412..9583ef0 100644 --- a/tmx_image.go +++ b/tmx_image.go @@ -49,7 +49,7 @@ type ImageLayer struct { // Whether the layer is shown (1) or hidden (0). Defaults to 1. Visible bool `xml:"visible,attr"` // Custom properties - Properties *Properties `xml:"properties"` + Properties Properties `xml:"properties>property"` // The group image Image *Image `xml:"image"` // The parallax x factor of the layer 0 - 1.0 @@ -79,16 +79,16 @@ func (l *ImageLayer) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error // Image source type Image struct { // Used for embedded images, in combination with a data child element. Valid values are file extensions like png, gif, jpg, bmp, etc. - Format string `xml:"format,attr,omitempty"` + Format string `xml:"format,attr"` // The reference to the tileset image file Source string `xml:"source,attr"` // Defines a specific color that is treated as transparent (example value: "#FF00FF" for magenta). // Up until Tiled 0.12, this value is written out without a # but this is planned to change. - Trans *HexColor `xml:"trans,attr,omitempty"` + Trans *HexColor `xml:"trans,attr"` // The image width in pixels (optional, used for tile index correction when the image changes) - Width int `xml:"width,attr,omitempty"` + Width int `xml:"width,attr"` // The image height in pixels (optional) - Height int `xml:"height,attr,omitempty"` + Height int `xml:"height,attr"` // Embedded image content - Data *Data `xml:"data,attr,omitempty"` + Data *Data `xml:"data,attr"` } diff --git a/tmx_layer.go b/tmx_layer.go index edbdd5a..2c70c64 100644 --- a/tmx_layer.go +++ b/tmx_layer.go @@ -85,7 +85,7 @@ type Layer struct { // The parallax y factor of the layer 0 - 1.0 ParallaxY float32 `xml:"parallaxy,attr"` // Custom properties - Properties *Properties `xml:"properties"` + Properties Properties `xml:"properties>property"` // This is the attribute you'd like to use, not Data. Tile entry at (x,y) is obtained using l.DecodedTiles[y*map.Width+x]. Tiles []*LayerTile // Data diff --git a/tmx_map.go b/tmx_map.go index 8ea59e0..fe6e91e 100644 --- a/tmx_map.go +++ b/tmx_map.go @@ -99,7 +99,7 @@ type Map struct { // Stores the next available ID for new objects. This number is stored to prevent reuse of the same ID after objects have been removed. (since 0.11) NextObjectID uint32 `xml:"nextobjectid,attr"` // Custom properties - Properties *Properties `xml:"properties"` + Properties *Properties `xml:"properties>property"` // Map tilesets Tilesets []*Tileset `xml:"tileset"` // Map layers diff --git a/tmx_property.go b/tmx_property.go index 798a903..783b23f 100644 --- a/tmx_property.go +++ b/tmx_property.go @@ -29,9 +29,7 @@ import ( ) // Properties wraps any number of custom properties -type Properties struct { - Property []*Property `xml:"property"` -} +type Properties []*Property // Property is used for custom properties type Property struct { @@ -49,7 +47,7 @@ type Property struct { // Get finds all properties by specified name func (p Properties) Get(name string) []string { var values []string - for _, property := range p.Property { + for _, property := range p { if property.Name == name { values = append(values, property.Value) } @@ -60,7 +58,7 @@ func (p Properties) Get(name string) []string { // GetString finds first string property by specified name func (p Properties) GetString(name string) string { var v string - for _, property := range p.Property { + for _, property := range p { if property.Name == name { if property.Type == "" { return property.Value @@ -74,7 +72,7 @@ func (p Properties) GetString(name string) string { // GetBool finds first bool property by specified name func (p Properties) GetBool(name string) bool { - for _, property := range p.Property { + for _, property := range p { if property.Name == name && property.Type == "boolean" { return property.Value == "true" } @@ -84,7 +82,7 @@ func (p Properties) GetBool(name string) bool { // GetInt finds first int property by specified name func (p Properties) GetInt(name string) int { - for _, property := range p.Property { + for _, property := range p { if property.Name == name && property.Type == "int" { v, err := strconv.Atoi(property.Value) if err != nil { @@ -98,7 +96,7 @@ func (p Properties) GetInt(name string) int { // GetFloat finds first float property by specified name func (p Properties) GetFloat(name string) float64 { - for _, property := range p.Property { + for _, property := range p { if property.Name == name && property.Type == "float" { v, err := strconv.ParseFloat(property.Value, 64) if err != nil { diff --git a/tmx_property_test.go b/tmx_property_test.go index 4271ba4..074246f 100644 --- a/tmx_property_test.go +++ b/tmx_property_test.go @@ -31,27 +31,25 @@ import ( func TestGetProperty(t *testing.T) { props := Properties{ - Property: []*Property{ - { - Name: "string-name", - Type: "string", - Value: "string-value", - }, - { - Name: "int-name", - Type: "int", - Value: "123", - }, - { - Name: "float-name", - Type: "float", - Value: "1.23", - }, - { - Name: "bool-name", - Type: "boolean", - Value: "true", - }, + { + Name: "string-name", + Type: "string", + Value: "string-value", + }, + { + Name: "int-name", + Type: "int", + Value: "123", + }, + { + Name: "float-name", + Type: "float", + Value: "1.23", + }, + { + Name: "bool-name", + Type: "boolean", + Value: "true", }, } @@ -59,5 +57,4 @@ func TestGetProperty(t *testing.T) { assert.Equal(t, 123, props.GetInt("int-name")) assert.Equal(t, 1.23, props.GetFloat("float-name")) assert.Equal(t, true, props.GetBool("bool-name")) - } diff --git a/tmx_tileset_test.go b/tmx_tileset_test.go index 3946cad..f5921b7 100644 --- a/tmx_tileset_test.go +++ b/tmx_tileset_test.go @@ -23,9 +23,7 @@ SOFTWARE. package tiled import ( - "encoding/xml" "image" - "io" "os" "path/filepath" "testing" @@ -184,9 +182,14 @@ var testLoadTilesetFile = &Tileset{ Source: "ProjectUtumno_full.png", Trans: nil, }, - Margin: 0, - Name: "ProjectUtumno_full", - Properties: nil, + Margin: 0, + Name: "ProjectUtumno_full", + Properties: Properties{ + { + Name: "testTilesetProperty", + Value: "valueOfTilesetProperty", + }, + }, Source: "", SourceLoaded: true, Spacing: 0, @@ -204,8 +207,14 @@ var testLoadTilesetFile = &Tileset{ Image: nil, ObjectGroups: nil, Probability: 0, - Properties: nil, - Terrain: "", + Properties: Properties{ + { + Name: "testTileProperty", + Type: "int", + Value: "7", + }, + }, + Terrain: "", }, }, Version: "1.2", @@ -281,38 +290,3 @@ func TestLoadTile(t *testing.T) { tile := tsx.Tiles[0] assert.Equal(t, testLoadTilesetTileFile, tile) } - -func assertXMLEqual(t *testing.T, expected io.Reader, obtained io.Reader) { - var expec node - var obt node - var err error - - err = xml.NewDecoder(expected).Decode(&expec) - assert.Nil(t, err) - err = xml.NewDecoder(obtained).Decode(&obt) - assert.Nil(t, err) - - assert.Equal(t, expec, obt) -} - -type node struct { - XMLName xml.Name - Attrs []xml.Attr `xml:",any,attr"` - Content string `xml:",innerxml"` - Nodes []node `xml:",any"` -} - -func (n *node) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { - type inNode node - - err := d.DecodeElement((*inNode)(n), &start) - if err != nil { - return err - } - - //Discard content if there are child nodes - if len(n.Nodes) > 0 { - n.Content = "" - } - return nil -} From 3ad72dc33845fe831f9fe6d61950137602b2a9c8 Mon Sep 17 00:00:00 2001 From: Jonathan Pearson Date: Sun, 29 Jun 2025 09:55:00 -0400 Subject: [PATCH 6/6] Fix naming of LoadTilesetReader functions to match local convention. --- tiled.go | 12 ++++++------ tmx_tileset_test.go | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tiled.go b/tiled.go index fb0195b..6389c6c 100644 --- a/tiled.go +++ b/tiled.go @@ -43,12 +43,12 @@ func LoadFile(fileName string, options ...LoaderOption) (*Map, error) { return l.LoadFile(fileName) } -// LoadTilesetFromReader loads a tileset from an io.Reader. +// LoadTilesetReader loads a tileset from an io.Reader. // baseDir is used to locate relative paths to additional tileset data; default // is currend directory if empty. -func LoadTilesetFromReader(baseDir string, r io.Reader, options ...LoaderOption) (*Tileset, error) { +func LoadTilesetReader(baseDir string, r io.Reader, options ...LoaderOption) (*Tileset, error) { l := newLoader(options...) - return l.LoadTilesetFromReader(baseDir, r) + return l.LoadTilesetReader(baseDir, r) } // LoadTilesetFile loads a tiled map in TSX format from a file. @@ -130,11 +130,11 @@ func (l *loader) LoadTilesetFile(fileName string) (*Tileset, error) { defer f.Close() dir := filepath.Dir(fileName) - return l.LoadTilesetFromReader(dir, f) + return l.LoadTilesetReader(dir, f) } -// LoadTilesetFromReader loads a .tsx file into a Tileset structure -func (l *loader) LoadTilesetFromReader(baseDir string, r io.Reader) (*Tileset, error) { +// LoadTilesetReader loads a .tsx file into a Tileset structure +func (l *loader) LoadTilesetReader(baseDir string, r io.Reader) (*Tileset, error) { d := xml.NewDecoder(r) t := &Tileset{ diff --git a/tmx_tileset_test.go b/tmx_tileset_test.go index f5921b7..92da031 100644 --- a/tmx_tileset_test.go +++ b/tmx_tileset_test.go @@ -272,7 +272,7 @@ func TestLoadTileset(t *testing.T) { assert.Nil(t, err) defer tsxFile.Close() - tsx, err := LoadTilesetFromReader(".", tsxFile) + tsx, err := LoadTilesetReader(".", tsxFile) assert.Nil(t, err) assert.Equal(t, testLoadTilesetFile, tsx) @@ -283,7 +283,7 @@ func TestLoadTile(t *testing.T) { assert.Nil(t, err) defer tsxFile.Close() - tsx, err := LoadTilesetFromReader(".", tsxFile) + tsx, err := LoadTilesetReader(".", tsxFile) assert.Nil(t, err) assert.Len(t, tsx.Tiles, 1)