2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
http://www.cocos2d-x.org
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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:
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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 "MouseTest.h"
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
template <typename T>
|
|
|
|
std::string tostr(const T& t)
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
os << t;
|
|
|
|
return os.str();
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
MouseTests::MouseTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(MouseEventTest);
|
|
|
|
ADD_TEST_CASE(HideMouseTest);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// MouseEventTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
MouseEventTest::MouseEventTest()
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
// Create a label to display the mouse action
|
2019-11-23 20:27:39 +08:00
|
|
|
_labelAction = Label::createWithTTF("Click mouse button and see this change", "fonts/arial.ttf", 22);
|
2021-12-28 21:27:32 +08:00
|
|
|
_labelAction->setPosition(Vec2(s.width / 2, s.height * 2 / 3));
|
2019-11-23 20:27:39 +08:00
|
|
|
addChild(_labelAction, 0);
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
// Create a label to display the mouse position
|
2019-11-23 20:27:39 +08:00
|
|
|
_labelPosition = Label::createWithTTF("Mouse not supported on this device", "fonts/arial.ttf", 22);
|
2021-12-28 21:27:32 +08:00
|
|
|
_labelPosition->setPosition(Vec2(s.width / 2, s.height / 3));
|
2019-11-23 20:27:39 +08:00
|
|
|
addChild(_labelPosition);
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
_mouseListener = EventListenerMouse::create();
|
|
|
|
_mouseListener->onMouseMove = CC_CALLBACK_1(MouseEventTest::onMouseMove, this);
|
|
|
|
_mouseListener->onMouseUp = CC_CALLBACK_1(MouseEventTest::onMouseUp, this);
|
|
|
|
_mouseListener->onMouseDown = CC_CALLBACK_1(MouseEventTest::onMouseDown, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
_mouseListener->onMouseScroll = CC_CALLBACK_1(MouseEventTest::onMouseScroll, this);
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(_mouseListener, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
MouseEventTest::~MouseEventTest()
|
|
|
|
{
|
|
|
|
_eventDispatcher->removeEventListener(_mouseListener);
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
void MouseEventTest::onMouseDown(Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
EventMouse* e = (EventMouse*)event;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string str = "Mouse Down detected, Key: ";
|
|
|
|
str += tostr(static_cast<int>(e->getMouseButton()));
|
|
|
|
_labelAction->setString(str.c_str());
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
void MouseEventTest::onMouseUp(Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
EventMouse* e = (EventMouse*)event;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string str = "Mouse Up detected, Key: ";
|
|
|
|
str += tostr(static_cast<int>(e->getMouseButton()));
|
|
|
|
_labelAction->setString(str.c_str());
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
void MouseEventTest::onMouseMove(Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
EventMouse* e = (EventMouse*)event;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string str = "MousePosition X:";
|
2021-12-28 21:27:32 +08:00
|
|
|
str = str + tostr(e->getCursorX()) + " Y:" + tostr(e->getCursorY());
|
2019-11-23 20:27:39 +08:00
|
|
|
_labelPosition->setString(str.c_str());
|
|
|
|
}
|
|
|
|
|
2021-12-28 21:27:32 +08:00
|
|
|
void MouseEventTest::onMouseScroll(Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
EventMouse* e = (EventMouse*)event;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string str = "Mouse Scroll detected, X: ";
|
2021-12-28 21:27:32 +08:00
|
|
|
str = str + tostr(e->getScrollX()) + " Y: " + tostr(e->getScrollY());
|
2019-11-23 20:27:39 +08:00
|
|
|
_labelAction->setString(str.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MouseEventTest::title() const
|
|
|
|
{
|
|
|
|
return "Mouse Event Test";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MouseEventTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "This tests the mouse events";
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// HideMouseTest
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
HideMouseTest::HideMouseTest()
|
|
|
|
{
|
2021-12-28 21:27:32 +08:00
|
|
|
|
|
|
|
_lis = EventListenerMouse::create();
|
|
|
|
_lis->onMouseDown = [](Event* e) { Director::getInstance()->getOpenGLView()->setCursorVisible(false); };
|
|
|
|
|
|
|
|
_lis->onMouseUp = [](Event* e) { Director::getInstance()->getOpenGLView()->setCursorVisible(true); };
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_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";
|
|
|
|
}
|