TreeViewModel.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel.Composition;
  4. using Microsoft.Practices.Prism.Mvvm;
  5. namespace Modules.BehaviorTreeModule
  6. {
  7. [Export(typeof (TreeViewModel)), PartCreationPolicy(CreationPolicy.NonShared)]
  8. public class TreeViewModel: BindableBase
  9. {
  10. private readonly ObservableCollection<TreeNodeViewModel> treeNodes =
  11. new ObservableCollection<TreeNodeViewModel>();
  12. private readonly Dictionary<int, TreeNodeViewModel> treeNodeDict =
  13. new Dictionary<int, TreeNodeViewModel>();
  14. public ObservableCollection<TreeNodeViewModel> TreeNodes
  15. {
  16. get
  17. {
  18. return this.treeNodes;
  19. }
  20. }
  21. public int TreeId { get; set; }
  22. public TreeViewModel(AllTreeViewModel allTreeViewModel)
  23. {
  24. this.AllTreeViewModel = allTreeViewModel;
  25. this.TreeId = ++allTreeViewModel.MaxTreeId;
  26. TreeNodeViewModel treeNodeViewModel = new TreeNodeViewModel(this, 300, 100);
  27. this.treeNodes.Add(treeNodeViewModel);
  28. this.treeNodeDict[treeNodeViewModel.Id] = treeNodeViewModel;
  29. var treeLayout = new TreeLayout(this);
  30. treeLayout.ExcuteLayout();
  31. }
  32. public TreeViewModel(List<TreeNodeData> treeNodeDatas)
  33. {
  34. foreach (TreeNodeData treeNodeData in treeNodeDatas)
  35. {
  36. TreeNodeViewModel treeNodeViewModel = new TreeNodeViewModel(this, treeNodeData);
  37. this.treeNodes.Add(treeNodeViewModel);
  38. this.treeNodeDict[treeNodeViewModel.Id] = treeNodeViewModel;
  39. }
  40. var treeLayout = new TreeLayout(this);
  41. treeLayout.ExcuteLayout();
  42. }
  43. public List<TreeNodeData> GetDatas()
  44. {
  45. List<TreeNodeData> treeNodeDatas = new List<TreeNodeData>();
  46. foreach (TreeNodeViewModel treeNodeViewModel in this.treeNodes)
  47. {
  48. treeNodeDatas.Add(treeNodeViewModel.Data);
  49. }
  50. return treeNodeDatas;
  51. }
  52. public AllTreeViewModel AllTreeViewModel { get; set; }
  53. public TreeNodeViewModel Root
  54. {
  55. get
  56. {
  57. return this.treeNodes.Count == 0? null : this.treeNodes[0];
  58. }
  59. }
  60. public TreeNodeViewModel Get(int id)
  61. {
  62. TreeNodeViewModel node;
  63. this.treeNodeDict.TryGetValue(id, out node);
  64. return node;
  65. }
  66. public void Add(TreeNodeViewModel treeNode, TreeNodeViewModel parent)
  67. {
  68. // 如果父节点是折叠的,需要先展开父节点
  69. if (parent != null && parent.IsFold)
  70. {
  71. this.UnFold(parent);
  72. }
  73. this.treeNodes.Add(treeNode);
  74. this.treeNodeDict[treeNode.Id] = treeNode;
  75. if (parent != null)
  76. {
  77. parent.Children.Add(treeNode.Id);
  78. }
  79. var treeLayout = new TreeLayout(this);
  80. treeLayout.ExcuteLayout();
  81. }
  82. private void GetChildrenIdAndSelf(TreeNodeViewModel treeNodeViewModel, List<int> children)
  83. {
  84. children.Add(treeNodeViewModel.Id);
  85. this.GetAllChildrenId(treeNodeViewModel, children);
  86. }
  87. private void GetAllChildrenId(TreeNodeViewModel treeNodeViewModel, List<int> children)
  88. {
  89. foreach (int childId in treeNodeViewModel.Children)
  90. {
  91. TreeNodeViewModel child = this.Get(childId);
  92. children.Add(child.Id);
  93. this.GetAllChildrenId(child, children);
  94. }
  95. }
  96. public void Remove(TreeNodeViewModel treeNodeViewModel)
  97. {
  98. List<int> allId = new List<int>();
  99. this.GetChildrenIdAndSelf(treeNodeViewModel, allId);
  100. foreach (int childId in allId)
  101. {
  102. TreeNodeViewModel child = this.Get(childId);
  103. this.treeNodes.Remove(child);
  104. this.treeNodes.Remove(treeNodeViewModel);
  105. }
  106. TreeNodeViewModel parent = treeNodeViewModel.Parent;
  107. if (parent != null)
  108. {
  109. parent.Children.Remove(treeNodeViewModel.Id);
  110. }
  111. var treeLayout = new TreeLayout(this);
  112. treeLayout.ExcuteLayout();
  113. }
  114. private void RecursionMove(TreeNodeViewModel treeNodeViewModel, double offsetX, double offsetY)
  115. {
  116. treeNodeViewModel.X += offsetX;
  117. treeNodeViewModel.Y += offsetY;
  118. foreach (var childId in treeNodeViewModel.Children)
  119. {
  120. TreeNodeViewModel child = this.Get(childId);
  121. this.RecursionMove(child, offsetX, offsetY);
  122. }
  123. }
  124. public void MoveToPosition(double offsetX, double offsetY)
  125. {
  126. this.RecursionMove(this.Root, offsetX, offsetY);
  127. }
  128. public void MoveToNode(TreeNodeViewModel from, TreeNodeViewModel to)
  129. {
  130. // from节点不能是to节点的父级节点
  131. TreeNodeViewModel tmpNode = to;
  132. while (tmpNode != null)
  133. {
  134. if (tmpNode.IsRoot)
  135. {
  136. break;
  137. }
  138. if (tmpNode.Id == from.Id)
  139. {
  140. return;
  141. }
  142. tmpNode = tmpNode.Parent;
  143. }
  144. if (from.IsFold)
  145. {
  146. this.UnFold(from);
  147. }
  148. if (to.IsFold)
  149. {
  150. this.UnFold(to);
  151. }
  152. from.Parent.Children.Remove(from.Id);
  153. to.Children.Add(from.Id);
  154. from.Parent = to;
  155. var treeLayout = new TreeLayout(this);
  156. treeLayout.ExcuteLayout();
  157. }
  158. /// <summary>
  159. /// 折叠节点
  160. /// </summary>
  161. /// <param name="treeNodeViewModel"></param>
  162. public void Fold(TreeNodeViewModel treeNodeViewModel)
  163. {
  164. List<int> allChildId = new List<int>();
  165. this.GetAllChildrenId(treeNodeViewModel, allChildId);
  166. foreach (int childId in allChildId)
  167. {
  168. TreeNodeViewModel child = this.Get(childId);
  169. this.treeNodes.Remove(child);
  170. }
  171. treeNodeViewModel.IsFold = true;
  172. var treeLayout = new TreeLayout(this);
  173. treeLayout.ExcuteLayout();
  174. }
  175. /// <summary>
  176. /// 展开节点,一级一级展开,一次只展开下层子节点,比如下层节点是折叠的,那下下层节点不展开
  177. /// </summary>
  178. /// <param name="treeNodeViewModel"></param>
  179. public void UnFold(TreeNodeViewModel treeNodeViewModel)
  180. {
  181. treeNodeViewModel.IsFold = false;
  182. List<int> allChildId = new List<int>();
  183. this.GetAllChildrenId(treeNodeViewModel, allChildId);
  184. foreach (int childId in allChildId)
  185. {
  186. TreeNodeViewModel child = this.Get(childId);
  187. this.treeNodes.Add(child);
  188. }
  189. var treeLayout = new TreeLayout(this);
  190. treeLayout.ExcuteLayout();
  191. }
  192. public void MoveLeft(TreeNodeViewModel treeNodeViewModel)
  193. {
  194. if (treeNodeViewModel.IsRoot)
  195. {
  196. return;
  197. }
  198. var parent = treeNodeViewModel.Parent;
  199. int index = parent.Children.IndexOf(treeNodeViewModel.Id);
  200. if (index == 0)
  201. {
  202. return;
  203. }
  204. parent.Children.Remove(treeNodeViewModel.Id);
  205. parent.Children.Insert(index - 1, treeNodeViewModel.Id);
  206. var treeLayout = new TreeLayout(this);
  207. treeLayout.ExcuteLayout();
  208. }
  209. public void MoveRight(TreeNodeViewModel treeNodeViewModel)
  210. {
  211. if (treeNodeViewModel.IsRoot)
  212. {
  213. return;
  214. }
  215. var parent = treeNodeViewModel.Parent;
  216. int index = parent.Children.IndexOf(treeNodeViewModel.Id);
  217. if (index == parent.Children.Count - 1)
  218. {
  219. return;
  220. }
  221. parent.Children.Remove(treeNodeViewModel.Id);
  222. parent.Children.Insert(index + 1, treeNodeViewModel.Id);
  223. var treeLayout = new TreeLayout(this);
  224. treeLayout.ExcuteLayout();
  225. }
  226. }
  227. }