diff --git a/cocos2dx/event_dispatcher/CCEventMouse.h b/cocos2dx/event_dispatcher/CCEventMouse.h index b44d6d4aca..1a9ea971e0 100644 --- a/cocos2dx/event_dispatcher/CCEventMouse.h +++ b/cocos2dx/event_dispatcher/CCEventMouse.h @@ -28,6 +28,15 @@ #include "CCEvent.h" +#define MOUSE_BUTTON_LEFT 0 +#define MOUSE_BUTTON_RIGHT 1 +#define MOUSE_BUTTON_MIDDLE 2 +#define MOUSE_BUTTON_4 3 +#define MOUSE_BUTTON_5 4 +#define MOUSE_BUTTON_6 5 +#define MOUSE_BUTTON_7 6 +#define MOUSE_BUTTON_8 7 + NS_CC_BEGIN class EventMouse : public Event @@ -50,6 +59,7 @@ public: EventMouse(MouseEventType mouseEventCode) : Event(EVENT_TYPE) , _mouseEventType(mouseEventCode) + , _mouseButton(0) , _x(0.0f) , _y(0.0f) , _scrollX(0.0f) @@ -62,6 +72,8 @@ public: inline float getScrollY() { return _scrollY; }; inline void setCursorPosition(float x, float y) { _x = x; _y = y; }; + inline void setMouseButton(int button) { _mouseButton = button; }; + inline int getMouseButton() { return _mouseButton; }; inline float getCursorX() { return _x; }; inline float getCursorY() { return _y; }; diff --git a/cocos2dx/platform/mac/CCEGLView.mm b/cocos2dx/platform/mac/CCEGLView.mm index 83d020cef1..b431a18c41 100644 --- a/cocos2dx/platform/mac/CCEGLView.mm +++ b/cocos2dx/platform/mac/CCEGLView.mm @@ -205,10 +205,6 @@ void EGLViewEventHandler::OnGLFWMouseCallBack(GLFWwindow* window, int button, in { int id = 0; eglView->handleTouchesBegin(1, &id, &s_mouseX, &s_mouseY); - - EventMouse event(EventMouse::MouseEventType::MOUSE_DOWN); - event.setCursorPosition(s_mouseX, s_mouseY); - EventDispatcher::getInstance()->dispatchEvent(&event); } } else if(GLFW_RELEASE == action) @@ -218,22 +214,33 @@ void EGLViewEventHandler::OnGLFWMouseCallBack(GLFWwindow* window, int button, in { int id = 0; eglView->handleTouchesEnd(1, &id, &s_mouseX, &s_mouseY); - - EventMouse event(EventMouse::MouseEventType::MOUSE_UP); - event.setCursorPosition(s_mouseX, s_mouseY); - EventDispatcher::getInstance()->dispatchEvent(&event); } } } + + if(GLFW_PRESS == action) + { + EventMouse event(EventMouse::MouseEventType::MOUSE_DOWN); + event.setCursorPosition(s_mouseX, eglView->getViewPortRect().size.height - s_mouseY); + event.setMouseButton(button); + EventDispatcher::getInstance()->dispatchEvent(&event); + } + else if(GLFW_RELEASE == action) + { + EventMouse event(EventMouse::MouseEventType::MOUSE_UP); + event.setCursorPosition(s_mouseX, eglView->getViewPortRect().size.height - s_mouseY); + event.setMouseButton(button); + EventDispatcher::getInstance()->dispatchEvent(&event); + } } void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y) { - s_mouseX = (float)x; - s_mouseY = (float)y; EGLView* eglView = EGLView::getInstance(); if(nullptr == eglView) return; - + s_mouseX = (float)x; + s_mouseY = (float)y; + s_mouseX /= eglView->getFrameZoomFactor(); s_mouseY /= eglView->getFrameZoomFactor(); @@ -247,15 +254,18 @@ void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x, } EventMouse event(EventMouse::MouseEventType::MOUSE_MOVE); - event.setCursorPosition(s_mouseX, s_mouseY); + event.setCursorPosition(s_mouseX, eglView->getViewPortRect().size.height - s_mouseY); EventDispatcher::getInstance()->dispatchEvent(&event); } void EGLViewEventHandler::OnGLFWMouseScrollCallback(GLFWwindow* window, double x, double y) { + EGLView* eglView = EGLView::getInstance(); + if(nullptr == eglView) return; + EventMouse event(EventMouse::MouseEventType::MOUSE_SCROLL); event.setScrollData((float)x, (float)y); - event.setCursorPosition(s_mouseX, s_mouseY); + event.setCursorPosition(s_mouseX, eglView->getViewPortRect().size.height - s_mouseY); EventDispatcher::getInstance()->dispatchEvent(&event); } diff --git a/samples/Cpp/TestCpp/Classes/InputTest/MouseTest.cpp b/samples/Cpp/TestCpp/Classes/InputTest/MouseTest.cpp new file mode 100644 index 0000000000..dd3dfaf1b1 --- /dev/null +++ b/samples/Cpp/TestCpp/Classes/InputTest/MouseTest.cpp @@ -0,0 +1,84 @@ +#include "MouseTest.h" + +MouseTest::MouseTest() +{ + auto s = Director::getInstance()->getWinSize(); + auto title = LabelTTF::create("Mouse Test", "Arial", 28); + addChild(title, 0); + title->setPosition( Point(s.width/2, s.height-50) ); + + //Create a label to display the mouse action + _labelAction = LabelTTF::create("Click mouse button and see this change", "Arial", 22); + _labelAction->setPosition(Point(s.width/2, s.height*2/3)); + addChild(_labelAction, 0); + + //Create a label to display the mouse position + _labelPosition = LabelTTF::create("Mouse not supported on this device", "Arial", 22); + _labelPosition->setPosition(Point(s.width/2, s.height/3)); + addChild(_labelPosition); + + + _mouseListener = EventListenerMouse::create(); + _mouseListener->onMouseMove = CC_CALLBACK_1(MouseTest::onMouseMove, this); + _mouseListener->onMouseUp = CC_CALLBACK_1(MouseTest::onMouseUp, this); + _mouseListener->onMouseDown = CC_CALLBACK_1(MouseTest::onMouseDown, this); + _mouseListener->onMouseScroll = CC_CALLBACK_1(MouseTest::onMouseScroll, this); + + EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(_mouseListener, this); + + _labelAction->retain(); + _labelPosition->retain(); + + this->setTouchEnabled(true); +} + +MouseTest::~MouseTest() +{ + EventDispatcher::getInstance()->removeEventListener(_mouseListener); + + _labelAction->release(); + _labelPosition->release(); +} + +template string tostr(const T& t) { ostringstream os; os<getMouseButton()); + _labelAction->setString(str.c_str()); +} + +void MouseTest::onMouseUp(Event *event) +{ + EventMouse* e = (EventMouse*)event; + string str = "Mouse Up detected, Key: "; + str += tostr(e->getMouseButton()); + _labelAction->setString(str.c_str()); +} + +void MouseTest::onMouseMove(Event *event) +{ + EventMouse* e = (EventMouse*)event; + string str = "MousePosition X:"; + str = str + tostr(e->getCursorX()) + " Y:" + tostr(e->getCursorY()); + _labelPosition->setString(str.c_str()); +} + +void MouseTest::onMouseScroll(Event *event) +{ + EventMouse* e = (EventMouse*)event; + string str = "Mouse Scroll detected, X: "; + str = str + tostr(e->getScrollX()) + " Y: " + tostr(e->getScrollY()); + _labelAction->setString(str.c_str()); +} + +void MouseTestScene::runThisTest() +{ + auto layer = new MouseTest(); + addChild(layer); + + Director::getInstance()->replaceScene(this); + layer->release(); +} \ No newline at end of file diff --git a/samples/Cpp/TestCpp/Classes/InputTest/MouseTest.h b/samples/Cpp/TestCpp/Classes/InputTest/MouseTest.h new file mode 100644 index 0000000000..8be1bd270d --- /dev/null +++ b/samples/Cpp/TestCpp/Classes/InputTest/MouseTest.h @@ -0,0 +1,30 @@ +#ifndef __MOUSE_TEST_H_ +#define __MOUSE_TEST_H_ + +#include "cocos2d.h" +#include "../testBasic.h" + +class MouseTest : public Layer +{ +public: + MouseTest(); + ~MouseTest(); + + void onMouseDown(Event* event); + void onMouseUp(Event* event); + void onMouseMove(Event* event); + void onMouseScroll(Event* event); + +private: + LabelTTF* _labelAction; + LabelTTF* _labelPosition; + EventListenerMouse* _mouseListener; +}; + +class MouseTestScene : public TestScene +{ +public: + virtual void runThisTest(); +}; + +#endif diff --git a/samples/Cpp/TestCpp/Classes/controller.cpp b/samples/Cpp/TestCpp/Classes/controller.cpp index 2f4059469a..587758fe02 100644 --- a/samples/Cpp/TestCpp/Classes/controller.cpp +++ b/samples/Cpp/TestCpp/Classes/controller.cpp @@ -50,6 +50,7 @@ struct { { "FontTest", []() { return new FontTestScene(); } }, { "IntervalTest", [](){return new IntervalTestScene(); } }, { "KeyboardTest", []() { return new KeyboardTestScene(); } }, + { "MouseTest", []() { return new MouseTestScene(); } }, #if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA) { "KeypadTest", []() { return new KeypadTestScene(); } }, #endif diff --git a/samples/Cpp/TestCpp/Classes/tests.h b/samples/Cpp/TestCpp/Classes/tests.h index b119f69de1..d613fe542e 100644 --- a/samples/Cpp/TestCpp/Classes/tests.h +++ b/samples/Cpp/TestCpp/Classes/tests.h @@ -32,6 +32,7 @@ #include "AccelerometerTest/AccelerometerTest.h" #include "KeypadTest/KeypadTest.h" #include "KeyboardTest/KeyboardTest.h" +#include "InputTest/MouseTest.h" #include "PerformanceTest/PerformanceTest.h" #include "ZwoptexTest/ZwoptexTest.h" #include "CocosDenshionTest/CocosDenshionTest.h" diff --git a/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id b/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id index efbf8806ae..06123eec12 100644 --- a/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/samples/samples.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -b759f56a07f242b20e9c96f2a6cd49c4b5c9dc9b \ No newline at end of file +3275ac270645139eb273b2a47c215f39bcb0e0d5 \ No newline at end of file