Skip to content

Commit ce869d1

Browse files
Functions to load/save tileset
1 parent 8e95b8a commit ce869d1

File tree

13 files changed

+332
-95
lines changed

13 files changed

+332
-95
lines changed
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>
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>

tiled.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,27 @@ 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+
}
113+
114+
func b(v bool) *bool {
115+
return &v
116+
}

tiled_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func TestFont(t *testing.T) {
184184
assert.Equal(t, false, text.Italic)
185185
assert.Equal(t, false, text.Underline)
186186
assert.Equal(t, false, text.Strikethrough)
187-
assert.Equal(t, true, text.Kerning)
187+
assert.Equal(t, true, *text.Kerning)
188188
assert.Equal(t, "left", text.HAlign)
189189
assert.Equal(t, "top", text.VAlign)
190190
}

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
@@ -79,7 +79,7 @@ type Layer struct {
7979
// Rendering offset for this layer in pixels. Defaults to 0. (since 0.14)
8080
OffsetY int `xml:"offsety,attr"`
8181
// Custom properties
82-
Properties Properties `xml:"properties>property"`
82+
Properties *Properties `xml:"properties"`
8383
// 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].
8484
Tiles []*LayerTile
8585
// 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: 37 additions & 37 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"`
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"`
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.
@@ -162,27 +162,27 @@ type Text struct {
162162
// The actual text
163163
Text string `xml:",chardata"`
164164
// The font family used (default: "sans-serif")
165-
FontFamily string `xml:"fontfamily,attr"`
165+
FontFamily string `xml:"fontfamily,attr,omitempty"`
166166
// The size of the font in pixels (not using points, because other sizes in the TMX format are also using pixels) (default: 16)
167-
Size int `xml:"pixelsize,attr"`
167+
Size int `xml:"pixelsize,attr,omitempty"`
168168
// Whether word wrapping is enabled (1) or disabled (0). Defaults to 0.
169-
Wrap bool `xml:"wrap,attr"`
169+
Wrap bool `xml:"wrap,attr,omitempty"`
170170
// Color of the text in #AARRGGBB or #RRGGBB format (default: #000000)
171-
Color *HexColor `xml:"color,attr"`
171+
Color *HexColor `xml:"color,attr,omitempty"`
172172
// Whether the font is bold (1) or not (0). Defaults to 0.
173-
Bold bool `xml:"bold,attr"`
173+
Bold bool `xml:"bold,attr,omitempty"`
174174
// Whether the font is italic (1) or not (0). Defaults to 0.
175-
Italic bool `xml:"italic,attr"`
175+
Italic bool `xml:"italic,attr,omitempty"`
176176
// Whether a line should be drawn below the text (1) or not (0). Defaults to 0.
177-
Underline bool `xml:"underline,attr"`
177+
Underline bool `xml:"underline,attr,omitempty"`
178178
// Whether a line should be drawn through the text (1) or not (0). Defaults to 0.
179-
Strikethrough bool `xml:"strikeout,attr"`
179+
Strikethrough bool `xml:"strikeout,attr,omitempty"`
180180
// Whether kerning should be used while rendering the text (1) or not (0). Default to 1.
181-
Kerning bool `xml:"kerning,attr"`
181+
Kerning *bool `xml:"kerning,attr,omitempty"`
182182
// Horizontal alignment of the text within the object (left (default), center, right or justify (since Tiled 1.2.1))
183-
HAlign string `xml:"halign,attr"`
183+
HAlign string `xml:"halign,attr,omitempty"`
184184
// Vertical alignment of the text within the object (top (default), center or bottom)
185-
VAlign string `xml:"valign,attr"`
185+
VAlign string `xml:"valign,attr,omitempty"`
186186
}
187187

188188
// 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 {
192192
item := Alias{
193193
FontFamily: "sans-serif",
194194
Size: 16,
195-
Kerning: true,
195+
Kerning: b(true),
196196
HAlign: "left",
197197
VAlign: "top",
198198
Color: &HexColor{},

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 {

0 commit comments

Comments
 (0)