Commands = {} -- 批量命令 function Commands:batchCommand(commands) --[[ local function isParent(node , parent) local myParent = node:getParent(); if myParent == parent then return true; else if myParent then return isParent(myParent , parent); end end end local function sortFunction(a , b) return isParent(b.node , a.node); end table.sort(commands , sortFunction); print("BatchStart"); for i, v in ipairs(commands) do print("节点" , v.node:getName()); end print("BatchEnd"); ]] local function redo() if next(commands) == nil then return false; end for i, v in ipairs(commands) do v:redo(); end return true; end -- local function undo() for i = #commands , 1 , -1 do commands[i]:undo(); end end local function clear() for i, v in ipairs(commands) do if type(v.clear) == "function" then v:clear(); end end end return { redo = redo, undo = undo, clear = clear }; end -- 选中命令 --[[function Commands:select(nodes) local selectNodes = clone(nodes); -- 恢复操作 local function redo() if table.nums(selectNodes) <= 0 then return false; end -- 通知c#那边选中第一个 if selectNodes[1] then app.cs.setPropertyObject(selectNodes[1]); end for i , v in pairs(selectNodes) do app.editor:selectNode(v); end return true; end -- 撤销操作 local function undo() for i , v in pairs(selectNodes) do app.editor:unSelectNode(v); end end return {redo = redo , undo = undo}; end -- 取消选中命令 function Commands:unselect(nodes) local selectNodes = clone(nodes); -- 恢复操作 local function redo() if table.nums(selectNodes) <= 0 then return false; end for i , v in pairs(selectNodes) do app.editor:unSelectNode(v); end return true; end -- 撤销操作 local function undo() for i , v in pairs(selectNodes) do app.editor:selectNode(v); end end return {redo = redo , undo = undo}; end--]] -- 移动命令 -- 这里使用的node的父节点空间的位置,使用者要在外部转换好 function Commands:moveNode(node , newPos) local originalPos; -- 恢复操作 local function redo() if not node then return false; end originalPos = node:getTranslation(); node:setTranslation(newPos); -- 通知c#设置关键帧 if app.cs.setKeyFrame then app.cs.setKeyFrame(node , "位置"); end return true; end -- 撤销操作 local function undo() node:setTranslation(originalPos); -- 通知c#设置关键帧 if app.cs.setKeyFrame then app.cs.setKeyFrame(node , "位置"); end end return {redo = redo , undo = undo , node = node}; end -- 旋转命令 -- 这里使用的node的父节点空间的位置,使用者要在外部转换好 function Commands:rotateNode(node , newPos) local originalPos; -- 恢复操作 local function redo() if not node then return false; end originalPos = node:getEulerRotation(); node:setEulerRotation(newPos); -- 通知c#设置关键帧 if app.cs.setKeyFrame then app.cs.setKeyFrame(node , "旋转"); end return true; end -- 撤销操作 local function undo() node:setEulerRotation(originalPos); -- 通知c#设置关键帧 if app.cs.setKeyFrame then app.cs.setKeyFrame(node , "旋转"); end end return {redo = redo , undo = undo , node = node}; end -- 移动命令 -- 这里使用的node的父节点空间的位置,使用者要在外部转换好 function Commands:setWorldPosition(node , newPos) local originalPos; -- 恢复操作 local function redo() if not node then return false; end originalPos = node:getWorldPosition(); node:setWorldPosition(newPos); -- 通知c#设置关键帧 if app.cs.setKeyFrame then app.cs.setKeyFrame(node , "位置"); end return true; end -- 撤销操作 local function undo() node:setWorldPosition(originalPos); -- 通知c#设置关键帧 if app.cs.setKeyFrame then app.cs.setKeyFrame(node , "位置"); end end return {redo = redo , undo = undo , node = node}; end -- 改变大小命令 -- 这里使用的node的父节点空间的位置,使用者要在外部转换好 function Commands:sizeNode(node , newSize) local originalSize; -- 恢复操作 local function redo() if not node then return false; end originalSize = node:getSize(); node:setSize(newSize); -- 通知c#设置关键帧 if app.cs.setKeyFrame then app.cs.setKeyFrame(node , "尺寸"); end app.editor:relayoutAll(); return true; end -- 撤销操作 local function undo() node:setSize(originalSize); -- 通知c#设置关键帧 if app.cs.setKeyFrame then app.cs.setKeyFrame(node , "尺寸"); end app.editor:relayoutAll(); end return {redo = redo , undo = undo , node = node}; end -- 删除一个节点 function Commands:deleteNode(node) local parent; local childrenIndexs = {}; local releaseNode = true; -- 恢复操作 local function redo() parent = node:getParent(); -- 记录子节点的索引位置 for i, v in pairs(parent:getChildren()) do childrenIndexs[v] = i; end node:retain(); node:removeFromParent(); -- 通知c#更新树状结构 app.cs.deleteNode(node); -- 删除这个节点 app.editor:deleteNode(node); -- 选中当前节点 app.cs.setPropertyObject(parent); --app.editor:selectNode(parent); releaseNode = true; app.editor:relayoutAll(); return true; end -- 撤销操作 local function undo() parent:addChild(node); app.editor:autoSetNodeName(node); for i, v in pairs(parent:getChildren()) do v:setOrderOfArrival(childrenIndexs[v]); end -- 重新排序 parent:sortAllChildren(); node:release(); -- 更新下树形结构 app.cs.addNode(node , node , childrenIndexs[node] - 1); -- 清空序列 childrenIndexs = {}; -- 选中这个节点 app.editor:selectNode(node); releaseNode = false; app.editor:relayoutAll(); end -- 清空操作 local function clear() if releaseNode then node:release(); end end return {redo = redo , undo = undo, clear = clear , node = node}; end -- 改变节点 function Commands:modifyNode(node, newParent) local originParent; -- 恢复操作 local function redo() if not node or not newParent or not node:getParent() then return false; end originParent = node:getParent(); node:retain(); node:removeFromParent(); newParent:addChild(node); app.editor:autoSetNodeName(node); node:release(); -- 通知编辑器的树更新 app.cs.modifyNode(node, newParent); app.editor:relayoutAll(); return true; end -- 撤销操作 local function undo() node:retain(); node:removeFromParent(); originParent:addChild(node); app.editor:autoSetNodeName(node); node:release(); -- 通知编辑器的树更新 app.cs.modifyNode(node, originParent); app.editor:relayoutAll(); end return {redo = redo , undo = undo , node = node}; end -- 交换节点 function Commands:swapNode(node1, node2) local originParent; -- 恢复操作 local function redo() if not node1 or not node1:getParent() or node1:getParent() ~= node2:getParent() then return false; end originParent = node1:getParent(); originParent:swapChild(node1 , node2); -- 通知编辑器的树更新 app.cs.swapNode(node1, node2); app.editor:relayoutAll(); return true; end -- 撤销操作 local function undo() originParent:swapChild(node1 , node2); -- 通知编辑器的树更新 app.cs.swapNode(node1, node2); app.editor:relayoutAll(); end return {redo = redo , undo = undo , node = node1}; end -- 拷贝一个节点 function Commands:addNode(node, parentNode) local releaseNode = false; local function redo() if node == nil or parentNode == nil then return false; end local oldCount = node:getReferenceCount(); -- 添加节点 parentNode:addChild(node); app.editor:autoSetNodeName(node); local newCount = node:getReferenceCount(); -- 添加节点失败,例如UIPageView不支持添加非Layout派生的节点成为他的子节点 if newCount == oldCount then return false; end if releaseNode then node:release(); end -- 更新树形结构 app.cs.addNode(node , node , -1); -- 选中当前节点 app.cs.setPropertyObject(node); releaseNode = false; app.editor:relayoutAll(); return true; end local function undo() if node == nil or parentNode == nil then return; end -- 这里需要保存下 node:retain(); node:removeFromParent(); -- 更新树形结构 app.cs.deleteNode(node); app.cs.setPropertyObject(parentNode); releaseNode = true; app.editor:relayoutAll(); end local function clear() if releaseNode then node:release(); end end return { redo = redo, undo = undo, clear = clear , node = node }; end -- 拷贝一个节点动画 function Commands:copyNodeAnim(node, parentNode) local releaseNode = false; local copyAnim = parentNode:saveAnimation(cc.CURVE_ANIMATION); local function redo() if node == nil or parentNode == nil then return false; end -- 复制动画 parentNode:removeAllAnimations(); node:cloneIntoAnim(parentNode); if releaseNode and not tolua.isnull(node) then node:release(); end -- 选中当前节点 app.cs.setPropertyObject(parentNode); releaseNode = false; app.editor:relayoutAll(); return true; end local function undo() if node == nil or parentNode == nil then return; end parentNode:removeAllAnimations(); local anim = parentNode:loadAnimation(copyAnim) parentNode:addAnimation(anim); -- 更新树形结构 app.cs.setPropertyObject(parentNode); releaseNode = true; app.editor:relayoutAll(); end local function clear() if releaseNode and not tolua.isnull(node) then node:release(); end end return { redo = redo, undo = undo, clear = clear , node = node }; end require("CombineEffectCommands")