TreeNodeViewModel.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Practices.Prism.Mvvm;
  4. namespace Modules.BehaviorTreeModule
  5. {
  6. public class TreeNodeViewModel: BindableBase
  7. {
  8. private static double width = 80;
  9. private static double height = 50;
  10. private readonly TreeViewModel treeViewModel;
  11. private readonly TreeNodeData data;
  12. private double x;
  13. private double y;
  14. private double connectorX2;
  15. private double connectorY2;
  16. private double prelim;
  17. private double modify;
  18. private double ancestorModify;
  19. private bool isFold;
  20. public TreeNodeViewModel(TreeViewModel treeViewModel, double x, double y)
  21. {
  22. this.treeViewModel = treeViewModel;
  23. this.x = x;
  24. this.y = y;
  25. this.data = new TreeNodeData();
  26. this.data.Id = ++treeViewModel.AllTreeViewModel.MaxNodeId;
  27. this.data.TreeId = treeViewModel.TreeId;
  28. this.data.Parent = 0;
  29. this.connectorX2 = 0;
  30. this.connectorY2 = Height / 2;
  31. }
  32. public TreeNodeViewModel(TreeViewModel treeViewModel, TreeNodeViewModel parent)
  33. {
  34. this.treeViewModel = treeViewModel;
  35. this.data = new TreeNodeData();
  36. this.data.Id = ++treeViewModel.AllTreeViewModel.MaxNodeId;
  37. this.data.TreeId = treeViewModel.TreeId;
  38. this.Parent = parent;
  39. this.connectorX2 = Width + this.Parent.X - this.X;
  40. this.connectorY2 = Height / 2 + this.Parent.Y - this.Y;
  41. }
  42. public TreeNodeViewModel(TreeViewModel treeViewModel, TreeNodeData data)
  43. {
  44. this.treeViewModel = treeViewModel;
  45. this.data = data;
  46. if (this.IsRoot)
  47. {
  48. this.x = 300;
  49. this.y = 100;
  50. this.connectorX2 = 0;
  51. this.connectorY2 = Height / 2;
  52. }
  53. else
  54. {
  55. this.connectorX2 = Width + this.Parent.X - this.X;
  56. this.connectorY2 = Height / 2 + this.Parent.Y - this.Y;
  57. }
  58. }
  59. public TreeNodeData Data
  60. {
  61. get
  62. {
  63. return this.data;
  64. }
  65. }
  66. public int Id
  67. {
  68. get
  69. {
  70. return this.data.Id;
  71. }
  72. set
  73. {
  74. this.data.Id = value;
  75. this.OnPropertyChanged("Id");
  76. }
  77. }
  78. public string Comment
  79. {
  80. get
  81. {
  82. return this.data.Comment;
  83. }
  84. set
  85. {
  86. this.data.Comment = value;
  87. this.OnPropertyChanged("Comment");
  88. }
  89. }
  90. public static double Width
  91. {
  92. get
  93. {
  94. return width;
  95. }
  96. set
  97. {
  98. width = value;
  99. }
  100. }
  101. public static double Height
  102. {
  103. get
  104. {
  105. return height;
  106. }
  107. set
  108. {
  109. height = value;
  110. }
  111. }
  112. public bool IsRoot
  113. {
  114. get
  115. {
  116. return this.Parent == null;
  117. }
  118. }
  119. public double Prelim
  120. {
  121. get
  122. {
  123. return this.prelim;
  124. }
  125. set
  126. {
  127. this.prelim = value;
  128. }
  129. }
  130. public double Modify
  131. {
  132. get
  133. {
  134. return this.modify;
  135. }
  136. set
  137. {
  138. this.modify = value;
  139. }
  140. }
  141. public double X
  142. {
  143. get
  144. {
  145. return this.x;
  146. }
  147. set
  148. {
  149. if (Math.Abs(this.x - value) < 0.1)
  150. {
  151. return;
  152. }
  153. this.x = value;
  154. this.OnPropertyChanged("X");
  155. if (this.Parent != null)
  156. {
  157. this.ConnectorX2 = Width / 2 + this.Parent.X - this.X;
  158. }
  159. foreach (var childId in this.Children)
  160. {
  161. TreeNodeViewModel child = this.treeViewModel.Get(childId);
  162. child.ConnectorX2 = Width / 2 + this.X - child.X;
  163. }
  164. }
  165. }
  166. public double Y
  167. {
  168. get
  169. {
  170. return this.y;
  171. }
  172. set
  173. {
  174. if (Math.Abs(this.Y - value) < 0.1)
  175. {
  176. return;
  177. }
  178. this.y = value;
  179. this.OnPropertyChanged("Y");
  180. if (this.Parent != null)
  181. {
  182. this.ConnectorY2 = Height + this.Parent.Y - this.Y;
  183. }
  184. foreach (var childId in this.Children)
  185. {
  186. TreeNodeViewModel child = this.treeViewModel.Get(childId);
  187. child.ConnectorY2 = Height + this.Y - child.Y;
  188. }
  189. }
  190. }
  191. public double ConnectorX1
  192. {
  193. get
  194. {
  195. return Width / 2;
  196. }
  197. }
  198. public double ConnectorY1
  199. {
  200. get
  201. {
  202. return 0;
  203. }
  204. }
  205. public double ConnectorX2
  206. {
  207. get
  208. {
  209. return this.IsRoot? Width / 2 : this.connectorX2;
  210. }
  211. set
  212. {
  213. this.SetProperty(ref this.connectorX2, value);
  214. }
  215. }
  216. public double ConnectorY2
  217. {
  218. get
  219. {
  220. return this.IsRoot? 0 : this.connectorY2;
  221. }
  222. set
  223. {
  224. this.SetProperty(ref this.connectorY2, value);
  225. }
  226. }
  227. public int Type
  228. {
  229. get
  230. {
  231. return this.data.Type;
  232. }
  233. set
  234. {
  235. if (this.data.Type == value)
  236. {
  237. return;
  238. }
  239. this.data.Type = value;
  240. this.OnPropertyChanged("Type");
  241. }
  242. }
  243. public List<string> Args
  244. {
  245. get
  246. {
  247. return this.data.Args;
  248. }
  249. set
  250. {
  251. if (this.data.Args == value)
  252. {
  253. return;
  254. }
  255. this.data.Args = value;
  256. this.OnPropertyChanged("Args");
  257. }
  258. }
  259. public int TreeId
  260. {
  261. get
  262. {
  263. return this.data.TreeId;
  264. }
  265. }
  266. public TreeNodeViewModel Parent
  267. {
  268. get
  269. {
  270. if (this.data.Parent == 0)
  271. {
  272. return null;
  273. }
  274. TreeNodeViewModel parent = this.treeViewModel.Get(this.data.Parent);
  275. return parent;
  276. }
  277. set
  278. {
  279. if (value == null)
  280. {
  281. this.data.Parent = 0;
  282. }
  283. this.data.Parent = value.Id;
  284. }
  285. }
  286. /// <summary>
  287. /// 节点是否折叠
  288. /// </summary>
  289. public bool IsFold
  290. {
  291. get
  292. {
  293. return this.isFold;
  294. }
  295. set
  296. {
  297. if (this.isFold == value)
  298. {
  299. return;
  300. }
  301. this.isFold = value;
  302. this.OnPropertyChanged("IsFold");
  303. }
  304. }
  305. public List<int> Children
  306. {
  307. get
  308. {
  309. if (this.isFold)
  310. {
  311. return new List<int>();
  312. }
  313. return this.data.Children;
  314. }
  315. }
  316. public TreeNodeViewModel LeftSibling
  317. {
  318. get
  319. {
  320. if (this.IsRoot)
  321. {
  322. return null;
  323. }
  324. int index = this.Parent.Children.IndexOf(this.Id);
  325. return index == 0? null : this.treeViewModel.Get(this.Parent.Children[index - 1]);
  326. }
  327. }
  328. public TreeNodeViewModel LastChild
  329. {
  330. get
  331. {
  332. if (this.Children.Count == 0)
  333. {
  334. return null;
  335. }
  336. int maxIndex = this.Children.Count - 1;
  337. return this.treeViewModel.Get(this.Children[maxIndex]);
  338. }
  339. }
  340. public TreeNodeViewModel FirstChild
  341. {
  342. get
  343. {
  344. return this.Children.Count == 0? null : this.treeViewModel.Get(this.Children[0]);
  345. }
  346. }
  347. public bool IsLeaf
  348. {
  349. get
  350. {
  351. return this.Children.Count == 0;
  352. }
  353. }
  354. public double AncestorModify
  355. {
  356. get
  357. {
  358. return this.ancestorModify;
  359. }
  360. set
  361. {
  362. this.ancestorModify = value;
  363. }
  364. }
  365. }
  366. }