forked from odrick/free-tex-packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPP.js
174 lines (139 loc) · 5.47 KB
/
APP.js
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
import { Observer, GLOBAL_EVENT } from './Observer';
import PackProcessor from './PackProcessor';
import TextureRenderer from './utils/TextureRenderer';
import { getFilterByType } from './filters';
import I18 from './utils/I18';
import { startExporter } from './exporters';
import Tinifyer from 'platform/Tinifyer';
import Downloader from 'platform/Downloader';
let INSTANCE = null;
class APP {
constructor() {
INSTANCE = this;
this.images = {};
this.packOptions = {};
this.packResult = null;
this.onPackComplete = this.onPackComplete.bind(this);
this.onPackError = this.onPackError.bind(this);
Observer.on(GLOBAL_EVENT.IMAGES_LIST_CHANGED, this.onImagesListChanged, this);
Observer.on(GLOBAL_EVENT.PACK_OPTIONS_CHANGED, this.onPackOptionsChanged, this);
Observer.on(GLOBAL_EVENT.PACK_EXPORTER_CHANGED, this.onPackExporterOptionsChanged, this);
Observer.on(GLOBAL_EVENT.START_EXPORT, this.startExport, this);
}
static get i() {
return INSTANCE;
}
onImagesListChanged(data) {
this.images = data;
this.pack();
}
onPackOptionsChanged(data) {
this.packOptions = data;
this.pack();
}
onPackExporterOptionsChanged(data) {
this.packOptions = data;
}
pack() {
let keys = Object.keys(this.images);
if (keys.length > 0) {
Observer.emit(GLOBAL_EVENT.SHOW_SHADER);
setTimeout(() => this.doPack(), 0);
}
else {
this.doPack();
}
}
doPack() {
PackProcessor.pack(this.images, this.packOptions, this.onPackComplete, this.onPackError);
}
onPackComplete(res) {
this.packResult = [];
for (let data of res) {
let renderer = new TextureRenderer(data, this.packOptions);
this.packResult.push({
data: data,
buffer: renderer.buffer,
renderer: renderer
});
}
Observer.emit(GLOBAL_EVENT.PACK_COMPLETE, this.packResult);
Observer.emit(GLOBAL_EVENT.HIDE_SHADER);
}
onPackError(err) {
Observer.emit(GLOBAL_EVENT.HIDE_SHADER);
Observer.emit(GLOBAL_EVENT.SHOW_MESSAGE, err.description);
}
startExport() {
if (!this.packResult || !this.packResult.length) {
Observer.emit(GLOBAL_EVENT.SHOW_MESSAGE, I18.f("NO_IMAGES_ERROR"));
return;
}
if (this.packOptions.tinify && !this.packOptions.tinifyKey) {
Observer.emit(GLOBAL_EVENT.SHOW_MESSAGE, I18.f("NO_TINIFY_KEY_ERROR"));
return;
}
Observer.emit(GLOBAL_EVENT.SHOW_SHADER);
setTimeout(() => this.doExport(), 0);
}
async doExport() {
let exporter = this.packOptions.exporter;
let textureName = this.packOptions.textureName;
let filterClass = getFilterByType(this.packOptions.filter);
let filter = new filterClass();
let files = [];
let ix = 0;
for (let item of this.packResult) {
let fName = textureName + (this.packResult.length > 1 ? "-" + ix : "");
let buffer = item.renderer.scale(this.packOptions.scale);
let imageData = filter.apply(buffer).toDataURL(this.packOptions.textureFormat === "png" ? "image/png" : "image/jpeg");
let parts = imageData.split(",");
parts.shift();
imageData = parts.join(",");
try {
imageData = await Tinifyer.start(imageData, this.packOptions);
}
catch (e) {
Observer.emit(GLOBAL_EVENT.HIDE_SHADER);
Observer.emit(GLOBAL_EVENT.SHOW_MESSAGE, e);
return;
}
files.push({
name: `${fName}.${this.packOptions.textureFormat}`,
content: imageData,
base64: true
});
//TODO: move to options
let pixelFormat = this.packOptions.textureFormat === "png" ? "RGBA8888" : "RGB888";
let options = {
imageName: `${fName}`,
imageFile: `${fName}.${this.packOptions.textureFormat}`,
imageData: imageData,
format: pixelFormat,
textureFormat: this.packOptions.textureFormat,
imageWidth: buffer.width,
imageHeight: buffer.height,
removeFileExtension: this.packOptions.removeFileExtension,
prependFolderName: this.packOptions.prependFolderName,
base64Export: this.packOptions.base64Export,
scale: this.packOptions.scale,
trimMode: this.packOptions.trimMode
};
try {
files.push({
name: fName + "." + this.packOptions.exporter.fileExt,
content: await startExporter(exporter, item.data, options)
});
}
catch (e) {
Observer.emit(GLOBAL_EVENT.HIDE_SHADER);
Observer.emit(GLOBAL_EVENT.SHOW_MESSAGE, I18.f("EXPORTER_ERROR", e));
return;
}
ix++;
}
Downloader.run(files, this.packOptions.fileName, this.packOptions.savePath);
Observer.emit(GLOBAL_EVENT.HIDE_SHADER);
}
}
export default APP;