simplified CCKeyboardDispatcher and CCLayer does not inherit from CCKeyboardDelegate

This commit is contained in:
Rene Klacan 2013-06-03 16:06:33 +02:00
parent 9e4c5bbca3
commit c4ec15752a
7 changed files with 29 additions and 316 deletions

View File

@ -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<CCObject*>(m_pDelegate)->release();
}
}
void CCKeyboardHandler::setDelegate(CCKeyboardDelegate *pDelegate)
{
if (pDelegate)
{
dynamic_cast<CCObject*>(pDelegate)->retain();
}
if (m_pDelegate)
{
dynamic_cast<CCObject*>(m_pDelegate)->release();
}
m_pDelegate = pDelegate;
}
bool CCKeyboardHandler::initWithDelegate(CCKeyboardDelegate *pDelegate)
{
CCAssert(pDelegate != NULL, "It's a wrong delegate!");
m_pDelegate = pDelegate;
dynamic_cast<CCObject*>(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

View File

@ -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__

View File

@ -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;

View File

@ -25,7 +25,8 @@ THE SOFTWARE.
#ifndef __CCKEYBOARD_DISPATCHER_H__
#define __CCKEYBOARD_DISPATCHER_H__
#include "CCKeyboardDelegate.h"
#include <vector>
#include <functional>
#include "cocoa/CCArray.h"
NS_CC_BEGIN
@ -35,7 +36,8 @@ NS_CC_BEGIN
* @{
*/
struct _ccCArray;
typedef std::function<void(int)> 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

View File

@ -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);
}
}
}

View File

@ -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);

View File

@ -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 \