Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / svx / source / sdr / animation / scheduler.cxx
blob9936dfdb4be6865cee6bcb7c588e72f1d7713bc8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <svx/sdr/animation/scheduler.hxx>
22 #include <vector>
25 // event class
27 namespace sdr
29 namespace animation
31 Event::Event() : mnTime(0)
35 Event::~Event()
40 void Event::SetTime(sal_uInt32 nNew)
42 if(mnTime != nNew)
44 mnTime = nNew;
48 bool CompareEvent::operator()(Event* const& lhs, Event* const& rhs) const
50 return lhs->GetTime() < rhs->GetTime();
54 Scheduler::Scheduler()
55 : mnTime(0L),
56 mnDeltaTime(0L),
57 mbIsPaused(false)
61 Scheduler::~Scheduler()
63 Stop();
66 void Scheduler::Invoke()
68 // stop timer and add time
69 Stop();
70 mnTime += mnDeltaTime;
72 // execute events
73 triggerEvents();
75 // re-start or stop timer according to event list
76 checkTimeout();
79 void Scheduler::triggerEvents()
81 if (maList.empty())
82 return;
84 // copy events which need to be executed to a vector. Remove them from
85 // the scheduler
86 ::std::vector< Event* > aToBeExecutedList;
88 while(!maList.empty() && maList[0]->GetTime() <= mnTime)
90 Event* pNextEvent = maList.front();
91 maList.erase(maList.begin());
92 aToBeExecutedList.push_back(pNextEvent);
95 // execute events from the vector
96 ::std::vector< Event* >::const_iterator aEnd = aToBeExecutedList.end();
97 for(::std::vector< Event* >::iterator aCandidate = aToBeExecutedList.begin();
98 aCandidate != aEnd; ++aCandidate)
100 // trigger event. This may re-insert the event to the scheduler again
101 (*aCandidate)->Trigger(mnTime);
105 void Scheduler::checkTimeout()
107 // re-start or stop timer according to event list
108 if(!IsPaused() && !maList.empty())
110 mnDeltaTime = maList.front()->GetTime() - mnTime;
112 if(0L != mnDeltaTime)
114 SetTimeout(mnDeltaTime);
115 Start();
118 else
120 Stop();
125 // #i38135#
126 void Scheduler::SetTime(sal_uInt32 nTime)
128 // reset time
129 Stop();
130 mnTime = nTime;
132 if (maList.empty())
133 return;
135 // reset event time points
136 for (auto & rEvent : maList)
138 rEvent->SetTime(nTime);
141 if(!IsPaused())
143 // without delta time, init events by triggering them. This will invalidate
144 // painted objects and add them to the scheduler again
145 mnDeltaTime = 0L;
146 triggerEvents();
147 checkTimeout();
151 void Scheduler::InsertEvent(Event* pNew)
153 maList.insert(pNew);
154 checkTimeout();
157 void Scheduler::RemoveEvent(Event* pOld)
159 if(!maList.empty())
161 auto it = maList.find(pOld);
162 if (it != maList.end())
163 maList.erase(it);
164 checkTimeout();
168 void Scheduler::SetPaused(bool bNew)
170 if(bNew != mbIsPaused)
172 mbIsPaused = bNew;
173 checkTimeout();
176 } // end of namespace animation
177 } // end of namespace sdr
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */