issue #3403:Add the EventDispatcher lua binding and the releated test cases

This commit is contained in:
samuele3hu 2013-12-22 22:10:21 +08:00
parent 589b71eecf
commit 97f26176e8
14 changed files with 1417 additions and 5 deletions

View File

@ -29,6 +29,7 @@
#include <string>
#include <stdint.h>
#include "CCObject.h"
#include "CCPlatformMacros.h"
NS_CC_BEGIN
@ -38,7 +39,7 @@ class Node;
/**
* Base class of all kinds of events.
*/
class Event
class Event : public Object
{
public:
enum class Type

View File

@ -44,6 +44,8 @@ private:
bool init(std::function<void(Acceleration*, Event* event)> callback);
std::function<void(Acceleration*, Event*)> onAccelerationEvent;
friend class LuaEventListenerAcceleration;
};
NS_CC_END

View File

@ -70,6 +70,8 @@ protected:
bool init(ListenerID listenerId, std::function<void(EventCustom*)> callback);
std::function<void(EventCustom*)> _onCustomEvent;
friend class LuaEventListenerCustom;
};
NS_CC_END

View File

@ -214,6 +214,12 @@ enum ScriptEventType
kAssetsManagerEvent,//Now it's only used in Lua Binding
kCocoStudioEventListener,//Now it's only used in Lua Binding
kArmatureWrapper,//Now it's only used in Lua Binding
kEventListenerAcc,//Now it's only used in Lua Binding
kEventListenerKeyboard,//Now it's only used in Lua Binding
kEventListenerTouch,//Now it's only used in Lua Binding
kEventListenerTouches,//Now it's only used in Lua Binding
kEventListenerMouse,//Now it's only used in Lua Binding
kEventListenerCustom,////Now it's only used in Lua Binding
};
struct BasicScriptData

View File

@ -23,12 +23,14 @@
****************************************************************************/
#include "CCLuaEngine.h"
#include "tolua_fix.h"
#include "cocos2d.h"
#include "CCArray.h"
#include "CCScheduler.h"
#include "LuaScriptHandlerMgr.h"
#include "extensions/GUI/CCControlExtension/CCControl.h"
#include "LuaOpengl.h"
#include "lua_cocos2dx_manual.hpp"
#include "lua_cocos2dx_extension_manual.h"
#include "lua_cocos2dx_coco_studio_manual.hpp"
@ -267,6 +269,36 @@ int LuaEngine::sendEvent(ScriptEvent* evt)
return handleArmatureWrapper(evt->data);
}
break;
case kEventListenerAcc:
{
return handleEventListenerAcc(evt->data);
}
break;
case kEventListenerKeyboard:
{
return handleEventListenerKeyboard(evt->data);
}
break;
case kEventListenerTouch:
{
return handleEventListenerTouch(evt->data);
}
break;
case kEventListenerTouches:
{
return handleEventListenerTouches(evt->data);
}
break;
case kEventListenerMouse:
{
return handleEventListenerMouse(evt->data);
}
break;
case kEventListenerCustom:
{
return handleEventListenerCustom(evt->data);
};
break;
default:
break;
}
@ -858,4 +890,227 @@ int LuaEngine::handleArmatureWrapper(void* data)
return 0;
}
int LuaEngine::handleEventListenerAcc(void* data)
{
if (nullptr == data)
return 0;
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
if (nullptr == basicScriptData->nativeObject || nullptr == basicScriptData->value)
return 0;
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, ScriptHandlerMgr::HandlerType::EVENTLISTENER_ACC);
if (0 == handler)
return 0;
lua_State* L = _stack->getLuaState();
LuaEventListenerAccelerationData* eventListennerAcc = static_cast<LuaEventListenerAccelerationData*>(basicScriptData->value);
toluafix_pushusertype_ccobject(L, eventListennerAcc->event->_ID, &(eventListennerAcc->event->_luaID), (void*)(eventListennerAcc->event),"Event");
Acceleration* accleration = static_cast<Acceleration*>(eventListennerAcc->acc);
lua_pushnumber(L,accleration->x);
lua_pushnumber(L,accleration->y);
lua_pushnumber(L,accleration->z);
lua_pushnumber(L,accleration->timestamp);
int ret = _stack->executeFunctionByHandler(handler, 5);
_stack->clean();
return ret;
}
int LuaEngine::handleEventListenerKeyboard(void* data)
{
if (nullptr == data)
return 0;
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
if (nullptr == basicScriptData->nativeObject || nullptr == basicScriptData->value)
return 0;
LuaEventListenerKeyboarData* keyboardData = static_cast<LuaEventListenerKeyboarData*>(basicScriptData->value);
ScriptHandlerMgr::HandlerType type = ScriptHandlerMgr::HandlerType::EVENTLISTENER_KEYBOARD_PRESSED;
if (keyboardData->status == LuaEventListenerKeyboarData::KeyboardStatus::RELEASE)
type = ScriptHandlerMgr::HandlerType::EVENTLISTENER_KEYBOARD_RELEASE;
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, type);
if (0 == handler)
return 0;
lua_State* L = _stack->getLuaState();
lua_pushinteger(L, keyboardData->keyCode);
toluafix_pushusertype_ccobject(L, keyboardData->event->_ID, &(keyboardData->event->_luaID), (void*)(keyboardData->event),"Event");
int ret = _stack->executeFunctionByHandler(handler, 2);
_stack->clean();
return ret;
}
int LuaEngine::handleEventListenerTouch(void* data)
{
if (nullptr == data)
return 0;
LuaEventListenerTouchData* listenerData = static_cast<LuaEventListenerTouchData*>(data);
if (nullptr == listenerData->nativeObject || nullptr == listenerData->touch || nullptr == listenerData->event)
return 0;
ScriptHandlerMgr::HandlerType handlerType;
switch (listenerData->eventCode)
{
case EventTouch::EventCode::BEGAN:
handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_BEGAN;
break;
case EventTouch::EventCode::MOVED:
handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_MOVED;
break;
case EventTouch::EventCode::ENDED:
handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_ENDED;
break;
case EventTouch::EventCode::CANCELLED:
handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_CANCELLED;
break;
default:
return 0;
}
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(listenerData->nativeObject, handlerType);
if (0 == handler)
return 0;
int ret = 0;
Touch* touch = listenerData->touch;
if (NULL != touch) {
lua_State* L = _stack->getLuaState();
toluafix_pushusertype_ccobject(L, listenerData->touch->_ID, &(listenerData->touch->_luaID), (void*)(listenerData->touch),"Touch");
toluafix_pushusertype_ccobject(L, listenerData->event->_ID, &(listenerData->event->_luaID), (void*)(listenerData->event),"Event");
ret = _stack->executeFunctionByHandler(handler, 2);
}
_stack->clean();
return ret;
}
int LuaEngine::handleEventListenerTouches(void* data)
{
if (nullptr == data)
return 0;
LuaEventListenerTouchesData * listenerData = static_cast<LuaEventListenerTouchesData*>(data);
if (NULL == listenerData->nativeObject || nullptr == listenerData->event || listenerData->touches.size() == 0)
return 0;
ScriptHandlerMgr::HandlerType handlerType;
switch (listenerData->eventCode)
{
case EventTouch::EventCode::BEGAN:
handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_BEGAN;
break;
case EventTouch::EventCode::MOVED:
handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_MOVED;
break;
case EventTouch::EventCode::ENDED:
handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_ENDED;
break;
case EventTouch::EventCode::CANCELLED:
handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_CANCELLED;
break;
default:
return 0;
}
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)listenerData->nativeObject, handlerType);
if (0 == handler)
return 0;
lua_State *L = _stack->getLuaState();
int ret = 0;
lua_newtable(L);
int i = 1;
for (auto& touch : listenerData->touches)
{
lua_pushnumber(L, (lua_Number)i);
toluafix_pushusertype_ccobject(L, touch->_ID, &(touch->_luaID), (void*)(touch),"Touch");
lua_rawset(L, -3);
++i;
}
toluafix_pushusertype_ccobject(L, listenerData->event->_ID, &(listenerData->event->_luaID), (void*)(listenerData->event),"Event");
ret = _stack->executeFunctionByHandler(handler, 2);
_stack->clean();
return ret;
}
int LuaEngine::handleEventListenerMouse(void* data)
{
if (nullptr == data)
return 0;
LuaEventListenerMouseData * listenerData = static_cast<LuaEventListenerMouseData*>(data);
if (NULL == listenerData->nativeObject || nullptr == listenerData->event )
return 0;
ScriptHandlerMgr::HandlerType handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_MOUSE_DOWN;
switch (listenerData->eventType)
{
case EventMouse::MouseEventType::MOUSE_DOWN:
handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_MOUSE_DOWN;
break;
case EventMouse::MouseEventType::MOUSE_UP:
handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_MOUSE_UP;
break;
case EventMouse::MouseEventType::MOUSE_MOVE:
handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_MOUSE_MOVE;
break;
case EventMouse::MouseEventType::MOUSE_SCROLL:
handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_MOUSE_SCROLL;
break;
default:
break;
}
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)listenerData->nativeObject, handlerType);
if (0 == handler)
return 0;
lua_State* L = _stack->getLuaState();
toluafix_pushusertype_ccobject(L, listenerData->event->_ID, &(listenerData->event->_luaID), (void*)(listenerData->event),"Event");
int ret = _stack->executeFunctionByHandler(handler, 1);
_stack->clean();
return ret;
}
int LuaEngine::handleEventListenerCustom(void* data)
{
if (nullptr == data)
return 0;
BasicScriptData * listenerData = static_cast<BasicScriptData*>(data);
if (NULL == listenerData->nativeObject || nullptr == listenerData->value )
return 0;
EventCustom* eventCustom = static_cast<EventCustom*>(listenerData->value);
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)listenerData->nativeObject, ScriptHandlerMgr::HandlerType::EVENTLISTENER_CUSTIOM);
if (0 == handler)
return 0;
lua_State* L = _stack->getLuaState();
toluafix_pushusertype_ccobject(L, eventCustom->_ID, &(eventCustom->_luaID), (void*)(eventCustom),"EventCustom");
int ret = _stack->executeFunctionByHandler(handler, 1);
_stack->clean();
return ret;
}
NS_CC_END

View File

@ -139,6 +139,12 @@ private:
int handleAssetsManagerEvent(void* data);
int handleCocoStudioEventListener(void* data);
int handleArmatureWrapper(void* data);
int handleEventListenerAcc(void* data);
int handleEventListenerKeyboard(void* data);
int handleEventListenerTouch(void* data);
int handleEventListenerTouches(void* data);
int handleEventListenerMouse(void* data);
int handleEventListenerCustom(void* data);
private:
static LuaEngine* _defaultEngine;
LuaStack *_stack;

View File

@ -144,6 +144,7 @@ bool LuaStack::init(void)
register_all_cocos2dx_extension(_state);
register_all_cocos2dx_deprecated(_state);
register_cocos2dx_extension_CCBProxy(_state);
register_cocos2dx_event_releated(_state);
tolua_opengl_open(_state);
register_all_cocos2dx_studio(_state);
register_all_cocos2dx_manual(_state);

View File

@ -110,6 +110,22 @@ public:
EVENT_LISTENER,
ARMATURE_EVENT,
EVENTLISTENER_ACC,
EVENTLISTENER_CUSTIOM,
EVENTLISTENER_KEYBOARD_PRESSED,
EVENTLISTENER_KEYBOARD_RELEASE,
EVENTLISTENER_TOUCH_BEGAN,
EVENTLISTENER_TOUCH_MOVED,
EVENTLISTENER_TOUCH_ENDED,
EVENTLISTENER_TOUCH_CANCELLED,
EVENTLISTENER_MOUSE_DOWN,
EVENTLISTENER_MOUSE_UP,
EVENTLISTENER_MOUSE_MOVE,
EVENTLISTENER_MOUSE_SCROLL,
};
typedef int Handler;

View File

@ -1 +1 @@
c1e3182aeb1023c573f64314788fbccb8da2de43
8d902acd33947b88d5c849c3e36c0da7c7b1af8c

View File

@ -9,6 +9,100 @@ extern "C" {
}
#endif
int register_all_cocos2dx_manual(lua_State* tolua_S);
#include "cocos2d.h"
NS_CC_BEGIN
class LuaEventListenerCustom
{
public:
static EventListenerCustom* create(const std::string& eventName);
};
class LuaEventListenerAcceleration
{
public:
static EventListenerAcceleration* create();
};
NS_CC_END
USING_NS_CC;
TOLUA_API int register_all_cocos2dx_manual(lua_State* tolua_S);
TOLUA_API int register_cocos2dx_event_releated(lua_State* tolua_S);
struct LuaEventListenerAccelerationData
{
void* acc;
Event* event;
LuaEventListenerAccelerationData(void* inAcc,Event* inEvent)
:acc(inAcc),event(inEvent)
{
}
};
struct LuaEventListenerKeyboarData
{
enum class KeyboardStatus :int
{
PRESSED = 0,
RELEASE,
};
int keyCode;
Event* event;
KeyboardStatus status;
LuaEventListenerKeyboarData(int inKeyCode,Event* inEvent,KeyboardStatus inStatus)
:keyCode(inKeyCode),event(inEvent),status(inStatus)
{
}
};
struct LuaEventListenerTouchData
{
EventTouch::EventCode eventCode;
void* nativeObject;
Touch* touch;
Event* event;
LuaEventListenerTouchData(EventTouch::EventCode inEventCode, void* inNativeObject, Touch* inTouch, Event* inEvent)
: eventCode(inEventCode),
nativeObject(inNativeObject),
touch(inTouch),
event(inEvent)
{
}
};
struct LuaEventListenerTouchesData
{
EventTouch::EventCode eventCode;
void* nativeObject;
std::vector<Touch*> touches;
Event* event;
LuaEventListenerTouchesData(EventTouch::EventCode inEventCode, void* inNativeObject, std::vector<Touch*> inTouches, Event* inEvent)
: eventCode(inEventCode),
nativeObject(inNativeObject),
touches(inTouches),
event(inEvent)
{
}
};
struct LuaEventListenerMouseData
{
EventMouse::MouseEventType eventType;
void* nativeObject;
Event* event;
LuaEventListenerMouseData(EventMouse::MouseEventType inEventType, void* inNativeObject, Event* inEvent)
: eventType(inEventType),
nativeObject(inNativeObject),
event(inEvent)
{
}
};
#endif // #ifndef COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_GENERATED_LUA_COCOS2DX_MANUAL_H

View File

@ -331,4 +331,20 @@ cc.GLYPHCOLLECTION_DYNAMIC = 0
cc.GLYPHCOLLECTION_NEHE = 1
cc.GLYPHCOLLECTION_ASCII = 2
cc.GLYPHCOLLECTION_CUSTOM = 3
cc.EVENTLISTENER_UNKNOWN = 0
cc.EVENTLISTENER_TOUCH_ONE_BY_ONE = 1
cc.EVENTLISTENER_TOUCH_ALL_AT_ONCE = 2
cc.EVENTLISTENER_KEYBOARD = 3
cc.EVENTLISTENER_MOUSE = 4
cc.EVENTLISTENER_ACCELERATION = 5
cc.EVENTLISTENER_CUSTOM = 6
cc.TOUCH_BEGAN = 0
cc.TOUCH_MOVED = 1
cc.TOUCH_ENDED = 2
cc.TOUCH_CANCELLED = 3
cc.KEYBOARD_PRESSED = 0
cc.KEYBOARD_RELEASE = 1

View File

@ -32,6 +32,7 @@ require "luaScript/LabelTestNew/LabelTestNew"
require "luaScript/LayerTest/LayerTest"
require "luaScript/MenuTest/MenuTest"
require "luaScript/MotionStreakTest/MotionStreakTest"
require "luaScript/NewEventDispatcherTest/NewEventDispatcherTest"
require "luaScript/NodeTest/NodeTest"
require "luaScript/OpenGLTest/OpenGLTest"
require "luaScript/ParallaxTest/ParallaxTest"
@ -87,6 +88,7 @@ local _allTests = {
{ isSupported = true, name = "MenuTest" , create_func = MenuTestMain },
{ isSupported = true, name = "MotionStreakTest" , create_func = MotionStreakTest },
{ isSupported = false, name = "MutiTouchTest" , create_func= MutiTouchTestMain },
{ isSupported = true, name = "NewEventDispatcherTest" , create_func = NewEventDispatcherTest },
{ isSupported = true, name = "NodeTest" , create_func = CocosNodeTest },
{ isSupported = true, name = "OpenGLTest" , create_func= OpenGLTestMain },
{ isSupported = true, name = "ParallaxTest" , create_func = ParallaxTestMain },

View File

@ -26,7 +26,7 @@ headers = %(cocosdir)s/cocos/2d/cocos2d.h %(cocosdir)s/cocos/audio/include/Simpl
# what classes to produce code for. You can use regular expressions here. When testing the regular
# expression, it will be enclosed in "^$", like this: "^Menu*$".
classes = New.* Sprite.* Scene Node.* Director Layer.* Menu.* Touch .*Action.* Move.* Rotate.* Blink.* Tint.* Sequence Repeat.* Fade.* Ease.* Scale.* Transition.* Spawn Animat.* Flip.* Delay.* Skew.* Jump.* Place.* Show.* Progress.* PointArray ToggleVisibility.* RemoveSelf Hide Particle.* Label.* Atlas.* TextureCache.* Texture2D Cardinal.* CatmullRom.* ParallaxNode TileMap.* TMX.* CallFunc RenderTexture GridAction Grid3DAction GridBase$ .+Grid Shaky3D Waves3D FlipX3D FlipY3D Speed ActionManager Set Data SimpleAudioEngine Scheduler Timer Orbit.* Follow.* Bezier.* CardinalSpline.* Camera.* DrawNode .*3D$ Liquid$ Waves$ ShuffleTiles$ TurnOffTiles$ Split.* Twirl$ FileUtils$ GLProgram ShaderCache Application ClippingNode MotionStreak ^Object$ UserDefault EGLViewProtocol EGLView Image
classes = New.* Sprite.* Scene Node.* Director Layer.* Menu.* Touch .*Action.* Move.* Rotate.* Blink.* Tint.* Sequence Repeat.* Fade.* Ease.* Scale.* Transition.* Spawn Animat.* Flip.* Delay.* Skew.* Jump.* Place.* Show.* Progress.* PointArray ToggleVisibility.* RemoveSelf Hide Particle.* Label.* Atlas.* TextureCache.* Texture2D Cardinal.* CatmullRom.* ParallaxNode TileMap.* TMX.* CallFunc RenderTexture GridAction Grid3DAction GridBase$ .+Grid Shaky3D Waves3D FlipX3D FlipY3D Speed ActionManager Set Data SimpleAudioEngine Scheduler Timer Orbit.* Follow.* Bezier.* CardinalSpline.* Camera.* DrawNode .*3D$ Liquid$ Waves$ ShuffleTiles$ TurnOffTiles$ Split.* Twirl$ FileUtils$ GLProgram ShaderCache Application ClippingNode MotionStreak ^Object$ UserDefault EGLViewProtocol EGLView Image Event.*
# what should we skip? in the format ClassName::[function function]
# ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also
@ -79,6 +79,9 @@ skip = Node::[setGLServerState description getUserObject .*UserData getGLServerS
Dictionary::[*],
Array::[*],
Range::[*],
EventListenerVector::[*],
EventListener.*::[create],
EventTouch::[(s|g)etTouches],
NotificationObserver::[*],
Image::[initWithString initWithImageData initWithRawData],
Sequence::[create],
@ -139,7 +142,7 @@ base_classes_to_skip = Clonable
# classes that create no constructor
# Set is special and we will use a hand-written constructor
abstract_classes = Action FiniteTimeAction ActionInterval ActionEase EaseRateAction EaseElastic EaseBounce ActionInstant GridAction Grid3DAction TiledGrid3DAction Director SpriteFrameCache TransitionEaseScene Set SimpleAudioEngine FileUtils Application ClippingNode Label EGLViewProtocol EGLView
abstract_classes = Action FiniteTimeAction ActionInterval ActionEase EaseRateAction EaseElastic EaseBounce ActionInstant GridAction Grid3DAction TiledGrid3DAction Director SpriteFrameCache TransitionEaseScene Set SimpleAudioEngine FileUtils Application ClippingNode Label EGLViewProtocol EGLView EventAcceleration
# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'.
script_control_cpp = no