axmol/tests/cpp-tests/Classes/InputTest/MouseTest.cpp

82 lines
2.6 KiB
C++
Raw Normal View History

2013-10-12 05:35:38 +08:00
#include "MouseTest.h"
MouseTest::MouseTest()
{
auto s = Director::getInstance()->getWinSize();
auto title = Label::createWithTTF("Mouse Test", "fonts/arial.ttf", 28);
2013-10-12 05:35:38 +08:00
addChild(title, 0);
title->setPosition( Vector2(s.width/2, s.height-50) );
2013-10-12 05:35:38 +08:00
//Create a label to display the mouse action
_labelAction = Label::createWithTTF("Click mouse button and see this change", "fonts/arial.ttf", 22);
_labelAction->setPosition(Vector2(s.width/2, s.height*2/3));
2013-10-12 05:35:38 +08:00
addChild(_labelAction, 0);
//Create a label to display the mouse position
_labelPosition = Label::createWithTTF("Mouse not supported on this device", "fonts/arial.ttf", 22);
_labelPosition->setPosition(Vector2(s.width/2, s.height/3));
2013-10-12 05:35:38 +08:00
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);
2013-10-31 14:19:36 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(_mouseListener, this);
2013-10-12 05:35:38 +08:00
_labelAction->retain();
_labelPosition->retain();
}
MouseTest::~MouseTest()
{
2013-10-31 14:19:36 +08:00
_eventDispatcher->removeEventListener(_mouseListener);
2013-10-12 05:35:38 +08:00
_labelAction->release();
_labelPosition->release();
}
template <typename T> std::string tostr(const T& t) { std::ostringstream os; os<<t; return os.str(); }
2013-10-12 05:35:38 +08:00
void MouseTest::onMouseDown(Event *event)
{
EventMouse* e = (EventMouse*)event;
std::string str = "Mouse Down detected, Key: ";
2013-10-12 05:35:38 +08:00
str += tostr(e->getMouseButton());
_labelAction->setString(str.c_str());
}
void MouseTest::onMouseUp(Event *event)
{
EventMouse* e = (EventMouse*)event;
std::string str = "Mouse Up detected, Key: ";
2013-10-12 05:35:38 +08:00
str += tostr(e->getMouseButton());
_labelAction->setString(str.c_str());
}
void MouseTest::onMouseMove(Event *event)
{
EventMouse* e = (EventMouse*)event;
std::string str = "MousePosition X:";
2013-10-12 05:35:38 +08:00
str = str + tostr(e->getCursorX()) + " Y:" + tostr(e->getCursorY());
_labelPosition->setString(str.c_str());
}
void MouseTest::onMouseScroll(Event *event)
{
EventMouse* e = (EventMouse*)event;
std::string str = "Mouse Scroll detected, X: ";
2013-10-12 05:35:38 +08:00
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();
}