You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 line
3.5 KiB

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