TreeViewModel.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.ComponentModel.Composition;
  6. using Common.Helper;
  7. using Microsoft.Practices.Prism.Mvvm;
  8. using MongoDB.Bson.Serialization.Attributes;
  9. namespace Modules.BehaviorTreeModule
  10. {
  11. [BsonDiscriminator("TreeProto", RootClass = true)]
  12. [Export(typeof (TreeViewModel)), PartCreationPolicy(CreationPolicy.NonShared)]
  13. public class TreeViewModel: BindableBase, ICloneable, ISupportInitialize
  14. {
  15. [BsonElement]
  16. private TreeNodeViewModel root;
  17. [BsonElement]
  18. private int treeId;
  19. [BsonElement]
  20. private int maxNodeId;
  21. [BsonIgnore]
  22. public ObservableCollection<TreeNodeViewModel> allNodes = new ObservableCollection<TreeNodeViewModel>();
  23. [BsonIgnore]
  24. public ObservableCollection<TreeNodeViewModel> AllNodes
  25. {
  26. get
  27. {
  28. return this.allNodes;
  29. }
  30. }
  31. [BsonIgnore]
  32. public int TreeId
  33. {
  34. get
  35. {
  36. return this.treeId;
  37. }
  38. set
  39. {
  40. if (this.treeId == value)
  41. {
  42. return;
  43. }
  44. this.treeId = value;
  45. this.OnPropertyChanged("TreeId");
  46. }
  47. }
  48. [BsonIgnore]
  49. public string Comment
  50. {
  51. get
  52. {
  53. if (this.root == null)
  54. {
  55. return "";
  56. }
  57. return this.root.Comment;
  58. }
  59. set
  60. {
  61. this.OnPropertyChanged("Comment");
  62. }
  63. }
  64. public int MaxNodeId
  65. {
  66. get
  67. {
  68. return this.maxNodeId;
  69. }
  70. set
  71. {
  72. this.maxNodeId = value;
  73. }
  74. }
  75. [BsonIgnore]
  76. public TreeNodeViewModel copy;
  77. [BsonIgnore]
  78. public TreeNodeViewModel Root
  79. {
  80. get
  81. {
  82. return this.root;
  83. }
  84. set
  85. {
  86. this.root = value;
  87. }
  88. }
  89. public void Add(TreeNodeViewModel treeNode, TreeNodeViewModel parent)
  90. {
  91. // 如果父节点是折叠的,需要先展开父节点
  92. if (parent != null)
  93. {
  94. if (parent.IsFold)
  95. {
  96. this.UnFold(parent);
  97. }
  98. treeNode.Parent = parent;
  99. parent.Children.Add(treeNode);
  100. }
  101. else
  102. {
  103. this.root = treeNode;
  104. }
  105. treeNode.TreeViewModel = this;
  106. allNodes.Add(treeNode);
  107. TreeLayout treeLayout = new TreeLayout(this);
  108. treeLayout.ExcuteLayout();
  109. }
  110. private void GetChildrenIdAndSelf(TreeNodeViewModel treeNodeViewModel, List<TreeNodeViewModel> children)
  111. {
  112. children.Add(treeNodeViewModel);
  113. this.GetAllChildrenId(treeNodeViewModel, children);
  114. }
  115. private void GetAllChildrenId(TreeNodeViewModel treeNodeViewModel, List<TreeNodeViewModel> children)
  116. {
  117. foreach (TreeNodeViewModel child in treeNodeViewModel.Children)
  118. {
  119. children.Add(child);
  120. this.GetAllChildrenId(child, children);
  121. }
  122. }
  123. public void Remove(TreeNodeViewModel treeNodeViewModel)
  124. {
  125. var all = new List<TreeNodeViewModel>();
  126. this.GetChildrenIdAndSelf(treeNodeViewModel, all);
  127. foreach (TreeNodeViewModel child in all)
  128. {
  129. this.allNodes.Remove(child);
  130. }
  131. TreeNodeViewModel parent = treeNodeViewModel.Parent;
  132. if (parent != null)
  133. {
  134. parent.Children.Remove(treeNodeViewModel);
  135. }
  136. TreeLayout treeLayout = new TreeLayout(this);
  137. treeLayout.ExcuteLayout();
  138. }
  139. private void RecursionMove(TreeNodeViewModel treeNodeViewModel, double offsetX, double offsetY)
  140. {
  141. treeNodeViewModel.XX += offsetX;
  142. treeNodeViewModel.YY += offsetY;
  143. foreach (TreeNodeViewModel child in treeNodeViewModel.Children)
  144. {
  145. this.RecursionMove(child, offsetX, offsetY);
  146. }
  147. }
  148. public void MoveToPosition(double offsetX, double offsetY)
  149. {
  150. this.RecursionMove(this.Root, offsetX, offsetY);
  151. }
  152. public void MoveToNode(TreeNodeViewModel from, TreeNodeViewModel to)
  153. {
  154. // from节点不能是to节点的父级节点
  155. TreeNodeViewModel tmpNode = to;
  156. while (tmpNode != null)
  157. {
  158. if (tmpNode.IsRoot)
  159. {
  160. break;
  161. }
  162. if (tmpNode.Id == from.Id)
  163. {
  164. return;
  165. }
  166. tmpNode = tmpNode.Parent;
  167. }
  168. if (from.IsFold)
  169. {
  170. this.UnFold(from);
  171. }
  172. if (to.IsFold)
  173. {
  174. this.UnFold(to);
  175. }
  176. from.Parent.Children.Remove(from);
  177. to.Children.Add(from);
  178. from.Parent = to;
  179. TreeLayout treeLayout = new TreeLayout(this);
  180. treeLayout.ExcuteLayout();
  181. }
  182. /// <summary>
  183. /// 折叠节点
  184. /// </summary>
  185. /// <param name="treeNodeViewModel"></param>
  186. public void Fold(TreeNodeViewModel treeNodeViewModel)
  187. {
  188. var allChild = new List<TreeNodeViewModel>();
  189. this.GetAllChildrenId(treeNodeViewModel, allChild);
  190. foreach (TreeNodeViewModel child in allChild)
  191. {
  192. this.allNodes.Remove(child);
  193. }
  194. treeNodeViewModel.IsFold = true;
  195. TreeLayout treeLayout = new TreeLayout(this);
  196. treeLayout.ExcuteLayout();
  197. }
  198. /// <summary>
  199. /// 展开节点,一级一级展开,一次只展开下层子节点,比如下层节点是折叠的,那下下层节点不展开
  200. /// </summary>
  201. /// <param name="treeNodeViewModel"></param>
  202. public void UnFold(TreeNodeViewModel treeNodeViewModel)
  203. {
  204. treeNodeViewModel.IsFold = false;
  205. var allChild = new List<TreeNodeViewModel>();
  206. this.GetAllChildrenId(treeNodeViewModel, allChild);
  207. foreach (TreeNodeViewModel child in allChild)
  208. {
  209. this.allNodes.Add(child);
  210. }
  211. TreeLayout treeLayout = new TreeLayout(this);
  212. treeLayout.ExcuteLayout();
  213. }
  214. public void MoveLeft(TreeNodeViewModel treeNodeViewModel)
  215. {
  216. if (treeNodeViewModel.IsRoot)
  217. {
  218. return;
  219. }
  220. TreeNodeViewModel parent = treeNodeViewModel.Parent;
  221. int index = parent.Children.IndexOf(treeNodeViewModel);
  222. if (index == 0)
  223. {
  224. return;
  225. }
  226. parent.Children.Remove(treeNodeViewModel);
  227. parent.Children.Insert(index - 1, treeNodeViewModel);
  228. TreeLayout treeLayout = new TreeLayout(this);
  229. treeLayout.ExcuteLayout();
  230. }
  231. public void MoveRight(TreeNodeViewModel treeNodeViewModel)
  232. {
  233. if (treeNodeViewModel.IsRoot)
  234. {
  235. return;
  236. }
  237. TreeNodeViewModel parent = treeNodeViewModel.Parent;
  238. int index = parent.Children.IndexOf(treeNodeViewModel);
  239. if (index == parent.Children.Count - 1)
  240. {
  241. return;
  242. }
  243. parent.Children.Remove(treeNodeViewModel);
  244. parent.Children.Insert(index + 1, treeNodeViewModel);
  245. TreeLayout treeLayout = new TreeLayout(this);
  246. treeLayout.ExcuteLayout();
  247. }
  248. public void Copy(TreeNodeViewModel copyTreeNodeViewModel)
  249. {
  250. this.copy = copyTreeNodeViewModel;
  251. }
  252. public void Paste(TreeNodeViewModel pasteTreeNodeViewModel)
  253. {
  254. if (this.copy == null)
  255. {
  256. return;
  257. }
  258. TreeNodeViewModel copyTreeNodeViewModel = this.copy;
  259. // copy节点不能是paste节点的父级节点
  260. TreeNodeViewModel tmpNode = pasteTreeNodeViewModel;
  261. while (tmpNode != null)
  262. {
  263. if (tmpNode.IsRoot)
  264. {
  265. break;
  266. }
  267. if (tmpNode.Id == copyTreeNodeViewModel.Id)
  268. {
  269. return;
  270. }
  271. tmpNode = tmpNode.Parent;
  272. }
  273. this.copy = null;
  274. this.CopyTree(copyTreeNodeViewModel, pasteTreeNodeViewModel);
  275. }
  276. private void CopyTree(TreeNodeViewModel copyTreeNodeViewModel, TreeNodeViewModel parent)
  277. {
  278. TreeNodeViewModel newTreeNodeViewModel = (TreeNodeViewModel)copyTreeNodeViewModel.Clone();
  279. newTreeNodeViewModel.Id = ++this.MaxNodeId;
  280. this.Add(newTreeNodeViewModel, parent);
  281. foreach (TreeNodeViewModel child in copyTreeNodeViewModel.Children)
  282. {
  283. this.CopyTree(child, newTreeNodeViewModel);
  284. }
  285. }
  286. public object Clone()
  287. {
  288. return MongoHelper.FromJson<TreeViewModel>(MongoHelper.ToJson(this));
  289. }
  290. private void SetChildParent(TreeNodeViewModel node)
  291. {
  292. if (node == null)
  293. {
  294. return;
  295. }
  296. node.TreeViewModel = this;
  297. allNodes.Add(node);
  298. foreach (TreeNodeViewModel child in node.Children)
  299. {
  300. child.Parent = node;
  301. SetChildParent(child);
  302. }
  303. }
  304. public void BeginInit()
  305. {
  306. }
  307. public void EndInit()
  308. {
  309. SetChildParent(Root);
  310. this.Root.XX = 250;
  311. this.Root.YY = 10;
  312. }
  313. }
  314. }