Merge pull request #4335 from Dhilan007/jssubmodule

[ci skip]recover controller on layer.
This commit is contained in:
minggo 2013-11-19 01:35:58 -08:00
commit 11d43e4652
3 changed files with 415 additions and 3 deletions

View File

@ -48,6 +48,14 @@ NS_CC_BEGIN
// Layer
Layer::Layer()
: _touchEnabled(false)
, _accelerometerEnabled(false)
, _keyboardEnabled(false)
, _touchMode(Touch::DispatchMode::ALL_AT_ONCE)
, _swallowsTouches(true)
, _touchListener(nullptr)
, _keyboardListener(nullptr)
, _accelerationListener(nullptr)
{
_ignoreAnchorPointForPosition = true;
setAnchorPoint(Point(0.5f, 0.5f));
@ -87,6 +95,343 @@ Layer *Layer::create()
}
}
/// Touch and Accelerometer related
void Layer::_addTouchListener()
{
if (_touchListener != nullptr)
return;
if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE )
{
// Register Touch Event
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = [=](const std::vector<Touch*>& touches, Event* event){
if (kScriptTypeNone != _scriptType)
{
TouchesScriptData data(EventTouch::EventCode::BEGAN, this, touches);
ScriptEvent event(kTouchesEvent, &data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
return;
}
CC_UNUSED_PARAM(touches);
CC_UNUSED_PARAM(event);
};
listener->onTouchesMoved = [=](const std::vector<Touch*>& touches, Event* event){
if (kScriptTypeNone != _scriptType)
{
TouchesScriptData data(EventTouch::EventCode::MOVED, this, touches);
ScriptEvent event(kTouchesEvent, &data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
return;
}
CC_UNUSED_PARAM(touches);
CC_UNUSED_PARAM(event);
};
listener->onTouchesEnded = [=](const std::vector<Touch*>& touches, Event* event){
if (kScriptTypeNone != _scriptType)
{
TouchesScriptData data(EventTouch::EventCode::ENDED, this, touches);
ScriptEvent event(kTouchesEvent, &data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
return;
}
CC_UNUSED_PARAM(touches);
CC_UNUSED_PARAM(event);
};
listener->onTouchesCancelled = [=](const std::vector<Touch*>& touches, Event* event){
if (kScriptTypeNone != _scriptType)
{
TouchesScriptData data(EventTouch::EventCode::CANCELLED, this, touches);
ScriptEvent event(kTouchesEvent, &data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
return;
}
CC_UNUSED_PARAM(touches);
CC_UNUSED_PARAM(event);
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
_touchListener = listener;
}
else
{
// Register Touch Event
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(_swallowsTouches);
listener->onTouchBegan = [=](Touch* touch, Event* event){
if (kScriptTypeNone != _scriptType)
{
TouchScriptData data(EventTouch::EventCode::BEGAN, this, touch);
ScriptEvent event(kTouchEvent, &data);
if(ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event))
return true;
else
return false;
}
return false;
};
listener->onTouchMoved = [=](Touch* touch, Event* event){
if (kScriptTypeNone != _scriptType)
{
TouchScriptData data(EventTouch::EventCode::MOVED, this, touch);
ScriptEvent event(kTouchEvent, &data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
}
};
listener->onTouchEnded = [=](Touch* touch, Event* event){
if (kScriptTypeNone != _scriptType)
{
TouchScriptData data(EventTouch::EventCode::ENDED, this, touch);
ScriptEvent event(kTouchEvent, &data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
}
};
listener->onTouchCancelled = [=](Touch* touch, Event* event){
if (kScriptTypeNone != _scriptType)
{
TouchScriptData data(EventTouch::EventCode::CANCELLED, this, touch);
ScriptEvent event(kTouchEvent, &data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
}
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
_touchListener = listener;
}
}
/// isTouchEnabled getter
bool Layer::isTouchEnabled() const
{
return _touchEnabled;
}
/// isTouchEnabled setter
void Layer::setTouchEnabled(bool enabled)
{
if (_touchEnabled != enabled)
{
_touchEnabled = enabled;
if (enabled)
{
this->_addTouchListener();
}
else
{
_eventDispatcher->removeEventListener(_touchListener);
_touchListener = nullptr;
}
}
}
void Layer::setTouchMode(Touch::DispatchMode mode)
{
if(_touchMode != mode)
{
_touchMode = mode;
if( _touchEnabled)
{
_eventDispatcher->removeEventListener(_touchListener);
_touchListener = nullptr;
this->_addTouchListener();
}
}
}
void Layer::setSwallowsTouches(bool swallowsTouches)
{
if (_swallowsTouches != swallowsTouches)
{
_swallowsTouches = swallowsTouches;
if( _touchEnabled)
{
_eventDispatcher->removeEventListener(_touchListener);
_touchListener = nullptr;
this->_addTouchListener();
}
}
}
Touch::DispatchMode Layer::getTouchMode() const
{
return _touchMode;
}
bool Layer::isSwallowsTouches() const
{
return _swallowsTouches;
}
/// isAccelerometerEnabled getter
bool Layer::isAccelerometerEnabled() const
{
return _accelerometerEnabled;
}
/// isAccelerometerEnabled setter
void Layer::setAccelerometerEnabled(bool enabled)
{
if (enabled != _accelerometerEnabled)
{
_accelerometerEnabled = enabled;
Device::setAccelerometerEnabled(enabled);
if (_running)
{
_eventDispatcher->removeEventListener(_accelerationListener);
_accelerationListener = nullptr;
if (enabled)
{
//Not to use onAcceleration for avoid warn from deprecated api
_accelerationListener = EventListenerAcceleration::create([=](Acceleration* acc, Event* event){
CC_UNUSED_PARAM(acc);
if(kScriptTypeNone != _scriptType)
{
BasicScriptData data(this,(void*)acc);
ScriptEvent event(kAccelerometerEvent,&data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
}
});
_eventDispatcher->addEventListenerWithSceneGraphPriority(_accelerationListener, this);
}
}
}
}
void Layer::setAccelerometerInterval(double interval) {
if (_accelerometerEnabled)
{
if (_running)
{
Device::setAccelerometerInterval(interval);
}
}
}
void Layer::onAcceleration(Acceleration* pAccelerationValue, Event* event)
{
CC_UNUSED_PARAM(pAccelerationValue);
if(kScriptTypeNone != _scriptType)
{
BasicScriptData data(this,(void*)pAccelerationValue);
ScriptEvent event(kAccelerometerEvent,&data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
}
}
void Layer::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event)
{
CC_UNUSED_PARAM(keyCode);
CC_UNUSED_PARAM(event);
}
void Layer::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event)
{
CC_UNUSED_PARAM(event);
if(kScriptTypeNone != _scriptType)
{
KeypadScriptData data(keyCode, this);
ScriptEvent event(kKeypadEvent,&data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
}
}
/// isKeyboardEnabled getter
bool Layer::isKeyboardEnabled() const
{
return _keyboardEnabled;
}
/// isKeyboardEnabled setter
void Layer::setKeyboardEnabled(bool enabled)
{
if (enabled != _keyboardEnabled)
{
_keyboardEnabled = enabled;
_eventDispatcher->removeEventListener(_keyboardListener);
_keyboardListener = nullptr;
if (enabled)
{
auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event){
CC_UNUSED_PARAM(keyCode);
CC_UNUSED_PARAM(event);
};
listener->onKeyReleased = [this](EventKeyboard::KeyCode keyCode, Event* event){
CC_UNUSED_PARAM(event);
if(kScriptTypeNone != _scriptType)
{
KeypadScriptData data(keyCode, this);
ScriptEvent event(kKeypadEvent,&data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
}
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
_keyboardListener = listener;
}
}
}
void Layer::setKeypadEnabled(bool enabled)
{
if (enabled != _keyboardEnabled)
{
_keyboardEnabled = enabled;
_eventDispatcher->removeEventListener(_keyboardListener);
_keyboardListener = nullptr;
if (enabled)
{
auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event){
CC_UNUSED_PARAM(keyCode);
CC_UNUSED_PARAM(event);
};
listener->onKeyReleased = [this](EventKeyboard::KeyCode keyCode, Event* event){
CC_UNUSED_PARAM(event);
if(kScriptTypeNone != _scriptType)
{
KeypadScriptData data(keyCode, this);
ScriptEvent event(kKeypadEvent,&data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
}
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
_keyboardListener = listener;
}
}
}
// LayerRGBA
LayerRGBA::LayerRGBA()
: _displayedOpacity(255)

View File

@ -46,6 +46,10 @@ NS_CC_BEGIN
class TouchScriptHandlerEntry;
class EventListenerTouch;
class EventListenerKeyboard;
class EventListenerAcceleration;
//
// Layer
//
@ -82,10 +86,23 @@ public:
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);}
// Deprecated touch callbacks.There not have add CC_DEPRECATED_ATTRIBUTE because of menu still use these api
virtual bool onTouchBegan(Touch *touch, Event *event) {CC_UNUSED_PARAM(touch); CC_UNUSED_PARAM(event); return false;}
virtual void onTouchMoved(Touch *touch, Event *event) {CC_UNUSED_PARAM(touch); CC_UNUSED_PARAM(event);}
virtual void onTouchEnded(Touch *touch, Event *event) {CC_UNUSED_PARAM(touch); CC_UNUSED_PARAM(event);}
virtual void onTouchCancelled(Touch *touch, Event *event) {CC_UNUSED_PARAM(touch); CC_UNUSED_PARAM(event);}
// Deprecated touch callbacks.
CC_DEPRECATED_ATTRIBUTE virtual void onTouchesBegan(const std::vector<Touch*>& touches, Event *event) {CC_UNUSED_PARAM(touches); CC_UNUSED_PARAM(event);}
CC_DEPRECATED_ATTRIBUTE virtual void onTouchesMoved(const std::vector<Touch*>& touches, Event *event) {CC_UNUSED_PARAM(touches); CC_UNUSED_PARAM(event);}
CC_DEPRECATED_ATTRIBUTE virtual void onTouchesEnded(const std::vector<Touch*>& touches, Event *event) {CC_UNUSED_PARAM(touches); CC_UNUSED_PARAM(event);}
CC_DEPRECATED_ATTRIBUTE virtual void onTouchesCancelled(const std::vector<Touch*>&touches, Event *event) {CC_UNUSED_PARAM(touches); CC_UNUSED_PARAM(event);}
/** @deprecated Please override onAcceleration */
CC_DEPRECATED_ATTRIBUTE virtual void didAccelerate(Acceleration* accelerationValue) final {};
CC_DEPRECATED_ATTRIBUTE virtual void onAcceleration(Acceleration* acc, Event* event);
/** If isTouchEnabled, this method is called onEnter. Override it to change the
way Layer receives touch events.
( Default: TouchDispatcher::sharedDispatcher()->addStandardDelegate(this,0); )
@ -98,6 +115,36 @@ public:
*/
CC_DEPRECATED_ATTRIBUTE virtual void registerWithTouchDispatcher() final {};
/** whether or not it will receive Touch events.
You can enable / disable touch events with this property.
Only the touches of this node will be affected. This "method" is not propagated to it's children.
@since v0.8.1
*/
CC_DEPRECATED_ATTRIBUTE virtual bool isTouchEnabled() const;
CC_DEPRECATED_ATTRIBUTE virtual void setTouchEnabled(bool value);
CC_DEPRECATED_ATTRIBUTE virtual void setTouchMode(Touch::DispatchMode mode);
CC_DEPRECATED_ATTRIBUTE virtual Touch::DispatchMode getTouchMode() const;
/** swallowsTouches of the touch events. Default is true */
CC_DEPRECATED_ATTRIBUTE virtual void setSwallowsTouches(bool swallowsTouches);
CC_DEPRECATED_ATTRIBUTE virtual bool isSwallowsTouches() const;
/** whether or not it will receive Accelerometer events
You can enable / disable accelerometer events with this property.
@since v0.8.1
*/
CC_DEPRECATED_ATTRIBUTE virtual bool isAccelerometerEnabled() const;
CC_DEPRECATED_ATTRIBUTE virtual void setAccelerometerEnabled(bool value);
CC_DEPRECATED_ATTRIBUTE virtual void setAccelerometerInterval(double interval);
/** whether or not it will receive keyboard or keypad events
You can enable / disable accelerometer events with this property.
it's new in cocos2d-x
*/
CC_DEPRECATED_ATTRIBUTE virtual bool isKeyboardEnabled() const;
CC_DEPRECATED_ATTRIBUTE virtual void setKeyboardEnabled(bool value);
/** Please use onKeyPressed instead. */
virtual void keyPressed(int keyCode) final {};
@ -105,6 +152,11 @@ public:
/** Please use onKeyReleased instead. */
virtual void keyReleased(int keyCode) final {};
CC_DEPRECATED_ATTRIBUTE virtual void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event);
CC_DEPRECATED_ATTRIBUTE virtual void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event);
CC_DEPRECATED_ATTRIBUTE virtual bool isKeypadEnabled() const final { return _keyboardEnabled; };
CC_DEPRECATED_ATTRIBUTE virtual void setKeypadEnabled(bool value);
/** @deprecated Please override onKeyReleased and check the keycode of KeyboardEvent::KeyCode::Menu(KEY_BACKSPACE) instead. */
CC_DEPRECATED_ATTRIBUTE virtual void keyBackClicked() final {};
@ -112,6 +164,23 @@ public:
//
// Overrides
//
protected:
CC_DEPRECATED_ATTRIBUTE void addTouchListener() { _addTouchListener();};
bool _touchEnabled;
bool _accelerometerEnabled;
bool _keyboardEnabled;
EventListener* _touchListener;
EventListenerKeyboard* _keyboardListener;
EventListenerAcceleration* _accelerationListener;
private:
//add the api for avoid use deprecated api
void _addTouchListener();
Touch::DispatchMode _touchMode;
bool _swallowsTouches;
};
#ifdef __apple__

View File

@ -151,8 +151,6 @@ public:
bool slice(PhysicsWorld& world, const PhysicsRayCastInfo& info, void* data);
void clipPoly(PhysicsShapePolygon* shape, Point normal, float distance);
bool onTouchBegan(Touch *touch, Event *event);
void onTouchMoved(Touch *touch, Event *event);
void onTouchEnded(Touch *touch, Event *event);
private: