TreeNodeViewModel.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. if (this.data.Id == value)
  75. {
  76. return;
  77. }
  78. this.data.Id = value;
  79. this.OnPropertyChanged("Id");
  80. }
  81. }
  82. public string Comment
  83. {
  84. get
  85. {
  86. return this.data.Comment;
  87. }
  88. set
  89. {
  90. if (this.data.Comment == value)
  91. {
  92. return;
  93. }
  94. this.data.Comment = value;
  95. this.OnPropertyChanged("Comment");
  96. }
  97. }
  98. public static double Width
  99. {
  100. get
  101. {
  102. return width;
  103. }
  104. set
  105. {
  106. width = value;
  107. }
  108. }
  109. public static double Height
  110. {
  111. get
  112. {
  113. return height;
  114. }
  115. set
  116. {
  117. height = value;
  118. }
  119. }
  120. public bool IsRoot
  121. {
  122. get
  123. {
  124. return this.Parent == null;
  125. }
  126. }
  127. public double Prelim
  128. {
  129. get
  130. {
  131. return this.prelim;
  132. }
  133. set
  134. {
  135. this.prelim = value;
  136. }
  137. }
  138. public double Modify
  139. {
  140. get
  141. {
  142. return this.modify;
  143. }
  144. set
  145. {
  146. this.modify = value;
  147. }
  148. }
  149. public double X
  150. {
  151. get
  152. {
  153. return this.x;
  154. }
  155. set
  156. {
  157. if (Math.Abs(this.x - value) < 0.1)
  158. {
  159. return;
  160. }
  161. this.x = value;
  162. this.OnPropertyChanged("X");
  163. if (this.Parent != null)
  164. {
  165. this.ConnectorX2 = Width / 2 + this.Parent.X - this.X;
  166. }
  167. foreach (var childId in this.Children)
  168. {
  169. TreeNodeViewModel child = this.treeViewModel.Get(childId);
  170. child.ConnectorX2 = Width / 2 + this.X - child.X;
  171. }
  172. }
  173. }
  174. public double Y
  175. {
  176. get
  177. {
  178. return this.y;
  179. }
  180. set
  181. {
  182. if (Math.Abs(this.Y - value) < 0.1)
  183. {
  184. return;
  185. }
  186. this.y = value;
  187. this.OnPropertyChanged("Y");
  188. if (this.Parent != null)
  189. {
  190. this.ConnectorY2 = Height + this.Parent.Y - this.Y;
  191. }
  192. foreach (var childId in this.Children)
  193. {
  194. TreeNodeViewModel child = this.treeViewModel.Get(childId);
  195. child.ConnectorY2 = Height + this.Y - child.Y;
  196. }
  197. }
  198. }
  199. public double ConnectorX1
  200. {
  201. get
  202. {
  203. return Width / 2;
  204. }
  205. }
  206. public double ConnectorY1
  207. {
  208. get
  209. {
  210. return 0;
  211. }
  212. }
  213. public double ConnectorX2
  214. {
  215. get
  216. {
  217. return this.IsRoot? Width / 2 : this.connectorX2;
  218. }
  219. set
  220. {
  221. this.SetProperty(ref this.connectorX2, value);
  222. }
  223. }
  224. public double ConnectorY2
  225. {
  226. get
  227. {
  228. return this.IsRoot? 0 : this.connectorY2;
  229. }
  230. set
  231. {
  232. this.SetProperty(ref this.connectorY2, value);
  233. }
  234. }
  235. public int Type
  236. {
  237. get
  238. {
  239. return this.data.Type;
  240. }
  241. set
  242. {
  243. if (this.data.Type == value)
  244. {
  245. return;
  246. }
  247. this.data.Type = value;
  248. this.OnPropertyChanged("Type");
  249. }
  250. }
  251. public List<string> Args
  252. {
  253. get
  254. {
  255. return this.data.Args;
  256. }
  257. set
  258. {
  259. if (this.data.Args == value)
  260. {
  261. return;
  262. }
  263. this.data.Args = value;
  264. this.OnPropertyChanged("Args");
  265. }
  266. }
  267. public int TreeId
  268. {
  269. get
  270. {
  271. return this.data.TreeId;
  272. }
  273. }
  274. public TreeNodeViewModel Parent
  275. {
  276. get
  277. {
  278. if (this.data.Parent == 0)
  279. {
  280. return null;
  281. }
  282. TreeNodeViewModel parent = this.treeViewModel.Get(this.data.Parent);
  283. return parent;
  284. }
  285. set
  286. {
  287. if (value == null)
  288. {
  289. this.data.Parent = 0;
  290. }
  291. this.data.Parent = value.Id;
  292. }
  293. }
  294. /// <summary>
  295. /// 节点是否折叠
  296. /// </summary>
  297. public bool IsFold
  298. {
  299. get
  300. {
  301. return this.isFold;
  302. }
  303. set
  304. {
  305. if (this.isFold == value)
  306. {
  307. return;
  308. }
  309. this.isFold = value;
  310. this.OnPropertyChanged("IsFold");
  311. }
  312. }
  313. public List<int> Children
  314. {
  315. get
  316. {
  317. if (this.isFold)
  318. {
  319. return new List<int>();
  320. }
  321. return this.data.Children;
  322. }
  323. }
  324. public TreeNodeViewModel LeftSibling
  325. {
  326. get
  327. {
  328. if (this.IsRoot)
  329. {
  330. return null;
  331. }
  332. int index = this.Parent.Children.IndexOf(this.Id);
  333. return index == 0? null : this.treeViewModel.Get(this.Parent.Children[index - 1]);
  334. }
  335. }
  336. public TreeNodeViewModel LastChild
  337. {
  338. get
  339. {
  340. if (this.Children.Count == 0)
  341. {
  342. return null;
  343. }
  344. int maxIndex = this.Children.Count - 1;
  345. return this.treeViewModel.Get(this.Children[maxIndex]);
  346. }
  347. }
  348. public TreeNodeViewModel FirstChild
  349. {
  350. get
  351. {
  352. return this.Children.Count == 0? null : this.treeViewModel.Get(this.Children[0]);
  353. }
  354. }
  355. public bool IsLeaf
  356. {
  357. get
  358. {
  359. return this.Children.Count == 0;
  360. }
  361. }
  362. public double AncestorModify
  363. {
  364. get
  365. {
  366. return this.ancestorModify;
  367. }
  368. set
  369. {
  370. this.ancestorModify = value;
  371. }
  372. }
  373. }
  374. }