Beispiel #1
0
        static int addEventListener(int L)
        {
            if (!LuaCommon.CheckAndShowArgsError(L, LConst.Function))
            {
                return(0);
            }

            Object control   = (Object)Lua.Lua_touserdata(L, 1);
            int    callbackF = Lua.LuaL_ref(L, Lua._LUA_REGISTRYINDEX);
            string eventName = Lua.Lua_tostring(L, 2);

            if (control != null && callbackF != 0)
            {
                var  pi      = control.GetType().GetProperty("View_");
                var  fe      = pi.GetValue(control, null);
                bool isPinch = false;
                if (fe is FrameworkElement)
                {
                    if (eventName.Equals("touchmove"))
                    {
                        EventHandler <ManipulationDeltaEventArgs> touchDeltaEventHandler = null;
                        TouchEventRegisterInfo      touchInfo = null;
                        Dictionary <string, object> touchDict = null;
                        if (!touchEventList.Any(c => (c.FE == fe as FrameworkElement)))
                        {
                            touchInfo = new TouchEventRegisterInfo();
                            touchEventList.Add(touchInfo);
                            touchInfo.FE = fe as FrameworkElement;
                            touchDict    = touchInfo.TouchDict;
                        }
                        else
                        {
                            touchInfo = touchEventList.FirstOrDefault(c => (c.FE == fe as FrameworkElement));
                            touchDict = touchInfo.TouchDict;
                        }

                        (fe as FrameworkElement).ManipulationDelta += touchDeltaEventHandler = (sender, e) =>
                        {
                            bool oldIsPinch = isPinch;
                            isPinch = e.PinchManipulation != null;

                            if (oldIsPinch == isPinch)
                            {
                                double           currentOffsetY = 0;
                                double           page_OffsetY   = 0;
                                object           obj            = e.OriginalSource;
                                Point            OriginPoint    = e.ManipulationOrigin;
                                Point            currentPoint   = new Point(0, 0);
                                Page             currentPage    = LuaCommon.FindCurrentPage(sender as FrameworkElement);
                                FrameworkElement container      = e.ManipulationContainer as FrameworkElement;
                                if (container != null && container is ScrollViewer)
                                {
                                    ScrollViewer sv = container as ScrollViewer;
                                    currentOffsetY = sv.VerticalOffset;
                                }
                                if (currentPage != null)
                                {
                                    GeneralTransform generalTransform1 = currentPage.TransformToVisual(sender as UIElement);
                                    currentPoint = generalTransform1.Transform(new Point(0, 0));
                                    ScrollViewer sv = LuaCommon.FindVisualChild <ScrollViewer>(currentPage);
                                    page_OffsetY = sv.VerticalOffset;
                                }
                                touchDict["target"]                  = control;
                                touchDict["viewX"]                   = OriginPoint.X;
                                touchDict["viewY"]                   = OriginPoint.Y;
                                touchDict["contentX"]                = OriginPoint.X;
                                touchDict["contentY"]                = OriginPoint.Y + currentOffsetY;
                                touchDict["clientX"]                 = OriginPoint.X - currentPoint.X;
                                touchDict["clientY"]                 = OriginPoint.Y - currentPoint.Y;
                                touchDict["pageX"]                   = OriginPoint.X - currentPoint.X;
                                touchDict["pageY"]                   = OriginPoint.Y - currentPoint.Y + page_OffsetY;
                                RYTTouchEvent.TouchesDict["1"]       = touchDict;
                                RYTTouchEvent.TargetTouchesDict["1"] = touchDict;
                                LuaManager.GetLuaManager(L).ExecuteCallBackFunctionWithAnyParams(callbackF, RYTTouchEvent.TouchEventDict);
                            }
                        };
                        touchInfo.TouchDeltaEventHandler = touchDeltaEventHandler;
                    }
                    else if (eventName.Equals("touchstart"))
                    {
                        EventHandler <ManipulationStartedEventArgs> touchStartedEventHandler = null;
                        TouchEventRegisterInfo      touchInfo = null;
                        Dictionary <string, object> touchDict = null;
                        if (!touchEventList.Any(c => (c.FE == fe as FrameworkElement)))
                        {
                            touchInfo = new TouchEventRegisterInfo();
                            touchEventList.Add(touchInfo);
                            touchInfo.FE = fe as FrameworkElement;
                            touchDict    = touchInfo.TouchDict;
                        }
                        else
                        {
                            touchInfo = touchEventList.FirstOrDefault(c => (c.FE == fe as FrameworkElement));
                            touchDict = touchInfo.TouchDict;
                        }

                        (fe as FrameworkElement).ManipulationStarted += touchStartedEventHandler = (sender, e) =>
                        {
                            double           currentOffsetY = 0;
                            double           page_OffsetY   = 0;
                            object           obj            = e.OriginalSource;
                            Point            OriginPoint    = e.ManipulationOrigin;
                            Point            currentPoint   = new Point(0, 0);
                            Page             currentPage    = LuaCommon.FindCurrentPage(sender as FrameworkElement);
                            FrameworkElement container      = e.ManipulationContainer as FrameworkElement;
                            if (container != null && container is ScrollViewer)
                            {
                                ScrollViewer sv = container as ScrollViewer;
                                currentOffsetY = sv.VerticalOffset;
                            }
                            if (currentPage != null)
                            {
                                GeneralTransform generalTransform1 = currentPage.TransformToVisual(sender as UIElement);
                                currentPoint = generalTransform1.Transform(new Point(0, 0));
                                ScrollViewer sv = LuaCommon.FindVisualChild <ScrollViewer>(currentPage);
                                page_OffsetY = sv.VerticalOffset;
                            }
                            touchDict["target"]                  = control;
                            touchDict["viewX"]                   = OriginPoint.X / LuaManager.WidthScale;
                            touchDict["viewY"]                   = OriginPoint.Y / LuaManager.HeightScale;
                            touchDict["contentX"]                = OriginPoint.X / LuaManager.WidthScale;
                            touchDict["contentY"]                = (OriginPoint.Y + currentOffsetY) / LuaManager.HeightScale;
                            touchDict["clientX"]                 = (OriginPoint.X - currentPoint.X) / LuaManager.WidthScale;
                            touchDict["clientY"]                 = (OriginPoint.Y - currentPoint.Y) / LuaManager.HeightScale;
                            touchDict["pageX"]                   = (OriginPoint.X - currentPoint.X) / LuaManager.WidthScale;
                            touchDict["pageY"]                   = (OriginPoint.Y - currentPoint.Y + page_OffsetY) / LuaManager.HeightScale;
                            RYTTouchEvent.TouchesDict["1"]       = touchDict;
                            RYTTouchEvent.TargetTouchesDict["1"] = touchDict;
                            LuaManager.GetLuaManager(L).ExecuteCallBackFunctionWithAnyParams(callbackF, RYTTouchEvent.TouchEventDict);
                        };
                        touchInfo.TouchStartedEventHandler = touchStartedEventHandler;
                    }
                    else if (eventName.Equals("touchend"))
                    {
                        EventHandler <ManipulationCompletedEventArgs> dragCompletedEventHandler = null;
                        TouchEventRegisterInfo      touchInfo = null;
                        Dictionary <string, object> touchDict = null;
                        if (!touchEventList.Any(c => (c.FE == fe as FrameworkElement)))
                        {
                            touchInfo = new TouchEventRegisterInfo();
                            touchEventList.Add(touchInfo);
                            touchInfo.FE = fe as FrameworkElement;
                            touchDict    = touchInfo.TouchDict;
                        }
                        else
                        {
                            touchInfo = touchEventList.FirstOrDefault(c => (c.FE == fe as FrameworkElement));
                            touchDict = touchInfo.TouchDict;
                        }
                        (fe as FrameworkElement).ManipulationCompleted += dragCompletedEventHandler = (sender, e) =>
                        {
                            double           currentOffsetY = 0;
                            double           page_OffsetY   = 0;
                            object           obj            = e.OriginalSource;
                            Point            OriginPoint    = e.ManipulationOrigin;
                            Point            currentPoint   = new Point(0, 0);
                            Page             currentPage    = LuaCommon.FindCurrentPage(sender as FrameworkElement);
                            FrameworkElement container      = e.ManipulationContainer as FrameworkElement;
                            if (container != null && container is ScrollViewer)
                            {
                                ScrollViewer sv = container as ScrollViewer;
                                currentOffsetY = sv.VerticalOffset;
                            }
                            if (currentPage != null)
                            {
                                GeneralTransform generalTransform1 = currentPage.TransformToVisual(sender as UIElement);
                                currentPoint = generalTransform1.Transform(new Point(0, 0));
                                ScrollViewer sv = LuaCommon.FindVisualChild <ScrollViewer>(currentPage);
                                page_OffsetY = sv.VerticalOffset;
                            }
                            touchDict["target"]                  = control;
                            touchDict["viewX"]                   = OriginPoint.X / LuaManager.WidthScale;
                            touchDict["viewY"]                   = OriginPoint.Y / LuaManager.HeightScale;
                            touchDict["contentX"]                = OriginPoint.X / LuaManager.WidthScale;
                            touchDict["contentY"]                = (OriginPoint.Y + currentOffsetY) / LuaManager.HeightScale;
                            touchDict["clientX"]                 = (OriginPoint.X - currentPoint.X) / LuaManager.WidthScale;
                            touchDict["clientY"]                 = (OriginPoint.Y - currentPoint.Y) / LuaManager.HeightScale;
                            touchDict["pageX"]                   = (OriginPoint.X - currentPoint.X) / LuaManager.WidthScale;
                            touchDict["pageY"]                   = (OriginPoint.Y - currentPoint.Y + page_OffsetY) / LuaManager.HeightScale;
                            RYTTouchEvent.TouchesDict["1"]       = touchDict;
                            RYTTouchEvent.TargetTouchesDict["1"] = touchDict;
                            LuaManager.GetLuaManager(L).ExecuteCallBackFunctionWithAnyParams(callbackF, RYTTouchEvent.TouchEventDict);
                        };
                    }
                    else if (eventName.Equals("touchcancel"))
                    {
                    }
                }
            }


            return(0);
        }