axmol/cocos/base/CCScriptSupport.cpp

187 lines
5.2 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
http://www.cocos2d-x.org
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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:
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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 "base/CCScriptSupport.h"
#if CC_ENABLE_SCRIPT_BINDING
2021-12-25 10:04:45 +08:00
# include "base/CCScheduler.h"
# include "2d/CCNode.h"
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
bool CC_DLL cc_assert_script_compatible(const char* msg)
2019-11-23 20:27:39 +08:00
{
cocos2d::ScriptEngineProtocol* engine = cocos2d::ScriptEngineManager::getInstance()->getScriptEngine();
if (engine && engine->handleAssert(msg))
{
return true;
}
return false;
}
NS_CC_BEGIN
2021-12-25 10:04:45 +08:00
//
2019-11-23 20:27:39 +08:00
// // ScriptHandlerEntry
ScriptHandlerEntry* ScriptHandlerEntry::create(int handler)
{
2021-12-08 00:11:53 +08:00
ScriptHandlerEntry* entry = new ScriptHandlerEntry(handler);
2019-11-23 20:27:39 +08:00
entry->autorelease();
return entry;
}
ScriptHandlerEntry::~ScriptHandlerEntry()
{
2021-12-25 10:04:45 +08:00
if (_handler != 0)
2019-11-23 20:27:39 +08:00
{
ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptHandler(_handler);
LUALOG("[LUA] Remove event handler: %d", _handler);
_handler = 0;
}
}
2021-12-25 10:04:45 +08:00
//
2019-11-23 20:27:39 +08:00
// // SchedulerScriptHandlerEntry
SchedulerScriptHandlerEntry* SchedulerScriptHandlerEntry::create(int handler, float interval, bool paused)
{
2021-12-08 00:11:53 +08:00
SchedulerScriptHandlerEntry* entry = new SchedulerScriptHandlerEntry(handler);
2019-11-23 20:27:39 +08:00
entry->init(interval, paused);
entry->autorelease();
return entry;
}
bool SchedulerScriptHandlerEntry::init(float interval, bool paused)
{
2021-12-08 00:11:53 +08:00
_timer = new TimerScriptHandler();
2019-11-23 20:27:39 +08:00
_timer->initWithScriptHandler(_handler, interval);
_paused = paused;
LUALOG("[LUA] ADD script schedule: %d, entryID: %d", _handler, _entryId);
return true;
}
SchedulerScriptHandlerEntry::~SchedulerScriptHandlerEntry()
{
_timer->release();
LUALOG("[LUA] DEL script schedule %d, entryID: %d", _handler, _entryId);
}
2021-12-25 10:04:45 +08:00
//
2019-11-23 20:27:39 +08:00
// // TouchScriptHandlerEntry
TouchScriptHandlerEntry* TouchScriptHandlerEntry::create(int handler,
2021-12-25 10:04:45 +08:00
bool isMultiTouches,
int priority,
bool swallowsTouches)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
TouchScriptHandlerEntry* entry = new TouchScriptHandlerEntry(handler);
2019-11-23 20:27:39 +08:00
entry->init(isMultiTouches, priority, swallowsTouches);
entry->autorelease();
return entry;
}
2021-12-25 10:04:45 +08:00
TouchScriptHandlerEntry::~TouchScriptHandlerEntry() {}
2019-11-23 20:27:39 +08:00
bool TouchScriptHandlerEntry::init(bool isMultiTouches, int priority, bool swallowsTouches)
{
2021-12-25 10:04:45 +08:00
_isMultiTouches = isMultiTouches;
_priority = priority;
2019-11-23 20:27:39 +08:00
_swallowsTouches = swallowsTouches;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return true;
}
2021-12-25 10:04:45 +08:00
//
2019-11-23 20:27:39 +08:00
// // ScriptEngineManager
static ScriptEngineManager* s_pSharedScriptEngineManager = nullptr;
ScriptEngineManager::~ScriptEngineManager()
{
removeScriptEngine();
}
2021-12-25 10:04:45 +08:00
void ScriptEngineManager::setScriptEngine(ScriptEngineProtocol* scriptEngine)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (_scriptEngine != scriptEngine)
{
removeScriptEngine();
_scriptEngine = scriptEngine;
}
2019-11-23 20:27:39 +08:00
}
void ScriptEngineManager::removeScriptEngine()
{
if (_scriptEngine)
{
delete _scriptEngine;
_scriptEngine = nullptr;
}
}
ScriptEngineManager* ScriptEngineManager::getInstance()
{
if (!s_pSharedScriptEngineManager)
{
2021-12-08 00:11:53 +08:00
s_pSharedScriptEngineManager = new ScriptEngineManager();
2019-11-23 20:27:39 +08:00
}
return s_pSharedScriptEngineManager;
}
void ScriptEngineManager::destroyInstance()
{
if (s_pSharedScriptEngineManager)
{
delete s_pSharedScriptEngineManager;
s_pSharedScriptEngineManager = nullptr;
}
}
void ScriptEngineManager::sendNodeEventToLua(Node* node, int action)
2019-11-23 20:27:39 +08:00
{
auto scriptEngine = getInstance()->getScriptEngine();
2021-12-25 10:04:45 +08:00
if (scriptEngine)
{
2019-11-23 20:27:39 +08:00
BasicScriptData data(node, (void*)&action);
ScriptEvent scriptEvent(kNodeEvent, (void*)&data);
2019-11-23 20:27:39 +08:00
scriptEngine->sendEvent(scriptEvent);
2019-11-23 20:27:39 +08:00
}
}
int ScriptEngineManager::sendEventToLua(const ScriptEvent& event)
2019-11-23 20:27:39 +08:00
{
auto scriptEngine = getInstance()->getScriptEngine();
if (scriptEngine)
return scriptEngine->sendEvent(event);
return 0;
2019-11-23 20:27:39 +08:00
}
NS_CC_END
2021-12-25 10:04:45 +08:00
#endif // #if CC_ENABLE_SCRIPT_BINDING