Esempio n. 1
0
        /// <summary>
        /// Bind an ev to a function call.  When calling the function, verifies that
        /// it belongs to the touch stream that is currently being processsed, and splits
        /// multitouch events into multiple events as needed.
        /// </summary>
        /// <param name="node">Node upon which to listen.</param>
        /// <param name="name">Event name to listen to (e.g. 'mousedown').</param>
        /// <param name="thisObject">The value of 'this' in the function.</param>
        /// <param name="func">Function to call when ev is triggered.</param>
        /// <param name="opt_noCaptureIdentifier">True if triggering on this ev
        /// should not block execution of other ev handlers on this touch or other
        /// simultaneous touches.</param>
        /// <returns>Opaque data that can be passed to unbindEvent_.</returns>
        internal static JsArray <EventWrapInfo> bindEventWithChecks_ <T>(Node node, string name, object thisObject,
                                                                         Action <T> func, bool opt_noCaptureIdentifier = false) where T : Event
        {
            if ((thisObject != null) && (thisObject != func.Target))
            {
                throw new ArgumentException();
            }
            var handled  = false;
            var wrapFunc = new Action <T>((e) => {
                var captureIdentifier = !opt_noCaptureIdentifier;
                // Handle each touch point separately.  If the ev was a mouse ev, this
                // will hand back an array with one element, which we're fine handling.
                var events = Touch.splitEventByTouches(e);
                foreach (var ev in events)
                {
                    if (captureIdentifier && !Touch.shouldHandleEvent(ev))
                    {
                        continue;
                    }
                    Touch.setClientFromTouch(ev);
                    if ((thisObject != null) && (thisObject != func.Target))
                    {
                        func.Method.Invoke(thisObject, new object[] { ev });
                    }
                    else
                    {
                        func.Invoke(ev);
                    }
                    handled = true;
                }
            });

            node.AddEventListener(name, wrapFunc, false);
            var bindData = new JsArray <EventWrapInfo> {
                new EventWrapInfo(node, name, wrapFunc)
            };

            // Add equivalent touch ev.
            if (Touch.TOUCH_MAP.ContainsKey(name))
            {
                var touchWrapFunc = new Action <T>((e) => {
                    wrapFunc(e);
                    // Stop the browser from scrolling/zooming the page.
                    if (handled)
                    {
                        e.PreventDefault();
                    }
                });
                string eventName;

                for (var i = 0; (eventName = (string)Touch.TOUCH_MAP[name][i]) != null; i++)
                {
                    node.AddEventListener(eventName, touchWrapFunc, false);
                    bindData.Push(new EventWrapInfo(node, eventName, touchWrapFunc));
                }
            }
            return(bindData);
        }