2012-02-09 14:07:11 +08:00
/****************************************************************************
2012-09-24 21:22:20 +08:00
Copyright ( c ) 2010 - 2012 cocos2d - x . org
2014-01-07 11:25:07 +08:00
Copyright ( c ) 2013 - 2014 Chukong Technologies Inc .
2012-09-02 00:38:57 +08:00
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__
2014-02-20 17:23:22 +08:00
# include "ccConfig.h"
2014-02-20 16:40:46 +08:00
# if CC_ENABLE_SCRIPT_BINDING
2012-06-19 13:50:11 +08:00
# include "platform/CCCommon.h"
2013-10-14 14:01:00 +08:00
# include "CCTouch.h"
# include "CCEventTouch.h"
# include "CCEventKeyboard.h"
# include "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
2014-03-01 13:30:20 +08:00
class TimerScriptHandler ;
2013-06-20 14:13:12 +08:00
class Layer ;
class MenuItem ;
class CallFunc ;
class Acceleration ;
2012-09-11 14:02:33 +08:00
2012-08-31 21:23:23 +08:00
enum ccScriptType {
kScriptTypeNone = 0 ,
kScriptTypeLua ,
kScriptTypeJavascript
} ;
2014-02-20 10:53:49 +08:00
class ScriptHandlerEntry : public Ref
2012-08-28 12:08:15 +08:00
{
public :
2013-12-18 17:47:20 +08:00
static ScriptHandlerEntry * create ( int handler ) ;
2013-09-13 13:52:42 +08:00
/**
* @ js NA
* @ lua NA
*/
2014-02-20 21:58:20 +08:00
virtual ~ ScriptHandlerEntry ( ) ;
2012-08-28 12:08:15 +08:00
int getHandler ( void ) {
2013-06-15 14:03:30 +08:00
return _handler ;
2012-08-28 12:08:15 +08:00
}
int getEntryId ( void ) {
2013-06-15 14:03:30 +08:00
return _entryId ;
2012-08-28 12:08:15 +08:00
}
protected :
2013-12-18 17:47:20 +08:00
ScriptHandlerEntry ( int handler )
: _handler ( handler )
2012-08-28 12:08:15 +08:00
{
static int newEntryId = 0 ;
newEntryId + + ;
2013-06-15 14:03:30 +08:00
_entryId = newEntryId ;
2012-08-28 12:08:15 +08:00
}
2013-06-15 14:03:30 +08:00
int _handler ;
int _entryId ;
2012-08-28 12:08:15 +08:00
} ;
2012-02-09 14:07:11 +08:00
2012-06-20 18:09:11 +08:00
/**
* @ addtogroup script_support
* @ {
*/
2013-06-20 14:13:12 +08:00
class SchedulerScriptHandlerEntry : public ScriptHandlerEntry
2012-02-09 14:07:11 +08:00
{
public :
// nHandler return by tolua_ref_function(), called from LuaCocos2d.cpp
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-12-18 17:47:20 +08:00
static SchedulerScriptHandlerEntry * create ( int handler , float interval , bool paused ) ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2014-02-20 21:58:20 +08:00
virtual ~ SchedulerScriptHandlerEntry ( ) ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2014-03-01 13:30:20 +08:00
TimerScriptHandler * getTimer ( void ) {
2013-06-15 14:03:30 +08:00
return _timer ;
2012-02-09 14:07:11 +08:00
}
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2012-08-28 12:08:15 +08:00
bool isPaused ( void ) {
2013-06-15 14:03:30 +08:00
return _paused ;
2012-02-09 14:07:11 +08:00
}
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2012-08-28 12:08:15 +08:00
void markedForDeletion ( void ) {
2013-06-15 14:03:30 +08:00
_markedForDeletion = true ;
2012-02-09 14:07:11 +08:00
}
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2012-08-28 12:08:15 +08:00
bool isMarkedForDeletion ( void ) {
2013-06-15 14:03:30 +08:00
return _markedForDeletion ;
2012-02-09 14:07:11 +08:00
}
private :
2013-12-18 17:47:20 +08:00
SchedulerScriptHandlerEntry ( int handler )
: ScriptHandlerEntry ( handler )
, _timer ( nullptr )
2013-06-15 14:03:30 +08:00
, _paused ( false )
, _markedForDeletion ( false )
2012-08-28 12:08:15 +08:00
{
}
2013-12-18 17:47:20 +08:00
bool init ( float interval , bool paused ) ;
2012-02-09 14:07:11 +08:00
2014-03-01 13:30:20 +08:00
TimerScriptHandler * _timer ;
2013-06-15 14:03:30 +08:00
bool _paused ;
bool _markedForDeletion ;
2012-02-09 14:07:11 +08:00
} ;
2012-08-28 12:08:15 +08:00
2013-06-20 14:13:12 +08:00
class TouchScriptHandlerEntry : public ScriptHandlerEntry
2012-02-09 14:07:11 +08:00
{
public :
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-12-18 17:47:20 +08:00
static TouchScriptHandlerEntry * create ( int handler , bool isMultiTouches , int priority , bool swallowsTouches ) ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2014-02-20 21:58:20 +08:00
virtual ~ TouchScriptHandlerEntry ( ) ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2012-08-28 12:08:15 +08:00
bool isMultiTouches ( void ) {
2013-06-15 14:03:30 +08:00
return _isMultiTouches ;
2012-02-09 14:07:11 +08:00
}
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2012-08-28 12:08:15 +08:00
int getPriority ( void ) {
2013-06-15 14:03:30 +08:00
return _priority ;
2012-02-09 14:07:11 +08:00
}
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2012-08-28 12:08:15 +08:00
bool getSwallowsTouches ( void ) {
2013-06-15 14:03:30 +08:00
return _swallowsTouches ;
2012-02-09 14:07:11 +08:00
}
private :
2013-12-18 17:47:20 +08:00
TouchScriptHandlerEntry ( int handler )
: ScriptHandlerEntry ( handler )
2013-06-15 14:03:30 +08:00
, _isMultiTouches ( false )
, _priority ( 0 )
, _swallowsTouches ( false )
2012-08-28 12:08:15 +08:00
{
}
2013-12-18 17:47:20 +08:00
bool init ( bool isMultiTouches , int priority , bool swallowsTouches ) ;
2012-02-09 14:07:11 +08:00
2013-06-15 14:03:30 +08:00
bool _isMultiTouches ;
int _priority ;
bool _swallowsTouches ;
2012-02-09 14:07:11 +08:00
} ;
2013-07-02 15:23:51 +08:00
enum ScriptEventType
{
kNodeEvent = 0 ,
2013-07-04 15:44:42 +08:00
kMenuClickedEvent ,
2013-07-02 15:23:51 +08:00
kCallFuncEvent ,
kScheduleEvent ,
2013-07-22 17:12:53 +08:00
kTouchEvent ,
2013-07-04 15:44:42 +08:00
kTouchesEvent ,
kKeypadEvent ,
2013-07-02 15:23:51 +08:00
kAccelerometerEvent ,
2013-07-16 09:55:06 +08:00
kControlEvent ,
2013-07-02 15:23:51 +08:00
kCommonEvent ,
} ;
2013-07-04 15:44:42 +08:00
struct BasicScriptData
{
2013-07-22 17:12:53 +08:00
// nativeobject:to get handler for lua or to get jsobject for js
2013-07-04 15:44:42 +08:00
void * nativeObject ;
2013-07-22 17:12:53 +08:00
// value: a pointer to a object that already defined
2013-07-04 15:44:42 +08:00
void * value ;
2013-07-22 17:12:53 +08:00
// Constructor
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-12-18 17:47:20 +08:00
BasicScriptData ( void * inObject , void * inValue = nullptr )
2013-07-22 17:12:53 +08:00
: nativeObject ( inObject ) , value ( inValue )
2013-07-04 15:44:42 +08:00
{
}
} ;
struct SchedulerScriptData
2013-07-02 15:23:51 +08:00
{
2013-07-22 17:12:53 +08:00
// lua use
2013-07-02 15:23:51 +08:00
int handler ;
float elapse ;
2013-07-22 17:12:53 +08:00
// js use
void * node ;
// Constructor
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-12-18 17:47:20 +08:00
SchedulerScriptData ( int inHandler , float inElapse , void * inNode = nullptr )
2013-07-22 17:12:53 +08:00
: handler ( inHandler ) ,
elapse ( inElapse ) ,
node ( inNode )
2013-07-03 14:19:00 +08:00
{
}
2013-07-02 15:23:51 +08:00
} ;
2013-07-04 15:44:42 +08:00
struct TouchesScriptData
2013-07-02 15:23:51 +08:00
{
2013-09-20 19:19:31 +08:00
EventTouch : : EventCode actionType ;
2013-07-04 15:44:42 +08:00
void * nativeObject ;
2013-09-03 18:22:03 +08:00
const std : : vector < Touch * > & touches ;
2014-03-04 13:42:06 +08:00
Event * event ;
2013-07-22 17:12:53 +08:00
// Constructor
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2014-03-04 13:42:06 +08:00
TouchesScriptData ( EventTouch : : EventCode inActionType , void * inNativeObject , const std : : vector < Touch * > & inTouches , Event * evt )
2013-07-22 17:12:53 +08:00
: actionType ( inActionType ) ,
nativeObject ( inNativeObject ) ,
2014-03-04 13:42:06 +08:00
touches ( inTouches ) ,
event ( evt )
2013-07-22 17:12:53 +08:00
{
}
} ;
struct TouchScriptData
{
2013-09-20 19:19:31 +08:00
EventTouch : : EventCode actionType ;
2013-07-22 17:12:53 +08:00
void * nativeObject ;
Touch * touch ;
2014-03-04 13:42:06 +08:00
Event * event ;
2013-07-22 17:12:53 +08:00
// Constructor
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2014-03-04 13:42:06 +08:00
TouchScriptData ( EventTouch : : EventCode inActionType , void * inNativeObject , Touch * inTouch , Event * evt )
2013-07-22 17:12:53 +08:00
: actionType ( inActionType ) ,
nativeObject ( inNativeObject ) ,
2014-03-04 13:42:06 +08:00
touch ( inTouch ) ,
event ( evt )
2013-07-03 14:19:00 +08:00
{
}
2013-07-02 15:23:51 +08:00
} ;
2013-07-04 15:44:42 +08:00
struct KeypadScriptData
2013-07-02 15:23:51 +08:00
{
2013-09-20 19:19:31 +08:00
EventKeyboard : : KeyCode actionType ;
2013-07-04 15:44:42 +08:00
void * nativeObject ;
2013-07-22 17:12:53 +08:00
// Constructor
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-09-20 19:19:31 +08:00
KeypadScriptData ( EventKeyboard : : KeyCode inActionType , void * inNativeObject )
2013-07-22 17:12:53 +08:00
: actionType ( inActionType ) , nativeObject ( inNativeObject )
2013-07-03 14:19:00 +08:00
{
}
2013-07-02 15:23:51 +08:00
} ;
2013-07-04 15:44:42 +08:00
struct CommonScriptData
2013-07-02 15:23:51 +08:00
{
2013-07-22 17:12:53 +08:00
// Now this struct is only used in LuaBinding.
2013-07-02 15:23:51 +08:00
int handler ;
char eventName [ 64 ] ;
2014-02-20 10:53:49 +08:00
Ref * eventSource ;
2013-07-02 15:23:51 +08:00
char eventSourceClassName [ 64 ] ;
2013-07-22 17:12:53 +08:00
// Constructor
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2014-02-20 10:53:49 +08:00
CommonScriptData ( int inHandler , const char * inName , Ref * inSource = nullptr , const char * inClassName = nullptr )
2013-07-22 17:12:53 +08:00
: handler ( inHandler ) ,
eventSource ( inSource )
2013-07-02 15:23:51 +08:00
{
2013-07-03 14:19:00 +08:00
strncpy ( eventName , inName , 64 ) ;
2013-07-02 15:23:51 +08:00
2013-12-18 17:47:20 +08:00
if ( nullptr = = inClassName )
2013-07-02 15:23:51 +08:00
{
memset ( eventSourceClassName , 0 , 64 * sizeof ( char ) ) ;
}
else
{
2013-07-03 14:19:00 +08:00
strncpy ( eventSourceClassName , inClassName , 64 ) ;
2013-07-02 15:23:51 +08:00
}
}
} ;
struct ScriptEvent
{
ScriptEventType type ;
void * data ;
2013-07-22 17:12:53 +08:00
// Constructor
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-07-02 15:23:51 +08:00
ScriptEvent ( ScriptEventType inType , void * inData )
2013-07-22 17:12:53 +08:00
: type ( inType ) ,
data ( inData )
2013-07-02 15:23:51 +08:00
{
}
} ;
2012-02-09 14:07:11 +08:00
2013-06-20 14:13:12 +08:00
// Don't make ScriptEngineProtocol inherits from Object since setScriptEngine is invoked only once in AppDelegate.cpp,
2012-08-29 14:55:55 +08:00
// 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.
2013-06-20 14:13:12 +08:00
class CC_DLL ScriptEngineProtocol
2012-02-09 14:07:11 +08:00
{
public :
2014-03-08 11:51:10 +08:00
ScriptEngineProtocol ( )
{ } ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-06-20 14:13:12 +08:00
virtual ~ ScriptEngineProtocol ( ) { } ;
2012-09-02 00:38:57 +08:00
2013-09-13 11:41:20 +08:00
/** Get script type
* @ js NA
* @ lua NA
*/
2012-08-31 21:23:23 +08:00
virtual ccScriptType getScriptType ( ) { return kScriptTypeNone ; } ;
2012-09-10 18:38:18 +08:00
2013-09-13 11:41:20 +08:00
/** Remove script object.
* @ js NA
* @ lua NA
*/
2014-02-20 10:53:49 +08:00
virtual void removeScriptObjectByObject ( Ref * obj ) = 0 ;
2012-02-09 14:07:11 +08:00
2013-09-13 11:41:20 +08:00
/** Remove script function handler, only LuaEngine class need to implement this function.
* @ js NA
* @ lua NA
*/
2013-12-18 17:47:20 +08:00
virtual void removeScriptHandler ( int handler ) { } ;
2012-02-09 14:07:11 +08:00
2013-09-13 11:41:20 +08:00
/** Reallocate script function handler, only LuaEngine class need to implement this function.
* @ js NA
* @ lua NA
*/
2013-12-18 17:47:20 +08:00
virtual int reallocateScriptHandler ( int handler ) { return 0 ; }
2013-06-05 15:17:00 +08:00
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 .
2013-09-13 11:41:20 +08:00
* @ js NA
* @ lua NA
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
2013-09-13 11:41:20 +08:00
* @ js NA
* @ lua NA
2012-02-09 14:07:11 +08:00
*/
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 .
2013-09-13 11:41:20 +08:00
* @ js NA
* @ lua NA
2012-02-09 14:07:11 +08:00
*/
virtual int executeGlobalFunction ( const char * functionName ) = 0 ;
2013-12-18 17:47:20 +08:00
/**when trigger a script event ,call this func,add params needed into ScriptEvent object.nativeObject is object triggering the event, can be nullptr in lua
2013-09-13 11:41:20 +08:00
* @ js NA
* @ lua NA
*/
2013-07-22 17:12:53 +08:00
virtual int sendEvent ( ScriptEvent * evt ) = 0 ;
2012-02-09 14:07:11 +08:00
2013-04-15 22:50:07 +08:00
/** called by CCAssert to allow scripting engine to handle failed assertions
* @ return true if the assert was handled by the script engine , false otherwise .
2013-09-13 11:41:20 +08:00
* @ js NA
* @ lua NA
2013-04-15 22:50:07 +08:00
*/
virtual bool handleAssert ( const char * msg ) = 0 ;
2013-12-30 20:57:46 +08:00
2014-03-08 11:51:10 +08:00
virtual void setCalledFromScript ( bool callFromScript ) { CC_UNUSED_PARAM ( callFromScript ) ; } ;
virtual bool isCalledFromScript ( ) { return false ; } ;
2013-12-30 20:57:46 +08:00
enum class ConfigType
{
NONE ,
COCOSTUDIO
} ;
/** Parse configuration file */
virtual bool parseConfig ( ConfigType type , const std : : string & str ) = 0 ;
2012-02-09 14:07:11 +08:00
} ;
/**
2013-06-20 14:13:12 +08:00
ScriptEngineManager is a singleton which holds an object instance of ScriptEngineProtocl
2012-02-09 14:07:11 +08:00
It helps cocos2d - x and the user code to find back LuaEngine object
@ since v0 .99 .5 - x - 0.8 .5
*/
2013-06-20 14:13:12 +08:00
class CC_DLL ScriptEngineManager
2012-02-09 14:07:11 +08:00
{
public :
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-06-20 14:13:12 +08:00
~ ScriptEngineManager ( void ) ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-06-20 14:13:12 +08:00
ScriptEngineProtocol * getScriptEngine ( void ) {
2013-06-15 14:03:30 +08:00
return _scriptEngine ;
2012-02-09 14:07:11 +08:00
}
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-12-18 17:47:20 +08:00
void setScriptEngine ( ScriptEngineProtocol * scriptEngine ) ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2012-02-09 14:07:11 +08:00
void removeScriptEngine ( void ) ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-07-22 17:19:31 +08:00
static ScriptEngineManager * getInstance ( ) ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-07-22 17:19:31 +08:00
static void destroyInstance ( ) ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-07-22 17:19:31 +08:00
CC_DEPRECATED_ATTRIBUTE static ScriptEngineManager * sharedManager ( ) { return ScriptEngineManager : : getInstance ( ) ; } ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-07-22 17:19:31 +08:00
CC_DEPRECATED_ATTRIBUTE static void purgeSharedManager ( ) { ScriptEngineManager : : destroyInstance ( ) ; } ;
2012-09-02 00:38:57 +08:00
2012-02-09 14:07:11 +08:00
private :
2013-06-20 14:13:12 +08:00
ScriptEngineManager ( void )
2013-12-18 17:47:20 +08:00
: _scriptEngine ( nullptr )
2012-02-09 14:07:11 +08:00
{
}
2013-06-20 14:13:12 +08:00
ScriptEngineProtocol * _scriptEngine ;
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
2014-02-20 16:40:46 +08:00
# endif // #if CC_ENABLE_SCRIPT_BINDING
2012-02-09 14:07:11 +08:00
# endif // __SCRIPT_SUPPORT_H__