Skip to content

Commit 87bd542

Browse files
Functions to load/save tileset
1 parent 000ed37 commit 87bd542

File tree

11 files changed

+304
-62
lines changed

11 files changed

+304
-62
lines changed

assets/tilesets/testLoadTile.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<tileset version="1.2" tiledversion="1.2.3" name="ProjectUtumno_full" tilewidth="32" tileheight="32" tilecount="6080" columns="64">
3+
<image source="ProjectUtumno_full.png" width="2048" height="3040"/>
4+
<tile id="464">
5+
<objectgroup draworder="index">
6+
<object id="1" x="-0.25" y="17.75" width="32.375" height="6.125"/>
7+
</objectgroup>
8+
<animation>
9+
<frame tileid="75" duration="500"/>
10+
<frame tileid="76" duration="500"/>
11+
</animation>
12+
</tile>
13+
</tileset>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<tileset version="1.2" tiledversion="1.2.3" name="ProjectUtumno_full" tilewidth="32" tileheight="32" tilecount="6080" columns="64">
3+
<image source="ProjectUtumno_full.png" width="2048" height="3040"/>
4+
<tile id="116" type="door"></tile>
5+
</tileset>

tiled.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,23 @@ func (l *Loader) LoadFromFile(fileName string) (*Map, error) {
9090

9191
return l.LoadFromReader(dir, f)
9292
}
93+
94+
func LoadTilesetFromReader(baseDir string, r io.Reader) (*Tileset, error) {
95+
d := xml.NewDecoder(r)
96+
97+
m := &Tileset{
98+
baseDir: baseDir,
99+
SourceLoaded: true,
100+
}
101+
if err := d.Decode(m); err != nil {
102+
return nil, err
103+
}
104+
105+
return m, nil
106+
}
107+
108+
func SaveTilesetToWriter(tileset *Tileset, w io.Writer) error {
109+
encoder := xml.NewEncoder(w)
110+
encoder.Indent("", " ")
111+
return encoder.Encode(tileset)
112+
}

tmx_group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type Group struct {
4343
// Whether the layer is shown (1) or hidden (0). Defaults to 1.
4444
Visible bool `xml:"visible,attr"`
4545
// Custom properties
46-
Properties Properties `xml:"properties>property"`
46+
Properties *Properties `xml:"properties"`
4747
// Map layers
4848
Layers []*Layer `xml:"layer"`
4949
// Map object groups

tmx_image.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type ImageLayer struct {
4747
// Whether the layer is shown (1) or hidden (0). Defaults to 1.
4848
Visible bool `xml:"visible,attr"`
4949
// Custom properties
50-
Properties Properties `xml:"properties>property"`
50+
Properties *Properties `xml:"properties"`
5151
// The group image
5252
Image *Image `xml:"image"`
5353
}
@@ -73,16 +73,16 @@ func (l *ImageLayer) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
7373
// Image source
7474
type Image struct {
7575
// Used for embedded images, in combination with a data child element. Valid values are file extensions like png, gif, jpg, bmp, etc.
76-
Format string `xml:"format,attr"`
76+
Format string `xml:"format,attr,omitempty"`
7777
// The reference to the tileset image file
7878
Source string `xml:"source,attr"`
7979
// Defines a specific color that is treated as transparent (example value: "#FF00FF" for magenta).
8080
// Up until Tiled 0.12, this value is written out without a # but this is planned to change.
81-
Trans *HexColor `xml:"trans,attr"`
81+
Trans *HexColor `xml:"trans,attr,omitempty"`
8282
// The image width in pixels (optional, used for tile index correction when the image changes)
83-
Width int `xml:"width,attr"`
83+
Width int `xml:"width,attr,omitempty"`
8484
// The image height in pixels (optional)
85-
Height int `xml:"height,attr"`
85+
Height int `xml:"height,attr,omitempty"`
8686
// Embedded image content
87-
Data *Data `xml:"data,attr"`
87+
Data *Data `xml:"data,attr,omitempty"`
8888
}

tmx_layer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type Layer struct {
7878
// Rendering offset for this layer in pixels. Defaults to 0. (since 0.14)
7979
OffsetY int `xml:"offsety,attr"`
8080
// Custom properties
81-
Properties Properties `xml:"properties>property"`
81+
Properties *Properties `xml:"properties"`
8282
// 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].
8383
Tiles []*LayerTile
8484
// Data

tmx_map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ type Map struct {
7979
// 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)
8080
NextObjectID uint32 `xml:"nextobjectid,attr"`
8181
// Custom properties
82-
Properties *Properties `xml:"properties>property"`
82+
Properties *Properties `xml:"properties"`
8383
// Map tilesets
8484
Tilesets []*Tileset `xml:"tileset"`
8585
// Map layers

tmx_object.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,60 +39,60 @@ type ObjectGroup struct {
3939
// Unique ID of the layer.
4040
// Each layer that added to a map gets a unique id. Even if a layer is deleted,
4141
// no layer ever gets the same ID. Can not be changed in Tiled. (since Tiled 1.2)
42-
ID uint32 `xml:"id,attr"`
42+
ID uint32 `xml:"id,attr,omitempty"`
4343
// The name of the object group.
44-
Name string `xml:"name,attr"`
44+
Name string `xml:"name,attr,omitempty"`
4545
// The color used to display the objects in this group.
46-
Color *HexColor `xml:"color,attr"`
46+
Color *HexColor `xml:"color,attr,omitempty"`
4747
// The opacity of the layer as a value from 0 to 1. Defaults to 1.
48-
Opacity float32 `xml:"opacity,attr"`
48+
Opacity float32 `xml:"opacity,attr,omitempty"`
4949
// Whether the layer is shown (1) or hidden (0). Defaults to 1.
50-
Visible bool `xml:"visible,attr"`
50+
Visible *bool `xml:"visible,attr"` // TODO: default to non 0 value
5151
// Rendering offset for this layer in pixels. Defaults to 0. (since 0.14)
52-
OffsetX int `xml:"offsetx,attr"`
52+
OffsetX int `xml:"offsetx,attr,omitempty"`
5353
// Rendering offset for this layer in pixels. Defaults to 0. (since 0.14)
54-
OffsetY int `xml:"offsety,attr"`
54+
OffsetY int `xml:"offsety,attr,omitempty"`
5555
// Whether the objects are drawn according to the order of appearance ("index") or sorted by their y-coordinate ("topdown"). Defaults to "topdown".
56-
DrawOrder string `xml:"draworder,attr"`
56+
DrawOrder string `xml:"draworder,attr,omitempty"`
5757
// Custom properties
58-
Properties Properties `xml:"properties>property"`
58+
Properties *Properties `xml:"properties,omitempty"`
5959
// Group objects
60-
Objects []*Object `xml:"object"`
60+
Objects []*Object `xml:"object,omitempty"`
6161
}
6262

6363
// Object is used to add custom information to your tile map, such as spawn points, warps, exits, etc.
6464
type Object struct {
6565
// 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.
6666
// Can not be changed in Tiled Qt. (since Tiled 0.11)
67-
ID uint32 `xml:"id,attr"`
67+
ID uint32 `xml:"id,attr,omitempty"`
6868
// The name of the object. An arbitrary string.
69-
Name string `xml:"name,attr"`
69+
Name string `xml:"name,attr,omitempty"`
7070
// The type of the object. An arbitrary string.
71-
Type string `xml:"type,attr"`
71+
Type string `xml:"type,attr,omitempty"`
7272
// The x coordinate of the object.
73-
X float64 `xml:"x,attr"`
73+
X float64 `xml:"x,attr,omitempty"`
7474
// The y coordinate of the object.
75-
Y float64 `xml:"y,attr"`
75+
Y float64 `xml:"y,attr,omitempty"`
7676
// The width of the object (defaults to 0).
77-
Width float64 `xml:"width,attr"`
77+
Width float64 `xml:"width,attr,omitempty"`
7878
// The height of the object (defaults to 0).
79-
Height float64 `xml:"height,attr"`
79+
Height float64 `xml:"height,attr,omitempty"`
8080
// The rotation of the object in degrees clockwise (defaults to 0). (since 0.10)
81-
Rotation float64 `xml:"rotation,attr"`
81+
Rotation float64 `xml:"rotation,attr,omitempty"`
8282
// An reference to a tile (optional).
83-
GID uint32 `xml:"gid,attr"`
83+
GID uint32 `xml:"gid,attr,omitempty"`
8484
// Whether the object is shown (1) or hidden (0). Defaults to 1. (since 0.9)
85-
Visible bool `xml:"visible,attr"`
85+
Visible *bool `xml:"visible,attr"` // TODO: default not 0
8686
// Custom properties
87-
Properties Properties `xml:"properties>property"`
87+
Properties *Properties `xml:"properties,omitempty"`
8888
// 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.
89-
Ellipses []*Ellipse `xml:"ellipse"`
89+
Ellipses []*Ellipse `xml:"ellipse,omitempty"`
9090
// Polygons
91-
Polygons []*Polygon `xml:"polygon"`
91+
Polygons []*Polygon `xml:"polygon,omitempty"`
9292
// Poly lines
93-
PolyLines []*PolyLine `xml:"polyline"`
93+
PolyLines []*PolyLine `xml:"polyline,omitempty"`
9494
// Text
95-
Text *Text `xml:"text"`
95+
Text *Text `xml:"text,omitempty"`
9696
}
9797

9898
// Ellipse is used to mark an object as an ellipse.

tmx_property.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ package tiled
2525
import "strconv"
2626

2727
// Properties wraps any number of custom properties
28-
type Properties []*Property
28+
type Properties struct {
29+
Property []*Property `xml:"property"`
30+
}
2931

3032
// Property is used for custom properties
3133
type Property struct {
@@ -43,7 +45,7 @@ type Property struct {
4345
// Get finds all properties by specified name
4446
func (p Properties) Get(name string) []string {
4547
var values []string
46-
for _, property := range p {
48+
for _, property := range p.Property {
4749
if property.Name == name {
4850
values = append(values, property.Value)
4951
}
@@ -54,7 +56,7 @@ func (p Properties) Get(name string) []string {
5456
// GetString finds first string property by specified name
5557
func (p Properties) GetString(name string) string {
5658
var v string
57-
for _, property := range p {
59+
for _, property := range p.Property {
5860
if property.Name == name {
5961
if property.Type == "" {
6062
return property.Value
@@ -68,7 +70,7 @@ func (p Properties) GetString(name string) string {
6870

6971
// GetBool finds first bool property by specified name
7072
func (p Properties) GetBool(name string) bool {
71-
for _, property := range p {
73+
for _, property := range p.Property {
7274
if property.Name == name && property.Type == "boolean" {
7375
return property.Value == "true"
7476
}
@@ -78,7 +80,7 @@ func (p Properties) GetBool(name string) bool {
7880

7981
// GetInt finds first int property by specified name
8082
func (p Properties) GetInt(name string) int {
81-
for _, property := range p {
83+
for _, property := range p.Property {
8284
if property.Name == name && property.Type == "int" {
8385
v, err := strconv.Atoi(property.Value)
8486
if err != nil {
@@ -92,7 +94,7 @@ func (p Properties) GetInt(name string) int {
9294

9395
// GetFloat finds first float property by specified name
9496
func (p Properties) GetFloat(name string) float64 {
95-
for _, property := range p {
97+
for _, property := range p.Property {
9698
if property.Name == name && property.Type == "float" {
9799
v, err := strconv.ParseFloat(property.Value, 64)
98100
if err != nil {

tmx_tileset.go

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,46 @@ type Tileset struct {
99
// Base directory
1010
baseDir string
1111

12+
// XMLName holds the xml name for this struct
13+
XMLName struct{} `xml:"tileset"`
14+
1215
// The TMX format version, generally 1.0.
13-
Version string `xml:"version,attr"`
16+
Version string `xml:"version,attr,omitempty"`
1417
// The Tiled version used to generate this file
15-
TiledVersion string `xml:"tiledversion,attr"`
18+
TiledVersion string `xml:"tiledversion,attr,omitempty"`
1619
// The first global tile ID of this tileset (this global ID maps to the first tile in this tileset).
17-
FirstGID uint32 `xml:"firstgid,attr"`
20+
FirstGID uint32 `xml:"firstgid,attr,omitempty"`
1821
// If this tileset is stored in an external TSX (Tile Set XML) file, this attribute refers to that file.
1922
// That TSX file has the same structure as the <tileset> element described here. (There is the firstgid
2023
// attribute missing and this source attribute is also not there. These two attributes are kept in the
2124
// TMX map, since they are map specific.)
22-
Source string `xml:"source,attr"`
25+
Source string `xml:"source,attr,omitempty"`
2326
// External TSX source loaded.
2427
SourceLoaded bool `xml:"-"`
2528
// The name of this tileset.
26-
Name string `xml:"name,attr"`
29+
Name string `xml:"name,attr,omitempty"`
2730
// The (maximum) width of the tiles in this tileset.
28-
TileWidth int `xml:"tilewidth,attr"`
31+
TileWidth int `xml:"tilewidth,attr,omitempty"`
2932
// The (maximum) height of the tiles in this tileset.
30-
TileHeight int `xml:"tileheight,attr"`
33+
TileHeight int `xml:"tileheight,attr,omitempty"`
3134
// The spacing in pixels between the tiles in this tileset (applies to the tileset image).
32-
Spacing int `xml:"spacing,attr"`
35+
Spacing int `xml:"spacing,attr,omitempty"`
3336
// The margin around the tiles in this tileset (applies to the tileset image).
34-
Margin int `xml:"margin,attr"`
37+
Margin int `xml:"margin,attr,omitempty"`
3538
// The number of tiles in this tileset (since 0.13)
36-
TileCount int `xml:"tilecount,attr"`
39+
TileCount int `xml:"tilecount,attr,omitempty"`
3740
// 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)
38-
Columns int `xml:"columns,attr"`
41+
Columns int `xml:"columns,attr,omitempty"`
3942
// Offset in pixels, to be applied when drawing a tile from the related tileset. When not present, no offset is applied.
40-
TileOffset *TilesetTileOffset `xml:"tileoffset"`
43+
TileOffset *TilesetTileOffset `xml:"tileoffset,omitempty"`
4144
// Custom properties
42-
Properties Properties `xml:"properties>property"`
45+
Properties *Properties `xml:"properties,omitempty"`
4346
// Embedded image
44-
Image *Image `xml:"image"`
47+
Image *Image `xml:"image,omitempty"`
4548
// Defines an array of terrain types, which can be referenced from the terrain of the tile element.
46-
TerrainTypes []*Terrain `xml:"terraintypes>terrain"`
49+
TerrainTypes *TerrainTypes `xml:"terraintypes,omitempty"`
4750
// Tiles in tileset
48-
Tiles []*TilesetTile `xml:"tile"`
51+
Tiles []*TilesetTile `xml:"tile,omitempty"`
4952
}
5053

5154
// GetFileFullPath returns path to file relative to tileset file
@@ -61,36 +64,44 @@ type TilesetTileOffset struct {
6164
Y int `xml:"y,attr"`
6265
}
6366

67+
type TerrainTypes struct {
68+
Terrain []*Terrain
69+
}
70+
6471
// Terrain type
6572
type Terrain struct {
6673
// The name of the terrain type.
6774
Name string `xml:"name,attr"`
6875
// The local tile-id of the tile that represents the terrain visually.
6976
Tile uint32 `xml:"tile,attr"`
7077
// Custom properties
71-
Properties Properties `xml:"properties>property"`
78+
Properties *Properties `xml:"properties"`
7279
}
7380

7481
// TilesetTile information
7582
type TilesetTile struct {
7683
// The local tile ID within its tileset.
7784
ID uint32 `xml:"id,attr"`
7885
// The type of the tile. Refers to an object type and is used by tile objects. (optional) (since 1.0)
79-
Type string `xml:"type,attr"`
86+
Type string `xml:"type,attr,omitempty"`
8087
// Defines the terrain type of each corner of the tile, given as comma-separated indexes in the terrain types
8188
// array in the order top-left, top-right, bottom-left, bottom-right.
8289
// Leaving out a value means that corner has no terrain. (optional) (since 0.9)
83-
Terrain string `xml:"terrain,attr"`
90+
Terrain string `xml:"terrain,attr,omitempty"`
8491
// 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)
85-
Probability int `xml:"probability,attr"`
92+
Probability int `xml:"probability,attr,omitempty"`
8693
// Custom properties
87-
Properties Properties `xml:"properties>property"`
94+
Properties *Properties `xml:"properties,omitempty"`
8895
// Embedded image
8996
Image *Image `xml:"image"`
9097
// Tile object groups
9198
ObjectGroups []*ObjectGroup `xml:"objectgroup"`
9299
// List of animation frames
93-
Animation []*AnimationFrame `xml:"animation>frame"`
100+
Animation *Animation `xml:"animation,omitempty"`
101+
}
102+
103+
type Animation struct {
104+
Frame []*AnimationFrame `xml:"frame"`
94105
}
95106

96107
// AnimationFrame is single frame of animation

0 commit comments

Comments
 (0)