TreeNodeViewModel.cs 3.8 KB

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