1- /**
2- * 瞬间树形布局算法
3- * 瞬间:一次性直接移动所有节点到合适的位置
4- * 树形:此布局算法仅限于树形结构,在代码上游保证
5- */
6-
71import { Rectangle } from "../../../dataStruct/shape/Rectangle" ;
82import { Vector } from "../../../dataStruct/Vector" ;
93import { GraphMethods } from "../../../stage/stageManager/basicMethods/GraphMethods" ;
104import { StageEntityMoveManager } from "../../../stage/stageManager/concreteMethods/StageEntityMoveManager" ;
115import { StageManager } from "../../../stage/stageManager/StageManager" ;
126import { ConnectableEntity } from "../../../stage/stageObject/abstract/ConnectableEntity" ;
7+
8+ /**
9+ * 瞬间树形布局算法
10+ * 瞬间:一次性直接移动所有节点到合适的位置
11+ * 树形:此布局算法仅限于树形结构,在代码上游保证
12+ */
1313export namespace AutoLayoutFastTree {
1414 /**
15- * 树形节点的根节点
16- * @param rootNode
15+ * 向下树形布局
16+ * @param rootNode 树形节点的根节点
1717 */
1818 export function autoLayoutFastTreeModeDown ( rootNode : ConnectableEntity ) {
1919 const dfs = ( node : ConnectableEntity ) => {
@@ -43,6 +43,7 @@ export namespace AutoLayoutFastTree {
4343 } ;
4444 dfs ( rootNode ) ;
4545 }
46+
4647 /**
4748 * 获取当前树的外接矩形,注意不要有环,有环就废了
4849 * @param node
@@ -105,10 +106,14 @@ export namespace AutoLayoutFastTree {
105106 * @param rootNode
106107 */
107108 export function autoLayoutFastTreeModeRight ( rootNode : ConnectableEntity ) {
109+ // 树形结构的根节点 左上角位置固定不动
108110 const initLocation = rootNode . collisionBox . getRectangle ( ) . leftTop . clone ( ) ;
109111
110112 const dfs = ( node : ConnectableEntity ) => {
111- const childList = GraphMethods . nodeChildrenArray ( node ) ;
113+ // 按照从上到下的顺序排序
114+ const childList = GraphMethods . nodeChildrenArray ( node ) . sort (
115+ ( a , b ) => a . collisionBox . getRectangle ( ) . top - b . collisionBox . getRectangle ( ) . top ,
116+ ) ;
112117 for ( const child of childList ) {
113118 dfs ( child ) ; // 递归口
114119 }
@@ -126,6 +131,9 @@ export namespace AutoLayoutFastTree {
126131 rootNode . isSelected = true ;
127132 StageEntityMoveManager . moveConnectableEntitiesWithChildren ( delta ) ;
128133 }
134+
135+ // ======================= 反转树的位置系列 ====================
136+
129137 export function treeReverseX ( selectedRootEntity : ConnectableEntity ) {
130138 treeReverse ( selectedRootEntity , "X" ) ;
131139 }
0 commit comments