TreeNodeViewModel.cs 7.0 KB

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