Skip to content

Commit

Permalink
Address #35 by removing Save feature.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hegh committed Dec 7, 2024
1 parent 13b7906 commit f535a77
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 122 deletions.
9 changes: 8 additions & 1 deletion assets/tilesets/testLoadTileset.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.2" tiledversion="1.2.3" name="ProjectUtumno_full" tilewidth="32" tileheight="32" tilecount="6080" columns="64">
<image source="ProjectUtumno_full.png" width="2048" height="3040"/>
<tile id="116" type="door"></tile>
<properties>
<property name="testTilesetProperty" value="valueOfTilesetProperty"/>
</properties>
<tile id="116" type="door">
<properties>
<property name="testTileProperty" type="int" value="7"/>
</properties>
</tile>
</tileset>
40 changes: 6 additions & 34 deletions tiled.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
6 changes: 3 additions & 3 deletions tiled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions tmx_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,20 @@ 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
}

// SetDefaults provides default values for Text.
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{}
Expand Down
2 changes: 1 addition & 1 deletion tmx_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions tmx_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"`
}
2 changes: 1 addition & 1 deletion tmx_layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tmx_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 6 additions & 8 deletions tmx_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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
Expand All @@ -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"
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
41 changes: 19 additions & 22 deletions tmx_property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,30 @@ 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",
},
}

assert.Equal(t, "string-value", props.GetString("string-name"))
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"))

}
58 changes: 16 additions & 42 deletions tmx_tileset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ SOFTWARE.
package tiled

import (
"encoding/xml"
"image"
"io"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -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
}

0 comments on commit f535a77

Please sign in to comment.