axmol/cocos/scripting/lua-bindings/manual/cocos2d/LuaScriptHandlerMgr.h

333 lines
9.8 KiB
C
Raw Normal View History

2014-03-10 14:04:58 +08:00
/****************************************************************************
2016-08-05 09:42:15 +08:00
Copyright (c) 2013-2016 Chukong Technologies Inc.
2014-03-10 14:04:58 +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.
****************************************************************************/
#ifndef __LUA_SCRIPT_HANDLER_MGR_H__
#define __LUA_SCRIPT_HANDLER_MGR_H__
extern "C" {
#include "tolua++.h"
}
#include "base/CCRef.h"
2014-04-30 08:37:36 +08:00
#include "base/ccMacros.h"
#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.
2015-03-27 15:27:08 +08:00
*
* @lua NA
* @js NA
2015-03-20 14:39:33 +08:00
*/
2014-03-10 14:04:58 +08:00
class LuaCallFunc:public cocos2d::CallFuncN
{
public:
2015-03-20 14:39:33 +08:00
/**
2015-11-03 03:31:17 +08:00
* Default constructor.
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
/**
2015-11-03 03:31:17 +08:00
* Destructor.
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.
*/
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.
*/
2014-03-10 14:04:58 +08:00
bool initWithFunction(const std::function<void(void* self,Node*)>& func);
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.
2015-11-03 03:31:17 +08:00
* In the current mechanism, for the class derived from the Ref, we construct a mapping relationship among c++ Ref object, HandlerType and the reference index corresponding to the pointer of Lua function. Then, using the ScriptHandlerMgr to manager uniformly.
2015-10-14 11:42:51 +08:00
* 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.
2015-03-20 14:39:33 +08:00
*/
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,
2015-04-25 05:02:39 +08:00
EVENT_PHYSICS_CONTACT_SEPARATE,
EVENT_FOCUS,
EVENT_CONTROLLER_CONNECTED,
EVENT_CONTROLLER_DISCONNECTED,
EVENT_CONTROLLER_KEYDOWN,
EVENT_CONTROLLER_KEYUP,
EVENT_CONTROLLER_KEYREPEAT,
EVENT_CONTROLLER_AXIS,
EVENT_SPINE_ANIMATION_START,
EVENT_SPINE_ANIMATION_END,
EVENT_SPINE_ANIMATION_COMPLETE,
EVENT_SPINE_ANIMATION_EVENT,
EVENT_CUSTOM_BEGAN = 10000,
EVENT_CUSTOM_ENDED = 11000,
};
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
*/
static void destroyInstance(void);
2015-03-20 14:39:33 +08:00
/**
2015-10-14 11:42:51 +08:00
* Construct or update the mapping relationship among c++ Ref object ,HandlerType and the reference index corresponding to the pointer of Lua function.
2015-03-20 14:39:33 +08:00
*
* @param object a Ref object.
2015-10-14 11:42:51 +08:00
* @param handler a reference index corresponding to the pointer of Lua function.
2015-03-20 14:39:33 +08:00
* @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
/**
2015-10-14 11:42:51 +08:00
* By the handlerType and object, find the correct reference index corresponding to the pointer of Lua function.
* If found, remove the reference of Lua function corresponding to this index in the 'toluafix_refid_function_mapping' table.
2015-03-20 14:39:33 +08:00
*
* @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
/**
2015-10-14 11:42:51 +08:00
* By the handlerType and object, find the correct reference index corresponding to the pointer of Lua function.
2015-03-20 14:39:33 +08:00
*
* @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
/**
2015-10-14 11:42:51 +08:00
* Remove the all relationship among the object, HandlerType and the reference index corresponding to the pointer of Lua function.
* Meanwhile, remove the reference of Lua function corresponding to the indexs the object has in the 'toluafix_refid_function_mapping' table.
2015-03-20 14:39:33 +08:00
*
* @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
/**
2015-10-14 11:42:51 +08:00
* Add customizable relationship among c++ Ref object, HandlerType and the reference index corresponding to the pointer of Lua function.
2015-03-20 14:39:33 +08:00
* 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.
2015-10-14 11:42:51 +08:00
* @param handler a reference index corresponding to the pointer of Lua function.
2015-03-20 14:39:33 +08:00
* @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
*/
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__