-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
191 lines (148 loc) · 5.8 KB
/
main.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
// -------------------------------------------------
// MAIN PROCESS ENTRY POINT
// -------------------------------------------------
// Electron Dependencies
const { app, BrowserWindow, Menu, ipcMain } = require('electron')
// 3rd Party Dependencies
var fs = require("fs-extra");
var path = require("path");
// Local Dependencies
var getAsset = require('./lib/getAsset.js')
// Current working directory of the process
var cwd = process.cwd();
// Global reference to main editor window
var mainWindow = null;
function createMainWindow() {
// Create the main browser window
const window = new BrowserWindow({
width: 1280,
height: 800,
webPreferences: {
nodeIntegration: true
},
title: "Super Pixelbox"
});
// Store global reference to main editor window
mainWindow = window;
// Load editor web contents
window.loadFile('editor.html');
}
function createAuxiliaryWindow(w, h, indexHtml) {
// Create an auxiliary browser window
const auxiliaryWindow = new BrowserWindow({
width: w,
height: h,
webPreferences: {
nodeIntegration: true
}
});
// and load the index.html of the app
auxiliaryWindow.loadFile(indexHtml);
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(createMainWindow)
app.allowRendererProcessReuse = false;
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if(process.platform !== 'darwin') {
app.quit();
}
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if(BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
})
// -------------------------------------------------
// PROCESS COMMUNICATIONS
// -------------------------------------------------
ipcMain.on('openDevTools', function(e, data){
// Open development tools for the editor
if(mainWindow) mainWindow.webContents.toggleDevTools();
});
ipcMain.on('getPixelboxSettings', function(e, data){
// Retrieve pixelbox settings for the editor
var pixelboxSettings = pixelboxGetSettings();
var TILE_WIDTH = pixelboxSettings.tileSize[0];
var TILE_HEIGHT = pixelboxSettings.tileSize[1];
pixelboxSettings.PIXEL_SIZE = Math.max(TILE_WIDTH, TILE_HEIGHT) >= 20 ? 1 : Math.max(TILE_WIDTH, TILE_HEIGHT) >= 10 ? 2 : 3;
pixelboxSettings.SOLID_LINE_GRID = false;
if(mainWindow){
// Return result of fetching pixelbox settings
mainWindow.webContents.send('getPixelboxSettingsReturn', JSON.stringify(pixelboxSettings));
}
});
ipcMain.on('getPixelboxStaticAssetList', function(e, data){
// Retrieve pixelbox static asset list for the editor
if(mainWindow){
// Return result of fetching static asset list
mainWindow.webContents.send('getPixelboxStaticAssetListReturn', pixelboxGetStaticAssetList());
}
});
ipcMain.on('saveMap', function(e, request){
// Save pixelbox maps for the editor
// FORMAT: request = {command: 'map.save', file: this.file, mapId: this.mapId, data: data };
// Construct object for pixelbox's maps file format
var mapsData = getMapsData(request.file);
mapsData.maps[request.mapId] = request.data;
// Write maps file
fs.writeFileSync(path.join(cwd, 'assets', request.file + '.json'), JSON.stringify(mapsData, null, '\t'));
// Tell the editor that we saved the map
if(mainWindow) mainWindow.webContents.send('mapSaved', null);
// Update global project data information
fs.writeFileSync(path.join(cwd, 'build/data.json'), pixelboxGetStaticAssetList());
//pixelbox.emit('tools/saveMaps', str);
});
ipcMain.on('run', function(e, data){
// Run our project for the editor
createAuxiliaryWindow(640, 640, 'index.html');
});
// -------------------------------------------------
// MISC PIXELBOX FUNCTIONS
// -------------------------------------------------
function pixelboxGetSettings() {
// Loads settings for pixelbox
var settings = JSON.parse(fs.readFileSync(path.join(cwd, 'settings.json'), 'utf8'));
var template = JSON.parse(fs.readFileSync(path.join(__dirname, 'init/settings.json'), 'utf8'));
settings.components = settings.components || {};
mergeObjects(settings.components, template.components);
return settings;
}
function pixelboxGetStaticAssetList() {
// Loads static assets for pixelbox
var staticAssetList = getAsset('assets', [
{ ext: ['png', 'jpg', 'jpeg'], id: 'img' },
{ ext: ['mp3', 'wav'], id: 'snd' },
{ ext: ['txt'] },
{ ext: ['json'], parser: JSON.parse }
]);
return JSON.stringify(staticAssetList);
}
function getMapsData(file) {
// Takes a maps collection file such as 'maps.json' and returns it as an object
// NOTE: Also converts old pixelbox maps file format to new pixelbox maps format
// Default filename is 'maps.json'
file = file || 'maps';
// Read JSON file into object
var mapsData = JSON.parse(fs.readFileSync(path.join(cwd, 'assets', file + '.json'), 'utf8'));
// Convert old map format to the new format if needed
if(Array.isArray(mapsData)){
mapsData = {_type: "maps", maps: mapsData};
}
return mapsData;
}
// -------------------------------------------------
// MISC UTILITY FUNCTIONS
// -------------------------------------------------
function mergeObjects(obj, template) {
for (var id in template) {
if (obj[id] !== undefined) continue;
obj[id] = template[id];
}
}