Browse Source

Merge branch 'castano-ic-dropevent-modifiers'

Andre Weissflog 2 years ago
parent
commit
1b5d6af327
2 changed files with 85 additions and 31 deletions
  1. 17 0
      CHANGELOG.md
  2. 68 31
      sokol_app.h

+ 17 - 0
CHANGELOG.md

@@ -1,5 +1,22 @@
 ## Updates
 
+- **20-May-2023**: some minor event-related cleanup in sokol_app.h:
+    - in the event `SAPP_EVENTTYPE_FILESDROPPED`:
+        - the `sapp_event.modifier` field now contains the active modifier keys
+          at the time of the file drop operations on the platforms macOS, Emscripten
+          and Win32 (on Linux I haven't figured out how this might work with the
+          Xlib API)
+        - on macOS, the `sapp_event.mouse_x/y` fields now contain the window-relative
+          mouse position where the drop happened (this already worked as expected on
+          the other desktop platforms)
+        - on macOS and Linux, the `sapp_event.mouse_dx/dy` fields are now set to zero
+          (this already was the case on Emscripten and Win32)
+    - in the events `SAPP_EVENTTYPE_MOUSE_ENTER` and `SAPP_EVENTTYPE_MOUSE_LEAVE`:
+        - the `sapp_event.mouse_dx/dy` fields are now set to zero, previously this
+          could be a very big value on some desktop platforms
+
+    Many thanks to @castano for the initial PR (https://github.com/floooh/sokol/pull/830)!
+
 - **19-May-2023**: _**BREAKING CHANGES**_ in sokol_gfx.h: Render passes are now more 'harmonized'
   with Metal and WebGPU by exposing a 'store action', and making MSAA resolve attachments
   explicit. The changes in detail:

+ 68 - 31
sokol_app.h

@@ -3464,7 +3464,7 @@ int main(int argc, char* argv[]) {
 #endif /* SOKOL_NO_ENTRY */
 
 _SOKOL_PRIVATE uint32_t _sapp_macos_mods(NSEvent* ev) {
-    const NSEventModifierFlags f = ev.modifierFlags;
+    const NSEventModifierFlags f = (ev == nil) ? NSEvent.modifierFlags : ev.modifierFlags;
     const NSUInteger b = NSEvent.pressedMouseButtons;
     uint32_t m = 0;
     if (f & NSEventModifierFlagShift) {
@@ -3610,12 +3610,15 @@ _SOKOL_PRIVATE void _sapp_macos_update_window_title(void) {
     [_sapp.macos.window setTitle: [NSString stringWithUTF8String:_sapp.window_title]];
 }
 
-_SOKOL_PRIVATE void _sapp_macos_mouse_update(NSEvent* event) {
+_SOKOL_PRIVATE void _sapp_macos_mouse_update_from_nspoint(NSPoint mouse_pos, bool clear_dxdy) {
     if (!_sapp.mouse.locked) {
-        const NSPoint mouse_pos = event.locationInWindow;
         float new_x = mouse_pos.x * _sapp.dpi_scale;
         float new_y = _sapp.framebuffer_height - (mouse_pos.y * _sapp.dpi_scale) - 1;
-        if (_sapp.mouse.pos_valid) {
+        if (clear_dxdy) {
+            _sapp.mouse.dx = 0.0f;
+            _sapp.mouse.dy = 0.0f;
+        }
+        else if (_sapp.mouse.pos_valid) {
             // don't update dx/dy in the very first update
             _sapp.mouse.dx = new_x - _sapp.mouse.x;
             _sapp.mouse.dy = new_y - _sapp.mouse.y;
@@ -3626,6 +3629,10 @@ _SOKOL_PRIVATE void _sapp_macos_mouse_update(NSEvent* event) {
     }
 }
 
+_SOKOL_PRIVATE void _sapp_macos_mouse_update_from_nsevent(NSEvent* event, bool clear_dxdy) {
+    _sapp_macos_mouse_update_from_nspoint(event.locationInWindow, clear_dxdy);
+}
+
 _SOKOL_PRIVATE void _sapp_macos_show_mouse(bool visible) {
     /* NOTE: this function is only called when the mouse visibility actually changes */
     if (visible) {
@@ -3956,7 +3963,9 @@ _SOKOL_PRIVATE void _sapp_macos_frame(void) {
         }
         if (!drop_failed) {
             if (_sapp_events_enabled()) {
+                _sapp_macos_mouse_update_from_nspoint(sender.draggingLocation, true);
                 _sapp_init_event(SAPP_EVENTTYPE_FILES_DROPPED);
+                _sapp.event.modifiers = _sapp_macos_mods(nil);
                 _sapp_call_event(&_sapp.event);
             }
         }
@@ -4076,7 +4085,7 @@ static void _sapp_gl_make_current(void) {
 
 - (void)mouseEntered:(NSEvent*)event {
     _sapp_gl_make_current();
-    _sapp_macos_mouse_update(event);
+    _sapp_macos_mouse_update_from_nsevent(event, true);
     /* don't send mouse enter/leave while dragging (so that it behaves the same as
        on Windows while SetCapture is active
     */
@@ -4086,38 +4095,38 @@ static void _sapp_gl_make_current(void) {
 }
 - (void)mouseExited:(NSEvent*)event {
     _sapp_gl_make_current();
-    _sapp_macos_mouse_update(event);
+    _sapp_macos_mouse_update_from_nsevent(event, true);
     if (0 == _sapp.macos.mouse_buttons) {
         _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_LEAVE, SAPP_MOUSEBUTTON_INVALID, _sapp_macos_mods(event));
     }
 }
 - (void)mouseDown:(NSEvent*)event {
     _sapp_gl_make_current();
-    _sapp_macos_mouse_update(event);
+    _sapp_macos_mouse_update_from_nsevent(event, false);
     _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, SAPP_MOUSEBUTTON_LEFT, _sapp_macos_mods(event));
     _sapp.macos.mouse_buttons |= (1<<SAPP_MOUSEBUTTON_LEFT);
 }
 - (void)mouseUp:(NSEvent*)event {
     _sapp_gl_make_current();
-    _sapp_macos_mouse_update(event);
+    _sapp_macos_mouse_update_from_nsevent(event, false);
     _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, SAPP_MOUSEBUTTON_LEFT, _sapp_macos_mods(event));
     _sapp.macos.mouse_buttons &= ~(1<<SAPP_MOUSEBUTTON_LEFT);
 }
 - (void)rightMouseDown:(NSEvent*)event {
     _sapp_gl_make_current();
-    _sapp_macos_mouse_update(event);
+    _sapp_macos_mouse_update_from_nsevent(event, false);
     _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, SAPP_MOUSEBUTTON_RIGHT, _sapp_macos_mods(event));
     _sapp.macos.mouse_buttons |= (1<<SAPP_MOUSEBUTTON_RIGHT);
 }
 - (void)rightMouseUp:(NSEvent*)event {
     _sapp_gl_make_current();
-    _sapp_macos_mouse_update(event);
+    _sapp_macos_mouse_update_from_nsevent(event, false);
     _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, SAPP_MOUSEBUTTON_RIGHT, _sapp_macos_mods(event));
     _sapp.macos.mouse_buttons &= ~(1<<SAPP_MOUSEBUTTON_RIGHT);
 }
 - (void)otherMouseDown:(NSEvent*)event {
     _sapp_gl_make_current();
-    _sapp_macos_mouse_update(event);
+    _sapp_macos_mouse_update_from_nsevent(event, false);
     if (2 == event.buttonNumber) {
         _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_DOWN, SAPP_MOUSEBUTTON_MIDDLE, _sapp_macos_mods(event));
         _sapp.macos.mouse_buttons |= (1<<SAPP_MOUSEBUTTON_MIDDLE);
@@ -4125,7 +4134,7 @@ static void _sapp_gl_make_current(void) {
 }
 - (void)otherMouseUp:(NSEvent*)event {
     _sapp_gl_make_current();
-    _sapp_macos_mouse_update(event);
+    _sapp_macos_mouse_update_from_nsevent(event, false);
     if (2 == event.buttonNumber) {
         _sapp_macos_mouse_event(SAPP_EVENTTYPE_MOUSE_UP, SAPP_MOUSEBUTTON_MIDDLE, _sapp_macos_mods(event));
         _sapp.macos.mouse_buttons &= (1<<SAPP_MOUSEBUTTON_MIDDLE);
@@ -4133,7 +4142,7 @@ static void _sapp_gl_make_current(void) {
 }
 - (void)otherMouseDragged:(NSEvent*)event {
     _sapp_gl_make_current();
-    _sapp_macos_mouse_update(event);
+    _sapp_macos_mouse_update_from_nsevent(event, false);
     if (2 == event.buttonNumber) {
         if (_sapp.mouse.locked) {
             _sapp.mouse.dx = [event deltaX];
@@ -4144,7 +4153,7 @@ static void _sapp_gl_make_current(void) {
 }
 - (void)mouseMoved:(NSEvent*)event {
     _sapp_gl_make_current();
-    _sapp_macos_mouse_update(event);
+    _sapp_macos_mouse_update_from_nsevent(event, false);
     if (_sapp.mouse.locked) {
         _sapp.mouse.dx = [event deltaX];
         _sapp.mouse.dy = [event deltaY];
@@ -4153,7 +4162,7 @@ static void _sapp_gl_make_current(void) {
 }
 - (void)mouseDragged:(NSEvent*)event {
     _sapp_gl_make_current();
-    _sapp_macos_mouse_update(event);
+    _sapp_macos_mouse_update_from_nsevent(event, false);
     if (_sapp.mouse.locked) {
         _sapp.mouse.dx = [event deltaX];
         _sapp.mouse.dy = [event deltaY];
@@ -4162,7 +4171,7 @@ static void _sapp_gl_make_current(void) {
 }
 - (void)rightMouseDragged:(NSEvent*)event {
     _sapp_gl_make_current();
-    _sapp_macos_mouse_update(event);
+    _sapp_macos_mouse_update_from_nsevent(event, false);
     if (_sapp.mouse.locked) {
         _sapp.mouse.dx = [event deltaX];
         _sapp.mouse.dy = [event deltaY];
@@ -4171,7 +4180,7 @@ static void _sapp_gl_make_current(void) {
 }
 - (void)scrollWheel:(NSEvent*)event {
     _sapp_gl_make_current();
-    _sapp_macos_mouse_update(event);
+    _sapp_macos_mouse_update_from_nsevent(event, true);
     if (_sapp_events_enabled()) {
         float dx = (float) event.scrollingDeltaX;
         float dy = (float) event.scrollingDeltaY;
@@ -4658,7 +4667,7 @@ EMSCRIPTEN_KEEPALIVE void _sapp_emsc_drop(int i, const char* name) {
     }
 }
 
-EMSCRIPTEN_KEEPALIVE void _sapp_emsc_end_drop(int x, int y) {
+EMSCRIPTEN_KEEPALIVE void _sapp_emsc_end_drop(int x, int y, int mods) {
     if (!_sapp.drop.enabled) {
         return;
     }
@@ -4674,6 +4683,11 @@ EMSCRIPTEN_KEEPALIVE void _sapp_emsc_end_drop(int x, int y) {
         _sapp.mouse.dx = 0.0f;
         _sapp.mouse.dy = 0.0f;
         _sapp_init_event(SAPP_EVENTTYPE_FILES_DROPPED);
+        // see sapp_js_add_dragndrop_listeners for mods constants
+        if (mods & 1) { _sapp.event.modifiers |= SAPP_MODIFIER_SHIFT; }
+        if (mods & 2) { _sapp.event.modifiers |= SAPP_MODIFIER_CTRL; }
+        if (mods & 4) { _sapp.event.modifiers |= SAPP_MODIFIER_ALT; }
+        if (mods & 8) { _sapp.event.modifiers |= SAPP_MODIFIER_SUPER; }
         _sapp_call_event(&_sapp.event);
     }
 }
@@ -4796,8 +4810,13 @@ EM_JS(void, sapp_js_add_dragndrop_listeners, (const char* canvas_name_cstr), {
                 __sapp_emsc_drop(i, cstr);
             });
         }
+        let mods = 0;
+        if (event.shiftKey) { mods |= 1; }
+        if (event.ctrlKey) { mods |= 2; }
+        if (event.altKey) { mods |= 4; }
+        if (event.metaKey) { mods |= 8; }
         // FIXME? see computation of targetX/targetY in emscripten via getClientBoundingRect
-        __sapp_emsc_end_drop(event.clientX, event.clientY);
+        __sapp_emsc_end_drop(event.clientX, event.clientY, mods);
     };
     canvas.addEventListener('dragenter', Module.sokol_dragenter, false);
     canvas.addEventListener('dragleave', Module.sokol_dragleave, false);
@@ -5117,8 +5136,7 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_mouse_cb(int emsc_type, const EmscriptenMouseE
     if (_sapp.mouse.locked) {
         _sapp.mouse.dx = (float) emsc_event->movementX;
         _sapp.mouse.dy = (float) emsc_event->movementY;
-    }
-    else {
+    } else {
         float new_x = emsc_event->targetX * _sapp.dpi_scale;
         float new_y = emsc_event->targetY * _sapp.dpi_scale;
         if (_sapp.mouse.pos_valid) {
@@ -5132,6 +5150,7 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_mouse_cb(int emsc_type, const EmscriptenMouseE
     if (_sapp_events_enabled() && (emsc_event->button >= 0) && (emsc_event->button < SAPP_MAX_MOUSEBUTTONS)) {
         sapp_event_type type;
         bool is_button_event = false;
+        bool clear_dxdy = false;
         switch (emsc_type) {
             case EMSCRIPTEN_EVENT_MOUSEDOWN:
                 type = SAPP_EVENTTYPE_MOUSE_DOWN;
@@ -5146,14 +5165,20 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_mouse_cb(int emsc_type, const EmscriptenMouseE
                 break;
             case EMSCRIPTEN_EVENT_MOUSEENTER:
                 type = SAPP_EVENTTYPE_MOUSE_ENTER;
+                clear_dxdy = true;
                 break;
             case EMSCRIPTEN_EVENT_MOUSELEAVE:
                 type = SAPP_EVENTTYPE_MOUSE_LEAVE;
+                clear_dxdy = true;
                 break;
             default:
                 type = SAPP_EVENTTYPE_INVALID;
                 break;
         }
+        if (clear_dxdy) {
+            _sapp.mouse.dx = 0.0f;
+            _sapp.mouse.dy = 0.0f;
+        }
         if (type != SAPP_EVENTTYPE_INVALID) {
             _sapp_init_event(type);
             _sapp.event.modifiers = _sapp_emsc_mouse_event_mods(emsc_event);
@@ -5164,13 +5189,12 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_mouse_cb(int emsc_type, const EmscriptenMouseE
                     case 2: _sapp.event.mouse_button = SAPP_MOUSEBUTTON_RIGHT; break;
                     default: _sapp.event.mouse_button = (sapp_mousebutton)emsc_event->button; break;
                 }
-            }
-            else {
+            } else {
                 _sapp.event.mouse_button = SAPP_MOUSEBUTTON_INVALID;
             }
             _sapp_call_event(&_sapp.event);
         }
-        /* mouse lock can only be activated in mouse button events (not in move, enter or leave) */
+        // mouse lock can only be activated in mouse button events (not in move, enter or leave)
         if (is_button_event) {
             _sapp_emsc_update_mouse_lock_state();
         }
@@ -7041,6 +7065,7 @@ _SOKOL_PRIVATE void _sapp_win32_files_dropped(HDROP hdrop) {
     if (!drop_failed) {
         if (_sapp_events_enabled()) {
             _sapp_init_event(SAPP_EVENTTYPE_FILES_DROPPED);
+            _sapp.event.modifiers = _sapp_win32_mods();
             _sapp_call_event(&_sapp.event);
         }
     }
@@ -7193,6 +7218,8 @@ _SOKOL_PRIVATE LRESULT CALLBACK _sapp_win32_wndproc(HWND hWnd, UINT uMsg, WPARAM
                         tme.dwFlags = TME_LEAVE;
                         tme.hwndTrack = _sapp.win32.hwnd;
                         TrackMouseEvent(&tme);
+                        _sapp.mouse.dx = 0.0f;
+                        _sapp.mouse.dy = 0.0f;
                         _sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_ENTER, SAPP_MOUSEBUTTON_INVALID);
                     }
                     _sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID);
@@ -7237,6 +7264,8 @@ _SOKOL_PRIVATE LRESULT CALLBACK _sapp_win32_wndproc(HWND hWnd, UINT uMsg, WPARAM
 
             case WM_MOUSELEAVE:
                 if (!_sapp.mouse.locked) {
+                    _sapp.mouse.dx = 0.0f;
+                    _sapp.mouse.dy = 0.0f;
                     _sapp.win32.mouse_tracked = false;
                     _sapp_win32_mouse_event(SAPP_EVENTTYPE_MOUSE_LEAVE, SAPP_MOUSEBUTTON_INVALID);
                 }
@@ -10094,11 +10123,14 @@ _SOKOL_PRIVATE sapp_mousebutton _sapp_x11_translate_button(const XEvent* event)
     }
 }
 
-_SOKOL_PRIVATE void _sapp_x11_mouse_update(int x, int y) {
+_SOKOL_PRIVATE void _sapp_x11_mouse_update(int x, int y, bool clear_dxdy) {
     if (!_sapp.mouse.locked) {
         const float new_x = (float) x;
         const float new_y = (float) y;
-        if (_sapp.mouse.pos_valid) {
+        if (clear_dxdy) {
+            _sapp.mouse.dx = 0.0f;
+            _sapp.mouse.dy = 0.0f;
+        } else if (_sapp.mouse.pos_valid) {
             _sapp.mouse.dx = new_x - _sapp.mouse.x;
             _sapp.mouse.dy = new_y - _sapp.mouse.y;
         }
@@ -10487,7 +10519,7 @@ _SOKOL_PRIVATE void _sapp_x11_process_event(XEvent* event) {
             break;
         case ButtonPress:
             {
-                _sapp_x11_mouse_update(event->xbutton.x, event->xbutton.y);
+                _sapp_x11_mouse_update(event->xbutton.x, event->xbutton.y, false);
                 const sapp_mousebutton btn = _sapp_x11_translate_button(event);
                 uint32_t mods = _sapp_x11_mods(event->xbutton.state);
                 // X11 doesn't set modifier bit on button down, so emulate that
@@ -10509,7 +10541,7 @@ _SOKOL_PRIVATE void _sapp_x11_process_event(XEvent* event) {
             break;
         case ButtonRelease:
             {
-                _sapp_x11_mouse_update(event->xbutton.x, event->xbutton.y);
+                _sapp_x11_mouse_update(event->xbutton.x, event->xbutton.y, false);
                 const sapp_mousebutton btn = _sapp_x11_translate_button(event);
                 if (btn != SAPP_MOUSEBUTTON_INVALID) {
                     uint32_t mods = _sapp_x11_mods(event->xbutton.state);
@@ -10523,19 +10555,19 @@ _SOKOL_PRIVATE void _sapp_x11_process_event(XEvent* event) {
         case EnterNotify:
             /* don't send enter/leave events while mouse button held down */
             if (0 == _sapp.x11.mouse_buttons) {
-                _sapp_x11_mouse_update(event->xcrossing.x, event->xcrossing.y);
+                _sapp_x11_mouse_update(event->xcrossing.x, event->xcrossing.y, true);
                 _sapp_x11_mouse_event(SAPP_EVENTTYPE_MOUSE_ENTER, SAPP_MOUSEBUTTON_INVALID, _sapp_x11_mods(event->xcrossing.state));
             }
             break;
         case LeaveNotify:
             if (0 == _sapp.x11.mouse_buttons) {
-                _sapp_x11_mouse_update(event->xcrossing.x, event->xcrossing.y);
+                _sapp_x11_mouse_update(event->xcrossing.x, event->xcrossing.y, true);
                 _sapp_x11_mouse_event(SAPP_EVENTTYPE_MOUSE_LEAVE, SAPP_MOUSEBUTTON_INVALID, _sapp_x11_mods(event->xcrossing.state));
             }
             break;
         case MotionNotify:
             if (!_sapp.mouse.locked) {
-                _sapp_x11_mouse_update(event->xmotion.x, event->xmotion.y);
+                _sapp_x11_mouse_update(event->xmotion.x, event->xmotion.y, false);
                 _sapp_x11_mouse_event(SAPP_EVENTTYPE_MOUSE_MOVE, SAPP_MOUSEBUTTON_INVALID, _sapp_x11_mods(event->xmotion.state));
             }
             break;
@@ -10666,7 +10698,12 @@ _SOKOL_PRIVATE void _sapp_x11_process_event(XEvent* event) {
                                                                 (unsigned char**) &data);
                 if (_sapp.drop.enabled && result) {
                     if (_sapp_x11_parse_dropped_files_list(data)) {
+                        _sapp.mouse.dx = 0.0f;
+                        _sapp.mouse.dy = 0.0f;
                         if (_sapp_events_enabled()) {
+                            // FIXME: Figure out how to get modifier key state here.
+                            // The XSelection event has no 'state' item, and
+                            // XQueryKeymap() always returns a zeroed array.
                             _sapp_init_event(SAPP_EVENTTYPE_FILES_DROPPED);
                             _sapp_call_event(&_sapp.event);
                         }