diff --git a/cocos2dx/keyboard_dispatcher/CCKeyboardDelegate.cpp b/cocos2dx/keyboard_dispatcher/CCKeyboardDelegate.cpp deleted file mode 100755 index 9fe8e99f66..0000000000 --- a/cocos2dx/keyboard_dispatcher/CCKeyboardDelegate.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/**************************************************************************** -Copyright (c) 2010 cocos2d-x.org - -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. -****************************************************************************/ - -#include "CCKeyboardDelegate.h" -#include "ccMacros.h" - -NS_CC_BEGIN - -//------------------------------------------------------------------ -// -// CCKeyboardHandler -// -//------------------------------------------------------------------ -CCKeyboardDelegate* CCKeyboardHandler::getDelegate() -{ - return m_pDelegate; -} - -CCKeyboardHandler::~CCKeyboardHandler() -{ - if (m_pDelegate) - { - dynamic_cast(m_pDelegate)->release(); - } -} - -void CCKeyboardHandler::setDelegate(CCKeyboardDelegate *pDelegate) -{ - if (pDelegate) - { - dynamic_cast(pDelegate)->retain(); - } - - if (m_pDelegate) - { - dynamic_cast(m_pDelegate)->release(); - } - m_pDelegate = pDelegate; -} - -bool CCKeyboardHandler::initWithDelegate(CCKeyboardDelegate *pDelegate) -{ - CCAssert(pDelegate != NULL, "It's a wrong delegate!"); - - m_pDelegate = pDelegate; - dynamic_cast(pDelegate)->retain(); - - return true; -} - -CCKeyboardHandler* CCKeyboardHandler::handlerWithDelegate(CCKeyboardDelegate *pDelegate) -{ - CCKeyboardHandler* pHandler = new CCKeyboardHandler; - - if (pHandler) - { - if (pHandler->initWithDelegate(pDelegate)) - { - pHandler->autorelease(); - } - else - { - CC_SAFE_RELEASE_NULL(pHandler); - } - } - - return pHandler; -} - -NS_CC_END diff --git a/cocos2dx/keyboard_dispatcher/CCKeyboardDelegate.h b/cocos2dx/keyboard_dispatcher/CCKeyboardDelegate.h deleted file mode 100755 index c4b22b5abe..0000000000 --- a/cocos2dx/keyboard_dispatcher/CCKeyboardDelegate.h +++ /dev/null @@ -1,78 +0,0 @@ -/**************************************************************************** -Copyright (c) 2010 cocos2d-x.org - -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. -****************************************************************************/ - -#ifndef __CCKEYBOARD_DELEGATE_H__ -#define __CCKEYBOARD_DELEGATE_H__ - - -#include "cocoa/CCObject.h" - -NS_CC_BEGIN - -/** - * @addtogroup input - * @{ - */ - -class CC_DLL CCKeyboardDelegate -{ -public: - /// is called once every time a key is pressed - virtual void keyPressed(int keyCode) {} - - /// is called when a key is released - virtual void keyReleased(int keyCode) {}; -}; - -/** -@brief -CCKeyboardHandler -Object than contains the CCKeyboardDelegate. -*/ -class CC_DLL CCKeyboardHandler : public CCObject -{ -public: - virtual ~CCKeyboardHandler(void); - - /** delegate */ - CCKeyboardDelegate* getDelegate(); - void setDelegate(CCKeyboardDelegate *pDelegate); - - /** initializes a CCKeyboardHandler with a delegate */ - virtual bool initWithDelegate(CCKeyboardDelegate *pDelegate); - -public: - /** allocates a CCKeyboardHandler with a delegate */ - static CCKeyboardHandler* handlerWithDelegate(CCKeyboardDelegate *pDelegate); - -protected: - CCKeyboardDelegate* m_pDelegate; -}; - -// end of input group -/// @} - -NS_CC_END - -#endif // __CCKEYBOARD_DELEGATE_H__ diff --git a/cocos2dx/keyboard_dispatcher/CCKeyboardDispatcher.cpp b/cocos2dx/keyboard_dispatcher/CCKeyboardDispatcher.cpp index 1f18b8b294..7f1ce3c61f 100755 --- a/cocos2dx/keyboard_dispatcher/CCKeyboardDispatcher.cpp +++ b/cocos2dx/keyboard_dispatcher/CCKeyboardDispatcher.cpp @@ -33,134 +33,34 @@ NS_CC_BEGIN // //------------------------------------------------------------------ CCKeyboardDispatcher::CCKeyboardDispatcher() -: m_bLocked(false) -, m_bToAdd(false) -, m_bToRemove(false) +: m_keyPressDelegate(NULL) +, m_keyReleaseDelegate(NULL) { - m_pDelegates = CCArray::create(); - m_pDelegates->retain(); - - m_pHandlersToAdd = ccCArrayNew(8); - m_pHandlersToRemove = ccCArrayNew(8); } CCKeyboardDispatcher::~CCKeyboardDispatcher() { - CC_SAFE_RELEASE(m_pDelegates); - if (m_pHandlersToAdd) - { - ccCArrayFree(m_pHandlersToAdd); - } - - if (m_pHandlersToRemove) - { - ccCArrayFree(m_pHandlersToRemove); - } } -void CCKeyboardDispatcher::removeDelegate(CCKeyboardDelegate* pDelegate) +void CCKeyboardDispatcher::setKeyPressDelegate(CCKeyboardDelegate delegate) { - if (!pDelegate) - { - return; - } - if (! m_bLocked) - { - forceRemoveDelegate(pDelegate); - } - else - { - ccCArrayAppendValue(m_pHandlersToRemove, pDelegate); - m_bToRemove = true; - } + m_keyPressDelegate = delegate; } -void CCKeyboardDispatcher::addDelegate(CCKeyboardDelegate* pDelegate) +void CCKeyboardDispatcher::setKeyReleaseDelegate(CCKeyboardDelegate delegate) { - if (!pDelegate) - { - return; - } - - if (! m_bLocked) - { - forceAddDelegate(pDelegate); - } - else - { - ccCArrayAppendValue(m_pHandlersToAdd, pDelegate); - m_bToAdd = true; - } -} - -void CCKeyboardDispatcher::forceAddDelegate(CCKeyboardDelegate* pDelegate) -{ - CCKeyboardHandler* pHandler = CCKeyboardHandler::handlerWithDelegate(pDelegate); - - if (pHandler) - { - m_pDelegates->addObject(pHandler); - } -} - -void CCKeyboardDispatcher::forceRemoveDelegate(CCKeyboardDelegate* pDelegate) -{ - CCKeyboardHandler* pHandler = NULL; - CCObject* pObj = NULL; - CCARRAY_FOREACH(m_pDelegates, pObj) - { - pHandler = (CCKeyboardHandler*)pObj; - if (pHandler && pHandler->getDelegate() == pDelegate) - { - m_pDelegates->removeObject(pHandler); - break; - } - } + m_keyReleaseDelegate = delegate; } bool CCKeyboardDispatcher::dispatchKeyboardEvent(int keyCode, bool pressed) { - CCKeyboardHandler* pHandler = NULL; - CCKeyboardDelegate* pDelegate = NULL; - - m_bLocked = true; - - if (m_pDelegates->count() > 0) + if (pressed) { - CCObject* pObj = NULL; - CCARRAY_FOREACH(m_pDelegates, pObj) - { - CC_BREAK_IF(!pObj); - - pHandler = (CCKeyboardHandler*)pObj; - pDelegate = pHandler->getDelegate(); - - if (pressed) - pDelegate->keyPressed(keyCode); - else - pDelegate->keyReleased(keyCode); - } + m_keyPressDelegate(keyCode); } - - m_bLocked = false; - if (m_bToRemove) + else { - m_bToRemove = false; - for (unsigned int i = 0; i < m_pHandlersToRemove->num; ++i) - { - forceRemoveDelegate((CCKeyboardDelegate*)m_pHandlersToRemove->arr[i]); - } - ccCArrayRemoveAllValues(m_pHandlersToRemove); - } - - if (m_bToAdd) - { - m_bToAdd = false; - for (unsigned int i = 0; i < m_pHandlersToAdd->num; ++i) - { - forceAddDelegate((CCKeyboardDelegate*)m_pHandlersToAdd->arr[i]); - } - ccCArrayRemoveAllValues(m_pHandlersToAdd); + m_keyReleaseDelegate(keyCode); } return true; diff --git a/cocos2dx/keyboard_dispatcher/CCKeyboardDispatcher.h b/cocos2dx/keyboard_dispatcher/CCKeyboardDispatcher.h index 603f3b7482..c13e66826b 100755 --- a/cocos2dx/keyboard_dispatcher/CCKeyboardDispatcher.h +++ b/cocos2dx/keyboard_dispatcher/CCKeyboardDispatcher.h @@ -25,7 +25,8 @@ THE SOFTWARE. #ifndef __CCKEYBOARD_DISPATCHER_H__ #define __CCKEYBOARD_DISPATCHER_H__ -#include "CCKeyboardDelegate.h" +#include +#include #include "cocoa/CCArray.h" NS_CC_BEGIN @@ -35,7 +36,8 @@ NS_CC_BEGIN * @{ */ -struct _ccCArray; +typedef std::function CCKeyboardDelegate; + /** @class CCKeyboardDispatcher @brief Dispatch the keypad message from the phone @@ -47,39 +49,23 @@ public: ~CCKeyboardDispatcher(); /** - @brief add delegate to concern keypad msg + @brief set delagate to key press event */ - void addDelegate(CCKeyboardDelegate* pDelegate); + void setKeyPressDelegate(CCKeyboardDelegate delegate); + /** + @brief set delagate to key release event + */ + void setKeyReleaseDelegate(CCKeyboardDelegate delegate); /** - @brief remove the delegate from the delegates who concern keypad msg - */ - void removeDelegate(CCKeyboardDelegate* pDelegate); - - /** - @brief force add the delegate - */ - void forceAddDelegate(CCKeyboardDelegate* pDelegate); - - /** - @brief force remove the delegate - */ - void forceRemoveDelegate(CCKeyboardDelegate* pDelegate); - - /** - @brief dispatch the key pad msg + @brief dispatch the key stroke event */ bool dispatchKeyboardEvent(int keyCode, bool pressed); protected: - CCArray* m_pDelegates; - bool m_bLocked; - bool m_bToAdd; - bool m_bToRemove; - - struct _ccCArray *m_pHandlersToAdd; - struct _ccCArray *m_pHandlersToRemove; + CCKeyboardDelegate m_keyPressDelegate; + CCKeyboardDelegate m_keyReleaseDelegate; }; // end of input group diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index afa2f4aadb..531be264d4 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -296,11 +296,13 @@ void CCLayer::setKeyboardEnabled(bool enabled) CCDirector* pDirector = CCDirector::sharedDirector(); if (enabled) { - pDirector->getKeyboardDispatcher()->addDelegate(this); + pDirector->getKeyboardDispatcher()->setKeyPressDelegate(std::bind(&CCLayer::keyPressed, this, std::placeholders::_1)); + pDirector->getKeyboardDispatcher()->setKeyReleaseDelegate(std::bind(&CCLayer::keyReleased, this, std::placeholders::_1)); } else { - pDirector->getKeyboardDispatcher()->removeDelegate(this); + pDirector->getKeyboardDispatcher()->setKeyPressDelegate(NULL); + pDirector->getKeyboardDispatcher()->setKeyReleaseDelegate(NULL); } } } diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h index 45f7d3ea32..f72c741ea4 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h @@ -36,9 +36,6 @@ THE SOFTWARE. #ifdef EMSCRIPTEN #include "base_nodes/CCGLBufferedNode.h" #endif // EMSCRIPTEN -#ifdef KEYBOARD_SUPPORT -#include "keyboard_dispatcher/CCKeyboardDelegate.h" -#endif NS_CC_BEGIN @@ -63,11 +60,7 @@ All features from CCNode are valid, plus the following new features: - It can receive iPhone Touches - It can receive Accelerometer input */ -#ifdef KEYBOARD_SUPPORT -class CC_DLL CCLayer : public CCNode, public CCTouchDelegate, public CCAccelerometerDelegate, public CCKeypadDelegate, public CCKeyboardDelegate -#else class CC_DLL CCLayer : public CCNode, public CCTouchDelegate, public CCAccelerometerDelegate, public CCKeypadDelegate -#endif { public: CCLayer(); @@ -144,6 +137,8 @@ public: #ifdef KEYBOARD_SUPPORT virtual bool isKeyboardEnabled(); virtual void setKeyboardEnabled(bool value); + virtual void keyPressed(int keyCode) {}; + virtual void keyReleased(int keyCode) {}; #endif virtual bool isKeypadEnabled(); virtual void setKeypadEnabled(bool value); diff --git a/cocos2dx/proj.linux/Makefile b/cocos2dx/proj.linux/Makefile index fe770b59e0..ad3c5a3677 100644 --- a/cocos2dx/proj.linux/Makefile +++ b/cocos2dx/proj.linux/Makefile @@ -42,7 +42,6 @@ SOURCES = ../actions/CCAction.cpp \ ../effects/CCGrid.cpp \ ../keypad_dispatcher/CCKeypadDelegate.cpp \ ../keypad_dispatcher/CCKeypadDispatcher.cpp \ -../keyboard_dispatcher/CCKeyboardDelegate.cpp \ ../keyboard_dispatcher/CCKeyboardDispatcher.cpp \ ../label_nodes/CCLabelAtlas.cpp \ ../label_nodes/CCLabelBMFont.cpp \