2014-03-10 14:04:58 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
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 __CC_LUA_ENGINE_H__
|
|
|
|
#define __CC_LUA_ENGINE_H__
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "lua.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "cocos2d.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "base/CCScriptSupport.h"
|
2014-03-10 14:04:58 +08:00
|
|
|
#include "CCLuaStack.h"
|
|
|
|
#include "CCLuaValue.h"
|
|
|
|
#include "LuaScriptHandlerMgr.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
// Lua support for cocos2d-x
|
|
|
|
class LuaEngine : public ScriptEngineProtocol
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static LuaEngine* getInstance(void);
|
|
|
|
CC_DEPRECATED_ATTRIBUTE static LuaEngine* defaultEngine(void) { return LuaEngine::getInstance(); }
|
|
|
|
virtual ~LuaEngine(void);
|
|
|
|
|
|
|
|
virtual ccScriptType getScriptType() {
|
|
|
|
return kScriptTypeLua;
|
|
|
|
};
|
|
|
|
|
|
|
|
LuaStack *getLuaStack(void) {
|
|
|
|
return _stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Add a path to find lua files in
|
|
|
|
@param path to be added to the Lua path
|
|
|
|
*/
|
|
|
|
virtual void addSearchPath(const char* path);
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Add lua loader, now it is used on android
|
|
|
|
*/
|
|
|
|
virtual void addLuaLoader(lua_CFunction func);
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief reload script code contained in the given string.
|
|
|
|
@param moduleFileName String object holding the filename of the script file that is to be executed
|
|
|
|
@return 0 if the string is excuted correctly.
|
|
|
|
@return other if the string is excuted wrongly.
|
|
|
|
*/
|
|
|
|
virtual int reload(const char* moduleFileName);
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Remove Object from lua state
|
|
|
|
@param object to remove
|
|
|
|
*/
|
|
|
|
virtual void removeScriptObjectByObject(Ref* object);
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Remove Lua function reference
|
|
|
|
*/
|
|
|
|
virtual void removeScriptHandler(int nHandler);
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Reallocate Lua function reference
|
|
|
|
*/
|
|
|
|
virtual int reallocateScriptHandler(int nHandler);
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Execute script code contained in the given string.
|
|
|
|
@param codes holding the valid script code that should be executed.
|
|
|
|
@return 0 if the string is excuted correctly.
|
|
|
|
@return other if the string is excuted wrongly.
|
|
|
|
*/
|
|
|
|
virtual int executeString(const char* codes);
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Execute a script file.
|
|
|
|
@param filename String object holding the filename of the script file that is to be executed
|
|
|
|
*/
|
|
|
|
virtual int executeScriptFile(const char* filename);
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Execute a scripted global function.
|
|
|
|
@brief The function should not take any parameters and should return an integer.
|
|
|
|
@param functionName String object holding the name of the function, in the global script environment, that is to be executed.
|
|
|
|
@return The integer value returned from the script function.
|
|
|
|
*/
|
|
|
|
virtual int executeGlobalFunction(const char* functionName);
|
|
|
|
|
|
|
|
virtual int executeNodeEvent(Node* pNode, int nAction);
|
|
|
|
virtual int executeMenuItemEvent(MenuItem* pMenuItem);
|
2014-03-11 17:35:09 +08:00
|
|
|
virtual int executeNotificationEvent(__NotificationCenter* pNotificationCenter, const char* pszName);
|
2014-03-10 14:04:58 +08:00
|
|
|
virtual int executeCallFuncActionEvent(CallFunc* pAction, Ref* pTarget = NULL);
|
|
|
|
virtual int executeSchedule(int nHandler, float dt, Node* pNode = NULL);
|
2014-03-11 17:35:09 +08:00
|
|
|
virtual int executeLayerTouchesEvent(Layer* pLayer, int eventType, __Set *pTouches);
|
2014-03-10 14:04:58 +08:00
|
|
|
virtual int executeLayerTouchEvent(Layer* pLayer, int eventType, Touch *pTouch);
|
|
|
|
virtual int executeLayerKeypadEvent(Layer* pLayer, int eventType);
|
|
|
|
/** execute a accelerometer event */
|
|
|
|
virtual int executeAccelerometerEvent(Layer* pLayer, Acceleration* pAccelerationValue);
|
|
|
|
virtual int executeEvent(int nHandler, const char* pEventName, Ref* pEventSource = NULL, const char* pEventSourceClassName = NULL);
|
|
|
|
|
|
|
|
virtual bool handleAssert(const char *msg);
|
|
|
|
|
|
|
|
virtual bool parseConfig(ConfigType type, const std::string& str) override;
|
|
|
|
virtual int sendEvent(ScriptEvent* message) override;
|
|
|
|
virtual int handleEvent(ScriptHandlerMgr::HandlerType type,void* data);
|
|
|
|
virtual int handleEvent(ScriptHandlerMgr::HandlerType type, void* data, int numResults, const std::function<void(lua_State*,int)>& func);
|
|
|
|
private:
|
|
|
|
LuaEngine(void)
|
2014-04-09 22:53:59 +08:00
|
|
|
: _stack(nullptr)
|
2014-03-10 14:04:58 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
bool init(void);
|
|
|
|
int handleNodeEvent(void* data);
|
|
|
|
int handleMenuClickedEvent(void* data);
|
|
|
|
int handleCallFuncActionEvent(void* data);
|
|
|
|
int handleScheduler(void* data);
|
|
|
|
int handleKeypadEvent(void* data);
|
|
|
|
int handleAccelerometerEvent(void* data);
|
|
|
|
int handleCommonEvent(void* data);
|
|
|
|
int handleTouchEvent(void* data);
|
|
|
|
int handleTouchesEvent(void* data);
|
|
|
|
int handlerControlEvent(void* data);
|
|
|
|
int handleEvenCustom(void* data);
|
|
|
|
int handleAssetsManagerEvent(ScriptHandlerMgr::HandlerType type,void* data);
|
|
|
|
int handleTableViewEvent(ScriptHandlerMgr::HandlerType type,void* data);
|
|
|
|
int handleTableViewEvent(ScriptHandlerMgr::HandlerType type,void* data, int numResults, const std::function<void(lua_State*,int)>& func);
|
|
|
|
int handleStudioEventListener(ScriptHandlerMgr::HandlerType type,void* data);
|
|
|
|
int handleArmatureWrapper(ScriptHandlerMgr::HandlerType type,void* data);
|
|
|
|
int handleEventAcc(void* data);
|
|
|
|
int handleEventKeyboard(ScriptHandlerMgr::HandlerType type,void* data);
|
|
|
|
int handleEventTouch(ScriptHandlerMgr::HandlerType type, void* data);
|
|
|
|
int handleEventTouches(ScriptHandlerMgr::HandlerType type, void* data);
|
|
|
|
int handleEventMouse(ScriptHandlerMgr::HandlerType type, void* data);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static LuaEngine* _defaultEngine;
|
|
|
|
LuaStack *_stack;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // __CC_LUA_ENGINE_H__
|