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"
|
2012-02-01 16:55:46 +08:00
|
|
|
|
2012-12-22 10:13:37 +08:00
|
|
|
void cc_lua_assert(bool cond, const char *msg);
|
|
|
|
|
2012-02-09 14:07:11 +08:00
|
|
|
NS_CC_BEGIN
|
2012-02-01 16:55:46 +08:00
|
|
|
|
2012-09-11 15:44:33 +08:00
|
|
|
typedef int LUA_FUNCTION;
|
|
|
|
typedef int LUA_TABLE;
|
|
|
|
typedef int LUA_STRING;
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
class CCLuaValue;
|
2012-09-11 14:02:33 +08:00
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
typedef std::map<std::string, CCLuaValue> CCLuaValueDict;
|
|
|
|
typedef CCLuaValueDict::const_iterator CCLuaValueDictIterator;
|
|
|
|
typedef std::list<CCLuaValue> CCLuaValueArray;
|
|
|
|
typedef CCLuaValueArray::const_iterator CCLuaValueArrayIterator;
|
2012-09-11 14:02:33 +08:00
|
|
|
|
|
|
|
typedef enum {
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaValueTypeInt,
|
|
|
|
CCLuaValueTypeFloat,
|
|
|
|
CCLuaValueTypeBoolean,
|
|
|
|
CCLuaValueTypeString,
|
|
|
|
CCLuaValueTypeDict,
|
|
|
|
CCLuaValueTypeArray,
|
|
|
|
CCLuaValueTypeCCObject
|
|
|
|
} CCLuaValueType;
|
2012-09-11 14:02:33 +08:00
|
|
|
|
|
|
|
typedef union {
|
|
|
|
int intValue;
|
|
|
|
float floatValue;
|
|
|
|
bool booleanValue;
|
|
|
|
std::string* stringValue;
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaValueDict* dictValue;
|
|
|
|
CCLuaValueArray* arrayValue;
|
2012-09-11 14:02:33 +08:00
|
|
|
CCObject* ccobjectValue;
|
2012-09-11 15:24:25 +08:00
|
|
|
} CCLuaValueField;
|
2012-09-11 14:02:33 +08:00
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
class CCLuaValue
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
|
|
|
public:
|
2012-09-11 15:24:25 +08:00
|
|
|
static const CCLuaValue intValue(const int intValue);
|
|
|
|
static const CCLuaValue floatValue(const float floatValue);
|
|
|
|
static const CCLuaValue booleanValue(const bool booleanValue);
|
|
|
|
static const CCLuaValue stringValue(const char* stringValue);
|
|
|
|
static const CCLuaValue stringValue(const std::string& stringValue);
|
|
|
|
static const CCLuaValue dictValue(const CCLuaValueDict& dictValue);
|
|
|
|
static const CCLuaValue arrayValue(const CCLuaValueArray& arrayValue);
|
|
|
|
static const CCLuaValue ccobjectValue(CCObject* ccobjectValue, const char* objectTypename);
|
|
|
|
static const CCLuaValue ccobjectValue(CCObject* ccobjectValue, const std::string& objectTypename);
|
|
|
|
|
|
|
|
CCLuaValue(void)
|
|
|
|
: m_type(CCLuaValueTypeInt)
|
2012-09-11 14:02:33 +08:00
|
|
|
, m_ccobjectType(NULL)
|
|
|
|
{
|
|
|
|
memset(&m_field, 0, sizeof(m_field));
|
|
|
|
}
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaValue(const CCLuaValue& rhs);
|
|
|
|
CCLuaValue& operator=(const CCLuaValue& rhs);
|
|
|
|
~CCLuaValue(void);
|
2012-09-11 14:02:33 +08:00
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
const CCLuaValueType getType(void) const {
|
2012-09-11 14:02:33 +08:00
|
|
|
return m_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& getCCObjectTypename(void) const {
|
|
|
|
return *m_ccobjectType;
|
|
|
|
}
|
|
|
|
|
|
|
|
int intValue(void) const {
|
|
|
|
return m_field.intValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
float floatValue(void) const {
|
|
|
|
return m_field.floatValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool booleanValue(void) const {
|
|
|
|
return m_field.booleanValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& stringValue(void) const {
|
|
|
|
return *m_field.stringValue;
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
const CCLuaValueDict& dictValue(void) const {
|
2012-09-11 14:02:33 +08:00
|
|
|
return *m_field.dictValue;
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
const CCLuaValueArray& arrayValue(void) const {
|
2012-09-11 14:02:33 +08:00
|
|
|
return *m_field.arrayValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCObject* ccobjectValue(void) const {
|
|
|
|
return m_field.ccobjectValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaValueField m_field;
|
|
|
|
CCLuaValueType m_type;
|
|
|
|
std::string* m_ccobjectType;
|
2012-09-11 14:02:33 +08:00
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
void copy(const CCLuaValue& rhs);
|
2012-09-11 14:02:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
// Lua support for cocos2d-x
|
2012-02-09 14:07:11 +08:00
|
|
|
class CCLuaEngine : public CCScriptEngineProtocol
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
|
|
|
public:
|
2012-09-11 16:11:42 +08:00
|
|
|
static CCLuaEngine* defaultEngine(void);
|
2012-09-11 15:24:25 +08:00
|
|
|
static CCLuaEngine* create(void);
|
|
|
|
virtual ~CCLuaEngine(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
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
/**
|
|
|
|
@brief Remove CCObject from lua state
|
|
|
|
@param object to remove
|
|
|
|
*/
|
2012-09-11 14:02:33 +08:00
|
|
|
virtual void removeScriptObjectByCCObject(CCObject* 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
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Add a path to find lua files in
|
|
|
|
@param path to be added to the Lua path
|
|
|
|
*/
|
2012-02-09 14:07:11 +08:00
|
|
|
virtual void addSearchPath(const char* path);
|
2012-02-01 16:55:46 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
virtual int executeNodeEvent(CCNode* pNode, int nAction);
|
|
|
|
virtual int executeMenuItemEvent(CCMenuItem* pMenuItem);
|
|
|
|
virtual int executeNotificationEvent(CCNotificationCenter* pNotificationCenter, const char* pszName);
|
|
|
|
virtual int executeCallFuncActionEvent(CCCallFunc* pAction, CCObject* pTarget = NULL);
|
2012-12-10 13:48:27 +08:00
|
|
|
virtual int executeSchedule(int nHandler, float dt, CCNode* pNode = NULL);
|
2012-09-11 14:02:33 +08:00
|
|
|
virtual int executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches);
|
|
|
|
virtual int executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch);
|
2012-12-02 15:17:34 +08:00
|
|
|
virtual int executeLayerKeypadEvent(CCLayer* pLayer, int eventType);
|
2012-11-06 14:18:13 +08:00
|
|
|
/** execute a accelerometer event */
|
2012-12-02 15:17:34 +08:00
|
|
|
virtual int executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue);
|
2012-09-11 15:24:25 +08:00
|
|
|
/**
|
|
|
|
@brief Method used to get a pointer to the lua_State that the script module is attached to.
|
|
|
|
@return A pointer to the lua_State that the script module is attached to.
|
|
|
|
*/
|
|
|
|
lua_State* getLuaState(void) {
|
|
|
|
return m_state;
|
|
|
|
}
|
2012-09-11 14:02:33 +08:00
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
int pushInt(int data);
|
|
|
|
int pushFloat(float data);
|
|
|
|
int pushBoolean(bool data);
|
|
|
|
int pushString(const char* data);
|
|
|
|
int pushString(const char* data, int length);
|
|
|
|
int pushNil(void);
|
|
|
|
int pushCCObject(CCObject* pObject, const char* typeName);
|
|
|
|
int pushCCLuaValue(const CCLuaValue& value);
|
|
|
|
int pushCCLuaValueDict(const CCLuaValueDict& dict);
|
|
|
|
int pushCCLuaValueArray(const CCLuaValueArray& array);
|
2012-09-11 14:02:33 +08:00
|
|
|
int executeFunctionByHandler(int nHandler, int numArgs);
|
2012-09-11 15:24:25 +08:00
|
|
|
void cleanStack(void);
|
2012-02-01 16:55:46 +08:00
|
|
|
|
2012-02-07 11:43:29 +08:00
|
|
|
// Add lua loader, now it is used on android
|
2012-09-11 15:24:25 +08:00
|
|
|
void addLuaLoader(lua_CFunction func);
|
2012-02-01 16:55:46 +08:00
|
|
|
|
|
|
|
private:
|
2012-02-09 14:07:11 +08:00
|
|
|
CCLuaEngine(void)
|
|
|
|
: m_state(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool init(void);
|
2012-08-25 15:05:59 +08:00
|
|
|
bool pushFunction(int nHandler);
|
2012-02-01 16:55:46 +08:00
|
|
|
|
|
|
|
lua_State* m_state;
|
2012-09-11 15:24:25 +08:00
|
|
|
static CCLuaEngine* m_defaultEngine;
|
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__
|