TreeViewModel.cs 7.9 KB

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