2 * emccpre.js: one of the Javascript components of an Emscripten-based
3 * web/Javascript front end for Puzzles.
5 * The other parts of this system live in emcc.c and emcclib.js.
7 * This file contains the Javascript code which is prefixed unmodified
8 * to Emscripten's output via the --pre-js option. It declares all our
9 * global variables, and provides the puzzle init function and a
10 * couple of other helper functions.
13 // To avoid flicker while doing complicated drawing, we use two
14 // canvases, the same size. One is actually on the web page, and the
15 // other is off-screen. We do all our drawing on the off-screen one
16 // first, and then copy rectangles of it to the on-screen canvas in
17 // response to draw_update() calls by the game backend.
18 var onscreen_canvas, offscreen_canvas;
20 // A persistent drawing context for the offscreen canvas, to save
21 // constructing one per individual graphics operation.
24 // Bounding rectangle for the copy to the onscreen canvas that will be
25 // done at drawing end time. Updated by js_canvas_draw_update and used
26 // by js_canvas_end_draw.
27 var update_xmin, update_xmax, update_ymin, update_ymax;
29 // Module object for Emscripten. We fill in these parameters to ensure
30 // that Module.run() won't be called until we're ready (we want to do
31 // our own init stuff first), and that when main() returns nothing
32 // will get cleaned up so we remain able to call the puzzle's various
36 'noExitRuntime': true,
39 // Variables used by js_canvas_find_font_midpoint().
40 var midpoint_test_str = "ABCDEFGHIKLMNOPRSTUVWXYZ0123456789";
41 var midpoint_cache = [];
43 // Variables used by js_activate_timer() and js_deactivate_timer().
45 var timer_reference_date;
47 // void timer_callback(double tplus);
49 // Called every 20ms while timing is active.
52 // The status bar object, if we create one.
55 // Currently live blitters. We keep an integer id for each one on the
56 // JS side; the C side, which expects a blitter to look like a struct,
57 // simply defines the struct to contain that integer id.
61 // State for the dialog-box mechanism. dlg_dimmer and dlg_form are the
62 // page-darkening overlay and the actual dialog box respectively;
63 // dlg_next_id is used to allocate each checkbox a unique id to use
64 // for linking its label to it (see js_dialog_boolean);
65 // dlg_return_funcs is a list of JS functions to be called when the OK
66 // button is pressed, to pass the results back to C.
67 var dlg_dimmer = null, dlg_form = null;
69 var dlg_return_funcs = null;
71 // void dlg_return_sval(int index, const char *val);
72 // void dlg_return_ival(int index, int val);
74 // C-side entry points called by functions in dlg_return_funcs, to
75 // pass back the final value in each dialog control.
76 var dlg_return_sval, dlg_return_ival;
78 // The <select> object implementing the game-type drop-down, and a
79 // list of the <option> objects inside it. Used by js_add_preset(),
80 // js_get_selected_preset() and js_select_preset().
81 var gametypeselector = null, gametypeoptions = [];
83 // The two anchors used to give permalinks to the current puzzle. Used
84 // by js_update_permalinks().
85 var permalink_seed, permalink_desc;
87 // The undo and redo buttons. Used by js_enable_undo_redo().
88 var undo_button, redo_button;
90 // Helper function which is passed a mouse event object and a DOM
91 // element, and returns the coordinates of the mouse event relative to
92 // the top left corner of the element by iterating upwards through the
93 // DOM finding each element's offset from its parent, and thus
94 // calculating the page-relative position of the target element so
95 // that we can subtract that from event.page{X,Y}.
96 function relative_mouse_coords(event, element) {
98 while (element.offsetParent) {
99 ex += element.offsetLeft;
100 ey += element.offsetTop;
101 element = element.offsetParent;
103 return {x: event.pageX - ex,
104 y: event.pageY - ey};
107 // Init function called from body.onload.
108 function initPuzzle() {
109 // Construct the off-screen canvas used for double buffering.
110 onscreen_canvas = document.getElementById("puzzlecanvas");
111 offscreen_canvas = document.createElement("canvas");
112 offscreen_canvas.width = onscreen_canvas.width;
113 offscreen_canvas.height = onscreen_canvas.height;
115 // Stop right-clicks on the puzzle from popping up a context menu.
116 // We need those right-clicks!
117 onscreen_canvas.oncontextmenu = function(event) { return false; }
119 // Set up mouse handlers. We do a bit of tracking of the currently
120 // pressed mouse buttons, to avoid sending mousemoves with no
121 // button down (our puzzles don't want those events).
122 mousedown = Module.cwrap('mousedown', 'void',
123 ['number', 'number', 'number']);
125 onscreen_canvas.onmousedown = function(event) {
126 var xy = relative_mouse_coords(event, onscreen_canvas);
127 mousedown(xy.x - onscreen_canvas.offsetLeft,
128 xy.y - onscreen_canvas.offsetTop,
130 buttons_down |= 1 << event.button;
131 onscreen_canvas.setCapture(true);
133 mousemove = Module.cwrap('mousemove', 'void',
134 ['number', 'number', 'number']);
135 onscreen_canvas.onmousemove = function(event) {
137 var xy = relative_mouse_coords(event, onscreen_canvas);
138 mousemove(xy.x - onscreen_canvas.offsetLeft,
139 xy.y - onscreen_canvas.offsetTop,
143 mouseup = Module.cwrap('mouseup', 'void',
144 ['number', 'number', 'number']);
145 onscreen_canvas.onmouseup = function(event) {
146 if (buttons_down & (1 << event.button)) {
147 buttons_down ^= 1 << event.button;
148 var xy = relative_mouse_coords(event, onscreen_canvas);
149 mouseup(xy.x - onscreen_canvas.offsetLeft,
150 xy.y - onscreen_canvas.offsetTop,
155 // Set up keyboard handlers. We expect ordinary keys (with a
156 // charCode) to be handled by onkeypress, but function keys
157 // (arrows etc) to be handled by onkeydown.
159 // We also call event.preventDefault() in both handlers. This
160 // means that while the canvas itself has focus, _all_ keypresses
161 // go only to the puzzle - so users of this puzzle collection in
162 // other media can indulge their instinct to press ^R for redo,
163 // for example, without accidentally reloading the page.
164 key = Module.cwrap('key', 'void',
165 ['number', 'number', 'number', 'number']);
166 onscreen_canvas.onkeydown = function(event) {
167 key(event.keyCode, event.charCode,
168 event.shiftKey ? 1 : 0, event.ctrlKey ? 1 : 0);
169 event.preventDefault();
171 onscreen_canvas.onkeypress = function(event) {
172 if (event.charCode != 0)
173 key(event.keyCode, event.charCode,
174 event.shiftKey ? 1 : 0, event.ctrlKey ? 1 : 0);
175 event.preventDefault();
178 // command() is a C function called to pass back events which
179 // don't fall into other categories like mouse and key events.
180 // Mostly those are button presses, but there's also one for the
181 // game-type dropdown having been changed.
182 command = Module.cwrap('command', 'void', ['number']);
184 // Event handlers for buttons and things, which call command().
185 document.getElementById("specific").onclick = function(event) {
186 // Ensure we don't accidentally process these events when a
187 // dialog is actually active, e.g. because the button still
188 // has keyboard focus
189 if (dlg_dimmer === null)
192 document.getElementById("random").onclick = function(event) {
193 if (dlg_dimmer === null)
196 document.getElementById("new").onclick = function(event) {
197 if (dlg_dimmer === null)
200 document.getElementById("restart").onclick = function(event) {
201 if (dlg_dimmer === null)
204 undo_button = document.getElementById("undo");
205 undo_button.onclick = function(event) {
206 if (dlg_dimmer === null)
209 redo_button = document.getElementById("redo");
210 redo_button.onclick = function(event) {
211 if (dlg_dimmer === null)
214 document.getElementById("solve").onclick = function(event) {
215 if (dlg_dimmer === null)
219 gametypeselector = document.getElementById("gametype");
220 gametypeselector.onchange = function(event) {
221 if (dlg_dimmer === null)
225 // In our dialog boxes, Return and Escape should be like pressing
226 // OK and Cancel respectively
227 document.addEventListener("keydown", function(event) {
229 if (dlg_dimmer !== null && event.keyCode == 13) {
230 for (var i in dlg_return_funcs)
231 dlg_return_funcs[i]();
235 if (dlg_dimmer !== null && event.keyCode == 27)
239 // Set up the function pointers we haven't already grabbed.
240 dlg_return_sval = Module.cwrap('dlg_return_sval', 'void',
241 ['number','string']);
242 dlg_return_ival = Module.cwrap('dlg_return_ival', 'void',
243 ['number','number']);
244 timer_callback = Module.cwrap('timer_callback', 'void', ['number']);
246 // Save references to the two permalinks.
247 permalink_desc = document.getElementById("permalink-desc");
248 permalink_seed = document.getElementById("permalink-seed");
250 // Default to giving keyboard focus to the puzzle.
251 onscreen_canvas.focus();
253 // And run the C setup function, passing argv[1] as the fragment
254 // identifier (so that permalinks of the form puzzle.html#game-id
255 // can launch the specified id).
256 Module.arguments = [location.hash];