|
- require("EditorGlobalFunction")
-
- local AlignTool = class("AlignTool")
- function AlignTool:ctor()
- end
-
- -- 计算最左边
- local function calcLeft()
- local minX = 65536;
- for i , v in pairs(app.editor.SelectedNodes) do
- local node = v;
- minX = math.min(minX , node:getWorldPositionX() - node:getAnchorPoint().x * node:getContentSize().width * node:getScaleX());
- end
- return minX;
- end
-
- -- 计算最右边
- local function calcRight()
- local maxX = 0;
- for i , v in pairs(app.editor.SelectedNodes) do
- local node = v;
- maxX = math.max(maxX , node:getWorldPositionX() + (1 - node:getAnchorPoint().x) * node:getContentSize().width * node:getScaleX());
- end
- return maxX;
- end
-
- -- 计算最上边
- local function calcTop()
- local maxY = 0;
- for i , v in pairs(app.editor.SelectedNodes) do
- local node = v;
- maxY = math.max(maxY , node:getWorldPositionY() + (1 - node:getAnchorPoint().y) * node:getContentSize().height * node:getScaleY());
- end
- return maxY;
- end
-
- -- 计算最下边
- local function calcBottom()
- local minY = 65536;
- for i , v in pairs(app.editor.SelectedNodes) do
- local node = v;
- minY = math.min(minY , node:getWorldPositionY() - node:getAnchorPoint().y * node:getContentSize().height * node:getScaleY());
- end
- return minY;
- end
-
- -- 获得所有选中元素的总宽高
- local function calcAllSize()
- local size = cc.size(0,0);
- for i , v in pairs(app.editor.SelectedNodes) do
- local node = v;
- local s = node:getSize();
- size.width = size.width + s.width;
- size.height = size.height + s.height;
- end
- return size;
- end
-
- -- 获得最后一个选中的节点
- local function getLastSelectedNode()
- local node;
- for i , v in pairs(app.editor.SelectedNodes) do
- if node == nil or v.SelectedIndex > node.SelectedIndex then
- node = v;
- end
- end
- return node;
- end
-
- function AlignTool:AlignLeft()
- local minX = calcLeft();
- local commands = {};
- for i , v in pairs(app.editor.SelectedNodes) do
- local node = v;
- local pos = node:getWorldPosition();
- pos.x = minX + node:getAnchorPoint().x * node:getContentSize().width * node:getScaleX();
- table.insert(commands, Commands:setWorldPosition(node, pos));
- end
- app.undoRedoManager:doCommand(Commands:batchCommand(commands));
- end
-
- function AlignTool:AlignCenter()
- local minX = calcLeft();
- local maxX = calcRight();
- local width = maxX - minX;
- local center = minX + width / 2;
-
- local commands = {};
- for i , v in pairs(app.editor.SelectedNodes) do
- local node = v;
- local offsetX = center - (node:getWorldPositionX() - node:getAnchorPoint().x * node:getContentSize().width * node:getScaleX() + node:getContentSize().width * node:getScaleX() / 2);
- local pos = node:getWorldPosition();
- pos.x = node:getWorldPositionX() + offsetX;
- table.insert(commands, Commands:setWorldPosition(node, pos));
- end
- app.undoRedoManager:doCommand(Commands:batchCommand(commands));
- end
-
- function AlignTool:AlignRight()
- local maxX = calcRight();
- local commands = {};
- for i , v in pairs(app.editor.SelectedNodes) do
- local node = v;
- local pos = node:getWorldPosition();
- pos.x = maxX - node:getContentSize().width * node:getScaleX() + node:getAnchorPoint().x * node:getContentSize().width * node:getScaleX();
- table.insert(commands, Commands:setWorldPosition(node, pos));
- end
- app.undoRedoManager:doCommand(Commands:batchCommand(commands));
- end
-
- function AlignTool:AlignTop()
- local maxY = calcTop();
- local commands = {};
- for i , v in pairs(app.editor.SelectedNodes) do
- local node = v;
- local pos = node:getWorldPosition();
- pos.y = maxY - node:getContentSize().height * node:getScaleY() + node:getAnchorPoint().y * node:getContentSize().height * node:getScaleY();
- table.insert(commands, Commands:setWorldPosition(node, pos));
- end
- app.undoRedoManager:doCommand(Commands:batchCommand(commands));
- end
-
- function AlignTool:AlignMiddle()
- local minY = calcBottom();
- local maxY = calcTop();
- local height = maxY - minY;
- local middle = minY + height / 2;
-
- local commands = {};
- for i , v in pairs(app.editor.SelectedNodes) do
- local node = v;
- local offsetY = middle - (node:getWorldPositionY() - node:getAnchorPoint().y * node:getContentSize().height * node:getScaleY() + node:getContentSize().height * node:getScaleY() / 2);
- local pos = node:getWorldPosition();
- pos.y = node:getWorldPositionY() + offsetY;
- table.insert(commands, Commands:setWorldPosition(node, pos));
- end
- app.undoRedoManager:doCommand(Commands:batchCommand(commands));
- end
-
- function AlignTool:AlignBottom()
- local minY = calcBottom();
- local commands = {};
- for i , v in pairs(app.editor.SelectedNodes) do
- local node = v;
- local offsetY = minY - (node:getWorldPositionY() - node:getAnchorPoint().y * node:getContentSize().height * node:getScaleY());
- local pos = node:getWorldPosition();
- pos.y = node:getWorldPositionY() + offsetY;
- table.insert(commands, Commands:setWorldPosition(node, pos));
- end
- app.undoRedoManager:doCommand(Commands:batchCommand(commands));
- end
-
- function AlignTool:SameWidth()
- local lastNode = getLastSelectedNode();
- if not lastNode then
- return;
- end
- local width = lastNode:getSize().width;
- local commands = {};
- for i , v in pairs(app.editor.SelectedNodes) do
- local size = v:getSize();
- size.width = width;
- table.insert(commands, Commands:sizeNode(v, size));
- end
- app.undoRedoManager:doCommand(Commands:batchCommand(commands));
- end
-
- function AlignTool:SameHeight()
- local lastNode = getLastSelectedNode();
- if not lastNode then
- return;
- end
- local height = lastNode:getSize().height;
- local commands = {};
- for i , v in pairs(app.editor.SelectedNodes) do
- local size = v:getSize();
- size.height = height;
- table.insert(commands, Commands:sizeNode(v, size));
- end
- app.undoRedoManager:doCommand(Commands:batchCommand(commands));
- end
-
- function AlignTool:SameSize()
- local lastNode = getLastSelectedNode();
- if not lastNode then
- return;
- end
- local commands = {};
- for i , v in pairs(app.editor.SelectedNodes) do
- table.insert(commands, Commands:sizeNode(v, lastNode:getSize()));
- end
- app.undoRedoManager:doCommand(Commands:batchCommand(commands));
- end
-
- function AlignTool:SameHorzSpace()
- local count = table.nums(app.editor.SelectedNodes);
- if count < 3 then
- return;
- end
- local allSize = calcAllSize();
- local left = calcLeft();
- local right = calcRight();
- -- 计算间距
- local space = 0;
- if allSize.width > right - left then
- space = 0;
- else
- space = ((right - left) - allSize.width) / (count - 1);
- end
- -- 计算顺序
- local nodes = {};
- for i , v in pairs(app.editor.SelectedNodes) do
- table.insert(nodes, v);
- end
- -- 从左到右排序
- 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);
-
- local commands = {};
- local posX = left;
- for i , node in ipairs(nodes) do
- local pos = node:getWorldPosition();
- pos.x = posX + node:getAnchorPoint().x * node:getContentSize().width * node:getScaleX();
-
- posX = posX + space + node:getSize().width;
- table.insert(commands, Commands:setWorldPosition(node, pos));
- end
- app.undoRedoManager:doCommand(Commands:batchCommand(commands));
- end
-
- function AlignTool:AddHorzSpace()
- end
-
- function AlignTool:SubHorzSpace()
- end
-
- function AlignTool:NoHorzSpace()
- end
-
- function AlignTool:SameVertSpace()
- local count = table.nums(app.editor.SelectedNodes);
- if count < 3 then
- return;
- end
- local allSize = calcAllSize();
- local bottom = calcBottom();
- local top = calcTop();
- -- 计算间距
- local space = 0;
- if allSize.height > top - bottom then
- space = 0;
- else
- space = ((top - bottom) - allSize.height) / (count - 1);
- end
- -- 计算顺序
- local nodes = {};
- for i , v in pairs(app.editor.SelectedNodes) do
- table.insert(nodes, v);
- end
- -- 从左到右排序
- 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);
-
- local commands = {};
- local posY = bottom;
- for i , node in ipairs(nodes) do
- local pos = node:getWorldPosition();
- pos.y = posY + node:getAnchorPoint().y * node:getContentSize().height * node:getScaleY();
-
- posY = posY + space + node:getSize().height;
- table.insert(commands, Commands:setWorldPosition(node, pos));
- end
- app.undoRedoManager:doCommand(Commands:batchCommand(commands));
- end
-
- function AlignTool:AddVertSpace()
- end
-
- function AlignTool:SubVertSpace()
- end
-
- function AlignTool:NoVertSpace()
- end
-
- function AlignTool:HorzCenter()
- local left = calcLeft();
- local right = calcRight();
- local size = app.setting.ResourceSize;
- local minX = (size.width - (right - left)) / 2;
-
- local commands = {};
- for i , v in pairs(app.editor.SelectedNodes) do
- local node = v;
- local pos = node:getWorldPosition();
- pos.x = minX + ((node:getWorldPositionX() - node:getAnchorPoint().x * node:getContentSize().width * node:getScaleX()) - left);
- table.insert(commands, Commands:setWorldPosition(node, pos));
- end
- app.undoRedoManager:doCommand(Commands:batchCommand(commands));
- end
-
- function AlignTool:VertCenter()
- local bottom = calcBottom();
- local top = calcTop();
- local size = app.setting.ResourceSize;
- local minY = (size.height - (top - bottom)) / 2;
-
- local commands = {};
- for i , v in pairs(app.editor.SelectedNodes) do
- local node = v;
- local pos = node:getWorldPosition();
- pos.y = minY + ((node:getWorldPositionY() - node:getAnchorPoint().y * node:getContentSize().height * node:getScaleY()) - bottom);
- table.insert(commands, Commands:setWorldPosition(node, pos));
- end
- app.undoRedoManager:doCommand(Commands:batchCommand(commands));
- end
-
- function AlignTool:MoveUp()
- local lastNode = getLastSelectedNode();
- if not lastNode then
- return;
- end
- local children = lastNode:getParent():getChildren();
- local node;
- for i , v in ipairs(children) do
- if v == lastNode then
- node = children[i - 1];
- break;
- end
- end
- if node and node ~= lastNode then
- app.undoRedoManager:doCommand(Commands:swapNode(lastNode, node))
- end
- end
-
- function AlignTool:MoveDown()
- local lastNode = getLastSelectedNode();
- if not lastNode then
- return;
- end
- local children = lastNode:getParent():getChildren();
- local node;
- for i , v in ipairs(children) do
- if v == lastNode then
- node = children[i + 1];
- break;
- end
- end
- if node and node ~= lastNode then
- app.undoRedoManager:doCommand(Commands:swapNode(lastNode, node))
- end
- end
-
- function AlignTool:MoveTop()
- local lastNode = getLastSelectedNode();
- if not lastNode then
- return;
- end
- local children = lastNode:getParent():getChildren();
- local node = children[1];
- if node and node ~= lastNode then
- local commands = {};
- local found = false;
- for i = #children , 1 , -1 do
- local child = children[i];
- if found then
- print("交换一个");
- table.insert(commands, Commands:swapNode(child, lastNode));
- end
- if child == lastNode then
- found = true;
- end
- end
- app.undoRedoManager:doCommand(Commands:batchCommand(commands));
- end
- end
-
- function AlignTool:MoveBottom()
- local lastNode = getLastSelectedNode();
- if not lastNode then
- return;
- end
- local children = lastNode:getParent():getChildren();
- local node = children[#children];
- if node and node ~= lastNode then
- local commands = {};
- local found = false;
- for i = 1 , #children do
- local child = children[i];
- if found then
- table.insert(commands, Commands:swapNode(child, lastNode));
- end
- if child == lastNode then
- found = true;
- end
- end
- app.undoRedoManager:doCommand(Commands:batchCommand(commands));
- end
- end
-
- return AlignTool;
|