Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

172 righe
4.1 KiB

  1. local UIPreviewEditor = class("UIPreviewEditor" , require("Editor"));
  2. function UIPreviewEditor:ctor()
  3. UIPreviewEditor.super.ctor(self);
  4. -- 挂接UI的根节点
  5. self.node = nil
  6. end
  7. function UIPreviewEditor:activate()
  8. UIPreviewEditor.super.activate(self);
  9. -- 恢复场景的位置
  10. app.mainScene:setPosition(cc.p(0,0));
  11. -- 恢复下场景的缩放
  12. app.setting.ScreenScale = 1;
  13. app.mainScene:setScale(app.setting.ScreenScale);
  14. app.setting.OriginAnchor = cc.p(0,0);
  15. app.setting.SourceNodeNum = 0;
  16. app.setting.TargetNodeNum = 0;
  17. app:refreshSetting();
  18. self.mBackgroundNode = self:createUIBackground();
  19. app.mainLayer:addChild(self.mBackgroundNode , -100);
  20. end
  21. function UIPreviewEditor:unactivate()
  22. UIPreviewEditor.super.unactivate(self);
  23. self:closeNode()
  24. if self.mBackgroundNode then
  25. self.mBackgroundNode:removeFromParent();
  26. self.mBackgroundNode = nil;
  27. end
  28. end
  29. -- 关闭当前打开的UI
  30. function UIPreviewEditor:closeNode()
  31. if self.node then
  32. self.node:removeFromParent();
  33. self.node = nil;
  34. end
  35. end
  36. -- 从文件载入节点
  37. function UIPreviewEditor:loadNode(nodeFile)
  38. self:closeNode();
  39. self.node = tolua.cast(cc.StreamObject:loadFromFile(nodeFile) , "cc.Node3D");
  40. self:initNode(self.node);
  41. return self.node;
  42. end
  43. -- 创建UI底图
  44. function UIPreviewEditor:createUIBackground()
  45. local glNode = gl.glNodeCreate()
  46. glNode:setAnchorPoint(cc.p(0, 0))
  47. local function primitivesDraw(transform, transformUpdated)
  48. local node = self.node;
  49. if node then
  50. -- 绑定相机、世界矩阵
  51. cc.DrawPrimitives.setCamera(cc.Director:getInstance():getActiveCamera());
  52. cc.DrawPrimitives.setWorldTransform(node:getNodeToWorldTransform());
  53. -- 画矩形
  54. local rect = cc.rect(0 , 0 , node:getContentSize().width , node:getContentSize().height);
  55. 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))
  56. -- 状态恢复
  57. gl.lineWidth(1)
  58. cc.DrawPrimitives.drawColor4B(255,255,255,255)
  59. cc.DrawPrimitives.setPointSize(1)
  60. end
  61. end
  62. glNode:registerScriptDrawHandler(primitivesDraw)
  63. return glNode;
  64. end
  65. function UIPreviewEditor:play()
  66. UIPreviewEditor.super.play(self)
  67. if self.node then
  68. self.node:playCurveAnimation();
  69. if self.node.stop then
  70. self.node:stop();
  71. end
  72. if self.node.play then
  73. self.node:play();
  74. end
  75. -- UIEffectNode需要重新播放下
  76. local function visit(node)
  77. if node.stop then
  78. node:stop()
  79. end
  80. if node.play then
  81. node:play()
  82. end
  83. end
  84. self.node:visitNode(visit);
  85. end
  86. end
  87. function UIPreviewEditor:stop()
  88. if self.node then
  89. self.node:stopCurveAnimation();
  90. if self.node.stop then
  91. self.node:stop();
  92. end
  93. end
  94. end
  95. -- 创建新的光效
  96. function UIPreviewEditor:newNode()
  97. local node = self:createNode("Layout");
  98. self:initNode(node)
  99. node:ignoreContentAdaptWithSize(false);
  100. node:setSizeType(1);
  101. node:setSizePercent(cc.p(1,1));
  102. node:setAnchorPoint(cc.p(0,0));
  103. node:setBackGroundColorType(cc.LayoutBackGroundColorType.none);
  104. return node;
  105. end
  106. -- 初始化光效,添加到场景中
  107. function UIPreviewEditor:initNode(node)
  108. self.node = node
  109. app.mainLayer:addChild(self.node)
  110. -- 默认播放更节点的动画
  111. if app.setting.PlayUIAnimation then
  112. self.node:playCurveAnimation()
  113. end
  114. end
  115. function UIPreviewEditor:relayoutAll()
  116. local function onNode(node)
  117. if type(node.requestDoLayout) == "function" then
  118. node:requestDoLayout();
  119. end
  120. return true;
  121. end
  122. -- 遍历所有子节点,把子UI取出来统一放在Items里
  123. if self.node then
  124. self.node:visitNode(onNode);
  125. end
  126. end
  127. -- 属性改变通知
  128. function UIPreviewEditor:onPropertyValueChanged(propName , newValue , oldValue)
  129. self:relayoutAll();
  130. end
  131. function UIPreviewEditor:createNode(nodeType)
  132. local node = UIPreviewEditor.super.createNode(self , nodeType);
  133. if node.enableAutoPlay then
  134. node:enableAutoPlay();
  135. end
  136. return node;
  137. end
  138. -- 各个派生类可以重写这个函数
  139. function UIPreviewEditor:save(filename)
  140. self.node:recalcAllPList();
  141. UIPreviewEditor.super.save(self, filename);
  142. local images = self.node:collectAllImage();
  143. if images then
  144. local msg = "保存UI" .. filename .. "成功,这个UI用到了这些图片:\r\n";
  145. for i , v in pairs(images) do
  146. msg = msg .. i .. " 用了 " .. v .. " 次\r\n";
  147. end
  148. alert(msg);
  149. end
  150. end
  151. return UIPreviewEditor;