-
Notifications
You must be signed in to change notification settings - Fork 0
/
randohm.sketchplugin
121 lines (91 loc) · 3.37 KB
/
randohm.sketchplugin
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
if(selection.length() == 0){
var app = [NSApplication sharedApplication];
[app displayDialog:"Woah there! Please select at least one layer to repeat within the artboard" withTitle:"RandOhm error"]
}
if(selection[0].parentArtboard() == null){
var app = [NSApplication sharedApplication];
[app displayDialog:"Woah there! Please put the layers you'd like to repeat within an artboard" withTitle:"RandOhm error"]
}
var layer = selection[0];
var layerHeight = layer.frame().height();
var layerWidth = layer.frame().width();
// log('layer height: ' + layerHeight)
// log('layer width: ' + layerWidth)
var artboardContainer = layer.parentArtboard();
log(artboardContainer);
var containerHeight = artboardContainer.frame().height()
var containerWidth = artboardContainer.frame().width()
var overlap = true;
var rotate = true;
// log('artboard height: ' + containerHeight)
// log('artboard width: ' + containerWidth)
var cloneCount = [doc askForUserInput:"How many copies should we create?" initialValue:"10"]
if(cloneCount > 0){
function getRotateOption(msg){
var alert = [[NSAlert alloc] init]
[alert setMessageText:msg]
[alert setInformativeText: "Do you want the cloned layers to be randomly rotated?"];
[alert addButtonWithTitle:'Yes']
[alert addButtonWithTitle:'No']
var choice = [alert runModal]
return [choice]
}
var rotateOption = getRotateOption('Randomly rotate the layers?')
if(rotateOption == 1001){
log('false')
rotate = false;
}
function getOverlapOption(msg){
var alert = [[NSAlert alloc] init]
[alert setMessageText:msg]
[alert setInformativeText: "Would you like to allow the cloned layers to overlap on the artboard edge?"];
[alert addButtonWithTitle:'Yes']
[alert addButtonWithTitle:'No']
var choice = [alert runModal]
return [choice]
}
var option = getOverlapOption('One last question...')
if(option == 1001){
overlap = false;
}
for (var i = 0; i < selection.length(); i++) {
var curLayer = selection[i];
var randomRotate = Math.random() * 360;
if(overlap == false){
var randomX = Math.random() * (containerWidth - layerWidth);
var randomY = Math.random() * (containerHeight - layerHeight);
}
else{
// extend boundaries
var randomX = Math.random() * (containerWidth + layerWidth) - (layerWidth);
var randomY = Math.random() * (containerHeight + layerHeight) - (layerHeight);
}
curLayer.frame().setX(randomX);
curLayer.frame().setY(randomY);
if(rotate){curLayer.setRotation(randomRotate)};
}
for (var i = 0; i < selection.length(); i++) {
for (var j = 0; j < cloneCount; j++) {
var layer = selection[i];
var duplicated = layer.duplicate();
var randomRotate = Math.random() * 360;
if(overlap == false){
var randomX = Math.random() * (containerWidth - layerWidth);
var randomY = Math.random() * (containerHeight - layerHeight);
}
else{
// extend boundaries
var randomX = Math.random() * (containerWidth + layerWidth) - (layerWidth);
var randomY = Math.random() * (containerHeight + layerHeight) - (layerHeight);
}
duplicated.frame().setX(randomX);
duplicated.frame().setY(randomY);
if(rotate){duplicated.setRotation(randomRotate)};
}
}
}
else{
log(cloneCount)
var app = [NSApplication sharedApplication];
[app displayDialog:"Woah there! Please enter numbers over 0 only, please" withTitle:"RandOhm error"]
}