local UIPreviewEditor = class("UIPreviewEditor" , require("Editor")); function UIPreviewEditor:ctor() UIPreviewEditor.super.ctor(self); -- 挂接UI的根节点 self.node = nil end function UIPreviewEditor:activate() UIPreviewEditor.super.activate(self); -- 恢复场景的位置 app.mainScene:setPosition(cc.p(0,0)); -- 恢复下场景的缩放 app.setting.ScreenScale = 1; app.mainScene:setScale(app.setting.ScreenScale); app.setting.OriginAnchor = cc.p(0,0); app.setting.SourceNodeNum = 0; app.setting.TargetNodeNum = 0; app:refreshSetting(); self.mBackgroundNode = self:createUIBackground(); app.mainLayer:addChild(self.mBackgroundNode , -100); end function UIPreviewEditor:unactivate() UIPreviewEditor.super.unactivate(self); self:closeNode() if self.mBackgroundNode then self.mBackgroundNode:removeFromParent(); self.mBackgroundNode = nil; end end -- 关闭当前打开的UI function UIPreviewEditor:closeNode() if self.node then self.node:removeFromParent(); self.node = nil; end end -- 从文件载入节点 function UIPreviewEditor:loadNode(nodeFile) self:closeNode(); self.node = tolua.cast(cc.StreamObject:loadFromFile(nodeFile) , "cc.Node3D"); self:initNode(self.node); return self.node; end -- 创建UI底图 function UIPreviewEditor:createUIBackground() local glNode = gl.glNodeCreate() glNode:setAnchorPoint(cc.p(0, 0)) local function primitivesDraw(transform, transformUpdated) local node = self.node; if node then -- 绑定相机、世界矩阵 cc.DrawPrimitives.setCamera(cc.Director:getInstance():getActiveCamera()); cc.DrawPrimitives.setWorldTransform(node:getNodeToWorldTransform()); -- 画矩形 local rect = cc.rect(0 , 0 , node:getContentSize().width , node:getContentSize().height); cc.DrawPrimitives.drawSolidRect(cc.p(rect.x , rect.y), cc.p(rect.x + rect.width , rect.y + rect.height) , cc.c4f(0.6,0.6,0.6,1)) -- 状态恢复 gl.lineWidth(1) cc.DrawPrimitives.drawColor4B(255,255,255,255) cc.DrawPrimitives.setPointSize(1) end end glNode:registerScriptDrawHandler(primitivesDraw) return glNode; end function UIPreviewEditor:play() UIPreviewEditor.super.play(self) if self.node then self.node:playCurveAnimation(); if self.node.stop then self.node:stop(); end if self.node.play then self.node:play(); end -- UIEffectNode需要重新播放下 local function visit(node) if node.stop then node:stop() end if node.play then node:play() end end self.node:visitNode(visit); end end function UIPreviewEditor:stop() if self.node then self.node:stopCurveAnimation(); if self.node.stop then self.node:stop(); end end end -- 创建新的光效 function UIPreviewEditor:newNode() local node = self:createNode("Layout"); self:initNode(node) node:ignoreContentAdaptWithSize(false); node:setSizeType(1); node:setSizePercent(cc.p(1,1)); node:setAnchorPoint(cc.p(0,0)); node:setBackGroundColorType(cc.LayoutBackGroundColorType.none); return node; end -- 初始化光效,添加到场景中 function UIPreviewEditor:initNode(node) self.node = node app.mainLayer:addChild(self.node) -- 默认播放更节点的动画 if app.setting.PlayUIAnimation then self.node:playCurveAnimation() end end function UIPreviewEditor:relayoutAll() local function onNode(node) if type(node.requestDoLayout) == "function" then node:requestDoLayout(); end return true; end -- 遍历所有子节点,把子UI取出来统一放在Items里 if self.node then self.node:visitNode(onNode); end end -- 属性改变通知 function UIPreviewEditor:onPropertyValueChanged(propName , newValue , oldValue) self:relayoutAll(); end function UIPreviewEditor:createNode(nodeType) local node = UIPreviewEditor.super.createNode(self , nodeType); if node.enableAutoPlay then node:enableAutoPlay(); end return node; end -- 各个派生类可以重写这个函数 function UIPreviewEditor:save(filename) self.node:recalcAllPList(); UIPreviewEditor.super.save(self, filename); local images = self.node:collectAllImage(); if images then local msg = "保存UI" .. filename .. "成功,这个UI用到了这些图片:\r\n"; for i , v in pairs(images) do msg = msg .. i .. " 用了 " .. v .. " 次\r\n"; end alert(msg); end end return UIPreviewEditor;