diff --git a/core/base/EventMouse.cpp b/core/base/EventMouse.cpp index d117360f69d..679e7c17288 100644 --- a/core/base/EventMouse.cpp +++ b/core/base/EventMouse.cpp @@ -34,8 +34,6 @@ EventMouse::EventMouse(MouseEventType mouseEventCode) : Event(Type::MOUSE) , _mouseEventType(mouseEventCode) , _mouseButton(MouseButton::BUTTON_UNSET) - , _x(0.0f) - , _y(0.0f) , _scrollX(0.0f) , _scrollY(0.0f) , _startPointCaptured(false){}; diff --git a/core/base/EventMouse.h b/core/base/EventMouse.h index a0904f086b7..46fd3d97d28 100644 --- a/core/base/EventMouse.h +++ b/core/base/EventMouse.h @@ -1,6 +1,7 @@ /**************************************************************************** Copyright (c) 2013-2016 Chukong Technologies Inc. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md). https://axmol.dev/ @@ -104,13 +105,12 @@ class AX_DLL EventMouse : public Event * @param y The y coordinate of cursor position. * @js setLocation */ - void setCursorPosition(float x, float y) + void setMouseInfo(float x, float y, MouseButton button) { - _x = x; - _y = y; _prevPoint = _point; _point.x = x; _point.y = y; + _mouseButton = button; if (!_startPointCaptured) { _startPoint = _point; @@ -135,13 +135,13 @@ class AX_DLL EventMouse : public Event * @return The x coordinate of cursor position. * @js getLocationX */ - float getCursorX() const { return _x; } + AX_DEPRECATED(2.2) float getCursorX() const { return getLocation().x; } /** Get the cursor position of y axis. * * @return The y coordinate of cursor position. * @js getLocationY */ - float getCursorY() const { return _y; } + AX_DEPRECATED(2.2) float getCursorY() const { return getLocation().y; } /** Returns the current touch location in OpenGL coordinates. * @@ -186,8 +186,6 @@ class AX_DLL EventMouse : public Event private: MouseEventType _mouseEventType; MouseButton _mouseButton; - float _x; - float _y; float _scrollX; float _scrollY; diff --git a/core/platform/GLView.cpp b/core/platform/GLView.cpp index b2fa4411e7e..dc6ddb3e937 100644 --- a/core/platform/GLView.cpp +++ b/core/platform/GLView.cpp @@ -321,8 +321,7 @@ void GLView::handleTouchesBegin(int num, intptr_t ids[], float xs[], float ys[]) } Touch* touch = g_touches[unusedIndex] = new Touch(); - touch->setTouchInfo(unusedIndex, (x - _viewPortRect.origin.x) / _scaleX, - (y - _viewPortRect.origin.y) / _scaleY); + touch->setTouchInfo(unusedIndex, transformInputX(x), transformInputY(y)); AXLOGV("x = {} y = {}", touch->getLocationInView().x, touch->getLocationInView().y); @@ -375,8 +374,7 @@ void GLView::handleTouchesMove(int num, intptr_t ids[], float xs[], float ys[], Touch* touch = g_touches[iter->second]; if (touch) { - touch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX, - (y - _viewPortRect.origin.y) / _scaleY, force, maxForce); + touch->setTouchInfo(iter->second, transformInputX(x), transformInputY(y), force, maxForce); touchEvent._touches.emplace_back(touch); } @@ -428,8 +426,7 @@ void GLView::handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode, if (touch) { AXLOGV("Ending touches with id: {}, x={}, y={}", (int)id, x, y); - touch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX, - (y - _viewPortRect.origin.y) / _scaleY); + touch->setTouchInfo(iter->second, transformInputX(x), transformInputY(y)); touchEvent._touches.emplace_back(touch); diff --git a/core/platform/GLView.h b/core/platform/GLView.h index 9e3958f31e5..79c39fd0559 100644 --- a/core/platform/GLView.h +++ b/core/platform/GLView.h @@ -25,8 +25,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -#ifndef __CCGLVIEW_H__ -#define __CCGLVIEW_H__ +#ifndef __AXGLVIEW_H__ +#define __AXGLVIEW_H__ #include "base/Types.h" #include "base/EventTouch.h" @@ -111,6 +111,7 @@ class Director; class AX_DLL GLView : public Object { friend class Director; + public: /** * @js ctor @@ -424,16 +425,16 @@ class AX_DLL GLView : public Object #if (AX_TARGET_PLATFORM == AX_PLATFORM_MAC) virtual void* getCocoaWindow() = 0; virtual void* getNSGLContext() = 0; // stevetranby: added -#endif /* (AX_TARGET_PLATFORM == AX_PLATFORM_MAC) */ +#endif /* (AX_TARGET_PLATFORM == AX_PLATFORM_MAC) */ #if (AX_TARGET_PLATFORM == AX_PLATFORM_LINUX) - virtual void* getX11Window() = 0; + virtual void* getX11Window() = 0; virtual void* getX11Display() = 0; /* TODO: Implement AX_PLATFORM_LINUX_WAYLAND virtual void* getWaylandWindow() = 0; virtual void* getWaylandDisplay() = 0; */ -#endif // #if (AX_TARGET_PLATFORM == AX_PLATFORM_LINUX) +#endif // #if (AX_TARGET_PLATFORM == AX_PLATFORM_LINUX) /** * Renders a Scene with a Renderer @@ -442,10 +443,13 @@ class AX_DLL GLView : public Object void renderScene(Scene* scene, Renderer* renderer); protected: - /** - * queue a priority operation in render thread for non-PC platforms, even through app in background - * invoked by Director - */ + float transformInputX(float x) { return (x - _viewPortRect.origin.x) / _scaleX; } + float transformInputY(float y) { return (y - _viewPortRect.origin.y) / _scaleY; } + + /** + * queue a priority operation in render thread for non-PC platforms, even through app in background + * invoked by Director + */ virtual void queueOperation(AsyncOperation op, void* param = nullptr); void updateDesignResolutionSize(); @@ -469,6 +473,6 @@ class AX_DLL GLView : public Object // end of platform group /// @} -} +} // namespace ax #endif /* __CCGLVIEW_H__ */ diff --git a/core/platform/GLViewImpl.cpp b/core/platform/GLViewImpl.cpp index 48af309c5d5..823c232525d 100644 --- a/core/platform/GLViewImpl.cpp +++ b/core/platform/GLViewImpl.cpp @@ -341,6 +341,24 @@ static keyCodeItem g_keyCodeStructArray[] = { // implement GLViewImpl ////////////////////////////////////////////////////////////////////////// +static EventMouse::MouseButton checkMouseButton(GLFWwindow* window) +{ + EventMouse::MouseButton mouseButton{EventMouse::MouseButton::BUTTON_UNSET}; + if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) + { + mouseButton = static_cast(GLFW_MOUSE_BUTTON_LEFT); + } + else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS) + { + mouseButton = static_cast(GLFW_MOUSE_BUTTON_RIGHT); + } + else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS) + { + mouseButton = static_cast(GLFW_MOUSE_BUTTON_MIDDLE); + } + return mouseButton; +} + GLViewImpl::GLViewImpl(bool initglfw) : _captured(false) , _isInRetinaMonitor(false) @@ -1079,22 +1097,19 @@ void GLViewImpl::onGLFWMouseCallBack(GLFWwindow* /*window*/, int button, int act } } - // Because OpenGL and axmol uses different Y axis, we need to convert the coordinate here - float cursorX = (_mouseX - _viewPortRect.origin.x) / _scaleX; - float cursorY = (_viewPortRect.origin.y + _viewPortRect.size.height - _mouseY) / _scaleY; + float cursorX = transformInputX(_mouseX); + float cursorY = transformInputY(_mouseY); if (GLFW_PRESS == action) { EventMouse event(EventMouse::MouseEventType::MOUSE_DOWN); - event.setCursorPosition(cursorX, cursorY); - event.setMouseButton(static_cast(button)); + event.setMouseInfo(cursorX, cursorY, static_cast(button)); Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); } else if (GLFW_RELEASE == action) { EventMouse event(EventMouse::MouseEventType::MOUSE_UP); - event.setCursorPosition(cursorX, cursorY); - event.setMouseButton(static_cast(button)); + event.setMouseInfo(cursorX, cursorY, static_cast(button)); Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); } } @@ -1124,25 +1139,13 @@ void GLViewImpl::onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y) } } - // Because OpenGL and axmol uses different Y axis, we need to convert the coordinate here - float cursorX = (_mouseX - _viewPortRect.origin.x) / _scaleX; - float cursorY = (_viewPortRect.origin.y + _viewPortRect.size.height - _mouseY) / _scaleY; + float cursorX = transformInputX(_mouseX); + float cursorY = transformInputY(_mouseY); EventMouse event(EventMouse::MouseEventType::MOUSE_MOVE); // Set current button - if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) - { - event.setMouseButton(static_cast(GLFW_MOUSE_BUTTON_LEFT)); - } - else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS) - { - event.setMouseButton(static_cast(GLFW_MOUSE_BUTTON_RIGHT)); - } - else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS) - { - event.setMouseButton(static_cast(GLFW_MOUSE_BUTTON_MIDDLE)); - } - event.setCursorPosition(cursorX, cursorY); + EventMouse::MouseButton mouseButton{EventMouse::MouseButton::BUTTON_UNSET}; + event.setMouseInfo(cursorX, cursorY, checkMouseButton(window)); Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); } @@ -1194,14 +1197,13 @@ void GLViewImpl::onWebTouchCallback(int eventType, const EmscriptenTouchEvent* t } #endif -void GLViewImpl::onGLFWMouseScrollCallback(GLFWwindow* /*window*/, double x, double y) +void GLViewImpl::onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y) { EventMouse event(EventMouse::MouseEventType::MOUSE_SCROLL); - // Because OpenGL and axmol uses different Y axis, we need to convert the coordinate here - float cursorX = (_mouseX - _viewPortRect.origin.x) / _scaleX; - float cursorY = (_viewPortRect.origin.y + _viewPortRect.size.height - _mouseY) / _scaleY; + float cursorX = transformInputX(_mouseX); + float cursorY = transformInputY(_mouseY); event.setScrollData((float)x, -(float)y); - event.setCursorPosition(cursorX, cursorY); + event.setMouseInfo(cursorX, cursorY, checkMouseButton(window)); Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); } diff --git a/core/platform/winrt/GLViewImpl-winrt.cpp b/core/platform/winrt/GLViewImpl-winrt.cpp index 1c8e0d7c4fb..0a225be24b9 100644 --- a/core/platform/winrt/GLViewImpl-winrt.cpp +++ b/core/platform/winrt/GLViewImpl-winrt.cpp @@ -46,6 +46,23 @@ namespace ax static GLViewImpl* s_pEglView = nullptr; +static EventMouse::MouseButton checkMouseButton(Windows::UI::Core::PointerEventArgs const& args) +{ + if (args.CurrentPoint().Properties().IsLeftButtonPressed()) + { + return EventMouse::MouseButton::BUTTON_LEFT; + } + else if (args.CurrentPoint().Properties().IsRightButtonPressed()) + { + return EventMouse::MouseButton::BUTTON_RIGHT; + } + else if (args.CurrentPoint().Properties().IsMiddleButtonPressed()) + { + return EventMouse::MouseButton::BUTTON_MIDDLE; + } + return EventMouse::MouseButton::BUTTON_UNSET; +} + GLViewImpl* GLViewImpl::create(std::string_view viewName) { auto ret = new GLViewImpl; @@ -324,22 +341,22 @@ void GLViewImpl::OnPointerReleased(Windows::UI::Core::PointerEventArgs const& ar void ax::GLViewImpl::OnMousePressed(Windows::UI::Core::PointerEventArgs const& args) { - Vec2 mousePosition = GetPointMouse(args); + Vec2 pt = GetPoint(args); // Emulated touch, if left mouse button if (args.CurrentPoint().Properties().IsLeftButtonPressed()) { intptr_t id = 0; - Vec2 pt = GetPoint(args); handleTouchesBegin(1, &id, &pt.x, &pt.y); } + float x = transformInputX(pt.x); + float y = transformInputY(pt.y); if (_lastMouseButtonPressed != EventMouse::MouseButton::BUTTON_UNSET) { EventMouse event(EventMouse::MouseEventType::MOUSE_UP); - event.setMouseButton(_lastMouseButtonPressed); - event.setCursorPosition(mousePosition.x, mousePosition.y); + event.setMouseInfo(x, y, _lastMouseButtonPressed); Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); } @@ -357,57 +374,41 @@ void ax::GLViewImpl::OnMousePressed(Windows::UI::Core::PointerEventArgs const& a { _lastMouseButtonPressed = EventMouse::MouseButton::BUTTON_MIDDLE; } - event.setMouseButton(_lastMouseButtonPressed); - event.setCursorPosition(mousePosition.x, mousePosition.y); + event.setMouseInfo(x, y, _lastMouseButtonPressed); Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); } void ax::GLViewImpl::OnMouseMoved(Windows::UI::Core::PointerEventArgs const& args) { - Vec2 mousePosition = GetPointMouse(args); + Vec2 pt = GetPoint(args); // Emulated touch, if left mouse button if (args.CurrentPoint().Properties().IsLeftButtonPressed()) { intptr_t id = 0; - Vec2 pt = GetPoint(args); handleTouchesMove(1, &id, &pt.x, &pt.y); } EventMouse event(EventMouse::MouseEventType::MOUSE_MOVE); - // Set current button - if (args.CurrentPoint().Properties().IsLeftButtonPressed()) - { - event.setMouseButton(EventMouse::MouseButton::BUTTON_LEFT); - } - else if (args.CurrentPoint().Properties().IsRightButtonPressed()) - { - event.setMouseButton(EventMouse::MouseButton::BUTTON_RIGHT); - } - else if (args.CurrentPoint().Properties().IsMiddleButtonPressed()) - { - event.setMouseButton(EventMouse::MouseButton::BUTTON_MIDDLE); - } - event.setCursorPosition(mousePosition.x, mousePosition.y); + + event.setMouseInfo(transformInputX(pt.x), transformInputY(pt.y), checkMouseButton(args)); Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); } void ax::GLViewImpl::OnMouseReleased(Windows::UI::Core::PointerEventArgs const& args) { - Vec2 mousePosition = GetPointMouse(args); + Vec2 pt = GetPoint(args); // Emulated touch, if left mouse button if (_lastMouseButtonPressed == EventMouse::MouseButton::BUTTON_LEFT) { intptr_t id = 0; - Vec2 pt = GetPoint(args); handleTouchesEnd(1, &id, &pt.x, &pt.y); } EventMouse event(EventMouse::MouseEventType::MOUSE_UP); - event.setMouseButton(_lastMouseButtonPressed); - event.setCursorPosition(mousePosition.x, mousePosition.y); + event.setMouseInfo(transformInputX(pt.x), transformInputY(pt.y), _lastMouseButtonPressed); Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); _lastMouseButtonPressed = EventMouse::MouseButton::BUTTON_UNSET; @@ -415,11 +416,9 @@ void ax::GLViewImpl::OnMouseReleased(Windows::UI::Core::PointerEventArgs const& void ax::GLViewImpl::OnMouseWheelChanged(Windows::UI::Core::PointerEventArgs const& args) { - Vec2 mousePosition = GetPointMouse(args); + Vec2 pt = GetPoint(args); EventMouse event(EventMouse::MouseEventType::MOUSE_SCROLL); // Because OpenGL and axmol uses different Y axis, we need to convert the coordinate here - float cursorX = (mousePosition.x - _viewPortRect.origin.x) / _scaleX; - float cursorY = (_viewPortRect.origin.y + _viewPortRect.size.height - mousePosition.y) / _scaleY; float delta = static_cast(args.CurrentPoint().Properties().MouseWheelDelta()); if (args.CurrentPoint().Properties().IsHorizontalMouseWheel()) { @@ -429,7 +428,7 @@ void ax::GLViewImpl::OnMouseWheelChanged(Windows::UI::Core::PointerEventArgs con { event.setScrollData(0.0f, -delta / WHEEL_DELTA); } - event.setCursorPosition(cursorX, cursorY); + event.setMouseInfo(transformInputX(pt.x), transformInputY(pt.y), checkMouseButton(args)); Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); } @@ -547,22 +546,9 @@ ax::Vec2 GLViewImpl::TransformToOrientation(Windows::Foundation::Point const& p) Vec2 GLViewImpl::GetPoint(Windows::UI::Core::PointerEventArgs const& args) { - return TransformToOrientation(args.CurrentPoint().Position()); } -Vec2 GLViewImpl::GetPointMouse(Windows::UI::Core::PointerEventArgs const& args) -{ - - Vec2 position = TransformToOrientation(args.CurrentPoint().Position()); - - // Because Windows and axmol uses different Y axis, we need to convert the coordinate here - position.x = (position.x - _viewPortRect.origin.x) / _scaleX; - position.y = (_viewPortRect.origin.y + _viewPortRect.size.height - position.y) / _scaleY; - - return position; -} - void GLViewImpl::QueueBackKeyPress() { std::shared_ptr e(new BackButtonEvent()); diff --git a/core/platform/winrt/GLViewImpl-winrt.h b/core/platform/winrt/GLViewImpl-winrt.h index ae729572562..f2c0bfe03e0 100644 --- a/core/platform/winrt/GLViewImpl-winrt.h +++ b/core/platform/winrt/GLViewImpl-winrt.h @@ -168,7 +168,6 @@ class AX_DLL GLViewImpl : public GLView ax::Vec2 TransformToOrientation(Windows::Foundation::Point const& point); ax::Vec2 GetPoint(Windows::UI::Core::PointerEventArgs const& args); - ax::Vec2 GetPointMouse(Windows::UI::Core::PointerEventArgs const& args); Windows::Foundation::Rect m_windowBounds; winrt::event_token m_eventToken;