-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tis
298 lines (246 loc) · 7.63 KB
/
main.tis
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
view.connectToInspector && view.connectToInspector(rootElement, inspectorIpAddress);
function self.ready() {
const w = 625dip;
const h = 306dip;
const (sw, sh) = view.screenBox(#frame, #dimension);
view.move((sw / 2) - (w / 2), (sh / 2) - (h / 2), w, h, false);
}
function emitLayerCreatedEvent() {
View.all.find((v) => v.id == "layers").root.postEvent("layer-created");
}
const globalThis = {
filename: "",
filter: "PNG Files (*.png)|*.png|All Files (*.*)|*.*",
extension: "png",
undos: [],
redos: []
};
const UNDO = 0;
const REDO = 1;
function addUndo(fn) {
const { undos } = globalThis;
undos.push(fn);
$(li #menu-undo).state#disabled = undos.length == 0;
updateLayerThumbnail($(div.layer.current));
}
function addRedo(fn) {
const { redos } = globalThis;
redos.push(fn);
$(li #menu-redo).state#disabled = redos.length == 0;
updateLayerThumbnail($(div.layer.current));
}
function getUndo() {
const { undos } = globalThis;
const undo = undos.pop();
$(li #menu-undo).state#disabled = undos.length == 0;
return undo;
}
function getRedo() {
const { redos } = globalThis;
const redo = redos.pop();
$(li #menu-redo).state#disabled = redos.length == 0;
return redo;
}
function performUndo() {
const undo = getUndo();
undo(UNDO);
addRedo(undo);
updateLayerThumbnail($(div.layer.current));
}
function performRedo() {
const redo = getRedo();
redo(REDO);
addUndo(redo);
updateLayerThumbnail($(div.layer.current));
}
function activateMenuItems() {
const lis = $$(#menu-file > li)
.concat($$(#menu-adjustments > li))
.concat($$(#menu-effects > li));
for (var li in lis) {
li.state#disabled = false;
}
}
function updateLayerThumbnail(layer) {
const thumbnailImage = new Image(
layer.style["width"].toInteger(),
layer.style["height"].toInteger(),
layer
);
View.all.find((v) => v.id == "layers").root.postEvent("update-layer-thumbnail", {
index: $$(div.layer).indexOf(layer),
thumbnailImage: thumbnailImage
});
}
$(li #menu-open) << event click {
const { filter } = globalThis;
const filename = view.selectFile(#open, filter);
if (! filename) return;
globalThis.filename = filename;
const image = Image.fromBytes(Bytes.load(filename));
const width = image.width;
const height = image.height;
$(div.layer.current).attributes.remove("current");
$(div#canvas).$append(<div.layer.current></div>);
$(div.layer.current).style.set { width: width, height: height };
$(div.layer.current).filters = [];
$(div.layer.current).paintContent = function(gfx) {
for (var filter in this.filters.clone().reverse()) {
gfx.pushLayer(#inner-box, filter);
}
gfx.blendImage(image, 0, 0);
}
activateMenuItems();
emitLayerCreatedEvent();
}
$(li #menu-save) << event click {
const { filename, filter, extension } = globalThis;
const (w, h) = $(div.layer.current).box(#dimension);
const image = new Image(w, h, $(div.layer.current));
const bytes = image.toBytes();
bytes.save(filename);
}
$(li #menu-save-as) << event click {
const { filter, extension } = globalThis;
const filename = view.selectFile(#save, filter, extension);
if (! filename) return;
const (w, h) = $(div.layer.current).box(#dimension);
const image = new Image(w, h, $(div.layer.current));
const bytes = image.toBytes();
bytes.save(filename);
}
$(li #menu-undo) << event click {
performUndo();
}
$(li #menu-redo) << event click {
performRedo();
}
function applyFilter(filter) {
const action = () => $(div.layer.current).filters.push(filter);
action();
const undo = function(which) {
if (which == UNDO) {
$(div.layer.current).filters.pop();
} else if (which == REDO) {
action();
}
}
addUndo(undo);
}
$(li #menu-invert-colors).on("click", :: applyFilter([invert:1]));
$(li #menu-black-and-white).on("click", :: applyFilter([grayscale:1.0]));
$(li #menu-sepia).on("click", :: applyFilter([sepia:1.0]));
// https://gist.github.com/xposedbones/75ebaef3c10060a3ee3b246166caab56
const map = (value, x1, y1, x2, y2) => (value - x1) * (y2 - x2) / (y1 - x1) + x2;
function mapBrightness(brightness) {
// [-100, 0, 100] => [-1, 0, 1]
return map(brightness, -100.0, 100.0, -1.0, 1.0);
}
function mapContrast(contrast) {
// [-100, 0, 50, 100] => [0, 1, 2, 10]
if (contrast <= 0.0) return map(contrast, -100.0, 0.0, 0.0, 1.0);
if (contrast > 0.0 && contrast <= 50.0) return map(contrast, 0.0, 50.0, 1.0, 2.0);
return map(contrast, 50.0, 100.0, 2.0, 10.0);
}
function mapGaussianBlurRadius(radius) {
return px(radius);
}
function openBrightnessContrastPopup() {
$(div.layer.current).filters.push([brightness:0.0]);
$(div.layer.current).filters.push([contrast:1.0]);
$(div.layer.current).refresh();
const onBrightnessChange = function(brightness) {
const index = $(div.layer.current).filters.length - 2;
const value = mapBrightness(brightness);
$(div.layer.current).filters[index] = [brightness:value];
$(div.layer.current).refresh();
}
const onContrastChange = function(contrast) {
const index = $(div.layer.current).filters.length - 1;
const value = mapContrast(contrast.toFloat());
$(div.layer.current).filters[index] = [contrast:value];
$(div.layer.current).refresh();
}
const onOk = function() {
const length = $(div.layer.current).filters.length;
const contrast = $(div.layer.current).filters.last[0];
const brightness = $(div.layer.current).filters[length - 2][0];
const undo = function(which) {
if (which == UNDO) {
$(div.layer.current).filters.pop();
$(div.layer.current).filters.pop();
} else if (which == REDO) {
$(div.layer.current).filters.push([brightness:brightness]);
$(div.layer.current).filters.push([contrast:contrast]);
}
}
addUndo(undo);
}
const onCancel = function() {
$(div.layer.current).filters.pop();
$(div.layer.current).filters.pop();
$(div.layer.current).refresh();
}
const popup = view.dialog({
caption: "Brightness / Contrast",
url: self.url("brightness-contrast/main.htm"),
parameters: {
foo: "bar",
callbacks: {
onBrightnessChange: onBrightnessChange,
onContrastChange: onContrastChange,
onOk: onOk,
onCancel: onCancel
}
}
});
}
$(li #menu-brightness-contrast) << event click {
openBrightnessContrastPopup();
}
function openGaussianBlurPopup() {
$(div.layer.current).filters.push([blur:0]);
$(div.layer.current).refresh();
const onBlurRadiusChange = function(radius) {
const index = $(div.layer.current).filters.length - 2;
const value = mapGaussianBlurRadius(radius);
$(div.layer.current).filters.last[0] = value;
$(div.layer.current).refresh();
}
const onOk = function() {
const blur = $(div.layer.current).filters.last[0];
const undo = function(which) {
if (which == UNDO) {
$(div.layer.current).filters.pop();
} else if (which == REDO) {
$(div.layer.current).filters.push([blur:blur]);
}
}
addUndo(undo);
}
const onCancel = function() {
$(div.layer.current).filters.pop();
$(div.layer.current).refresh();
}
const popup = view.dialog({
caption: "Gaussian Blur",
url: self.url("effects/blurs/gaussian-blur/main.htm"),
parameters: {
foo: "bar",
callbacks: {
onBlurRadiusChange: onBlurRadiusChange,
onOk: onOk,
onCancel: onCancel
}
}
});
}
$(li #menu-gaussian-blur) << event click {
openGaussianBlurPopup();
}
$(li #menu-about) << event click {
view.msgbox(#information,
"This application uses Sciter Engine (https://sciter.com), © Terra Informatica Software, Inc.",
"About"
);
}