TreeNodeViewModel.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using System.Collections.ObjectModel;
  2. using Microsoft.Practices.Prism.ViewModel;
  3. namespace BehaviorTree
  4. {
  5. public class TreeNodeViewModel : NotificationObject
  6. {
  7. private static int globalNum;
  8. private readonly int num;
  9. private const double width = 80;
  10. private const double height = 50;
  11. private readonly TreeNode treeNode;
  12. private double connectorX2;
  13. private double connectorY2;
  14. private double prelim;
  15. private double modify;
  16. private double ancestorModify;
  17. private TreeNodeViewModel parent;
  18. private ObservableCollection<TreeNodeViewModel> children = new ObservableCollection<TreeNodeViewModel>();
  19. public TreeNodeViewModel(TreeNode treeNode, TreeNodeViewModel parent)
  20. {
  21. this.num = globalNum++;
  22. this.treeNode = treeNode;
  23. this.parent = parent ?? this;
  24. if (this.parent == this)
  25. {
  26. this.connectorX2 = 0;
  27. this.connectorY2 = Height / 2;
  28. }
  29. else
  30. {
  31. this.connectorX2 = Width + this.Parent.X - this.X;
  32. this.connectorY2 = Height / 2 + this.Parent.Y - this.Y;
  33. }
  34. }
  35. public int Num
  36. {
  37. get
  38. {
  39. return this.num;
  40. }
  41. }
  42. public static double Width
  43. {
  44. get
  45. {
  46. return width;
  47. }
  48. }
  49. public static double Height
  50. {
  51. get
  52. {
  53. return height;
  54. }
  55. }
  56. public bool IsRoot
  57. {
  58. get
  59. {
  60. return this.Parent == this;
  61. }
  62. }
  63. public double Prelim
  64. {
  65. get
  66. {
  67. return this.prelim;
  68. }
  69. set
  70. {
  71. this.prelim = value;
  72. this.RaisePropertyChanged("Prelim");
  73. }
  74. }
  75. public double Modify
  76. {
  77. get
  78. {
  79. return this.modify;
  80. }
  81. set
  82. {
  83. this.RaisePropertyChanged("Modify");
  84. this.modify = value;
  85. }
  86. }
  87. public double X
  88. {
  89. get
  90. {
  91. return this.treeNode.X;
  92. }
  93. set
  94. {
  95. if (this.treeNode.X == value)
  96. {
  97. return;
  98. }
  99. this.treeNode.X = value;
  100. this.RaisePropertyChanged("X");
  101. this.ConnectorX2 = Width / 2 + this.Parent.X - this.X;
  102. foreach (TreeNodeViewModel child in this.Children)
  103. {
  104. child.ConnectorX2 = Width / 2 + this.treeNode.X - child.X;
  105. }
  106. }
  107. }
  108. public double Y
  109. {
  110. get
  111. {
  112. return this.treeNode.Y;
  113. }
  114. set
  115. {
  116. if (this.treeNode.Y == value)
  117. {
  118. return;
  119. }
  120. this.treeNode.Y = value;
  121. this.RaisePropertyChanged("Y");
  122. this.ConnectorY2 = Height + this.Parent.Y - this.Y;
  123. foreach (var child in this.Children)
  124. {
  125. child.ConnectorY2 = Height + this.treeNode.Y - child.Y;
  126. }
  127. }
  128. }
  129. public double ConnectorX1
  130. {
  131. get
  132. {
  133. return Width / 2;
  134. }
  135. }
  136. public double ConnectorY1
  137. {
  138. get
  139. {
  140. return 0;
  141. }
  142. }
  143. public double ConnectorX2
  144. {
  145. get
  146. {
  147. return this.IsRoot ? width / 2 : this.connectorX2;
  148. }
  149. set
  150. {
  151. this.connectorX2 = value;
  152. this.RaisePropertyChanged("ConnectorX2");
  153. }
  154. }
  155. public double ConnectorY2
  156. {
  157. get
  158. {
  159. return this.IsRoot ? 0 : this.connectorY2;
  160. }
  161. set
  162. {
  163. this.connectorY2 = value;
  164. this.RaisePropertyChanged("ConnectorY2");
  165. }
  166. }
  167. public int Type
  168. {
  169. get
  170. {
  171. return this.treeNode.Type;
  172. }
  173. set
  174. {
  175. if (this.treeNode.Type == value)
  176. {
  177. return;
  178. }
  179. this.treeNode.Type = value;
  180. this.RaisePropertyChanged("Type");
  181. }
  182. }
  183. public TreeNodeViewModel Parent
  184. {
  185. get
  186. {
  187. return this.parent;
  188. }
  189. set
  190. {
  191. this.parent = value;
  192. }
  193. }
  194. public ObservableCollection<TreeNodeViewModel> Children
  195. {
  196. get
  197. {
  198. return this.children;
  199. }
  200. set
  201. {
  202. this.children = value;
  203. }
  204. }
  205. public TreeNodeViewModel LeftSibling
  206. {
  207. get
  208. {
  209. if (this.IsRoot)
  210. {
  211. return null;
  212. }
  213. int index = this.Parent.Children.IndexOf(this);
  214. return index == 0 ? null : this.Parent.Children[index - 1];
  215. }
  216. }
  217. public int Index
  218. {
  219. get
  220. {
  221. return this.IsRoot ? 0 : this.Parent.Children.IndexOf(this);
  222. }
  223. }
  224. public TreeNodeViewModel LastChild
  225. {
  226. get
  227. {
  228. if (this.Children.Count == 0)
  229. {
  230. return null;
  231. }
  232. int maxIndex = this.Children.Count - 1;
  233. return this.Children[maxIndex];
  234. }
  235. }
  236. public TreeNodeViewModel FirstChild
  237. {
  238. get
  239. {
  240. return this.Children.Count == 0 ? null : this.Children[0];
  241. }
  242. }
  243. public bool IsLeaf
  244. {
  245. get
  246. {
  247. return this.Children.Count == 0;
  248. }
  249. }
  250. public double AncestorModify
  251. {
  252. get
  253. {
  254. return this.ancestorModify;
  255. }
  256. set
  257. {
  258. this.ancestorModify = value;
  259. }
  260. }
  261. }
  262. }