コード例 #1
0
        private void Tick(float dt)
        {
            _isDispatching = true;
            for (int index = 0; index < _listeners.Count; index++)
            {
                SchedulerEntry entry  = _listeners[index];
                var            status = GetStatusInPending(entry.Handler);
                if (status != SchedulerEntry.EntryStatus.ToDelete)                   //不是待移除状态才处理
                {
                    entry.Update(dt);
                }
            }
            _isDispatching = false;

            //deal with pendings
            int count = _pendings.Count;

            for (int i = 0; i < count; ++i)
            {
                if (_pendings[i].Status == SchedulerEntry.EntryStatus.ToInsert)
                {
                    _pendings[i].Status = SchedulerEntry.EntryStatus.Active;
                    _listeners.Add(_pendings[i]);
                }
                else if (_pendings[i].Status == SchedulerEntry.EntryStatus.ToDelete)
                {
                    SafeRemoveListener(_pendings[i].Handler);
                    _pool.DisposeObject(_pendings[i]);
                }
            }
            _pendings.Clear();
        }
コード例 #2
0
        private SchedulerEntry.EntryStatus GetStatusInPending(ScheduleHandler handler)
        {
            SchedulerEntry.EntryStatus status = SchedulerEntry.EntryStatus.Idle;
            int            count = _pendings.Count;
            SchedulerEntry entry = null;

            for (int i = count - 1; i > -1; --i)
            {
                entry = _pendings [i];
                if (entry.Handler == handler)
                {
                    if (entry.Status == SchedulerEntry.EntryStatus.ToInsert)
                    {
                        status = SchedulerEntry.EntryStatus.ToInsert;
                        break;
                    }
                    else if (entry.Status == SchedulerEntry.EntryStatus.ToDelete)
                    {
                        status = SchedulerEntry.EntryStatus.ToDelete;
                        break;
                    }
                }
            }
            return(status);
        }
コード例 #3
0
        public bool HasListener(ScheduleHandler handler)
        {
            bool bRet = false;

            for (int i = 0; i < _listeners.Count; i++)
            {
                SchedulerEntry entry = _listeners[i];
                if (entry.Handler == handler)
                {
                    bRet = true;
                    break;
                }
            }

            // when dispatching,check the toInsert,toDelete
            if (_isDispatching)
            {
                int count       = _pendings.Count;
                int insertTimes = 0;
                for (int i = 0; i < count; ++i)
                {
                    if (_pendings[i].Handler == handler)
                    {
                        if (_pendings[i].Status == SchedulerEntry.EntryStatus.ToInsert)
                        {
                            insertTimes++;
                            if (insertTimes > 1)
                            {
                                insertTimes = 1;
                            }
                        }
                        else if (_pendings[i].Status == SchedulerEntry.EntryStatus.ToDelete)
                        {
                            insertTimes--;
                            if (insertTimes < -1)
                            {
                                insertTimes = -1;
                            }
                        }
                    }
                }
                if (bRet && (insertTimes < 0))
                {
                    bRet = false;
                }
                if (!bRet && (insertTimes > 0))
                {
                    bRet = true;
                }
            }
            return(bRet);
        }
コード例 #4
0
 public void RemoveListener(ScheduleHandler handler)
 {
     if (_isDispatching)
     {
         SchedulerEntry entry = _pool.GetObject();
         entry.Status  = SchedulerEntry.EntryStatus.ToDelete;
         entry.Handler = handler;
         _pendings.Add(entry);
     }
     else
     {
         SafeRemoveListener(handler);
     }
 }
コード例 #5
0
        public void AddListener(ScheduleHandler handler, float interval = 0f)
        {
            if (HasListener(handler))
            {
                return;
            }
            SchedulerEntry entry = _pool.GetObject();

            entry.Elapsed  = 0;
            entry.Interval = interval;
            entry.Handler  = handler;
            if (!_isDispatching)
            {
                entry.Status = SchedulerEntry.EntryStatus.Active;
                _listeners.Add(entry);
            }
            else
            {
                entry.Status = SchedulerEntry.EntryStatus.ToDelete;
                _pendings.Add(entry);
            }
        }