TreeNodeViewModel.cs 4.2 KB

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