Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ControlTouchTracker
- {
- private List<FrameworkElement> controls = new List<FrameworkElement>();
- private Dictionary<FrameworkElement, ControlRegion> controlBounds = new Dictionary<FrameworkElement, ControlRegion>();
- public ControlTouchTracker(FrameworkElement rootElement)
- {
- rootElement.LayoutUpdated += this.OnLayoutUpdated;
- }
- public void RegisterControl(FrameworkElement control)
- {
- controls.Add(control);
- }
- public void RemoveControl(FrameworkElement control)
- {
- controls.Remove(control);
- controlBounds.Remove(control);
- }
- private void OnLayoutUpdated(object sender, EventArgs e)
- {
- foreach (Control control in this.controls)
- {
- this.RefreshControlBounds(control);
- }
- }
- private void RefreshControlBounds(FrameworkElement control)
- {
- if (control.Visibility == Visibility.Visible)
- {
- GeneralTransform controlTransform = control.TransformToVisual(Application.Current.RootVisual);
- Point offset = controlTransform.Transform(new Point(0, 0));
- this.controlBounds[control] = new ControlRegion
- {
- Left = (float)offset.X,
- Right = (float)(offset.X + control.ActualWidth),
- Top = (float)offset.Y,
- Bottom = (float)(offset.Y + control.ActualHeight)
- };
- }
- else
- {
- if (this.controlBounds.ContainsKey(control))
- {
- this.controlBounds.Remove(control);
- }
- }
- }
- public bool TouchesControl(Vector2 touchPosition)
- {
- foreach (ControlRegion region in this.controlBounds.Values)
- {
- if (touchPosition.X >= region.Left && touchPosition.X <= region.Right &&
- touchPosition.Y >= region.Top && touchPosition.Y <= region.Bottom)
- {
- return true;
- }
- }
- return false;
- }
- public class ControlRegion
- {
- public float Left { get; set; }
- public float Right { get; set; }
- public float Top { get; set; }
- public float Bottom { get; set; }
- }
- }
- if (TouchPanel.IsGestureAvailable)
- {
- if (TouchPanel.ReadGesture().GestureType == GestureType.Tap)
- {
- if (TouchPanel.ReadGesture().Position == new Vector2(120, 120))
- {
- }
- }
- }
Add Comment
Please, Sign In to add comment