BehaviorTreeLayout.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace BehaviorTree
  6. {
  7. class BehaviorTreeLayout
  8. {
  9. private static double xGap = 20;
  10. private static double yGap = 10;
  11. private TreeNodeViewModel root = null;
  12. public BehaviorTreeLayout(TreeNodeViewModel root)
  13. {
  14. this.root = root;
  15. }
  16. public static double XGap
  17. {
  18. get
  19. {
  20. return BehaviorTreeLayout.xGap;
  21. }
  22. set
  23. {
  24. BehaviorTreeLayout.xGap = value;
  25. }
  26. }
  27. public static double YGap
  28. {
  29. get
  30. {
  31. return BehaviorTreeLayout.yGap;
  32. }
  33. set
  34. {
  35. BehaviorTreeLayout.yGap = value;
  36. }
  37. }
  38. public void CountPrelim(TreeNodeViewModel treeNode)
  39. {
  40. if (treeNode.Index == 0)
  41. {
  42. treeNode.Prelim = treeNode.Index * (treeNode.Width + BehaviorTreeLayout.XGap);
  43. return;
  44. }
  45. double childrenCenter = 0;
  46. if (treeNode.Children.Count > 0)
  47. {
  48. int maxIndex = treeNode.Children.Count - 1;
  49. childrenCenter = (treeNode.Children[0].Prelim + treeNode.Children[maxIndex].Prelim) / 2;
  50. }
  51. else
  52. {
  53. childrenCenter = 0;
  54. }
  55. treeNode.Prelim = childrenCenter;
  56. }
  57. public void CountModify(TreeNodeViewModel treeNode, double prelim)
  58. {
  59. double childrenCenter = 0;
  60. if (treeNode.Children.Count > 0)
  61. {
  62. int maxIndex = treeNode.Children.Count - 1;
  63. childrenCenter = (treeNode.Children[0].Prelim + treeNode.Children[maxIndex].Prelim) / 2;
  64. }
  65. treeNode.Modify = prelim - childrenCenter;
  66. }
  67. public void CountPrelimAndModify(TreeNodeViewModel treeNode)
  68. {
  69. CountPrelim(treeNode);
  70. CountModify(treeNode, treeNode.Prelim);
  71. foreach (var node in treeNode.Children)
  72. {
  73. CountPrelimAndModify(node);
  74. }
  75. }
  76. public void AjustTwoSubTreeGap(TreeNodeViewModel left, TreeNodeViewModel right)
  77. {
  78. if (left.IsLeaf || right.IsLeaf)
  79. {
  80. return;
  81. }
  82. double offset = 0;
  83. TreeNodeViewModel tLeft = left;
  84. TreeNodeViewModel tRight = right;
  85. double leftTreeModify = 0;
  86. double rightTreeModify = 0;
  87. for (int i = 0; ; ++i)
  88. {
  89. if (tLeft.IsLeaf || tRight.IsLeaf)
  90. {
  91. right.Modify += offset;
  92. return;
  93. }
  94. leftTreeModify += tLeft.Modify;
  95. rightTreeModify += tRight.Modify;
  96. tLeft = tLeft.RightMostChild;
  97. tRight = tRight.LeftMostChild;
  98. double tGap = (tRight.Prelim + rightTreeModify) - (tLeft.Prelim + leftTreeModify);
  99. if (tGap - BehaviorTreeLayout.XGap > offset)
  100. {
  101. offset = tGap - BehaviorTreeLayout.XGap;
  102. }
  103. }
  104. }
  105. public void AjustTreeGap(TreeNodeViewModel treeNode)
  106. {
  107. for (int i = 0; i < treeNode.Children.Count - 1; ++i)
  108. {
  109. TreeNodeViewModel left = treeNode.Children[i];
  110. TreeNodeViewModel right = treeNode.Children[i + 1];
  111. AjustTwoSubTreeGap(left, right);
  112. }
  113. }
  114. public void ApplyXY(TreeNodeViewModel treeNode)
  115. {
  116. treeNode.X = treeNode.Prelim;
  117. double realModify = treeNode.Prelim - treeNode.Modify;
  118. foreach (var node in treeNode.Children)
  119. {
  120. }
  121. }
  122. public void ExcuteLayout()
  123. {
  124. CountPrelimAndModify(root);
  125. AjustTreeGap(root);
  126. ApplyXY(root);
  127. }
  128. }
  129. }