Beispiel #1
0
 public void Subscribe(IScheduleHandler <T> handler)
 {
     if (currentHandler == null)
     {
         currentHandler = handler;
     }
 }
        private void Remove(JWArrayList <Data> datas, IScheduleHandler handler)
        {
            for (int i = 0; i < datas.Count; i++)
            {
                if (datas[i].Handler != handler)
                {
                    continue;
                }

                Data data = datas[i];
                data.Handler = null;
                datas[i]     = data;
            }
        }
        /// <summary>
        /// 移除更新
        /// </summary>
        /// <param name="handler">更新回调接口</param>
        /// <returns></returns>
        public void RemoveUpdate(IScheduleHandler handler)
        {
            if (handler == null)
            {
                return;
            }

            for (int i = 0; i < _updates.Count; i++)
            {
                if (_updates[i] == handler)
                {
                    _updates[i] = null;
                }
            }
        }
        /// <summary>
        /// 添加定帧回调
        /// </summary>
        /// <param name="interval">间隔帧数</param>
        /// <param name="repeat">是否重复</param>
        /// <param name="handler">回调接口</param>
        /// <returns>定帧ID(==0添加失败)</returns>
        public uint AddFrame(int interval, bool repeat, IScheduleHandler handler)
        {
            if (handler == null)
            {
                return(0);
            }

            Data data;

            data.ID       = _currentID++;
            data.Interval = interval;
            data.Remain   = interval;
            data.Repeat   = repeat;
            data.Handler  = handler;
            _frames.Add(data);

            return(data.ID);
        }
        /// <summary>
        /// 添加定时回调
        /// </summary>
        /// <param name="interval">定时器间隔,单位:ms</param>
        /// <param name="repeat">是否重复触发</param>
        /// <param name="handler">回调接口</param>
        /// <returns>定时器ID(==0添加失败)</returns>
        public uint AddTimer(int interval, bool repeat, IScheduleHandler handler)
        {
            if (interval <= 0 || handler == null)
            {
                return(0);
            }

            interval *= 100;

            Data data;

            data.ID       = _currentID++;
            data.Interval = interval;
            data.Remain   = interval;
            data.Repeat   = repeat;
            data.Handler  = handler;
            _timers.Add(data);

            return(data.ID);
        }
        /// <summary>
        /// 添加更新
        /// </summary>
        /// <param name="handler">更新回调接口</param>
        /// <param name="isFirst">添加到List头部,目前用于Time更新,其余情况不要使用</param>
        /// <returns></returns>
        public void AddUpdate(IScheduleHandler handler, bool isFirst = false)
        {
            if (handler == null)
            {
                return;
            }

            for (int i = 0; i < _updates.Count; i++)
            {
                if (_updates[i] == handler)
                {
                    return;
                }
            }

            if (isFirst)
            {
                _updates.Insert(0, handler);
            }
            else
            {
                _updates.Add(handler);
            }
        }
 public ScheduleCollection(IScheduleHandler scheduleHandler)
 {
     _scheduleHandler = scheduleHandler;
 }
Beispiel #8
0
 public SchedulerController(IScheduleHandler scheduleHandler, INotifier notifier, INotificationQueue queue)
 {
     _scheduleHandler = scheduleHandler;
     _notifier        = notifier;
     _queue           = queue;
 }
        /// <summary>
        /// Mono Behavior 驱动
        /// </summary>
        protected void Update()
        {
            // Update
            bool updateDiscard = false;

            int count = _updates.Count;

            for (int i = 0; i < count && i < _updates.Count; i++)
            {
                IScheduleHandler handler = _updates[i];
                if (handler != null)
                {
                    try
                    {
                        handler.OnScheduleHandle(ScheduleType.Updator, 0);
                    }
                    catch (Exception e)
                    {
                        JW.Common.Log.LogE("ScheduleService.Update : update exception {0} at handler {1}", e, handler.GetType().Name);
                        throw;
                    }
                }
                else
                {
                    updateDiscard = true;
                }
            }

            // Timer
            int deltaTime = (int)(Time.deltaTime * 100000.0f);

            bool timerDiscard = false;

            count = _timers.Count;
            for (int i = 0; i < count && i < _timers.Count; i++)
            {
                Data             data    = _timers[i];
                IScheduleHandler handler = data.Handler;
                if (handler == null)
                {
                    timerDiscard = true;
                    continue;
                }

                data.Remain -= deltaTime;
                if (data.Remain > 0)
                {
                    _timers[i] = data;
                    continue;
                }

                if (data.Repeat)
                {
                    data.Remain += data.Interval;
                }
                else
                {
                    data.Handler = null;
                }

                _timers[i] = data;

                try
                {
                    handler.OnScheduleHandle(ScheduleType.Timer, data.ID);
                }
                catch (Exception e)
                {
                    JW.Common.Log.LogE("ScheduleService.Update : timer exception {0} at handler {1}", e, handler.GetType().Name);
                    throw;
                }
            }

            // 定帧
            bool frameDiscard = false;

            count = _frames.Count;
            for (int i = 0; i < count && i < _frames.Count; i++)
            {
                Data             data    = _frames[i];
                IScheduleHandler handler = data.Handler;
                if (handler == null)
                {
                    frameDiscard = true;
                    continue;
                }

                --data.Remain;
                if (data.Remain > 0)
                {
                    _frames[i] = data;
                    continue;
                }

                if (data.Repeat)
                {
                    data.Remain = data.Interval;
                }
                else
                {
                    data.Handler = null;
                }

                _frames[i] = data;

                try
                {
                    handler.OnScheduleHandle(ScheduleType.Frame, data.ID);
                }
                catch (Exception e)
                {
                    JW.Common.Log.LogE("ScheduleService.Update : frame exception {0} at handler {1}", e, handler.GetType().Name);
                    throw;
                }
            }

            // remove discard
            if (updateDiscard)
            {
                for (int i = _updates.Count - 1; i >= 0; --i)
                {
                    if (_updates[i] == null)
                    {
                        _updates.RemoveAt(i);
                    }
                }
            }

            if (timerDiscard)
            {
                for (int i = _timers.Count - 1; i >= 0; --i)
                {
                    if (_timers[i].Handler == null)
                    {
                        _timers.RemoveAt(i);
                    }
                }
            }

            if (frameDiscard)
            {
                for (int i = _frames.Count - 1; i >= 0; --i)
                {
                    if (_frames[i].Handler == null)
                    {
                        _frames.RemoveAt(i);
                    }
                }
            }
        }
 /// <summary>
 /// 移除回调接口相关的所有定帧回调
 /// </summary>
 /// <param name="handler">回调接口</param>
 /// <returns></returns>
 public void RemoveFrame(IScheduleHandler handler)
 {
     Remove(_frames, handler);
 }
 /// <summary>
 /// 移除回调接口相关的所有定时回调
 /// </summary>
 /// <param name="handler">回调接口</param>
 /// <returns></returns>
 public void RemoveTimer(IScheduleHandler handler)
 {
     Remove(_timers, handler);
 }
Beispiel #12
0
 public static void RemoveAllSchedule(this IScheduleHandler handler)
 {
     handler.RemoveUpdate();
     handler.RemoveTimer();
     handler.RemoveFrame();
 }
Beispiel #13
0
 public static void RemoveFrame(this IScheduleHandler handler)
 {
     ScheduleService.GetInstance().RemoveFrame(handler);
 }
Beispiel #14
0
 public static uint AddFrame(this IScheduleHandler handler, int interval, bool repeat)
 {
     return(ScheduleService.GetInstance().AddFrame(interval, repeat, handler));
 }
Beispiel #15
0
 public static void RemoveTimer(this IScheduleHandler handler, uint id)
 {
     ScheduleService.GetInstance().RemoveTimer(id);
 }
Beispiel #16
0
 public static void AddUpdate(this IScheduleHandler handler, bool isFirst = false)
 {
     ScheduleService.GetInstance().AddUpdate(handler, isFirst);
 }