2012-02-09 14:07:11 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __SCRIPT_SUPPORT_H__
|
|
|
|
#define __SCRIPT_SUPPORT_H__
|
|
|
|
|
|
|
|
#include "CCCommon.h"
|
|
|
|
#include "CCTouch.h"
|
|
|
|
#include "CCSet.h"
|
|
|
|
|
|
|
|
typedef struct lua_State lua_State;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-06-12 17:07:54 +08:00
|
|
|
class CCTimer;
|
2012-02-09 14:07:11 +08:00
|
|
|
|
|
|
|
// Lua support for CCScheduler
|
|
|
|
class CCSchedulerScriptHandlerEntry : public CCObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// nHandler return by tolua_ref_function(), called from LuaCocos2d.cpp
|
2012-06-08 13:55:28 +08:00
|
|
|
static CCSchedulerScriptHandlerEntry* entryWithHandler(int nHandler, float fInterval, bool bPaused);
|
2012-02-09 14:07:11 +08:00
|
|
|
~CCSchedulerScriptHandlerEntry(void);
|
|
|
|
|
2012-06-12 17:07:54 +08:00
|
|
|
inline cocos2d::CCTimer* getTimer(void) {
|
2012-02-09 14:07:11 +08:00
|
|
|
return m_pTimer;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isPaused(void) {
|
|
|
|
return m_bPaused;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int getEntryID(void) {
|
|
|
|
return m_nEntryID;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void markedForDeletion(void) {
|
|
|
|
m_bMarkedForDeletion = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isMarkedForDeletion(void) {
|
|
|
|
return m_bMarkedForDeletion;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CCSchedulerScriptHandlerEntry(void);
|
2012-06-08 13:55:28 +08:00
|
|
|
bool initWithHandler(int nHandler, float fInterval, bool bPaused);
|
2012-02-09 14:07:11 +08:00
|
|
|
|
2012-06-12 17:07:54 +08:00
|
|
|
cocos2d::CCTimer* m_pTimer;
|
2012-02-09 14:07:11 +08:00
|
|
|
bool m_bPaused;
|
|
|
|
bool m_bMarkedForDeletion;
|
|
|
|
int m_nHandler;
|
|
|
|
int m_nEntryID;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Lua support for touch events
|
|
|
|
class CCTouchScriptHandlerEntry : public CCObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static CCTouchScriptHandlerEntry* entryWithHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches);
|
|
|
|
~CCTouchScriptHandlerEntry(void);
|
|
|
|
|
|
|
|
inline int getHandler(void) {
|
|
|
|
return m_nHandler;
|
|
|
|
}
|
|
|
|
|
2012-06-15 16:47:30 +08:00
|
|
|
inline bool isMultiTouches(void) {
|
2012-02-09 14:07:11 +08:00
|
|
|
return m_bIsMultiTouches;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int getPriority(void) {
|
|
|
|
return m_nPriority;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool getSwallowsTouches(void) {
|
|
|
|
return m_bSwallowsTouches;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CCTouchScriptHandlerEntry(void);
|
|
|
|
bool initWithHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches);
|
|
|
|
|
|
|
|
int m_nHandler;
|
|
|
|
bool m_bIsMultiTouches;
|
|
|
|
int m_nPriority;
|
|
|
|
bool m_bSwallowsTouches;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class CC_DLL CCScriptEngineProtocol : public CCObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
@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.
|
|
|
|
*/
|
|
|
|
virtual lua_State* getLuaState(void) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Remove CCObject from lua state
|
|
|
|
@param object to remove
|
|
|
|
*/
|
|
|
|
virtual void removeCCObjectByID(int nLuaID) = 0;
|
|
|
|
|
|
|
|
/**
|
2012-03-07 20:27:26 +08:00
|
|
|
@brief Remove Lua function handler
|
2012-02-09 14:07:11 +08:00
|
|
|
*/
|
|
|
|
virtual void removeLuaHandler(int nHandler) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Add a path to find lua files in
|
|
|
|
@param path to be added to the Lua path
|
|
|
|
*/
|
|
|
|
virtual void addSearchPath(const char* path) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
@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) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
@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) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
@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) = 0;
|
|
|
|
|
|
|
|
/**
|
2012-03-07 20:27:26 +08:00
|
|
|
@brief Execute a function by handler
|
|
|
|
@param The function handler
|
2012-02-09 14:07:11 +08:00
|
|
|
@param Number of parameters
|
|
|
|
@return The integer value returned from the script function.
|
|
|
|
*/
|
2012-03-07 20:27:26 +08:00
|
|
|
virtual int executeFunctionByHandler(int nHandler, int numArgs = 0) = 0;
|
2012-02-09 14:07:11 +08:00
|
|
|
virtual int executeFunctionWithIntegerData(int nHandler, int data) = 0;
|
|
|
|
virtual int executeFunctionWithFloatData(int nHandler, float data) = 0;
|
|
|
|
virtual int executeFunctionWithBooleanData(int nHandler, bool data) = 0;
|
2012-03-07 20:27:26 +08:00
|
|
|
virtual int executeFunctionWithCCObject(int nHandler, CCObject* pObject, const char* typeName) = 0;
|
|
|
|
virtual int pushIntegerToLuaStack(int data) = 0;
|
|
|
|
virtual int pushFloatToLuaStack(int data) = 0;
|
|
|
|
virtual int pushBooleanToLuaStack(int data) = 0;
|
|
|
|
virtual int pushCCObjectToLuaStack(CCObject* pObject, const char* typeName) = 0;
|
2012-02-09 14:07:11 +08:00
|
|
|
|
|
|
|
// functions for excute touch event
|
|
|
|
virtual int executeTouchEvent(int nHandler, int eventType, CCTouch *pTouch) = 0;
|
|
|
|
virtual int executeTouchesEvent(int nHandler, int eventType, CCSet *pTouches) = 0;
|
|
|
|
|
|
|
|
// execute a schedule function
|
2012-06-08 13:55:28 +08:00
|
|
|
virtual int executeSchedule(int nHandler, float dt) = 0;
|
2012-02-09 14:07:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
CCScriptEngineManager is a singleton which holds an object instance of CCScriptEngineProtocl
|
|
|
|
It helps cocos2d-x and the user code to find back LuaEngine object
|
|
|
|
@since v0.99.5-x-0.8.5
|
|
|
|
*/
|
|
|
|
class CC_DLL CCScriptEngineManager
|
|
|
|
{
|
|
|
|
public:
|
2012-04-19 14:35:52 +08:00
|
|
|
~CCScriptEngineManager(void);
|
2012-02-09 14:07:11 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCScriptEngineProtocol* getScriptEngine(void) {
|
2012-02-09 14:07:11 +08:00
|
|
|
return m_pScriptEngine;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
void setScriptEngine(CCScriptEngineProtocol *pScriptEngine);
|
2012-02-09 14:07:11 +08:00
|
|
|
void removeScriptEngine(void);
|
|
|
|
|
2012-02-09 17:48:15 +08:00
|
|
|
static CCScriptEngineManager* sharedManager(void);
|
2012-02-09 14:07:11 +08:00
|
|
|
static void purgeSharedManager(void);
|
|
|
|
|
|
|
|
private:
|
|
|
|
CCScriptEngineManager(void)
|
|
|
|
: m_pScriptEngine(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCScriptEngineProtocol *m_pScriptEngine;
|
2012-02-09 14:07:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // __SCRIPT_SUPPORT_H__
|