TreeViewModel.cs 8.2 KB

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