From e603771cef3b72547a71709243b492172ac8bb8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 30 Dec 2019 15:42:39 +0100 Subject: [PATCH] Platform: fix EmscriptenApplication mouse events with Emscripten master. The canvasX/Y properties were silently deprecated (I wouldn't know without looking at the source, the docs say no such thing) and when -s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=1 is enabled, these are not initialized to any meaningful value. --- doc/changelog.dox | 3 +++ src/Magnum/Platform/EmscriptenApplication.cpp | 24 ++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/doc/changelog.dox b/doc/changelog.dox index cc1218f96..f0e1fc0d4 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -172,6 +172,9 @@ See also: @ref Platform::EmscriptenApplication::KeyEvent::Key::Semicolon "Key::Semicolon" and @ref Platform::EmscriptenApplication::KeyEvent::Key::Backquote "Key::Backquote" in @ref Platform::EmscriptenApplication +- Fixed broken @ref Platform::EmscriptenApplication mouse event coordinates + when `-s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=1` is enabled (see + [mosra/magnum#408](https://github.com/mosra/magnum/issues/408)) @subsection changelog-latest-deprecated Deprecated APIs diff --git a/src/Magnum/Platform/EmscriptenApplication.cpp b/src/Magnum/Platform/EmscriptenApplication.cpp index 2d6cf886d..0ce0af7c2 100644 --- a/src/Magnum/Platform/EmscriptenApplication.cpp +++ b/src/Magnum/Platform/EmscriptenApplication.cpp @@ -476,10 +476,13 @@ void EmscriptenApplication::setupCallbacks(bool resizable) { emscripten_set_mousemove_callback("#canvas", this, false, ([](int, const EmscriptenMouseEvent* event, void* userData) -> Int { auto& app = *static_cast(userData); - /* Avoid bogus offset at first -- report 0 when the event is called - for the first time */ - Vector2i position{Int(event->canvasX), Int(event->canvasY)}; + /* With DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR, canvasX/Y is + not initialized, so we have to rely on the target being the + canvas. That's always true for mouse events. */ + Vector2i position{Int(event->targetX), Int(event->targetY)}; MouseMoveEvent e{*event, + /* Avoid bogus offset at first -- report 0 when the event is + calledĀ for the first time. */ app._previousMouseMovePosition == Vector2i{-1} ? Vector2i{} : position - app._previousMouseMovePosition}; app._previousMouseMovePosition = position; @@ -735,7 +738,10 @@ EmscriptenApplication::MouseEvent::Button EmscriptenApplication::MouseEvent::but } Vector2i EmscriptenApplication::MouseEvent::position() const { - return {Int(_event.canvasX), Int(_event.canvasY)}; + /* With DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR, canvasX/Y is not + initialized, so we have to rely on the target being the canvas. That's + always true for mouse events. */ + return {Int(_event.targetX), Int(_event.targetY)}; } EmscriptenApplication::MouseEvent::Modifiers EmscriptenApplication::MouseEvent::modifiers() const { @@ -752,7 +758,10 @@ EmscriptenApplication::MouseMoveEvent::Buttons EmscriptenApplication::MouseMoveE } Vector2i EmscriptenApplication::MouseMoveEvent::position() const { - return {Int(_event.canvasX), Int(_event.canvasY)}; + /* With DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR, canvasX/Y is not + initialized, so we have to rely on the target being the canvas. That's + always true for mouse events. */ + return {Int(_event.targetX), Int(_event.targetY)}; } EmscriptenApplication::MouseMoveEvent::Modifiers EmscriptenApplication::MouseMoveEvent::modifiers() const { @@ -778,7 +787,10 @@ Vector2 EmscriptenApplication::MouseScrollEvent::offset() const { } Vector2i EmscriptenApplication::MouseScrollEvent::position() const { - return {Int(_event.mouse.canvasX), Int(_event.mouse.canvasY)}; + /* With DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR, canvasX/Y is not + initialized, so we have to rely on the target being the canvas. That's + always true for mouse events. */ + return {Int(_event.mouse.targetX), Int(_event.mouse.targetY)}; } EmscriptenApplication::InputEvent::Modifiers EmscriptenApplication::MouseScrollEvent::modifiers() const {