Example #1
0
        /** Hackish stuff - stole touches from other CCTouchDispatcher targeted delegates.
         * Used to claim touch without receiving ccTouchBegan. */
        void claimTouch(UITouch aTouch)
        {
            CCTouchDispatcher dispatcher = CCDirector.sharedDirector.touchDispatcher;

            // Enumerate through all targeted handlers.

            var enumerator = dispatcher.getTargetedHandlers().GetEnumerator();

            while (enumerator.MoveNext())
            {
                CCTargetedTouchHandler handler = (CCTargetedTouchHandler)enumerator.Current;
                // Only our handler should claim the touch.
                if (handler.delegate_ == this)
                {
                    if (!(handler.claimedTouches.Contains(aTouch)))
                    {
                        handler.claimedTouches.Add(aTouch);
                    }
                }
                else
                {
                    // Steal touch from other targeted delegates, if they claimed it.
                    if (handler.claimedTouches.Contains(aTouch))
                    {
                        CCTouchOneByOneDelegate oneByOneTouchDelegate = (CCTouchOneByOneDelegate)handler.delegate_;
                        if (oneByOneTouchDelegate != null)
                        {
                            oneByOneTouchDelegate.ccTouchCancelled(aTouch);
                        }
                        handler.claimedTouches.Remove(aTouch);
                    }
                }
            }
        }
        public void addTargetedDelegate(CCTouchOneByOneDelegate aDelegate, int priority, bool swallowsTouches)
        {
            CCTouchHandler handler = new CCTargetedTouchHandler(aDelegate, priority, swallowsTouches);

            if (!locked)
            {
                forceAddHandler(handler, targetedHandlers);
            }
            else
            {
                if (!removeDelegate(aDelegate, handlersToRemove))
                {
                    handlersToAdd.Add(handler);
                    toAdd = true;
                }
            }
        }
        //
        // dispatch events
        //
        void touches(HashSet <UITouch> touches, kCCTouch touchType)
        {
            NSUtils.Assert((int)touchType < 4, "Invalid idx value");

            HashSet <UITouch> mutableTouches;

            locked = true;

            // optimization to prevent a mutable copy when it is not necessary
            int  targetedHandlersCount = targetedHandlers.Count;
            int  standardHandlersCount = standardHandlers.Count;
            bool needsMutableSet       = (targetedHandlersCount != 0 && standardHandlersCount != 0);

            mutableTouches = needsMutableSet?(new HashSet <UITouch>(touches)):touches;

            ccTouchHandlerHelperData helper = handlerHelperData[(int)touchType];

            //
            // process the target handlers 1st
            //
            if (targetedHandlersCount > 0)
            {
                var enumerator = touches.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    UITouch touch = enumerator.Current;

                    var targetedHandlersEnumerator = targetedHandlers.GetEnumerator();
                    while (targetedHandlersEnumerator.MoveNext())
                    {
                        CCTargetedTouchHandler handler = (CCTargetedTouchHandler)targetedHandlersEnumerator.Current;
                        bool claimed = false;
                        if (touchType == kCCTouch.Began)
                        {
                            claimed = ((CCTouchOneByOneDelegate)handler.delegate_).ccTouchBegan(touch);
                            if (claimed)
                            {
                                handler.claimedTouches.Add(touch);
                            }
                        }

                        // else (moved, ended, cancelled)
                        else if (handler.claimedTouches.Contains(touch))
                        {
                            claimed = true;
                            if (((int)handler.enabledSelectors & (int)(helper.type)) != 0)
                            {
                                Type       thisType  = handler.delegate_.GetType();
                                MethodInfo theMethod = thisType.GetMethod(helper.touchSel);
                                theMethod.Invoke(handler.delegate_, new object[1] {
                                    touch
                                });
                            }
                            if (((int)helper.type & ((int)kCCTouchSelectorFlag.CancelledBit | (int)kCCTouchSelectorFlag.EndedBit)) != 0)
                            {
                                handler.claimedTouches.Remove(touch);
                            }
                        }

                        if (claimed && handler.swallowsTouches)
                        {
                            if (needsMutableSet)
                            {
                                mutableTouches.Remove(touch);
                            }
                            break;
                        }
                    }
                }
            }

            //
            // process standard handlers 2nd
            //
            if (standardHandlersCount > 0 && mutableTouches.Count > 0)
            {
                var enumerator = standardHandlers.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    var handler = enumerator.Current;
                    if (((int)handler.enabledSelectors & ((int)(helper.type))) != 0)
                    {
                        Type       thisType  = handler.delegate_.GetType();
                        MethodInfo theMethod = thisType.GetMethod(helper.touchesSel);
                        theMethod.Invoke(handler.delegate_, new object[1] {
                            mutableTouches
                        });
                    }
                }
            }
            if (needsMutableSet)
            {
                mutableTouches = null;
            }

            //
            // Optimization. To prevent a [handlers copy] which is expensive
            // the add/removes/quit is done after the iterations
            //
            locked = false;

            //issue 1084, 1139 first add then remove
            if (toAdd)
            {
                toAdd = false;

                var enumerator = handlersToAdd.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    var handler = enumerator.Current;
                    if (handler is CCTargetedTouchHandler)
                    {
                        forceAddHandler((CCTouchHandler)handler, targetedHandlers);
                    }
                    else
                    {
                        forceAddHandler((CCTouchHandler)handler, standardHandlers);
                    }
                }
                handlersToAdd.Clear();
            }

            if (toRemove)
            {
                toRemove = false;


                var enumerator = handlersToRemove.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    var aDelegate = enumerator.Current;
                    forceRemoveDelegate(aDelegate);
                }
                handlersToRemove.Clear();
            }

            if (toQuit)
            {
                toQuit = false;
                forceRemoveAllDelegates();
            }
        }
		public void addTargetedDelegate(CCTouchOneByOneDelegate aDelegate, int priority, bool swallowsTouches)
		{
			CCTouchHandler handler = new CCTargetedTouchHandler(aDelegate, priority, swallowsTouches);
			if( ! locked ) {
				forceAddHandler(handler, targetedHandlers);
			} else {
				if(!removeDelegate(aDelegate, handlersToRemove)){
					handlersToAdd.Add(handler);
					toAdd = true;
				}
			}
		}