Merge pull request #7262 from pandamicro/origin_v3

Indicate correct button and cursor position infomation in mouse move event
This commit is contained in:
minggo 2014-07-08 21:07:09 +08:00
commit c2474ea7c0
2 changed files with 25 additions and 7 deletions

View File

@ -30,7 +30,7 @@ NS_CC_BEGIN
EventMouse::EventMouse(MouseEventType mouseEventCode) EventMouse::EventMouse(MouseEventType mouseEventCode)
: Event(Type::MOUSE) : Event(Type::MOUSE)
, _mouseEventType(mouseEventCode) , _mouseEventType(mouseEventCode)
, _mouseButton(0) , _mouseButton(-1)
, _x(0.0f) , _x(0.0f)
, _y(0.0f) , _y(0.0f)
, _scrollX(0.0f) , _scrollX(0.0f)

View File

@ -565,19 +565,21 @@ void GLView::onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int
} }
} }
//Because OpenGL and cocos2d-x 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;
if(GLFW_PRESS == action) if(GLFW_PRESS == action)
{ {
EventMouse event(EventMouse::MouseEventType::MOUSE_DOWN); EventMouse event(EventMouse::MouseEventType::MOUSE_DOWN);
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here event.setCursorPosition(cursorX, cursorY);
event.setCursorPosition(_mouseX, this->getViewPortRect().size.height - _mouseY);
event.setMouseButton(button); event.setMouseButton(button);
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
} }
else if(GLFW_RELEASE == action) else if(GLFW_RELEASE == action)
{ {
EventMouse event(EventMouse::MouseEventType::MOUSE_UP); EventMouse event(EventMouse::MouseEventType::MOUSE_UP);
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here event.setCursorPosition(cursorX, cursorY);
event.setCursorPosition(_mouseX, this->getViewPortRect().size.height - _mouseY);
event.setMouseButton(button); event.setMouseButton(button);
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
} }
@ -606,9 +608,25 @@ void GLView::onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
this->handleTouchesMove(1, &id, &_mouseX, &_mouseY); this->handleTouchesMove(1, &id, &_mouseX, &_mouseY);
} }
EventMouse event(EventMouse::MouseEventType::MOUSE_MOVE);
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here //Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
event.setCursorPosition(_mouseX, this->getViewPortRect().size.height - _mouseY); float cursorX = (_mouseX - _viewPortRect.origin.x) / _scaleX;
float cursorY = (_viewPortRect.origin.y + _viewPortRect.size.height - _mouseY) / _scaleY;
EventMouse event(EventMouse::MouseEventType::MOUSE_MOVE);
// Set current button
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
{
event.setMouseButton(GLFW_MOUSE_BUTTON_LEFT);
}
else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)
{
event.setMouseButton(GLFW_MOUSE_BUTTON_RIGHT);
}
else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS)
{
event.setMouseButton(GLFW_MOUSE_BUTTON_MIDDLE);
}
event.setCursorPosition(cursorX, cursorY);
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
} }