Merge pull request #3051 from samuele3hu/execDev

issue #2244:make some execute funs into one fun in ScriptEngineProtocol
This commit is contained in:
minggo 2013-07-04 03:33:12 -07:00
commit 7bcafb46a4
18 changed files with 729 additions and 86 deletions

View File

@ -160,9 +160,11 @@ void Timer::update(float dt)
(_target->*_selector)(_elapsed);
}
if (_scriptHandler)
if (0 != _scriptHandler)
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeSchedule(_scriptHandler, _elapsed);
SchedulerScriptData data(_scriptHandler,_elapsed);
ScriptEvent event(kScheduleEvent,&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
_elapsed = 0;
}
@ -179,9 +181,11 @@ void Timer::update(float dt)
(_target->*_selector)(_elapsed);
}
if (_scriptHandler)
if (0 != _scriptHandler)
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeSchedule(_scriptHandler, _elapsed);
SchedulerScriptData data(_scriptHandler,_elapsed);
ScriptEvent event(kScheduleEvent,&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
_elapsed = _elapsed - _delay;
@ -198,9 +202,11 @@ void Timer::update(float dt)
(_target->*_selector)(_elapsed);
}
if (_scriptHandler)
if (0 != _scriptHandler)
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeSchedule(_scriptHandler, _elapsed);
SchedulerScriptData data(_scriptHandler,_elapsed);
ScriptEvent event(kScheduleEvent,&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
_elapsed = 0;

View File

@ -560,8 +560,10 @@ void CallFunc::execute() {
(_selectorTarget->*_callFunc)();
} else if( _function )
_function();
if (_scriptHandler) {
ScriptEngineManager::sharedManager()->getScriptEngine()->executeCallFuncActionEvent(this);
if (0 != _scriptHandler) {
BasicScriptData data((void*)&_scriptHandler);
ScriptEvent event(kCallFuncEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
}

View File

@ -542,7 +542,14 @@ void Node::cleanup()
this->stopAllActions();
this->unscheduleAllSelectors();
if ( _scriptType != kScriptTypeNone)
if ( _scriptType == kScriptTypeLua)
{
int action = kNodeOnCleanup;
BasicScriptData data((void*)this,(void*)&action);
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent);
}
else if(_scriptType != kScriptTypeJavascript)
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this, kNodeOnCleanup);
}
@ -914,7 +921,14 @@ void Node::onEnter()
_running = true;
if (_scriptType != kScriptTypeNone)
if (_scriptType == kScriptTypeLua)
{
int action = kNodeOnEnter;
BasicScriptData data((void*)this,(void*)&action);
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent);
}
else if(_scriptType == kScriptTypeJavascript)
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this, kNodeOnEnter);
}
@ -926,7 +940,14 @@ void Node::onEnterTransitionDidFinish()
arrayMakeObjectsPerformSelector(_children, onEnterTransitionDidFinish, Node*);
if (_scriptType == kScriptTypeJavascript)
if (_scriptType == kScriptTypeLua)
{
int action = kNodeOnEnterTransitionDidFinish;
BasicScriptData data((void*)this,(void*)&action);
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent);
}
else if (_scriptType == kScriptTypeJavascript)
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this, kNodeOnEnterTransitionDidFinish);
}
@ -935,8 +956,13 @@ void Node::onEnterTransitionDidFinish()
void Node::onExitTransitionDidStart()
{
arrayMakeObjectsPerformSelector(_children, onExitTransitionDidStart, Node*);
if (_scriptType == kScriptTypeJavascript)
if (_scriptType == kScriptTypeLua)
{
int action = kNodeOnExitTransitionDidStart;
BasicScriptData data((void*)this,(void*)&action);
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent); }
else if (_scriptType == kScriptTypeJavascript)
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this, kNodeOnExitTransitionDidStart);
}
@ -947,8 +973,14 @@ void Node::onExit()
this->pauseSchedulerAndActions();
_running = false;
if ( _scriptType != kScriptTypeNone)
if (_scriptType == kScriptTypeLua)
{
int action = kNodeOnExit;
BasicScriptData data((void*)this,(void*)&action);
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent);
}
else if ( _scriptType == kScriptTypeJavascript)
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this, kNodeOnExit);
}
@ -1123,9 +1155,12 @@ void Node::pauseSchedulerAndActions()
// override me
void Node::update(float fDelta)
{
if (_updateScriptHandler)
if (0 != _updateScriptHandler)
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeSchedule(_updateScriptHandler, fDelta, this);
//only lua use
SchedulerScriptData data(_updateScriptHandler,fDelta);
ScriptEvent event(kScheduleEvent,&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
if (_componentContainer && !_componentContainer->isEmpty())

View File

@ -142,12 +142,35 @@ void Layer::unregisterScriptTouchHandler(void)
int Layer::excuteScriptTouchHandler(int nEventType, Touch *pTouch)
{
return ScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerTouchEvent(this, nEventType, pTouch);
if (kScriptTypeLua == _scriptType)
{
Set touches;
touches.addObject((Object*)pTouch);
TouchesScriptData data(nEventType,kLayerTouches,(void*)this,&touches);
ScriptEvent event(kTouchesEvent,&data);
return ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
else if(kScriptTypeJavascript == _scriptType)
{
return ScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerTouchEvent(this, nEventType, pTouch);
}
//can not reach it
return 0;
}
int Layer::excuteScriptTouchHandler(int nEventType, Set *pTouches)
{
return ScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerTouchesEvent(this, nEventType, pTouches);
if (kScriptTypeLua == _scriptType)
{
TouchesScriptData data(nEventType,kLayerTouches,(void*)this,pTouches);
ScriptEvent event(kTouchesEvent,&data);
return ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
else if(kScriptTypeJavascript == _scriptType)
{
return ScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerTouchesEvent(this, nEventType, pTouches);
}
return 0;
}
/// isTouchEnabled getter
@ -257,10 +280,16 @@ void Layer::setAccelerometerInterval(double interval) {
void Layer::didAccelerate(Acceleration* pAccelerationValue)
{
CC_UNUSED_PARAM(pAccelerationValue);
if ( _scriptType != kScriptTypeNone)
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeAccelerometerEvent(this, pAccelerationValue);
}
if (kScriptTypeJavascript == _scriptType)
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeAccelerometerEvent(this, pAccelerationValue);
}
else if(kScriptTypeLua == _scriptType)
{
BasicScriptData data((void*)this,(void*)pAccelerationValue);
ScriptEvent event(kAccelerometerEvent,&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
}
void Layer::registerScriptAccelerateHandler(int nHandler)
@ -342,7 +371,13 @@ void Layer::unregisterScriptKeypadHandler(void)
void Layer::keyBackClicked(void)
{
if (_scriptKeypadHandlerEntry || _scriptType == kScriptTypeJavascript)
if (NULL != _scriptKeypadHandlerEntry && 0 != _scriptKeypadHandlerEntry->getHandler())
{
KeypadScriptData data(kTypeBackClicked,kLayerKeypad,(void*)this);
ScriptEvent event(kKeypadEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
else if(kScriptTypeJavascript == _scriptType)
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerKeypadEvent(this, kTypeBackClicked);
}
@ -350,9 +385,11 @@ void Layer::keyBackClicked(void)
void Layer::keyMenuClicked(void)
{
if (_scriptKeypadHandlerEntry)
if (NULL != _scriptKeypadHandlerEntry && 0 != _scriptKeypadHandlerEntry->getHandler())
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerKeypadEvent(this, kTypeMenuClicked);
KeypadScriptData data(kTypeMenuClicked,kLayerKeypad,(void*)this);
ScriptEvent event(kKeypadEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
}

View File

@ -132,7 +132,13 @@ void MenuItem::activate()
_callback(this);
}
if (kScriptTypeNone != _scriptType)
if (kScriptTypeLua == _scriptType)
{
BasicScriptData data((void*)this);
ScriptEvent scriptEvent(kMenuClickedEvent,&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent);
}
else if (kScriptTypeJavascript == _scriptType)
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeMenuItemEvent(this);
}

View File

@ -156,6 +156,116 @@ private:
bool _swallowsTouches;
};
enum ScriptEventType
{
kNodeEvent = 0,
kMenuClickedEvent,
kNotificationEvent,
kCallFuncEvent,
kScheduleEvent,
kTouchesEvent,
kKeypadEvent,
kAccelerometerEvent,
kCommonEvent,
};
enum TouchesObjectType
{
kLayerTouches = 0,
};
enum KeypadObjectType
{
kLayerKeypad = 0,
};
struct BasicScriptData
{
//nativeobject:to get handler for lua or to get jsobject for js
void* nativeObject;
//value: a pointer to a object that already defined
void* value;
BasicScriptData(void* inObject,void* inValue = NULL)
:nativeObject(inObject),value(inValue)
{
}
};
struct SchedulerScriptData
{
//lua use
int handler;
float elapse;
//js use
Node* node;
SchedulerScriptData(int inHandler,float inElapse,Node* inNode = NULL)
:handler(inHandler),
elapse(inElapse),
node(inNode)
{
}
};
struct TouchesScriptData
{
int actionType;
int objectType;
void* nativeObject;
Set* touches;
TouchesScriptData(int inActionType,int inObjectType,void* inNativeObject,Set* inTouches)
:actionType(inActionType),
objectType(inObjectType),
nativeObject(inNativeObject),
touches(inTouches)
{
}
};
struct KeypadScriptData
{
int actionType;
int objectType;
void* nativeObject;
KeypadScriptData(int inActionType,int inObjectType,void* inNativeObject)
:actionType(inActionType),objectType(inObjectType),nativeObject(inNativeObject)
{
}
};
struct CommonScriptData
{
//now,only use lua
int handler;
char eventName[64];
Object* eventSource;
char eventSourceClassName[64];
CommonScriptData(int inHandler,const char* inName,Object* inSource = NULL,const char* inClassName = NULL)
:handler(inHandler),
eventSource(inSource)
{
strncpy(eventName, inName, 64);
if (NULL == inClassName)
{
memset(eventSourceClassName, 0, 64*sizeof(char));
}
else
{
strncpy(eventSourceClassName, inClassName, 64);
}
}
};
struct ScriptEvent
{
ScriptEventType type;
void* data;
ScriptEvent(ScriptEventType inType,void* inData)
:type(inType),
data(inData)
{
}
};
// Don't make ScriptEngineProtocol inherits from Object since setScriptEngine is invoked only once in AppDelegate.cpp,
// It will affect the lifecycle of ScriptCore instance, the autorelease pool will be destroyed before destructing ScriptCore.
@ -233,6 +343,10 @@ public:
* @return true if the assert was handled by the script engine, false otherwise.
*/
virtual bool handleAssert(const char *msg) = 0;
//when trigger a script event ,call this func,add params needed into ScriptEvent object.nativeObject is object triggering the event, can be NULL in lua
virtual int sendEvent(ScriptEvent* message){ return 0;}
//
};
/**

View File

@ -180,8 +180,9 @@ void NotificationCenter::postNotification(const char *name, Object *object)
{
if (0 != observer->getHandler())
{
ScriptEngineProtocol* engine = ScriptEngineManager::sharedManager()->getScriptEngine();
engine->executeNotificationEvent(this, name);
BasicScriptData data((void*)this,(void*)name);
ScriptEvent scriptEvent(kNotificationEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent);
}
else
{
@ -200,7 +201,7 @@ int NotificationCenter::getObserverHandlerByName(const char* name)
{
if (NULL == name || strlen(name) == 0)
{
return -1;
return 0;
}
Object* obj = NULL;
@ -217,7 +218,7 @@ int NotificationCenter::getObserverHandlerByName(const char* name)
}
}
return -1;
return 0;
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -131,8 +131,11 @@ void Control::sendActionsForControlEvents(ControlEvent controlEvents)
if (kScriptTypeNone != _scriptType)
{
int nHandler = this->getHandleOfControlEvent(controlEvents);
if (-1 != nHandler) {
ScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(nHandler,"",this);
if (0 != nHandler)
{
cocos2d::CommonScriptData data(nHandler, "",(Object*)this);
cocos2d::ScriptEvent event(cocos2d::kCommonEvent,(void*)&data);
cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
}
}
@ -359,6 +362,6 @@ int Control::getHandleOfControlEvent(ControlEvent controlEvent)
if (_mapHandleOfControlEvent.end() != Iter)
return Iter->second;
return -1;
return 0;
}
NS_CC_EXT_END

View File

@ -258,11 +258,18 @@ static void editBoxCallbackFunc(const char* pText, void* ctx)
EditBox* pEditBox = thiz->getEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
cocos2d::ScriptEngineProtocol* pEngine = cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine();
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "ended",pEditBox);
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "return",pEditBox);
{
CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
ScriptEvent event(kCommonEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
memset(data.eventName,0,64*sizeof(char));
strncpy(data.eventName,"ended",64);
event.data = (void*)&data;
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
memset(data.eventName,0,64*sizeof(char));
strncpy(data.eventName,"return",64);
event.data = (void*)&data;
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
}
@ -274,9 +281,10 @@ void EditBoxImplAndroid::openKeyboard()
}
EditBox* pEditBox = this->getEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
cocos2d::ScriptEngineProtocol* pEngine = cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine();
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
{
CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
ScriptEvent event(cocos2d::kCommonEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
showEditTextDialogJNI( _placeHolder.c_str(),

View File

@ -158,9 +158,10 @@ static const int CC_EDIT_BOX_PADDING = 5;
cocos2d::extension::EditBox* pEditBox= getEditBoxImplIOS()->getEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
cocos2d::ScriptEngineProtocol* pEngine = cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine();
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
{
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
cocos2d::ScriptEvent event(cocos2d::kCommonEvent,(void*)&data);
cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
return YES;
}
@ -181,9 +182,13 @@ static const int CC_EDIT_BOX_PADDING = 5;
cocos2d::extension::EditBox* pEditBox= getEditBoxImplIOS()->getEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
cocos2d::ScriptEngineProtocol* pEngine = cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine();
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "ended",pEditBox);
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "return",pEditBox);
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "ended",pEditBox);
cocos2d::ScriptEvent event(cocos2d::kCommonEvent,(void*)&data);
cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
memset(data.eventName,0,64*sizeof(char));
strncpy(data.eventName,"return",64);
event.data = (void*)&data;
cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
if(editBox_ != nil)
@ -231,8 +236,9 @@ static const int CC_EDIT_BOX_PADDING = 5;
cocos2d::extension::EditBox* pEditBox= getEditBoxImplIOS()->getEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
cocos2d::ScriptEngineProtocol* pEngine = cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine();
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
cocos2d::ScriptEvent event(cocos2d::kCommonEvent,(void*)&data);
cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
}

View File

@ -150,8 +150,9 @@
cocos2d::extension::EditBox* pEditBox= getEditBoxImplMac()->getEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
cocos2d::ScriptEngineProtocol* pEngine = cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine();
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
cocos2d::ScriptEvent event(kCommonEvent,(void*)&data);
cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
return YES;
}
@ -169,9 +170,13 @@
cocos2d::extension::EditBox* pEditBox= getEditBoxImplMac()->getEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
cocos2d::ScriptEngineProtocol* pEngine = cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine();
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "ended",pEditBox);
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "return",pEditBox);
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "ended",pEditBox);
cocos2d::ScriptEvent event(kCommonEvent,(void*)&data);
cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
memset(data.eventName,0,64*sizeof(char));
strncpy(data.eventName,"return",64);
event.data = (void*)&data;
cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
return YES;
}
@ -213,8 +218,9 @@
cocos2d::extension::EditBox* pEditBox= getEditBoxImplMac()->getEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
cocos2d::ScriptEngineProtocol* pEngine = cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine();
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
cocos2d::CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
cocos2d::ScriptEvent event(kCommonEvent,(void*)&data);
cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
}

View File

@ -250,11 +250,18 @@ static void editBoxCallbackFunc(const char* pText, void* ctx)
EditBox* pEditBox = thiz->getEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
cocos2d::ScriptEngineProtocol* pEngine = cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine();
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "ended",pEditBox);
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "return",pEditBox);
{
CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
ScriptEvent event(kCommonEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
memset(data.eventName,0,64*sizeof(char));
strncpy(data.eventName,"ended",64);
event.data = (void*)&data;
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
memset(data.eventName,0,64*sizeof(char));
strncpy(data.eventName,"return",64);
event.data = (void*)&data;
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
}
@ -267,8 +274,9 @@ void EditBoxImplTizen::openKeyboard()
EditBox* pEditBox = this->getEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
cocos2d::ScriptEngineProtocol* pEngine = cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine();
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
ScriptEvent event(kCommonEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
KeypadStyle keypadStyle = KEYPAD_STYLE_NORMAL;

View File

@ -238,10 +238,17 @@ static void editBoxCallbackFunc(const char* pText, void* ctx)
EditBox* pEditBox = thiz->getEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
cocos2d::ScriptEngineProtocol* pEngine = cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine();
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "ended",pEditBox);
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "return",pEditBox);
CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
ScriptEvent event(kCommonEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
memset(data.eventName,0,64*sizeof(char));
strncpy(data.eventName,"ended",64);
event.data = (void*)&data;
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
memset(data.eventName,0,64*sizeof(char));
strncpy(data.eventName,"return",64);
event.data = (void*)&data;
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
}
@ -255,8 +262,9 @@ void EditBoxImplWin::openKeyboard()
EditBox* pEditBox = this->getEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
cocos2d::ScriptEngineProtocol* pEngine = cocos2d::ScriptEngineManager::sharedManager()->getScriptEngine();
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
ScriptEvent event(kCommonEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
std::string placeHolder = _labelPlaceHolder->getString();

View File

@ -331,4 +331,380 @@ int LuaEngine::reallocateScriptHandler(int nHandler)
return nRet;
}
int LuaEngine::sendEvent(ScriptEvent* message)
{
if (NULL == message)
return 0;
switch (message->type)
{
case kNodeEvent:
{
return handleNodeEvent(message->data);
}
break;
case kMenuClickedEvent:
{
return handleMenuClickedEvent(message->data);
}
break;
case kNotificationEvent:
{
return handleNotificationEvent(message->data);
}
break;
case kCallFuncEvent:
{
return handleCallFuncActionEvent(message->data);
}
break;
case kScheduleEvent:
{
return handleScheduler(message->data);
}
break;
case kTouchesEvent:
{
return handleTouchesEvent(message->data);
}
break;
case kKeypadEvent:
{
return handleKeypadEvent(message->data);
}
break;
case kAccelerometerEvent:
{
return handleAccelerometerEvent(message->data);
}
break;
case kCommonEvent:
{
return handleCommonEvent(message->data);
}
break;
default:
break;
}
return 0;
}
int LuaEngine::handleNodeEvent(void* data)
{
if (NULL == data)
return 0;
BasicScriptData* basicScriptData = (BasicScriptData*)data;
if (NULL == basicScriptData->nativeObject || NULL == basicScriptData->value)
return 0;
Node* node = (Node*)(basicScriptData->nativeObject);
int handler = node->getScriptHandler();
if (0 == handler)
return 0;
int action = *((int*)(basicScriptData->value));
switch (action)
{
case kNodeOnEnter:
_stack->pushString("enter");
break;
case kNodeOnExit:
_stack->pushString("exit");
break;
case kNodeOnEnterTransitionDidFinish:
_stack->pushString("enterTransitionFinish");
break;
case kNodeOnExitTransitionDidStart:
_stack->pushString("exitTransitionStart");
break;
case kNodeOnCleanup:
_stack->pushString("cleanup");
break;
default:
return 0;
}
int ret = _stack->executeFunctionByHandler(handler, 1);
_stack->clean();
return ret;
}
int LuaEngine::handleMenuClickedEvent(void* data)
{
if (NULL == data)
return 0;
BasicScriptData* basicScriptData = (BasicScriptData*)data;
if (NULL == basicScriptData->nativeObject)
return 0;
MenuItem* menuItem = (MenuItem*)(basicScriptData->nativeObject);
int handler = menuItem->getScriptTapHandler();
if (0 == handler)
return 0;
_stack->pushInt(menuItem->getTag());
_stack->pushObject(menuItem, "CCMenuItem");
int ret = _stack->executeFunctionByHandler(handler, 2);
_stack->clean();
return ret;
}
int LuaEngine::handleNotificationEvent(void* data)
{
if ( NULL == data)
return 0;
BasicScriptData* basicScriptData = (BasicScriptData*)(data);
if (NULL == basicScriptData->nativeObject ||NULL == basicScriptData->value)
return 0;
NotificationCenter* center = (NotificationCenter*)(basicScriptData->nativeObject);
int handler = center->getObserverHandlerByName((const char*)basicScriptData->value);
if (0 == handler)
return 0;
_stack->pushString((const char*)basicScriptData->value);
int ret = _stack->executeFunctionByHandler(handler, 1);
_stack->clean();
return ret;
}
int LuaEngine::handleCallFuncActionEvent(void* data)
{
if (NULL == data)
return 0;
BasicScriptData* basicScriptData = (BasicScriptData*)(data);
if (NULL == basicScriptData->nativeObject ||NULL == basicScriptData->value)
return 0;
CallFunc* callFunc = (CallFunc*)(basicScriptData->nativeObject);
int handler = callFunc->getScriptHandler();
Object* target = (Object*)(basicScriptData->value);
if (NULL != target)
{
_stack->pushObject(target, "CCNode");
}
int ret = _stack->executeFunctionByHandler(handler, target ? 1 : 0);
_stack->clean();
return ret;
}
int LuaEngine::handleScheduler(void* data)
{
if (NULL == data)
return 0;
SchedulerScriptData* schedulerInfo = (SchedulerScriptData*)data;
_stack->pushFloat(schedulerInfo->elapse);
int ret = _stack->executeFunctionByHandler(schedulerInfo->handler, 1);
_stack->clean();
return ret;
}
int LuaEngine::handleKeypadEvent(void* data)
{
if (NULL == data)
return 0;
KeypadScriptData* keypadScriptData = (KeypadScriptData*)data;
if (NULL == keypadScriptData->nativeObject)
return 0;
switch (keypadScriptData->objectType)
{
case kLayerKeypad:
{
Layer* layer = (Layer*)(keypadScriptData->nativeObject);
return handleLayerKeypadEvent(layer, keypadScriptData->actionType);
}
break;
default:
break;
}
return 0;
}
int LuaEngine::handleAccelerometerEvent(void* data)
{
if (NULL == data)
return 0;
BasicScriptData* basicScriptData = (BasicScriptData*)data;
if (NULL == basicScriptData->nativeObject || NULL == basicScriptData->value)
return 0;
Layer* layer = (Layer*)(basicScriptData->nativeObject);
ScriptHandlerEntry* scriptHandlerEntry = layer->getScriptAccelerateHandlerEntry();
if (NULL == scriptHandlerEntry || 0 == scriptHandlerEntry->getHandler())
return 0;
Acceleration* accelerationValue = (Acceleration*)(basicScriptData->value);
_stack->pushFloat(accelerationValue->x);
_stack->pushFloat(accelerationValue->y);
_stack->pushFloat(accelerationValue->z);
_stack->pushFloat(accelerationValue->timestamp);
int ret = _stack->executeFunctionByHandler(scriptHandlerEntry->getHandler(), 4);
_stack->clean();
return ret;
}
int LuaEngine::handleCommonEvent(void* data)
{
if (NULL == data)
return 0;
CommonScriptData* commonInfo = (CommonScriptData*)data;
if (NULL == commonInfo->eventName || 0 == commonInfo->handler)
return 0;
_stack->pushString(commonInfo->eventName);
if (NULL != commonInfo->eventSource)
{
if (NULL != commonInfo->eventSourceClassName && strlen(commonInfo->eventSourceClassName) > 0)
{
_stack->pushObject(commonInfo->eventSource, commonInfo->eventSourceClassName);
}
else
{
_stack->pushObject(commonInfo->eventSource, "CCObject");
}
}
int ret = _stack->executeFunctionByHandler(commonInfo->handler, commonInfo->eventSource ? 2 : 1);
_stack->clean();
return ret;
}
int LuaEngine::handleTouchesEvent(void* data)
{
if (NULL == data)
return 0;
TouchesScriptData* touchesScriptData = (TouchesScriptData*)data;
if (NULL == touchesScriptData->nativeObject || NULL == touchesScriptData->touches)
return 0;
switch (touchesScriptData->objectType)
{
case kLayerTouches:
{
Layer* layer = (Layer*)(touchesScriptData->nativeObject);
return handleLayerTouchesEvent(layer, touchesScriptData->actionType, touchesScriptData->touches);
}
break;
default:
break;
}
return 0;
}
int LuaEngine::handleLayerTouchesEvent(Layer* layer,int actionType,Set* touches)
{
if (NULL == layer || NULL == touches)
return 0;
TouchScriptHandlerEntry* scriptHandlerEntry = layer->getScriptTouchHandlerEntry();
if (NULL == scriptHandlerEntry || 0 == scriptHandlerEntry->getHandler())
return 0;
switch (actionType)
{
case CCTOUCHBEGAN:
_stack->pushString("began");
break;
case CCTOUCHMOVED:
_stack->pushString("moved");
break;
case CCTOUCHENDED:
_stack->pushString("ended");
break;
case CCTOUCHCANCELLED:
_stack->pushString("cancelled");
break;
default:
return 0;
}
Director* pDirector = Director::sharedDirector();
lua_State *L = _stack->getLuaState();
int count = touches->count();
int ret = 0;
if (count == 1)
{
Touch* touch = (Touch*)*(touches->begin());
if (NULL != touch) {
const Point pt = Director::sharedDirector()->convertToGL(touch->getLocationInView());
_stack->pushFloat(pt.x);
_stack->pushFloat(pt.y);
ret = _stack->executeFunctionByHandler(scriptHandlerEntry->getHandler(), 3);
}
}
else if(count > 1)
{
lua_newtable(L);
int i = 1;
for (SetIterator it = touches->begin(); it != touches->end(); ++it)
{
Touch* pTouch = (Touch*)*it;
Point pt = pDirector->convertToGL(pTouch->getLocationInView());
lua_pushnumber(L, pt.x);
lua_rawseti(L, -2, i++);
lua_pushnumber(L, pt.y);
lua_rawseti(L, -2, i++);
lua_pushinteger(L, pTouch->getID());
lua_rawseti(L, -2, i++);
}
ret = _stack->executeFunctionByHandler(scriptHandlerEntry->getHandler(), 2);
}
_stack->clean();
return ret;
}
int LuaEngine::handleLayerKeypadEvent(Layer* layer,int actionType)
{
if (NULL == layer)
return 0;
ScriptHandlerEntry* pScriptHandlerEntry = layer->getScriptKeypadHandlerEntry();
int action = actionType;
switch (action)
{
case kTypeBackClicked:
_stack->pushString("backClicked");
break;
case kTypeMenuClicked:
_stack->pushString("menuClicked");
break;
default:
return 0;
}
int ret = _stack->executeFunctionByHandler(pScriptHandlerEntry->getHandler(), 1);
_stack->clean();
return ret;
}
NS_CC_END

View File

@ -118,6 +118,19 @@ public:
virtual bool handleAssert(const char *msg);
virtual int sendEvent(ScriptEvent* message);
private:
int handleNodeEvent(void* data);
int handleMenuClickedEvent(void* data);
int handleNotificationEvent(void* data);
int handleCallFuncActionEvent(void* data);
int handleScheduler(void* data);
int handleKeypadEvent(void* data);
int handleAccelerometerEvent(void* data);
int handleCommonEvent(void* data);
int handleTouchesEvent(void* data);
int handleLayerTouchesEvent(Layer* layer,int actionType,Set* touches);
int handleLayerKeypadEvent(Layer* layer,int actionType);
private:
LuaEngine(void)
: _stack(NULL)

View File

@ -1 +1 @@
fb7e01e5b657a41142a4774075e84a7436010de3
9396440a0938c40aad23960e8c269af670a12110

View File

@ -33,9 +33,11 @@ public:
if (NULL != luaView)
{
int nHandler = luaView->getScriptHandler(LuaScrollView::kScrollViewScriptScroll);
if (-1 != nHandler)
if (0 != nHandler)
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(nHandler,"");
CommonScriptData data(nHandler,"");
ScriptEvent event(kCommonEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
}
}
@ -46,9 +48,11 @@ public:
if (NULL != luaView)
{
int nHandler = luaView->getScriptHandler(LuaScrollView::kScrollViewScriptZoom);
if (-1 != nHandler)
if (0 != nHandler)
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(nHandler,"");
CommonScriptData data(nHandler,"");
ScriptEvent event(kCommonEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
}
}
@ -86,7 +90,7 @@ public:
if (_mapScriptHandler.end() != Iter)
return Iter->second;
return -1;
return 0;
}
private:
std::map<int,int> _mapScriptHandler;

View File

@ -102,7 +102,7 @@ public:
if (_mapScriptHandler.end() != Iter)
return Iter->second;
return -1;
return 0;
}
void InitScriptHandleMap()
@ -115,8 +115,10 @@ public:
LuaWebSocket* luaWs = dynamic_cast<LuaWebSocket*>(ws);
if (NULL != luaWs) {
int nHandler = luaWs->getScriptHandler(LuaWebSocket::kWebSocketScriptHandlerOpen);
if (-1 != nHandler) {
ScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(nHandler,"");
if (0 != nHandler) {
CommonScriptData data(nHandler,"");
ScriptEvent event(kCommonEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
}
}
@ -127,15 +129,17 @@ public:
if (NULL != luaWs) {
if (data.isBinary) {
int nHandler = luaWs->getScriptHandler(LuaWebSocket::kWebSocketScriptHandlerMessage);
if (-1 != nHandler) {
if (0 != nHandler) {
SendBinaryMessageToLua(nHandler, (const unsigned char*)data.bytes, data.len);
}
}
else{
int nHandler = luaWs->getScriptHandler(LuaWebSocket::kWebSocketScriptHandlerMessage);
if (-1 != nHandler) {
ScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(nHandler,data.bytes);
if (0 != nHandler) {
CommonScriptData commonData(nHandler,data.bytes);
ScriptEvent event(kCommonEvent,(void*)&commonData);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
}
}
@ -146,8 +150,11 @@ public:
LuaWebSocket* luaWs = dynamic_cast<LuaWebSocket*>(ws);
if (NULL != luaWs) {
int nHandler = luaWs->getScriptHandler(LuaWebSocket::kWebSocketScriptHandlerClose);
if (-1 != nHandler) {
ScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(nHandler,"");
if (0 != nHandler)
{
CommonScriptData data(nHandler,"");
ScriptEvent event(kCommonEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
}
}
@ -157,8 +164,11 @@ public:
LuaWebSocket* luaWs = dynamic_cast<LuaWebSocket*>(ws);
if (NULL != luaWs) {
int nHandler = luaWs->getScriptHandler(LuaWebSocket::kWebSocketScriptHandlerError);
if (-1 != nHandler) {
ScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(nHandler,"");
if (0 != nHandler)
{
CommonScriptData data(nHandler,"");
ScriptEvent event(kCommonEvent,(void*)&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
}
}
}