-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayerset.go
298 lines (278 loc) · 6.5 KB
/
layerset.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package ps
import (
"encoding/json"
"fmt"
"log"
"strings"
)
// LayerSet holds a group of Layer objects and a group of LayerSet objects.
type LayerSet struct {
name string
bounds [2][2]int
parent Group
current bool // Whether we've checked this layer since we loaded from disk.
visible bool
artLayers []*ArtLayer
layerSets []*LayerSet
}
// LayerSetJSON is an exported version of LayerSet, that allows LayerSets to be
// saved to and loaded from JSON.
type LayerSetJSON struct {
Name string
Bounds [2][2]int
Visible bool
ArtLayers []*ArtLayer
LayerSets []*LayerSet
}
// MarshalJSON returns the LayerSet in JSON form.
func (l *LayerSet) MarshalJSON() ([]byte, error) {
return json.Marshal(&LayerSetJSON{
Name: l.name,
Bounds: l.bounds,
Visible: l.visible,
ArtLayers: l.artLayers,
LayerSets: l.layerSets,
})
}
// UnmarshalJSON loads the json data into this LayerSet.
func (l *LayerSet) UnmarshalJSON(b []byte) error {
tmp := &LayerSetJSON{}
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
l.name = tmp.Name
l.bounds = tmp.Bounds
l.visible = tmp.Visible
l.artLayers = tmp.ArtLayers
for _, lyr := range l.artLayers {
lyr.SetParent(l)
}
l.layerSets = tmp.LayerSets
for _, set := range l.layerSets {
set.SetParent(l)
}
l.current = false
return nil
}
// Name returns the name of the LayerSet
func (l LayerSet) Name() string {
return l.name
}
// ArtLayers returns the LayerSet's ArtLayers.
func (l *LayerSet) ArtLayers() []*ArtLayer {
if Mode != Fast {
for _, lyr := range l.artLayers {
if !lyr.current {
if err := lyr.Refresh(); err != nil {
log.Println(err)
}
lyr.current = true
}
}
}
return l.artLayers
}
// ArtLayer returns the first top level ArtLayer matching
// the given name.
func (l *LayerSet) ArtLayer(name string) *ArtLayer {
for _, lyr := range l.artLayers {
if lyr.name == name {
if Mode == 0 && !lyr.current {
err := lyr.Refresh()
if err != nil {
if err = l.Refresh(); err != nil {
log.Panic(err)
}
if err := lyr.Refresh(); err != nil {
log.Panic(err)
}
}
}
return lyr
}
}
return nil
}
// LayerSets returns the LayerSets contained within
// this set.
func (l *LayerSet) LayerSets() []*LayerSet {
return l.layerSets
}
// LayerSet returns the first top level LayerSet matching
// the given name.
func (l *LayerSet) LayerSet(name string) *LayerSet {
for _, set := range l.layerSets {
if set.name == name {
return set
}
}
return nil
}
// MustExist returns a Layer from the set with the given name, and
// panics if it doesn't exist.
//
// If there is a LayerSet and an ArtLayer with the same name,
// it will return the LayerSet.
func (l *LayerSet) MustExist(name string) Layer {
set := l.LayerSet(name)
if set == nil {
lyr := l.ArtLayer(name)
if lyr == nil {
log.Panicf("no Layer found at \"%s%s\"", l.Path(), name)
}
return lyr
}
return set
}
// Bounds returns the furthest corners of the LayerSet.
func (l LayerSet) Bounds() [2][2]int {
return l.bounds
}
// SetParent puts this LayerSet into the given group.
func (l *LayerSet) SetParent(g Group) {
l.parent = g
}
// Parent returns this layerSet's parent.
func (l *LayerSet) Parent() Group {
return l.parent
}
// Path returns the layer path to this Set.
func (l *LayerSet) Path() string {
if l.parent == nil {
return l.name
}
return fmt.Sprintf("%s%s/", l.parent.Path(), l.name)
}
// NewLayerSet grabs the LayerSet with the given path and returns it.
func NewLayerSet(path string, g Group) (*LayerSet, error) {
path = strings.Replace(path, "//", "/", -1)
byt, err := DoJS("getLayerSet.jsx", JSLayer(path), JSLayerMerge(path))
if err != nil {
return nil, err
}
var out *LayerSet
err = json.Unmarshal(byt, &out)
if err != nil {
log.Println(JSLayer(path))
log.Println(string(byt))
return nil, err
}
out.SetParent(g)
log.Printf("Loading ActiveDocument/%s\n", out.Path())
for _, lyr := range out.artLayers {
lyr.SetParent(out)
}
for i, set := range out.layerSets {
var s *LayerSet
s, err = NewLayerSet(fmt.Sprintf("%s%s/", path, set.Name()), out)
if err != nil {
log.Fatal(err)
}
out.layerSets[i] = s
s.SetParent(out)
}
out.current = true
return out, err
}
// Visible returns whether or not the LayerSet is currently visible.
func (l LayerSet) Visible() bool {
return l.visible
}
// SetVisible makes the LayerSet visible.
func (l *LayerSet) SetVisible(b bool) error {
if l.visible == b {
return nil
}
js := fmt.Sprintf("%s.visible=%v;", JSLayer(l.Path()), b)
if _, err := DoJS("compilejs.jsx", js); err != nil {
return err
}
l.visible = b
return nil
}
// SetPos snaps the given layerset boundary to the given point.
// Valid options for bound are: "TL", "TR", "BL", "BR"
func (l *LayerSet) SetPos(x, y int, bound string) {
if !l.visible || (x == 0 && y == 0) {
return
}
path := JSLayer(l.Path())
mrgPath := JSLayerMerge(l.Path())
byt, err := DoJS("LayerSetBounds.jsx", path, mrgPath)
if err != nil {
log.Println(string(byt))
log.Panic(err)
}
var bnds *[2][2]int
err = json.Unmarshal(byt, &bnds)
if err != nil {
fmt.Println(string(byt))
log.Panic(err)
}
l.bounds = *bnds
var lyrX, lyrY int
switch bound[:1] {
case "B":
lyrY = l.bounds[1][1]
case "T":
fallthrough
default:
lyrY = l.bounds[0][1]
}
switch bound[1:] {
case "R":
lyrX = l.bounds[1][0]
case "L":
fallthrough
default:
lyrX = l.bounds[0][0]
}
byt, err = DoJS("moveLayer.jsx", JSLayer(l.Path()), fmt.Sprint(x-lyrX),
fmt.Sprint(y-lyrY), JSLayerMerge(l.Path()))
if err != nil {
fmt.Println("byte:", string(byt))
panic(err)
}
var lyr LayerSet
if err = json.Unmarshal(byt, &lyr); err != nil {
fmt.Println("byte:", string(byt))
log.Panic(err)
}
l.bounds = lyr.bounds
}
// Refresh syncs the LayerSet with Photoshop.
func (l *LayerSet) Refresh() error {
var tmp *LayerSet
byt, err := DoJS("getLayerSet.jsx", JSLayer(l.Path()))
if err != nil {
if err = DoAction("Undo", "DK"); err != nil {
return err
}
if byt, err = DoJS("getLayerSet.jsx", JSLayer(l.Path())); err != nil {
return err
}
}
err = json.Unmarshal(byt, &tmp)
if err != nil {
log.Println("Error in LayerSet.Refresh() \"", string(byt), "\"", "for", l.Path())
return err
}
tmp.SetParent(l.Parent())
for _, lyr := range l.artLayers {
err = lyr.Refresh()
if err != nil {
l.artLayers = tmp.artLayers
break
}
}
for _, set := range l.layerSets {
if err = set.Refresh(); err != nil {
return err
}
}
l.name = tmp.name
l.bounds = tmp.bounds
l.visible = tmp.visible
l.current = true
return nil
}