BehaviorPanel.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. using System.Windows.Shapes;
  6. namespace BehaviorTree
  7. {
  8. public class BehaviorPanel : Panel
  9. {
  10. private LayeredTreeDraw ltd;
  11. private int iNextNameSuffix = 0;
  12. private Path pthConnections = null;
  13. public static readonly DependencyProperty VerticalBufferProperty =
  14. DependencyProperty.Register(
  15. "VerticalBuffer",
  16. typeof(double),
  17. typeof(BehaviorPanel),
  18. null
  19. );
  20. public static readonly DependencyProperty VerticalJustificationProperty =
  21. DependencyProperty.Register(
  22. "VerticalJustification",
  23. typeof(VerticalJustification),
  24. typeof(BehaviorPanel),
  25. null
  26. );
  27. public readonly static DependencyProperty HorizontalBufferSubtreeProperty =
  28. DependencyProperty.Register(
  29. "HorizontalBufferSubtree",
  30. typeof(double),
  31. typeof(BehaviorPanel),
  32. null
  33. );
  34. public readonly static DependencyProperty HorizontalBufferProperty =
  35. DependencyProperty.Register(
  36. "HorizontalBuffer",
  37. typeof(double),
  38. typeof(BehaviorPanel),
  39. null
  40. );
  41. public static readonly DependencyProperty RootProperty =
  42. DependencyProperty.Register(
  43. "Root",
  44. typeof(String),
  45. typeof(BehaviorPanel),
  46. null
  47. );
  48. private static Point PtFromPoint(Point dpt)
  49. {
  50. return new Point(dpt.X, dpt.Y);
  51. }
  52. public string Root
  53. {
  54. get
  55. {
  56. return (string)GetValue(RootProperty);
  57. }
  58. set
  59. {
  60. SetValue(RootProperty, value);
  61. }
  62. }
  63. public VerticalJustification VerticalJustification
  64. {
  65. get
  66. {
  67. return (VerticalJustification)GetValue(VerticalJustificationProperty);
  68. }
  69. set
  70. {
  71. SetValue(VerticalJustificationProperty, value);
  72. }
  73. }
  74. public double VerticalBuffer
  75. {
  76. get { return (double)GetValue(VerticalBufferProperty); }
  77. set { SetValue(VerticalBufferProperty, value); }
  78. }
  79. public double HorizontalBufferSubtree
  80. {
  81. get { return (double)GetValue(HorizontalBufferSubtreeProperty); }
  82. set { SetValue(HorizontalBufferSubtreeProperty, value); }
  83. }
  84. public double HorizontalBuffer
  85. {
  86. get { return (double)GetValue(HorizontalBufferProperty); }
  87. set { SetValue(HorizontalBufferProperty, value); }
  88. }
  89. private void SetParents(TreeNode tnRoot)
  90. {
  91. // First pass to clear all parents
  92. foreach (UIElement uiel in Children)
  93. {
  94. TreeNode tn = uiel as TreeNode;
  95. if (tn != null)
  96. {
  97. tn.ClearParent();
  98. }
  99. }
  100. // Second pass to properly set them from their children...
  101. foreach (UIElement uiel in Children)
  102. {
  103. TreeNode tn = uiel as TreeNode;
  104. if (tn != null && tn != tnRoot)
  105. {
  106. tn.SetParent();
  107. }
  108. }
  109. }
  110. public void Clear()
  111. {
  112. Children.Clear();
  113. pthConnections = null;
  114. }
  115. private void SetName(TreeNode tn, string strName)
  116. {
  117. tn.Name = strName;
  118. tn.SetValue(Panel.NameProperty, strName);
  119. }
  120. public TreeNode AddRoot(Object objContent, string strName)
  121. {
  122. TreeNode tnNew = new TreeNode();
  123. SetName(tnNew, strName);
  124. tnNew.Content = objContent;
  125. Children.Add(tnNew);
  126. Root = strName;
  127. return tnNew;
  128. }
  129. public TreeNode AddRoot(Object objContent)
  130. {
  131. return AddRoot(objContent, StrNextName());
  132. }
  133. public TreeNode AddNode(Object objContent, string strName, string strParent)
  134. {
  135. TreeNode tnNew = new TreeNode();
  136. SetName(tnNew, strName);
  137. tnNew.Content = objContent;
  138. tnNew.TreeParent = strParent;
  139. Children.Add(tnNew);
  140. return tnNew;
  141. }
  142. private string StrNextName()
  143. {
  144. return "__TreeNode" + iNextNameSuffix++;
  145. }
  146. public TreeNode AddNode(Object objContent, string strName, TreeNode tnParent)
  147. {
  148. return AddNode(objContent, strName, tnParent.Name);
  149. }
  150. public TreeNode AddNode(Object objContent, TreeNode tnParent)
  151. {
  152. return AddNode(objContent, StrNextName(), tnParent.Name);
  153. }
  154. protected override Size MeasureOverride(Size availableSize)
  155. {
  156. if (Children.Count == 0)
  157. {
  158. return new Size(100, 20);
  159. }
  160. if (pthConnections != null && Children.Contains(pthConnections))
  161. {
  162. Children.Remove(pthConnections);
  163. pthConnections = null;
  164. }
  165. Size szFinal = new Size(0, 0);
  166. string strRoot = Root;
  167. TreeNode tnRoot = this.FindName(strRoot) as TreeNode;
  168. foreach (UIElement uiel in Children)
  169. {
  170. uiel.Measure(availableSize);
  171. Size szThis = uiel.DesiredSize;
  172. if (szThis.Width > szFinal.Width || szThis.Height > szFinal.Height)
  173. {
  174. szFinal = new Size(
  175. Math.Max(szThis.Width, szFinal.Width),
  176. Math.Max(szThis.Height, szFinal.Height));
  177. }
  178. }
  179. if (tnRoot != null)
  180. {
  181. SetParents(tnRoot);
  182. ltd = new LayeredTreeDraw(tnRoot, HorizontalBuffer, HorizontalBufferSubtree, VerticalBuffer, VerticalJustification.TOP);
  183. ltd.LayoutTree();
  184. szFinal = new Size(ltd.PxOverallWidth, ltd.PxOverallHeight);
  185. }
  186. // Put in the connections too...
  187. if (ltd.Connections != null)
  188. {
  189. pthConnections = new Path();
  190. PathGeometry pg = new PathGeometry();
  191. pthConnections.Stroke = new SolidColorBrush(Colors.Black);
  192. pthConnections.StrokeThickness = 1.0;
  193. pg.Figures = new PathFigureCollection();
  194. foreach (TreeConnection tcn in ltd.Connections)
  195. {
  196. PathFigure pf = new PathFigure();
  197. pf.StartPoint = PtFromPoint(tcn.LstPt[0]);
  198. pf.IsClosed = false;
  199. pf.Segments = new PathSegmentCollection();
  200. for (int iPt = 1; iPt < tcn.LstPt.Count; iPt++)
  201. {
  202. LineSegment ls = new LineSegment();
  203. ls.Point = PtFromPoint(tcn.LstPt[iPt]);
  204. pf.Segments.Add(ls);
  205. }
  206. pg.Figures.Add(pf);
  207. }
  208. pthConnections.Data = pg;
  209. Children.Add(pthConnections);
  210. pthConnections.Measure(availableSize);
  211. }
  212. return szFinal;
  213. }
  214. protected override Size ArrangeOverride(Size finalSize)
  215. {
  216. foreach (UIElement uiel in Children)
  217. {
  218. TreeNode tn = uiel as TreeNode;
  219. Point ptLocation = new Point(0, 0);
  220. if (tn != null)
  221. {
  222. ptLocation = new Point(ltd.X(tn), ltd.Y(tn));
  223. }
  224. uiel.Arrange(new Rect(ptLocation, uiel.DesiredSize));
  225. }
  226. return finalSize;
  227. }
  228. }
  229. }