TreeNodeViewModel.cs 4.3 KB

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