2012-02-09 14:07:11 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-09-02 00:38:57 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2012-02-09 14:07:11 +08:00
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "base/CCScriptSupport.h"
|
2014-02-20 16:40:46 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCScheduler.h"
|
2014-06-18 12:47:48 +08:00
|
|
|
#include "2d/CCNode.h"
|
2012-02-09 14:07:11 +08:00
|
|
|
|
2013-04-15 22:50:07 +08:00
|
|
|
bool CC_DLL cc_assert_script_compatible(const char *msg)
|
2012-12-26 18:33:55 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
cocos2d::ScriptEngineProtocol* engine = cocos2d::ScriptEngineManager::getInstance()->getScriptEngine();
|
|
|
|
if (engine && engine->handleAssert(msg))
|
2012-12-26 18:33:55 +08:00
|
|
|
{
|
2013-04-15 22:50:07 +08:00
|
|
|
return true;
|
2012-12-26 18:33:55 +08:00
|
|
|
}
|
2013-04-15 22:50:07 +08:00
|
|
|
return false;
|
2012-12-26 18:33:55 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 14:07:11 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-03-26 14:54:50 +08:00
|
|
|
//
|
|
|
|
// // ScriptHandlerEntry
|
2012-08-28 12:08:15 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
ScriptHandlerEntry* ScriptHandlerEntry::create(int handler)
|
2012-08-28 12:08:15 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
ScriptHandlerEntry* entry = new (std::nothrow) ScriptHandlerEntry(handler);
|
2012-08-28 12:08:15 +08:00
|
|
|
entry->autorelease();
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ScriptHandlerEntry::~ScriptHandlerEntry(void)
|
2012-08-28 12:08:15 +08:00
|
|
|
{
|
2014-02-20 21:58:20 +08:00
|
|
|
if (_handler != 0 )
|
|
|
|
{
|
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptHandler(_handler);
|
|
|
|
LUALOG("[LUA] Remove event handler: %d", _handler);
|
|
|
|
_handler = 0;
|
|
|
|
}
|
2012-08-28 12:08:15 +08:00
|
|
|
}
|
|
|
|
|
2014-03-26 14:54:50 +08:00
|
|
|
//
|
|
|
|
// // SchedulerScriptHandlerEntry
|
2012-08-25 15:05:59 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
SchedulerScriptHandlerEntry* SchedulerScriptHandlerEntry::create(int handler, float interval, bool paused)
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
SchedulerScriptHandlerEntry* entry = new (std::nothrow) SchedulerScriptHandlerEntry(handler);
|
2013-12-18 17:47:20 +08:00
|
|
|
entry->init(interval, paused);
|
|
|
|
entry->autorelease();
|
|
|
|
return entry;
|
2012-02-09 14:07:11 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
bool SchedulerScriptHandlerEntry::init(float interval, bool paused)
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
_timer = new (std::nothrow) TimerScriptHandler();
|
2013-12-18 17:47:20 +08:00
|
|
|
_timer->initWithScriptHandler(_handler, interval);
|
|
|
|
_paused = paused;
|
2013-06-15 14:03:30 +08:00
|
|
|
LUALOG("[LUA] ADD script schedule: %d, entryID: %d", _handler, _entryId);
|
2012-02-09 14:07:11 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SchedulerScriptHandlerEntry::~SchedulerScriptHandlerEntry(void)
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_timer->release();
|
|
|
|
LUALOG("[LUA] DEL script schedule %d, entryID: %d", _handler, _entryId);
|
2012-02-09 14:07:11 +08:00
|
|
|
}
|
|
|
|
|
2014-03-26 14:54:50 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// // TouchScriptHandlerEntry
|
2012-02-09 14:07:11 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
TouchScriptHandlerEntry* TouchScriptHandlerEntry::create(int handler,
|
|
|
|
bool isMultiTouches,
|
|
|
|
int priority,
|
|
|
|
bool swallowsTouches)
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
TouchScriptHandlerEntry* entry = new (std::nothrow) TouchScriptHandlerEntry(handler);
|
2013-12-18 17:47:20 +08:00
|
|
|
entry->init(isMultiTouches, priority, swallowsTouches);
|
|
|
|
entry->autorelease();
|
|
|
|
return entry;
|
2012-02-09 14:07:11 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TouchScriptHandlerEntry::~TouchScriptHandlerEntry(void)
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
bool TouchScriptHandlerEntry::init(bool isMultiTouches, int priority, bool swallowsTouches)
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
_isMultiTouches = isMultiTouches;
|
|
|
|
_priority = priority;
|
|
|
|
_swallowsTouches = swallowsTouches;
|
2012-02-09 14:07:11 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-26 14:54:50 +08:00
|
|
|
//
|
|
|
|
// // ScriptEngineManager
|
2012-02-09 14:07:11 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
static ScriptEngineManager* s_pSharedScriptEngineManager = nullptr;
|
2012-02-09 14:07:11 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ScriptEngineManager::~ScriptEngineManager(void)
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
|
|
|
removeScriptEngine();
|
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
void ScriptEngineManager::setScriptEngine(ScriptEngineProtocol *scriptEngine)
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
2014-02-10 18:15:30 +08:00
|
|
|
if (_scriptEngine != scriptEngine)
|
|
|
|
{
|
|
|
|
removeScriptEngine();
|
|
|
|
_scriptEngine = scriptEngine;
|
|
|
|
}
|
2012-02-09 14:07:11 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ScriptEngineManager::removeScriptEngine(void)
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_scriptEngine)
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
delete _scriptEngine;
|
2013-12-18 17:47:20 +08:00
|
|
|
_scriptEngine = nullptr;
|
2012-02-09 14:07:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-22 17:19:31 +08:00
|
|
|
ScriptEngineManager* ScriptEngineManager::getInstance()
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
|
|
|
if (!s_pSharedScriptEngineManager)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
s_pSharedScriptEngineManager = new (std::nothrow) ScriptEngineManager();
|
2012-02-09 14:07:11 +08:00
|
|
|
}
|
|
|
|
return s_pSharedScriptEngineManager;
|
|
|
|
}
|
|
|
|
|
2013-07-22 17:19:31 +08:00
|
|
|
void ScriptEngineManager::destroyInstance()
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
2012-02-09 20:07:55 +08:00
|
|
|
if (s_pSharedScriptEngineManager)
|
|
|
|
{
|
|
|
|
delete s_pSharedScriptEngineManager;
|
2013-12-18 17:47:20 +08:00
|
|
|
s_pSharedScriptEngineManager = nullptr;
|
2012-02-09 20:07:55 +08:00
|
|
|
}
|
2012-02-09 14:07:11 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 17:55:38 +08:00
|
|
|
bool ScriptEngineManager::sendActionEventToJS(Action* actionObject, int eventType, void* param)
|
|
|
|
{
|
|
|
|
auto scriptEngine = getInstance()->getScriptEngine();
|
|
|
|
|
|
|
|
ActionObjectScriptData data(actionObject,(int*)&eventType, param);
|
|
|
|
ScriptEvent scriptEvent(kScriptActionEvent,(void*)&data);
|
|
|
|
if (scriptEngine->sendEvent(&scriptEvent))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-18 11:51:52 +08:00
|
|
|
bool ScriptEngineManager::sendNodeEventToJS(Node* node, int action)
|
|
|
|
{
|
|
|
|
auto scriptEngine = getInstance()->getScriptEngine();
|
|
|
|
|
|
|
|
if (scriptEngine->isCalledFromScript())
|
|
|
|
{
|
|
|
|
// Should only be invoked at root class Node
|
|
|
|
scriptEngine->setCalledFromScript(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BasicScriptData data(node,(void*)&action);
|
|
|
|
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
|
|
|
|
if (scriptEngine->sendEvent(&scriptEvent))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptEngineManager::sendNodeEventToJSExtended(Node* node, int action)
|
|
|
|
{
|
|
|
|
auto scriptEngine = getInstance()->getScriptEngine();
|
|
|
|
|
|
|
|
if (!scriptEngine->isCalledFromScript())
|
|
|
|
{
|
|
|
|
BasicScriptData data(node,(void*)&action);
|
|
|
|
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
|
|
|
|
if (scriptEngine->sendEvent(&scriptEvent))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptEngineManager::sendNodeEventToLua(Node* node, int action)
|
|
|
|
{
|
|
|
|
auto scriptEngine = getInstance()->getScriptEngine();
|
|
|
|
|
|
|
|
BasicScriptData data(node,(void*)&action);
|
|
|
|
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
|
|
|
|
|
|
|
|
scriptEngine->sendEvent(&scriptEvent);
|
|
|
|
}
|
|
|
|
|
2012-04-19 11:46:08 +08:00
|
|
|
NS_CC_END
|
2014-02-20 16:40:46 +08:00
|
|
|
|
|
|
|
#endif // #if CC_ENABLE_SCRIPT_BINDING
|