public MainWindow()
        {
            this.Loaded += MainWindow_Loaded;

            InitializeComponent();

            TouchInjector.InitializeTouchInjection();

            mainCanvas.Width = Screen.PrimaryScreen.Bounds.Width;
            mainCanvas.Height = Screen.PrimaryScreen.Bounds.Height;

            BitmapImage bi0 = new BitmapImage();
            bi0.BeginInit();
            bi0.UriSource = new Uri(Globals.ExecutablePath + "\\win_cursor_plus\\cursor0.png");
            bi0.EndInit();

            BitmapImage bi1 = new BitmapImage();
            bi1.BeginInit();
            bi1.UriSource = new Uri(Globals.ExecutablePath + "\\win_cursor_plus\\cursor1.png");
            bi1.EndInit();

            cursorImageIndex.Source = bi0;
            cursorImageIndex.Width = cursorImageIndex.Height = 100;

            cursorImageThumb.Source = bi0;
            cursorImageThumb.Width = cursorImageThumb.Height = 100;

            cursorImageIndex.Opacity = 0;
            cursorImageThumb.Opacity = 0;

            IPC ipc = new IPC("win_cursor_plus");

            ipc.MapFunction("exit", delegate(string messageBody)
            {
                ipc.Clear();
                Environment.Exit(0);
                return 1;
            });

            ipc.SetUDPCallback(delegate(string message)
            {
                if (message != "hide_cursor_index" || message != "hide_cursor_thumb")
                {
                    string[] xyStr = message.Split('!');

                    if (xyStr.Length == 5)
                    {
                        cursorIndexDown = false;
                        cursorThumbDown = false;

                        float x;
                        float y;
                        float z;
                        int down;

                        bool b0 = float.TryParse(xyStr[0], System.Globalization.NumberStyles.Float, null, out x);
                        bool b1 = float.TryParse(xyStr[1], System.Globalization.NumberStyles.Float, null, out y);
                        bool b2 = float.TryParse(xyStr[2], System.Globalization.NumberStyles.Float, null, out z);
                        bool b3 = int.TryParse(xyStr[3], System.Globalization.NumberStyles.Float, null, out down);

                        if (b0 && b1 && b2)
                            if (xyStr[4] == "index")
                            {
                                xCursorIndex = x * screenWidth / 1000;
                                if (xCursorIndex < 0)
                                    xCursorIndex = 0;
                                else if (xCursorIndex > screenWidth)
                                    xCursorIndex = screenWidth;

                                yCursorIndex = y * screenHeight / 1000;
                                if (yCursorIndex < 0)
                                    yCursorIndex = 0;
                                else if (yCursorIndex > screenHeight)
                                    yCursorIndex = screenHeight;

                                zCursorIndex = z;

                                showCursorIndex = true;
                                cursorIndexDown = down == 1;
                            }
                            else if (xyStr[4] == "thumb")
                            {
                                xCursorThumb = x * screenWidth / 1000;
                                if (xCursorThumb < 0)
                                    xCursorThumb = 0;
                                else if (xCursorThumb > screenWidth)
                                    xCursorThumb = screenWidth;

                                yCursorThumb = y * screenHeight / 1000;
                                if (yCursorThumb < 0)
                                    yCursorThumb = 0;
                                else if (yCursorThumb > screenHeight)
                                    yCursorThumb = screenHeight;

                                zCursorThumb = z;

                                showCursorThumb = true;
                                cursorIndexDown = true;
                                cursorThumbDown = true;
                            }
                    }
                    else if (message == "hide_cursor_index")
                        showCursorIndex = false;
                    else if (message == "hide_cursor_thumb")
                        showCursorThumb = false;
                    else if (xyStr.Length == 2 && xyStr[0] == "update")
                        updateNumNew = int.Parse(xyStr[1]);
                }

                return 1;
            });

            Timer timer = new Timer();
            timer.Interval = 20;
            timer.Tick += delegate(object o, EventArgs e)
            {
                ipc.Update();

                if (updateNumNew == updateNumOld && showCursorIndex)
                    return;

                updateNumOld = updateNumNew;

                if (!useTUIO && showCursorIndex)
                {
                    List<PointerTouchInfo> contacts = new List<PointerTouchInfo>();

                    if (cursorIndexDown && !cursorIndexDownOld)
                    {
                        PointerTouchInfo contact = MakePointerTouchInfo((int)xCursorIndex, (int)yCursorIndex, 2, 1);
                        contact.PointerInfo.PointerFlags = PointerFlags.DOWN | PointerFlags.INRANGE | PointerFlags.INCONTACT;
                        contacts.Add(contact);
                    }
                    else if (cursorIndexDown && cursorIndexDownOld)
                    {
                        PointerTouchInfo contact = MakePointerTouchInfo((int)xCursorIndex, (int)yCursorIndex, 2, 1);
                        contact.PointerInfo.PointerFlags = PointerFlags.UPDATE | PointerFlags.INRANGE | PointerFlags.INCONTACT;
                        contacts.Add(contact);
                    }
                    else if (!cursorIndexDown && cursorIndexDownOld)
                    {
                        PointerTouchInfo contact = MakePointerTouchInfo((int)xCursorIndexOld, (int)yCursorIndexOld, 2, 1);
                        contact.PointerInfo.PointerFlags = PointerFlags.UP;
                        contacts.Add(contact);
                    }

                    if (cursorThumbDown && !cursorThumbDownOld)
                    {
                        PointerTouchInfo contact = MakePointerTouchInfo((int)xCursorThumb, (int)yCursorThumb, 2, 2);
                        contact.PointerInfo.PointerFlags = PointerFlags.DOWN | PointerFlags.INRANGE | PointerFlags.INCONTACT;
                        contacts.Add(contact);
                    }
                    else if (cursorThumbDown && cursorThumbDownOld)
                    {
                        PointerTouchInfo contact = MakePointerTouchInfo((int)xCursorThumb, (int)yCursorThumb, 2, 2);
                        contact.PointerInfo.PointerFlags = PointerFlags.UPDATE | PointerFlags.INRANGE | PointerFlags.INCONTACT;
                        contacts.Add(contact);
                    }
                    else if (!cursorThumbDown && cursorThumbDownOld)
                    {
                        PointerTouchInfo contact = MakePointerTouchInfo((int)xCursorThumbOld, (int)yCursorThumbOld, 2, 2);
                        contact.PointerInfo.PointerFlags = PointerFlags.UP;
                        contacts.Add(contact);
                    }

                    bool success = TouchInjector.InjectTouchInput(contacts.Count, contacts.ToArray());
                }
                else if (showCursorIndex)
                {
                    Object[] tCur0 = new Object[7];
                    Object[] tCur1 = new Object[7];
                    OSCBundle bundle = new OSCBundle();
                    bundle.Append(TUIO.TUIOFseq(tuioFSeq));
                    ArrayList TUIOSessions = new ArrayList();

                    if (cursorIndexDown)
                    {
                        bundle.Append(TUIO.TUIO2DcurExt(0, xCursorIndex / screenWidth, yCursorIndex / screenHeight, 0, 0, 0, 10, 10));
                        TUIOSessions.Add(0);
                    }
                    if (cursorThumbDown) {
                        bundle.Append(TUIO.TUIO2DcurExt(1, xCursorThumb / screenWidth, yCursorThumb / screenHeight, 0, 0, 0, 10, 10));
                        TUIOSessions.Add(1);
                    }
                    bundle.Append(TUIO.TUIOAlive(TUIOSessions));
                    transmitter.Send(bundle);
                    tuioFSeq++;
                }

                if (showCursorIndex)
                {
                    if (cursorIndexDown)
                        cursorImageIndex.Source = bi1;
                    else
                        cursorImageIndex.Source = bi0;

                    if (cursorImageIndex.Opacity < 1)
                        cursorImageIndex.Opacity += 0.2;

                    int cursorSize = (int)(zCursorIndex * 5 * (float)windowWidth / (float)screenWidth);
                    if (cursorSize > 150)
                        cursorSize = 150;
                    else if (cursorSize < 50)
                        cursorSize = 50;

                    cursorImageIndex.Width = cursorImageIndex.Height = cursorSize;

                    int xPosRemapped = (int)map_val(xCursorIndex, 0, screenWidth, 0, windowWidth);
                    int yPosRemapped = (int)map_val(yCursorIndex, 0, screenHeight, 0, windowHeight);

                    if (xPosRemapped < 0)
                        xPosRemapped = 0;
                    else if (xPosRemapped > screenWidth)
                        xPosRemapped = screenWidth;
                    if (yPosRemapped < 0)
                        yPosRemapped = 0;
                    else if (yPosRemapped > screenHeight)
                        yPosRemapped = screenHeight;

                    Canvas.SetLeft(cursorImageIndex, xPosRemapped - (cursorSize / 2));
                    Canvas.SetTop(cursorImageIndex, yPosRemapped - (cursorSize / 2));
                }
                else if (cursorImageIndex.Opacity > 0)
                    cursorImageIndex.Opacity -= 0.2;

                if (showCursorThumb)
                {
                    if (cursorThumbDown)
                        cursorImageThumb.Source = bi1;
                    else
                        cursorImageThumb.Source = bi0;

                    if (cursorImageThumb.Opacity < 1)
                        cursorImageThumb.Opacity += 0.2;

                    int cursorSize = (int)(zCursorThumb * 5 * (float)windowWidth / (float)screenWidth);
                    if (cursorSize > 150)
                        cursorSize = 150;
                    else if (cursorSize < 50)
                        cursorSize = 50;

                    cursorImageThumb.Width = cursorImageThumb.Height = cursorSize;

                    int xPosRemapped = (int)map_val(xCursorThumb, 0, screenWidth, 0, windowWidth);
                    int yPosRemapped = (int)map_val(yCursorThumb, 0, screenHeight, 0, windowHeight);

                    if (xPosRemapped < 0)
                        xPosRemapped = 0;
                    else if (xPosRemapped > screenWidth)
                        xPosRemapped = screenWidth;
                    if (yPosRemapped < 0)
                        yPosRemapped = 0;
                    else if (yPosRemapped > screenHeight)
                        yPosRemapped = screenHeight;

                    Canvas.SetLeft(cursorImageThumb, xPosRemapped - (cursorSize / 2));
                    Canvas.SetTop(cursorImageThumb, yPosRemapped - (cursorSize / 2));
                }
                else if (cursorImageThumb.Opacity > 0)
                    cursorImageThumb.Opacity -= 0.2;

                cursorIndexDownOld = cursorIndexDown;
                cursorThumbDownOld = cursorThumbDown;

                xCursorThumbOld = xCursorThumb;
                yCursorThumbOld = yCursorThumb;
                zCursorThumbOld = zCursorThumb;

                xCursorIndexOld = xCursorIndex;
                yCursorIndexOld = yCursorIndex;
                zCursorIndexOld = zCursorIndex;
            };

            timer.Start();
        }
Exemplo n.º 2
0
        public MainWindow()
        {
            this.Loaded += MainWindow_Loaded;

            InitializeComponent();

            TouchInjector.InitializeTouchInjection();

            mainCanvas.Width  = Screen.PrimaryScreen.Bounds.Width;
            mainCanvas.Height = Screen.PrimaryScreen.Bounds.Height;

            BitmapImage bi0 = new BitmapImage();

            bi0.BeginInit();
            bi0.UriSource = new Uri(Globals.ExecutablePath + "\\win_cursor_plus\\cursor0.png");
            bi0.EndInit();

            BitmapImage bi1 = new BitmapImage();

            bi1.BeginInit();
            bi1.UriSource = new Uri(Globals.ExecutablePath + "\\win_cursor_plus\\cursor1.png");
            bi1.EndInit();

            cursorImageIndex.Source = bi0;
            cursorImageIndex.Width  = cursorImageIndex.Height = 100;

            cursorImageThumb.Source = bi0;
            cursorImageThumb.Width  = cursorImageThumb.Height = 100;

            cursorImageIndex.Opacity = 0;
            cursorImageThumb.Opacity = 0;

            IPC ipc = new IPC("win_cursor_plus");

            ipc.MapFunction("exit", delegate(string messageBody)
            {
                ipc.Clear();
                Environment.Exit(0);
                return(1);
            });

            ipc.SetUDPCallback(delegate(string message)
            {
                if (message != "hide_cursor_index" || message != "hide_cursor_thumb")
                {
                    string[] xyStr = message.Split('!');

                    if (xyStr.Length == 5)
                    {
                        cursorIndexDown = false;
                        cursorThumbDown = false;

                        float x;
                        float y;
                        float z;
                        int down;

                        bool b0 = float.TryParse(xyStr[0], System.Globalization.NumberStyles.Float, null, out x);
                        bool b1 = float.TryParse(xyStr[1], System.Globalization.NumberStyles.Float, null, out y);
                        bool b2 = float.TryParse(xyStr[2], System.Globalization.NumberStyles.Float, null, out z);
                        bool b3 = int.TryParse(xyStr[3], System.Globalization.NumberStyles.Float, null, out down);

                        if (b0 && b1 && b2)
                        {
                            if (xyStr[4] == "index")
                            {
                                xCursorIndex = x * screenWidth / 1000;
                                if (xCursorIndex < 0)
                                {
                                    xCursorIndex = 0;
                                }
                                else if (xCursorIndex > screenWidth)
                                {
                                    xCursorIndex = screenWidth;
                                }

                                yCursorIndex = y * screenHeight / 1000;
                                if (yCursorIndex < 0)
                                {
                                    yCursorIndex = 0;
                                }
                                else if (yCursorIndex > screenHeight)
                                {
                                    yCursorIndex = screenHeight;
                                }

                                zCursorIndex = z;

                                showCursorIndex = true;
                                cursorIndexDown = down == 1;
                            }
                            else if (xyStr[4] == "thumb")
                            {
                                xCursorThumb = x * screenWidth / 1000;
                                if (xCursorThumb < 0)
                                {
                                    xCursorThumb = 0;
                                }
                                else if (xCursorThumb > screenWidth)
                                {
                                    xCursorThumb = screenWidth;
                                }

                                yCursorThumb = y * screenHeight / 1000;
                                if (yCursorThumb < 0)
                                {
                                    yCursorThumb = 0;
                                }
                                else if (yCursorThumb > screenHeight)
                                {
                                    yCursorThumb = screenHeight;
                                }

                                zCursorThumb = z;

                                showCursorThumb = true;
                                cursorIndexDown = true;
                                cursorThumbDown = true;
                            }
                        }
                    }
                    else if (message == "hide_cursor_index")
                    {
                        showCursorIndex = false;
                    }
                    else if (message == "hide_cursor_thumb")
                    {
                        showCursorThumb = false;
                    }
                    else if (xyStr.Length == 2 && xyStr[0] == "update")
                    {
                        updateNumNew = int.Parse(xyStr[1]);
                    }
                }

                return(1);
            });

            Timer ipcTimer = new Timer();

            ipcTimer.Interval = 100;
            ipcTimer.Tick    += delegate(object o, EventArgs e)
            {
                ipc.Update();
            };
            ipcTimer.Start();

            Timer timer = new Timer();

            timer.Interval = 20;
            timer.Tick    += delegate(object o, EventArgs e)
            {
                if (updateNumNew == updateNumOld && showCursorIndex)
                {
                    return;
                }

                updateNumOld = updateNumNew;

                if (!useTUIO && showCursorIndex)
                {
                    if (!useFallback)
                    {
                        List <PointerTouchInfo> contacts = new List <PointerTouchInfo>();

                        if (cursorIndexDown && !cursorIndexDownOld)
                        {
                            PointerTouchInfo contact = MakePointerTouchInfo((int)xCursorIndex, (int)yCursorIndex, 2, 1);
                            contact.PointerInfo.PointerFlags = PointerFlags.DOWN | PointerFlags.INRANGE | PointerFlags.INCONTACT;
                            contacts.Add(contact);
                        }
                        else if (cursorIndexDown && cursorIndexDownOld)
                        {
                            PointerTouchInfo contact = MakePointerTouchInfo((int)xCursorIndex, (int)yCursorIndex, 2, 1);
                            contact.PointerInfo.PointerFlags = PointerFlags.UPDATE | PointerFlags.INRANGE | PointerFlags.INCONTACT;
                            contacts.Add(contact);
                        }
                        else if (!cursorIndexDown && cursorIndexDownOld)
                        {
                            PointerTouchInfo contact = MakePointerTouchInfo((int)xCursorIndexOld, (int)yCursorIndexOld, 2, 1);
                            contact.PointerInfo.PointerFlags = PointerFlags.UP;
                            contacts.Add(contact);
                        }

                        if (cursorThumbDown && !cursorThumbDownOld)
                        {
                            PointerTouchInfo contact = MakePointerTouchInfo((int)xCursorThumb, (int)yCursorThumb, 2, 2);
                            contact.PointerInfo.PointerFlags = PointerFlags.DOWN | PointerFlags.INRANGE | PointerFlags.INCONTACT;
                            contacts.Add(contact);
                        }
                        else if (cursorThumbDown && cursorThumbDownOld)
                        {
                            PointerTouchInfo contact = MakePointerTouchInfo((int)xCursorThumb, (int)yCursorThumb, 2, 2);
                            contact.PointerInfo.PointerFlags = PointerFlags.UPDATE | PointerFlags.INRANGE | PointerFlags.INCONTACT;
                            contacts.Add(contact);
                        }
                        else if (!cursorThumbDown && cursorThumbDownOld)
                        {
                            PointerTouchInfo contact = MakePointerTouchInfo((int)xCursorThumbOld, (int)yCursorThumbOld, 2, 2);
                            contact.PointerInfo.PointerFlags = PointerFlags.UP;
                            contacts.Add(contact);
                        }

                        bool success = TouchInjector.InjectTouchInput(contacts.Count, contacts.ToArray());
                    }
                    else
                    {
                        System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xCursorIndex, (int)yCursorIndex);

                        if (cursorIndexDown)
                        {
                            mouseDown();
                        }
                        else
                        {
                            mouseUp();
                        }
                    }
                }
                else if (showCursorIndex)
                {
                    Object[]  tCur0  = new Object[7];
                    Object[]  tCur1  = new Object[7];
                    OSCBundle bundle = new OSCBundle();
                    bundle.Append(TUIO.TUIOFseq(tuioFSeq));
                    ArrayList TUIOSessions = new ArrayList();

                    if (cursorIndexDown)
                    {
                        bundle.Append(TUIO.TUIO2DcurExt(0, xCursorIndex / screenWidth, yCursorIndex / screenHeight, 0, 0, 0, 10, 10));
                        TUIOSessions.Add(0);
                    }
                    if (cursorThumbDown)
                    {
                        bundle.Append(TUIO.TUIO2DcurExt(1, xCursorThumb / screenWidth, yCursorThumb / screenHeight, 0, 0, 0, 10, 10));
                        TUIOSessions.Add(1);
                    }
                    bundle.Append(TUIO.TUIOAlive(TUIOSessions));
                    transmitter.Send(bundle);
                    tuioFSeq++;
                }

                if (showCursorIndex)
                {
                    if (cursorIndexDown)
                    {
                        cursorImageIndex.Source = bi1;
                    }
                    else
                    {
                        cursorImageIndex.Source = bi0;
                    }

                    if (cursorImageIndex.Opacity < 1)
                    {
                        cursorImageIndex.Opacity += 0.2;
                    }

                    int cursorSize = (int)(zCursorIndex * 5 * (float)windowWidth / (float)screenWidth);
                    if (cursorSize > 150)
                    {
                        cursorSize = 150;
                    }
                    else if (cursorSize < 50)
                    {
                        cursorSize = 50;
                    }

                    cursorImageIndex.Width = cursorImageIndex.Height = cursorSize;

                    int xPosRemapped = (int)map_val(xCursorIndex, 0, screenWidth, 0, windowWidth);
                    int yPosRemapped = (int)map_val(yCursorIndex, 0, screenHeight, 0, windowHeight);

                    if (xPosRemapped < 0)
                    {
                        xPosRemapped = 0;
                    }
                    else if (xPosRemapped > screenWidth)
                    {
                        xPosRemapped = screenWidth;
                    }
                    if (yPosRemapped < 0)
                    {
                        yPosRemapped = 0;
                    }
                    else if (yPosRemapped > screenHeight)
                    {
                        yPosRemapped = screenHeight;
                    }

                    Canvas.SetLeft(cursorImageIndex, xPosRemapped - (cursorSize / 2));
                    Canvas.SetTop(cursorImageIndex, yPosRemapped - (cursorSize / 2));
                }
                else if (cursorImageIndex.Opacity > 0)
                {
                    cursorImageIndex.Opacity -= 0.2;
                }

                if (showCursorThumb)
                {
                    if (cursorThumbDown)
                    {
                        cursorImageThumb.Source = bi1;
                    }
                    else
                    {
                        cursorImageThumb.Source = bi0;
                    }

                    if (cursorImageThumb.Opacity < 1)
                    {
                        cursorImageThumb.Opacity += 0.2;
                    }

                    int cursorSize = (int)(zCursorThumb * 5 * (float)windowWidth / (float)screenWidth);
                    if (cursorSize > 150)
                    {
                        cursorSize = 150;
                    }
                    else if (cursorSize < 50)
                    {
                        cursorSize = 50;
                    }

                    cursorImageThumb.Width = cursorImageThumb.Height = cursorSize;

                    int xPosRemapped = (int)map_val(xCursorThumb, 0, screenWidth, 0, windowWidth);
                    int yPosRemapped = (int)map_val(yCursorThumb, 0, screenHeight, 0, windowHeight);

                    if (xPosRemapped < 0)
                    {
                        xPosRemapped = 0;
                    }
                    else if (xPosRemapped > screenWidth)
                    {
                        xPosRemapped = screenWidth;
                    }
                    if (yPosRemapped < 0)
                    {
                        yPosRemapped = 0;
                    }
                    else if (yPosRemapped > screenHeight)
                    {
                        yPosRemapped = screenHeight;
                    }

                    Canvas.SetLeft(cursorImageThumb, xPosRemapped - (cursorSize / 2));
                    Canvas.SetTop(cursorImageThumb, yPosRemapped - (cursorSize / 2));
                }
                else if (cursorImageThumb.Opacity > 0)
                {
                    cursorImageThumb.Opacity -= 0.2;
                }

                cursorIndexDownOld = cursorIndexDown;
                cursorThumbDownOld = cursorThumbDown;

                xCursorThumbOld = xCursorThumb;
                yCursorThumbOld = yCursorThumb;
                zCursorThumbOld = zCursorThumb;

                xCursorIndexOld = xCursorIndex;
                yCursorIndexOld = yCursorIndex;
                zCursorIndexOld = zCursorIndex;
            };

            timer.Start();
        }