Exemple #1
0
 void OnNewContact(object sender, NewContactEventArgs e)
 {
     if (ClickMode != ClickMode.Hover)
     {
         e.Handled = true;
         if (e.Contact.Capture(this))
         {
             IsPressed = true;
             if (ClickMode == ClickMode.Press && MultitouchScreen.GetContactsCaptured(this).Count() == 1)
             {
                 bool failed = true;
                 try
                 {
                     OnClick();
                     failed = false;
                 }
                 finally
                 {
                     if (failed)
                     {
                         IsPressed = false;
                         e.Contact.ReleaseCapture();
                     }
                 }
             }
         }
     }
 }
Exemple #2
0
        public void MultipleContactsCaptureSameObject()
        {
            Run(() =>
            {
                window             = new TestWindow();
                window.NewContact += HandleEvent;
                window.Show();

                HwndSource source = (HwndSource)PresentationSource.FromVisual(window);

                Assert.AreEqual(0, MultitouchScreen.GetContactsCaptured(window.testElement2).Count());
                Assert.AreEqual(0, MultitouchScreen.GetContactsCaptured(window.testElement).Count());
                window.ExecuteOnNextContact(c => c.Capture(window.testElement));

                RawMultitouchReport report = new RawMultitouchReport(CreateContact(GetContactData(0, ContactState.New, new Point(100, 100)), 0, source));
                InputManager.Current.ProcessInput(report);

                Assert.AreEqual(1, MultitouchScreen.GetContactsCaptured(window.testElement).Count());
                window.ExecuteOnNextContact(c => c.Capture(window.testElement));

                report = new RawMultitouchReport(CreateContact(GetContactData(1, ContactState.New, new Point(100, 100)), 1, source));
                InputManager.Current.ProcessInput(report);

                Assert.AreEqual(2, MultitouchScreen.GetContactsCaptured(window.testElement).Count());
                Assert.AreEqual(0, MultitouchScreen.GetContactsCaptured(window.testElement2).Count());

                Dispatcher.ExitAllFrames();
                resetEvent.Set();
            });
            resetEvent.WaitOne();
        }
Exemple #3
0
 void OnContactRemoved(object sender, ContactEventArgs e)
 {
     if (ClickMode != ClickMode.Hover)
     {
         e.Handled = true;
         if (e.Contact.Captured == this)
         {
             e.Contact.ReleaseCapture();
             if (MultitouchScreen.GetContactsCaptured(this).Count() == 0)
             {
                 StopTimer();
                 IsPressed = false;
             }
         }
     }
 }
Exemple #4
0
 void OnContactRemoved(object sender, ContactEventArgs e)
 {
     if (ClickMode != ClickMode.Hover)
     {
         e.Handled = true;
         if (e.Contact.Captured == this)
         {
             bool shouldMakeClick = IsPressed && ClickMode == ClickMode.Release;
             e.Contact.ReleaseCapture();
             if (MultitouchScreen.GetContactsCaptured(this).Count() == 0)
             {
                 if (shouldMakeClick)
                 {
                     OnClick();
                 }
                 IsPressed = false;
             }
         }
     }
 }
        void OnContactMoved(object sender, ContactEventArgs e)
        {
            Point position = e.GetPosition(this);

            FixedHingeJoint joint;

            if (contactJoints.TryGetValue(e.Contact.Id, out joint))
            {
                joint.Anchor = position.ToVector2D();

                //scale
                Body             body             = joint.Bodies.First();
                FrameworkElement frameworkElement = body.Tag as FrameworkElement;
                if (frameworkElement != null && GetIsScalable(frameworkElement))
                {
                    ScaleState state;
                    if (elementToScale.TryGetValue(frameworkElement, out state))
                    {
                        IEnumerable <Contact> contacts = MultitouchScreen.GetContactsCaptured((IInputElement)e.Source);
                        double    previousDistance     = 0;
                        double    currentDistance      = 0;
                        int       divisor       = 0;
                        Contact[] contactsArray = contacts.ToArray();

                        Point center = new Point(frameworkElement.ActualWidth / 2, frameworkElement.ActualHeight / 2);

                        for (int i = 0; i < contactsArray.Length; i++)
                        {
                            for (int j = i + 1; j < contactsArray.Length; j++)
                            {
                                Point  currFirst  = contactsArray[j].GetPosition(this);
                                Point  currSecond = contactsArray[i].GetPosition(this);
                                Vector vector     = frameworkElement.PointFromScreen(currFirst) - frameworkElement.PointFromScreen(currSecond);
                                currentDistance += vector.Length;

                                Point prevFirst = contactsArray[j].GetPoints(this).FirstOrDefault();
                                if (default(Point) == prevFirst)
                                {
                                    prevFirst = currFirst;
                                }
                                Point prevSecond = contactsArray[i].GetPoints(this).FirstOrDefault();
                                if (default(Point) == prevSecond)
                                {
                                    prevSecond = currSecond;
                                }
                                Vector previousVector = frameworkElement.PointFromScreen(prevFirst) - frameworkElement.PointFromScreen(prevSecond);
                                previousDistance += previousVector.Length;
                                divisor++;
                            }
                        }
                        if (divisor == 0)
                        {
                            divisor = 1;
                        }

                        previousDistance /= divisor;
                        currentDistance  /= divisor;

                        double delta = currentDistance / previousDistance;
                        if (double.IsNaN(delta))
                        {
                            delta = 1;
                        }

                        var newScale = state.Scale * delta;
                        if (newScale > MaxScale)
                        {
                            delta = MaxScale / state.Scale;
                        }
                        else if (newScale < MinScale)
                        {
                            delta = MinScale / state.Scale;
                        }

                        state.Scale         *= delta;
                        state.Center         = center;
                        body.Transformation *= Matrix2x3.FromScale(new Vector2D(delta, delta));
                    }
                }
            }
        }