mirror of https://github.com/axmolengine/axmol.git
Merge pull request #4035 from darkdukey/mousewheel_mac
issue #3099: Adding MouseEvent and MouseListener, Adding Mouse Support For Desktop Platform.
This commit is contained in:
commit
c754e1b64f
|
@ -70,6 +70,9 @@ xcschememanagement.plist
|
|||
xcuserdata/
|
||||
DerivedData/
|
||||
|
||||
# Ignore files built by AppCode
|
||||
.idea/
|
||||
|
||||
# Ignore files built by bada
|
||||
.Simulator-Debug/
|
||||
.Target-Debug/
|
||||
|
|
|
@ -1 +1 @@
|
|||
245681d0a739a5432c0e588df5627789e03d902f
|
||||
8c640bf1e2f1cd0a489e6169c930234b809636f2
|
|
@ -1 +1 @@
|
|||
7d3978ce3b84a7fd50e88c4e4b6a6e5bfdc92a20
|
||||
9581e9b5831d86fdf7d4ea38295ca4bda9101e01
|
|
@ -39,6 +39,8 @@ CCEventAcceleration.cpp \
|
|||
CCEventCustom.cpp \
|
||||
CCEventDispatcher.cpp \
|
||||
CCEventKeyboard.cpp \
|
||||
CCEventMouse.cpp \
|
||||
CCEventListenerMouse.cpp \
|
||||
CCEventListener.cpp \
|
||||
CCEventListenerAcceleration.cpp \
|
||||
CCEventListenerCustom.cpp \
|
||||
|
|
|
@ -46,6 +46,7 @@ public:
|
|||
TOUCH,
|
||||
KEYBOARD,
|
||||
ACCELERATION,
|
||||
MOUSE,
|
||||
CUSTOM
|
||||
};
|
||||
|
||||
|
|
|
@ -77,6 +77,9 @@ static EventListener::ListenerID getListenerID(Event* event)
|
|||
case Event::Type::KEYBOARD:
|
||||
ret = static_cast<EventListener::ListenerID>(EventListener::Type::KEYBOARD);
|
||||
break;
|
||||
case Event::Type::MOUSE:
|
||||
ret = static_cast<EventListener::ListenerID>(EventListener::Type::MOUSE);
|
||||
break;
|
||||
case Event::Type::TOUCH:
|
||||
// Touch listener is very special, it contains two kinds of listeners, EventListenerTouchOneByOne and EventListenerTouchAllAtOnce.
|
||||
// return UNKNOW instead.
|
||||
|
|
|
@ -52,6 +52,7 @@ public:
|
|||
TOUCH_ONE_BY_ONE,
|
||||
TOUCH_ALL_AT_ONCE,
|
||||
KEYBOARD,
|
||||
MOUSE,
|
||||
ACCELERATION,
|
||||
CUSTOM
|
||||
};
|
||||
|
|
|
@ -0,0 +1,110 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2013 cocos2d-x.org
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
****************************************************************************/
|
||||
|
||||
#include "CCEventListenerMouse.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
bool EventListenerMouse::checkAvailable()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
EventListenerMouse* EventListenerMouse::create()
|
||||
{
|
||||
auto ret = new EventListenerMouse();
|
||||
if (ret && ret->init())
|
||||
{
|
||||
ret->autorelease();
|
||||
}
|
||||
else
|
||||
{
|
||||
CC_SAFE_DELETE(ret);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
EventListenerMouse* EventListenerMouse::clone()
|
||||
{
|
||||
auto ret = new EventListenerMouse();
|
||||
if (ret && ret->init())
|
||||
{
|
||||
ret->autorelease();
|
||||
ret->onMouseUp = onMouseUp;
|
||||
ret->onMouseDown = onMouseDown;
|
||||
ret->onMouseMove = onMouseMove;
|
||||
ret->onMouseScroll = onMouseScroll;
|
||||
}
|
||||
else
|
||||
{
|
||||
CC_SAFE_DELETE(ret);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
EventListenerMouse::EventListenerMouse()
|
||||
: onMouseUp(nullptr)
|
||||
, onMouseDown(nullptr)
|
||||
, onMouseMove(nullptr)
|
||||
, onMouseScroll(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
bool EventListenerMouse::init()
|
||||
{
|
||||
auto listener = [this](Event* event){
|
||||
auto mouseEvent = static_cast<EventMouse*>(event);
|
||||
switch (mouseEvent->_mouseEventType)
|
||||
{
|
||||
case EventMouse::MouseEventType::MOUSE_DOWN:
|
||||
if(onMouseDown != nullptr)
|
||||
onMouseDown(event);
|
||||
break;
|
||||
case EventMouse::MouseEventType::MOUSE_UP:
|
||||
if(onMouseUp != nullptr)
|
||||
onMouseUp(event);
|
||||
break;
|
||||
case EventMouse::MouseEventType::MOUSE_MOVE:
|
||||
if(onMouseMove != nullptr)
|
||||
onMouseMove(event);
|
||||
break;
|
||||
case EventMouse::MouseEventType::MOUSE_SCROLL:
|
||||
if(onMouseScroll != nullptr)
|
||||
onMouseScroll(event);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
if (EventListener::init(Type::MOUSE, static_cast<ListenerID>(Type::MOUSE), listener))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
NS_CC_END
|
|
@ -0,0 +1,57 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2013 cocos2d-x.org
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
****************************************************************************/
|
||||
|
||||
#include "CCEventListener.h"
|
||||
#include "CCEventMouse.h"
|
||||
|
||||
#ifndef __cocos2d_libs__CCMouseEventListener__
|
||||
#define __cocos2d_libs__CCMouseEventListener__
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
class Event;
|
||||
|
||||
class EventListenerMouse : public EventListener
|
||||
{
|
||||
public:
|
||||
static EventListenerMouse* create();
|
||||
|
||||
/// Overrides
|
||||
virtual EventListenerMouse* clone() override;
|
||||
virtual bool checkAvailable() override;
|
||||
|
||||
std::function<void(Event* event)> onMouseDown;
|
||||
std::function<void(Event* event)> onMouseUp;
|
||||
std::function<void(Event* event)> onMouseMove;
|
||||
std::function<void(Event* event)> onMouseScroll;
|
||||
|
||||
private:
|
||||
EventListenerMouse();
|
||||
bool init();
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif /* defined(__cocos2d_libs__CCMouseEventListener__) */
|
|
@ -0,0 +1,41 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2013 cocos2d-x.org
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
****************************************************************************/
|
||||
|
||||
#include "CCEventMouse.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
EventMouse::EventMouse(MouseEventType mouseEventCode)
|
||||
: Event(Type::MOUSE)
|
||||
, _mouseEventType(mouseEventCode)
|
||||
, _mouseButton(0)
|
||||
, _x(0.0f)
|
||||
, _y(0.0f)
|
||||
, _scrollX(0.0f)
|
||||
, _scrollY(0.0f)
|
||||
{
|
||||
};
|
||||
|
||||
NS_CC_END
|
|
@ -0,0 +1,83 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2013 cocos2d-x.org
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __cocos2d_libs__CCMouseEvent__
|
||||
#define __cocos2d_libs__CCMouseEvent__
|
||||
|
||||
#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
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Different types of MouseEvent
|
||||
*/
|
||||
enum class MouseEventType
|
||||
{
|
||||
MOUSE_NONE,
|
||||
MOUSE_DOWN,
|
||||
MOUSE_UP,
|
||||
MOUSE_MOVE,
|
||||
MOUSE_SCROLL,
|
||||
};
|
||||
|
||||
EventMouse(MouseEventType mouseEventCode);
|
||||
|
||||
/** Set mouse scroll data */
|
||||
inline void setScrollData(float scrollX, float scrollY) { _scrollX = scrollX; _scrollY = scrollY; };
|
||||
inline float getScrollX() { return _scrollX; };
|
||||
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; };
|
||||
|
||||
private:
|
||||
MouseEventType _mouseEventType;
|
||||
int _mouseButton;
|
||||
float _x;
|
||||
float _y;
|
||||
float _scrollX;
|
||||
float _scrollY;
|
||||
|
||||
friend class EventListenerMouse;
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif /* defined(__cocos2d_libs__CCMouseEvent__) */
|
|
@ -36,6 +36,8 @@ CCEventDispatcher.cpp \
|
|||
CCEventListener.cpp \
|
||||
CCEventKeyboard.cpp \
|
||||
CCEventListenerKeyboard.cpp \
|
||||
CCEventMouse.cpp \
|
||||
CCEventListenerMouse.cpp \
|
||||
CCTouch.cpp \
|
||||
CCEventTouch.cpp \
|
||||
CCEventListenerTouch.cpp \
|
||||
|
|
|
@ -226,6 +226,8 @@ THE SOFTWARE.
|
|||
#include "CCEventTouch.h"
|
||||
#include "CCEventListenerKeyboard.h"
|
||||
#include "CCEventKeyboard.h"
|
||||
#include "CCEventListenerMouse.h"
|
||||
#include "CCEventMouse.h"
|
||||
#include "CCEventAcceleration.h"
|
||||
#include "CCEventListenerAcceleration.h"
|
||||
#include "CCEventCustom.h"
|
||||
|
|
|
@ -243,7 +243,9 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou
|
|||
<ClCompile Include="CCEventListenerAcceleration.cpp" />
|
||||
<ClCompile Include="CCEventListenerCustom.cpp" />
|
||||
<ClCompile Include="CCEventListenerKeyboard.cpp" />
|
||||
<ClCompile Include="CCEventListenerMouse.cpp" />
|
||||
<ClCompile Include="CCEventListenerTouch.cpp" />
|
||||
<ClCompile Include="CCEventMouse.cpp" />
|
||||
<ClCompile Include="CCEventTouch.cpp" />
|
||||
<ClCompile Include="CCFont.cpp" />
|
||||
<ClCompile Include="CCFontAtlas.cpp" />
|
||||
|
@ -414,7 +416,9 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32-specific\gles\prebuilt\*.*" "$(Ou
|
|||
<ClInclude Include="CCEventListenerAcceleration.h" />
|
||||
<ClInclude Include="CCEventListenerCustom.h" />
|
||||
<ClInclude Include="CCEventListenerKeyboard.h" />
|
||||
<ClInclude Include="CCEventListenerMouse.h" />
|
||||
<ClInclude Include="CCEventListenerTouch.h" />
|
||||
<ClInclude Include="CCEventMouse.h" />
|
||||
<ClInclude Include="CCEventTouch.h" />
|
||||
<ClInclude Include="CCEventType.h" />
|
||||
<ClInclude Include="CCFont.h" />
|
||||
|
|
|
@ -572,6 +572,12 @@
|
|||
<ClCompile Include="ccUtils.cpp">
|
||||
<Filter>support</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CCEventListenerMouse.cpp">
|
||||
<Filter>event_dispatcher</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CCEventMouse.cpp">
|
||||
<Filter>event_dispatcher</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\physics\CCPhysicsBody.h">
|
||||
|
@ -1152,5 +1158,11 @@
|
|||
<ClInclude Include="..\base\CCString.h">
|
||||
<Filter>base</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CCEventListenerMouse.h">
|
||||
<Filter>event_dispatcher</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CCEventMouse.h">
|
||||
<Filter>event_dispatcher</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -13,6 +13,7 @@
|
|||
#include "CCIMEDispatcher.h"
|
||||
#include "CCEventDispatcher.h"
|
||||
#include "CCEventKeyboard.h"
|
||||
#include "CCEventMouse.h"
|
||||
#include <unistd.h>
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
@ -159,6 +160,7 @@ public:
|
|||
static void OnGLFWError(int errorID, const char* errorDesc);
|
||||
static void OnGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify);
|
||||
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 OnGLFWCharCallback(GLFWwindow* window, unsigned int character);
|
||||
static void OnGLFWWindowPosCallback(GLFWwindow* windows, int x, int y);
|
||||
|
@ -198,6 +200,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);
|
||||
Director::getInstance()->getEventDispatcher()->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);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
||||
}
|
||||
}
|
||||
|
||||
void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
|
||||
|
@ -218,6 +235,23 @@ void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x,
|
|||
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);
|
||||
Director::getInstance()->getEventDispatcher()->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);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
||||
}
|
||||
|
||||
void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
|
||||
|
@ -283,6 +317,7 @@ bool EGLView::init(const char* viewName, float width, float height, float frameZ
|
|||
|
||||
glfwSetMouseButtonCallback(_mainWindow,EGLViewEventHandler::OnGLFWMouseCallBack);
|
||||
glfwSetCursorPosCallback(_mainWindow,EGLViewEventHandler::OnGLFWMouseMoveCallBack);
|
||||
glfwSetScrollCallback(_mainWindow, EGLViewEventHandler::OnGLFWMouseScrollCallback);
|
||||
glfwSetCharCallback(_mainWindow, EGLViewEventHandler::OnGLFWCharCallback);
|
||||
glfwSetKeyCallback(_mainWindow, EGLViewEventHandler::OnGLFWKeyCallback);
|
||||
glfwSetWindowPosCallback(_mainWindow, EGLViewEventHandler::OnGLFWWindowPosCallback);
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include "CCTouch.h"
|
||||
#include "CCEventDispatcher.h"
|
||||
#include "CCEventKeyboard.h"
|
||||
#include "CCEventMouse.h"
|
||||
#include "CCIMEDispatcher.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
@ -175,6 +176,7 @@ public:
|
|||
static void OnGLFWError(int errorID, const char* errorDesc);
|
||||
static void OnGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify);
|
||||
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 OnGLFWCharCallback(GLFWwindow* window, unsigned int character);
|
||||
static void OnGLFWWindowPosCallback(GLFWwindow* windows, int x, int y);
|
||||
|
@ -214,14 +216,31 @@ void EGLViewEventHandler::OnGLFWMouseCallBack(GLFWwindow* window, int button, in
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(GLFW_PRESS == action)
|
||||
{
|
||||
EventMouse event(EventMouse::MouseEventType::MOUSE_DOWN);
|
||||
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
|
||||
event.setCursorPosition(s_mouseX, eglView->getViewPortRect().size.height - s_mouseY);
|
||||
event.setMouseButton(button);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
||||
}
|
||||
else if(GLFW_RELEASE == action)
|
||||
{
|
||||
EventMouse event(EventMouse::MouseEventType::MOUSE_UP);
|
||||
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
|
||||
event.setCursorPosition(s_mouseX, eglView->getViewPortRect().size.height - s_mouseY);
|
||||
event.setMouseButton(button);
|
||||
Director::getInstance()->getEventDispatcher()->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();
|
||||
|
@ -234,6 +253,23 @@ void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x,
|
|||
eglView->handleTouchesMove(1, &id, &s_mouseX, &s_mouseY);
|
||||
}
|
||||
}
|
||||
|
||||
EventMouse event(EventMouse::MouseEventType::MOUSE_MOVE);
|
||||
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
|
||||
event.setCursorPosition(s_mouseX, eglView->getViewPortRect().size.height - s_mouseY);
|
||||
Director::getInstance()->getEventDispatcher()->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 and cocos2d-x uses different Y axis, we need to convert the coordinate here
|
||||
event.setScrollData((float)x, -(float)y);
|
||||
event.setCursorPosition(s_mouseX, eglView->getViewPortRect().size.height - s_mouseY);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
||||
}
|
||||
|
||||
void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
|
||||
|
@ -301,6 +337,7 @@ bool EGLView::init(const char *viewName, float width, float height, float frameZ
|
|||
|
||||
glfwSetMouseButtonCallback(_mainWindow,EGLViewEventHandler::OnGLFWMouseCallBack);
|
||||
glfwSetCursorPosCallback(_mainWindow,EGLViewEventHandler::OnGLFWMouseMoveCallBack);
|
||||
glfwSetScrollCallback(_mainWindow, EGLViewEventHandler::OnGLFWMouseScrollCallback);
|
||||
glfwSetCharCallback(_mainWindow, EGLViewEventHandler::OnGLFWCharCallback);
|
||||
glfwSetKeyCallback(_mainWindow, EGLViewEventHandler::OnGLFWKeyCallback);
|
||||
glfwSetWindowPosCallback(_mainWindow, EGLViewEventHandler::OnGLFWWindowPosCallback);
|
||||
|
|
|
@ -31,7 +31,7 @@ THE SOFTWARE.
|
|||
#include "CCTouch.h"
|
||||
#include "CCEventDispatcher.h"
|
||||
#include "CCEventKeyboard.h"
|
||||
|
||||
#include "CCEventMouse.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
|
@ -277,6 +277,7 @@ public:
|
|||
static void OnGLFWError(int errorID, const char* errorDesc);
|
||||
static void OnGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify);
|
||||
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 OnGLFWCharCallback(GLFWwindow* window, unsigned int character);
|
||||
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);
|
||||
Director::getInstance()->getEventDispatcher()->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);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
Director::getInstance()->getEventDispatcher()->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);
|
||||
Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
||||
}
|
||||
|
||||
void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
|
||||
|
@ -406,8 +439,10 @@ bool EGLView::init(const char* viewName, float width, float height, float frameZ
|
|||
glfwGetFramebufferSize(_mainWindow, &_frameBufferSize[0], &_frameBufferSize[1]);
|
||||
glfwSetMouseButtonCallback(_mainWindow,EGLViewEventHandler::OnGLFWMouseCallBack);
|
||||
glfwSetCursorPosCallback(_mainWindow,EGLViewEventHandler::OnGLFWMouseMoveCallBack);
|
||||
glfwSetScrollCallback(_mainWindow, EGLViewEventHandler::OnGLFWMouseScrollCallback);
|
||||
glfwSetCharCallback(_mainWindow, EGLViewEventHandler::OnGLFWCharCallback);
|
||||
glfwSetKeyCallback(_mainWindow, EGLViewEventHandler::OnGLFWKeyCallback);
|
||||
glfwSetWindowPosCallback(_mainWindow, EGLViewEventHandler::OnGLFWWindowPosCallback);
|
||||
|
||||
// check OpenGL version at first
|
||||
const GLubyte* glVersion = glGetString(GL_VERSION);
|
||||
|
|
|
@ -99,6 +99,7 @@ Classes/ExtensionsTest/TableViewTest/TableViewTestScene.cpp \
|
|||
Classes/FileUtilsTest/FileUtilsTest.cpp \
|
||||
Classes/FontTest/FontTest.cpp \
|
||||
Classes/IntervalTest/IntervalTest.cpp \
|
||||
Classes/InputTest/MouseTest.cpp \
|
||||
Classes/KeyboardTest/KeyboardTest.cpp \
|
||||
Classes/KeypadTest/KeypadTest.cpp \
|
||||
Classes/LabelTest/LabelTest.cpp \
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
#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->addEventListenerWithSceneGraphPriority(_mouseListener, this);
|
||||
|
||||
_labelAction->retain();
|
||||
_labelPosition->retain();
|
||||
}
|
||||
|
||||
MouseTest::~MouseTest()
|
||||
{
|
||||
_eventDispatcher->removeEventListener(_mouseListener);
|
||||
|
||||
_labelAction->release();
|
||||
_labelPosition->release();
|
||||
}
|
||||
|
||||
template <typename T> string tostr(const T& t) { ostringstream os; os<<t; return os.str(); }
|
||||
|
||||
void MouseTest::onMouseDown(Event *event)
|
||||
{
|
||||
EventMouse* e = (EventMouse*)event;
|
||||
string str = "Mouse Down detected, Key: ";
|
||||
str += tostr(e->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();
|
||||
}
|
|
@ -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
|
|
@ -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
|
||||
|
@ -130,6 +131,10 @@ TestController::TestController()
|
|||
listener->onTouchMoved = CC_CALLBACK_2(TestController::onTouchMoved, this);
|
||||
|
||||
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
|
||||
auto mouseListener = EventListenerMouse::create();
|
||||
mouseListener->onMouseScroll = CC_CALLBACK_1(TestController::onMouseScroll, this);
|
||||
_eventDispatcher->addEventListenerWithSceneGraphPriority(mouseListener, this);
|
||||
}
|
||||
|
||||
TestController::~TestController()
|
||||
|
@ -193,3 +198,27 @@ void TestController::onTouchMoved(Touch* touch, Event *event)
|
|||
_beginPos = touchLocation;
|
||||
s_tCurPos = nextPos;
|
||||
}
|
||||
|
||||
void TestController::onMouseScroll(Event *event)
|
||||
{
|
||||
auto mouseEvent = static_cast<EventMouse*>(event);
|
||||
float nMoveY = mouseEvent->getScrollY() * 6;
|
||||
|
||||
auto curPos = _itemMenu->getPosition();
|
||||
auto nextPos = Point(curPos.x, curPos.y + nMoveY);
|
||||
|
||||
if (nextPos.y < 0.0f)
|
||||
{
|
||||
_itemMenu->setPosition(Point::ZERO);
|
||||
return;
|
||||
}
|
||||
|
||||
if (nextPos.y > ((g_testCount + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height))
|
||||
{
|
||||
_itemMenu->setPosition(Point(0, ((g_testCount + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height)));
|
||||
return;
|
||||
}
|
||||
|
||||
_itemMenu->setPosition(nextPos);
|
||||
s_tCurPos = nextPos;
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@ public:
|
|||
bool onTouchBegan(Touch* touches, Event *event);
|
||||
void onTouchMoved(Touch* touches, Event *event);
|
||||
|
||||
void onMouseScroll(Event *event);
|
||||
|
||||
private:
|
||||
Point _beginPos;
|
||||
Menu* _itemMenu;
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -90,6 +90,7 @@ SOURCES = ../Classes/AccelerometerTest/AccelerometerTest.cpp \
|
|||
../Classes/FontTest/FontTest.cpp \
|
||||
../Classes/IntervalTest/IntervalTest.cpp \
|
||||
../Classes/KeyboardTest/KeyboardTest.cpp \
|
||||
../Classes/InputTest/MouseTest.cpp \
|
||||
../Classes/KeypadTest/KeypadTest.cpp \
|
||||
../Classes/LabelTest/LabelTest.cpp \
|
||||
../Classes/LabelTest/LabelTestNew.cpp \
|
||||
|
|
|
@ -179,6 +179,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\..\..\external\websockets\prebuilt\win32\*.*" "$
|
|||
<ClCompile Include="..\Classes\ExtensionsTest\TableViewTest\CustomTableViewCell.cpp" />
|
||||
<ClCompile Include="..\Classes\ExtensionsTest\TableViewTest\TableViewTestScene.cpp" />
|
||||
<ClCompile Include="..\Classes\FileUtilsTest\FileUtilsTest.cpp" />
|
||||
<ClCompile Include="..\Classes\InputTest\MouseTest.cpp" />
|
||||
<ClCompile Include="..\Classes\KeyboardTest\KeyboardTest.cpp" />
|
||||
<ClCompile Include="..\Classes\LabelTest\LabelTestNew.cpp" />
|
||||
<ClCompile Include="..\Classes\NewEventDispatcherTest\NewEventDispatcherTest.cpp" />
|
||||
|
@ -315,6 +316,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\..\..\external\websockets\prebuilt\win32\*.*" "$
|
|||
<ClInclude Include="..\Classes\ExtensionsTest\TableViewTest\CustomTableViewCell.h" />
|
||||
<ClInclude Include="..\Classes\ExtensionsTest\TableViewTest\TableViewTestScene.h" />
|
||||
<ClInclude Include="..\Classes\FileUtilsTest\FileUtilsTest.h" />
|
||||
<ClInclude Include="..\Classes\InputTest\MouseTest.h" />
|
||||
<ClInclude Include="..\Classes\KeyboardTest\KeyboardTest.h" />
|
||||
<ClInclude Include="..\Classes\LabelTest\LabelTestNew.h" />
|
||||
<ClInclude Include="..\Classes\NewEventDispatcherTest\NewEventDispatcherTest.h" />
|
||||
|
|
|
@ -301,6 +301,9 @@
|
|||
<Filter Include="Classes\KeyboardTest">
|
||||
<UniqueIdentifier>{8d7d37cd-5cc2-4a7d-9bd2-7b5c928adbb5}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Classes\InputTest">
|
||||
<UniqueIdentifier>{8e8124bd-adb2-4a8f-8727-9868a9c42d80}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.cpp">
|
||||
|
@ -697,6 +700,9 @@
|
|||
<ClCompile Include="..\Classes\KeyboardTest\KeyboardTest.cpp">
|
||||
<Filter>Classes\KeyboardTest</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Classes\InputTest\MouseTest.cpp">
|
||||
<Filter>Classes\InputTest</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="main.h">
|
||||
|
@ -1270,5 +1276,8 @@
|
|||
<ClInclude Include="..\Classes\KeyboardTest\KeyboardTest.h">
|
||||
<Filter>Classes\KeyboardTest</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\Classes\InputTest\MouseTest.h">
|
||||
<Filter>Classes\InputTest</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -0,0 +1 @@
|
|||
3275ac270645139eb273b2a47c215f39bcb0e0d5
|
Loading…
Reference in New Issue