2012-02-01 16:55:46 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2011 cocos2d-x.org
|
|
|
|
|
|
|
|
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-02 15:58:10 +08:00
|
|
|
|
|
|
|
#ifndef __CC_LUA_ENGINE_H__
|
|
|
|
#define __CC_LUA_ENGINE_H__
|
2012-02-01 16:55:46 +08:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "lua.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "ccTypes.h"
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "cocoa/CCObject.h"
|
|
|
|
#include "touch_dispatcher/CCTouch.h"
|
|
|
|
#include "cocoa/CCSet.h"
|
|
|
|
#include "base_nodes/CCNode.h"
|
|
|
|
#include "script_support/CCScriptSupport.h"
|
2013-02-02 02:05:52 +08:00
|
|
|
#include "CCLuaStack.h"
|
|
|
|
#include "CCLuaValue.h"
|
2012-12-22 10:13:37 +08:00
|
|
|
|
2012-02-09 14:07:11 +08:00
|
|
|
NS_CC_BEGIN
|
2012-02-01 16:55:46 +08:00
|
|
|
|
|
|
|
// Lua support for cocos2d-x
|
2013-06-20 14:33:59 +08:00
|
|
|
class LuaEngine : public ScriptEngineProtocol
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-20 14:33:59 +08:00
|
|
|
static LuaEngine* defaultEngine(void);
|
|
|
|
virtual ~LuaEngine(void);
|
2012-09-02 00:38:57 +08:00
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
virtual ccScriptType getScriptType() {
|
|
|
|
return kScriptTypeLua;
|
|
|
|
};
|
2012-09-11 14:02:33 +08:00
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
LuaStack *getLuaStack(void) {
|
2013-06-15 14:03:30 +08:00
|
|
|
return _stack;
|
2013-02-02 02:05:52 +08:00
|
|
|
}
|
2013-02-04 11:28:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
@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);
|
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
/**
|
2013-06-20 14:33:59 +08:00
|
|
|
@brief Remove Object from lua state
|
2012-02-01 16:55:46 +08:00
|
|
|
@param object to remove
|
|
|
|
*/
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual void removeScriptObjectByObject(Object* pObj);
|
2012-02-01 16:55:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Remove Lua function reference
|
|
|
|
*/
|
2012-09-11 14:02:33 +08:00
|
|
|
virtual void removeScriptHandler(int nHandler);
|
2012-02-01 16:55:46 +08:00
|
|
|
|
2013-06-05 15:17:00 +08:00
|
|
|
/**
|
|
|
|
@brief Reallocate Lua function reference
|
|
|
|
*/
|
|
|
|
virtual int reallocateScriptHandler(int nHandler);
|
|
|
|
|
2012-02-02 19:00:43 +08:00
|
|
|
/**
|
|
|
|
@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.
|
|
|
|
*/
|
2012-02-09 14:07:11 +08:00
|
|
|
virtual int executeString(const char* codes);
|
2012-02-02 19:00:43 +08:00
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
/**
|
|
|
|
@brief Execute a script file.
|
|
|
|
@param filename String object holding the filename of the script file that is to be executed
|
|
|
|
*/
|
2012-02-09 14:07:11 +08:00
|
|
|
virtual int executeScriptFile(const char* filename);
|
2012-02-01 16:55:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Execute a scripted global function.
|
|
|
|
@brief The function should not take any parameters and should return an integer.
|
2012-02-09 14:07:11 +08:00
|
|
|
@param functionName String object holding the name of the function, in the global script environment, that is to be executed.
|
2012-02-01 16:55:46 +08:00
|
|
|
@return The integer value returned from the script function.
|
|
|
|
*/
|
2012-02-09 14:07:11 +08:00
|
|
|
virtual int executeGlobalFunction(const char* functionName);
|
2012-09-11 14:02:33 +08:00
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual int executeNodeEvent(Node* pNode, int nAction);
|
|
|
|
virtual int executeMenuItemEvent(MenuItem* pMenuItem);
|
|
|
|
virtual int executeNotificationEvent(NotificationCenter* pNotificationCenter, const char* pszName);
|
|
|
|
virtual int executeCallFuncActionEvent(CallFunc* pAction, Object* pTarget = NULL);
|
|
|
|
virtual int executeSchedule(int nHandler, float dt, Node* pNode = NULL);
|
|
|
|
virtual int executeLayerTouchesEvent(Layer* pLayer, int eventType, Set *pTouches);
|
|
|
|
virtual int executeLayerTouchEvent(Layer* pLayer, int eventType, Touch *pTouch);
|
|
|
|
virtual int executeLayerKeypadEvent(Layer* pLayer, int eventType);
|
2012-11-06 14:18:13 +08:00
|
|
|
/** execute a accelerometer event */
|
2013-06-20 14:33:59 +08:00
|
|
|
virtual int executeAccelerometerEvent(Layer* pLayer, Acceleration* pAccelerationValue);
|
|
|
|
virtual int executeEvent(int nHandler, const char* pEventName, Object* pEventSource = NULL, const char* pEventSourceClassName = NULL);
|
2013-04-15 22:50:07 +08:00
|
|
|
|
|
|
|
virtual bool handleAssert(const char *msg);
|
2012-02-01 16:55:46 +08:00
|
|
|
|
2013-07-04 15:44:42 +08:00
|
|
|
virtual int sendEvent(ScriptEvent* message);
|
2013-07-01 15:04:14 +08:00
|
|
|
private:
|
2013-07-04 15:44:42 +08:00
|
|
|
int handleNodeEvent(void* data);
|
|
|
|
int handleMenuClickedEvent(void* data);
|
|
|
|
int handleNotificationEvent(void* data);
|
|
|
|
int handleCallFuncActionEvent(void* data);
|
2013-07-02 15:23:51 +08:00
|
|
|
int handleScheduler(void* data);
|
2013-07-04 15:44:42 +08:00
|
|
|
int handleKeypadEvent(void* data);
|
|
|
|
int handleAccelerometerEvent(void* data);
|
2013-07-02 15:23:51 +08:00
|
|
|
int handleCommonEvent(void* data);
|
2013-07-04 15:44:42 +08:00
|
|
|
int handleTouchesEvent(void* data);
|
|
|
|
int handleLayerTouchesEvent(Layer* layer,int actionType,Set* touches);
|
|
|
|
int handleLayerKeypadEvent(Layer* layer,int actionType);
|
2012-02-01 16:55:46 +08:00
|
|
|
private:
|
2013-06-20 14:33:59 +08:00
|
|
|
LuaEngine(void)
|
2013-06-15 14:03:30 +08:00
|
|
|
: _stack(NULL)
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool init(void);
|
2012-12-26 18:33:55 +08:00
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
static LuaEngine* _defaultEngine;
|
|
|
|
LuaStack *_stack;
|
2012-02-01 16:55:46 +08:00
|
|
|
};
|
2012-09-02 00:38:57 +08:00
|
|
|
|
2012-02-09 14:07:11 +08:00
|
|
|
NS_CC_END
|
2012-02-01 16:55:46 +08:00
|
|
|
|
2012-02-02 15:58:10 +08:00
|
|
|
#endif // __CC_LUA_ENGINE_H__
|