PuzzleSDK
MultiTouchTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "MultiTouchTest.h"
26
28
29MultiTouchTests::MultiTouchTests()
30{
32}
33
34static const Color3B* s_TouchColors[5] = {
35 &Color3B::YELLOW,
36 &Color3B::BLUE,
37 &Color3B::GREEN,
38 &Color3B::RED,
39 &Color3B::MAGENTA
40};
41
42class TouchPoint : public Node
43{
44public:
45 TouchPoint(const Vec2 &touchPoint, const Color3B &touchColor)
46 {
47 DrawNode* drawNode = DrawNode::create();
48 auto s = Director::getInstance()->getWinSize();
49 Color4F color(touchColor.r/255.0f, touchColor.g/255.0f, touchColor.b/255.0f, 1.0f);
50 drawNode->drawLine(Vec2(0.0f, touchPoint.y), Vec2(s.width, touchPoint.y), color);
51 drawNode->drawLine(Vec2(touchPoint.x, 0.0f), Vec2(touchPoint.x, s.height), color);
52 drawNode->drawDot(touchPoint, 3, color);
53 addChild(drawNode);
54 }
55
56 static TouchPoint* touchPointWithParent(Node* pParent, const Vec2 &touchPoint, const Color3B &touchColor)
57 {
58 auto pRet = new (std::nothrow) TouchPoint(touchPoint, touchColor);
59 pRet->setContentSize(pParent->getContentSize());
60 pRet->setAnchorPoint(Vec2(0.0f, 0.0f));
61 pRet->autorelease();
62 return pRet;
63 }
64};
65
67{
68 if (TestCase::init())
69 {
70 auto listener = EventListenerTouchAllAtOnce::create();
71 listener->onTouchesBegan = CC_CALLBACK_2(MultiTouchTest::onTouchesBegan, this);
72 listener->onTouchesMoved = CC_CALLBACK_2(MultiTouchTest::onTouchesMoved, this);
73 listener->onTouchesEnded = CC_CALLBACK_2(MultiTouchTest::onTouchesEnded, this);
74 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
75
76 auto title = Label::createWithSystemFont("Please touch the screen!", "", 24);
77 title->setPosition(VisibleRect::top()+Vec2(0.0f, -40.0f));
78 addChild(title);
79
80 return true;
81 }
82 return false;
83}
84
85static Map<int, TouchPoint*> s_map;
86
87void MultiTouchTest::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
88{
89 for ( auto &item: touches )
90 {
91 auto touch = item;
92 auto location = touch->getLocation();
93 auto touchPoint = TouchPoint::touchPointWithParent(this, location, *s_TouchColors[touch->getID()%5]);
94
95 addChild(touchPoint);
96 s_map.insert(touch->getID(), touchPoint);
97 }
98}
99
100void MultiTouchTest::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
101{
102 for( auto &item: touches)
103 {
104 auto touch = item;
105 auto pTP = s_map.at(touch->getID());
106 auto location = touch->getLocation();
107
108 removeChild(pTP, true);
109 s_map.erase(touch->getID());
110
111 auto touchPointNew = TouchPoint::touchPointWithParent(this, location, *s_TouchColors[touch->getID()%5]);
112 addChild(touchPointNew);
113 s_map.insert(touch->getID(), touchPointNew);
114 }
115}
116
117void MultiTouchTest::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
118{
119 for ( auto &item: touches )
120 {
121 auto touch = item;
122 auto pTP = s_map.at(touch->getID());
123 removeChild(pTP, true);
124 s_map.erase(touch->getID());
125 }
126}
127
128void MultiTouchTest::onTouchesCancelled(const std::vector<Touch*>& touches, Event *event)
129{
130 onTouchesEnded(touches, event);
131}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
static Map< int, TouchPoint * > s_map
USING_NS_CC
static const Color3B * s_TouchColors[5]
void onTouchesMoved(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
void onTouchesCancelled(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
void onTouchesBegan(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
virtual bool init() override
virtual std::string title() const
Definition: BaseTest.h:59
static TouchPoint * touchPointWithParent(Node *pParent, const Vec2 &touchPoint, const Color3B &touchColor)
TouchPoint(const Vec2 &touchPoint, const Color3B &touchColor)
static cocos2d::Vec2 top()
Definition: VisibleRect.cpp:57