From b8d4010ae50cd5f929f6e21eb067d17db702f504 Mon Sep 17 00:00:00 2001 From: samuele3hu Date: Thu, 2 Jan 2014 12:00:37 +0800 Subject: [PATCH] Develop branch adds lua trigger for CocoStudio --- .../project.pbxproj.REMOVED.git-id | 2 +- cocos/scripting/lua/bindings/CCLuaEngine.cpp | 14 +- .../scripting/lua/script}/extern.lua | 0 cocos/scripting/lua/script/lua_cocos2d.lua | 13 + .../lua/script/lua_cocos2d_studio.lua | 373 ++++++++++++++++++ .../CocoStudioSceneTest.lua | 53 ++- .../CocoStudioSceneTest/TriggerCode/acts.lua | 122 ++++++ .../CocoStudioSceneTest/TriggerCode/cons.lua | 1 + .../TriggerCode/eventDef.lua | 12 + .../Resources/luaScript/TouchesTest/Ball.lua | 2 +- .../luaScript/TouchesTest/Paddle.lua | 2 +- .../Resources/luaScript/VisibleRect.lua | 2 +- .../TestLua/Resources/luaScript/mainMenu.lua | 3 +- 13 files changed, 589 insertions(+), 10 deletions(-) rename {samples/Lua/TestLua/Resources/luaScript => cocos/scripting/lua/script}/extern.lua (100%) create mode 100644 cocos/scripting/lua/script/lua_cocos2d.lua create mode 100644 cocos/scripting/lua/script/lua_cocos2d_studio.lua create mode 100644 samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/acts.lua create mode 100644 samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/cons.lua create mode 100644 samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/eventDef.lua diff --git a/build/cocos2d_samples.xcodeproj/project.pbxproj.REMOVED.git-id b/build/cocos2d_samples.xcodeproj/project.pbxproj.REMOVED.git-id index 2d497c5e0b..4186b716ce 100644 --- a/build/cocos2d_samples.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/build/cocos2d_samples.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -b83c3a0fd60d6b1b1a032495615afd77fccd5050 \ No newline at end of file +ab749c10e473dbc8a0ae687363de08a217f3a07a \ No newline at end of file diff --git a/cocos/scripting/lua/bindings/CCLuaEngine.cpp b/cocos/scripting/lua/bindings/CCLuaEngine.cpp index 4005c83462..b064774b2a 100644 --- a/cocos/scripting/lua/bindings/CCLuaEngine.cpp +++ b/cocos/scripting/lua/bindings/CCLuaEngine.cpp @@ -188,8 +188,18 @@ int LuaEngine::reallocateScriptHandler(int nHandler) bool LuaEngine::parseConfig(ConfigType type, const std::string& str) { - // FIXME: TO IMPLEMENT - return false; + lua_getglobal(_stack->getLuaState(), "__onParseConfig"); + if (!lua_isfunction(_stack->getLuaState(), -1)) + { + CCLOG("[LUA ERROR] name '%s' does not represent a Lua function", "__onParseConfig"); + lua_pop(_stack->getLuaState(), 1); + return false; + } + + _stack->pushInt((int)type); + _stack->pushString(str.c_str()); + + return _stack->executeFunction(2); } int LuaEngine::sendEvent(ScriptEvent* evt) diff --git a/samples/Lua/TestLua/Resources/luaScript/extern.lua b/cocos/scripting/lua/script/extern.lua similarity index 100% rename from samples/Lua/TestLua/Resources/luaScript/extern.lua rename to cocos/scripting/lua/script/extern.lua diff --git a/cocos/scripting/lua/script/lua_cocos2d.lua b/cocos/scripting/lua/script/lua_cocos2d.lua new file mode 100644 index 0000000000..bcf5f9d979 --- /dev/null +++ b/cocos/scripting/lua/script/lua_cocos2d.lua @@ -0,0 +1,13 @@ +require "lua_cocos2d_studio" + +local ConfigType = +{ + NONE = 0, + COCOSTUDIO = 1, +} + +function __onParseConfig(configType,jasonStr) + if configType == ConfigType.COCOSTUDIO then + ccs.TriggerMng.getInstance():parse(jasonStr) + end +end diff --git a/cocos/scripting/lua/script/lua_cocos2d_studio.lua b/cocos/scripting/lua/script/lua_cocos2d_studio.lua new file mode 100644 index 0000000000..33cda6970c --- /dev/null +++ b/cocos/scripting/lua/script/lua_cocos2d_studio.lua @@ -0,0 +1,373 @@ +require "json" +require "extern" + +ccs = ccs or {} + +function ccs.sendTriggerEvent(event) + local triggerObjArr = ccs.TriggerMng.getInstance():get(event) + + if nil == triggerObjArr then + return + end + + for i = 1, table.getn(triggerObjArr) do + local triObj = triggerObjArr[i] + if nil ~= triObj and triObj.detect then + triObj:done() + end + end +end + +function ccs.registerTriggerClass(className, createFunc) + ccs.TInfo.new(className,createFunc) +end + +ccs.TInfo = class("TInfo") +ccs.TInfo._className = "" +ccs.TInfo._fun = nil + +function ccs.TInfo:ctor(c,f) + -- @param {String|ccs.TInfo}c + -- @param {Function}f + if nil ~= f then + self._className = c + self._fun = f + else + self._className = c._className + self._fun = c._fun + end + + ccs.ObjectFactory.getInstance():registerType(self) +end + +ccs.ObjectFactory = class("ObjectFactory") +ccs.ObjectFactory._typeMap = nil +ccs.ObjectFactory._instance = nil + +function ccs.ObjectFactory:ctor() + self._typeMap = {} +end + +function ccs.ObjectFactory.getInstance() + if nil == ccs.ObjectFactory._instance then + ccs.ObjectFactory._instance = ccs.ObjectFactory.new() + end + + return ccs.ObjectFactory._instance +end + +function ccs.ObjectFactory.destroyInstance() + ccs.ObjectFactory._instance = nil +end + +function ccs.ObjectFactory:createObject(classname) + local obj = nil + local t = self._typeMap[classname] + if nil ~= t then + obj = t._fun() + end + + return obj +end + +function ccs.ObjectFactory:registerType(t) + self._typeMap[t._className] = t +end + +ccs.TriggerObj = class("TriggerObj") +ccs.TriggerObj._cons = {} +ccs.TriggerObj._acts = {} +ccs.TriggerObj._enable = false +ccs.TriggerObj._id = 0 +ccs.TriggerObj._vInt = {} + +function ccs.TriggerObj.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TriggerObj) + return target +end + +function ccs.TriggerObj:ctor() + self:init() +end + +function ccs.TriggerObj:init() + self._id = 0 + self._enable = true + self._cons = {} + self._acts = {} + self._vInt = {} +end + +function ccs.TriggerObj:detect() + if (not self._enable) or (table.getn(self._cons) == 0) then + return true + end + + local ret = true + local obj = nil + for i = 1 , table.getn(self._cons) do + obj = self._cons[i] + if nil ~= obj and obj.detect then + ret = ret and obj:detect() + end + end + return ret +end + +function ccs.TriggerObj:done() + if (not self._enable) or (table.getn(self._acts) == 0) then + return + end + + local obj = nil + for i = 1, table.getn(self._acts) do + obj = self._acts[i] + if nil ~= obj and obj.done then + obj:done() + end + end +end + +function ccs.TriggerObj:removeAll() + local obj = nil + for i=1, table.getn(self._cons) do + obj = self._cons[i] + if nil ~= obj then + obj:removeAll() + end + end + self._cons = {} + + for i=1, table.getn(self._acts) do + obj = self._acts[i] + if nil ~= obj then + obj:removeAll() + end + end + self._acts = {} +end + +function ccs.TriggerObj:serialize(jsonValue) + self._id = jsonValue["id"] + local count = 0 + + --condition + local cons = jsonValue["conditions"] + if nil ~= cons then + count = table.getn(cons) + for i = 1, count do + local subDict = cons[i] + local className = subDict["classname"] + if nil ~= className then + local obj = ObjectFactory.getInstance():createObject(className) + assert(nil ~= obj, string.format("class named %s can not implement!",className)) + obj:serialize(subDict) + obj:init() + table.insert(self._cons, obj) + end + end + end + + local actions = jsonValue["actions"] + if nil ~= actions then + count = table.getn(actions) + for i = 1,count do + local subAction = actions[i] + local className = subAction["classname"] + if nil ~= className then + local act = ccs.ObjectFactory.getInstance():createObject(className) + assert(nil ~= act ,string.format("class named %s can not implement!",className)) + act:serialize(subAction) + act:init() + table.insert(self._acts,act) + end + end + end + + local events = jsonValue["events"] + if nil ~= events then + count = table.getn(events) + for i = 1, count do + local subEveent = events[i] + local eventID = subEveent["id"] + if eventID >= 0 then + table.insert(self._vInt,eventID) + end + end + end +end + +function ccs.TriggerObj:getId() + return self._id +end + +function ccs.TriggerObj:setEnable(enable) + self._enable = enable +end + +function ccs.TriggerObj:getEvents() + return self._vInt +end + +ccs.TriggerMng = class("TriggerMng") +ccs.TriggerMng._eventTriggers = nil +ccs.TriggerMng._triggerObjs = nil +ccs.TriggerMng._movementDispatches = nil +ccs.TriggerMng._instance = nil + +function ccs.TriggerMng:ctor() + self._triggerObjs = {} + self._movementDispatches = {} + self._eventTriggers = {} +end + +function ccs.TriggerMng.getInstance() + if ccs.TriggerMng._instance == nil then + ccs.TriggerMng._instance = ccs.TriggerMng.new() + end + + return ccs.TriggerMng._instance +end + +function ccs.TriggerMng.destroyInstance() + if ccs.TriggerMng._instance ~= nil then + ccs.TriggerMng._instance:removeAll() + ccs.TriggerMng._instance = nil + end +end + +function ccs.TriggerMng:triggerMngVersion() + return "1.0.0.0" +end + +function ccs.TriggerMng:parse(jsonStr) + local parseTable = json.decode(jsonStr,1) + if nil == parseTable then + return + end + + local count = table.getn(parseTable) + for i = 1, count do + local subDict = parseTable[i] + local triggerObj = ccs.TriggerObj.new() + triggerObj:serialize(subDict) + local events = triggerObj:getEvents() + for j = 1, table.getn(events) do + local event = events[j] + self:add(event, triggerObj) + end + + self._triggerObjs[triggerObj:getId()] = triggerObj + end +end + +function ccs.TriggerMng:get(event) + return self._eventTriggers[event] +end + +function ccs.TriggerMng:getTriggerObj(id) + return self._triggerObjs[id] +end + +function ccs.TriggerMng:add(event,triggerObj) + local eventTriggers = self._eventTriggers[event] + if nil == eventTriggers then + eventTriggers = {} + end + + local exist = false + for i = 1, table.getn(eventTriggers) do + if eventTriggers[i] == triggers then + exist = true + break + end + end + + if not exist then + table.insert(eventTriggers,triggerObj) + self._eventTriggers[event] = eventTriggers + end +end + +function ccs.TriggerMng:removeAll( ) + for k in pairs(self._eventTriggers) do + local triObjArr = self._eventTriggers[k] + for j = 1, table.getn(triObjArr) do + local obj = triObjArr[j] + obj:removeAll() + end + end + self._eventTriggers = {} +end + +function ccs.TriggerMng:remove(event, obj) + + if nil ~= obj then + return self:removeObjByEvent(event, obj) + end + + assert(event >= 0,"event must be larger than 0") + if nil == self._eventTriggers then + return false + end + + local triObjects = self._eventTriggers[event] + if nil == triObjects then + return false + end + + for i = 1, table.getn(triObjects) do + local triObject = triggers[i] + if nil ~= triObject then + triObject:remvoeAll() + end + end + + self._eventTriggers[event] = nil + return true +end + +function ccs.TriggerMng:removeObjByEvent(event, obj) + assert(event >= 0,"event must be larger than 0") + if nil == self._eventTriggers then + return false + end + + local triObjects = self._eventTriggers[event] + if nil == triObjects then + return false + end + + for i = 1,table.getn(triObjects) do + local triObject = triObjects[i] + if nil ~= triObject and triObject == obj then + triObject:remvoeAll() + table.remove(triObjects, i) + return true + end + end +end + +function ccs.TriggerMng:removeTriggerObj(id) + local obj = self.getTriggerObj(id) + + if nil == obj then + return false + end + + local events = obj:getEvents() + for i = 1, table.getn(events) do + self:remove(events[i],obj) + end + + return true +end + +function ccs.TriggerMng:isEmpty() + return (not (nil == self._eventTriggers)) or table.getn(self._eventTriggers) <= 0 +end diff --git a/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/CocoStudioSceneTest.lua b/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/CocoStudioSceneTest.lua index c7f557b0be..9d9fb3f504 100644 --- a/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/CocoStudioSceneTest.lua +++ b/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/CocoStudioSceneTest.lua @@ -1,3 +1,7 @@ +require "luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/acts" +require "luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/cons" +require "luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/eventDef" + local SceneEditorTestLayer = class("SceneEditorTestLayer") SceneEditorTestLayer._curNode = nil @@ -19,8 +23,9 @@ function SceneEditorTestLayer:createGameScene() self._curNode = node local function menuCloseCallback( sender ) - ccs.SceneReader:getInstance():destroySceneReader() - ccs.ActionManagerEx:destroyActionManager() + ccs.SceneReader:destroyInstance() + ccs.ActionManagerEx:destroyInstance() + ccs.TriggerMng.destroyInstance() local scene = CocoStudioTestMain() if scene ~= nil then cc.Director:getInstance():replaceScene(scene) @@ -42,9 +47,53 @@ function SceneEditorTestLayer:createGameScene() ccs.ActionManagerEx:getInstance():playActionByName("startMenu_1.json","Animation1") + local function onNodeEvent(event) + if event == "enter" then + self:onEnter() + elseif event == "exit" then + self:onExit() + end + end + + self:registerScriptHandler(onNodeEvent) + + local listener = cc.EventListenerTouchOneByOne:create() + listener:setSwallowTouches(true) + listener:registerScriptHandler(self.onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN ) + listener:registerScriptHandler(self.onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED ) + listener:registerScriptHandler(self.onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED ) + listener:registerScriptHandler(self.onTouchCancelled,cc.Handler.EVENT_TOUCH_CANCELLED ) + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self) + return node end +function SceneEditorTestLayer:onEnter() + ccs.sendTriggerEvent(triggerEventDef.TRIGGEREVENT_ENTERSCENE) +end + +function SceneEditorTestLayer:onExit() + ccs.sendTriggerEvent(triggerEventDef.TRIGGEREVENT_LEAVESCENE) +end + +function SceneEditorTestLayer:onTouchBegan(touch,event) + ccs.sendTriggerEvent(triggerEventDef.TRIGGEREVENT_TOUCHBEGAN) + return true +end + +function SceneEditorTestLayer:onTouchMoved(touch,event) + ccs.sendTriggerEvent(triggerEventDef.TRIGGEREVENT_TOUCHMOVED) +end + +function SceneEditorTestLayer:onTouchEnded(touch,event) + ccs.sendTriggerEvent(triggerEventDef.TRIGGEREVENT_TOUCHENDED) +end + +function SceneEditorTestLayer:onTouchCancelled(touch,event) + sendTriggerEvent(triggerEventDef.TRIGGEREVENT_TOUCHCANCELLED) +end + function SceneEditorTestLayer.create() local scene = cc.Scene:create() local layer = SceneEditorTestLayer.extend(cc.LayerColor:create()) diff --git a/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/acts.lua b/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/acts.lua new file mode 100644 index 0000000000..b0a79acd2a --- /dev/null +++ b/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/acts.lua @@ -0,0 +1,122 @@ +require "lua_cocos2d_studio" + +local TMoveBy = class("TMoveBy") +TMoveBy._tag = -1 +TMoveBy._duration = 0 +TMoveBy._x = 0 +TMoveBy._y = 0 +TMoveBy._reverse = false +function TMoveBy:ctor() + self._tag = -1 + self._duration = 0.0 + self._x = 0 + self._y = 0 + self._reverse = false +end + +function TMoveBy:init() + return true +end + +function TMoveBy:done() + local node = ccs.SceneReader:getInstance():getNodeByTag(self._tag) + if nil == node then + return + end + + local actionBy = cc.MoveBy:create(self._duration, cc.p(self._x, self._y)) + if nil == actionBy then + return + end + + if false == self._reverse then + local actionByBack = actionBy:reverse() + node:runAction(cc.Sequence:create(actionBy, actionByBack)) + else + node:runAction(actionBy) + end +end + +function TMoveBy:serialize(value) + local dataItems = value["dataitems"] + if nil ~= dataItems then + local count = table.getn(dataItems) + for i = 1, count do + local subDict = dataItems[i] + local key = subDict["key"] + if key == "Tag" then + self._tag = subDict["value"] + elseif key == "Duration" then + self._duration = subDict["value"] + elseif key == "x" then + self._x = subDict["value"] + elseif key == "y" then + self._y = subDict["value"] + elseif key == "IsReverse" then + self._reverse = subDict["value"] + end + end + end +end + +function TMoveBy:removeAll() + print("TMoveBy::removeAll") +end + +local TScaleTo = class("TScaleTo") +TScaleTo._tag = -1 +TScaleTo._duration = 0 +TScaleTo._scaleX = 0 +TScaleTo._scaleY = 0 + +function TScaleTo:ctor() + self._tag = -1 + self._duration = 0 + self._scaleX = 0 + self._scaleY = 0 +end + +function TScaleTo:init() + return true +end + +function TScaleTo:done() + local node = ccs.SceneReader:getInstance():getNodeByTag(self._tag) + if nil == node then + return + end + + local actionTo = cc.ScaleTo:create(self._duration, self._scaleX, self._scaleY) + if nil == actionTo then + return + end + + node:runAction(actionTo) +end + +function TScaleTo:serialize(value) + local dataItems = value["dataitems"] + if nil ~= dataItems then + local count = table.getn(dataItems) + for i = 1, count do + local subDict = dataItems[i] + local key = subDict["key"] + if key == "Tag" then + self._tag = subDict["value"] + elseif key == "Duration" then + self._duration = subDict["value"] + elseif key == "ScaleX" then + self._scaleX = subDict["value"] + elseif key == "ScaleY" then + self._scaleY = subDict["value"] + end + end + end +end + +function TScaleTo:removeAll() + print("TScaleTo::removeAll") +end + +ccs.registerTriggerClass("TScaleTo",TScaleTo.new) +ccs.registerTriggerClass("TMoveBy",TMoveBy.new) diff --git a/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/cons.lua b/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/cons.lua new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/cons.lua @@ -0,0 +1 @@ + diff --git a/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/eventDef.lua b/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/eventDef.lua new file mode 100644 index 0000000000..a973745b38 --- /dev/null +++ b/samples/Lua/TestLua/Resources/luaScript/CocoStudioTest/CocoStudioSceneTest/TriggerCode/eventDef.lua @@ -0,0 +1,12 @@ + +triggerEventDef = +{ + TRIGGEREVENT_ENTERSCENE = 0, + TRIGGEREVENT_LEAVESCENE = 1, + TRIGGEREVENT_INITSCENE = 2, + TRIGGEREVENT_UPDATESCENE = 3, + TRIGGEREVENT_TOUCHBEGAN = 4, + TRIGGEREVENT_TOUCHMOVED = 5, + TRIGGEREVENT_TOUCHENDED = 6, + TRIGGEREVENT_TOUCHCANCELLED = 7, +} diff --git a/samples/Lua/TestLua/Resources/luaScript/TouchesTest/Ball.lua b/samples/Lua/TestLua/Resources/luaScript/TouchesTest/Ball.lua index 06c967b2a6..60cb715089 100644 --- a/samples/Lua/TestLua/Resources/luaScript/TouchesTest/Ball.lua +++ b/samples/Lua/TestLua/Resources/luaScript/TouchesTest/Ball.lua @@ -1,5 +1,5 @@ -require "luaScript/extern" +require "extern" require "luaScript/VisibleRect" require "luaScript/TouchesTest/Paddle" diff --git a/samples/Lua/TestLua/Resources/luaScript/TouchesTest/Paddle.lua b/samples/Lua/TestLua/Resources/luaScript/TouchesTest/Paddle.lua index 050d587ed5..61da0a5171 100644 --- a/samples/Lua/TestLua/Resources/luaScript/TouchesTest/Paddle.lua +++ b/samples/Lua/TestLua/Resources/luaScript/TouchesTest/Paddle.lua @@ -1,4 +1,4 @@ -require "luaScript/extern" +require "extern" require "luaScript/VisibleRect" Paddle = class("Paddle", function(texture) diff --git a/samples/Lua/TestLua/Resources/luaScript/VisibleRect.lua b/samples/Lua/TestLua/Resources/luaScript/VisibleRect.lua index 7d00240f8a..5d53b1f8e5 100644 --- a/samples/Lua/TestLua/Resources/luaScript/VisibleRect.lua +++ b/samples/Lua/TestLua/Resources/luaScript/VisibleRect.lua @@ -1,4 +1,4 @@ -require "luaScript/extern" +require "extern" require "Cocos2d" VisibleRect = class("VisibleRect") diff --git a/samples/Lua/TestLua/Resources/luaScript/mainMenu.lua b/samples/Lua/TestLua/Resources/luaScript/mainMenu.lua index bba1c79a98..cdb4c57d83 100644 --- a/samples/Lua/TestLua/Resources/luaScript/mainMenu.lua +++ b/samples/Lua/TestLua/Resources/luaScript/mainMenu.lua @@ -1,5 +1,4 @@ - - +require "lua_cocos2d" require "Cocos2d" require "Cocos2dConstants" require "Opengl"