Added Refresh function to update layers

* Fixed functions that were mode dependant.
* Fixed getLayerSet.jsx to return set visibility.
This commit is contained in:
Unknown
2018-04-03 16:56:12 -04:00
parent 669d1182f4
commit d52e896ac3
4 changed files with 95 additions and 47 deletions

View File

@@ -2,6 +2,7 @@ package ps
import (
"encoding/hex"
// "fmt"
)
// Color is an interface for color objects, allowing colors to be
@@ -64,3 +65,7 @@ type Stroke struct {
Size float32
Color
}
// func (s *Stroke) String() string {
// return fmt.Sprintf("%vpt %v", s.Size, s.Color.RGB())
// }

18
ps.go
View File

@@ -154,19 +154,11 @@ func DoAction(set, name string) error {
return err
}
// Layers returns an array of ArtLayers from the active document
// based on the given path string.
/*func Layers(path string) ([]ArtLayer, error) {
byt, err := DoJs("getLayers.jsx", JSLayer(path))
var out []ArtLayer
err = json.Unmarshal(byt, &out)
if err != nil {
return []ArtLayer{}, err
}
return out, err
}*/
// ApplyDataset fills out a template file with information from a given dataset (csv) file.
// ApplyDataset fills out a template file with information
// from a given dataset (csv) file. It is important to note that running this
// function will change data in the Photoshop document, but will not update
// data in the Go Document struct (if any); you will have to implement syncing
// them yourself.
func ApplyDataset(name string) ([]byte, error) {
return DoJs("applyDataset.jsx", name)
}

View File

@@ -19,7 +19,7 @@ for (var i = 0; i < set.artLayers.length; i++) {
stdout.write('], "LayerSets": [')
for (var i = 0; i < set.layerSets.length; i++) {
var s = set.layerSets[i];
stdout.write('{"Name": "'+ s.name +'"}');
stdout.write('{"Name": "'+ s.name +'", "Visible": '+s.visible+'}');
if (i < set.layerSets.length - 1)
stdout.writeln(",");
}

View File

@@ -2,7 +2,6 @@ package ps
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
@@ -153,6 +152,7 @@ func ActiveDocument() (*Document, error) {
}
func (d *Document) Dump() {
log.Println("Dumping to disk")
f, err := os.Create(d.Filename())
if err != nil {
log.Fatal(err)
@@ -265,8 +265,11 @@ func (a *ArtLayer) SetActive() ([]byte, error) {
// SetColor creates a color overlay for the layer
func (a *ArtLayer) SetColor(c Color) {
if Mode == 2 && a.Color.RGB() == c.RGB() {
return
if a.Color.RGB() == c.RGB() {
if Mode == 2 || (Mode == 0 && a.current) {
log.Println("Skipping color: already set.")
return
}
}
if a.Stroke.Size != 0 {
a.SetStroke(*a.Stroke, c)
@@ -296,9 +299,10 @@ func (a *ArtLayer) SetColor(c Color) {
}
func (a *ArtLayer) SetStroke(stk Stroke, fill Color) {
if Mode == 2 {
if stk.Size == a.Stroke.Size && stk.Color.RGB() == a.Color.RGB() {
if a.Color.RGB() == fill.RGB() {
if stk.Size == a.Stroke.Size && stk.Color.RGB() == a.Stroke.Color.RGB() {
if a.Color.RGB() == fill.RGB() {
if Mode == 2 || (Mode == 0 && a.current) {
log.Println("Skipping stroke: already set.")
return
}
}
@@ -310,6 +314,8 @@ func (a *ArtLayer) SetStroke(stk Stroke, fill Color) {
if err != nil {
log.Panic(err)
}
log.Printf(" layer %s stroke was %.2fpt %v and color to %v\n", a.name, a.Stroke.Size,
a.Stroke.Color.RGB(), a.Color.RGB())
a.Stroke = &stk
a.Color = fill
stkCol := stk.Color.RGB()
@@ -348,12 +354,14 @@ func Layer(path string) (ArtLayer, error) {
// SetVisible makes the layer visible.
func (a *ArtLayer) SetVisible(b bool) {
if a.Visible() == b {
if a.visible == b {
return
}
if b {
a.visible = b
switch b {
case true:
log.Printf("Showing %s", a.name)
} else {
case false:
log.Printf("Hiding %s", a.name)
}
js := fmt.Sprintf("%s.visible=%v;",
@@ -392,18 +400,36 @@ func (a *ArtLayer) SetPos(x, y int, bound string) {
lyrX = a.X1()
}
byt, err := DoJs("moveLayer.jsx", JSLayer(a.Path()), fmt.Sprint(x-lyrX), fmt.Sprint(y-lyrY))
var bounds [2][2]int
if err != nil {
panic(err)
}
json.Unmarshal(byt, bounds)
a.bounds = bounds
var lyr ArtLayer
err = json.Unmarshal(byt, &lyr)
if err != nil {
log.Panic(err)
}
a.bounds = lyr.bounds
}
func (a *ArtLayer) Refresh() {
tmp, err := Layer(a.Path())
if err != nil {
log.Panic(err)
}
tmp.SetParent(a.Parent())
a.name = tmp.name
a.bounds = tmp.bounds
a.Text = tmp.Text
a.parent = tmp.Parent()
a.visible = tmp.visible
a.current = true
}
type LayerSet struct {
name string
parent Group
current bool // Whether we've checked this layer since we loaded from disk.
visible bool
artLayers []*ArtLayer
layerSets []*LayerSet
}
@@ -445,9 +471,11 @@ func (l *LayerSet) Name() string {
}
func (l *LayerSet) ArtLayers() []*ArtLayer {
for i := 0; i < len(l.artLayers); i++ {
if l.artLayers[i] != nil && !l.artLayers[i].current {
l.artLayers[i] = l.ArtLayer(l.artLayers[i].name)
if Mode != 2 {
for _, lyr := range l.artLayers {
if !lyr.current {
lyr.Refresh()
}
}
}
return l.artLayers
@@ -459,20 +487,23 @@ func (l *LayerSet) ArtLayer(name string) *ArtLayer {
for _, lyr := range l.artLayers {
if lyr.name == name {
if Mode == 0 && !lyr.current {
byt, err := DoJs("getLayer.jsx", JSLayer(lyr.Path()))
if err != nil {
log.Panic(err)
}
var lyr2 *ArtLayer
err = json.Unmarshal(byt, &lyr2)
if err != nil {
log.Panic(err)
}
lyr.name = lyr2.name
lyr.bounds = lyr2.bounds
lyr.visible = lyr2.visible
lyr.current = true
lyr.Text = lyr2.Text
lyr.Refresh()
/*
byt, err := DoJs("getLayer.jsx", JSLayer(lyr.Path()))
if err != nil {
log.Panic(err)
}
var lyr2 *ArtLayer
err = json.Unmarshal(byt, &lyr2)
if err != nil {
log.Panic(err)
}
lyr.name = lyr2.name
lyr.bounds = lyr2.bounds
lyr.visible = lyr2.visible
lyr.current = true
lyr.Text = lyr2.Text
*/
}
return lyr
}
@@ -519,10 +550,6 @@ func NewLayerSet(path string, g Group) (*LayerSet, error) {
log.Println(string(byt))
log.Panic(err)
}
if flag.Lookup("test.v") != nil {
// log.Println(path)
// log.Println(out)
}
out.SetParent(g)
log.Printf("Loading ActiveDocument/%s\n", out.Path())
if err != nil {
@@ -544,6 +571,30 @@ func NewLayerSet(path string, g Group) (*LayerSet, error) {
// SetVisible makes the LayerSet visible.
func (l *LayerSet) SetVisible(b bool) {
if l.visible == b {
return
}
l.visible = b
js := fmt.Sprintf("%s%v", JSLayer(strings.TrimRight(l.Path(), ";")), b)
DoJs("compilejs.jsx", js)
}
func (l *LayerSet) Refresh() {
var tmp *LayerSet
byt, err := DoJs("getLayerSet.jsx", JSLayer(l.Path()))
err = json.Unmarshal(byt, &tmp)
if err != nil {
log.Println(string(byt))
log.Panic(err)
}
tmp.SetParent(l.Parent())
for _, lyr := range l.artLayers {
lyr.Refresh()
}
for _, set := range l.layerSets {
set.Refresh()
}
l.name = tmp.name
l.visible = tmp.visible
l.current = true
}