Skip to content

Commit

Permalink
Fix EventMouse coords (axmolengine#2141)
Browse files Browse the repository at this point in the history
  • Loading branch information
halx99 authored and xfbird committed Sep 18, 2024
1 parent 4fcc127 commit a226711
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 203 deletions.
2 changes: 0 additions & 2 deletions core/base/EventMouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,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){};
Expand Down
12 changes: 5 additions & 7 deletions core/base/EventMouse.h
Original file line number Diff line number Diff line change
@@ -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/
Expand Down Expand Up @@ -103,13 +104,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;
Expand All @@ -134,13 +134,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.
*
Expand Down Expand Up @@ -185,8 +185,6 @@ class AX_DLL EventMouse : public Event
private:
MouseEventType _mouseEventType;
MouseButton _mouseButton;
float _x;
float _y;
float _scrollX;
float _scrollY;

Expand Down
9 changes: 3 additions & 6 deletions core/platform/GLView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,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);

Expand Down Expand Up @@ -374,8 +373,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);
}
Expand Down Expand Up @@ -427,8 +425,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);

Expand Down
24 changes: 14 additions & 10 deletions core/platform/GLView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -110,6 +110,7 @@ class Director;
class AX_DLL GLView : public Object
{
friend class Director;

public:
/**
* @js ctor
Expand Down Expand Up @@ -423,16 +424,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
Expand All @@ -441,10 +442,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();
Expand All @@ -467,7 +471,7 @@ class AX_DLL GLView : public Object

// end of platform group
/// @}

NS_AX_END
// } // namespace ax

#endif /* __CCGLVIEW_H__ */
58 changes: 30 additions & 28 deletions core/platform/GLViewImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,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<ax::EventMouse::MouseButton>(GLFW_MOUSE_BUTTON_LEFT);
}
else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)
{
mouseButton = static_cast<ax::EventMouse::MouseButton>(GLFW_MOUSE_BUTTON_RIGHT);
}
else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS)
{
mouseButton = static_cast<ax::EventMouse::MouseButton>(GLFW_MOUSE_BUTTON_MIDDLE);
}
return mouseButton;
}

GLViewImpl::GLViewImpl(bool initglfw)
: _captured(false)
, _isInRetinaMonitor(false)
Expand Down Expand Up @@ -1078,22 +1096,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<ax::EventMouse::MouseButton>(button));
event.setMouseInfo(cursorX, cursorY, static_cast<ax::EventMouse::MouseButton>(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<ax::EventMouse::MouseButton>(button));
event.setMouseInfo(cursorX, cursorY, static_cast<ax::EventMouse::MouseButton>(button));
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
}
}
Expand Down Expand Up @@ -1123,25 +1138,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<ax::EventMouse::MouseButton>(GLFW_MOUSE_BUTTON_LEFT));
}
else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)
{
event.setMouseButton(static_cast<ax::EventMouse::MouseButton>(GLFW_MOUSE_BUTTON_RIGHT));
}
else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS)
{
event.setMouseButton(static_cast<ax::EventMouse::MouseButton>(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);
}

Expand Down Expand Up @@ -1193,14 +1196,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);
}

Expand Down
Loading

0 comments on commit a226711

Please sign in to comment.