[EventDispatcher] Refactoring Touch, Touch doesn't inherit from Object now. Using std::vector<Touch*> to dispatch event and a little performance improved.

This commit is contained in:
James Chen 2013-09-12 14:26:39 +08:00
parent 7071240e30
commit 6623879f6d
5 changed files with 97 additions and 103 deletions

View File

@ -35,7 +35,7 @@ NS_CC_BEGIN
* @{ * @{
*/ */
class CC_DLL Touch : public Object class CC_DLL Touch
{ {
public: public:
/** how the touches are dispathced */ /** how the touches are dispathced */

View File

@ -35,6 +35,7 @@ class TouchEvent : public Event
{ {
public: public:
static const char* EVENT_TYPE; static const char* EVENT_TYPE;
static const int MAX_TOUCHES = 5;
enum class EventCode enum class EventCode
{ {
@ -47,6 +48,7 @@ public:
TouchEvent() TouchEvent()
: Event(EVENT_TYPE) : Event(EVENT_TYPE)
{ {
_touches.reserve(MAX_TOUCHES);
} }
EventCode getEventCode() { return _eventCode; }; EventCode getEventCode() { return _eventCode; };

View File

@ -3,25 +3,25 @@
#include "CCDirector.h" #include "CCDirector.h"
#include "cocoa/CCSet.h" #include "cocoa/CCSet.h"
#include "event_dispatcher/CCEventDispatcher.h" #include "event_dispatcher/CCEventDispatcher.h"
#include "event_dispatcher/CCTouchEvent.h"
NS_CC_BEGIN NS_CC_BEGIN
namespace { namespace {
static Touch* _touches[CC_MAX_TOUCHES] = { NULL }; static Touch* g_touches[TouchEvent::MAX_TOUCHES] = { NULL };
static unsigned int _indexBitsUsed = 0; static unsigned int g_indexBitsUsed = 0;
// System touch pointer ID (It may not be ascending order number) <-> Ascending order number from 0 // System touch pointer ID (It may not be ascending order number) <-> Ascending order number from 0
static std::map<int, int> _touchIdReorderMap; static std::map<int, int> g_touchIdReorderMap;
static int getUnUsedIndex() static int getUnUsedIndex()
{ {
int i; int i;
int temp = _indexBitsUsed; int temp = g_indexBitsUsed;
for (i = 0; i < CC_MAX_TOUCHES; i++) { for (i = 0; i < TouchEvent::MAX_TOUCHES; i++) {
if (! (temp & 0x00000001)) { if (! (temp & 0x00000001)) {
_indexBitsUsed |= (1 << i); g_indexBitsUsed |= (1 << i);
return i; return i;
} }
@ -34,14 +34,14 @@ namespace {
static void removeUsedIndexBit(int index) static void removeUsedIndexBit(int index)
{ {
if (index < 0 || index >= CC_MAX_TOUCHES) if (index < 0 || index >= TouchEvent::MAX_TOUCHES)
{ {
return; return;
} }
unsigned int temp = 1 << index; unsigned int temp = 1 << index;
temp = ~temp; temp = ~temp;
_indexBitsUsed &= temp; g_indexBitsUsed &= temp;
} }
} }
@ -203,18 +203,25 @@ const char* EGLViewProtocol::getViewName()
void EGLViewProtocol::handleTouchesBegin(int num, int ids[], float xs[], float ys[]) void EGLViewProtocol::handleTouchesBegin(int num, int ids[], float xs[], float ys[])
{ {
Set set; int id = 0;
float x = 0.0f;
float y = 0.0f;
int nUnusedIndex = 0;
_touchEvent.setValid(true);
_touchEvent._touches.clear();
for (int i = 0; i < num; ++i) for (int i = 0; i < num; ++i)
{ {
int id = ids[i]; id = ids[i];
float x = xs[i]; x = xs[i];
float y = ys[i]; y = ys[i];
auto iter = _touchIdReorderMap.find(id); auto iter = g_touchIdReorderMap.find(id);
int nUnusedIndex = 0; nUnusedIndex = 0;
// it is a new touch // it is a new touch
if (iter == _touchIdReorderMap.end()) if (iter == g_touchIdReorderMap.end())
{ {
nUnusedIndex = getUnUsedIndex(); nUnusedIndex = getUnUsedIndex();
@ -224,59 +231,57 @@ void EGLViewProtocol::handleTouchesBegin(int num, int ids[], float xs[], float y
continue; continue;
} }
Touch* pTouch = _touches[nUnusedIndex] = new Touch(); Touch* touch = g_touches[nUnusedIndex] = new Touch();
pTouch->setTouchInfo(nUnusedIndex, (x - _viewPortRect.origin.x) / _scaleX, touch->setTouchInfo(nUnusedIndex, (x - _viewPortRect.origin.x) / _scaleX,
(y - _viewPortRect.origin.y) / _scaleY); (y - _viewPortRect.origin.y) / _scaleY);
//CCLOG("x = %f y = %f", pTouch->getLocationInView().x, pTouch->getLocationInView().y); CCLOGINFO("x = %f y = %f", pTouch->getLocationInView().x, pTouch->getLocationInView().y);
_touchIdReorderMap.insert(std::make_pair(id, nUnusedIndex)); g_touchIdReorderMap.insert(std::make_pair(id, nUnusedIndex));
set.addObject(pTouch); _touchEvent._touches.push_back(touch);
} }
} }
if (set.count() == 0) if (_touchEvent._touches.size() == 0)
{ {
CCLOG("touchesBegan: count = 0"); CCLOG("touchesBegan: size = 0");
return; return;
} }
_touchEvent._eventCode = TouchEvent::EventCode::BEGAN;
TouchEvent touchEvent; EventDispatcher::getInstance()->dispatchEvent(&_touchEvent);
for (auto iter = set.begin(); iter != set.end(); ++iter)
{
touchEvent._touches.push_back(static_cast<Touch*>(*iter));
}
touchEvent._eventCode = TouchEvent::EventCode::BEGAN;
EventDispatcher::getInstance()->dispatchEvent(&touchEvent);
} }
void EGLViewProtocol::handleTouchesMove(int num, int ids[], float xs[], float ys[]) void EGLViewProtocol::handleTouchesMove(int num, int ids[], float xs[], float ys[])
{ {
Set set; int id = 0;
float x = 0.0f;
float y = 0.0f;
_touchEvent.setValid(true);
_touchEvent._touches.clear();
for (int i = 0; i < num; ++i) for (int i = 0; i < num; ++i)
{ {
int id = ids[i]; id = ids[i];
float x = xs[i]; x = xs[i];
float y = ys[i]; y = ys[i];
auto iter = _touchIdReorderMap.find(id); auto iter = g_touchIdReorderMap.find(id);
if (iter == _touchIdReorderMap.end()) if (iter == g_touchIdReorderMap.end())
{ {
CCLOG("if the index doesn't exist, it is an error"); CCLOG("if the index doesn't exist, it is an error");
continue; continue;
} }
CCLOGINFO("Moving touches with id: %d, x=%f, y=%f", id, x, y); CCLOGINFO("Moving touches with id: %d, x=%f, y=%f", id, x, y);
Touch* pTouch = _touches[iter->second]; Touch* touch = g_touches[iter->second];
if (pTouch) if (touch)
{ {
pTouch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX, touch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX,
(y - _viewPortRect.origin.y) / _scaleY); (y - _viewPortRect.origin.y) / _scaleY);
set.addObject(pTouch); _touchEvent._touches.push_back(touch);
} }
else else
{ {
@ -286,54 +291,52 @@ void EGLViewProtocol::handleTouchesMove(int num, int ids[], float xs[], float ys
} }
} }
if (set.count() == 0) if (_touchEvent._touches.size() == 0)
{ {
CCLOG("touchesMoved: count = 0"); CCLOG("touchesMoved: size = 0");
return; return;
} }
TouchEvent touchEvent; _touchEvent._eventCode = TouchEvent::EventCode::MOVED;
EventDispatcher::getInstance()->dispatchEvent(&_touchEvent);
for (auto iter = set.begin(); iter != set.end(); ++iter)
{
touchEvent._touches.push_back(static_cast<Touch*>(*iter));
}
touchEvent._eventCode = TouchEvent::EventCode::MOVED;
EventDispatcher::getInstance()->dispatchEvent(&touchEvent);
} }
void EGLViewProtocol::getSetOfTouchesEndOrCancel(Set& set, int num, int ids[], float xs[], float ys[]) void EGLViewProtocol::handleTouchesOfEndOrCancel(TouchEvent::EventCode eventCode, int num, int ids[], float xs[], float ys[])
{ {
int id = 0;
float x = 0.0f;
float y = 0.0f;
_touchEvent.setValid(true);
_touchEvent._touches.clear();
for (int i = 0; i < num; ++i) for (int i = 0; i < num; ++i)
{ {
int id = ids[i]; id = ids[i];
float x = xs[i]; x = xs[i];
float y = ys[i]; y = ys[i];
auto iter = _touchIdReorderMap.find(id); auto iter = g_touchIdReorderMap.find(id);
if (iter == _touchIdReorderMap.end()) if (iter == g_touchIdReorderMap.end())
{ {
CCLOG("if the index doesn't exist, it is an error"); CCLOG("if the index doesn't exist, it is an error");
continue; continue;
} }
/* Add to the set to send to the director */ /* Add to the set to send to the director */
Touch* pTouch = _touches[iter->second]; Touch* touch = g_touches[iter->second];
if (pTouch) if (touch)
{ {
CCLOGINFO("Ending touches with id: %d, x=%f, y=%f", id, x, y); CCLOGINFO("Ending touches with id: %d, x=%f, y=%f", id, x, y);
pTouch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX, touch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX,
(y - _viewPortRect.origin.y) / _scaleY); (y - _viewPortRect.origin.y) / _scaleY);
set.addObject(pTouch); _touchEvent._touches.push_back(touch);
// release the object g_touches[iter->second] = NULL;
pTouch->release();
_touches[iter->second] = NULL;
removeUsedIndexBit(iter->second); removeUsedIndexBit(iter->second);
_touchIdReorderMap.erase(id); g_touchIdReorderMap.erase(id);
} }
else else
{ {
@ -343,43 +346,30 @@ void EGLViewProtocol::getSetOfTouchesEndOrCancel(Set& set, int num, int ids[], f
} }
if (set.count() == 0) if (_touchEvent._touches.size() == 0)
{ {
CCLOG("touchesEnded or touchesCancel: count = 0"); CCLOG("touchesEnded or touchesCancel: size = 0");
return; return;
} }
_touchEvent._eventCode = eventCode;
EventDispatcher::getInstance()->dispatchEvent(&_touchEvent);
for (auto& touch : _touchEvent._touches)
{
// delete the touch object.
delete touch;
}
} }
void EGLViewProtocol::handleTouchesEnd(int num, int ids[], float xs[], float ys[]) void EGLViewProtocol::handleTouchesEnd(int num, int ids[], float xs[], float ys[])
{ {
Set set; handleTouchesOfEndOrCancel(TouchEvent::EventCode::ENDED, num, ids, xs, ys);
getSetOfTouchesEndOrCancel(set, num, ids, xs, ys);
TouchEvent touchEvent;
for (auto iter = set.begin(); iter != set.end(); ++iter)
{
touchEvent._touches.push_back(static_cast<Touch*>(*iter));
}
touchEvent._eventCode = TouchEvent::EventCode::ENDED;
EventDispatcher::getInstance()->dispatchEvent(&touchEvent);
} }
void EGLViewProtocol::handleTouchesCancel(int num, int ids[], float xs[], float ys[]) void EGLViewProtocol::handleTouchesCancel(int num, int ids[], float xs[], float ys[])
{ {
Set set; handleTouchesOfEndOrCancel(TouchEvent::EventCode::CANCELLED, num, ids, xs, ys);
getSetOfTouchesEndOrCancel(set, num, ids, xs, ys);
TouchEvent touchEvent;
for (auto iter = set.begin(); iter != set.end(); ++iter)
{
touchEvent._touches.push_back(static_cast<Touch*>(*iter));
}
touchEvent._eventCode = TouchEvent::EventCode::CANCELLED;
EventDispatcher::getInstance()->dispatchEvent(&touchEvent);
} }
const Rect& EGLViewProtocol::getViewPortRect() const const Rect& EGLViewProtocol::getViewPortRect() const

View File

@ -2,6 +2,9 @@
#define __CCEGLVIEWPROTOCOL_H__ #define __CCEGLVIEWPROTOCOL_H__
#include "ccTypes.h" #include "ccTypes.h"
#include "event_dispatcher/CCTouchEvent.h"
#include <vector>
enum class ResolutionPolicy enum class ResolutionPolicy
{ {
@ -30,10 +33,7 @@ enum class ResolutionPolicy
NS_CC_BEGIN NS_CC_BEGIN
#define CC_MAX_TOUCHES 5
class EGLTouchDelegate; class EGLTouchDelegate;
class Set;
/** /**
* @addtogroup platform * @addtogroup platform
@ -149,7 +149,7 @@ public:
*/ */
float getScaleY() const; float getScaleY() const;
private: private:
void getSetOfTouchesEndOrCancel(Set& set, int num, int ids[], float xs[], float ys[]); void handleTouchesOfEndOrCancel(TouchEvent::EventCode eventCode, int num, int ids[], float xs[], float ys[]);
protected: protected:
EGLTouchDelegate* _delegate; EGLTouchDelegate* _delegate;
@ -166,6 +166,8 @@ protected:
float _scaleX; float _scaleX;
float _scaleY; float _scaleY;
ResolutionPolicy _resolutionPolicy; ResolutionPolicy _resolutionPolicy;
TouchEvent _touchEvent;
}; };
// end of platform group // end of platform group

View File

@ -1,7 +1,7 @@
#include "MutiTouchTest.h" #include "MutiTouchTest.h"
static const Color3B* s_TouchColors[CC_MAX_TOUCHES] = { static const Color3B* s_TouchColors[TouchEvent::MAX_TOUCHES] = {
&Color3B::YELLOW, &Color3B::YELLOW,
&Color3B::BLUE, &Color3B::BLUE,
&Color3B::GREEN, &Color3B::GREEN,