forked from yui540/satella.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
408 lines (355 loc) · 9.01 KB
/
core.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
'use strict';
// module
var fs = require('fs')
, electron = require('electron')
, app = electron.app
, BrowserWindow = electron.BrowserWindow
, ipcMain = electron.ipcMain
, dialog = electron.dialog;
// data
var STATE = {} // プロジェクト情報
, DATA = {} // ウィンドウのデータ
, VIEW = {}; // ウィンドウ
/******************************************************************
*
* app
*
******************************************************************/
app.on('ready', () => { // プロセス準備完了
set_data();
create_window('INIT');
});
/******************************************************************
*
* ipc
*
******************************************************************/
/**
* 指定のウィンドウを閉じる
*/
ipcMain.on('close_window', (event, id) => {
VIEW[id].close();
});
/**
* 指定のウィンドウを最小化
*/
ipcMain.on('quit_window', (event, id) => {
VIEW[id].minimize();
});
/**
* プロジェクト履歴の取得
*/
ipcMain.on('history', (event) => {
let save = readFile(__dirname + '/lib/save.json');
save = JSON.parse(save);
save = kill_history(save);
writeFile(__dirname + '/lib/save.json', JSON.stringify(save));
event.sender.send('history', save);
});
/**
* 新規プロジェクト
*/
ipcMain.on('new_project', (event, params) => {
dialog.showSaveDialog(VIEW['INIT'], {
properties: ['createDirectory']
}, function(d_path) {
if(!d_path) return;
STATE.directory_path = d_path + '/';
create_project({ // プロジェクトアーカイブの作成
directory: d_path + '/',
author: params.author,
content_name: params.content_name,
description: params.description,
tag: params.tag
});
// メインアプリケーション起動
create_window('MAIN');
// プロジェクトの履歴更新
push_history(STATE.directory_path);
setTimeout(function() {VIEW['INIT'].close()}, 1000);
});
});
/**
* プロジェクトを開く
*/
ipcMain.on('open_project', (event, directory) => {
STATE.directory_path = directory;
// app.jsonの存在確認
if(!existFile(directory + 'app.json')) {
return;
}
// メインアプリケーション起動
create_window('MAIN');
// プロジェクトの履歴更新
push_history(STATE.directory_path);
setTimeout(function() {VIEW['INIT'].close()}, 1000);
});
/**
* 他のプロジェクトを開く
*/
ipcMain.on('other_project', (event, params) => {
dialog.showOpenDialog(VIEW['INIT'], {
title: 'Open project',
properties: ['openDirectory']
}, function(d_path) {
if(!d_path) return;
STATE.directory_path = d_path + '/';
// app.jsonの存在確認
if(!existFile(d_path + '/app.json')) {
return;
}
// メインアプリケーション起動
create_window('MAIN');
// プロジェクトの履歴更新
push_history(STATE.directory_path);
setTimeout(function() {VIEW['INIT'].close()}, 1000);
});
});
/**
* プロジェクトの起動
*/
ipcMain.on('start_project', (event, params) => {
event.sender.send('start_project', STATE);
});
/**
* SDKの生成
*/
ipcMain.on('sdk', (event, params) => {
dialog.showOpenDialog(VIEW['MAIN'], {
title: 'create SDK',
properties: ['openDirectory']
}, function(d_path) {
if(!d_path) return;
create_sdk(d_path);
event.sender.send('sdk');
});
});
/******************************************************************
*
* function
*
******************************************************************/
/**
* プロジェクトの履歴更新
*/
function push_history(dir) {
let save = JSON.parse(readFile(__dirname + '/lib/save.json'));
save = kill_history(save); // パスが切れているものを消去
for(let i=0; i < save.length; i++) { // 重複のチェック
let _dir = save[i];
if(dir === _dir)
save.splice(i, 1);
}
save.push(dir);
if(save.length > 20) // 20以内で丸める
save.shift();
save = JSON.stringify(save);
writeFile(__dirname + '/lib/save.json', save);
}
/**
* プロジェクト履歴のパスが切れているものを消去
*/
function kill_history(save) {
let _save = copy(save);
for(let i=0; i < save.length; i++) {
let dir = save[i];
if(!existFile(dir + 'app.json'))
_save.splice(i, 1);
}
return _save;
}
/**
* ウィンドウ情報の設定
*/
function set_data() {
let size = electron.screen.getPrimaryDisplay().workAreaSize;
DATA['INIT'] = { // init
FILE_NAME: 'init.html',
WIDTH: 600,
HEIGHT: 400,
MIN_WIDTH: 600,
MIN_HEIGHT: 400,
RESIZE: false
};
DATA['MAIN'] = { // main
FILE_NAME: 'main.html',
WIDTH: size.width,
HEIGHT: size.height,
MIN_WIDTH: 1000,
MIN_HEIGHT: 650,
RESIZE: true
};
}
/**
* 新規ウィンドウの作成
*/
function create_window(id) {
VIEW[id] = new BrowserWindow({
width: DATA[id].WIDTH,
height: DATA[id].HEIGHT,
minWidth: DATA[id].MIN_WIDTH,
minHeight: DATA[id].MIN_HEIGHT,
resizable: DATA[id].RESIZE,
transparent: true,
frame: false,
show: false
});
VIEW[id].loadURL(`file://${__dirname}/views/${DATA[id].FILE_NAME}`);
VIEW[id].on('ready-to-show', () => {
VIEW[id].show();
});
VIEW[id].on('closed', () => {
VIEW[id] = null;
});
}
/**
* プロジェクトアーカイブの作成
*/
function create_project(params) {
let thumb = readFile( // thumb.png
__dirname + '/img/assets/thumb.png', 'base64');
let app_state_json = JSON.stringify({ // app-state.json
directory: params.directory, // ディレクトリパス
current_time: 0, // 現在時間
current_layer: 0, // 選択レイヤー
current_parameter: 'default',// 選択パラメータ
current_keyframes: false, // 選択キーフレーム
current_history: 0, // 履歴位置
play: false, // 再生有無
mode: 'polygon', // マウスモード
scale: 1, // 拡大縮小比
position: { // 位置
x: 0.5,
y: 0.5
},
parameter: { // パラメータの状態
default: {
x: 0.5, y: 0.5
}
},
history: [] // 履歴
});
let app_json = JSON.stringify({ // app.json
author: params.author, // 作成者
content_name: params.content_name, // 作品名
description: params.description, // 説明文
tag: params.tag, // タグ
layer: [], // レイヤー
parameter: { // パラメータ
default: {
type: 4,
layer: []
}
},
keyframes: { // キーフレーム
default: {
default: []
}
}
});
mkdir(params.directory);
mkdir(params.directory + '/texture');
writeFile( // thumb.png
params.directory + '/thumb.png',
thumb, 'base64');
writeFile( // app_state.json
params.directory + '/app-state.json',
app_state_json);
writeFile( // eriri.json
params.directory + '/app.json',
app_json);
}
/**
* SDKの作成
*/
function create_sdk(directory) {
// パス
let axis_path = STATE.directory_path
, sdk_js = __dirname + '/lib/sdk.js'
, app_json = axis_path + 'app.json'
, texture = axis_path + 'texture'
, thumb = axis_path + 'thumb.png';
// データ
let sdk_js_data = readFile(sdk_js)
, app_json_data = readFile(app_json)
, thumb_data = readFile(thumb, 'base64')
, texture_data = {}
, texs = readdir(texture);
for(let i=0; i < texs.length; i++) {
if(texs[i].match(/.+\.png/)) {
let data = readFile(texture + '/' + texs[i], 'base64');
texture_data[texs[i]] = data;
}
}
// 書き出し
mkdir(directory + '/satella-sdk');
mkdir(directory + '/satella-sdk/texture');
mkdir(directory + '/satella-sdk/lib');
writeFile(directory + '/satella-sdk/sdk.js', sdk_js_data);
writeFile(directory + '/satella-sdk/lib/app.json', app_json_data);
writeFile(directory + '/satella-sdk/thumb.png', thumb_data, 'base64');
for(let name in texture_data) {
let file_name = directory + '/satella-sdk/texture/' + name
, data = texture_data[name];
writeFile(file_name, data, 'base64');
}
}
/**
* ディレクトリの読み込み
*/
function readdir(directory) {
try {
return fs.readdirSync(directory);
} catch(err) {
return false;
}
}
/**
* ファイルの存在確認
*/
function existFile(file_name) {
try {
fs.statSync(file_name);
return true;
} catch(err) {
return false;
}
}
/**
* ファイルの作成
*/
function writeFile(file_name, data, encoding='utf8') {
try {
fs.writeFileSync(file_name, data, encoding);
return true;
} catch(err) {
return false;
}
}
/**
* ファイルの読み込み
*/
function readFile(file_name, encoding='utf8') {
try {
return fs.readFileSync(file_name, encoding);
} catch(err) {
return false;
}
}
/**
* コピー
*/
function copy(obj) {
return JSON.parse(JSON.stringify(obj));
}
/**
* ディレクトリの作成
*/
function mkdir(directory_name) {
try {
fs.mkdirSync(directory_name);
return true;
} catch(err) {
return false;
}
}