TreeViewModel.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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(
  115. TreeNodeViewModel treeNodeViewModel, double offsetX, double offsetY)
  116. {
  117. treeNodeViewModel.X += offsetX;
  118. treeNodeViewModel.Y += offsetY;
  119. foreach (var childId in treeNodeViewModel.Children)
  120. {
  121. TreeNodeViewModel child = this.Get(childId);
  122. this.RecursionMove(child, offsetX, offsetY);
  123. }
  124. }
  125. public void MoveToPosition(double offsetX, double offsetY)
  126. {
  127. this.RecursionMove(this.Root, offsetX, offsetY);
  128. }
  129. public void MoveToNode(TreeNodeViewModel from, TreeNodeViewModel to)
  130. {
  131. // from节点不能是to节点的父级节点
  132. TreeNodeViewModel tmpNode = to;
  133. while (tmpNode != null)
  134. {
  135. if (tmpNode.IsRoot)
  136. {
  137. break;
  138. }
  139. if (tmpNode.Id == from.Id)
  140. {
  141. return;
  142. }
  143. tmpNode = tmpNode.Parent;
  144. }
  145. if (from.IsFold)
  146. {
  147. this.UnFold(from);
  148. }
  149. if (to.IsFold)
  150. {
  151. this.UnFold(to);
  152. }
  153. from.Parent.Children.Remove(from.Id);
  154. to.Children.Add(from.Id);
  155. from.Parent = to;
  156. var treeLayout = new TreeLayout(this);
  157. treeLayout.ExcuteLayout();
  158. }
  159. /// <summary>
  160. /// 折叠节点
  161. /// </summary>
  162. /// <param name="treeNodeViewModel"></param>
  163. public void Fold(TreeNodeViewModel treeNodeViewModel)
  164. {
  165. List<int> allChildId = new List<int>();
  166. this.GetAllChildrenId(treeNodeViewModel, allChildId);
  167. foreach (int childId in allChildId)
  168. {
  169. TreeNodeViewModel child = this.Get(childId);
  170. this.treeNodes.Remove(child);
  171. }
  172. treeNodeViewModel.IsFold = true;
  173. var treeLayout = new TreeLayout(this);
  174. treeLayout.ExcuteLayout();
  175. }
  176. /// <summary>
  177. /// 展开节点,一级一级展开,一次只展开下层子节点,比如下层节点是折叠的,那下下层节点不展开
  178. /// </summary>
  179. /// <param name="treeNodeViewModel"></param>
  180. public void UnFold(TreeNodeViewModel treeNodeViewModel)
  181. {
  182. treeNodeViewModel.IsFold = false;
  183. List<int> allChildId = new List<int>();
  184. this.GetAllChildrenId(treeNodeViewModel, allChildId);
  185. foreach (int childId in allChildId)
  186. {
  187. TreeNodeViewModel child = this.Get(childId);
  188. this.treeNodes.Add(child);
  189. }
  190. var treeLayout = new TreeLayout(this);
  191. treeLayout.ExcuteLayout();
  192. }
  193. public void MoveLeft(TreeNodeViewModel treeNodeViewModel)
  194. {
  195. if (treeNodeViewModel.IsRoot)
  196. {
  197. return;
  198. }
  199. var parent = treeNodeViewModel.Parent;
  200. int index = parent.Children.IndexOf(treeNodeViewModel.Id);
  201. if (index == 0)
  202. {
  203. return;
  204. }
  205. parent.Children.Remove(treeNodeViewModel.Id);
  206. parent.Children.Insert(index - 1, treeNodeViewModel.Id);
  207. var treeLayout = new TreeLayout(this);
  208. treeLayout.ExcuteLayout();
  209. }
  210. public void MoveRight(TreeNodeViewModel treeNodeViewModel)
  211. {
  212. if (treeNodeViewModel.IsRoot)
  213. {
  214. return;
  215. }
  216. var parent = treeNodeViewModel.Parent;
  217. int index = parent.Children.IndexOf(treeNodeViewModel.Id);
  218. if (index == parent.Children.Count - 1)
  219. {
  220. return;
  221. }
  222. parent.Children.Remove(treeNodeViewModel.Id);
  223. parent.Children.Insert(index + 1, treeNodeViewModel.Id);
  224. var treeLayout = new TreeLayout(this);
  225. treeLayout.ExcuteLayout();
  226. }
  227. }
  228. }