Skip to content

Commit f535a77

Browse files
committed
Address lafriks#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.
1 parent 13b7906 commit f535a77

File tree

11 files changed

+70
-122
lines changed

11 files changed

+70
-122
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<tileset version="1.2" tiledversion="1.2.3" name="ProjectUtumno_full" tilewidth="32" tileheight="32" tilecount="6080" columns="64">
33
<image source="ProjectUtumno_full.png" width="2048" height="3040"/>
4-
<tile id="116" type="door"></tile>
4+
<properties>
5+
<property name="testTilesetProperty" value="valueOfTilesetProperty"/>
6+
</properties>
7+
<tile id="116" type="door">
8+
<properties>
9+
<property name="testTileProperty" type="int" value="7"/>
10+
</properties>
11+
</tile>
512
</tileset>

tiled.go

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,6 @@ func (l *loader) LoadFile(fileName string) (*Map, error) {
121121
return l.LoadReader(dir, f)
122122
}
123123

124-
// LoadTilesetFromReader loads a tileset in TSX format from an io.Reader.
125-
// baseDir is used to locate relative pats to additional tile data; default is
126-
// the current directory if empty.
127-
func (l *loader) LoadTilesetFromReader(baseDir string, r io.Reader) (*Tileset, error) {
128-
d := xml.NewDecoder(r)
129-
130-
t := &Tileset{
131-
baseDir: baseDir,
132-
}
133-
if err := d.Decode(t); err != nil {
134-
return nil, err
135-
}
136-
137-
t.SourceLoaded = true
138-
return t, nil
139-
}
140-
141124
// LoadTilesetFile loads a tileset in TSX format from a file.
142125
func (l *loader) LoadTilesetFile(fileName string) (*Tileset, error) {
143126
f, err := l.open(fileName)
@@ -151,27 +134,16 @@ func (l *loader) LoadTilesetFile(fileName string) (*Tileset, error) {
151134
}
152135

153136
// LoadTilesetFromReader loads a .tsx file into a Tileset structure
154-
func LoadTilesetFromReader(baseDir string, r io.Reader) (*Tileset, error) {
137+
func (l *loader) LoadTilesetFromReader(baseDir string, r io.Reader) (*Tileset, error) {
155138
d := xml.NewDecoder(r)
156139

157-
m := &Tileset{
158-
baseDir: baseDir,
159-
SourceLoaded: true,
140+
t := &Tileset{
141+
baseDir: baseDir,
160142
}
161-
if err := d.Decode(m); err != nil {
143+
if err := d.Decode(t); err != nil {
162144
return nil, err
163145
}
164146

165-
return m, nil
166-
}
167-
168-
// SaveTilesetToWriter saves a Tileset structure into a given writer
169-
func SaveTilesetToWriter(tileset *Tileset, w io.Writer) error {
170-
encoder := xml.NewEncoder(w)
171-
encoder.Indent("", " ")
172-
return encoder.Encode(tileset)
173-
}
174-
175-
func b(v bool) *bool {
176-
return &v
147+
t.SourceLoaded = true
148+
return t, nil
177149
}

tiled_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ func TestLoadFile(t *testing.T) {
9696
// Test ObjectGroups.Visible defaults to true
9797
assert.Len(t, m.ObjectGroups, 1)
9898
assert.Equal(t, uint32(2), m.ObjectGroups[0].ID)
99-
assert.Equal(t, true, *m.ObjectGroups[0].Visible)
99+
assert.Equal(t, true, m.ObjectGroups[0].Visible)
100100

101101
// Test Object.Visible defaults to true
102102
assert.Len(t, m.ObjectGroups[0].Objects, 1)
103103
assert.Equal(t, uint32(2), m.ObjectGroups[0].Objects[0].ID)
104-
assert.Equal(t, true, *m.ObjectGroups[0].Objects[0].Visible)
104+
assert.Equal(t, true, m.ObjectGroups[0].Objects[0].Visible)
105105
}
106106

107107
func TestLoadFileError(t *testing.T) {
@@ -208,7 +208,7 @@ func TestFont(t *testing.T) {
208208
assert.Equal(t, false, text.Italic)
209209
assert.Equal(t, false, text.Underline)
210210
assert.Equal(t, false, text.Strikethrough)
211-
assert.Equal(t, true, *text.Kerning)
211+
assert.Equal(t, true, text.Kerning)
212212
assert.Equal(t, "left", text.HAlign)
213213
assert.Equal(t, "top", text.VAlign)
214214
}

tmx_defaults.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,20 @@ func (a *aliasMap) SetDefaults() {
5757

5858
// SetDefaults provides default values for Object.
5959
func (a *aliasObject) SetDefaults() {
60-
a.Visible = b(true)
60+
a.Visible = true
6161
}
6262

6363
// SetDefaults provides default values for ObjectGroup.
6464
func (a *aliasObjectGroup) SetDefaults() {
65-
a.Visible = b(true)
65+
a.Visible = true
6666
a.Opacity = 1
6767
}
6868

6969
// SetDefaults provides default values for Text.
7070
func (a *aliasText) SetDefaults() {
7171
a.FontFamily = "sans-serif"
7272
a.Size = 16
73-
a.Kerning = b(true)
73+
a.Kerning = true
7474
a.HAlign = "left"
7575
a.VAlign = "top"
7676
a.Color = &HexColor{}

tmx_group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type Group struct {
4949
// The parallax y factor of the layer 0 - 1.0
5050
ParallaxY float32 `xml:"parallaxy,attr"`
5151
// Custom properties
52-
Properties *Properties `xml:"properties"`
52+
Properties Properties `xml:"properties>property"`
5353
// Map layers
5454
Layers []*Layer `xml:"layer"`
5555
// Map object groups

tmx_image.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type ImageLayer struct {
4949
// Whether the layer is shown (1) or hidden (0). Defaults to 1.
5050
Visible bool `xml:"visible,attr"`
5151
// Custom properties
52-
Properties *Properties `xml:"properties"`
52+
Properties Properties `xml:"properties>property"`
5353
// The group image
5454
Image *Image `xml:"image"`
5555
// 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
7979
// Image source
8080
type Image struct {
8181
// Used for embedded images, in combination with a data child element. Valid values are file extensions like png, gif, jpg, bmp, etc.
82-
Format string `xml:"format,attr,omitempty"`
82+
Format string `xml:"format,attr"`
8383
// The reference to the tileset image file
8484
Source string `xml:"source,attr"`
8585
// Defines a specific color that is treated as transparent (example value: "#FF00FF" for magenta).
8686
// Up until Tiled 0.12, this value is written out without a # but this is planned to change.
87-
Trans *HexColor `xml:"trans,attr,omitempty"`
87+
Trans *HexColor `xml:"trans,attr"`
8888
// The image width in pixels (optional, used for tile index correction when the image changes)
89-
Width int `xml:"width,attr,omitempty"`
89+
Width int `xml:"width,attr"`
9090
// The image height in pixels (optional)
91-
Height int `xml:"height,attr,omitempty"`
91+
Height int `xml:"height,attr"`
9292
// Embedded image content
93-
Data *Data `xml:"data,attr,omitempty"`
93+
Data *Data `xml:"data,attr"`
9494
}

tmx_layer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ type Layer struct {
8585
// The parallax y factor of the layer 0 - 1.0
8686
ParallaxY float32 `xml:"parallaxy,attr"`
8787
// Custom properties
88-
Properties *Properties `xml:"properties"`
88+
Properties Properties `xml:"properties>property"`
8989
// 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].
9090
Tiles []*LayerTile
9191
// Data

tmx_map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ type Map struct {
9999
// 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)
100100
NextObjectID uint32 `xml:"nextobjectid,attr"`
101101
// Custom properties
102-
Properties *Properties `xml:"properties"`
102+
Properties *Properties `xml:"properties>property"`
103103
// Map tilesets
104104
Tilesets []*Tileset `xml:"tileset"`
105105
// Map layers

tmx_property.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ import (
2929
)
3030

3131
// Properties wraps any number of custom properties
32-
type Properties struct {
33-
Property []*Property `xml:"property"`
34-
}
32+
type Properties []*Property
3533

3634
// Property is used for custom properties
3735
type Property struct {
@@ -49,7 +47,7 @@ type Property struct {
4947
// Get finds all properties by specified name
5048
func (p Properties) Get(name string) []string {
5149
var values []string
52-
for _, property := range p.Property {
50+
for _, property := range p {
5351
if property.Name == name {
5452
values = append(values, property.Value)
5553
}
@@ -60,7 +58,7 @@ func (p Properties) Get(name string) []string {
6058
// GetString finds first string property by specified name
6159
func (p Properties) GetString(name string) string {
6260
var v string
63-
for _, property := range p.Property {
61+
for _, property := range p {
6462
if property.Name == name {
6563
if property.Type == "" {
6664
return property.Value
@@ -74,7 +72,7 @@ func (p Properties) GetString(name string) string {
7472

7573
// GetBool finds first bool property by specified name
7674
func (p Properties) GetBool(name string) bool {
77-
for _, property := range p.Property {
75+
for _, property := range p {
7876
if property.Name == name && property.Type == "boolean" {
7977
return property.Value == "true"
8078
}
@@ -84,7 +82,7 @@ func (p Properties) GetBool(name string) bool {
8482

8583
// GetInt finds first int property by specified name
8684
func (p Properties) GetInt(name string) int {
87-
for _, property := range p.Property {
85+
for _, property := range p {
8886
if property.Name == name && property.Type == "int" {
8987
v, err := strconv.Atoi(property.Value)
9088
if err != nil {
@@ -98,7 +96,7 @@ func (p Properties) GetInt(name string) int {
9896

9997
// GetFloat finds first float property by specified name
10098
func (p Properties) GetFloat(name string) float64 {
101-
for _, property := range p.Property {
99+
for _, property := range p {
102100
if property.Name == name && property.Type == "float" {
103101
v, err := strconv.ParseFloat(property.Value, 64)
104102
if err != nil {

tmx_property_test.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,30 @@ import (
3131
func TestGetProperty(t *testing.T) {
3232

3333
props := Properties{
34-
Property: []*Property{
35-
{
36-
Name: "string-name",
37-
Type: "string",
38-
Value: "string-value",
39-
},
40-
{
41-
Name: "int-name",
42-
Type: "int",
43-
Value: "123",
44-
},
45-
{
46-
Name: "float-name",
47-
Type: "float",
48-
Value: "1.23",
49-
},
50-
{
51-
Name: "bool-name",
52-
Type: "boolean",
53-
Value: "true",
54-
},
34+
{
35+
Name: "string-name",
36+
Type: "string",
37+
Value: "string-value",
38+
},
39+
{
40+
Name: "int-name",
41+
Type: "int",
42+
Value: "123",
43+
},
44+
{
45+
Name: "float-name",
46+
Type: "float",
47+
Value: "1.23",
48+
},
49+
{
50+
Name: "bool-name",
51+
Type: "boolean",
52+
Value: "true",
5553
},
5654
}
5755

5856
assert.Equal(t, "string-value", props.GetString("string-name"))
5957
assert.Equal(t, 123, props.GetInt("int-name"))
6058
assert.Equal(t, 1.23, props.GetFloat("float-name"))
6159
assert.Equal(t, true, props.GetBool("bool-name"))
62-
6360
}

0 commit comments

Comments
 (0)