TreeNodeViewModel.cs 6.4 KB

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