Exemplo n.º 1
0
        /** Schedules the 'update' selector for a given target with a given priority.
         *   The 'update' selector will be called every frame.
         *   The lower the priority, the earlier it is called.
         *   @since v0.99.3
         */

        public void Schedule(ICCUpdatable targt, int priority, bool paused)
        {
            HashUpdateEntry element;

            if (hashForUpdates.TryGetValue(targt, out element))
            {
                Debug.Assert(element.Entry.MarkedForDeletion);

                // TODO: check if priority has changed!
                element.Entry.MarkedForDeletion = false;

                return;
            }

            // most of the updates are going to be 0, that's way there
            // is an special list for updates with priority 0
            if (priority == 0)
            {
                AppendIn(updates0List, targt, paused);
            }
            else if (priority < 0)
            {
                PriorityIn(updatesNegList, targt, priority, paused);
            }
            else
            {
                PriorityIn(updatesPosList, targt, priority, paused);
            }
        }
Exemplo n.º 2
0
        /** Unschedules all selectors for a given target.
         *   This also includes the "update" selector.
         *   @since v0.99.3
         */

        public void UnscheduleAll(ICCUpdatable target)
        {
            // explicit NULL handling
            if (target == null)
            {
                return;
            }

            // custom selectors
            HashTimeEntry element;

            if (hashForTimers.TryGetValue(target, out element))
            {
                if (element.Timers.Contains(element.CurrentTimer))
                {
                    element.CurrentTimerSalvaged = true;
                }
                element.Timers.Clear();

                if (currentTarget == element)
                {
                    isCurrentTargetSalvaged = true;
                }
                else
                {
                    RemoveHashElement(element);
                }
            }

            // update selector
            Unschedule(target);
        }
Exemplo n.º 3
0
        /** The scheduled method will be called every 'interval' seconds.
         * If paused is YES, then it won't be called until it is resumed.
         * If 'interval' is 0, it will be called every frame, but if so, it's recommended to use 'scheduleUpdateForTarget:' instead.
         * If the selector is already scheduled, then only the interval parameter will be updated without re-scheduling it again.
         * repeat let the action be repeated repeat + 1 times, use RepeatForever to let the action run continuously
         * delay is the amount of time the action will wait before it'll start
         *
         * @since v0.99.3, repeat and delay added in v1.1
         */

        public void Schedule(Action <float> selector, ICCUpdatable target, float interval, uint repeat,
                             float delay, bool paused)
        {
            Debug.Assert(selector != null);
            Debug.Assert(target != null);

            HashTimeEntry element;

            lock (hashForTimers)
            {
                if (!hashForTimers.TryGetValue(target, out element))
                {
                    element = new HashTimeEntry {
                        Target = target
                    };
                    hashForTimers[target] = element;

                    // Is this the 1st element ? Then set the pause level to all the selectors of this target
                    element.Paused = paused;
                }
                else
                {
                    if (element != null)
                    {
                        Debug.Assert(element.Paused == paused, "CCScheduler.Schedule: All are paused");
                    }
                }
                if (element != null)
                {
                    if (element.Timers == null)
                    {
                        element.Timers = new List <CCTimer>();
                    }
                    else
                    {
                        CCTimer[] timers = element.Timers.ToArray();
                        foreach (var timer in timers)
                        {
                            if (timer == null)
                            {
                                continue;
                            }
                            if (selector == timer.Selector)
                            {
                                CCLog.Log(
                                    "CCSheduler#scheduleSelector. Selector already scheduled. Updating interval from: {0} to {1}",
                                    timer.Interval, interval);
                                timer.Interval = interval;
                                return;
                            }
                        }
                    }

                    element.Timers.Add(new CCTimer(this, target, selector, interval, repeat, delay));
                }
            }
        }
Exemplo n.º 4
0
 public CCTimer(CCScheduler scheduler, ICCUpdatable target, Action <float> selector, float seconds,
                uint repeat, float delay)
 {
     this.Scheduler        = scheduler;
     this.target           = target;
     this.Selector         = selector;
     this.elapsed          = -1;
     this.OriginalInterval = seconds;
     this.Interval         = seconds;
     this.delay            = delay;
     this.useDelay         = delay > 0f;
     this.repeat           = repeat;
     this.runForever       = repeat == uint.MaxValue;
 }
Exemplo n.º 5
0
        public CCTimer(CCScheduler scheduler, ICCUpdatable target, Action<float> selector, float seconds,
                       uint repeat, float delay)
        {
			this.Scheduler = scheduler;
			this.target = target;
			this.Selector = selector;
			this.elapsed = -1;
			this.OriginalInterval = seconds;
			this.Interval = seconds;
			this.delay = delay;
			this.useDelay = delay > 0f;
			this.repeat = repeat;
			this.runForever = repeat == uint.MaxValue;
        }
Exemplo n.º 6
0
        /** Unschedule a selector for a given target.
         *   If you want to unschedule the "update", use unscheudleUpdateForTarget.
         *   @since v0.99.3
         */

        public void Unschedule(Action <float> selector, ICCUpdatable target)
        {
            // explicity handle nil arguments when removing an object
            if (selector == null || target == null)
            {
                return;
            }

            HashTimeEntry element;

            if (hashForTimers.TryGetValue(target, out element))
            {
                for (int i = 0; i < element.Timers.Count; i++)
                {
                    var timer = element.Timers[i];

                    if (selector == timer.Selector)
                    {
                        if (timer == element.CurrentTimer && (!element.CurrentTimerSalvaged))
                        {
                            element.CurrentTimerSalvaged = true;
                        }

                        element.Timers.RemoveAt(i);

                        // update timerIndex in case we are in tick:, looping over the actions
                        if (element.TimerIndex >= i)
                        {
                            element.TimerIndex--;
                        }

                        if (element.Timers.Count == 0)
                        {
                            if (currentTarget == element)
                            {
                                isCurrentTargetSalvaged = true;
                            }
                            else
                            {
                                RemoveHashElement(element);
                            }
                        }

                        return;
                    }
                }
            }
        }
Exemplo n.º 7
0
        void PriorityIn(LinkedList <ListEntry> list, ICCUpdatable target, int priority, bool paused)
        {
            var listElement = new ListEntry
            {
                Target            = target,
                Priority          = priority,
                Paused            = paused,
                MarkedForDeletion = false
            };

            if (list.First == null)
            {
                list.AddFirst(listElement);
            }
            else
            {
                bool added = false;
                for (LinkedListNode <ListEntry> node = list.First; node != null; node = node.Next)
                {
                    if (priority < node.Value.Priority)
                    {
                        list.AddBefore(node, listElement);
                        added = true;
                        break;
                    }
                }

                if (!added)
                {
                    list.AddLast(listElement);
                }
            }

            // update hash entry for quick access
            var hashElement = new HashUpdateEntry
            {
                Target = target,
                List   = list,
                Entry  = listElement
            };

            hashForUpdates.Add(target, hashElement);
        }
Exemplo n.º 8
0
        public void PauseTarget(ICCUpdatable target)
        {
            Debug.Assert(target != null);

            // custom selectors
            HashTimeEntry entry;

            if (hashForTimers.TryGetValue(target, out entry))
            {
                entry.Paused = true;
            }

            // Update selector
            HashUpdateEntry updateEntry;

            if (hashForUpdates.TryGetValue(target, out updateEntry))
            {
                updateEntry.Entry.Paused = true;
            }
        }
Exemplo n.º 9
0
        public void Resume(ICCUpdatable target)
        {
            Debug.Assert(target != null);

            // custom selectors
            HashTimeEntry element;

            if (hashForTimers.TryGetValue(target, out element))
            {
                element.Paused = false;
            }

            // Update selector
            HashUpdateEntry elementUpdate;

            if (hashForUpdates.TryGetValue(target, out elementUpdate))
            {
                elementUpdate.Entry.Paused = false;
            }
        }
Exemplo n.º 10
0
        void AppendIn(LinkedList <ListEntry> list, ICCUpdatable target, bool paused)
        {
            var listElement = new ListEntry
            {
                Target            = target,
                Paused            = paused,
                MarkedForDeletion = false
            };

            list.AddLast(listElement);

            // update hash entry for quicker access
            var hashElement = new HashUpdateEntry
            {
                Target = target,
                List   = list,
                Entry  = listElement
            };

            hashForUpdates.Add(target, hashElement);
        }
Exemplo n.º 11
0
        public void Unschedule(ICCUpdatable target)
        {
            if (target == null)
            {
                return;
            }

            HashUpdateEntry element;

            if (hashForUpdates.TryGetValue(target, out element))
            {
                if (isUpdateHashLocked)
                {
                    element.Entry.MarkedForDeletion = true;
                }
                else
                {
                    RemoveUpdateFromHash(element.Entry);
                }
            }
        }
Exemplo n.º 12
0
        public bool IsTargetPaused(ICCUpdatable target)
        {
            Debug.Assert(target != null, "target must be non nil");

            // Custom selectors
            HashTimeEntry element;

            if (hashForTimers.TryGetValue(target, out element))
            {
                return(element.Paused);
            }

            // We should check update selectors if target does not have custom selectors
            HashUpdateEntry elementUpdate;

            if (hashForUpdates.TryGetValue(target, out elementUpdate))
            {
                return(elementUpdate.Entry.Paused);
            }

            return(false); // should never get here
        }
Exemplo n.º 13
0
        internal void Update(float dt)
        {
            isUpdateHashLocked = true;

            try
            {
                if (TimeScale != 1.0f)
                {
                    dt *= TimeScale;
                }

                LinkedListNode <ListEntry> next;

                // updates with priority < 0
                //foreach (ListEntry entry in _updatesNegList)
                for (LinkedListNode <ListEntry> node = updatesNegList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (!node.Value.Paused && !node.Value.MarkedForDeletion)
                    {
                        node.Value.Target.Update(dt);
                    }
                }

                // updates with priority == 0
                //foreach (ListEntry entry in _updates0List)
                for (LinkedListNode <ListEntry> node = updates0List.First; node != null; node = next)
                {
                    next = node.Next;
                    if (!node.Value.Paused && !node.Value.MarkedForDeletion)
                    {
                        node.Value.Target.Update(dt);
                    }
                }

                // updates with priority > 0
                for (LinkedListNode <ListEntry> node = updatesPosList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (!node.Value.Paused && !node.Value.MarkedForDeletion)
                    {
                        node.Value.Target.Update(dt);
                    }
                }

                // Iterate over all the custom selectors
                var count = hashForTimers.Count;
                if (count > 0)
                {
                    if (tmpSelectorArray.Length < count)
                    {
                        tmpSelectorArray = new ICCUpdatable[tmpSelectorArray.Length * 2];
                    }
                    hashForTimers.Keys.CopyTo(tmpSelectorArray, 0);

                    for (int i = 0; i < count; i++)
                    {
                        ICCUpdatable key = tmpSelectorArray[i];
                        if (!hashForTimers.ContainsKey(key))
                        {
                            continue;
                        }
                        HashTimeEntry elt = hashForTimers[key];

                        currentTarget           = elt;
                        isCurrentTargetSalvaged = false;

                        if (!currentTarget.Paused)
                        {
                            // The 'timers' array may change while inside this loop
                            for (elt.TimerIndex = 0; elt.TimerIndex < elt.Timers.Count; ++elt.TimerIndex)
                            {
                                elt.CurrentTimer = elt.Timers[elt.TimerIndex];
                                if (elt.CurrentTimer != null)
                                {
                                    elt.CurrentTimerSalvaged = false;

                                    elt.CurrentTimer.Update(dt);

                                    elt.CurrentTimer = null;
                                }
                            }
                        }

                        // only delete currentTarget if no actions were scheduled during the cycle (issue #481)
                        if (isCurrentTargetSalvaged && currentTarget.Timers.Count == 0)
                        {
                            RemoveHashElement(currentTarget);
                        }
                    }
                }

                // delete all updates that are marked for deletion
                // updates with priority < 0
                for (LinkedListNode <ListEntry> node = updatesNegList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (node.Value.MarkedForDeletion)
                    {
                        updatesNegList.Remove(node);
                        RemoveUpdateFromHash(node.Value);
                    }
                }

                // updates with priority == 0
                for (LinkedListNode <ListEntry> node = updates0List.First; node != null; node = next)
                {
                    next = node.Next;
                    if (node.Value.MarkedForDeletion)
                    {
                        updates0List.Remove(node);
                        RemoveUpdateFromHash(node.Value);
                    }
                }

                // updates with priority > 0
                for (LinkedListNode <ListEntry> node = updatesPosList.First; node != null; node = next)
                {
                    next = node.Next;
                    if (node.Value.MarkedForDeletion)
                    {
                        updatesPosList.Remove(node);
                        RemoveUpdateFromHash(node.Value);
                    }
                }
            }
            finally
            {
                // Always do this just in case there is a problem

                isUpdateHashLocked = false;
                currentTarget      = null;
            }
        }
Exemplo n.º 14
0
        /** Schedules the 'update' selector for a given target with a given priority.
    	     The 'update' selector will be called every frame.
    	     The lower the priority, the earlier it is called.
    	     @since v0.99.3
    	     */

        public void Schedule (ICCUpdatable targt, int priority, bool paused)
        {
            HashUpdateEntry element;

            if (hashForUpdates.TryGetValue(targt, out element))
            {
                Debug.Assert(element.Entry.MarkedForDeletion);

                // TODO: check if priority has changed!
                element.Entry.MarkedForDeletion = false;

                return;
            }

            // most of the updates are going to be 0, that's way there
            // is an special list for updates with priority 0
            if (priority == 0)
            {
                AppendIn(updates0List, targt, paused);
            }
            else if (priority < 0)
            {
                PriorityIn(updatesNegList, targt, priority, paused);
            }
            else
            {
                PriorityIn(updatesPosList, targt, priority, paused);
            }
        }
Exemplo n.º 15
0
        /** Unschedule a selector for a given target.
    	     If you want to unschedule the "update", use unscheudleUpdateForTarget.
    	     @since v0.99.3
    	     */

		public void Unschedule (Action<float> selector, ICCUpdatable target)
        {
            // explicity handle nil arguments when removing an object
            if (selector == null || target == null)
            {
                return;
            }

            HashTimeEntry element;
            if (hashForTimers.TryGetValue(target, out element))
            {
                for (int i = 0; i < element.Timers.Count; i++)
                {
                    var timer = element.Timers[i];

                    if (selector == timer.Selector)
                    {
                        if (timer == element.CurrentTimer && (!element.CurrentTimerSalvaged))
                        {
                            element.CurrentTimerSalvaged = true;
                        }

                        element.Timers.RemoveAt(i);

                        // update timerIndex in case we are in tick:, looping over the actions
                        if (element.TimerIndex >= i)
                        {
                            element.TimerIndex--;
                        }

                        if (element.Timers.Count == 0)
                        {
                            if (currentTarget == element)
                            {
                                isCurrentTargetSalvaged = true;
                            }
                            else
                            {
                                RemoveHashElement(element);
                            }
                        }

                        return;
                    }
                }
            }
        }
Exemplo n.º 16
0
        /** Unschedules all selectors for a given target.
    	     This also includes the "update" selector.
    	     @since v0.99.3
    	     */

		public void UnscheduleAll (ICCUpdatable target)
        {
            // explicit NULL handling
            if (target == null)
            {
                return;
            }

            // custom selectors           
            HashTimeEntry element;

            if (hashForTimers.TryGetValue(target, out element))
            {
                if (element.Timers.Contains(element.CurrentTimer))
                {
                    element.CurrentTimerSalvaged = true;
                }
                element.Timers.Clear();

                if (currentTarget == element)
                {
                    isCurrentTargetSalvaged = true;
                }
                else
                {
                    RemoveHashElement(element);
                }
            }

            // update selector
            Unschedule(target);
        }
Exemplo n.º 17
0
        public void PauseTarget(ICCUpdatable target)
        {
            Debug.Assert(target != null);

            // custom selectors
            HashTimeEntry entry;
            if (hashForTimers.TryGetValue(target, out entry))
            {
                entry.Paused = true;
            }

            // Update selector
            HashUpdateEntry updateEntry;
            if (hashForUpdates.TryGetValue(target, out updateEntry))
            {
                updateEntry.Entry.Paused = true;
            }
        }
Exemplo n.º 18
0
		public void Unschedule (ICCUpdatable target)
        {
            if (target == null)
            {
                return;
            }

            HashUpdateEntry element;
            if (hashForUpdates.TryGetValue(target, out element))
            {
                if (isUpdateHashLocked)
                {
                    element.Entry.MarkedForDeletion = true;
                }
                else
                {
                    RemoveUpdateFromHash(element.Entry);
                }
            }
        }
Exemplo n.º 19
0
		public void Resume (ICCUpdatable target)
        {
            Debug.Assert(target != null);

            // custom selectors
            HashTimeEntry element;
            if (hashForTimers.TryGetValue(target, out element))
            {
                element.Paused = false;
            }

            // Update selector
            HashUpdateEntry elementUpdate;
            if (hashForUpdates.TryGetValue(target, out elementUpdate))
            {
                elementUpdate.Entry.Paused = false;
            }
        }
Exemplo n.º 20
0
 public CCTimer(CCScheduler scheduler, ICCUpdatable target, Action <float> selector, float seconds)
     : this(scheduler, target, selector, seconds, 0, 0)
 {
 }
Exemplo n.º 21
0
        public bool IsTargetPaused(ICCUpdatable target)
        {
            Debug.Assert(target != null, "target must be non nil");

            // Custom selectors
            HashTimeEntry element;
            if (hashForTimers.TryGetValue(target, out element))
            {
                return element.Paused;
            }

            // We should check update selectors if target does not have custom selectors
            HashUpdateEntry elementUpdate;
            if (hashForUpdates.TryGetValue(target, out elementUpdate))
            {
                return elementUpdate.Entry.Paused;
            }

            return false; // should never get here
        }
Exemplo n.º 22
0
 public CCTimer(CCScheduler scheduler, ICCUpdatable target, Action<float> selector, float seconds)
     : this(scheduler, target, selector, seconds, 0, 0)
 {
 }
Exemplo n.º 23
0
 public CCMenuItemLabelAtlas(string value, string charMapFile, int itemWidth, int itemHeight, char startCharMap, 
     ICCUpdatable updatable, Action<object> target) 
     : this(new CCLabelAtlas(value, charMapFile, itemWidth, itemHeight, startCharMap), target)
 {
 }
Exemplo n.º 24
0
        void AppendIn(LinkedList<ListEntry> list, ICCUpdatable target, bool paused)
        {
            var listElement = new ListEntry
                {
                    Target = target,
                    Paused = paused,
                    MarkedForDeletion = false
                };

            list.AddLast(listElement);

            // update hash entry for quicker access
            var hashElement = new HashUpdateEntry
                {
                    Target = target,
                    List = list,
                    Entry = listElement
                };

            hashForUpdates.Add(target, hashElement);
        }
Exemplo n.º 25
0
        void PriorityIn(LinkedList<ListEntry> list, ICCUpdatable target, int priority, bool paused)
        {
            var listElement = new ListEntry
                {
                    Target = target,
                    Priority = priority,
                    Paused = paused,
                    MarkedForDeletion = false
                };

            if (list.First == null)
            {
                list.AddFirst(listElement);
            }
            else
            {
                bool added = false;
                for (LinkedListNode<ListEntry> node = list.First; node != null; node = node.Next)
                {
                    if (priority < node.Value.Priority)
                    {
                        list.AddBefore(node, listElement);
                        added = true;
                        break;
                    }
                }

                if (!added)
                {
                    list.AddLast(listElement);
                }
            }

            // update hash entry for quick access
            var hashElement = new HashUpdateEntry
                {
                    Target = target,
                    List = list,
                    Entry = listElement
                };

            hashForUpdates.Add(target, hashElement);
        }
Exemplo n.º 26
0
 public CCMenuItemLabelAtlas(string value, string charMapFile, int itemWidth, int itemHeight, char startCharMap,
                             ICCUpdatable updatable, Action <object> target)
     : this(new CCLabelAtlas(value, charMapFile, itemWidth, itemHeight, startCharMap), target)
 {
 }
Exemplo n.º 27
0
        /** The scheduled method will be called every 'interval' seconds.
         If paused is YES, then it won't be called until it is resumed.
         If 'interval' is 0, it will be called every frame, but if so, it's recommended to use 'scheduleUpdateForTarget:' instead.
         If the selector is already scheduled, then only the interval parameter will be updated without re-scheduling it again.
         repeat let the action be repeated repeat + 1 times, use RepeatForever to let the action run continuously
         delay is the amount of time the action will wait before it'll start

         @since v0.99.3, repeat and delay added in v1.1
         */

        public void Schedule (Action<float> selector, ICCUpdatable target, float interval, uint repeat,
                                     float delay, bool paused)
        {
            Debug.Assert(selector != null);
            Debug.Assert(target != null);

            HashTimeEntry element;

            lock (hashForTimers)
            {
                if (!hashForTimers.TryGetValue(target, out element))
                {
                    element = new HashTimeEntry { Target = target };
                    hashForTimers[target] = element;

                    // Is this the 1st element ? Then set the pause level to all the selectors of this target
                    element.Paused = paused;
                }
                else
                {
                    if (element != null)
                    {
						Debug.Assert(element.Paused == paused, "CCScheduler.Schedule: All are paused");
                    }
                }
                if (element != null)
                {
                    if (element.Timers == null)
                    {
                        element.Timers = new List<CCTimer>();
                    }
                    else
                    {
                        CCTimer[] timers = element.Timers.ToArray();
                        foreach (var timer in timers)
                        {
                            if (timer == null)
                            {
                                continue;
                            }
                            if (selector == timer.Selector)
                            {
                                CCLog.Log(
                                    "CCSheduler#scheduleSelector. Selector already scheduled. Updating interval from: {0} to {1}",
                                    timer.Interval, interval);
                                timer.Interval = interval;
                                return;
                            }
                        }
                    }

                    element.Timers.Add(new CCTimer(this, target, selector, interval, repeat, delay));
                }
            }
        }