轉載請註明來自_鞋男blog :http://blog.csdn.net/wushao126/article/details/40374395
cocos2.2.5還在編譯依賴庫,還是先看3.2的吧
環境:win8 + vs2013
時間有限 還是先說說這個把
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
#define CC_DEPRECATED_ATTRIBUTE __attribute__((deprecated))
#elif _MSC_VER >= 1400 //vs 2005 or higher
#define CC_DEPRECATED_ATTRIBUTE __declspec(deprecated)
#else
#define CC_DEPRECATED_ATTRIBUTE
#endif
看不懂也沒關係,主要是提醒大家 但凡函數加了CC_DEPRECATED_ATTRIBUTE
表示廢棄了的意思,也只能在3.x中才能看到,想跟下去看看怎麼實現,水平有限,跟不了。誰知道告訴我一聲,先謝了。但是有些函數沒有加這個修飾,但是在編譯的過程中也提示廢棄,以後再深入研究。
CC_DEPRECATED_ATTRIBUTE virtual bool ccTouchBegan(Touch *pTouch, Event *pEvent) final {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent); return false;};
CC_DEPRECATED_ATTRIBUTE virtual void ccTouchMoved(Touch *pTouch, Event *pEvent) final {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
CC_DEPRECATED_ATTRIBUTE virtual void ccTouchEnded(Touch *pTouch, Event *pEvent) final {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
CC_DEPRECATED_ATTRIBUTE virtual void ccTouchCancelled(Touch *pTouch, Event *pEvent) final {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
CC_DEPRECATED_ATTRIBUTE virtual void ccTouchesBegan(__Set *pTouches, Event *pEvent) final {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
CC_DEPRECATED_ATTRIBUTE virtual void ccTouchesMoved(__Set *pTouches, Event *pEvent) final {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
CC_DEPRECATED_ATTRIBUTE virtual void ccTouchesEnded(__Set *pTouches, Event *pEvent) final {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
CC_DEPRECATED_ATTRIBUTE virtual void ccTouchesCancelled(__Set *pTouches, Event *pEvent) final {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
被廢棄了,爲什麼被廢棄了(還是可以用吧),下面會講的
virtual bool onTouchBegan(Touch *touch, Event *unused_event);
virtual void onTouchMoved(Touch *touch, Event *unused_event);
virtual void onTouchEnded(Touch *touch, Event *unused_event);
virtual void onTouchCancelled(Touch *touch, Event *unused_event);
virtual void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event);
virtual void onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event);
virtual void onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event);
virtual void onTouchesCancelled(const std::vector<Touch*>&touches, Event *unused_event);
註冊監聽:
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(MainLayer::onTouchBegan, this);
listener->onTouchEnded = CC_CALLBACK_2(MainLayer::onTouchEnded, this);
listener->onTouchMoved = CC_CALLBACK_2(MainLayer::onTouchMove, this);//其實這裏也可以用lambda去寫
listener->onTouchMoved = [](const std::vector<Touch*>&touches, Event *unused_event)->bool{ /* 函數體*/}
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);//默認優先級0 ,
創建listener對象,根據listenerID映射listeners容器,將listener對象存入容器中
void EventDispatcher::addEventListener(EventListener* listener)
{
if (_inDispatch == 0)
{
forceAddEventListener(listener);
}
else
{
_toAddedListeners.push_back(listener);
}
listener->retain();
}
之後,在_nodeListenersMap中建立 node(target)與關聯的listener的映射
void EventDispatcher::associateNodeAndEventListener(Node* node, EventListener* listener)
{
std::vector<EventListener*>* listeners = nullptr;
auto found = _nodeListenersMap.find(node);
if (found != _nodeListenersMap.end())
{
listeners = found->second;
}
else
{
listeners = new std::vector<EventListener*>();
_nodeListenersMap.insert(std::make_pair(node, listeners));
}
listeners->push_back(listener);
}
另一個種
_eventDispatcher->addEventListenerWithFixedPriority(listener, Priority);//
我這裏發現了個問題
void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority)
{
CCASSERT(listener, "Invalid parameters.");
CCASSERT(!listener->isRegistered(), "The listener has been registered.");
CCASSERT(fixedPriority != 0, "0 priority is forbidden for fixed priority since it's used for scene graph based priority.");
if (!listener->checkAvailable())
return;
listener->setAssociatedNode(nullptr);
listener->setFixedPriority(fixedPriority);
listener->setRegistered(true);
listener->setPaused(false);
addEventListener(listener);
}//我 TM的超級鬱悶 ,設置非0吧,assert了,設置〇吧,沒有node ,甭的更厲害
//無奈之下注釋了第三行,OK了 唉只能說 被白fuck了一個小時,還能說了什麼呢
此不綁定target,所以需要手動釋放
_eventDispatcher->removeEventListener(listener),此處沒有根據nonde
第三
_eventDispatcher->addCustomEventListener("custom", [](EventCustom*)->void{ log("a event");});
EventListenerCustom* EventDispatcher::addCustomEventListener(const std::string &eventName, const std::function<void(EventCustom*)>& callback)
{
EventListenerCustom *listener = EventListenerCustom::create(eventName, callback);//在這裏創建listener
addEventListenerWithFixedPriority(listener, 1);
return listener;
}
測試了下,編譯過了,但是點擊沒有反應,知道這個意思就好了 以後在看了
現在看看事件分發:
glview->pollEvents();
。。。。。。//物理事件傳遞
class GLFWEventHandler
{
public:
static void onGLFWError(int errorID, const char* errorDesc)
{
if (_view)
_view->onGLFWError(errorID, errorDesc);
}
static void onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify)
{
if (_view)
_view->onGLFWMouseCallBack(window, button, action, modify);
}
static void onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
{
if (_view)
_view->onGLFWMouseMoveCallBack(window, x, y);
}
static void onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y)
{
if (_view)
_view->onGLFWMouseScrollCallback(window, x, y);
}
static void onGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (_view)
_view->onGLFWKeyCallback(window, key, scancode, action, mods);
}
static void onGLFWCharCallback(GLFWwindow* window, unsigned int character)
{
if (_view)
_view->onGLFWCharCallback(window, character);
}
static void onGLFWWindowPosCallback(GLFWwindow* windows, int x, int y)
{
if (_view)
_view->onGLFWWindowPosCallback(windows, x, y);
}
static void onGLFWframebuffersize(GLFWwindow* window, int w, int h)
{
if (_view)
_view->onGLFWframebuffersize(window, w, h);
}
static void onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height)
{
if (_view)
_view->onGLFWWindowSizeFunCallback(window, width, height);
}
static void setGLView(GLView* view)
{
_view = view;
}
private:
static GLView* _view;
};
我是用鼠標點的 響應了鼠標的那個函數調用,根據鼠標事件調用了void GLViewProtocol::handleTouchesEnd(int num, intptr_t ids[], float xs[], float ys[])
{
handleTouchesOfEndOrCancel(EventTouch::EventCode::ENDED, num, ids, xs, ys);
}
接下來纔到我們之前註冊的事件TouchEvent
void EventDispatcher::dispatchEvent(Event* event){
這裏面乾的是
對事件排序,按優先級。
auto oneByOneListeners = getListeners(EventListenerTouchOneByOne::LISTENER_ID);
auto allAtOnceListeners = getListeners(EventListenerTouchAllAtOnce::LISTENER_ID);
得到各個TouchMode的Listeners:
auto oneByOneListeners = getListeners(EventListenerTouchOneByOne::LISTENER_ID);
auto allAtOnceListeners = getListeners(EventListenerTouchAllAtOnce::LISTENER_ID);
就是各種超找匹配找出是否有註冊的Event: if (oneByOneListeners)
{
auto mutableTouchesIter = mutableTouches.begin();
auto touchesIter = originalTouches.begin();
for (; touchesIter != originalTouches.end(); ++touchesIter)
{
bool isSwallowed = false;
auto onTouchEvent = [&](EventListener* l) -> bool { // Return true to break
EventListenerTouchOneByOne* listener = static_cast<EventListenerTouchOneByOne*>(l);
// Skip if the listener was removed.
if (!listener->_isRegistered)
return false;
event->setCurrentTarget(listener->_node);
bool isClaimed = false;
std::vector<Touch*>::iterator removedIter;
EventTouch::EventCode eventCode = event->getEventCode();
if (eventCode == EventTouch::EventCode::BEGAN)
{
if (listener->onTouchBegan)
{
isClaimed = listener->onTouchBegan(*touchesIter, event);
if (isClaimed && listener->_isRegistered)
{
listener->_claimedTouches.push_back(*touchesIter);
}
}
}
else if (listener->_claimedTouches.size() > 0
&& ((removedIter = std::find(listener->_claimedTouches.begin(), listener->_claimedTouches.end(), *touchesIter)) != listener->_claimedTouches.end()))
{
isClaimed = true;
switch (eventCode)
{
case EventTouch::EventCode::MOVED:
if (listener->onTouchMoved)
{
listener->onTouchMoved(*touchesIter, event);
}
break;
case EventTouch::EventCode::ENDED:
if (listener->onTouchEnded)
{
listener->onTouchEnded(*touchesIter, event);
}
if (listener->_isRegistered)
{
listener->_claimedTouches.erase(removedIter);
}
break;
case EventTouch::EventCode::CANCELLED:
if (listener->onTouchCancelled)
{
listener->onTouchCancelled(*touchesIter, event);
}
if (listener->_isRegistered)
{
listener->_claimedTouches.erase(removedIter);
}
break;
default:
CCASSERT(false, "The eventcode is invalid.");
break;
}
}
// If the event was stopped, return directly.
if (event->isStopped())
{
updateListeners(event);
return true;
}
CCASSERT((*touchesIter)->getID() == (*mutableTouchesIter)->getID(), "");
if (isClaimed && listener->_isRegistered && listener->_needSwallow)
{
if (isNeedsMutableSet)
{
mutableTouchesIter = mutableTouches.erase(mutableTouchesIter);
isSwallowed = true;
}
return true;
}
return false;
};
//
dispatchEventToListeners(oneByOneListeners, onTouchEvent);//主要在這裏 這裏用了lambda,執行循序要搞清楚了
if (event->isStopped())
{
return;
}
if (!isSwallowed)
++mutableTouchesIter;
}
}
void EventDispatcher::dispatchEventToListeners(EventListenerVector* listeners, const std::function<bool(EventListener*)>& onEvent)
這個函數告訴我們了 若匹配到了不想下分發了
然後進入那個lambda函數裏執行註冊過的touch系列函數了。執行完了還會在下一個循環刪除這一事件
現在講講ccs 2.2.x ,一直在發佈新版本,ccs現在完善了不少,我們就拿最新的來將好了,ccs 3.0x之前的版本大家都熟悉了,不熟悉,網上教程一大把,我這裏略微補充下
類:CCTouchDispatcher
void CCTouchDispatcher::addStandardDelegate(CCTouchDelegate *pDelegate, int nPriority)
{
CCTouchHandler *pHandler = CCStandardTouchHandler::handlerWithDelegate(pDelegate, nPriority);
if (! m_bLocked)
{
forceAddHandler(pHandler, m_pStandardHandlers);
}
else
{
/* If pHandler is contained in m_pHandlersToRemove, if so remove it from m_pHandlersToRemove and return.
* Refer issue #752(cocos2d-x)
*/
if (ccCArrayContainsValue(m_pHandlersToRemove, pDelegate))
{
ccCArrayRemoveValue(m_pHandlersToRemove, pDelegate);
return;
}
m_pHandlersToAdd->addObject(pHandler);
m_bToAdd = true;
}
}
我們經常重寫void CCLayer::registerWithTouchDispatcher(),然後調用上面的註冊函數,
void CCLayer::registerWithTouchDispatcher()
{
CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher();
// Using LuaBindings
if (m_pScriptTouchHandlerEntry)
{
if (m_pScriptTouchHandlerEntry->isMultiTouches())
{
pDispatcher->addStandardDelegate(this, 0);
LUALOG("[LUA] Add multi-touches event handler: %d", m_pScriptTouchHandlerEntry->getHandler());
}
else
{
pDispatcher->addTargetedDelegate(this,
m_pScriptTouchHandlerEntry->getPriority(),
m_pScriptTouchHandlerEntry->getSwallowsTouches());
LUALOG("[LUA] Add touch event handler: %d", m_pScriptTouchHandlerEntry->getHandler());
}
}
else
{
if( m_eTouchMode == kCCTouchesAllAtOnce ) {
pDispatcher->addStandardDelegate(this, 0);
} else {
pDispatcher->addTargetedDelegate(this, m_nTouchPriority, true);
}
}
}
從上面代碼可以看出如果我們改變m_eTouchMode就可以修改註冊模式,完成這個使命的函數CCLayer::setTouchMode( ... )
但是一定要注意調用的順序,我們都知道在layer切換的時候先是init(),然後onEnter(),onEnterTransitionDidFinish()
在CCDirect類中有這樣的一代碼可以看出
m_pRunningScene->onEnter();
m_pRunningScene->onEnterTransitionDidFinish();
我們都知道registerWithTouchDispatch()是在CCLayer的onEnter裏面調用,我們只需init()或者重寫onEnter(){ setTouchMode(...) ,CCLayer::onEnter();},OK!
還有一點:
void CCTouchDispatcher::addStandardDelegate(CCTouchDelegate *pDelegate, int nPriority)
{
CCTouchHandler *pHandler = CCStandardTouchHandler::handlerWithDelegate(pDelegate, nPriority);
if (! m_bLocked)
{
forceAddHandler(pHandler, m_pStandardHandlers);
}
else
{
/* If pHandler is contained in m_pHandlersToRemove, if so remove it from m_pHandlersToRemove and return.
* Refer issue #752(cocos2d-x)
*/
if (ccCArrayContainsValue(m_pHandlersToRemove, pDelegate))
{
ccCArrayRemoveValue(m_pHandlersToRemove, pDelegate);
return;
}
m_pHandlersToAdd->addObject(pHandler);
m_bToAdd = true;
}
}
從參數類型看,class CC_DLL CCTouchDelegate
{
public:
CCTouchDelegate() {}
virtual ~CCTouchDelegate()
{
}
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent); return false;};
// optional
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
// optional
virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
};
我們已經知道他們想幹什麼事情了。protected:
CCArray* m_pTargetedHandlers;
CCArray* m_pStandardHandlers;
bool m_bLocked;
...
CCArray* m_pHandlersToAdd;
struct _ccCArray *m_pHandlersToRemove;
...
從CCTouchDispatcher的數據結構來看,主要是兩個容器CCArray* m_pTargetedHandlers; CCArray* m_pStandardHandlers; m_bLocked
m_pHandlersToAdd;m_pHandlersToRemove主要是同步安全機制
註冊好了 就是分發了,主要算法都在 CCTouchDispatcher::touches(cocos2d::CCSet * pTouches, cocos2d::CCEvent * pEvent, unsigned int uIndex) ,分類處理
switch (sHelper.m_type)
{
case CCTOUCHBEGAN:
pHandler->getDelegate()->ccTouchesBegan(pMutableTouches, pEvent);
break;
case CCTOUCHMOVED:
pHandler->getDelegate()->ccTouchesMoved(pMutableTouches, pEvent);
break;
case CCTOUCHENDED:
pHandler->getDelegate()->ccTouchesEnded(pMutableTouches, pEvent);
break;
case CCTOUCHCANCELLED:
pHandler->getDelegate()->ccTouchesCancelled(pMutableTouches, pEvent);
break;
}
暫時就寫到這了,若有錯誤,還請留言指出,謝謝