2012-02-09 14:07:11 +08:00
|
|
|
/****************************************************************************
|
2012-09-02 00:38:57 +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.
|
|
|
|
****************************************************************************/
|
2012-02-09 14:07:11 +08:00
|
|
|
|
|
|
|
#ifndef __SCRIPT_SUPPORT_H__
|
|
|
|
#define __SCRIPT_SUPPORT_H__
|
|
|
|
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "platform/CCCommon.h"
|
|
|
|
#include "touch_dispatcher/CCTouch.h"
|
|
|
|
#include "cocoa/CCSet.h"
|
2012-08-25 15:05:59 +08:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
2012-02-09 14:07:11 +08:00
|
|
|
|
|
|
|
typedef struct lua_State lua_State;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-09-11 14:02:33 +08:00
|
|
|
class CCTimer;
|
|
|
|
class CCLayer;
|
|
|
|
class CCMenuItem;
|
|
|
|
class CCNotificationCenter;
|
|
|
|
class CCCallFunc;
|
|
|
|
|
2012-08-31 21:23:23 +08:00
|
|
|
enum ccScriptType {
|
|
|
|
kScriptTypeNone = 0,
|
|
|
|
kScriptTypeLua,
|
|
|
|
kScriptTypeJavascript
|
|
|
|
};
|
|
|
|
|
2012-09-10 18:38:18 +08:00
|
|
|
// #pragma mark -
|
|
|
|
// #pragma mark CCScriptHandlerEntry
|
2012-08-28 12:08:15 +08:00
|
|
|
|
|
|
|
class CCScriptHandlerEntry : public CCObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static CCScriptHandlerEntry* create(int nHandler);
|
|
|
|
~CCScriptHandlerEntry(void);
|
|
|
|
|
|
|
|
int getHandler(void) {
|
|
|
|
return m_nHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getEntryId(void) {
|
|
|
|
return m_nEntryId;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
CCScriptHandlerEntry(int nHandler)
|
|
|
|
: m_nHandler(nHandler)
|
|
|
|
{
|
|
|
|
static int newEntryId = 0;
|
|
|
|
newEntryId++;
|
|
|
|
m_nEntryId = newEntryId;
|
|
|
|
}
|
|
|
|
|
|
|
|
int m_nHandler;
|
|
|
|
int m_nEntryId;
|
|
|
|
};
|
2012-02-09 14:07:11 +08:00
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup script_support
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-09-10 18:38:18 +08:00
|
|
|
// #pragma mark -
|
|
|
|
// #pragma mark CCSchedulerScriptHandlerEntry
|
2012-08-28 12:08:15 +08:00
|
|
|
|
|
|
|
class CCSchedulerScriptHandlerEntry : public CCScriptHandlerEntry
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// nHandler return by tolua_ref_function(), called from LuaCocos2d.cpp
|
2012-08-28 12:08:15 +08:00
|
|
|
static CCSchedulerScriptHandlerEntry* create(int nHandler, float fInterval, bool bPaused);
|
2012-02-09 14:07:11 +08:00
|
|
|
~CCSchedulerScriptHandlerEntry(void);
|
|
|
|
|
2012-08-28 12:08:15 +08:00
|
|
|
cocos2d::CCTimer* getTimer(void) {
|
2012-02-09 14:07:11 +08:00
|
|
|
return m_pTimer;
|
|
|
|
}
|
|
|
|
|
2012-08-28 12:08:15 +08:00
|
|
|
bool isPaused(void) {
|
2012-02-09 14:07:11 +08:00
|
|
|
return m_bPaused;
|
|
|
|
}
|
|
|
|
|
2012-08-28 12:08:15 +08:00
|
|
|
void markedForDeletion(void) {
|
2012-02-09 14:07:11 +08:00
|
|
|
m_bMarkedForDeletion = true;
|
|
|
|
}
|
|
|
|
|
2012-08-28 12:08:15 +08:00
|
|
|
bool isMarkedForDeletion(void) {
|
2012-02-09 14:07:11 +08:00
|
|
|
return m_bMarkedForDeletion;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-08-28 12:08:15 +08:00
|
|
|
CCSchedulerScriptHandlerEntry(int nHandler)
|
|
|
|
: CCScriptHandlerEntry(nHandler)
|
|
|
|
, m_pTimer(NULL)
|
|
|
|
, m_bPaused(false)
|
|
|
|
, m_bMarkedForDeletion(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
bool init(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;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-09-10 18:38:18 +08:00
|
|
|
// #pragma mark -
|
|
|
|
// #pragma mark CCTouchScriptHandlerEntry
|
2012-08-28 12:08:15 +08:00
|
|
|
|
|
|
|
class CCTouchScriptHandlerEntry : public CCScriptHandlerEntry
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
|
|
|
public:
|
2012-08-28 12:08:15 +08:00
|
|
|
static CCTouchScriptHandlerEntry* create(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches);
|
2012-02-09 14:07:11 +08:00
|
|
|
~CCTouchScriptHandlerEntry(void);
|
|
|
|
|
2012-08-28 12:08:15 +08:00
|
|
|
bool isMultiTouches(void) {
|
2012-02-09 14:07:11 +08:00
|
|
|
return m_bIsMultiTouches;
|
|
|
|
}
|
|
|
|
|
2012-08-28 12:08:15 +08:00
|
|
|
int getPriority(void) {
|
2012-02-09 14:07:11 +08:00
|
|
|
return m_nPriority;
|
|
|
|
}
|
|
|
|
|
2012-08-28 12:08:15 +08:00
|
|
|
bool getSwallowsTouches(void) {
|
2012-02-09 14:07:11 +08:00
|
|
|
return m_bSwallowsTouches;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-08-28 12:08:15 +08:00
|
|
|
CCTouchScriptHandlerEntry(int nHandler)
|
|
|
|
: CCScriptHandlerEntry(nHandler)
|
|
|
|
, m_bIsMultiTouches(false)
|
|
|
|
, m_nPriority(0)
|
|
|
|
, m_bSwallowsTouches(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
bool init(bool bIsMultiTouches, int nPriority, bool bSwallowsTouches);
|
2012-02-09 14:07:11 +08:00
|
|
|
|
|
|
|
bool m_bIsMultiTouches;
|
|
|
|
int m_nPriority;
|
|
|
|
bool m_bSwallowsTouches;
|
|
|
|
};
|
|
|
|
|
2012-09-10 18:38:18 +08:00
|
|
|
// #pragma mark -
|
|
|
|
// #pragma mark CCScriptEngineProtocol
|
2012-02-09 14:07:11 +08:00
|
|
|
|
2012-08-29 14:55:55 +08:00
|
|
|
// Don't make CCScriptEngineProtocol inherits from CCObject since setScriptEngine is invoked only once in AppDelegate.cpp,
|
|
|
|
// It will affect the lifecycle of ScriptCore instance, the autorelease pool will be destroyed before destructing ScriptCore.
|
|
|
|
// So a crash will appear on Win32 if you click the close button.
|
2012-09-02 00:38:57 +08:00
|
|
|
class CC_DLL CCScriptEngineProtocol
|
2012-02-09 14:07:11 +08:00
|
|
|
{
|
|
|
|
public:
|
2012-08-29 14:55:55 +08:00
|
|
|
virtual ~CCScriptEngineProtocol() {};
|
2012-09-02 00:38:57 +08:00
|
|
|
|
2012-09-11 14:02:33 +08:00
|
|
|
/** Get script type */
|
2012-08-31 21:23:23 +08:00
|
|
|
virtual ccScriptType getScriptType() { return kScriptTypeNone; };
|
2012-09-10 18:38:18 +08:00
|
|
|
|
2012-09-11 14:02:33 +08:00
|
|
|
/** Remove script object. */
|
|
|
|
virtual void removeScriptObjectByCCObject(CCObject* pObj) = 0;
|
2012-02-09 14:07:11 +08:00
|
|
|
|
2012-09-11 14:02:33 +08:00
|
|
|
/** Remove script function handler, only CCLuaEngine class need to implement this function. */
|
|
|
|
virtual void removeScriptHandler(int nHandler) {};
|
2012-02-09 14:07:11 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Execute script code contained in the given string.
|
|
|
|
@param codes holding the valid script code that should be executed.
|
2012-09-17 15:02:24 +08:00
|
|
|
@return 0 if the string is executed correctly.
|
|
|
|
@return other if the string is executed wrongly.
|
2012-02-09 14:07:11 +08:00
|
|
|
*/
|
|
|
|
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-09-11 14:02:33 +08:00
|
|
|
@brief Execute a node event function
|
|
|
|
@param pNode which node produce this event
|
|
|
|
@param nAction kCCNodeOnEnter,kCCNodeOnExit,kCCMenuItemActivated,kCCNodeOnEnterTransitionDidFinish,kCCNodeOnExitTransitionDidStart
|
2012-02-09 14:07:11 +08:00
|
|
|
@return The integer value returned from the script function.
|
|
|
|
*/
|
2012-09-11 14:02:33 +08:00
|
|
|
virtual int executeNodeEvent(CCNode* pNode, int nAction) = 0;
|
|
|
|
|
|
|
|
virtual int executeMenuItemEvent(CCMenuItem* pMenuItem) = 0;
|
|
|
|
/** Execute a notification event function */
|
|
|
|
virtual int executeNotificationEvent(CCNotificationCenter* pNotificationCenter, const char* pszName) = 0;
|
2012-08-27 14:05:38 +08:00
|
|
|
|
2012-09-11 14:02:33 +08:00
|
|
|
/** execute a callfun event */
|
|
|
|
virtual int executeCallFuncActionEvent(CCCallFunc* pAction, CCObject* pTarget = NULL) = 0;
|
|
|
|
/** execute a schedule function */
|
|
|
|
virtual int executeSchedule(CCTimer* pTimer, float dt, CCNode* pNode = NULL) = 0;
|
2012-02-09 14:07:11 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
/** functions for execute touch event */
|
2012-09-11 14:02:33 +08:00
|
|
|
virtual int executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches) = 0;
|
|
|
|
virtual int executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch) = 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-09-02 00:38:57 +08:00
|
|
|
|
2012-02-09 17:48:15 +08:00
|
|
|
static CCScriptEngineManager* sharedManager(void);
|
2012-02-09 14:07:11 +08:00
|
|
|
static void purgeSharedManager(void);
|
2012-09-02 00:38:57 +08:00
|
|
|
|
2012-02-09 14:07:11 +08:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of script_support group
|
|
|
|
/// @}
|
|
|
|
|
2012-02-09 14:07:11 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // __SCRIPT_SUPPORT_H__
|