Implement mouse callback for windows

This commit is contained in:
Nite Luo 2013-10-11 16:33:49 -07:00
parent 09c0e23b5d
commit ddf21e8bd1
6 changed files with 64 additions and 2 deletions

View File

@ -31,7 +31,7 @@ THE SOFTWARE.
#include "event_dispatcher/CCTouch.h" #include "event_dispatcher/CCTouch.h"
#include "event_dispatcher/CCEventDispatcher.h" #include "event_dispatcher/CCEventDispatcher.h"
#include "event_dispatcher/CCEventKeyboard.h" #include "event_dispatcher/CCEventKeyboard.h"
#include "event_dispatcher/CCEventMouse.h"
NS_CC_BEGIN NS_CC_BEGIN
@ -277,6 +277,7 @@ public:
static void OnGLFWError(int errorID, const char* errorDesc); static void OnGLFWError(int errorID, const char* errorDesc);
static void OnGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify); static void OnGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify);
static void OnGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y); static void OnGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y);
static void OnGLFWMouseScrollCallback(GLFWwindow* window, double x, double y);
static void OnGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); static void OnGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
static void OnGLFWCharCallback(GLFWwindow* window, unsigned int character); static void OnGLFWCharCallback(GLFWwindow* window, unsigned int character);
static void OnGLFWWindowPosCallback(GLFWwindow* windows, int x, int y); static void OnGLFWWindowPosCallback(GLFWwindow* windows, int x, int y);
@ -316,6 +317,21 @@ void EGLViewEventHandler::OnGLFWMouseCallBack(GLFWwindow* window, int button, in
} }
} }
} }
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) void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
@ -336,6 +352,23 @@ void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x,
eglView->handleTouchesMove(1, &id, &s_mouseX, &s_mouseY); eglView->handleTouchesMove(1, &id, &s_mouseX, &s_mouseY);
} }
} }
EventMouse event(EventMouse::MouseEventType::MOUSE_MOVE);
//Because OpenGL use upper left as origin point, we need to revert the mouse y coordinate here
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);
//Because OpenGL use upper left as origin point, we need to revert the mouse y coordinate here
event.setScrollData((float)x, -(float)y);
event.setCursorPosition(s_mouseX, eglView->getViewPortRect().size.height - s_mouseY);
EventDispatcher::getInstance()->dispatchEvent(&event);
} }
void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
@ -405,8 +438,10 @@ bool EGLView::init(const char* viewName, float width, float height, float frameZ
glfwGetFramebufferSize(_mainWindow, &_frameBufferSize[0], &_frameBufferSize[1]); glfwGetFramebufferSize(_mainWindow, &_frameBufferSize[0], &_frameBufferSize[1]);
glfwSetMouseButtonCallback(_mainWindow,EGLViewEventHandler::OnGLFWMouseCallBack); glfwSetMouseButtonCallback(_mainWindow,EGLViewEventHandler::OnGLFWMouseCallBack);
glfwSetCursorPosCallback(_mainWindow,EGLViewEventHandler::OnGLFWMouseMoveCallBack); glfwSetCursorPosCallback(_mainWindow,EGLViewEventHandler::OnGLFWMouseMoveCallBack);
glfwSetScrollCallback(_mainWindow, EGLViewEventHandler::OnGLFWMouseScrollCallback);
glfwSetCharCallback(_mainWindow, EGLViewEventHandler::OnGLFWCharCallback); glfwSetCharCallback(_mainWindow, EGLViewEventHandler::OnGLFWCharCallback);
glfwSetKeyCallback(_mainWindow, EGLViewEventHandler::OnGLFWKeyCallback); glfwSetKeyCallback(_mainWindow, EGLViewEventHandler::OnGLFWKeyCallback);
glfwSetWindowPosCallback(_mainWindow, EGLViewEventHandler::OnGLFWWindowPosCallback);
// check OpenGL version at first // check OpenGL version at first
const GLubyte* glVersion = glGetString(GL_VERSION); const GLubyte* glVersion = glGetString(GL_VERSION);

View File

@ -182,7 +182,9 @@ xcopy /Y /Q "$(ProjectDir)..\platform\third_party\win32\libraries\*.*" "$(OutDir
<ClCompile Include="..\event_dispatcher\CCEventListenerAcceleration.cpp" /> <ClCompile Include="..\event_dispatcher\CCEventListenerAcceleration.cpp" />
<ClCompile Include="..\event_dispatcher\CCEventListenerCustom.cpp" /> <ClCompile Include="..\event_dispatcher\CCEventListenerCustom.cpp" />
<ClCompile Include="..\event_dispatcher\CCEventListenerKeyboard.cpp" /> <ClCompile Include="..\event_dispatcher\CCEventListenerKeyboard.cpp" />
<ClCompile Include="..\event_dispatcher\CCEventListenerMouse.cpp" />
<ClCompile Include="..\event_dispatcher\CCEventListenerTouch.cpp" /> <ClCompile Include="..\event_dispatcher\CCEventListenerTouch.cpp" />
<ClCompile Include="..\event_dispatcher\CCEventMouse.cpp" />
<ClCompile Include="..\event_dispatcher\CCEventTouch.cpp" /> <ClCompile Include="..\event_dispatcher\CCEventTouch.cpp" />
<ClCompile Include="..\event_dispatcher\CCTouch.cpp" /> <ClCompile Include="..\event_dispatcher\CCTouch.cpp" />
<ClCompile Include="..\label_nodes\CCFont.cpp" /> <ClCompile Include="..\label_nodes\CCFont.cpp" />
@ -344,7 +346,9 @@ xcopy /Y /Q "$(ProjectDir)..\platform\third_party\win32\libraries\*.*" "$(OutDir
<ClInclude Include="..\event_dispatcher\CCEventListenerAcceleration.h" /> <ClInclude Include="..\event_dispatcher\CCEventListenerAcceleration.h" />
<ClInclude Include="..\event_dispatcher\CCEventListenerCustom.h" /> <ClInclude Include="..\event_dispatcher\CCEventListenerCustom.h" />
<ClInclude Include="..\event_dispatcher\CCEventListenerKeyboard.h" /> <ClInclude Include="..\event_dispatcher\CCEventListenerKeyboard.h" />
<ClInclude Include="..\event_dispatcher\CCEventListenerMouse.h" />
<ClInclude Include="..\event_dispatcher\CCEventListenerTouch.h" /> <ClInclude Include="..\event_dispatcher\CCEventListenerTouch.h" />
<ClInclude Include="..\event_dispatcher\CCEventMouse.h" />
<ClInclude Include="..\event_dispatcher\CCEventTouch.h" /> <ClInclude Include="..\event_dispatcher\CCEventTouch.h" />
<ClInclude Include="..\event_dispatcher\CCTouch.h" /> <ClInclude Include="..\event_dispatcher\CCTouch.h" />
<ClInclude Include="..\include\ccConfig.h" /> <ClInclude Include="..\include\ccConfig.h" />

View File

@ -572,6 +572,12 @@
<ClCompile Include="..\event_dispatcher\CCEventTouch.cpp"> <ClCompile Include="..\event_dispatcher\CCEventTouch.cpp">
<Filter>event_dispatcher</Filter> <Filter>event_dispatcher</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\event_dispatcher\CCEventListenerMouse.cpp">
<Filter>event_dispatcher</Filter>
</ClCompile>
<ClCompile Include="..\event_dispatcher\CCEventMouse.cpp">
<Filter>event_dispatcher</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\base_nodes\CCAtlasNode.h"> <ClInclude Include="..\base_nodes\CCAtlasNode.h">
@ -1155,5 +1161,11 @@
<ClInclude Include="..\event_dispatcher\CCEventTouch.h"> <ClInclude Include="..\event_dispatcher\CCEventTouch.h">
<Filter>event_dispatcher</Filter> <Filter>event_dispatcher</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\event_dispatcher\CCEventListenerMouse.h">
<Filter>event_dispatcher</Filter>
</ClInclude>
<ClInclude Include="..\event_dispatcher\CCEventMouse.h">
<Filter>event_dispatcher</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -204,7 +204,7 @@ void TestController::onTouchMoved(Touch* touch, Event *event)
void TestController::onMouseScroll(Event *event) void TestController::onMouseScroll(Event *event)
{ {
auto mouseEvent = static_cast<EventMouse*>(event); auto mouseEvent = static_cast<EventMouse*>(event);
float nMoveY = -mouseEvent->getScrollY(); float nMoveY = mouseEvent->getScrollY() * 6;
auto curPos = _itemMenu->getPosition(); auto curPos = _itemMenu->getPosition();
auto nextPos = Point(curPos.x, curPos.y + nMoveY); auto nextPos = Point(curPos.x, curPos.y + nMoveY);

View File

@ -177,6 +177,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\..\..\external\libwebsockets\win32\lib\*.*" "$(O
<ClCompile Include="..\Classes\ExtensionsTest\TableViewTest\CustomTableViewCell.cpp" /> <ClCompile Include="..\Classes\ExtensionsTest\TableViewTest\CustomTableViewCell.cpp" />
<ClCompile Include="..\Classes\ExtensionsTest\TableViewTest\TableViewTestScene.cpp" /> <ClCompile Include="..\Classes\ExtensionsTest\TableViewTest\TableViewTestScene.cpp" />
<ClCompile Include="..\Classes\FileUtilsTest\FileUtilsTest.cpp" /> <ClCompile Include="..\Classes\FileUtilsTest\FileUtilsTest.cpp" />
<ClCompile Include="..\Classes\InputTest\MouseTest.cpp" />
<ClCompile Include="..\Classes\KeyboardTest\KeyboardTest.cpp" /> <ClCompile Include="..\Classes\KeyboardTest\KeyboardTest.cpp" />
<ClCompile Include="..\Classes\LabelTest\LabelTestNew.cpp" /> <ClCompile Include="..\Classes\LabelTest\LabelTestNew.cpp" />
<ClCompile Include="..\Classes\NewEventDispatcherTest\NewEventDispatcherTest.cpp" /> <ClCompile Include="..\Classes\NewEventDispatcherTest\NewEventDispatcherTest.cpp" />
@ -313,6 +314,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\..\..\external\libwebsockets\win32\lib\*.*" "$(O
<ClInclude Include="..\Classes\ExtensionsTest\TableViewTest\CustomTableViewCell.h" /> <ClInclude Include="..\Classes\ExtensionsTest\TableViewTest\CustomTableViewCell.h" />
<ClInclude Include="..\Classes\ExtensionsTest\TableViewTest\TableViewTestScene.h" /> <ClInclude Include="..\Classes\ExtensionsTest\TableViewTest\TableViewTestScene.h" />
<ClInclude Include="..\Classes\FileUtilsTest\FileUtilsTest.h" /> <ClInclude Include="..\Classes\FileUtilsTest\FileUtilsTest.h" />
<ClInclude Include="..\Classes\InputTest\MouseTest.h" />
<ClInclude Include="..\Classes\KeyboardTest\KeyboardTest.h" /> <ClInclude Include="..\Classes\KeyboardTest\KeyboardTest.h" />
<ClInclude Include="..\Classes\LabelTest\LabelTestNew.h" /> <ClInclude Include="..\Classes\LabelTest\LabelTestNew.h" />
<ClInclude Include="..\Classes\NewEventDispatcherTest\NewEventDispatcherTest.h" /> <ClInclude Include="..\Classes\NewEventDispatcherTest\NewEventDispatcherTest.h" />

View File

@ -301,6 +301,9 @@
<Filter Include="Classes\KeyboardTest"> <Filter Include="Classes\KeyboardTest">
<UniqueIdentifier>{8d7d37cd-5cc2-4a7d-9bd2-7b5c928adbb5}</UniqueIdentifier> <UniqueIdentifier>{8d7d37cd-5cc2-4a7d-9bd2-7b5c928adbb5}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="Classes\InputTest">
<UniqueIdentifier>{8e8124bd-adb2-4a8f-8727-9868a9c42d80}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="main.cpp"> <ClCompile Include="main.cpp">
@ -697,6 +700,9 @@
<ClCompile Include="..\Classes\KeyboardTest\KeyboardTest.cpp"> <ClCompile Include="..\Classes\KeyboardTest\KeyboardTest.cpp">
<Filter>Classes\KeyboardTest</Filter> <Filter>Classes\KeyboardTest</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\Classes\InputTest\MouseTest.cpp">
<Filter>Classes\InputTest</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="main.h"> <ClInclude Include="main.h">
@ -1270,5 +1276,8 @@
<ClInclude Include="..\Classes\KeyboardTest\KeyboardTest.h"> <ClInclude Include="..\Classes\KeyboardTest\KeyboardTest.h">
<Filter>Classes\KeyboardTest</Filter> <Filter>Classes\KeyboardTest</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\Classes\InputTest\MouseTest.h">
<Filter>Classes\InputTest</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>