axmol/cocos/base/CCScriptSupport.h

537 lines
12 KiB
C
Raw Normal View History

2012-02-09 14:07:11 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
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-04-30 08:37:36 +08:00
#include "base/ccConfig.h"
#if CC_ENABLE_SCRIPT_BINDING
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "platform/CCCommon.h"
2014-04-30 08:37:36 +08:00
#include "base/CCTouch.h"
#include "base/CCEventTouch.h"
#include "base/CCEventKeyboard.h"
#include <map>
#include <string>
#include <list>
2012-02-09 14:07:11 +08:00
typedef struct lua_State lua_State;
NS_CC_BEGIN
class TimerScriptHandler;
class Layer;
class MenuItem;
class CallFunc;
class Acceleration;
enum ccScriptType {
kScriptTypeNone = 0,
kScriptTypeLua,
kScriptTypeJavascript
};
class ScriptHandlerEntry : public Ref
{
public:
static ScriptHandlerEntry* create(int handler);
/**
* @js NA
* @lua NA
*/
virtual ~ScriptHandlerEntry();
int getHandler(void) {
return _handler;
}
int getEntryId(void) {
return _entryId;
}
protected:
ScriptHandlerEntry(int handler)
: _handler(handler)
{
static int newEntryId = 0;
newEntryId++;
_entryId = newEntryId;
}
int _handler;
int _entryId;
};
2012-02-09 14:07:11 +08:00
2012-06-20 18:09:11 +08:00
/**
* @addtogroup script_support
* @{
*/
class SchedulerScriptHandlerEntry : public ScriptHandlerEntry
2012-02-09 14:07:11 +08:00
{
public:
// nHandler return by tolua_ref_function(), called from LuaCocos2d.cpp
/**
* @js NA
* @lua NA
*/
static SchedulerScriptHandlerEntry* create(int handler, float interval, bool paused);
/**
* @js NA
* @lua NA
*/
virtual ~SchedulerScriptHandlerEntry();
/**
* @js NA
* @lua NA
*/
TimerScriptHandler* getTimer(void) {
return _timer;
2012-02-09 14:07:11 +08:00
}
/**
* @js NA
* @lua NA
*/
bool isPaused(void) {
return _paused;
2012-02-09 14:07:11 +08:00
}
/**
* @js NA
* @lua NA
*/
void markedForDeletion(void) {
_markedForDeletion = true;
2012-02-09 14:07:11 +08:00
}
/**
* @js NA
* @lua NA
*/
bool isMarkedForDeletion(void) {
return _markedForDeletion;
2012-02-09 14:07:11 +08:00
}
private:
SchedulerScriptHandlerEntry(int handler)
: ScriptHandlerEntry(handler)
, _timer(nullptr)
, _paused(false)
, _markedForDeletion(false)
{
}
bool init(float interval, bool paused);
2012-02-09 14:07:11 +08:00
TimerScriptHandler* _timer;
bool _paused;
bool _markedForDeletion;
2012-02-09 14:07:11 +08:00
};
class TouchScriptHandlerEntry : public ScriptHandlerEntry
2012-02-09 14:07:11 +08:00
{
public:
/**
* @js NA
* @lua NA
*/
static TouchScriptHandlerEntry* create(int handler, bool isMultiTouches, int priority, bool swallowsTouches);
/**
* @js NA
* @lua NA
*/
virtual ~TouchScriptHandlerEntry();
/**
* @js NA
* @lua NA
*/
bool isMultiTouches(void) {
return _isMultiTouches;
2012-02-09 14:07:11 +08:00
}
/**
* @js NA
* @lua NA
*/
int getPriority(void) {
return _priority;
2012-02-09 14:07:11 +08:00
}
/**
* @js NA
* @lua NA
*/
bool getSwallowsTouches(void) {
return _swallowsTouches;
2012-02-09 14:07:11 +08:00
}
private:
TouchScriptHandlerEntry(int handler)
: ScriptHandlerEntry(handler)
, _isMultiTouches(false)
, _priority(0)
, _swallowsTouches(false)
{
}
bool init(bool isMultiTouches, int priority, bool swallowsTouches);
2012-02-09 14:07:11 +08:00
bool _isMultiTouches;
int _priority;
bool _swallowsTouches;
2012-02-09 14:07:11 +08:00
};
enum ScriptEventType
{
kNodeEvent = 0,
kMenuClickedEvent,
kCallFuncEvent,
kScheduleEvent,
kTouchEvent,
kTouchesEvent,
kKeypadEvent,
kAccelerometerEvent,
kControlEvent,
kCommonEvent,
kComponentEvent
};
struct BasicScriptData
{
// nativeobject:to get handler for lua or to get jsobject for js
void* nativeObject;
// value: a pointer to a object that already defined
void* value;
// Constructor
/**
* @js NA
* @lua NA
*/
BasicScriptData(void* inObject,void* inValue = nullptr)
: nativeObject(inObject),value(inValue)
{
}
};
struct SchedulerScriptData
{
// lua use
int handler;
float elapse;
// js use
void* node;
// Constructor
/**
* @js NA
* @lua NA
*/
SchedulerScriptData(int inHandler,float inElapse,void* inNode = nullptr)
: handler(inHandler),
elapse(inElapse),
node(inNode)
2013-07-03 14:19:00 +08:00
{
}
};
struct TouchesScriptData
{
EventTouch::EventCode actionType;
void* nativeObject;
const std::vector<Touch*>& touches;
Event* event;
// Constructor
/**
* @js NA
* @lua NA
*/
TouchesScriptData(EventTouch::EventCode inActionType, void* inNativeObject, const std::vector<Touch*>& inTouches, Event* evt)
: actionType(inActionType),
nativeObject(inNativeObject),
touches(inTouches),
event(evt)
{
}
};
struct TouchScriptData
{
EventTouch::EventCode actionType;
void* nativeObject;
Touch* touch;
Event* event;
// Constructor
/**
* @js NA
* @lua NA
*/
TouchScriptData(EventTouch::EventCode inActionType, void* inNativeObject, Touch* inTouch, Event* evt)
: actionType(inActionType),
nativeObject(inNativeObject),
touch(inTouch),
event(evt)
2013-07-03 14:19:00 +08:00
{
}
};
struct KeypadScriptData
{
EventKeyboard::KeyCode actionType;
void* nativeObject;
// Constructor
/**
* @js NA
* @lua NA
*/
KeypadScriptData(EventKeyboard::KeyCode inActionType,void* inNativeObject)
: actionType(inActionType),nativeObject(inNativeObject)
2013-07-03 14:19:00 +08:00
{
}
};
struct CommonScriptData
{
// Now this struct is only used in LuaBinding.
int handler;
char eventName[64];
Ref* eventSource;
char eventSourceClassName[64];
// Constructor
/**
* @js NA
* @lua NA
*/
CommonScriptData(int inHandler,const char* inName, Ref* inSource = nullptr,const char* inClassName = nullptr)
: handler(inHandler),
eventSource(inSource)
{
2013-07-03 14:19:00 +08:00
strncpy(eventName, inName, 64);
if (nullptr == inClassName)
{
memset(eventSourceClassName, 0, 64*sizeof(char));
}
else
{
2013-07-03 14:19:00 +08:00
strncpy(eventSourceClassName, inClassName, 64);
}
}
};
struct ScriptEvent
{
ScriptEventType type;
void* data;
// Constructor
/**
* @js NA
* @lua NA
*/
ScriptEvent(ScriptEventType inType,void* inData)
: type(inType),
data(inData)
{
}
};
2012-02-09 14:07:11 +08:00
// Don't make ScriptEngineProtocol inherits from Object 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.
class CC_DLL ScriptEngineProtocol
2012-02-09 14:07:11 +08:00
{
public:
ScriptEngineProtocol()
{};
/**
* @js NA
* @lua NA
*/
virtual ~ScriptEngineProtocol() {};
/** Get script type
* @js NA
* @lua NA
*/
virtual ccScriptType getScriptType() { return kScriptTypeNone; };
2012-09-10 18:38:18 +08:00
/** Remove script object.
* @js NA
* @lua NA
*/
virtual void removeScriptObjectByObject(Ref* obj) = 0;
2012-02-09 14:07:11 +08:00
/** Remove script function handler, only LuaEngine class need to implement this function.
* @js NA
* @lua NA
*/
virtual void removeScriptHandler(int handler) {};
2012-02-09 14:07:11 +08:00
/** Reallocate script function handler, only LuaEngine class need to implement this function.
* @js NA
* @lua NA
*/
virtual int reallocateScriptHandler(int handler) { return 0;}
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.
@return 0 if the string is executed correctly.
@return other if the string is executed wrongly.
* @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
* @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.
* @js NA
* @lua NA
2012-02-09 14:07:11 +08:00
*/
virtual int executeGlobalFunction(const char* functionName) = 0;
/**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
* @js NA
* @lua NA
*/
virtual int sendEvent(ScriptEvent* evt) = 0;
2012-02-09 14:07:11 +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.
* @js NA
* @lua NA
*/
virtual bool handleAssert(const char *msg) = 0;
virtual void setCalledFromScript(bool callFromScript) { CC_UNUSED_PARAM(callFromScript); };
virtual bool isCalledFromScript() { return false; };
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
};
class Node;
2012-02-09 14:07:11 +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
*/
class CC_DLL ScriptEngineManager
2012-02-09 14:07:11 +08:00
{
public:
/**
* @js NA
* @lua NA
*/
~ScriptEngineManager(void);
/**
* @js NA
* @lua NA
*/
ScriptEngineProtocol* getScriptEngine(void) {
return _scriptEngine;
2012-02-09 14:07:11 +08:00
}
/**
* @js NA
* @lua NA
*/
void setScriptEngine(ScriptEngineProtocol *scriptEngine);
/**
* @js NA
* @lua NA
*/
2012-02-09 14:07:11 +08:00
void removeScriptEngine(void);
/**
* @js NA
* @lua NA
*/
static ScriptEngineManager* getInstance();
/**
* @js NA
* @lua NA
*/
static void destroyInstance();
/**
* @js NA
* @lua NA
*/
static bool sendNodeEventToJS(Node* node, int action);
/**
* @js NA
* @lua NA
*/
static bool sendNodeEventToJSExtended(Node* node, int action);
/**
* @js NA
* @lua NA
*/
static void sendNodeEventToLua(Node* node, int action);
/**
* @js NA
* @lua NA
*/
CC_DEPRECATED_ATTRIBUTE static ScriptEngineManager* sharedManager() { return ScriptEngineManager::getInstance(); };
/**
* @js NA
* @lua NA
*/
CC_DEPRECATED_ATTRIBUTE static void purgeSharedManager() { ScriptEngineManager::destroyInstance(); };
2012-02-09 14:07:11 +08:00
private:
ScriptEngineManager(void)
: _scriptEngine(nullptr)
2012-02-09 14:07:11 +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
#endif // #if CC_ENABLE_SCRIPT_BINDING
2012-02-09 14:07:11 +08:00
#endif // __SCRIPT_SUPPORT_H__