1 /*
2 Copyright (c) 2013 Andrey Penechko
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license the "Software" to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module anchovy.gui.timermanager;
30 
31 public import anchovy.gui.timer;
32 
33 import core.time;
34 import std.array;
35 import std.algorithm;
36 import std.math: isNaN;
37 import std.stdio;
38 
39 import anchovy.core.types;
40 
41 class TimerManager
42 {
43 	this(double delegate() currentTimeCallback)
44 	in
45 	{
46 		assert(currentTimeCallback);
47 	}
48 	body
49 	{
50 		freeTimers.reserve(128);
51 		queue.reserve(128);
52 		currentTime = currentTimeCallback;
53 	}
54 
55 	void updateTimers(double currentTime)
56 	{
57 		while(!queue.empty)
58 		{
59 			foreach(i, t; queue)
60 			{
61 				if (currentTime < t.nextUpdateTime)
62 					return;
63 				t.tick(currentTime);
64 				if (t.nextUpdateTime <= 0 || isNaN(t.nextUpdateTime))
65 				{
66 					freeTimers ~= t;
67 					queue = queue[0..i] ~ queue[i + 1..$];
68 					break;
69 				}
70 				if (t.tickType == TimerTickType.PROCESS_ALL_ORDERED)
71 				{
72 					break;
73 				}
74 			}
75 			sortTimers();
76 		}
77 	}
78 
79 	/// 	initialDelay can be used to specify first delay to be different from following, that are set with delay parameter.
80 	/// 				Must be not NaN and > 0 to be used as first delay.
81 	Timer addTimer(double _delay, TimerHandler _handler, double _initialDelay = double.nan, TimerTickType _tickType = TimerTickType.init)
82 	{
83 		Timer timer = popFreeTimer();
84 
85 		double startTime = currentTime();
86 
87 		if (!isNaN(_initialDelay) || _initialDelay < 0)
88 		{
89 			startTime += _initialDelay;
90 		}
91 		else
92 			startTime += _delay;
93 
94 		timer.init(startTime, currentTime(), _delay, _handler, _tickType);
95 		addToQueue(timer);
96 
97 		return timer;
98 	}
99 
100 	/// Resets timer's delay to newDelay if > 0 or to timer.delay otherwise.
101 	/// 
102 	/// Timer.delay will not be changed. Timer.nextUpdate only chabges.
103 	/// If you wish change Timer.delay you can do this by returning new delay in timer callback or 
104 	/// by setting it directly trough the reference returned by addTimer.
105 	void resetTimer(Timer timer, double newDelay = double.nan)
106 	{
107 		double _delay = newDelay;
108 		if (!(_delay > 0)) _delay = timer.delay;
109 		timer.nextUpdateTime = currentTime() + _delay;
110 
111 		sortTimers();
112 	}
113 
114 	void stopTimer(Timer timer)
115 	{
116 		foreach(i, ref t; queue)
117 		{
118 			if (t == timer)
119 			{
120 				freeTimers ~= t;
121 				queue = queue[0..i] ~ queue[i + 1..$];
122 				return;
123 			}
124 		}
125 
126 		debug throw new Exception("Tried to stop not running timer");
127 	}
128 
129 protected:
130 
131 	Timer popFreeTimer()
132 	{
133 		if (freeTimers.length > 0)
134 		{
135 			scope(exit) freeTimers.popBack;
136 			return freeTimers.back;
137 		}
138 		else
139 		{
140 			return new Timer();
141 		}
142 	}
143 
144 	/// timer must be previously removed from freeTimers.
145 	void addToQueue(Timer timer)
146 	{
147 		queue ~= timer;
148 		sortTimers();
149 	}
150 
151 	void sortTimers()
152 	{
153 		sort!("a.nextUpdateTime<b.nextUpdateTime")(queue);
154 	}
155 
156 	Timer[]	freeTimers;
157 	Timer[] queue;
158 
159 	double delegate()	currentTime;
160 }