2014-03-10 14:04:58 +08:00
|
|
|
/****************************************************************************
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef __LUA_SCRIPT_HANDLER_MGR_H__
|
|
|
|
#define __LUA_SCRIPT_HANDLER_MGR_H__
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "tolua++.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCRef.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCActionInstant.h"
|
2014-03-10 14:04:58 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
|
2015-03-26 22:07:44 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup lua
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2014-03-10 14:04:58 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class ScheduleHandlerDelegate;
|
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/// @cond
|
2014-03-10 14:04:58 +08:00
|
|
|
typedef std::vector<ScheduleHandlerDelegate*> VecShedule;
|
|
|
|
typedef std::map<cocos2d::Node*,VecShedule> MapNodeSchedules;
|
|
|
|
|
|
|
|
class ScheduleHandlerDelegate:public cocos2d::Ref
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ScheduleHandlerDelegate():_isUpdateSchedule(false)
|
|
|
|
{}
|
|
|
|
virtual ~ScheduleHandlerDelegate()
|
|
|
|
{}
|
|
|
|
|
|
|
|
static ScheduleHandlerDelegate* create();
|
|
|
|
|
|
|
|
void scheduleFunc(float elapse);
|
|
|
|
|
|
|
|
virtual void update(float elapse);
|
|
|
|
|
|
|
|
void setUpdateSchedule(bool isUpdateSchedule){ _isUpdateSchedule = isUpdateSchedule; }
|
|
|
|
bool isUpdateSchedule(){ return _isUpdateSchedule; }
|
|
|
|
private:
|
|
|
|
bool _isUpdateSchedule;
|
|
|
|
};
|
2015-03-20 14:39:33 +08:00
|
|
|
/// @endcond
|
2014-03-10 14:04:58 +08:00
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/**
|
|
|
|
* The LuaCallFunc is wrapped to call the callback function in the Lua Conveniently and don't insert useless code to processing the lua in the CallFuncN.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
class LuaCallFunc:public cocos2d::CallFuncN
|
|
|
|
{
|
|
|
|
public:
|
2015-03-20 14:39:33 +08:00
|
|
|
/**
|
|
|
|
* Default construtor.
|
2015-03-26 22:07:44 +08:00
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
* @js NA
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
LuaCallFunc():_functionLua(nullptr)
|
|
|
|
{}
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destrutor.
|
2015-03-26 22:07:44 +08:00
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
* @js NA
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
virtual ~LuaCallFunc()
|
|
|
|
{}
|
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/**
|
|
|
|
* Create a LuaCallFunc object by a function pointer for callback.
|
|
|
|
*
|
|
|
|
* @param func a function pointer for callback.
|
2015-03-26 22:07:44 +08:00
|
|
|
* @lua NA
|
|
|
|
* @js NA
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
static LuaCallFunc* create(const std::function<void(void* self,Node*)>& func);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Init a LuaCallFunc object by a function pointer for callback.
|
|
|
|
*
|
|
|
|
* @param func a function pointer for callback.
|
|
|
|
* @lua NA
|
2015-03-26 22:07:44 +08:00
|
|
|
* @js NA
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
bool initWithFunction(const std::function<void(void* self,Node*)>& func);
|
2015-03-15 07:10:08 +08:00
|
|
|
virtual LuaCallFunc* clone() const override;
|
2014-03-10 14:04:58 +08:00
|
|
|
virtual void execute() override;
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
std::function<void(void* self,Node*)> _functionLua;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/**
|
|
|
|
* In order to reduce the coupling of lua script engine and native c++ engine.
|
|
|
|
* In the current mechanism, for the class derived frome the Ref, we constuct a mapping relationship among c++ Ref object ,HandlerType and the refrence index corresponding to the pointer of Lua function.Then, using the ScriptHandlerMgr to manager uniformly.
|
|
|
|
* By this mechanism,when native c++ Ref object wants to call the Lua function, we didn't insert the processing code in the native c++ class.
|
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
class ScriptHandlerMgr
|
|
|
|
{
|
|
|
|
public:
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* HandlerType enum.
|
|
|
|
* This enum class represent the processing type for c++ call the Lua function.
|
2015-03-26 22:07:44 +08:00
|
|
|
*
|
|
|
|
* @js NA
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
enum class HandlerType: int
|
|
|
|
{
|
|
|
|
NODE = 0,
|
|
|
|
MENU_CLICKED,
|
|
|
|
CALLFUNC,
|
|
|
|
SCHEDULE,
|
|
|
|
TOUCHES,
|
|
|
|
KEYPAD,
|
|
|
|
ACCELEROMETER,
|
|
|
|
|
|
|
|
CONTROL_TOUCH_DOWN,
|
|
|
|
CONTROL_TOUCH_DRAG_INSIDE,
|
|
|
|
CONTROL_TOUCH_DRAG_OUTSIDE,
|
|
|
|
CONTROL_TOUCH_DRAG_ENTER,
|
|
|
|
CONTROL_TOUCH_DRAG_EXIT,
|
|
|
|
CONTROL_TOUCH_UP_INSIDE,
|
|
|
|
CONTROL_TOUCH_UP_OUTSIDE,
|
|
|
|
CONTROL_TOUCH_UP_CANCEL,
|
|
|
|
CONTROL_VALUE_CHANGED,
|
|
|
|
|
|
|
|
WEBSOCKET_OPEN,
|
|
|
|
WEBSOCKET_MESSAGE,
|
|
|
|
WEBSOCKET_CLOSE,
|
|
|
|
WEBSOCKET_ERROR,
|
|
|
|
|
|
|
|
GL_NODE_DRAW,
|
|
|
|
|
|
|
|
SCROLLVIEW_SCROLL,
|
|
|
|
SCROLLVIEW_ZOOM,
|
|
|
|
|
|
|
|
TABLECELL_TOUCHED,
|
|
|
|
TABLECELL_HIGHLIGHT,
|
|
|
|
TABLECELL_UNHIGHLIGHT,
|
|
|
|
TABLECELL_WILL_RECYCLE,
|
|
|
|
TABLECELL_SIZE_FOR_INDEX,
|
|
|
|
TABLECELL_AT_INDEX,
|
|
|
|
TABLEVIEW_NUMS_OF_CELLS,
|
|
|
|
|
|
|
|
XMLHTTPREQUEST_READY_STATE_CHANGE,
|
|
|
|
|
|
|
|
ASSETSMANAGER_PROGRESS,
|
|
|
|
ASSETSMANAGER_SUCCESS,
|
|
|
|
ASSETSMANAGER_ERROR,
|
|
|
|
|
|
|
|
STUDIO_EVENT_LISTENER,
|
|
|
|
ARMATURE_EVENT,
|
|
|
|
|
|
|
|
EVENT_ACC,
|
|
|
|
EVENT_CUSTIOM,
|
|
|
|
|
|
|
|
EVENT_KEYBOARD_PRESSED,
|
|
|
|
EVENT_KEYBOARD_RELEASED,
|
|
|
|
|
|
|
|
EVENT_TOUCH_BEGAN,
|
|
|
|
EVENT_TOUCH_MOVED,
|
|
|
|
EVENT_TOUCH_ENDED,
|
|
|
|
EVENT_TOUCH_CANCELLED,
|
|
|
|
|
|
|
|
EVENT_TOUCHES_BEGAN,
|
|
|
|
EVENT_TOUCHES_MOVED,
|
|
|
|
EVENT_TOUCHES_ENDED,
|
|
|
|
EVENT_TOUCHES_CANCELLED,
|
|
|
|
|
|
|
|
EVENT_MOUSE_DOWN,
|
|
|
|
EVENT_MOUSE_UP,
|
|
|
|
EVENT_MOUSE_MOVE,
|
|
|
|
EVENT_MOUSE_SCROLL,
|
|
|
|
|
|
|
|
EVENT_SPINE,
|
|
|
|
|
|
|
|
EVENT_PHYSICS_CONTACT_BEGIN,
|
|
|
|
EVENT_PHYSICS_CONTACT_PRESOLVE,
|
|
|
|
EVENT_PHYSICS_CONTACT_POSTSOLVE,
|
|
|
|
EVENT_PHYSICS_CONTACT_SEPERATE,
|
2014-03-19 10:50:11 +08:00
|
|
|
|
2014-06-25 15:08:59 +08:00
|
|
|
EVENT_FOCUS,
|
|
|
|
|
2014-07-09 18:05:02 +08:00
|
|
|
EVENT_CONTROLLER_CONNECTED,
|
|
|
|
EVENT_CONTROLLER_DISCONNECTED,
|
|
|
|
EVENT_CONTROLLER_KEYDOWN,
|
|
|
|
EVENT_CONTROLLER_KEYUP,
|
|
|
|
EVENT_CONTROLLER_KEYREPEAT,
|
|
|
|
EVENT_CONTROLLER_AXIS,
|
|
|
|
|
2014-10-16 20:23:58 +08:00
|
|
|
EVENT_SPINE_ANIMATION_START,
|
|
|
|
EVENT_SPINE_ANIMATION_END,
|
|
|
|
EVENT_SPINE_ANIMATION_COMPLETE,
|
|
|
|
EVENT_SPINE_ANIMATION_EVENT,
|
|
|
|
|
2014-03-21 11:24:24 +08:00
|
|
|
EVENT_CUSTOM_BEGAN = 10000,
|
|
|
|
EVENT_CUSTOM_ENDED = 11000,
|
2014-03-19 17:28:23 +08:00
|
|
|
};
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
///! @cond
|
2014-03-10 14:04:58 +08:00
|
|
|
typedef int Handler;
|
|
|
|
typedef std::pair<HandlerType, Handler> HandlerPair;
|
|
|
|
typedef std::vector<HandlerPair> VecHandlerPairs;
|
|
|
|
typedef std::map<void*,VecHandlerPairs> MapObjectHandlers;
|
2015-03-20 14:39:33 +08:00
|
|
|
/// @endcond
|
2014-03-10 14:04:58 +08:00
|
|
|
|
2015-03-20 14:39:33 +08:00
|
|
|
/**
|
|
|
|
* Default constructor.
|
2015-03-26 22:07:44 +08:00
|
|
|
*
|
2015-03-20 14:39:33 +08:00
|
|
|
* @lua NA
|
2015-03-26 22:07:44 +08:00
|
|
|
* @js NA
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
ScriptHandlerMgr(void);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*
|
|
|
|
* @lua NA
|
2015-03-26 22:07:44 +08:00
|
|
|
* @js NA
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
virtual ~ScriptHandlerMgr(void);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the instance of the ScriptHandlerMgr.
|
|
|
|
*
|
|
|
|
* @return the instance of the ScriptHandlerMgr.
|
2015-03-26 22:07:44 +08:00
|
|
|
* @js NA
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
static ScriptHandlerMgr* getInstance(void);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy the instance of the ScriptHandlerMgr.
|
2015-03-26 22:07:44 +08:00
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
* @js NA
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-04-05 21:09:49 +08:00
|
|
|
static void destroyInstance(void);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct or update the mapping relationship among c++ Ref object ,HandlerType and the refrence index corresponding to the pointer of Lua function.
|
|
|
|
*
|
|
|
|
* @param object a Ref object.
|
|
|
|
* @param handler a refrence index corresponding to the pointer of Lua function.
|
|
|
|
* @param handlerType ScriptHandlerMgr::HandlerType.
|
2015-03-26 22:07:44 +08:00
|
|
|
* @lua registerScriptHandler
|
|
|
|
* @js NA
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
void addObjectHandler(void* object,int handler,ScriptHandlerMgr::HandlerType handlerType);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* By the handlerType and object, find the correct refrence index corresponding to the pointer of Lua function.
|
|
|
|
* If found, remove the refrence of Lua function corresponding to this index in the 'toluafix_refid_function_mapping' table.
|
|
|
|
*
|
|
|
|
* @param object a Ref object.
|
|
|
|
* @param handlerType ScriptHandlerMgr::HandlerType.
|
2015-03-26 22:07:44 +08:00
|
|
|
* @lua unregisterScriptHandler
|
|
|
|
* @js NA
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
void removeObjectHandler(void* object,ScriptHandlerMgr::HandlerType handlerType);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* By the handlerType and object, find the correct refrence index corresponding to the pointer of Lua function.
|
|
|
|
*
|
|
|
|
* @param object a Ref object.
|
|
|
|
* @param handlerType ScriptHandlerMgr::HandlerType.
|
|
|
|
* @return index corresponding to the pointer of Lua function,otherwise 0.
|
2015-03-26 22:07:44 +08:00
|
|
|
* @lua NA
|
|
|
|
* @js NA
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
int getObjectHandler(void* object,ScriptHandlerMgr::HandlerType handlerType);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the all relationship among the object, HandlerType and the refrence index corresponding to the pointer of Lua function.
|
|
|
|
* Meanwhile, remove the refrence of Lua function corresponding to the indexs the object has in the 'toluafix_refid_function_mapping' table.
|
|
|
|
*
|
|
|
|
* @param object the Ref object.
|
2015-03-26 22:07:44 +08:00
|
|
|
* @js NA
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-10 14:04:58 +08:00
|
|
|
void removeObjectAllHandlers(void* object);
|
2015-03-20 14:39:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add customizable relationship among c++ Ref object, HandlerType and the refrence index corresponding to the pointer of Lua function.
|
|
|
|
* In the customizable relationship, we don't pass the HandlerType, it will obtain the HandlerType by auto-increasing.
|
|
|
|
* The HandlerTypes used to customizable relationship are between EVENT_CUSTOM_BEGAN(10000) and EVENT_CUSTOM_ENDED(11000).
|
|
|
|
* If the HandlerType increased more than 12,it would trigger assert.
|
|
|
|
*
|
|
|
|
* @param object the Ref object.
|
|
|
|
* @param handler a refrence index corresponding to the pointer of Lua function.
|
|
|
|
* @return ScriptHandlerMgr::HandlerType the value of current ScriptHandlerMgr::HandlerType after adding.
|
2015-03-26 22:07:44 +08:00
|
|
|
* @lua NA
|
|
|
|
* @js NA
|
2015-03-20 14:39:33 +08:00
|
|
|
*/
|
2014-03-21 11:24:24 +08:00
|
|
|
ScriptHandlerMgr::HandlerType addCustomHandler(void* object, int handler);
|
2014-03-10 14:04:58 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void init(void);
|
|
|
|
static ScriptHandlerMgr* _scriptHandlerMgr;
|
|
|
|
MapObjectHandlers _mapObjectHandlers;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
2015-03-26 22:07:44 +08:00
|
|
|
// end group
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// @cond
|
2014-03-10 14:04:58 +08:00
|
|
|
TOLUA_API int tolua_script_handler_mgr_open(lua_State* tolua_S);
|
2015-03-26 22:07:44 +08:00
|
|
|
/// @endcond
|
2014-03-10 14:04:58 +08:00
|
|
|
|
|
|
|
#endif //__LUA_SCRIPT_HANDLER_MGR_H__
|