BehaviorTreeViewModel.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel.Composition;
  4. namespace Tree
  5. {
  6. [Export(contractType: typeof (BehaviorTreeViewModel)),
  7. PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
  8. internal class BehaviorTreeViewModel
  9. {
  10. private readonly ObservableCollection<TreeNodeViewModel> treeNodes =
  11. new ObservableCollection<TreeNodeViewModel>();
  12. public ObservableCollection<TreeNodeViewModel> TreeNodes
  13. {
  14. get
  15. {
  16. return this.treeNodes;
  17. }
  18. }
  19. private TreeNodeViewModel Root
  20. {
  21. get
  22. {
  23. return this.treeNodes.Count == 0? null : this.treeNodes[0];
  24. }
  25. }
  26. public void Add(TreeNode treeNode, TreeNodeViewModel parent)
  27. {
  28. // 如果父节点是折叠的,需要先展开父节点
  29. if (parent != null && parent.IsFolder)
  30. {
  31. UnFold(parent);
  32. }
  33. var treeNodeViewModel = new TreeNodeViewModel(treeNode, parent);
  34. this.treeNodes.Add(treeNodeViewModel);
  35. if (parent != null)
  36. {
  37. parent.Children.Add(treeNodeViewModel);
  38. }
  39. BehaviorTreeLayout.ExcuteLayout(this.Root);
  40. }
  41. private void RecursionRemove(TreeNodeViewModel treeNodeViewModel)
  42. {
  43. for (int i = 0; i < treeNodeViewModel.Children.Count; ++i)
  44. {
  45. this.RecursionRemove(treeNodeViewModel.Children[i]);
  46. }
  47. this.treeNodes.Remove(treeNodeViewModel);
  48. }
  49. public void Remove(TreeNodeViewModel treeNodeViewModel)
  50. {
  51. this.RecursionRemove(treeNodeViewModel);
  52. treeNodeViewModel.Parent.Children.Remove(treeNodeViewModel);
  53. BehaviorTreeLayout.ExcuteLayout(this.Root);
  54. }
  55. private void RecursionMove(TreeNodeViewModel treeNodeViewModel, double offsetX, double offsetY)
  56. {
  57. treeNodeViewModel.X += offsetX;
  58. treeNodeViewModel.Y += offsetY;
  59. foreach (var node in treeNodeViewModel.Children)
  60. {
  61. this.RecursionMove(node, offsetX, offsetY);
  62. }
  63. }
  64. public void MoveToPosition(double offsetX, double offsetY)
  65. {
  66. this.RecursionMove(this.Root, offsetX, offsetY);
  67. }
  68. public void MoveToNode(TreeNodeViewModel from, TreeNodeViewModel to)
  69. {
  70. if (from.IsFolder)
  71. {
  72. this.UnFold(from);
  73. }
  74. if (to.IsFolder)
  75. {
  76. this.UnFold(to);
  77. }
  78. // from节点不能是to节点的父级节点
  79. TreeNodeViewModel tmpNode = to;
  80. while (tmpNode != null)
  81. {
  82. if (tmpNode.IsRoot)
  83. {
  84. break;
  85. }
  86. if (tmpNode.Num == from.Num)
  87. {
  88. return;
  89. }
  90. tmpNode = tmpNode.Parent;
  91. }
  92. from.Parent.Children.Remove(from);
  93. to.Children.Add(from);
  94. from.Parent = to;
  95. BehaviorTreeLayout.ExcuteLayout(this.Root);
  96. }
  97. /// <summary>
  98. /// 折叠节点
  99. /// </summary>
  100. /// <param name="treeNodeViewModel"></param>
  101. public void Fold(TreeNodeViewModel treeNodeViewModel)
  102. {
  103. foreach (var node in treeNodeViewModel.Children)
  104. {
  105. this.RecursionRemove(node);
  106. }
  107. treeNodeViewModel.IsFolder = true;
  108. BehaviorTreeLayout.ExcuteLayout(this.Root);
  109. }
  110. /// <summary>
  111. /// 展开节点
  112. /// </summary>
  113. /// <param name="unFoldNode"></param>
  114. public void UnFold(TreeNodeViewModel unFoldNode)
  115. {
  116. foreach (var tn in unFoldNode.Children)
  117. {
  118. this.RecursionAdd(tn);
  119. }
  120. unFoldNode.IsFolder = false;
  121. BehaviorTreeLayout.ExcuteLayout(this.Root);
  122. }
  123. private void RecursionAdd(TreeNodeViewModel treeNodeViewModel)
  124. {
  125. if (!this.treeNodes.Contains(treeNodeViewModel))
  126. {
  127. this.treeNodes.Add(treeNodeViewModel);
  128. }
  129. ObservableCollection<TreeNodeViewModel> children = treeNodeViewModel.Children;
  130. if (treeNodeViewModel.IsFolder)
  131. {
  132. return;
  133. }
  134. foreach (var tn in children)
  135. {
  136. this.RecursionAdd(tn);
  137. }
  138. }
  139. }
  140. }