TreeLayout.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. namespace Modules.BehaviorTreeModule
  2. {
  3. public class TreeLayout
  4. {
  5. private readonly TreeViewModel treeViewModel;
  6. private const double XGap = 20;
  7. private const double YGap = 10;
  8. private double rootOrigX;
  9. private double rootOrigY;
  10. private double rootOffsetX;
  11. private double rootOffsetY;
  12. public TreeLayout(TreeViewModel treeViewModel)
  13. {
  14. this.treeViewModel = treeViewModel;
  15. }
  16. private TreeNodeViewModel LeftMostOffspring(
  17. TreeNodeViewModel treeNodeViewModel, int currentLevel, int searchLevel)
  18. {
  19. if (currentLevel == searchLevel)
  20. {
  21. return treeNodeViewModel;
  22. }
  23. for (int i = 0; i < treeNodeViewModel.Children.Count; ++i)
  24. {
  25. var child = this.treeViewModel.Get(treeNodeViewModel.Children[i]);
  26. child.AncestorModify = treeNodeViewModel.Modify + treeNodeViewModel.AncestorModify;
  27. var offspring = this.LeftMostOffspring(child, currentLevel + 1, searchLevel);
  28. if (offspring == null)
  29. {
  30. continue;
  31. }
  32. return offspring;
  33. }
  34. return null;
  35. }
  36. private TreeNodeViewModel RightMostOffspring(
  37. TreeNodeViewModel treeNodeViewModel, int currentLevel, int searchLevel)
  38. {
  39. if (currentLevel == searchLevel)
  40. {
  41. return treeNodeViewModel;
  42. }
  43. for (int i = treeNodeViewModel.Children.Count - 1; i >= 0; --i)
  44. {
  45. var child = this.treeViewModel.Get(treeNodeViewModel.Children[i]);
  46. child.AncestorModify = treeNodeViewModel.Modify + treeNodeViewModel.AncestorModify;
  47. var offspring = this.RightMostOffspring(child, currentLevel + 1, searchLevel);
  48. if (offspring == null)
  49. {
  50. continue;
  51. }
  52. return offspring;
  53. }
  54. return null;
  55. }
  56. private void AjustSubTreeGap(TreeNodeViewModel left, TreeNodeViewModel right)
  57. {
  58. double offset = 0;
  59. TreeNodeViewModel tLeft = left;
  60. TreeNodeViewModel tRight = right;
  61. left.AncestorModify = 0;
  62. right.AncestorModify = 0;
  63. for (int i = 0; tLeft != null && tRight != null; ++i)
  64. {
  65. double tGap = (tRight.Prelim + tRight.AncestorModify) -
  66. (tLeft.Prelim + tLeft.AncestorModify);
  67. if (XGap + TreeNodeViewModel.Width - tGap > offset)
  68. {
  69. offset = XGap + TreeNodeViewModel.Width - tGap;
  70. }
  71. tLeft = this.RightMostOffspring(left, 0, i + 1);
  72. tRight = this.LeftMostOffspring(right, 0, i + 1);
  73. }
  74. right.Modify += offset;
  75. right.Prelim += offset;
  76. }
  77. private void AjustTreeGap(TreeNodeViewModel treeNodeViewModel)
  78. {
  79. for (int i = 0; i < treeNodeViewModel.Children.Count - 1; ++i)
  80. {
  81. for (int j = i + 1; j < treeNodeViewModel.Children.Count; ++j)
  82. {
  83. var left = this.treeViewModel.Get(treeNodeViewModel.Children[i]);
  84. var right = this.treeViewModel.Get(treeNodeViewModel.Children[j]);
  85. this.AjustSubTreeGap(left, right);
  86. }
  87. }
  88. }
  89. private void CalculatePrelimAndModify(TreeNodeViewModel treeNodeViewModel)
  90. {
  91. foreach (int childId in treeNodeViewModel.Children)
  92. {
  93. TreeNodeViewModel child = this.treeViewModel.Get(childId);
  94. this.CalculatePrelimAndModify(child);
  95. }
  96. double prelim = 0;
  97. double modify = 0;
  98. if (treeNodeViewModel.IsLeaf)
  99. {
  100. if (treeNodeViewModel.LeftSibling == null)
  101. {
  102. // 如果没有左邻居,不需要设置modify
  103. prelim = 0;
  104. }
  105. else
  106. {
  107. prelim = treeNodeViewModel.LeftSibling.Prelim + TreeNodeViewModel.Width + XGap;
  108. }
  109. }
  110. else
  111. {
  112. // 调整子树间的间距
  113. this.AjustTreeGap(treeNodeViewModel);
  114. double childrenCenter = (treeNodeViewModel.FirstChild.Prelim +
  115. treeNodeViewModel.LastChild.Prelim) / 2;
  116. if (treeNodeViewModel.LeftSibling == null)
  117. {
  118. // 如果没有左邻居,不需要设置modify
  119. prelim = childrenCenter;
  120. }
  121. else
  122. {
  123. prelim = treeNodeViewModel.LeftSibling.Prelim + TreeNodeViewModel.Width + XGap;
  124. modify = prelim - childrenCenter;
  125. }
  126. }
  127. treeNodeViewModel.Prelim = prelim;
  128. treeNodeViewModel.Modify = modify;
  129. // Log.Debug("Id: " + treeNodeViewModel.Id + " Prelim: " + treeNodeViewModel.Prelim + " Modify: " +
  130. // treeNodeViewModel.Modify);
  131. }
  132. private void CalculateRelativeXAndY(
  133. TreeNodeViewModel treeNodeViewModel, int level, double totalModify)
  134. {
  135. foreach (int childId in treeNodeViewModel.Children)
  136. {
  137. TreeNodeViewModel child = this.treeViewModel.Get(childId);
  138. this.CalculateRelativeXAndY(child, level + 1, treeNodeViewModel.Modify + totalModify);
  139. }
  140. if (treeNodeViewModel.IsLeaf)
  141. {
  142. treeNodeViewModel.X = treeNodeViewModel.Prelim + totalModify;
  143. }
  144. else
  145. {
  146. treeNodeViewModel.X = (treeNodeViewModel.FirstChild.X +
  147. treeNodeViewModel.LastChild.X) / 2;
  148. }
  149. treeNodeViewModel.Y = level * (TreeNodeViewModel.Height + YGap);
  150. }
  151. private void FixXAndY(TreeNodeViewModel treeNode)
  152. {
  153. treeNode.X += this.rootOffsetX;
  154. treeNode.Y += this.rootOffsetY;
  155. foreach (var childId in treeNode.Children)
  156. {
  157. TreeNodeViewModel child = this.treeViewModel.Get(childId);
  158. this.FixXAndY(child);
  159. }
  160. }
  161. public void ExcuteLayout(TreeNodeViewModel root)
  162. {
  163. if (root == null)
  164. {
  165. return;
  166. }
  167. this.rootOrigX = root.X;
  168. this.rootOrigY = root.Y;
  169. this.CalculatePrelimAndModify(root);
  170. this.CalculateRelativeXAndY(root, 0, 0);
  171. this.rootOffsetX = this.rootOrigX - root.X;
  172. this.rootOffsetY = this.rootOrigY - root.Y;
  173. this.FixXAndY(root);
  174. }
  175. }
  176. }