2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-12 05:35:38 +08:00
|
|
|
#include "MouseTest.h"
|
|
|
|
|
2015-04-09 08:37:30 +08:00
|
|
|
USING_NS_CC;
|
|
|
|
|
2015-03-04 17:42:24 +08:00
|
|
|
template <typename T> std::string tostr(const T& t) { std::ostringstream os; os<<t; return os.str(); }
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
MouseTests::MouseTests()
|
2015-03-04 06:52:55 +08:00
|
|
|
{
|
2015-04-03 14:31:03 +08:00
|
|
|
ADD_TEST_CASE(MouseEventTest);
|
|
|
|
ADD_TEST_CASE(HideMouseTest);
|
2015-03-04 06:52:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// MouseEventTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
MouseEventTest::MouseEventTest()
|
2013-10-12 05:35:38 +08:00
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
//Create a label to display the mouse action
|
2014-04-09 23:31:05 +08:00
|
|
|
_labelAction = Label::createWithTTF("Click mouse button and see this change", "fonts/arial.ttf", 22);
|
2014-05-15 01:07:09 +08:00
|
|
|
_labelAction->setPosition(Vec2(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
|
2014-04-09 23:31:05 +08:00
|
|
|
_labelPosition = Label::createWithTTF("Mouse not supported on this device", "fonts/arial.ttf", 22);
|
2014-05-15 01:07:09 +08:00
|
|
|
_labelPosition->setPosition(Vec2(s.width/2, s.height/3));
|
2013-10-12 05:35:38 +08:00
|
|
|
addChild(_labelPosition);
|
|
|
|
|
|
|
|
|
|
|
|
_mouseListener = EventListenerMouse::create();
|
2015-03-04 06:52:55 +08:00
|
|
|
_mouseListener->onMouseMove = CC_CALLBACK_1(MouseEventTest::onMouseMove, this);
|
|
|
|
_mouseListener->onMouseUp = CC_CALLBACK_1(MouseEventTest::onMouseUp, this);
|
|
|
|
_mouseListener->onMouseDown = CC_CALLBACK_1(MouseEventTest::onMouseDown, this);
|
|
|
|
_mouseListener->onMouseScroll = CC_CALLBACK_1(MouseEventTest::onMouseScroll, this);
|
2013-10-12 05:35:38 +08:00
|
|
|
|
2013-10-31 14:19:36 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(_mouseListener, this);
|
2013-10-12 05:35:38 +08:00
|
|
|
}
|
|
|
|
|
2015-03-04 06:52:55 +08:00
|
|
|
MouseEventTest::~MouseEventTest()
|
2013-10-12 05:35:38 +08:00
|
|
|
{
|
2013-10-31 14:19:36 +08:00
|
|
|
_eventDispatcher->removeEventListener(_mouseListener);
|
2013-10-12 05:35:38 +08:00
|
|
|
}
|
|
|
|
|
2015-03-04 06:52:55 +08:00
|
|
|
void MouseEventTest::onMouseDown(Event *event)
|
2013-10-12 05:35:38 +08:00
|
|
|
{
|
|
|
|
EventMouse* e = (EventMouse*)event;
|
2013-12-19 03:45:30 +08:00
|
|
|
std::string str = "Mouse Down detected, Key: ";
|
2017-03-02 13:39:33 +08:00
|
|
|
str += tostr(static_cast<int>(e->getMouseButton()));
|
2013-10-12 05:35:38 +08:00
|
|
|
_labelAction->setString(str.c_str());
|
|
|
|
}
|
|
|
|
|
2015-03-04 06:52:55 +08:00
|
|
|
void MouseEventTest::onMouseUp(Event *event)
|
2013-10-12 05:35:38 +08:00
|
|
|
{
|
|
|
|
EventMouse* e = (EventMouse*)event;
|
2013-12-19 03:45:30 +08:00
|
|
|
std::string str = "Mouse Up detected, Key: ";
|
2017-03-02 13:39:33 +08:00
|
|
|
str += tostr(static_cast<int>(e->getMouseButton()));
|
2013-10-12 05:35:38 +08:00
|
|
|
_labelAction->setString(str.c_str());
|
|
|
|
}
|
|
|
|
|
2015-03-04 06:52:55 +08:00
|
|
|
void MouseEventTest::onMouseMove(Event *event)
|
2013-10-12 05:35:38 +08:00
|
|
|
{
|
|
|
|
EventMouse* e = (EventMouse*)event;
|
2013-12-19 03:45:30 +08:00
|
|
|
std::string str = "MousePosition X:";
|
2015-03-04 17:42:24 +08:00
|
|
|
str = str + tostr(e->getCursorX()) + " Y:" + tostr(e->getCursorY());
|
2013-10-12 05:35:38 +08:00
|
|
|
_labelPosition->setString(str.c_str());
|
|
|
|
}
|
|
|
|
|
2015-03-04 06:52:55 +08:00
|
|
|
void MouseEventTest::onMouseScroll(Event *event)
|
2013-10-12 05:35:38 +08:00
|
|
|
{
|
|
|
|
EventMouse* e = (EventMouse*)event;
|
2013-12-19 03:45:30 +08:00
|
|
|
std::string str = "Mouse Scroll detected, X: ";
|
2015-03-04 17:42:24 +08:00
|
|
|
str = str + tostr(e->getScrollX()) + " Y: " + tostr(e->getScrollY());
|
2013-10-12 05:35:38 +08:00
|
|
|
_labelAction->setString(str.c_str());
|
|
|
|
}
|
|
|
|
|
2015-03-04 06:52:55 +08:00
|
|
|
std::string MouseEventTest::title() const
|
2013-10-12 05:35:38 +08:00
|
|
|
{
|
2015-03-04 06:52:55 +08:00
|
|
|
return "Mouse Event Test";
|
|
|
|
}
|
2013-10-12 05:35:38 +08:00
|
|
|
|
2015-03-04 06:52:55 +08:00
|
|
|
std::string MouseEventTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "This tests the mouse events";
|
2014-06-19 19:45:24 +08:00
|
|
|
}
|
2015-03-04 06:52:55 +08:00
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// HideMouseTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
HideMouseTest::HideMouseTest()
|
|
|
|
{
|
|
|
|
|
|
|
|
_lis = EventListenerMouse::create();
|
|
|
|
_lis->onMouseDown = [](Event* e){
|
|
|
|
Director::getInstance()->getOpenGLView()->setCursorVisible(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
_lis->onMouseUp = [](Event* e){
|
|
|
|
Director::getInstance()->getOpenGLView()->setCursorVisible(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(_lis, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
HideMouseTest::~HideMouseTest()
|
|
|
|
{
|
|
|
|
_eventDispatcher->removeEventListener(_lis);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string HideMouseTest::title() const
|
|
|
|
{
|
|
|
|
return "Hide/Show Mouse";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string HideMouseTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Click to hide mouse";
|
|
|
|
}
|
|
|
|
|