chiark / gitweb /
IE doesn't default to giving focus to the puzzle canvas on a mouse
[sgt-puzzles.git] / emccpre.js
index ea2077174cb43ea3c8f5a0f3616b4e41bb2ac50e..38d7815ae90bbb685712a72963a0dfe0ab28379d 100644 (file)
@@ -2,7 +2,11 @@
  * emccpre.js: one of the Javascript components of an Emscripten-based
  * web/Javascript front end for Puzzles.
  *
- * The other parts of this system live in emcc.c and emcclib.js.
+ * The other parts of this system live in emcc.c and emcclib.js. It
+ * also depends on being run in the context of a web page containing
+ * an appropriate collection of bits and pieces (a canvas, some
+ * buttons and links etc), which is generated for each puzzle by the
+ * script html/jspage.pl.
  *
  * This file contains the Javascript code which is prefixed unmodified
  * to Emscripten's output via the --pre-js option. It declares all our
@@ -33,7 +37,7 @@ var update_xmin, update_xmax, update_ymin, update_ymax;
 // callbacks.
 var Module = {
     'noInitialRun': true,
-    'noExitRuntime': true,
+    'noExitRuntime': true
 };
 
 // Variables used by js_canvas_find_font_midpoint().
@@ -229,6 +233,12 @@ function initPuzzle() {
             command(2);
     };
 
+    // In IE, the canvas doesn't automatically gain focus on a mouse
+    // click, so make sure it does
+    onscreen_canvas.addEventListener("mousedown", function(event) {
+        onscreen_canvas.focus();
+    });
+
     // In our dialog boxes, Return and Escape should be like pressing
     // OK and Cancel respectively
     document.addEventListener("keydown", function(event) {
@@ -257,9 +267,16 @@ function initPuzzle() {
     // Default to giving keyboard focus to the puzzle.
     onscreen_canvas.focus();
 
-    // And run the C setup function, passing argv[1] as the fragment
+    // Run the C setup function, passing argv[1] as the fragment
     // identifier (so that permalinks of the form puzzle.html#game-id
     // can launch the specified id).
     Module.arguments = [location.hash];
     Module.run();
+
+    // And if we get here with everything having gone smoothly, i.e.
+    // we haven't crashed for one reason or another during setup, then
+    // it's probably safe to hide the 'sorry, no puzzle here' div and
+    // show the div containing the actual puzzle.
+    document.getElementById("apology").style.display = "none";
+    document.getElementById("puzzle").style.display = "inline";
 }