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.

399 line
11 KiB

  1. require("EditorGlobalFunction")
  2. local AlignTool = class("AlignTool")
  3. function AlignTool:ctor()
  4. end
  5. -- 计算最左边
  6. local function calcLeft()
  7. local minX = 65536;
  8. for i , v in pairs(app.editor.SelectedNodes) do
  9. local node = v;
  10. minX = math.min(minX , node:getWorldPositionX() - node:getAnchorPoint().x * node:getContentSize().width * node:getScaleX());
  11. end
  12. return minX;
  13. end
  14. -- 计算最右边
  15. local function calcRight()
  16. local maxX = 0;
  17. for i , v in pairs(app.editor.SelectedNodes) do
  18. local node = v;
  19. maxX = math.max(maxX , node:getWorldPositionX() + (1 - node:getAnchorPoint().x) * node:getContentSize().width * node:getScaleX());
  20. end
  21. return maxX;
  22. end
  23. -- 计算最上边
  24. local function calcTop()
  25. local maxY = 0;
  26. for i , v in pairs(app.editor.SelectedNodes) do
  27. local node = v;
  28. maxY = math.max(maxY , node:getWorldPositionY() + (1 - node:getAnchorPoint().y) * node:getContentSize().height * node:getScaleY());
  29. end
  30. return maxY;
  31. end
  32. -- 计算最下边
  33. local function calcBottom()
  34. local minY = 65536;
  35. for i , v in pairs(app.editor.SelectedNodes) do
  36. local node = v;
  37. minY = math.min(minY , node:getWorldPositionY() - node:getAnchorPoint().y * node:getContentSize().height * node:getScaleY());
  38. end
  39. return minY;
  40. end
  41. -- 获得所有选中元素的总宽高
  42. local function calcAllSize()
  43. local size = cc.size(0,0);
  44. for i , v in pairs(app.editor.SelectedNodes) do
  45. local node = v;
  46. local s = node:getSize();
  47. size.width = size.width + s.width;
  48. size.height = size.height + s.height;
  49. end
  50. return size;
  51. end
  52. -- 获得最后一个选中的节点
  53. local function getLastSelectedNode()
  54. local node;
  55. for i , v in pairs(app.editor.SelectedNodes) do
  56. if node == nil or v.SelectedIndex > node.SelectedIndex then
  57. node = v;
  58. end
  59. end
  60. return node;
  61. end
  62. function AlignTool:AlignLeft()
  63. local minX = calcLeft();
  64. local commands = {};
  65. for i , v in pairs(app.editor.SelectedNodes) do
  66. local node = v;
  67. local pos = node:getWorldPosition();
  68. pos.x = minX + node:getAnchorPoint().x * node:getContentSize().width * node:getScaleX();
  69. table.insert(commands, Commands:setWorldPosition(node, pos));
  70. end
  71. app.undoRedoManager:doCommand(Commands:batchCommand(commands));
  72. end
  73. function AlignTool:AlignCenter()
  74. local minX = calcLeft();
  75. local maxX = calcRight();
  76. local width = maxX - minX;
  77. local center = minX + width / 2;
  78. local commands = {};
  79. for i , v in pairs(app.editor.SelectedNodes) do
  80. local node = v;
  81. local offsetX = center - (node:getWorldPositionX() - node:getAnchorPoint().x * node:getContentSize().width * node:getScaleX() + node:getContentSize().width * node:getScaleX() / 2);
  82. local pos = node:getWorldPosition();
  83. pos.x = node:getWorldPositionX() + offsetX;
  84. table.insert(commands, Commands:setWorldPosition(node, pos));
  85. end
  86. app.undoRedoManager:doCommand(Commands:batchCommand(commands));
  87. end
  88. function AlignTool:AlignRight()
  89. local maxX = calcRight();
  90. local commands = {};
  91. for i , v in pairs(app.editor.SelectedNodes) do
  92. local node = v;
  93. local pos = node:getWorldPosition();
  94. pos.x = maxX - node:getContentSize().width * node:getScaleX() + node:getAnchorPoint().x * node:getContentSize().width * node:getScaleX();
  95. table.insert(commands, Commands:setWorldPosition(node, pos));
  96. end
  97. app.undoRedoManager:doCommand(Commands:batchCommand(commands));
  98. end
  99. function AlignTool:AlignTop()
  100. local maxY = calcTop();
  101. local commands = {};
  102. for i , v in pairs(app.editor.SelectedNodes) do
  103. local node = v;
  104. local pos = node:getWorldPosition();
  105. pos.y = maxY - node:getContentSize().height * node:getScaleY() + node:getAnchorPoint().y * node:getContentSize().height * node:getScaleY();
  106. table.insert(commands, Commands:setWorldPosition(node, pos));
  107. end
  108. app.undoRedoManager:doCommand(Commands:batchCommand(commands));
  109. end
  110. function AlignTool:AlignMiddle()
  111. local minY = calcBottom();
  112. local maxY = calcTop();
  113. local height = maxY - minY;
  114. local middle = minY + height / 2;
  115. local commands = {};
  116. for i , v in pairs(app.editor.SelectedNodes) do
  117. local node = v;
  118. local offsetY = middle - (node:getWorldPositionY() - node:getAnchorPoint().y * node:getContentSize().height * node:getScaleY() + node:getContentSize().height * node:getScaleY() / 2);
  119. local pos = node:getWorldPosition();
  120. pos.y = node:getWorldPositionY() + offsetY;
  121. table.insert(commands, Commands:setWorldPosition(node, pos));
  122. end
  123. app.undoRedoManager:doCommand(Commands:batchCommand(commands));
  124. end
  125. function AlignTool:AlignBottom()
  126. local minY = calcBottom();
  127. local commands = {};
  128. for i , v in pairs(app.editor.SelectedNodes) do
  129. local node = v;
  130. local offsetY = minY - (node:getWorldPositionY() - node:getAnchorPoint().y * node:getContentSize().height * node:getScaleY());
  131. local pos = node:getWorldPosition();
  132. pos.y = node:getWorldPositionY() + offsetY;
  133. table.insert(commands, Commands:setWorldPosition(node, pos));
  134. end
  135. app.undoRedoManager:doCommand(Commands:batchCommand(commands));
  136. end
  137. function AlignTool:SameWidth()
  138. local lastNode = getLastSelectedNode();
  139. if not lastNode then
  140. return;
  141. end
  142. local width = lastNode:getSize().width;
  143. local commands = {};
  144. for i , v in pairs(app.editor.SelectedNodes) do
  145. local size = v:getSize();
  146. size.width = width;
  147. table.insert(commands, Commands:sizeNode(v, size));
  148. end
  149. app.undoRedoManager:doCommand(Commands:batchCommand(commands));
  150. end
  151. function AlignTool:SameHeight()
  152. local lastNode = getLastSelectedNode();
  153. if not lastNode then
  154. return;
  155. end
  156. local height = lastNode:getSize().height;
  157. local commands = {};
  158. for i , v in pairs(app.editor.SelectedNodes) do
  159. local size = v:getSize();
  160. size.height = height;
  161. table.insert(commands, Commands:sizeNode(v, size));
  162. end
  163. app.undoRedoManager:doCommand(Commands:batchCommand(commands));
  164. end
  165. function AlignTool:SameSize()
  166. local lastNode = getLastSelectedNode();
  167. if not lastNode then
  168. return;
  169. end
  170. local commands = {};
  171. for i , v in pairs(app.editor.SelectedNodes) do
  172. table.insert(commands, Commands:sizeNode(v, lastNode:getSize()));
  173. end
  174. app.undoRedoManager:doCommand(Commands:batchCommand(commands));
  175. end
  176. function AlignTool:SameHorzSpace()
  177. local count = table.nums(app.editor.SelectedNodes);
  178. if count < 3 then
  179. return;
  180. end
  181. local allSize = calcAllSize();
  182. local left = calcLeft();
  183. local right = calcRight();
  184. -- 计算间距
  185. local space = 0;
  186. if allSize.width > right - left then
  187. space = 0;
  188. else
  189. space = ((right - left) - allSize.width) / (count - 1);
  190. end
  191. -- 计算顺序
  192. local nodes = {};
  193. for i , v in pairs(app.editor.SelectedNodes) do
  194. table.insert(nodes, v);
  195. end
  196. -- 从左到右排序
  197. table.sort(nodes , function(a,b)return a:getWorldPositionX() - a:getAnchorPoint().x * a:getContentSize().width * a:getScaleX() < b:getWorldPositionX() - b:getAnchorPoint().x * b:getContentSize().width * a:getScaleX();end);
  198. local commands = {};
  199. local posX = left;
  200. for i , node in ipairs(nodes) do
  201. local pos = node:getWorldPosition();
  202. pos.x = posX + node:getAnchorPoint().x * node:getContentSize().width * node:getScaleX();
  203. posX = posX + space + node:getSize().width;
  204. table.insert(commands, Commands:setWorldPosition(node, pos));
  205. end
  206. app.undoRedoManager:doCommand(Commands:batchCommand(commands));
  207. end
  208. function AlignTool:AddHorzSpace()
  209. end
  210. function AlignTool:SubHorzSpace()
  211. end
  212. function AlignTool:NoHorzSpace()
  213. end
  214. function AlignTool:SameVertSpace()
  215. local count = table.nums(app.editor.SelectedNodes);
  216. if count < 3 then
  217. return;
  218. end
  219. local allSize = calcAllSize();
  220. local bottom = calcBottom();
  221. local top = calcTop();
  222. -- 计算间距
  223. local space = 0;
  224. if allSize.height > top - bottom then
  225. space = 0;
  226. else
  227. space = ((top - bottom) - allSize.height) / (count - 1);
  228. end
  229. -- 计算顺序
  230. local nodes = {};
  231. for i , v in pairs(app.editor.SelectedNodes) do
  232. table.insert(nodes, v);
  233. end
  234. -- 从左到右排序
  235. table.sort(nodes , function(a,b)return a:getWorldPositionY() - a:getAnchorPoint().y * a:getContentSize().height * a:getScaleY() < b:getWorldPositionY() - b:getAnchorPoint().y * b:getContentSize().height * a:getScaleY();end);
  236. local commands = {};
  237. local posY = bottom;
  238. for i , node in ipairs(nodes) do
  239. local pos = node:getWorldPosition();
  240. pos.y = posY + node:getAnchorPoint().y * node:getContentSize().height * node:getScaleY();
  241. posY = posY + space + node:getSize().height;
  242. table.insert(commands, Commands:setWorldPosition(node, pos));
  243. end
  244. app.undoRedoManager:doCommand(Commands:batchCommand(commands));
  245. end
  246. function AlignTool:AddVertSpace()
  247. end
  248. function AlignTool:SubVertSpace()
  249. end
  250. function AlignTool:NoVertSpace()
  251. end
  252. function AlignTool:HorzCenter()
  253. local left = calcLeft();
  254. local right = calcRight();
  255. local size = app.setting.ResourceSize;
  256. local minX = (size.width - (right - left)) / 2;
  257. local commands = {};
  258. for i , v in pairs(app.editor.SelectedNodes) do
  259. local node = v;
  260. local pos = node:getWorldPosition();
  261. pos.x = minX + ((node:getWorldPositionX() - node:getAnchorPoint().x * node:getContentSize().width * node:getScaleX()) - left);
  262. table.insert(commands, Commands:setWorldPosition(node, pos));
  263. end
  264. app.undoRedoManager:doCommand(Commands:batchCommand(commands));
  265. end
  266. function AlignTool:VertCenter()
  267. local bottom = calcBottom();
  268. local top = calcTop();
  269. local size = app.setting.ResourceSize;
  270. local minY = (size.height - (top - bottom)) / 2;
  271. local commands = {};
  272. for i , v in pairs(app.editor.SelectedNodes) do
  273. local node = v;
  274. local pos = node:getWorldPosition();
  275. pos.y = minY + ((node:getWorldPositionY() - node:getAnchorPoint().y * node:getContentSize().height * node:getScaleY()) - bottom);
  276. table.insert(commands, Commands:setWorldPosition(node, pos));
  277. end
  278. app.undoRedoManager:doCommand(Commands:batchCommand(commands));
  279. end
  280. function AlignTool:MoveUp()
  281. local lastNode = getLastSelectedNode();
  282. if not lastNode then
  283. return;
  284. end
  285. local children = lastNode:getParent():getChildren();
  286. local node;
  287. for i , v in ipairs(children) do
  288. if v == lastNode then
  289. node = children[i - 1];
  290. break;
  291. end
  292. end
  293. if node and node ~= lastNode then
  294. app.undoRedoManager:doCommand(Commands:swapNode(lastNode, node))
  295. end
  296. end
  297. function AlignTool:MoveDown()
  298. local lastNode = getLastSelectedNode();
  299. if not lastNode then
  300. return;
  301. end
  302. local children = lastNode:getParent():getChildren();
  303. local node;
  304. for i , v in ipairs(children) do
  305. if v == lastNode then
  306. node = children[i + 1];
  307. break;
  308. end
  309. end
  310. if node and node ~= lastNode then
  311. app.undoRedoManager:doCommand(Commands:swapNode(lastNode, node))
  312. end
  313. end
  314. function AlignTool:MoveTop()
  315. local lastNode = getLastSelectedNode();
  316. if not lastNode then
  317. return;
  318. end
  319. local children = lastNode:getParent():getChildren();
  320. local node = children[1];
  321. if node and node ~= lastNode then
  322. local commands = {};
  323. local found = false;
  324. for i = #children , 1 , -1 do
  325. local child = children[i];
  326. if found then
  327. print("交换一个");
  328. table.insert(commands, Commands:swapNode(child, lastNode));
  329. end
  330. if child == lastNode then
  331. found = true;
  332. end
  333. end
  334. app.undoRedoManager:doCommand(Commands:batchCommand(commands));
  335. end
  336. end
  337. function AlignTool:MoveBottom()
  338. local lastNode = getLastSelectedNode();
  339. if not lastNode then
  340. return;
  341. end
  342. local children = lastNode:getParent():getChildren();
  343. local node = children[#children];
  344. if node and node ~= lastNode then
  345. local commands = {};
  346. local found = false;
  347. for i = 1 , #children do
  348. local child = children[i];
  349. if found then
  350. table.insert(commands, Commands:swapNode(child, lastNode));
  351. end
  352. if child == lastNode then
  353. found = true;
  354. end
  355. end
  356. app.undoRedoManager:doCommand(Commands:batchCommand(commands));
  357. end
  358. end
  359. return AlignTool;