Mouse working on mac

This commit is contained in:
Nite Luo 2013-10-11 14:35:38 -07:00
parent e19957136e
commit 09c0e23b5d
7 changed files with 152 additions and 14 deletions

View File

@ -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; };

View File

@ -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);
}

View File

@ -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 <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();
}

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -1 +1 @@
b759f56a07f242b20e9c96f2a6cd49c4b5c9dc9b
3275ac270645139eb273b2a47c215f39bcb0e0d5