TreeNodeViewModel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System.Collections.ObjectModel;
  2. using Microsoft.Practices.Prism.ViewModel;
  3. namespace Modules.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. }
  73. }
  74. public double Modify
  75. {
  76. get
  77. {
  78. return this.modify;
  79. }
  80. set
  81. {
  82. this.modify = value;
  83. }
  84. }
  85. public double X
  86. {
  87. get
  88. {
  89. return this.treeNode.X;
  90. }
  91. set
  92. {
  93. if (this.treeNode.X == value)
  94. {
  95. return;
  96. }
  97. this.treeNode.X = value;
  98. this.RaisePropertyChanged("X");
  99. this.ConnectorX2 = Width / 2 + this.Parent.X - this.X;
  100. foreach (TreeNodeViewModel child in this.Children)
  101. {
  102. child.ConnectorX2 = Width / 2 + this.treeNode.X - child.X;
  103. }
  104. }
  105. }
  106. public double Y
  107. {
  108. get
  109. {
  110. return this.treeNode.Y;
  111. }
  112. set
  113. {
  114. if (this.treeNode.Y == value)
  115. {
  116. return;
  117. }
  118. this.treeNode.Y = value;
  119. this.RaisePropertyChanged("Y");
  120. this.ConnectorY2 = Height + this.Parent.Y - this.Y;
  121. foreach (var child in this.Children)
  122. {
  123. child.ConnectorY2 = Height + this.treeNode.Y - child.Y;
  124. }
  125. }
  126. }
  127. public double ConnectorX1
  128. {
  129. get
  130. {
  131. return Width / 2;
  132. }
  133. }
  134. public double ConnectorY1
  135. {
  136. get
  137. {
  138. return 0;
  139. }
  140. }
  141. public double ConnectorX2
  142. {
  143. get
  144. {
  145. return this.IsRoot? width / 2 : this.connectorX2;
  146. }
  147. set
  148. {
  149. this.connectorX2 = value;
  150. this.RaisePropertyChanged("ConnectorX2");
  151. }
  152. }
  153. public double ConnectorY2
  154. {
  155. get
  156. {
  157. return this.IsRoot? 0 : this.connectorY2;
  158. }
  159. set
  160. {
  161. this.connectorY2 = value;
  162. this.RaisePropertyChanged("ConnectorY2");
  163. }
  164. }
  165. public int Type
  166. {
  167. get
  168. {
  169. return this.treeNode.Type;
  170. }
  171. set
  172. {
  173. if (this.treeNode.Type == value)
  174. {
  175. return;
  176. }
  177. this.treeNode.Type = value;
  178. this.RaisePropertyChanged("Type");
  179. }
  180. }
  181. public TreeNodeViewModel Parent
  182. {
  183. get
  184. {
  185. return this.parent;
  186. }
  187. set
  188. {
  189. this.parent = value;
  190. }
  191. }
  192. public ObservableCollection<TreeNodeViewModel> Children
  193. {
  194. get
  195. {
  196. return this.children;
  197. }
  198. set
  199. {
  200. this.children = value;
  201. }
  202. }
  203. public TreeNodeViewModel LeftSibling
  204. {
  205. get
  206. {
  207. if (this.IsRoot)
  208. {
  209. return null;
  210. }
  211. int index = this.Parent.Children.IndexOf(this);
  212. return index == 0? null : this.Parent.Children[index - 1];
  213. }
  214. }
  215. public TreeNodeViewModel LastChild
  216. {
  217. get
  218. {
  219. if (this.Children.Count == 0)
  220. {
  221. return null;
  222. }
  223. int maxIndex = this.Children.Count - 1;
  224. return this.Children[maxIndex];
  225. }
  226. }
  227. public TreeNodeViewModel FirstChild
  228. {
  229. get
  230. {
  231. return this.Children.Count == 0? null : this.Children[0];
  232. }
  233. }
  234. public bool IsLeaf
  235. {
  236. get
  237. {
  238. return this.Children.Count == 0;
  239. }
  240. }
  241. public double AncestorModify
  242. {
  243. get
  244. {
  245. return this.ancestorModify;
  246. }
  247. set
  248. {
  249. this.ancestorModify = value;
  250. }
  251. }
  252. }
  253. }