-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.js
501 lines (446 loc) · 13.1 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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*global document*/
/*global window*/
/*global all_bubbles*/
/*global all_curves*/
/*global Blob*/
/*global Bubble*/
/*global Bubbles*/
/*global Colors*/
/*global ConMenu*/
/*global Context*/
/*global Curve*/
/*global Curves*/
/*global Edit*/
/*global FactBox*/
/*global Floaty*/
/*global Floatys*/
/*global Help*/
/*global KEY*/
/*global localStorage*/
/*global MathJax*/
/*global navigator*/
/*global Sounds*/
/*global Storage*/
/*global Teleport*/
/*global TempLine*/
"use strict";
var context = new Context(document.getElementById('canvas'));
var floaties = new Floatys();
var factBox = new FactBox();
var bubbles = new Bubbles();
var curves = new Curves();
var edit = new Edit();
var help = new Help();
var templine = new TempLine();
var sounds = new Sounds();
sounds.playBGM();
var teleport = new Teleport();
var openBubble = null;
var editingBubble = null;
function renderEverything() {
context.renderBG();
context.draw([floaties, bubbles, curves, templine]);
context.drawAbsolute(factBox);
context.drawAbsolute(sounds);
context.drawAbsolute(edit);
context.drawAbsolute(help);
context.drawAbsolute(teleport);
context.drawDevMode();
}
function updateEverything() {
context.applySpeed();
floaties.update(context.low(), context.high(), context.left(), context.width());
sounds.refreshBgm();
sounds.fadeOut();
help.fadeOut();
teleport.fadeOut();
}
function editGraph() {
var mousePos = context.scaledMousePos(),
curve = null,
bubble = bubbles.collide(mousePos);
if (bubble === undefined && editingBubble === null) {
console.log("create");
bubble = new Bubble(bubbles.genUniqId(), mousePos.x, mousePos.y, 100, bubbles.length().toString(), "New knowledge shall arrive here soon");
bubbles.add(bubble.getIndex(), bubble);
} else if (bubble === undefined && editingBubble !== null) {
console.log("move-end");
edit.off();
editingBubble.moveTo(mousePos.x, mousePos.y);
curves.reposition(editingBubble.getIndex(), bubbles);
editingBubble = null;
templine.setStart(null);
} else if (bubble !== undefined && editingBubble !== null) {
console.log("link-end");
edit.off();
curve = new Curve(editingBubble.x, editingBubble.y, editingBubble.r, bubble.x, bubble.y, bubble.r);
curves.append(curve, editingBubble.getIndex(), bubble.getIndex());
editingBubble = null;
templine.setStart(null);
} else if (bubble !== undefined && editingBubble === null) {
console.log("movelink-start");
edit.on();
editingBubble = bubble;
if (bubble !== undefined) {
templine.setStart(bubble.getXY());
} else {
templine.setStart(null);
}
}
}
function mouseHoverListener(evt) {
var smousePos = context.scaledMousePos(evt),
mPos = context.mousePos(evt);
bubbles.hover(smousePos, sounds);
sounds.hoverButton(mPos);
help.hoverButton(mPos);
teleport.hoverButton(mPos);
templine.setStop(smousePos);
}
function zoom(evt) {
if (evt.deltaY !== undefined) {
context.zoomMouseVal(evt.deltaY);
} else if (evt.detail !== undefined) {
context.zoomMouseVal(evt.detail);
}
}
function mouseMoveListener(evt) {
var mousePos = context.mousePos(evt),
offtmp = context.mouseDown,
dx = offtmp.x - mousePos.x,
dy = offtmp.y - mousePos.y;
if (factBox.isActive() === false) {
context.offsetTemporary(dx, dy);
}
}
function mouseUpListener(evt) {
context.canvas.removeEventListener('mousemove', mouseMoveListener, false);
window.removeEventListener("mouseup", mouseUpListener, false);
context.canvas.addEventListener('mousemove', mouseHoverListener, false);
var mouseOnUp = context.mousePos(evt),
mouseOnDown = context.mouseDown;
if (factBox.isActive() === false) {
context.addOffset(mouseOnDown.x - mouseOnUp.x, mouseOnDown.y - mouseOnUp.y);
}
}
function drawFactBox(onCircle) {
if (onCircle.hit) {
sounds.openInfo();
window.removeEventListener("mouseup", mouseUpListener, false);
context.canvas.addEventListener('mousemove', mouseHoverListener, false);
factBox.show(onCircle.facts, openBubble.getFav(), openBubble.isMastered());
} else {
context.canvas.addEventListener('mousemove', mouseMoveListener, false);
factBox.hide();
}
}
function drawFactBoxSpace(onCircle) {
if (factBox.isActive() === true) {
factBox.hide();
return;
}
openBubble = onCircle.bubble;
if (onCircle.hit) {
factBox.show(onCircle.facts, openBubble.getFav(), openBubble.isMastered());
sounds.openInfo();
window.removeEventListener("mouseup", mouseUpListener, false);
context.canvas.addEventListener('mousemove', mouseHoverListener, false);
}
}
function openEditor() {
var nameFacts = openBubble.getNameAndFacts();
factBox.openEditor(nameFacts);
}
function closeEditor() {
factBox.closeEditor(openBubble);
}
function closeEditorNoSave() {
factBox.closeEditorNoSave();
}
function master() {
var mastered;
openBubble.flipMaster();
curves.masterFrom(openBubble.getIndex(), openBubble.isMastered());
factBox.setMaster(openBubble.isMastered());
if (Storage !== undefined) {
mastered = JSON.stringify(bubbles.getMastered());
localStorage.setItem("mastered", mastered);
}
}
function teleportTo(x, y) {
var bubble;
if (y === undefined && x !== undefined) {
bubble = bubbles.getNamed(x);
if (bubble !== undefined) {
context.centerAbs(bubble.getXY().x, bubble.getXY().y);
factBox.show(bubble.getNameAndFacts(), bubble.getFav(), bubble.isMastered());
}
} else if (x !== undefined) {
context.centerAbs(x, y);
}
}
function openTeleport() {
openBubble = bubbles.getNamed('magic_carpet');
drawFactBox({
hit: true,
facts: openBubble.getNameAndFacts()
});
}
function mouseDownListener(evt) {
var onCircle = false,
mousePos = null,
scaledPos = null;
if (evt.button === 2) {
return;
}
if (factBox.isEditing()) {
return;
}
context.canvas.removeEventListener('mousemove', mouseHoverListener, false);
window.addEventListener("mouseup", mouseUpListener, false);
mousePos = context.mousePos(evt);
scaledPos = context.scaledMousePos(evt);
context.mouseDown = mousePos;
onCircle = bubbles.click(scaledPos);
if (onCircle.hit) {
openBubble = onCircle.bubble;
} else {
factBox.hide();
}
drawFactBox(onCircle);
sounds.onClick(mousePos);
help.click();
if (teleport.click()) {
openTeleport();
}
}
function isMovementKey(key) {
switch (key.which) {
case KEY.LEFT:
case KEY.UP:
case KEY.RIGHT:
case KEY.DOWN:
case KEY.W:
case KEY.A:
case KEY.S:
case KEY.D:
case KEY.H:
case KEY.J:
case KEY.K:
case KEY.L:
return true;
default:
return false;
}
}
function setCanvasSpeed(key, speed) {
switch (key.which) {
case KEY.LEFT:
case KEY.A:
case KEY.H:
context.setSpeedX(-speed);
break;
case KEY.UP:
case KEY.W:
case KEY.K:
context.setSpeedY(-speed);
break;
case KEY.RIGHT:
case KEY.D:
case KEY.L:
context.setSpeedX(speed);
break;
case KEY.DOWN:
case KEY.S:
case KEY.J:
context.setSpeedY(speed);
break;
}
if (isMovementKey(key)) {
bubbles.hover(context.getCenterPos(), sounds);
}
}
function downloadData(filename, data) {
var blob = new Blob([data], {
type: 'text/csv'
}),
elem;
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, filename);
} else {
elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob);
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
}
function surroundQuotes(string) {
return '"' + string + '"';
}
function jsEscape(string) {
return string.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/"/g, '\\"');
}
function generateDataJs() {
var tot = '"use strict";\n\nvar all_bubbles = {\n',
i = null,
bubble = null;
for (i = 0; i < bubbles.length(); i += 1) {
bubble = bubbles.getBubble(i);
tot += ' ' + bubble.getIndex() + ': {\n';
tot += ' x: ' + bubble.getXY().x + ',\n';
tot += ' y: ' + bubble.getXY().y + ',\n';
tot += ' r: ' + bubble.getR() + ',\n';
tot += ' color: "' + bubble.getColor() + '",\n';
tot += ' link: [' + curves.getForwards(bubble.getIndex()).map(surroundQuotes) + '],\n';
tot += ' title: "' + bubble.getTitle().replace(/[\""]/g, '\\"') + '",\n';
tot += ' facts: "' + jsEscape(bubble.getFacts()) + '",\n';
tot += ' },\n';
}
tot += '};';
downloadData('Data.js', tot);
}
function keyboardDown(key) {
var movingSpeed = 20;
if (factBox.isActive()) {
if (key.which === KEY.ESC && factBox.isEditing() === false) {
drawFactBoxSpace();
return;
}
return;
}
help.deactivate();
switch (key.which) {
case KEY.ESC:
editingBubble = null;
templine.setStart(null);
edit.off();
break;
case KEY.G:
generateDataJs();
break;
case KEY.SPACE:
drawFactBoxSpace(bubbles.click(context.getCenterPos()));
break;
case KEY.M:
sounds.nextState();
break;
case KEY.E:
context.flipDevMode();
break;
case KEY.T:
openTeleport();
break;
case KEY.Q:
editGraph();
break;
case KEY.MIN:
context.zoomOut();
break;
case KEY.PLUS:
context.zoomIn();
break;
default:
setCanvasSpeed(key, movingSpeed);
factBox.hide();
break;
}
}
function keyboardUp(key) {
setCanvasSpeed(key, 0);
}
function onResize() {
context.onResize();
help.resize(context.canvas.width);
teleport.resize(context.canvas.width);
}
function contextMenu() {
editGraph();
return false;
}
function toggleFavorite() {
var favd;
openBubble.toggleFav();
if (Storage !== undefined) {
favd = JSON.stringify(bubbles.getFavd());
localStorage.setItem("favd", favd);
}
}
function setupBubblesAndCurves() {
var i = null,
j = null,
b = null,
bubble,
curve = null,
end = null,
mastereds,
tmps;
for (i in all_bubbles) {
if (all_bubbles.hasOwnProperty(i)) {
b = all_bubbles[i];
bubble = new Bubble(i, b.x, b.y, b.r, b.title, b.facts);
if (b.color !== undefined) {
bubble.setColor(b.color);
}
bubbles.add(i, bubble);
}
}
for (i in all_bubbles) {
if (all_bubbles.hasOwnProperty(i)) {
b = all_bubbles[i];
for (j = 0; j < b.link.length; j += 1) {
end = all_bubbles[b.link[j]];
curve = new Curve(b.x, b.y, b.r, end.x, end.y, end.r);
curve.setColor(b.color);
curves.append(curve, i, b.link[j]);
}
}
}
if (Storage === undefined) {
document.getElementById("viewmaster").disabled = true;
} else {
if (localStorage.mastered !== undefined) {
mastereds = JSON.parse(localStorage.mastered);
bubbles.setMastereds(mastereds);
for (i = 0; i < mastereds.length; i += 1) {
curves.masterFrom(mastereds[i], true);
}
}
if (localStorage.favd !== undefined) {
mastereds = JSON.parse(localStorage.favd);
for (i = 0; i < mastereds.length; i += 1) {
tmps = bubbles.getNamed(mastereds[i]);
if (tmps !== undefined) {
tmps.toggleFav();
}
}
}
}
}
function loop() {
var frametime = 30;
updateEverything();
renderEverything();
setTimeout(loop, frametime);
}
function main() {
var mousewheelevt = null;
setupBubblesAndCurves();
onResize();
context.centerOn(0, 0);
context.canvas.addEventListener('mousemove', mouseHoverListener, false);
context.canvas.addEventListener("mousedown", mouseDownListener, false);
window.addEventListener("resize", onResize, false);
document.addEventListener("keydown", keyboardDown, false);
document.addEventListener("keyup", keyboardUp, false);
mousewheelevt = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel";
if (context.canvas.attachEvent) {
context.canvas.attachEvent("on" + mousewheelevt, zoom);
} else if (document.addEventListener) {
context.canvas.addEventListener(mousewheelevt, zoom, false);
}
context.canvas.oncontextmenu = contextMenu;
loop();
}
window.onload = main;