-
Notifications
You must be signed in to change notification settings - Fork 8
/
Android Elements.jsx
293 lines (240 loc) · 6.44 KB
/
Android Elements.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
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
// Android Elements
// Author: Tim Roes <[email protected]>
/*
<javascriptresource>
<name>Android Elements</name>
<category>Android_PS_Tools</category>
<about>Insert Android elements into your document.</about>
<enableinfo>true</enableinfo>
</javascriptresource>
*/
#target photoshop
#include "./~android-funcs.js"
app.bringToFront();
var search;
var win;
var elList, preview, preview_txt;
var holoDark, holoLight;
var backbutton;
var donotclose;
var currentFolder;
Image.prototype.onDraw = function() {
// written by Marc Autret
// "this" is the container; "this.image" is the graphic
if( !this.image ) return;
var WH = this.size
var wh = this.image.size;
var k;
if( wh[0] <= WH[0] && wh[1] <= WH[1])
k = 1;
else
k = Math.min(WH[0]/wh[0], WH[1]/wh[1]);
var xy;
// Resize proportionally:
wh = [k*wh[0],k*wh[1]];
// Center:
xy = [ (WH[0]-wh[0])/2, (WH[1]-wh[1])/2 ];
this.graphics.drawImage(this.image,xy[0],xy[1],wh[0],wh[1]);
WH = wh = xy = null;
}
function thumbPath(file) {
var thumbPath = file.fullName.replace(EL_DIR, EL_THUMB_DIR);
thumbPath = thumbPath.substr(0, thumbPath.lastIndexOf('.')) + '.png';
return thumbPath;
}
/**
* Click in list (or Enter) will either show subelements,
* or include the element in the current document.
*/
function clickList(ev) {
var sel = elList.selection.file_object;
if(sel instanceof File) {
preview.image = thumbPath(sel);
} else if(sel instanceof Folder) {
search.text = "";
currentFolder = sel;
loadSubElements(sel);
}
}
function dblClickList(ev) {
var sel = elList.selection.file_object;
search.text = "";
if(sel instanceof File) {
importFile(sel);
if(!donotclose.value)
win.close();
preview.image = thumbPath(sel);
} else if(sel instanceof Folder) {
// We selected a folder, so go deeper
search.text = "";
currentFolder = sel;
loadSubElements(sel);
}
}
/**
* Go Back to element selection.
*/
function goBack() {
loadElements();
}
/**
* When holo theme changed, try to find the current resource in
* new holo theme, otherwise go back to selection.
*/
function holoChanged(ev) {
saveHoloPref(getHolo());
if(currentFolder) {
var notHolo = (getHolo() == HOLO_DARK) ? HOLO_LIGHT : HOLO_DARK;
var newPath = currentFolder.fullName.replace(notHolo, getHolo());
currentFolder = new Folder(newPath);
if(!currentFolder) {
loadElements();
} else {
loadSubElements(currentFolder);
}
} else {
loadElements();
}
}
function buildElementUI() {
win = new Window('dialog', 'Android Elements');
win.alignChildren = "fill";
win.addEventListener("keydown", function(ev) {
// If user hit Enter, select first item in list.
if(ev.keyName == "Enter" && elList.items.length > 0) {
elList.selection = 0;
clickList(null);
}
if(ev.keyName == "Escape" && ev.shiftKey) {
goBack();
}
search.active = true;
});
var searchGroup = win.add("group");
var searchLabel = searchGroup.add("statictext");
searchLabel.text = "Search:";
search = searchGroup.add("edittext");
search.characters = 30;
search.active = true;
search.onChanging = function(ev) {
updateList();
};
var holoGroup = win.add("group");
holoGroup.orientation = "row";
holoLight = holoGroup.add("radiobutton", undefined, "Holo &Light");
holoDark = holoGroup.add("radiobutton", undefined, "Holo &Dark");
if(getHoloPref() == HOLO_DARK)
holoDark.value = true;
else
holoLight.value = true;
holoLight.onClick = holoChanged;
holoDark.onClick = holoChanged;
backbutton = holoGroup.add("button", undefined, "&Select other");
backbutton.alignment = "right";
backbutton.hide();
backbutton.onClick = goBack;
elList = win.add("listbox", [0, 0, 350, 300]);
elList.onDoubleClick = dblClickList;
elList.onClick = clickList;
var font = elList.graphics.font;
elList.graphics.font = ScriptUI.newFont(font.name, font.style, 16);
preview_txt = win.add("statictext", undefined, "Click to preview, Double click to insert");
preview_txt.graphics.font = ScriptUI.newFont(font.name, font.style, 16);
preview = win.add("image", [0, 0, 200, 200], undefined);
preview.onClick = function() {
dblClickList();
}
donotclose = win.add("checkbox", undefined, "&Don't close window after insert");
donotclose.value = getClosePref();
donotclose.onClick = function() {
saveClosePref(donotclose.value);
}
holoChanged();
win.show();
}
/**
* Sort all elements, that are loaded alphabeticaly by their
* display name.
*/
function sortElements() {
allElements.sort(function(a, b) {
return a.displayName > b.displayName;
});
}
/**
* Load subelements inside a specific folder.
*/
function loadSubElements(folder) {
allElements = new Array();
loadDir(folder);
sortElements();
updateList();
// We are no in the deeper level, so show back button
backbutton.show();
}
/**
* Load all elements.
*/
function loadElements() {
// Load holo dark or light elements
var dir = Folder.commonFiles;
dir.changePath(DIR);
dir.changePath(EL_DIR);
dir.changePath(getHolo());
allElements = new Array();
loadDir(dir);
// Load general holo elements
dir = Folder.commonFiles;
dir.changePath(DIR);
dir.changePath(EL_DIR);
dir.changePath(HOLO);
loadDir(dir);
sortElements();
updateList();
// Hide back button, we are on highest level
backbutton.hide();
currentFolder = null;
}
/**
* Load all files inside a given directory and add them to the list.
*/
function loadDir(dir) {
var elements = dir.getFiles();
for(var i in elements) {
allElements.push(elements[i]);
}
}
function getHolo() {
return (holoDark.value == true) ? HOLO_DARK : HOLO_LIGHT;
}
/**
* Update the list of elements, taking the current search filter into respect.
*/
function updateList() {
var filter = search.text.toLowerCase();
elList.removeAll();
for(var i in allElements) {
var e = allElements[i];
if(!filter || e.displayName.toLowerCase().indexOf(filter) !== -1) {
var item = elList.add("item", stripExt(e.displayName));
if(e instanceof Folder) {
item.text += " \u00BB";
}
item.file_object = e;
}
}
}
if(!documents.length) {
alert('There are no documents open.', 'No Documents Open', true);
} else if(parseInt(version, 10) < 10) {
alert('This script requires at least Photoshop CS3.', 'Wrong Photoshop Version', true);
} else {
var defaultRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
try {
app.activeDocument.suspendHistory('Insert Android element', 'buildElementUI();');
} catch(e) {
alert(e, 'Android Elements Error', true);
}
app.preferences.rulerUnits = defaultRulerUnits;
}