Guest User

Untitled

a guest
Jul 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. public class ControlTouchTracker
  2. {
  3. private List<FrameworkElement> controls = new List<FrameworkElement>();
  4. private Dictionary<FrameworkElement, ControlRegion> controlBounds = new Dictionary<FrameworkElement, ControlRegion>();
  5.  
  6. public ControlTouchTracker(FrameworkElement rootElement)
  7. {
  8. rootElement.LayoutUpdated += this.OnLayoutUpdated;
  9. }
  10.  
  11. public void RegisterControl(FrameworkElement control)
  12. {
  13. controls.Add(control);
  14. }
  15.  
  16. public void RemoveControl(FrameworkElement control)
  17. {
  18. controls.Remove(control);
  19. controlBounds.Remove(control);
  20. }
  21.  
  22. private void OnLayoutUpdated(object sender, EventArgs e)
  23. {
  24. foreach (Control control in this.controls)
  25. {
  26. this.RefreshControlBounds(control);
  27. }
  28. }
  29.  
  30. private void RefreshControlBounds(FrameworkElement control)
  31. {
  32. if (control.Visibility == Visibility.Visible)
  33. {
  34. GeneralTransform controlTransform = control.TransformToVisual(Application.Current.RootVisual);
  35. Point offset = controlTransform.Transform(new Point(0, 0));
  36.  
  37. this.controlBounds[control] = new ControlRegion
  38. {
  39. Left = (float)offset.X,
  40. Right = (float)(offset.X + control.ActualWidth),
  41. Top = (float)offset.Y,
  42. Bottom = (float)(offset.Y + control.ActualHeight)
  43. };
  44. }
  45. else
  46. {
  47. if (this.controlBounds.ContainsKey(control))
  48. {
  49. this.controlBounds.Remove(control);
  50. }
  51. }
  52. }
  53.  
  54. public bool TouchesControl(Vector2 touchPosition)
  55. {
  56. foreach (ControlRegion region in this.controlBounds.Values)
  57. {
  58. if (touchPosition.X >= region.Left && touchPosition.X <= region.Right &&
  59. touchPosition.Y >= region.Top && touchPosition.Y <= region.Bottom)
  60. {
  61. return true;
  62. }
  63. }
  64.  
  65. return false;
  66. }
  67.  
  68. public class ControlRegion
  69. {
  70. public float Left { get; set; }
  71. public float Right { get; set; }
  72. public float Top { get; set; }
  73. public float Bottom { get; set; }
  74. }
  75. }
  76.  
  77. if (TouchPanel.IsGestureAvailable)
  78. {
  79. if (TouchPanel.ReadGesture().GestureType == GestureType.Tap)
  80. {
  81. if (TouchPanel.ReadGesture().Position == new Vector2(120, 120))
  82. {
  83.  
  84. }
  85. }
  86. }
Add Comment
Please, Sign In to add comment