Skip to content

Commit cb24c07

Browse files
pierreclafriks
andauthored
Object: added support for templates (lafriks#54)
Co-authored-by: Lauris BH <[email protected]>
1 parent dd74439 commit cb24c07

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

tmx_object.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package tiled
2525
import (
2626
"encoding/xml"
2727
"errors"
28+
"path/filepath"
2829
"strconv"
2930
"strings"
3031
)
@@ -69,6 +70,9 @@ func (g *ObjectGroup) DecodeObjectGroup(m *Map) {
6970
// won't be loaded.
7071
m.TileGIDToTile(object.GID)
7172
}
73+
if len(object.TemplateSource) > 0 {
74+
object.initTemplate(m)
75+
}
7276
}
7377
}
7478

@@ -119,6 +123,41 @@ type Object struct {
119123
PolyLines []*PolyLine `xml:"polyline"`
120124
// Text
121125
Text *Text `xml:"text"`
126+
// Template
127+
TemplateSource string `xml:"template,attr"`
128+
TemplateLoaded bool `xml:"-"`
129+
Template *Template
130+
}
131+
132+
func (o *Object) initTemplate(m *Map) error {
133+
if o.TemplateLoaded {
134+
return nil
135+
}
136+
if len(o.TemplateSource) == 0 {
137+
o.TemplateLoaded = true
138+
return nil
139+
}
140+
sourcePath := m.GetFileFullPath(o.TemplateSource)
141+
f, err := m.loader.open(sourcePath)
142+
if err != nil {
143+
return err
144+
}
145+
defer f.Close()
146+
147+
d := xml.NewDecoder(f)
148+
if err := d.Decode(&o.Template); err != nil {
149+
return err
150+
}
151+
o.TemplateLoaded = true
152+
153+
if o.Template == nil || o.Template.Tileset == nil || o.Template.Object == nil {
154+
return nil
155+
}
156+
if src := o.Template.Tileset.Source; len(src) > 0 {
157+
// The tileset source may be relative from the template location.
158+
o.Template.Tileset.Source = filepath.Join(filepath.Dir(o.TemplateSource), src)
159+
}
160+
return m.initTileset(o.Template.Tileset)
122161
}
123162

124163
// UnmarshalXML decodes a single XML element beginning with the given start element.

tmx_template.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright (c) 2021 Lauris Bukšis-Haberkorns <[email protected]>
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
package tiled
24+
25+
// Template is used for custom properties.
26+
type Template struct {
27+
Tileset *Tileset `xml:"tileset"`
28+
Object *Object `xml:"object"`
29+
}

0 commit comments

Comments
 (0)