TreeLayout.cs 5.5 KB

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