PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/SilverlightMultiTouch/MultiTouch.Behaviors.WP7/MultiTouchBehavior.WP7.Debug.cs

#
C# | 318 lines | 262 code | 42 blank | 14 comment | 49 complexity | 2af5594bf48da09125068652a5f46872 MD5 | raw file
  1. // ****************************************************************************
  2. // <copyright file="MultiTouchBehavior.WP7.Debug.cs" company="Laurent Bugnion">
  3. // Copyright © Laurent Bugnion 2010
  4. // </copyright>
  5. // ****************************************************************************
  6. // <author>Laurent Bugnion</author>
  7. // <email>laurent@galasoft.ch</email>
  8. // <date>20.6.2010</date>
  9. // <project>MultiTouch.Behaviors.WP7</project>
  10. // <web>http://multitouch.codeplex.com/</web>
  11. // <license>
  12. // See http://multitouch.codeplex.com/license.
  13. // </license>
  14. // ****************************************************************************
  15. using System;
  16. using System.Collections.Generic;
  17. using System.ComponentModel;
  18. using System.Linq;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using System.Windows.Shapes;
  24. using Microsoft.Phone.Controls;
  25. namespace MultiTouch.Behaviors.WP7
  26. {
  27. #if DEBUG
  28. public partial class MultiTouchBehavior
  29. {
  30. private enum DebugInfo
  31. {
  32. [Description("Scale: {0:N2}")]
  33. Scale,
  34. [Description("Rotate: {0:N2}")]
  35. Rotate,
  36. [Description("Translate X | Y: {0:N2} | {1:N2}")]
  37. Translate,
  38. [Description("Finger: {0}")]
  39. FingerPosition,
  40. [Description("Active: {0}")]
  41. IsManipulationActive,
  42. [Description("S | R | Tx | Ty on: {0} | {1} | {2} | {3}")]
  43. ScaleRotateTranslateEnabled,
  44. [Description("MinScale: {0}")]
  45. MinimumScale,
  46. [Description("MaxScale: {0}")]
  47. MaximumScale,
  48. }
  49. private const double DebugMarkerSize = 100;
  50. private const string DebugTextPrefix = "DebugText";
  51. private bool _attached;
  52. private Canvas _debugCanvas;
  53. private readonly List<Ellipse> _fingerMarkers = new List<Ellipse>();
  54. private void OutputDebugInfo(DebugInfo info, params object[] values)
  55. {
  56. if (!_isDebugModeActive
  57. || _debugCanvas == null)
  58. {
  59. return;
  60. }
  61. var text = _debugCanvas.FindName(DebugTextPrefix + info) as TextBlock;
  62. if (text == null
  63. || text.Tag == null)
  64. {
  65. return;
  66. }
  67. text.Text = string.Format(text.Tag.ToString(), values);
  68. }
  69. private void OutputInitialDebugInfo(DebugInfo index, TextBlock value)
  70. {
  71. if (!_isDebugModeActive)
  72. {
  73. return;
  74. }
  75. ManipulationStarted += (s, e) => OutputDebugInfo(DebugInfo.IsManipulationActive, true);
  76. ManipulationCompleted += (s, e) => OutputDebugInfo(DebugInfo.IsManipulationActive, false);
  77. switch (index)
  78. {
  79. case DebugInfo.Scale:
  80. value.Text = string.Format(value.Tag.ToString(), 1);
  81. break;
  82. case DebugInfo.Rotate:
  83. value.Text = string.Format(value.Tag.ToString(), 0);
  84. break;
  85. case DebugInfo.Translate:
  86. value.Text = string.Format(value.Tag.ToString(), 0, 0);
  87. break;
  88. case DebugInfo.ScaleRotateTranslateEnabled:
  89. value.Text = string.Format(
  90. value.Tag.ToString(),
  91. IsScaleEnabled,
  92. IsRotateEnabled,
  93. IsTranslateXEnabled,
  94. IsTranslateYEnabled);
  95. break;
  96. case DebugInfo.MaximumScale:
  97. value.Text = string.Format(value.Tag.ToString(), MaximumScale);
  98. break;
  99. case DebugInfo.MinimumScale:
  100. value.Text = string.Format(value.Tag.ToString(), MinimumScale);
  101. break;
  102. case DebugInfo.IsManipulationActive:
  103. value.Text = string.Format(value.Tag.ToString(), false);
  104. break;
  105. }
  106. }
  107. public void AttachDebugMode()
  108. {
  109. if (_debugCanvas == null)
  110. {
  111. var parent = VisualTreeHelper.GetParent(AssociatedObject);
  112. Panel rootPanel = null;
  113. while (parent != null)
  114. {
  115. var parentParent = VisualTreeHelper.GetParent(parent);
  116. if (parentParent != null
  117. && parentParent is PhoneApplicationPage)
  118. {
  119. rootPanel = parent as Panel;
  120. break;
  121. }
  122. parent = parentParent;
  123. }
  124. if (rootPanel != null
  125. && (rootPanel is Grid
  126. || rootPanel is Canvas))
  127. {
  128. _debugCanvas = new Canvas
  129. {
  130. VerticalAlignment = VerticalAlignment.Top,
  131. HorizontalAlignment = HorizontalAlignment.Left,
  132. IsHitTestVisible = false
  133. };
  134. rootPanel.Children.Add(_debugCanvas);
  135. }
  136. }
  137. if (_debugCanvas == null)
  138. {
  139. throw new InvalidOperationException("Cannot enter debug mode, check the documentation");
  140. }
  141. if (_isDebugModeActive)
  142. {
  143. var foregroundBrush = new SolidColorBrush(Colors.Red);
  144. var backgroundBrush = new SolidColorBrush(Color.FromArgb(128, 255, 255, 255));
  145. var enumType = typeof(DebugInfo);
  146. for (var index = DebugInfo.Scale; index <= DebugInfo.MaximumScale; index++)
  147. {
  148. var panel = new StackPanel
  149. {
  150. Height = 47,
  151. Background = backgroundBrush,
  152. Orientation = Orientation.Horizontal
  153. };
  154. var info = enumType.GetField(index.ToString());
  155. var attribute = (DescriptionAttribute)info.GetCustomAttributes(typeof(DescriptionAttribute), false)[0];
  156. var value = new TextBlock
  157. {
  158. FontSize = 24,
  159. Foreground = foregroundBrush,
  160. Name = DebugTextPrefix + index,
  161. Tag = attribute.Description
  162. };
  163. OutputInitialDebugInfo(index, value);
  164. Canvas.SetTop(panel, (int)index * 47.0 + 50);
  165. panel.Children.Add(value);
  166. _debugCanvas.Children.Add(panel);
  167. }
  168. _attached = true;
  169. }
  170. }
  171. internal void HandleMockDebugInfoAndFingers(
  172. IEnumerable<ITouchPoint> touchpoints,
  173. ITouchPoint primaryTouchPoint)
  174. {
  175. if (primaryTouchPoint == null
  176. || touchpoints == null
  177. || touchpoints.Count() == 0)
  178. {
  179. return;
  180. }
  181. CheckAttachDebugMode();
  182. ExecuteHandleDebugInfoAndFingers(
  183. touchpoints.Select(tp => tp.Position).ToArray(),
  184. touchpoints.Select(tp => tp.Action).ToArray(),
  185. primaryTouchPoint.Position);
  186. }
  187. private void HandleDebugInfoAndFingers(
  188. IEnumerable<TouchPoint> touchpoints,
  189. TouchPoint primaryTouchPoint)
  190. {
  191. if (primaryTouchPoint == null
  192. || touchpoints == null
  193. || touchpoints.Count() == 0)
  194. {
  195. return;
  196. }
  197. CheckAttachDebugMode();
  198. ExecuteHandleDebugInfoAndFingers(
  199. touchpoints.Select(tp => tp.Position).ToArray(),
  200. touchpoints.Select(tp => tp.Action).ToArray(),
  201. primaryTouchPoint.Position);
  202. }
  203. private void CheckAttachDebugMode()
  204. {
  205. if (!_attached)
  206. {
  207. if (_isDebugModeActive || _areFingersVisible)
  208. {
  209. AttachDebugMode();
  210. }
  211. }
  212. }
  213. private void ExecuteHandleDebugInfoAndFingers(
  214. IList<Point> positions,
  215. IList<TouchAction> actions,
  216. Point primaryPosition)
  217. {
  218. if (!_areFingersVisible)
  219. {
  220. return;
  221. }
  222. var lastIndex = 0;
  223. var debugInfo = string.Empty;
  224. for (var index = 0; index < positions.Count; index++)
  225. {
  226. var isMain = positions[index] == primaryPosition;
  227. if (_fingerMarkers.Count <= index)
  228. {
  229. var fill = isMain
  230. ? new SolidColorBrush(Colors.Red)
  231. : new SolidColorBrush(Colors.Blue);
  232. var debugMarker = new Ellipse
  233. {
  234. Height = DebugMarkerSize,
  235. Width = DebugMarkerSize,
  236. Opacity = 0.5,
  237. IsHitTestVisible = false,
  238. Fill = fill
  239. };
  240. _debugCanvas.Children.Add(debugMarker);
  241. _fingerMarkers.Add(debugMarker);
  242. }
  243. if (actions[index] == TouchAction.Down
  244. || actions[index] == TouchAction.Move)
  245. {
  246. _fingerMarkers[index].Visibility = Visibility.Visible;
  247. var centerPoint = positions[index];
  248. var actualPoint = new Point(
  249. centerPoint.X - DebugMarkerSize / 2,
  250. centerPoint.Y - DebugMarkerSize / 2);
  251. Canvas.SetLeft(_fingerMarkers[index], actualPoint.X);
  252. Canvas.SetTop(_fingerMarkers[index], actualPoint.Y);
  253. if (isMain)
  254. {
  255. debugInfo += "[" + centerPoint + "]";
  256. }
  257. else
  258. {
  259. debugInfo += "(" + centerPoint + ")";
  260. }
  261. }
  262. else
  263. {
  264. _fingerMarkers[index].Visibility = Visibility.Collapsed;
  265. }
  266. lastIndex = index;
  267. }
  268. for (var index = lastIndex + 1; index < _fingerMarkers.Count; index++)
  269. {
  270. _fingerMarkers[index].Visibility = Visibility.Collapsed;
  271. }
  272. OutputDebugInfo(DebugInfo.FingerPosition, debugInfo);
  273. }
  274. }
  275. #endif
  276. }