mirror of https://github.com/axmolengine/axmol.git
Develop branch adds lua trigger for CocoStudio
This commit is contained in:
parent
e5389ddb31
commit
b8d4010ae5
|
@ -1 +1 @@
|
|||
b83c3a0fd60d6b1b1a032495615afd77fccd5050
|
||||
ab749c10e473dbc8a0ae687363de08a217f3a07a
|
|
@ -188,10 +188,20 @@ int LuaEngine::reallocateScriptHandler(int nHandler)
|
|||
|
||||
bool LuaEngine::parseConfig(ConfigType type, const std::string& str)
|
||||
{
|
||||
// FIXME: TO IMPLEMENT
|
||||
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)
|
||||
{
|
||||
if (NULL == evt)
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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())
|
||||
|
|
|
@ -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)
|
|
@ -0,0 +1 @@
|
|||
|
|
@ -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,
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
require "luaScript/extern"
|
||||
require "extern"
|
||||
require "luaScript/VisibleRect"
|
||||
require "luaScript/TouchesTest/Paddle"
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
require "luaScript/extern"
|
||||
require "extern"
|
||||
require "luaScript/VisibleRect"
|
||||
|
||||
Paddle = class("Paddle", function(texture)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
require "luaScript/extern"
|
||||
require "extern"
|
||||
require "Cocos2d"
|
||||
|
||||
VisibleRect = class("VisibleRect")
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
|
||||
|
||||
require "lua_cocos2d"
|
||||
require "Cocos2d"
|
||||
require "Cocos2dConstants"
|
||||
require "Opengl"
|
||||
|
|
Loading…
Reference in New Issue