This repository has been archived by the owner on Feb 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Gridiculous-1.0a.jsx
254 lines (204 loc) · 9.09 KB
/
Gridiculous-1.0a.jsx
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
/**
* Gridiculous 1.0a
*
* Author: Jarkko Tuunanen
* Date: Sun Feb 26 23:02 2012 +0200
*
*/
#target photoshop
var gridiculous = (function() {
var version = "1.0a";
var columnCount = 12,
columnWidth = 60,
gutterWidth = 20,
gridStartPosition = 0,
doc = null;
// Utility functions.
function gridWidth() { return (parseInt(columnCount) * (parseInt(columnWidth) + parseInt(gutterWidth))); }
function marginWidth() { return Math.floor((gutterWidth / 2)); }
function isPositiveNumber(num) { return (!isNaN(num) && num > 0 ? true : false); }
function roundToNearest(num, acc) { return (acc < 0 ? Math.round(num*acc)/acc : Math.round(num/acc)*acc); }
//
// Settings Dialog Module
//
var settingsDialog = function(){
// Builds a dummy modal window with necessary labels and fields.
function buildSettingsDialog() {
// Create dialog window.
var dialog = new Window('dialog', 'Gridiculous');
// Grid settings panel.
dialog.settingsPanel = dialog.add('panel', undefined, 'Grid Settings');
dialog.settingsPanel.alignChildren = 'right';
dialog.settingsPanel.columnCount = dialog.settingsPanel.add('group'); // Column Count group.
dialog.settingsPanel.columnWidth = dialog.settingsPanel.add('group'); // Column Width group.
dialog.settingsPanel.gutterWidth = dialog.settingsPanel.add('group'); // Gutter Width group.
// Column Count.
dialog.settingsPanel.columnCount.label = dialog.settingsPanel.columnCount.add('statictext', undefined, 'Number of Columns');
dialog.settingsPanel.columnCount.input = dialog.settingsPanel.columnCount.add('edittext', undefined, columnCount);
dialog.settingsPanel.columnCount.input.preferredSize = [40, 20];
// Column Width.
dialog.settingsPanel.columnWidth.label = dialog.settingsPanel.columnWidth.add('statictext', undefined, 'Column Width (px)');
dialog.settingsPanel.columnWidth.input = dialog.settingsPanel.columnWidth.add('edittext', undefined, columnWidth);
dialog.settingsPanel.columnWidth.input.preferredSize = [40, 20];
// Gutter Width.
dialog.settingsPanel.gutterWidth.label = dialog.settingsPanel.gutterWidth.add('statictext', undefined, 'Gutter Width (px)');
dialog.settingsPanel.gutterWidth.input = dialog.settingsPanel.gutterWidth.add('edittext', undefined, gutterWidth);
dialog.settingsPanel.gutterWidth.input.preferredSize = [40, 20];
// Total Width field.
dialog.settingsPanel.totalWidthField = dialog.settingsPanel.add('statictext', undefined, gridWidth());
dialog.settingsPanel.totalWidthField.preferredSize = [40, 20];
// Additional settings panel.
dialog.additionalSettingsPanel = dialog.add('panel', undefined, 'Additional settings (optional)');
dialog.additionalSettingsPanel.availableDocumentsLabel = dialog.additionalSettingsPanel.add('statictext', undefined, 'Select a document in which to insert the grid into. Select none if you wish to create a new document.', {multiline: true});
// Available documents Listbox.
dialog.additionalSettingsPanel.availableDocuments = dialog.additionalSettingsPanel.add('listbox', [0, 0, 200, 80], 'documentSelection');
// Check for open documents.
if(app.documents.length) {
// Populate list with open documents.
for(var i = 0; i < app.documents.length; i++)
dialog.additionalSettingsPanel.availableDocuments.add('item', app.documents[i].name);
}
// Grid start position group.
dialog.additionalSettingsPanel.startPosition = dialog.additionalSettingsPanel.add('group');
dialog.additionalSettingsPanel.startPosition.label = dialog.additionalSettingsPanel.startPosition.add('statictext', undefined, 'Grid start position (X coord.)');
dialog.additionalSettingsPanel.startPosition.input = dialog.additionalSettingsPanel.startPosition.add('edittext', undefined, 0);
dialog.additionalSettingsPanel.startPosition.input.preferredSize = [40, 20];
dialog.additionalSettingsPanel.startPosition.input.enabled = false;
// OK & Cancel button toolbar.
dialog.toolbar = dialog.add('group');
dialog.toolbar.btnCancel = dialog.toolbar.add('button', undefined, 'Cancel', {name: 'btnCancel'});
dialog.toolbar.btnCreate = dialog.toolbar.add('button', undefined, 'Create', {name: 'btnCreate'});
return dialog;
}
// Modal window event bindings.
function initializeSettingsDialog(dialog) {
// Button toolbar button event handling.
var tb = dialog.toolbar;
tb.btnCancel.onClick = function(){ dialog.close(2); }
tb.btnCreate.onClick = function(){ dialog.close(1); }
// Settings panel input event handling.
var sp = dialog.settingsPanel,
totalWidthField = dialog.settingsPanel.totalWidthField;
sp.columnCount.input.onChange = function() {
columnCount = this.text = (isPositiveNumber(this.text) ? parseInt(this.text) : 0);
totalWidthField.text = gridWidth();
};
sp.columnWidth.input.onChange = function() {
columnWidth = this.text = (isPositiveNumber(this.text) ? parseInt(this.text) : 0);
totalWidthField.text = gridWidth();
};
sp.gutterWidth.input.onChange = function() {
// Only accept positive integers that are divisible by 2. Fallback to zero.
gutterWidth = this.text = (isPositiveNumber(this.text) ? ((this.text %2 === 0) ? parseInt(this.text) : roundToNearest(this.text, 2)) : 0);
totalWidthField.text = gridWidth();
};
// Additional settings panel event handling.
var ds = dialog.additionalSettingsPanel;
// Grid start position button.
ds.startPosition.input.onChange = function() {
gridStartPosition = this.text = (isPositiveNumber(this.text) ? parseInt(this.text) : 0);
}
if(app.documents.length) {
ds.availableDocuments.onChange = function() {
if(this.selection) {
// Make selected document as working document.
doc = app.activeDocument = app.documents[this.selection.index];
// Enable Grid start position button.
ds.startPosition.input.enabled = true;
}
else {
// Reset selected document setting.
doc = null;
// Reset Grid start position value.
gridStartPosition = 0;
// Disable Grid start position button.
ds.startPosition.input.enabled = false;
}
}
}
return dialog;
}
// Init
function create() {
// Build dialog window.
var dlg = buildSettingsDialog();
// Initialize controls.
dlg = initializeSettingsDialog(dlg);
return dlg;
}
return {
create: create
}
}();
//
// Grid Maker module
//
var grid = function(){
function create() {
var cc = columnCount,
cw = columnWidth,
gw = gutterWidth,
mw = marginWidth(),
dw = gridWidth(),
x = gridStartPosition,
d = doc || app.documents.add(dw, 640, 72, 'Grid-' + cc + '-' + cw + '-' + gw);
// Loop through grid.
for(var i = 0; i < cc; i++) {
// Column left side margin guide.
d.guides.add(Direction.VERTICAL, UnitValue(x, "px"));
// Column left edge guide.
d.guides.add(Direction.VERTICAL, UnitValue(x + mw, "px"));
// Column right edge guide.
d.guides.add(Direction.VERTICAL, UnitValue(x + mw + cw, "px"));
// Calculate values for the next round.
x += gw + cw;
}
// Last column right side margin guide.
d.guides.add(Direction.VERTICAL, UnitValue(x, "px"));
}
function init() {
// Store original ruler unit setting.
var originalRulerUnits = app.preferences.rulerUnits;
// Set ruler unit to pixels.
app.preferences.rulerUnits = Units.PIXELS;
// Create grid.
create();
// Restore original ruler unit setting.
app.preferences.rulerunits = originalRulerUnits;
}
return {
init: init
}
}();
//
// Main init
//
function init(){
try {
// Check current version of Photoshop.
// The Guides object was not accessible through the Photoshop API until CS5.
// Stop execution immediately if older version.
if(app.version < "12")
throw 'Unfortunately Gridiculous does not work in your current version of Photoshop (' + app.version + ') since Guides were not introduced to the Photoshop API until CS5 (version 12).';
// Everything OK. Create and show dialog.
var sd = settingsDialog.create();
var ret = sd.show();
// User pressed OK.
if(ret == 1) {
// Make grid.
grid.init();
}
}
catch(e)
{
alert(e);
}
return true;
}
return {
version: version,
init: init
}
})();
// Initialize Gridiculous.
gridiculous.init();