-
Notifications
You must be signed in to change notification settings - Fork 43
/
spine.go
134 lines (115 loc) · 2.5 KB
/
spine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bufio"
"bytes"
"errors"
"image"
"strings"
)
const (
statNone = 0
statAtlasPart = 1
statFramePart = 2
)
type SpineAtlasPart struct {
Name string
Parts map[string]*FramePart
}
type FramePart struct {
Name string
Rotate string
XY string
Size string
Orig string
Offset string
Index string
}
type SpineAtlas struct {
Parts map[string]*SpineAtlasPart
}
func dumpSpine(c *DumpContext) error {
reader := bufio.NewReader(bytes.NewReader(c.FileContent))
state := 0
atlas := SpineAtlas{}
atlas.Parts = map[string]*SpineAtlasPart{}
var currentAtlasPart *SpineAtlasPart
var currentFramePart *FramePart
for {
linebytes, _, err := reader.ReadLine()
if err != nil {
break
}
line := string(linebytes)
if line == "" {
state = statAtlasPart
currentAtlasPart = nil
continue
}
if state == statAtlasPart {
if currentAtlasPart == nil {
currentAtlasPart = &SpineAtlasPart{
Name: strings.TrimSpace(line),
Parts: map[string]*FramePart{},
}
atlas.Parts[currentAtlasPart.Name] = currentAtlasPart
continue
}
if strings.Index(line, ":") == -1 {
state = statFramePart
}
}
if state == statFramePart {
if strings.HasPrefix(line, " ") {
line = strings.TrimSpace(line)
kv := strings.Split(line, ":")
v := strings.TrimSpace(kv[1])
switch kv[0] {
case "rotate":
currentFramePart.Rotate = v
case "xy":
currentFramePart.XY = v
case "size":
currentFramePart.Size = v
case "orig":
currentFramePart.Orig = v
case "offset":
currentFramePart.Offset = v
case "index":
currentFramePart.Index = v
default:
return errors.New("error parse line " + line)
}
} else {
currentFramePart = nil
}
if currentFramePart == nil {
currentFramePart = &FramePart{
Name: strings.TrimSpace(line),
}
currentAtlasPart.Parts[currentFramePart.Name] = currentFramePart
continue
}
}
}
for _, sp := range atlas.Parts {
part := c.AppendPart()
for _, spf := range sp.Parts {
xy := intArr(spf.XY)
orig := intArr(spf.Orig)
offset := intArr(spf.Offset)
size := intArr(spf.Size)
ro := false
if spf.Rotate == "true" {
ro = true
}
part.ImageFile = sp.Name
part.Frames[spf.Name] = &Frame{
Rotated: ifelse(ro, 270, 0),
OriginalSize: image.Pt(orig[0], orig[1]),
Offset: image.Pt(offset[0], offset[1]),
Rect: image.Rect(xy[0], xy[1], xy[0]+size[0], xy[1]+size[1]),
}
}
}
return nil
}