-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmain.js
More file actions
239 lines (192 loc) · 5.01 KB
/
Copy pathmain.js
File metadata and controls
239 lines (192 loc) · 5.01 KB
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
/****************************\
============================
CCBRIDGE JS
============================
\****************************/
// import packages
const electron = require('electron');
const url = require('url');
const path = require('path');
// app
const {app, BrowserWindow, Menu, ipcMain} = electron;
/****************************\
============================
GAME EDITOR
============================
\****************************/
// main window
let gameEditor;
// Listen for app ready
app.on('ready', function() {
// create new window
gameEditor = new BrowserWindow({
show: false,
resizable: false,
width: 447,
height: 564,
webPreferences: {
enableRemoteModule: true,
nodeIntegration: true,
contextIsolation: false
}
});
// load URL into window
gameEditor.loadURL(url.format({
pathname: path.join(__dirname, 'views/game_editor.html'),
protocol: 'file:',
slashes: true
}));
// to look more like desktop app
gameEditor.webContents.on('did-finish-load', function() {
gameEditor.show();
// Open the DevTools.
//gameEditor.webContents.openDevTools()
});
// quit app when closed
gameEditor.on('closed', function() {
app.quit();
});
// Build menu from template
const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
// Insert menu
Menu.setApplicationMenu(mainMenu);
});
/****************************\
============================
ENGINE WINDOW
============================
\****************************/
// window instances
let engineWindow1 = null;
let engineWindow2 = null;
// play with engines
function createEngineWindow1() {
if (engineWindow1 == null) {
// create new window
engineWindow1 = new BrowserWindow({
show: false,
resizable: false,
width: 391,
height: 588,
title: 'Engine 1',
webPreferences: {
enableRemoteModule: true,
nodeIntegration: true,
contextIsolation: false
}
});
// load URL into window
engineWindow1.loadURL(url.format({
pathname: path.join(__dirname, 'views/engine_play.html'),
protocol: 'file:',
slashes: true
}));
// to look more like desktop app
engineWindow1.webContents.on('did-finish-load', function() {
engineWindow1.show();
});
// garbage collection handle
engineWindow1.on('close', function() {
engineWindow1 = null;
});
// Insert menu
engineWindow1.setMenu(null);
}
}
// play with engines
function createEngineWindow2() {
if (engineWindow2 == null) {
// create new window
engineWindow2 = new BrowserWindow({
show: false,
resizable: false,
width: 391,
height: 588,
title: 'Engine 2',
webPreferences: {
enableRemoteModule: true,
nodeIntegration: true,
contextIsolation: false
}
});
// load URL into window
engineWindow2.loadURL(url.format({
pathname: path.join(__dirname, 'views/engine_play.html'),
protocol: 'file:',
slashes: true
}));
// to look more like desktop app
engineWindow2.webContents.on('did-finish-load', function() {
engineWindow2.show();
});
// garbage collection handle
engineWindow2.on('close', function() {
engineWindow2 = null;
});
// Insert menu
engineWindow2.setMenu(null);
}
}
/****************************\
============================
WINDOW MENU TEMPLATES
============================
\****************************/
const mainMenuTemplate = [
{
label: 'File',
submenu: [
{
label: 'New game',
click() { gameEditor.webContents.send('newgame'); }
},
{
label: 'Open game',
click() { gameEditor.webContents.send('opengame'); }
},
{
label: 'Save game',
click() { gameEditor.webContents.send('savegame'); }
}
]
},
{
label: 'Engine',
submenu: [
{
label: 'Load engine 1',
click() { createEngineWindow1(); }
},
{
label: 'Load engine 2',
click() { createEngineWindow2(); }
}
]
},
{
label: 'Movelist',
click() { gameEditor.webContents.send('movelist'); }
}
];
/****************************\
============================
WINDOW COMMUNICATIONS
============================
\****************************/
// listen to create engine window request
ipcMain.on('engine1', function() {
createEngineWindow1();
});
// listen to create engine window request
ipcMain.on('engine2', function() {
createEngineWindow2();
});
// listen to best move from engine
ipcMain.on('bestmove', function(e, bestMove){
gameEditor.webContents.send('bestmove', bestMove);
});
// listen to GUI move
ipcMain.on('guifen', function(e, guiFen) {
if (engineWindow1) engineWindow1.webContents.send('guifen', guiFen);
if (engineWindow2) engineWindow2.webContents.send('guifen', guiFen);
});