shithub: puzzles

Download patch

ref: c5a2446fae603a480de58b912fa349549bd9f247
parent: 4a37f7cf782592b670d0180a38eb1fd680288421
author: Ben Harris <bjh21@bjh21.me.uk>
date: Sat Nov 5 13:15:52 EDT 2022

js: Cancel UI events when the mid end says they've been handled

This means that if a key doesn't do anything in a puzzle, it can operate
the browser instead.

--- a/emcc.c
+++ b/emcc.c
@@ -246,28 +246,37 @@
 /*
  * Mouse event handlers called from JS.
  */
-void mousedown(int x, int y, int button)
+bool mousedown(int x, int y, int button)
 {
+    bool handled;
+
     button = (button == 0 ? LEFT_BUTTON :
               button == 1 ? MIDDLE_BUTTON : RIGHT_BUTTON);
-    midend_process_key(me, x, y, button, NULL);
+    midend_process_key(me, x, y, button, &handled);
     update_undo_redo();
+    return handled;
 }
 
-void mouseup(int x, int y, int button)
+bool mouseup(int x, int y, int button)
 {
+    bool handled;
+
     button = (button == 0 ? LEFT_RELEASE :
               button == 1 ? MIDDLE_RELEASE : RIGHT_RELEASE);
-    midend_process_key(me, x, y, button, NULL);
+    midend_process_key(me, x, y, button, &handled);
     update_undo_redo();
+    return handled;
 }
 
-void mousemove(int x, int y, int buttons)
+bool mousemove(int x, int y, int buttons)
 {
     int button = (buttons & 2 ? MIDDLE_DRAG :
                   buttons & 4 ? RIGHT_DRAG : LEFT_DRAG);
-    midend_process_key(me, x, y, button, NULL);
+    bool handled;
+
+    midend_process_key(me, x, y, button, &handled);
     update_undo_redo();
+    return handled;
 }
 
 /*
@@ -283,6 +292,7 @@
     #define DOM_KEY_LOCATION_RIGHT    2
     #define DOM_KEY_LOCATION_NUMPAD   3
     int keyevent = -1;
+    bool handled;
 
     if (!strnullcmp(key, "Backspace") || !strnullcmp(key, "Delete") ||
         !strnullcmp(key, "Del"))
@@ -379,9 +389,9 @@
             location == DOM_KEY_LOCATION_NUMPAD)
             keyevent |= MOD_NUM_KEYPAD;
 
-        midend_process_key(me, 0, 0, keyevent, NULL);
+        midend_process_key(me, 0, 0, keyevent, &handled);
         update_undo_redo();
-        return true; /* We've probably handled the event. */
+        return handled;
     }
     return false; /* Event not handled, because we don't even recognise it. */
 }
--- a/emccpre.js
+++ b/emccpre.js
@@ -254,7 +254,7 @@
     // Set up mouse handlers. We do a bit of tracking of the currently
     // pressed mouse buttons, to avoid sending mousemoves with no
     // button down (our puzzles don't want those events).
-    mousedown = Module.cwrap('mousedown', 'void',
+    mousedown = Module.cwrap('mousedown', 'boolean',
                              ['number', 'number', 'number']);
 
     button_phys2log = [null, null, null];
@@ -277,21 +277,23 @@
         else if (event.ctrlKey)
             logbutton = 2;   // Ctrl-click overrides to right button
 
-        mousedown(xy.x, xy.y, logbutton);
+        if (mousedown(xy.x, xy.y, logbutton))
+            event.preventDefault();
         button_phys2log[event.button] = logbutton;
 
         onscreen_canvas.setCapture(true);
     };
-    mousemove = Module.cwrap('mousemove', 'void',
+    mousemove = Module.cwrap('mousemove', 'boolean',
                              ['number', 'number', 'number']);
     onscreen_canvas.onmousemove = function(event) {
         var down = buttons_down();
         if (down) {
             var xy = canvas_mouse_coords(event, onscreen_canvas);
-            mousemove(xy.x, xy.y, down);
+            if (mousemove(xy.x, xy.y, down))
+                event.preventDefault();
         }
     };
-    mouseup = Module.cwrap('mouseup', 'void',
+    mouseup = Module.cwrap('mouseup', 'boolean',
                            ['number', 'number', 'number']);
     onscreen_canvas.onmouseup = function(event) {
         if (event.button >= 3)
@@ -299,7 +301,8 @@
 
         if (button_phys2log[event.button] !== null) {
             var xy = canvas_mouse_coords(event, onscreen_canvas);
-            mouseup(xy.x, xy.y, button_phys2log[event.button]);
+            if (mouseup(xy.x, xy.y, button_phys2log[event.button]))
+                event.preventDefault();
             button_phys2log[event.button] = null;
         }
     };