-
Notifications
You must be signed in to change notification settings - Fork 0
/
playfield.js
136 lines (119 loc) · 4.5 KB
/
playfield.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
/** @summary Library for managing multiple experiments on a playfield */
(function() {
const Playfield = window.Playfield = Object.create(null);
Playfield._loaded = false;
Playfield._canvas = null;
Playfield._tabs = null;
Playfield._pending_experiments = [];
Playfield._loaded_experiments = [];
Playfield._current_module = null;
Playfield.register_experiment = function(name, definition) {
if (Playfield._loaded) {
Playfield._load_experiment(name, definition);
} else {
Playfield._pending_experiments.push({name, definition});
}
};
Playfield.register_handler = function(module, event, handler) {
if (module._handlers.has(event))
throw new Error('Handler already registered');
module._handlers.set(event, handler);
if (Playfield._current_module === module)
module.canvas.addEventListener(event, handler);
};
Playfield.unregister_handler = function(module, event) {
if (Playfield._current_module === module) {
let handler = module._handlers.get(event);
if (handler !== undefined)
module.canvas.removeEventListener(event, handler);
}
if (!module._handlers.delete(event))
console.error('Module', module, 'does not have event handler for: '+event);
}
Playfield._load_experiment = function(name, definition) {
const module = Object.create(null);
module.name = name;
module._backbuffer = null; // For swapping canvas image
module._handlers = new Map;
module.canvas = Playfield._canvas;
module.init = null;
module.render = null;
// Make a tab
const tab = document.createElement('td');
Playfield._tabs.firstChild.appendChild(tab);
const button = document.createElement('button');
tab.appendChild(button);
button.textContent = module.name;
button.addEventListener('click', function() {
Playfield._switch_module(module);
});
module._tab = tab;
// Execute the module definition
definition.call(null, module);
// Push the module
Playfield._loaded_experiments.push(module);
};
Playfield._start = function() {
// First module or null
Playfield._current_module = Playfield._loaded_experiments[0] ?? null;
if (Playfield._current_module === null) {
const ctx = Playfield._canvas.getContext('2d');
ctx.fillStyle = '#808080';
ctx.fillRect(0, 0, Playfield._canvas.width, Playfield._canvas.height);
}
for (let m of Playfield._loaded_experiments) {
if (m.init !== null)
m.init();
}
Playfield._render(0);
};
Playfield._render = function(dt) {
requestAnimationFrame(Playfield._render);
if (Playfield._current_module === null)
return;
if (Playfield._current_module.render) {
Playfield._current_module.render(dt);
}
};
Playfield._switch_module = function(new_mod) {
if (Playfield._current_module === new_mod)
return;
if (Playfield._current_module !== null) {
// Save the current image of the canvas
Playfield._current_module._backbuffer =
Playfield._canvas.getContext('2d').getImageData(0, 0, Playfield._canvas.width, Playfield._canvas.height);
for (let [event, handler] of Playfield._current_module._handlers.entries())
Playfield._current_module.canvas.removeEventListener(event, handler);
}
Playfield._current_module = new_mod;
if (new_mod._backbuffer !== null) {
// Restore saved image
Playfield._canvas.getContext('2d').putImageData(new_mod._backbuffer, 0, 0);
new_mod._backbuffer = null;
}
for (let [event, handler] of new_mod._handlers.entries())
new_mod.canvas.addEventListener(event, handler);
};
document.addEventListener('DOMContentLoaded', function() {
// Prepare the environment
Playfield._canvas = document.getElementById('PlayfieldCanvas');
Playfield._tabs = document.getElementById('PlayfieldTabs');
Playfield._tabs.appendChild(document.createElement('th'));
Playfield._loaded = true;
const cnv = /** @type {HTMLCanvasElement} */(Playfield._canvas);
cnv.width = cnv.offsetWidth;
cnv.height = cnv.offsetHeight;
window.addEventListener('resize', function() {
cnv.width = cnv.height = 1;
cnv.width = cnv.offsetWidth;
cnv.height = cnv.offsetHeight;
});
// Load all pending experiments
while (Playfield._pending_experiments.length !== 0) {
const pend = Playfield._pending_experiments.shift();
Playfield._load_experiment(pend.name, pend.definition);
}
// Start the playfield
Playfield._start();
});
})();