Click here to Skip to main content
15,851,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#




C#
using System;
using System.Windows;
using System.Windows.Media.Imaging;
using Leap;
using System.Windows.Threading;
using System.IO;
using System.Collections;
using System.Linq;

namespace T7TestSwipeGesture
{
    public partial class MainWindow : Window, ILeapEventDelegate
    {

        private Controller controller = new Controller();



        private LeapListener listener;

        //  private int countSwipe = 0;

        //  private BitmapImage imageSource;

        private static BitmapImage[] images = new BitmapImage[102];

        private static Frame lastFrame = new Frame();


        public MainWindow()
        {
            InitializeComponent();

            this.controller = new Controller();

            this.listener = new LeapListener(this);
            controller.AddListener(listener);


            for (int iCount = 1; iCount < 23; iCount++)
            {
                images[iCount] = new BitmapImage();
                images[iCount].BeginInit();
                images[iCount].UriSource = new Uri("Images/" + iCount + ".png", UriKind.Relative);
                images[iCount].EndInit();
            }
            image.Source = images[1];
        }



        delegate void LeapEventDelegate(string EventName);




        public void LeapEventNotification(string EventName)
        {
            if (this.CheckAccess())
            {
                switch (EventName)
                {
                    case "onInit":

                        break;
                    case "onConnect":

                        this.connectHandler();
                        break;
                    case "onFrame":

                        this.checkGestures(this.controller.Frame());

                        break;
                }
            }
            else
            {
                Dispatcher.Invoke(new LeapEventDelegate(LeapEventNotification
                    ), new object[] { EventName });
            }
        }

        public void connectHandler()
        {
            controller.EnableGesture(Gesture.GestureType.TYPE_SWIPE);
            this.controller.EnableGesture(Gesture.GestureType.TYPE_CIRCLE);
            this.controller.EnableGesture(Gesture.GestureType.TYPE_KEY_TAP);
            this.controller.EnableGesture(Gesture.GestureType.TYPE_SCREEN_TAP);


            // controller.Config.SetFloat("Gesture.Swipe.Speed", 4000.0f);
            controller.Config.SetFloat("Gesture.Swipe.MinVelocity", 1250f);
            controller.Config.SetFloat("Gesture.Swipe.MinLength", 300f);

            controller.Config.SetFloat("Gesture.KeyTap.MinDownVelocity", 40.0f);
            controller.Config.SetFloat("Gesture.KeyTap.HistorySeconds", .2f);
            controller.Config.SetFloat("Gesture.KeyTap.MinDistance", 1.0f);

            //     controller.Config.SetFloat("Gesture.Circle.MinRadius", 15.0f);
            //   controller.Config.SetFloat("Gesture.Circle.MinArc", 15f);

            controller.Config.SetFloat("InteractionBox.Width", 160.0f);
            controller.Config.SetFloat("InteractionBox.Height", 1600.0f);

            controller.Config.Save();
        }


        public void checkGestures(Frame frame)
        {
            //GestureList gestures = frame.Gestures();
            //Frame newFrame = controller.Frame();

            GestureList gestures = frame.Gestures(lastFrame);

            lastFrame = frame;
            bool isSwipingRight = false;
            bool isSwipingLeft = false;
            bool isSwipingUp = false;
            bool isSwipingDown = false;
            int currentSwipeID = -1;

            foreach (Gesture gesture in gestures)
            {
                // Leap.Vector swipeDirection = swipe.Direction;

                if (gesture.Type == Gesture.GestureType.TYPE_SWIPE)
                {

                    if (gestures.Count() == 0)
                    {
                        SwipeGesture swipe = new SwipeGesture(gesture);
                        Leap.Vector swipeDirection = swipe.Direction;
                        if (isSwipingRight || isSwipingLeft || isSwipingUp || isSwipingDown)
                        // if (swipe.State == Gesture.GestureState.STATE_START && swipeDirection.x < 0 || swipe.State == Gesture.GestureState.STATE_START && swipeDirection.x > 0)
                        {
                            Console.WriteLine("Ended swiping.");
                            //EndSwipe();
                            isSwipingRight = false;
                            isSwipingLeft = false;
                            isSwipingUp = false;
                            isSwipingDown = false;
                        }
                    }


                    else

                    {
                        if (isSwipingRight || isSwipingLeft || isSwipingUp || isSwipingDown)
                        {
                            //Check continuing swipe
                            for (int g = 0; g < gestures.Count(); g++)
                            {
                                //println("Continue " + currentGestures.get(g).id());
                                if (gestures[g].Id == currentSwipeID)
                                {
                                    SwipeGesture swipe = new SwipeGesture(gestures[g]);

                                    Console.WriteLine("Continued swiping " + (isSwipingRight ? "left." : "right."));
                                    Console.WriteLine("Continued swiping " + (isSwipingUp ? "Up." : "Down."));
                                    //  ContinueSwipe(swipe);
                                    break;
                                }
                            }

                        }



                        else
                        {
                            // It must be a new swipe
                            for (int g = 0; g < gestures.Count(); g++)
                            {
                                SwipeGesture swipe = new SwipeGesture(gestures[g]);
                                Leap.Vector swipeDirection = swipe.Direction;
                                if (swipeDirection.x < 0)
                                {
                                    //Swiping left
                                    isSwipingLeft = true;
                                    currentSwipeID = swipe.Id;
                                    // StartSwipe(swipe);
                                    //Console.WriteLine("Started swiping " + (isSwipingRight ? "left." : "right."));
                                    Console.WriteLine("Left");

                                    break;
                                }
                            else    if (swipeDirection.x > 0)
                                {
                                    //Swiping right
                                    isSwipingRight = true;
                                    currentSwipeID = swipe.Id;
                                    // StartSwipe(swipe);
                                    //   Console.WriteLine("Started swiping " + (isSwipingRight ? "left." : "right."));
                                    Console.WriteLine("Right");
                                    break;
                                }
                             else   if (swipeDirection.y < 0)
                                {
                                    //Swiping Down
                                    isSwipingDown = true;
                                    currentSwipeID = swipe.Id;
                                    // StartSwipe(swipe);
                                    // Console.WriteLine("Started swiping " + (isSwipingUp ? "Up." : "Down."));
                                    Console.WriteLine("Down");

                                    break;
                                }
                             else   if (swipeDirection.y > 0)
                                {
                                    //Swiping Up
                                    isSwipingUp = true;
                                    currentSwipeID = swipe.Id;
                                    // StartSwipe(swipe);
                                    //  Console.WriteLine("Started swiping " + (isSwipingUp ? "Up." : "Down."));
                                      Console.WriteLine("Up");
                                    break;
                                }
                            }
                        }
                    }
                }


                if (image.Source.Equals(images[1]))
                {

                    if (isSwipingLeft)
                    {

                        image.Source = images[2];

                    }
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];
                        Console.WriteLine("Circle");
                    }

                }



                // For Image 2
                else if (image.Source.Equals(images[2]))
                {

                    if (isSwipingUp)

                    {
                        image.Source = images[3];
                    }
                    else if (isSwipingLeft)
                    {
                        image.Source = images[7];
                    }
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];
                        Console.WriteLine("Circle");
                    }

                }


                // For Image 3
                else if (image.Source.Equals(images[3]))
                {

                    if (isSwipingUp)

                    {
                        image.Source = images[4];
                    }
                    if (isSwipingRight)
                    {
                        image.Source = images[16];
                    }
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];
                        Console.WriteLine("Circle");
                    }
                }


                // For Image 4
                else if (image.Source.Equals(images[4]))
                {


                    if (isSwipingDown)

                    {
                        image.Source = images[5];
                    }

                    else if (isSwipingUp)

                    {
                        image.Source = images[4];
                    }
                    else if (isSwipingUp)
                    {
                        image.Source = images[6];
                    }
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];
                        Console.WriteLine("Circle");
                    }

                }


                // For Image 5
                else if (image.Source.Equals(images[5]))
                {


                    if (isSwipingUp)

                    {
                        image.Source = images[4];
                    }

                    else if (isSwipingDown)
                    {
                        image.Source = images[21];
                    }
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];
                        Console.WriteLine("Circle");
                    }
                }


                // For Image 6
                else if (image.Source.Equals(images[6]))
                {
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];
                    }
                }




                // For Image 7
                else if (image.Source.Equals(images[7]))
                {


                    if (isSwipingUp)

                    {
                        image.Source = images[8];
                    }


                    else if (isSwipingLeft)
                    {
                        image.Source = images[9];
                    }



                    else if (isSwipingDown)
                    {
                        image.Source = images[2];
                    }
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];
                        Console.WriteLine("Circle");
                    }
                }


                // For Image 8
                else if (image.Source.Equals(images[8]))
                {
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];

                    }
                }


                // For Image 9
                else if (image.Source.Equals(images[9]))
                {


                    if (isSwipingUp)

                    {
                        image.Source = images[10];
                    }


                    else if (isSwipingLeft)
                    {
                        image.Source = images[11];
                    }



                    else if (isSwipingDown)
                    {
                        image.Source = images[7];
                    }
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];
                        Console.WriteLine("Circle");
                    }
                }



                // For Image 10
                else if (image.Source.Equals(images[10]))
                {
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];

                    }
                }


                // For Image 11
                else if (image.Source.Equals(images[11]))
                {


                    if (isSwipingUp)

                    {
                        image.Source = images[12];
                    }


                    else if (isSwipingLeft)
                    {
                        image.Source = images[13];
                    }



                    else if (isSwipingDown)
                    {
                        image.Source = images[9];
                    }
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];
                        Console.WriteLine("Circle");
                    }
                }



                // For Image 12
                else if (image.Source.Equals(images[12]))
                {
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];

                    }
                }


                // For Image 13
                else if (image.Source.Equals(images[13]))
                {

                    if (isSwipingUp)

                    {
                        image.Source = images[14];
                    }


                    else if (isSwipingLeft)
                    {
                        image.Source = images[22];
                    }



                    else if (isSwipingDown)
                    {
                        image.Source = images[11];
                    }
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];
                        Console.WriteLine("Circle");
                    }
                }

                // For Image 14
                else if (image.Source.Equals(images[14]))
                {
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];

                    }
                }




                // For Image 22
                else if (image.Source.Equals(images[22]))
                {


                    if (isSwipingUp)

                    {
                        image.Source = images[15];
                    }



                    else if (isSwipingDown)
                    {
                        image.Source = images[13];
                    }
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];
                        Console.WriteLine("Circle");
                    }
                }

                // For Image 15
                else if (image.Source.Equals(images[15]))
                {
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];

                    }
                }



                // For Image 16
                else if (image.Source.Equals(images[16]))
                {

                    if (isSwipingDown)

                    {
                        image.Source = images[20];
                    }



                    else if (isSwipingDown)
                    {
                        image.Source = images[17];
                    }
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];
                        Console.WriteLine("Circle");
                    }
                }

                // For Image 17
                else if (image.Source.Equals(images[17]))
                {
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];

                    }
                }

                // For Image 18
                else if (image.Source.Equals(images[18]))
                {
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];

                    }
                }

                else if (image.Source.Equals(images[20]))
                {

                    if (isSwipingLeft)
                    {

                        image.Source = images[18];

                    }
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];
                        Console.WriteLine("Circle");
                    }
                }
                // For Image 21
                else if (image.Source.Equals(images[21]))
                {
                    if (gesture.Type == Gesture.GestureType.TYPE_KEY_TAP)
                    {
                        KeyTapGesture TapGesture = new KeyTapGesture(gesture);
                        image.Source = images[1];

                    }
                }

            }

        }
    }


    /* public void StartSwipe(SwipeGesture swipe)
     {

         Console.WriteLine("Started swiping " + (isSwipingLeft ? "left." : "right."));
     }

     void ContinueSwipe(SwipeGesture swipe)
     {
         Console.WriteLine("Continued swiping " + (isSwipingLeft ? "left." : "right."));

     }

     void EndSwipe()
     {
         Console.WriteLine("Ended swiping.");
     }*/



    public interface ILeapEventDelegate
    {
        //definded a method that can be reused
        void LeapEventNotification(string EventName);
    }

    //listener class
    public class LeapListener : Listener
    {

        //create a interface 
        ILeapEventDelegate eventDelegate;


        //create a constructor with interface argument
        public LeapListener(ILeapEventDelegate delegateObject)
        {
            //create a object of interface
            this.eventDelegate = delegateObject;

        }

        public override void OnInit(Controller controller)
        {

            this.eventDelegate.LeapEventNotification("onInit");
        }
        public override void OnConnect(Controller controller)
        {

            this.eventDelegate.LeapEventNotification("onConnect");
        }

        public override void OnFrame(Controller controller)
        {
            this.eventDelegate.LeapEventNotification("onFrame");
        }
        public override void OnExit(Controller controller)
        {
            this.eventDelegate.LeapEventNotification("onExit");
        }
        public override void OnDisconnect(Controller controller)
        {
            this.eventDelegate.LeapEventNotification("onDisconnect");
        }

    }
}


What I have tried:

I have tried comparing the previous and the current frames id , so that it just detects one swipe. Changing the min length and velocity.
Posted
Comments
BillWoodruff 19-Jun-16 0:06am    
Why don't you post this question on some Leap developer forum ? As it is, this question is just a massive code-dump without specifics; a good question describes specific issues clearly and quotes the relevant code, and includes descrption of any error messages.
Member 12517614 21-Jun-16 3:40am    
It is not a specific issue. Its a matter of increasing performance, i posted the entire code to give an idea of the areas where improvements can be made. There is no error, it is just not detecting and differentiating between different swipes accurately.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900