Skip to content

Commit

Permalink
Object: added support for templates (#54)
Browse files Browse the repository at this point in the history
Co-authored-by: Lauris BH <lauris@nix.lv>
  • Loading branch information
pierrec and lafriks authored Mar 30, 2021
1 parent dd74439 commit cb24c07
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tmx_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package tiled
import (
"encoding/xml"
"errors"
"path/filepath"
"strconv"
"strings"
)
Expand Down Expand Up @@ -69,6 +70,9 @@ func (g *ObjectGroup) DecodeObjectGroup(m *Map) {
// won't be loaded.
m.TileGIDToTile(object.GID)
}
if len(object.TemplateSource) > 0 {
object.initTemplate(m)
}
}
}

Expand Down Expand Up @@ -119,6 +123,41 @@ type Object struct {
PolyLines []*PolyLine `xml:"polyline"`
// Text
Text *Text `xml:"text"`
// Template
TemplateSource string `xml:"template,attr"`
TemplateLoaded bool `xml:"-"`
Template *Template
}

func (o *Object) initTemplate(m *Map) error {
if o.TemplateLoaded {
return nil
}
if len(o.TemplateSource) == 0 {
o.TemplateLoaded = true
return nil
}
sourcePath := m.GetFileFullPath(o.TemplateSource)
f, err := m.loader.open(sourcePath)
if err != nil {
return err
}
defer f.Close()

d := xml.NewDecoder(f)
if err := d.Decode(&o.Template); err != nil {
return err
}
o.TemplateLoaded = true

if o.Template == nil || o.Template.Tileset == nil || o.Template.Object == nil {
return nil
}
if src := o.Template.Tileset.Source; len(src) > 0 {
// The tileset source may be relative from the template location.
o.Template.Tileset.Source = filepath.Join(filepath.Dir(o.TemplateSource), src)
}
return m.initTileset(o.Template.Tileset)
}

// UnmarshalXML decodes a single XML element beginning with the given start element.
Expand Down
29 changes: 29 additions & 0 deletions tmx_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright (c) 2021 Lauris Bukšis-Haberkorns <lauris@nix.lv>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package tiled

// Template is used for custom properties.
type Template struct {
Tileset *Tileset `xml:"tileset"`
Object *Object `xml:"object"`
}

0 comments on commit cb24c07

Please sign in to comment.