Skip to content

Commit 013284d

Browse files
committed
helper funcs
1 parent 518941c commit 013284d

5 files changed

Lines changed: 60 additions & 15 deletions

File tree

_examples/parser/main.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"strconv"
56

67
"github.com/anthonynsimon/bild/imgio"
@@ -15,14 +16,22 @@ func main() {
1516
panic(err)
1617
}
1718

18-
ase.BuildTilemapImages()
19+
fmt.Println(len(ase.Tags))
20+
21+
tag2 := ase.GetTagByName("tag2")
22+
myImageLayer := ase.GetLayerByName("my image layer")
23+
imgio.Save(
24+
myImageLayer.Name+"_"+tag2.Name+"_start.png",
25+
myImageLayer.Cel(tag2.Start).Image,
26+
imgio.PNGEncoder(),
27+
)
1928

29+
ase.BuildTilemapImages()
2030
frame := 0
2131
for i, layer := range ase.Layers {
22-
if layer.IsCelEmpty(0) || layer.Type == aseprite.Group {
23-
continue
32+
if !layer.IsCelEmpty(frame) && layer.IsTilemapLayer() {
33+
imgio.Save(strconv.Itoa(i)+"_"+layer.Name+".png", layer.Cel(frame).Image, imgio.PNGEncoder())
2434
}
25-
imgio.Save(strconv.Itoa(i)+"_"+layer.Name+".png", layer.Cel(frame).Image, imgio.PNGEncoder())
2635
}
2736

2837
}

ase.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type Ase struct {
3737
Slices []Slice
3838

3939
// Timeline animation tags
40-
Tags []Tag
40+
Tags []*Tag
4141

4242
// Tilesets used in Tilemap layers
4343
Tilesets []*Tileset
@@ -63,6 +63,18 @@ func (a *Ase) GetLayerByName(name string) *Layer {
6363
return a.Layers[idx]
6464
}
6565

66+
// GetTagByName returns the first Tag with the given name.
67+
// Returns nil if no layer is found with that name.
68+
func (a *Ase) GetTagByName(tagName string) *Tag {
69+
idx := slices.IndexFunc(a.Tags, func(t *Tag) bool {
70+
return t.Name == tagName
71+
})
72+
if idx == -1 {
73+
return nil
74+
}
75+
return a.Tags[idx]
76+
}
77+
6678
// GetLayerByUUID returns the layer with the given UUID.
6779
// Returns nil if no layer is found with that UUID.
6880
func (a *Ase) GetLayerByUUID(id uuid.UUID) *Layer {
@@ -218,7 +230,7 @@ func (a *Ase) parse(r io.Reader, onlyVisible bool) (filesize int64, err error) {
218230
startIdx := len(a.Tags)
219231
a.Tags = append(a.Tags, newTags...)
220232
for k := range newTags {
221-
tagQueue = append(tagQueue, &a.Tags[startIdx+k])
233+
tagQueue = append(tagQueue, a.Tags[startIdx+k])
222234
}
223235
lastUserDataTarget = nil
224236
case 0x2022:
@@ -357,18 +369,21 @@ func (a *Ase) parseSlice(raw []byte, totalFrames int) Slice {
357369
}
358370

359371
// Chunk0x2018
360-
func (a *Ase) parseTags(raw []byte) []Tag {
372+
func (a *Ase) parseTags(raw []byte) []*Tag {
361373
ntags := int(binary.LittleEndian.Uint16(raw))
362-
tags := make([]Tag, ntags)
374+
tags := make([]*Tag, 0, ntags)
363375
ptr := raw[10:]
364-
for i := range ntags {
365-
t := &tags[i]
376+
377+
for range ntags {
378+
t := &Tag{}
366379
t.Start = int(binary.LittleEndian.Uint16(ptr))
367380
t.End = int(binary.LittleEndian.Uint16(ptr[2:]))
368381
t.LoopDirection = LoopDirection(ptr[4])
369382
t.Repeat = binary.LittleEndian.Uint16(ptr[5:])
370383
nameLen := binary.LittleEndian.Uint16(ptr[17:])
371384
t.Name = string(ptr[19 : 19+nameLen])
385+
386+
tags = append(tags, t)
372387
ptr = ptr[19+nameLen:]
373388
}
374389

ase_test.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ func TestAseDataStructure(t *testing.T) {
151151
Type: 2,
152152
Opacity: 255,
153153
Image: &image.Paletted{},
154+
UserData: UserData{
155+
Text: "tag2_start",
156+
},
154157
},
155158
{
156159
LayerIndex: 2,
@@ -178,7 +181,7 @@ func TestAseDataStructure(t *testing.T) {
178181
},
179182
},
180183
},
181-
Tags: []Tag{
184+
Tags: []*Tag{
182185
{
183186
Name: "tag1",
184187
UserData: UserData{
@@ -252,14 +255,25 @@ func TestAseProp(t *testing.T) {
252255

253256
func TestPalettedTilemapRender(t *testing.T) {
254257

255-
a, err := Read("test_files/test_paletted.ase", false)
258+
ase, err := Read("test_files/test_paletted.ase", false)
256259
if err != nil {
257260
t.Fatalf("Failed to read file: %v", err)
258261
}
259262

260-
a.BuildTilemapImages()
263+
tag2 := ase.GetTagByName("tag2")
264+
imlayer := ase.GetLayerByName("my image layer")
261265

262-
gotImage := a.GetLayerByName("test tilemap").Cel(0).Image
266+
if imlayer.Cel(tag2.Start).UserData.Text != "tag2_start" {
267+
t.Errorf("tag2 start userdata text is not tag2_start")
268+
}
269+
270+
ase.BuildTilemapImages()
271+
272+
if !ase.GetLayerByName("test tilemap").IsTilemapLayer() {
273+
t.Errorf("test tilemap layer is not tilemap layer")
274+
}
275+
276+
gotImage := ase.GetLayerByName("test tilemap").Cel(0).Image
263277

264278
gotImageP, ok := gotImage.(*image.Paletted)
265279

@@ -557,7 +571,7 @@ func compareAse(t *testing.T, exp, act *Ase) {
557571
t.Errorf("Tags count: expected %d, got %d", len(exp.Tags), len(act.Tags))
558572
} else {
559573
for i := range exp.Tags {
560-
compareTag(t, i, &exp.Tags[i], &act.Tags[i])
574+
compareTag(t, i, exp.Tags[i], act.Tags[i])
561575
}
562576
}
563577

test_files/test_paletted.ase

22 Bytes
Binary file not shown.

types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,13 @@ func (l *Layer) IsReferenceLayer() bool {
361361
return l.Flags&64 != 0
362362
}
363363

364+
func (l *Layer) IsTilemapLayer() bool {
365+
return l.Type == Tilemap
366+
}
367+
func (l *Layer) IsGroupLayer() bool {
368+
return l.Type == Group
369+
}
370+
364371
// GetTileset returns the Tileset used by Tilemap layers.
365372
// It returns nil if layer is not a tilemap.
366373
func (l *Layer) GetTileset() *Tileset {

0 commit comments

Comments
 (0)