TreeViewModel.cs 7.9 KB

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