BehaviorTreeLayout.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using NLog;
  2. namespace BehaviorTree
  3. {
  4. public class BehaviorTreeLayout
  5. {
  6. private const double XGap = 20;
  7. private const double YGap = 10;
  8. private static readonly Logger logger = LogManager.GetCurrentClassLogger();
  9. private static void CountPrelimAndModify(TreeNodeViewModel treeNode)
  10. {
  11. foreach (TreeNodeViewModel node in treeNode.Children)
  12. {
  13. CountPrelimAndModify(node);
  14. }
  15. double prelim = 0;
  16. double modify = 0;
  17. double childrenCenter = 0;
  18. if (!treeNode.IsLeaf)
  19. {
  20. childrenCenter = (treeNode.LeftMostChild.Prelim + treeNode.RightMostChild.Prelim) / 2;
  21. }
  22. if (treeNode.Index == 0)
  23. {
  24. // 如果没有左邻居,不需要设置modify
  25. prelim = childrenCenter;
  26. }
  27. else
  28. {
  29. prelim = treeNode.Index * (TreeNodeViewModel.Width + XGap) + treeNode.Parent.LeftMostChild.Prelim;
  30. modify = prelim - childrenCenter;
  31. }
  32. treeNode.Prelim = prelim;
  33. treeNode.Modify = modify;
  34. logger.Debug("Num: " + treeNode.Num + " Prelim: " + treeNode.Prelim + " Modify: " + treeNode.Modify);
  35. }
  36. private static void AjustTwoSubTreeGap(TreeNodeViewModel left, TreeNodeViewModel right)
  37. {
  38. double offset = 0;
  39. TreeNodeViewModel tLeft = left;
  40. TreeNodeViewModel tRight = right;
  41. double leftTreeModify = 0;
  42. double rightTreeModify = 0;
  43. for (int i = 0; ; ++i)
  44. {
  45. double tGap = (tRight.Prelim + rightTreeModify) - (tLeft.Prelim + leftTreeModify);
  46. if (XGap + TreeNodeViewModel.Width - tGap > offset)
  47. {
  48. offset = XGap + TreeNodeViewModel.Width - tGap;
  49. }
  50. if (tLeft.IsLeaf || tRight.IsLeaf)
  51. {
  52. break;
  53. }
  54. leftTreeModify += tLeft.Modify;
  55. rightTreeModify += tRight.Modify;
  56. tLeft = tLeft.RightMostChild;
  57. tRight = tRight.LeftMostChild;
  58. }
  59. right.Modify += offset;
  60. right.Prelim += offset;
  61. }
  62. private static void AjustTreeGap(TreeNodeViewModel treeNode)
  63. {
  64. if (treeNode.IsLeaf)
  65. {
  66. return;
  67. }
  68. foreach (var child in treeNode.Children)
  69. {
  70. AjustTreeGap(child);
  71. }
  72. for (int i = 0; i < treeNode.Children.Count - 1; ++i)
  73. {
  74. var left = treeNode.Children[i];
  75. var right = treeNode.Children[i + 1];
  76. AjustTwoSubTreeGap(left, right);
  77. }
  78. }
  79. private static void ApplyXY(TreeNodeViewModel treeNode, int level, double totalModify)
  80. {
  81. foreach (TreeNodeViewModel node in treeNode.Children)
  82. {
  83. ApplyXY(node, level + 1, treeNode.Modify + totalModify);
  84. }
  85. if (treeNode.IsLeaf)
  86. {
  87. treeNode.X = treeNode.Prelim + totalModify;
  88. }
  89. else
  90. {
  91. treeNode.X = (treeNode.LeftMostChild.X + treeNode.RightMostChild.X) / 2;
  92. }
  93. treeNode.Y = level * (TreeNodeViewModel.Height + YGap);
  94. }
  95. public static void ExcuteLayout(TreeNodeViewModel root)
  96. {
  97. if (root == null)
  98. {
  99. return;
  100. }
  101. CountPrelimAndModify(root);
  102. AjustTreeGap(root);
  103. ApplyXY(root, 0, 0);
  104. }
  105. }
  106. }