1*f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f6e50924SAndrew Rist  * distributed with this work for additional information
6*f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9*f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f6e50924SAndrew Rist  *
11*f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f6e50924SAndrew Rist  *
13*f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15*f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f6e50924SAndrew Rist  * specific language governing permissions and limitations
18*f6e50924SAndrew Rist  * under the License.
19*f6e50924SAndrew Rist  *
20*f6e50924SAndrew Rist  *************************************************************/
21*f6e50924SAndrew Rist 
22*f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir #include <svx/sdr/animation/scheduler.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <vector>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
31cdf0e10cSrcweir // event class
32cdf0e10cSrcweir 
33cdf0e10cSrcweir namespace sdr
34cdf0e10cSrcweir {
35cdf0e10cSrcweir 	namespace animation
36cdf0e10cSrcweir 	{
Event(sal_uInt32 nTime)37cdf0e10cSrcweir 		Event::Event(sal_uInt32 nTime)
38cdf0e10cSrcweir 		:	mnTime(nTime),
39cdf0e10cSrcweir 			mpNext(0L)
40cdf0e10cSrcweir 		{
41cdf0e10cSrcweir 		}
42cdf0e10cSrcweir 
~Event()43cdf0e10cSrcweir 		Event::~Event()
44cdf0e10cSrcweir 		{
45cdf0e10cSrcweir 		}
46cdf0e10cSrcweir 
GetNext() const47cdf0e10cSrcweir 		Event* Event::GetNext() const
48cdf0e10cSrcweir 		{
49cdf0e10cSrcweir 			return mpNext;
50cdf0e10cSrcweir 		}
51cdf0e10cSrcweir 
SetNext(Event * pNew)52cdf0e10cSrcweir 		void Event::SetNext(Event* pNew)
53cdf0e10cSrcweir 		{
54cdf0e10cSrcweir 			if(pNew != mpNext)
55cdf0e10cSrcweir 			{
56cdf0e10cSrcweir 				mpNext = pNew;
57cdf0e10cSrcweir 			}
58cdf0e10cSrcweir 		}
59cdf0e10cSrcweir 
GetTime() const60cdf0e10cSrcweir 		sal_uInt32 Event::GetTime() const
61cdf0e10cSrcweir 		{
62cdf0e10cSrcweir 			return mnTime;
63cdf0e10cSrcweir 		}
64cdf0e10cSrcweir 
SetTime(sal_uInt32 nNew)65cdf0e10cSrcweir 		void Event::SetTime(sal_uInt32 nNew)
66cdf0e10cSrcweir 		{
67cdf0e10cSrcweir 			if(mnTime != nNew)
68cdf0e10cSrcweir 			{
69cdf0e10cSrcweir 				mnTime = nNew;
70cdf0e10cSrcweir 			}
71cdf0e10cSrcweir 		}
72cdf0e10cSrcweir 	} // end of namespace animation
73cdf0e10cSrcweir } // end of namespace sdr
74cdf0e10cSrcweir 
75cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
76cdf0e10cSrcweir // eventlist class
77cdf0e10cSrcweir 
78cdf0e10cSrcweir namespace sdr
79cdf0e10cSrcweir {
80cdf0e10cSrcweir 	namespace animation
81cdf0e10cSrcweir 	{
EventList()82cdf0e10cSrcweir 		EventList::EventList()
83cdf0e10cSrcweir 		:	mpHead(0L)
84cdf0e10cSrcweir 		{
85cdf0e10cSrcweir 		}
86cdf0e10cSrcweir 
~EventList()87cdf0e10cSrcweir 		EventList::~EventList()
88cdf0e10cSrcweir 		{
89cdf0e10cSrcweir 			Clear();
90cdf0e10cSrcweir 		}
91cdf0e10cSrcweir 
Insert(Event * pNew)92cdf0e10cSrcweir 		void EventList::Insert(Event* pNew)
93cdf0e10cSrcweir 		{
94cdf0e10cSrcweir 			if(pNew)
95cdf0e10cSrcweir 			{
96cdf0e10cSrcweir 				Event* pCurrent = mpHead;
97cdf0e10cSrcweir 				Event* pPrev = 0L;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 				while(pCurrent && pCurrent->GetTime() < pNew->GetTime())
100cdf0e10cSrcweir 				{
101cdf0e10cSrcweir 					pPrev = pCurrent;
102cdf0e10cSrcweir 					pCurrent = pCurrent->GetNext();
103cdf0e10cSrcweir 				}
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 				if(pPrev)
106cdf0e10cSrcweir 				{
107cdf0e10cSrcweir 					pNew->SetNext(pPrev->GetNext());
108cdf0e10cSrcweir 					pPrev->SetNext(pNew);
109cdf0e10cSrcweir 				}
110cdf0e10cSrcweir 				else
111cdf0e10cSrcweir 				{
112cdf0e10cSrcweir 					pNew->SetNext(mpHead);
113cdf0e10cSrcweir 					mpHead = pNew;
114cdf0e10cSrcweir 				}
115cdf0e10cSrcweir 			}
116cdf0e10cSrcweir 		}
117cdf0e10cSrcweir 
Remove(Event * pOld)118cdf0e10cSrcweir 		void EventList::Remove(Event* pOld)
119cdf0e10cSrcweir 		{
120cdf0e10cSrcweir 			if(pOld && mpHead)
121cdf0e10cSrcweir 			{
122cdf0e10cSrcweir 				Event* pCurrent = mpHead;
123cdf0e10cSrcweir 				Event* pPrev = 0L;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 				while(pCurrent && pCurrent != pOld)
126cdf0e10cSrcweir 				{
127cdf0e10cSrcweir 					pPrev = pCurrent;
128cdf0e10cSrcweir 					pCurrent = pCurrent->GetNext();
129cdf0e10cSrcweir 				}
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 				if(pPrev)
132cdf0e10cSrcweir 				{
133cdf0e10cSrcweir 					pPrev->SetNext(pOld->GetNext());
134cdf0e10cSrcweir 				}
135cdf0e10cSrcweir 				else
136cdf0e10cSrcweir 				{
137cdf0e10cSrcweir 					mpHead = pOld->GetNext();
138cdf0e10cSrcweir 				}
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 				pOld->SetNext(0L);
141cdf0e10cSrcweir 			}
142cdf0e10cSrcweir 		}
143cdf0e10cSrcweir 
Clear()144cdf0e10cSrcweir 		void EventList::Clear()
145cdf0e10cSrcweir 		{
146cdf0e10cSrcweir 			while(mpHead)
147cdf0e10cSrcweir 			{
148cdf0e10cSrcweir 				Event* pNext = mpHead->GetNext();
149cdf0e10cSrcweir 				mpHead->SetNext(0L);
150cdf0e10cSrcweir 				mpHead = pNext;
151cdf0e10cSrcweir 			}
152cdf0e10cSrcweir 		}
153cdf0e10cSrcweir 
GetFirst()154cdf0e10cSrcweir 		Event* EventList::GetFirst()
155cdf0e10cSrcweir 		{
156cdf0e10cSrcweir 			return mpHead;
157cdf0e10cSrcweir 		}
158cdf0e10cSrcweir 	} // end of namespace animation
159cdf0e10cSrcweir } // end of namespace sdr
160cdf0e10cSrcweir 
161cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
162cdf0e10cSrcweir // scheduler class
163cdf0e10cSrcweir 
164cdf0e10cSrcweir namespace sdr
165cdf0e10cSrcweir {
166cdf0e10cSrcweir 	namespace animation
167cdf0e10cSrcweir 	{
Scheduler()168cdf0e10cSrcweir 		Scheduler::Scheduler()
169cdf0e10cSrcweir 		:	mnTime(0L),
170cdf0e10cSrcweir 			mnDeltaTime(0L),
171cdf0e10cSrcweir 			mbIsPaused(false)
172cdf0e10cSrcweir 		{
173cdf0e10cSrcweir 		}
174cdf0e10cSrcweir 
~Scheduler()175cdf0e10cSrcweir 		Scheduler::~Scheduler()
176cdf0e10cSrcweir 		{
177cdf0e10cSrcweir 			Stop();
178cdf0e10cSrcweir 		}
179cdf0e10cSrcweir 
Timeout()180cdf0e10cSrcweir 		void Scheduler::Timeout()
181cdf0e10cSrcweir 		{
182cdf0e10cSrcweir 			// stop timer and add time
183cdf0e10cSrcweir 			Stop();
184cdf0e10cSrcweir 			mnTime += mnDeltaTime;
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 			// execute events
187cdf0e10cSrcweir 			triggerEvents();
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 			// re-start or stop timer according to event list
190cdf0e10cSrcweir 			checkTimeout();
191cdf0e10cSrcweir 		}
192cdf0e10cSrcweir 
triggerEvents()193cdf0e10cSrcweir 		void Scheduler::triggerEvents()
194cdf0e10cSrcweir 		{
195cdf0e10cSrcweir 			Event* pNextEvent = maList.GetFirst();
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 			if(pNextEvent)
198cdf0e10cSrcweir 			{
199cdf0e10cSrcweir 				// copy events which need to be executed to a vector. Remove them from
200cdf0e10cSrcweir 				// the scheduler
201cdf0e10cSrcweir 				::std::vector< Event* > EventPointerVector;
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 				while(pNextEvent && pNextEvent->GetTime() <= mnTime)
204cdf0e10cSrcweir 				{
205cdf0e10cSrcweir 					maList.Remove(pNextEvent);
206cdf0e10cSrcweir 					EventPointerVector.push_back(pNextEvent);
207cdf0e10cSrcweir 					pNextEvent = maList.GetFirst();
208cdf0e10cSrcweir 				}
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 				// execute events from the vector
211cdf0e10cSrcweir 				for(::std::vector< Event* >::iterator aCandidate = EventPointerVector.begin();
212cdf0e10cSrcweir 					aCandidate != EventPointerVector.end(); aCandidate++)
213cdf0e10cSrcweir 				{
214cdf0e10cSrcweir 					// trigger event. This may re-insert the event to the scheduler again
215cdf0e10cSrcweir 					(*aCandidate)->Trigger(mnTime);
216cdf0e10cSrcweir 				}
217cdf0e10cSrcweir 			}
218cdf0e10cSrcweir 		}
219cdf0e10cSrcweir 
checkTimeout()220cdf0e10cSrcweir 		void Scheduler::checkTimeout()
221cdf0e10cSrcweir 		{
222cdf0e10cSrcweir 			// re-start or stop timer according to event list
223cdf0e10cSrcweir 			if(!IsPaused() && maList.GetFirst())
224cdf0e10cSrcweir 			{
225cdf0e10cSrcweir 				mnDeltaTime = maList.GetFirst()->GetTime() - mnTime;
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 				if(0L != mnDeltaTime)
228cdf0e10cSrcweir 				{
229cdf0e10cSrcweir 					SetTimeout(mnDeltaTime);
230cdf0e10cSrcweir 					Start();
231cdf0e10cSrcweir 				}
232cdf0e10cSrcweir 			}
233cdf0e10cSrcweir 			else
234cdf0e10cSrcweir 			{
235cdf0e10cSrcweir 				Stop();
236cdf0e10cSrcweir 			}
237cdf0e10cSrcweir 		}
238cdf0e10cSrcweir 
GetTime()239cdf0e10cSrcweir 		sal_uInt32 Scheduler::GetTime()
240cdf0e10cSrcweir 		{
241cdf0e10cSrcweir 			return mnTime;
242cdf0e10cSrcweir 		}
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 		// #i38135#
SetTime(sal_uInt32 nTime)245cdf0e10cSrcweir 		void Scheduler::SetTime(sal_uInt32 nTime)
246cdf0e10cSrcweir 		{
247cdf0e10cSrcweir 			// reset time
248cdf0e10cSrcweir 			Stop();
249cdf0e10cSrcweir 			mnTime = nTime;
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 			// get event pointer
252cdf0e10cSrcweir 			Event* pEvent = maList.GetFirst();
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 			if(pEvent)
255cdf0e10cSrcweir 			{
256cdf0e10cSrcweir 				// retet event time points
257cdf0e10cSrcweir 				while(pEvent)
258cdf0e10cSrcweir 				{
259cdf0e10cSrcweir 					pEvent->SetTime(nTime);
260cdf0e10cSrcweir 					pEvent = pEvent->GetNext();
261cdf0e10cSrcweir 				}
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 				if(!IsPaused())
264cdf0e10cSrcweir 				{
265cdf0e10cSrcweir 					// without delta time, init events by triggering them. This will invalidate
266cdf0e10cSrcweir 					// painted objects and add them to the scheduler again
267cdf0e10cSrcweir 					mnDeltaTime = 0L;
268cdf0e10cSrcweir 					triggerEvents();
269cdf0e10cSrcweir 					checkTimeout();
270cdf0e10cSrcweir 				}
271cdf0e10cSrcweir 			}
272cdf0e10cSrcweir 		}
273cdf0e10cSrcweir 
Reset(sal_uInt32 nTime)274cdf0e10cSrcweir 		void Scheduler::Reset(sal_uInt32 nTime)
275cdf0e10cSrcweir 		{
276cdf0e10cSrcweir 			mnTime = nTime;
277cdf0e10cSrcweir 			mnDeltaTime = 0L;
278cdf0e10cSrcweir 			maList.Clear();
279cdf0e10cSrcweir 		}
280cdf0e10cSrcweir 
InsertEvent(Event * pNew)281cdf0e10cSrcweir 		void Scheduler::InsertEvent(Event* pNew)
282cdf0e10cSrcweir 		{
283cdf0e10cSrcweir 			if(pNew)
284cdf0e10cSrcweir 			{
285cdf0e10cSrcweir 				maList.Insert(pNew);
286cdf0e10cSrcweir 				checkTimeout();
287cdf0e10cSrcweir 			}
288cdf0e10cSrcweir 		}
289cdf0e10cSrcweir 
RemoveEvent(Event * pOld)290cdf0e10cSrcweir 		void Scheduler::RemoveEvent(Event* pOld)
291cdf0e10cSrcweir 		{
292cdf0e10cSrcweir 			if(pOld && maList.GetFirst())
293cdf0e10cSrcweir 			{
294cdf0e10cSrcweir 				maList.Remove(pOld);
295cdf0e10cSrcweir 				checkTimeout();
296cdf0e10cSrcweir 			}
297cdf0e10cSrcweir 		}
298cdf0e10cSrcweir 
SetPaused(bool bNew)299cdf0e10cSrcweir 		void Scheduler::SetPaused(bool bNew)
300cdf0e10cSrcweir 		{
301cdf0e10cSrcweir 			if(bNew != mbIsPaused)
302cdf0e10cSrcweir 			{
303cdf0e10cSrcweir 				mbIsPaused = bNew;
304cdf0e10cSrcweir 				checkTimeout();
305cdf0e10cSrcweir 			}
306cdf0e10cSrcweir 		}
307cdf0e10cSrcweir 	} // end of namespace animation
308cdf0e10cSrcweir } // end of namespace sdr
309cdf0e10cSrcweir 
310cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
311cdf0e10cSrcweir // eof
312