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

334 lines
9.7 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
2022-02-24 18:51:36 +08:00
https://adxeproject.github.io/
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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:
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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__
#include "tolua++.h"
#include "base/CCRef.h"
#include "base/ccMacros.h"
#include "2d/CCActionInstant.h"
#include <vector>
#include <map>
/**
* @addtogroup lua
* @{
*/
NS_CC_BEGIN
class ScheduleHandlerDelegate;
/// @cond
typedef std::vector<ScheduleHandlerDelegate*> VecShedule;
2021-12-25 10:04:45 +08:00
typedef std::map<cocos2d::Node*, VecShedule> MapNodeSchedules;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
class ScheduleHandlerDelegate : public cocos2d::Ref
2019-11-23 20:27:39 +08:00
{
public:
2021-12-25 10:04:45 +08:00
ScheduleHandlerDelegate() : _isUpdateSchedule(false) {}
virtual ~ScheduleHandlerDelegate() {}
2019-11-23 20:27:39 +08:00
static ScheduleHandlerDelegate* create();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void scheduleFunc(float elapse);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
virtual void update(float elapse);
2021-12-25 10:04:45 +08:00
void setUpdateSchedule(bool isUpdateSchedule) { _isUpdateSchedule = isUpdateSchedule; }
bool isUpdateSchedule() { return _isUpdateSchedule; }
2019-11-23 20:27:39 +08:00
private:
bool _isUpdateSchedule;
};
/// @endcond
/**
2021-12-25 10:04:45 +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.
2019-11-23 20:27:39 +08:00
*
* @lua NA
* @js NA
*/
2021-12-25 10:04:45 +08:00
class LuaCallFunc : public cocos2d::CallFuncN
2019-11-23 20:27:39 +08:00
{
public:
/**
* Default constructor.
*/
2021-12-25 10:04:45 +08:00
LuaCallFunc() : _functionLua(nullptr) {}
2019-11-23 20:27:39 +08:00
/**
* Destructor.
*/
2021-12-25 10:04:45 +08:00
virtual ~LuaCallFunc() {}
2019-11-23 20:27:39 +08:00
/**
* Create a LuaCallFunc object by a function pointer for callback.
*
* @param func a function pointer for callback.
*/
2021-12-25 10:04:45 +08:00
static LuaCallFunc* create(const std::function<void(void* self, Node*)>& func);
2019-11-23 20:27:39 +08:00
/**
* Init a LuaCallFunc object by a function pointer for callback.
*
* @param func a function pointer for callback.
*/
2021-12-25 10:04:45 +08:00
bool initWithFunction(const std::function<void(void* self, Node*)>& func);
2019-11-23 20:27:39 +08:00
virtual LuaCallFunc* clone() const override;
virtual void execute() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
2021-12-25 10:04:45 +08:00
std::function<void(void* self, Node*)> _functionLua;
2019-11-23 20:27:39 +08:00
};
/**
* In order to reduce the coupling of lua script engine and native c++ engine.
2021-12-25 10:04:45 +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. 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.
2019-11-23 20:27:39 +08:00
*/
class ScriptHandlerMgr
{
public:
/**
* HandlerType enum.
* This enum class represent the processing type for c++ call the Lua function.
*
* @js NA
*/
2021-12-25 10:04:45 +08:00
enum class HandlerType : int
2019-11-23 20:27:39 +08:00
{
NODE = 0,
MENU_CLICKED,
CALLFUNC,
SCHEDULE,
TOUCHES,
KEYPAD,
ACCELEROMETER,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
WEBSOCKET_OPEN,
WEBSOCKET_MESSAGE,
WEBSOCKET_CLOSE,
WEBSOCKET_ERROR,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
LUANODE_DRAW,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
SCROLLVIEW_SCROLL,
SCROLLVIEW_ZOOM,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
TABLECELL_TOUCHED,
TABLECELL_HIGHLIGHT,
TABLECELL_UNHIGHLIGHT,
TABLECELL_WILL_RECYCLE,
TABLECELL_SIZE_FOR_INDEX,
TABLECELL_AT_INDEX,
TABLEVIEW_NUMS_OF_CELLS,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
XMLHTTPREQUEST_READY_STATE_CHANGE,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
ASSETSMANAGER_PROGRESS,
ASSETSMANAGER_SUCCESS,
ASSETSMANAGER_ERROR,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
STUDIO_EVENT_LISTENER,
ARMATURE_EVENT,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
EVENT_ACC,
EVENT_CUSTIOM,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
EVENT_KEYBOARD_PRESSED,
EVENT_KEYBOARD_RELEASED,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
EVENT_TOUCH_BEGAN,
EVENT_TOUCH_MOVED,
EVENT_TOUCH_ENDED,
EVENT_TOUCH_CANCELLED,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
EVENT_TOUCHES_BEGAN,
EVENT_TOUCHES_MOVED,
EVENT_TOUCHES_ENDED,
EVENT_TOUCHES_CANCELLED,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
EVENT_MOUSE_DOWN,
EVENT_MOUSE_UP,
EVENT_MOUSE_MOVE,
EVENT_MOUSE_SCROLL,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
EVENT_SPINE,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
EVENT_PHYSICS_CONTACT_BEGIN,
EVENT_PHYSICS_CONTACT_PRESOLVE,
EVENT_PHYSICS_CONTACT_POSTSOLVE,
EVENT_PHYSICS_CONTACT_SEPARATE,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
EVENT_FOCUS,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
EVENT_CONTROLLER_CONNECTED,
EVENT_CONTROLLER_DISCONNECTED,
EVENT_CONTROLLER_KEYDOWN,
EVENT_CONTROLLER_KEYUP,
EVENT_CONTROLLER_KEYREPEAT,
EVENT_CONTROLLER_AXIS,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
EVENT_SPINE_ANIMATION_START,
EVENT_SPINE_ANIMATION_INTERRUPT,
EVENT_SPINE_ANIMATION_END,
EVENT_SPINE_ANIMATION_DISPOSE,
EVENT_SPINE_ANIMATION_COMPLETE,
EVENT_SPINE_ANIMATION_EVENT,
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
EVENT_CUSTOM_BEGAN = 10000,
EVENT_CUSTOM_ENDED = 11000,
};
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
///! @cond
typedef int Handler;
typedef std::pair<HandlerType, Handler> HandlerPair;
typedef std::vector<HandlerPair> VecHandlerPairs;
2021-12-25 10:04:45 +08:00
typedef std::map<void*, VecHandlerPairs> MapObjectHandlers;
2019-11-23 20:27:39 +08:00
/// @endcond
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Default constructor.
*
* @lua NA
* @js NA
*/
ScriptHandlerMgr(void);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Destructor.
*
* @lua NA
* @js NA
*/
virtual ~ScriptHandlerMgr(void);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Get the instance of the ScriptHandlerMgr.
*
* @return the instance of the ScriptHandlerMgr.
* @js NA
*/
static ScriptHandlerMgr* getInstance(void);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Destroy the instance of the ScriptHandlerMgr.
*
* @lua NA
* @js NA
*/
static void destroyInstance(void);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
2021-12-25 10:04:45 +08:00
* Construct or update the mapping relationship among c++ Ref object ,HandlerType and the reference index
* corresponding to the pointer of Lua function.
2019-11-23 20:27:39 +08:00
*
* @param object a Ref object.
* @param handler a reference index corresponding to the pointer of Lua function.
* @param handlerType ScriptHandlerMgr::HandlerType.
* @lua registerScriptHandler
* @js NA
*/
2021-12-25 10:04:45 +08:00
void addObjectHandler(void* object, int handler, ScriptHandlerMgr::HandlerType handlerType);
2019-11-23 20:27:39 +08:00
/**
* By the handlerType and object, find the correct reference index corresponding to the pointer of Lua function.
2021-12-25 10:04:45 +08:00
* If found, remove the reference of Lua function corresponding to this index in the
* 'toluafix_refid_function_mapping' table.
*
2019-11-23 20:27:39 +08:00
* @param object a Ref object.
* @param handlerType ScriptHandlerMgr::HandlerType.
* @lua unregisterScriptHandler
* @js NA
*/
2021-12-25 10:04:45 +08:00
void removeObjectHandler(void* object, ScriptHandlerMgr::HandlerType handlerType);
2019-11-23 20:27:39 +08:00
/**
* By the handlerType and object, find the correct reference 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.
* @lua NA
* @js NA
*/
2021-12-25 10:04:45 +08:00
int getObjectHandler(void* object, ScriptHandlerMgr::HandlerType handlerType);
2019-11-23 20:27:39 +08:00
/**
2021-12-25 10:04:45 +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.
2019-11-23 20:27:39 +08:00
*
* @param object the Ref object.
* @js NA
*/
void removeObjectAllHandlers(void* object);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
2021-12-25 10:04:45 +08:00
* Add customizable relationship among c++ Ref object, HandlerType and the reference 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.
2019-11-23 20:27:39 +08:00
*
* @param object the Ref object.
* @param handler a reference index corresponding to the pointer of Lua function.
* @return ScriptHandlerMgr::HandlerType the value of current ScriptHandlerMgr::HandlerType after adding.
* @lua NA
* @js NA
*/
ScriptHandlerMgr::HandlerType addCustomHandler(void* object, int handler);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
private:
void init(void);
static ScriptHandlerMgr* _scriptHandlerMgr;
MapObjectHandlers _mapObjectHandlers;
};
NS_CC_END
// end group
/// @}
/// @cond
TOLUA_API int tolua_script_handler_mgr_open(lua_State* tolua_S);
/// @endcond
2021-12-25 10:04:45 +08:00
#endif //__LUA_SCRIPT_HANDLER_MGR_H__