TreeLayout.cs 7.2 KB

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