-
Notifications
You must be signed in to change notification settings - Fork 2
/
front.js
executable file
·333 lines (295 loc) · 9.31 KB
/
front.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
/**
* @fileoverview This file contains all the graphical functions and event manager that work with the interface (client-side on browser)
* @see mainClient.js
*/
let name = "";
document.getElementById('user-name-input').value = generateNickname();
document.getElementById('version').innerHTML += softVersion;
let workspace;
let lastbeacon = null;
createLedTable(nbRows, nbColumns, disabled_pixels);
initWorkspace();
/**
* Display informations relative to backend only if a backend is connected
*/
if(!simulation_enabled){
socket.on('logged',(user) => {
$('#user-name').text(user.name);
$('#user-ip').text(user.ip);
//hideLoginScreen();
lastbeacon = (new Date()).getTime();
$('.overlay-popup').hide();
});
socket.on('granted', function () {
granted = true;
$('.connect-style').replaceWith('<p class="connect-style live">live</p>');
});
socket.on('ungranted', function () {
granted = false;
$('.live').replaceWith('<p class="connect-style">Connecté</p>');
});
socket.on('isAlive', function () {
lastbeacon = (new Date()).getTime();
socket.emit('isAlive');
console.log("isAlive replied");
});
function beaconChecker() {
console.log("beanconChecker");
if ( (lastbeacon != null) && (new Date()).getTime() - lastbeacon > 3000){
granted = false;
$('.connect-style').replaceWith('<p class="connect-style">Déconnecté</p>');
}
}
setInterval(beaconChecker, 1000);
} else {
$('.info-user').css({
"display": 'none'
});
}
// Event keys for Blockly, stores the corresponding event in a sharedArray to be read by the worker
$(document).on('keydown', function (e) {
if (isRunning) {
switch (e.which) {
case 38: // UP
blocklyWorker.postMessage({message:'keyEvent', key: 'up'});
break;
case 39: // RIGHT
blocklyWorker.postMessage({message:'keyEvent', key: 'right'});
break;
case 40: // DOWN
blocklyWorker.postMessage({message:'keyEvent', key: 'down'});
break;
case 37: // LEFT
blocklyWorker.postMessage({message:'keyEvent', key: 'left'});
break;
case 32: // SPACE
blocklyWorker.postMessage({message:'keyEvent', key: 'space'});
break;
}
}
});
// Animations and clicking behaviour definitions
$('#play').on('click', function (e) {
e.preventDefault();
run();
});
$('#stop').on('click', function (e) {
e.preventDefault();
stop();
});
$('#config').on('click', function (e) {
e.preventDefault();
settingForm();
$('.overlay-popup3').fadeIn(200);
$("#setting-module").fadeIn(200, function () {
$('#setting-submit').on('click', function () {
if (simulation_enabled){
if (setconfig($('#setting-rows').val(), $('#setting-cols').val(), $('#setting-disabled').val())){
createLedTable(nbRows, nbColumns);
$('#setting-module').fadeOut(200);
$('.overlay-popup3').fadeOut(200);
} else {
alert("invalid arguments");
}
} else{
$('#setting-module').fadeOut(200);
$('.overlay-popup3').fadeOut(200);
}
})
})
})
$('#import').on('click', function (e) {
e.preventDefault();
if (Blockly.mainWorkspace.getAllBlocks().length > 1){
if (!confirm('Vous allez perdre votre travail. Êtes vous sûr(e) de vouloir continuer ?')){
return;
}
}
$('#fileImport').click();
});
$('#export').on('click', function () {
document.getElementById('export-input').value = name.replace(' ', '_');
$('.overlay-popup3').fadeIn(200);
$("#export-module").fadeIn(200, function () {
$('#export-file').on('click', function () {
let name = $('#export-input').val();
save(name);
$('#export-module').fadeOut(200);
$('.overlay-popup3').fadeOut(200);
})
})
})
$('#export-input').keypress(function (event) {
if (event.which == 13) {
$('#export-file').click();
}
});
$('#fileImport').on('change', function (e) {
importWorkspace();
});
$('#example').on('click', function () {
$('.overlay-popup3').fadeIn(200);
$('#example-module').fadeIn(200);
});
$('#file').on('click',function(){
if (Blockly.mainWorkspace.getAllBlocks().length > 1){
if (!confirm('Vous allez perdre votre travail. Êtes vous sûr(e) de vouloir continuer ?')){
return;
}
}
workspace.clear();
let mainBlock = workspace.newBlock('main_script');
mainBlock.initSvg();
mainBlock.render();
mainBlock.moveBy(300,30);
});
$('#challenges').on('click', function () {
$('.overlay-popup3').fadeIn(200)
$('#challenges-module').fadeIn(200)
})
$('#informations').on('click', function () {
$('.overlay-popup3').fadeIn(200)
$('#informations-module').fadeIn(200)
})
$('.overlay-popup3').on('click', function () {
$(this).fadeOut(200, function () {
$('#example-module').fadeOut(200)
$('#export-module').fadeOut(200)
$('#setting-module').fadeOut(200)
$('#informations-module').fadeOut(200)
$('#challenges-module').fadeOut(200)
})
});
$('#full-screen').on('click', function (e) {
e.preventDefault();
toogleFullScreen();
});
$('.setting-menu').hover(function () {
$('.info-user').fadeOut(200);
}, function () {
if (!simulation_enabled){
$('.info-user').fadeIn(200);
}
});
$('#send-name').on('click', function () {
name = $('#user-name-input').val()
if(name != ""){
hideLoginScreen();
if (! simulation_enabled){
socket.emit('login', name);
}
}
});
$('#user-name-input').keypress(function (event) {
if (event.which == 13) {
$('#send-name').click();
}
});
$('#turn-led').on('click', function(e){
e.preventDefault();
$('#led-table').toggleClass('active-rotate');
$('.led-coords').toggleClass('active-rotate-revert');
$(this).toggleClass('active-rotate-button');
});
$(".example-item").on('click',function(){
loadExemple($(this).data('name'));
$("#example-module").fadeOut(200);
$('.overlay-popup3').fadeOut(200);
});
function hideLoginScreen(){
$('.overlay-popup').css({
"transition": "0.4s ease",
"transform": " scale(1.05)"
}).delay(500).fadeOut(400);
}
/**
* Init the blockly workspace and the toolbox
*/
function initWorkspace() {
let toolbox = document.getElementById('toolbox');
// Creating the workspace
workspace = Blockly.inject('blocklyDiv', {
toolbox: toolbox,
collapse: true,
comments: true,
disable: true,
maxBlocks: Infinity,
trashcan: true,
horizontalLayout: false,
toolboxPosition: 'start',
css: true,
rtl: false,
scrollbars: false,
sounds: true,
oneBasedIndex: true
});
// Adding a program block to this workspace
let mainBlock = workspace.newBlock('main_script');
mainBlock.initSvg();
mainBlock.render();
mainBlock.moveBy(300,30);
}
/**
* Tells if the i,j pixel is disabled
* @param {Number} row
* @param {Number} column
*/
function is_disabled(i,j){
for (let pos of disabled_pixels) {
if ((pos[0] == i) && (pos[1] == j)){
return true;
}
}
return false;
}
/**
* Generate the HTML table of the pixels for the simulation
* @param {Number} nbRows
* @param {Number} nbColumns
*/
function createLedTable(nbRows, nbColumns) {
let ledContainer = document.getElementById('led-table');
ledContainer.innerHTML = "";
for (let i = 0; i < nbRows; i++) {
let newRow = ledContainer.insertRow();
for (let j = 0; j < nbColumns; j++) {
if (is_disabled(i, j)) {
newRow.insertCell(j).innerHTML = `<div class="dled" data-r="${i}" data-c="${j}"></div>`;
} else {
newRow.insertCell(j).innerHTML = `<div class="led" data-r="${i}" data-c="${j}"><div class="led-coords"><p>Ligne ${i}</p><p>Colonne ${j}</p></div></div>`;
}
}
}
}
/**
* Toogle the full-screen mode of the simulation
*/
function toogleFullScreen() {
if (!$('.led-content').hasClass('full-screen')) {
$('.led-content').wrap('<div class="overlay-popup2"></div>');
$('.led-content').addClass('full-screen');
} else {
$('.led-content').removeClass('full-screen');
$('.overlay-popup2').contents().unwrap();
}
}
/**
* Switch the colors of Play and Stop buttons
*/
function switchPlayStopColors() {
if (isRunning) {
$('#play').css({
"background-image": 'url(../asset/images/icon/play.png)'
});
$('#stop').css({
"background-image": 'url(../asset/images/icon/stop_red.png)'
});
} else {
$('#play').css({
"background-image": 'url(../asset/images/icon/play_green.png)'
});
$('#stop').css({
"background-image": 'url(../asset/images/icon/stop.png)'
});
}
}