/src/libreoffice/svx/source/sdr/animation/scheduler.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | |
20 | | #include <svx/sdr/animation/scheduler.hxx> |
21 | | |
22 | | #include <algorithm> |
23 | | #include <vector> |
24 | | |
25 | | |
26 | | // event class |
27 | | |
28 | | namespace sdr::animation |
29 | | { |
30 | 0 | Event::Event() : mnTime(0) |
31 | 0 | { |
32 | 0 | } |
33 | | |
34 | | Event::~Event() |
35 | 0 | { |
36 | 0 | } |
37 | | |
38 | | |
39 | | void Event::SetTime(sal_uInt32 nNew) |
40 | 0 | { |
41 | 0 | if(mnTime != nNew) |
42 | 0 | { |
43 | 0 | mnTime = nNew; |
44 | 0 | } |
45 | 0 | } |
46 | | |
47 | | Scheduler::Scheduler() |
48 | 62.8k | : Timer("sdr::animation::Scheduler"), |
49 | 62.8k | mnTime(0), |
50 | 62.8k | mnDeltaTime(0), |
51 | 62.8k | mbIsPaused(false) |
52 | 62.8k | { |
53 | 62.8k | SetPriority(TaskPriority::POST_PAINT); |
54 | 62.8k | } |
55 | | |
56 | | Scheduler::~Scheduler() |
57 | 62.8k | { |
58 | 62.8k | Stop(); |
59 | 62.8k | } |
60 | | |
61 | | void Scheduler::Invoke() |
62 | 0 | { |
63 | | // stop timer and add time |
64 | 0 | Stop(); |
65 | 0 | mnTime += mnDeltaTime; |
66 | | |
67 | | // execute events |
68 | 0 | triggerEvents(); |
69 | | |
70 | | // re-start or stop timer according to event list |
71 | 0 | checkTimeout(); |
72 | 0 | } |
73 | | |
74 | | void Scheduler::triggerEvents() |
75 | 0 | { |
76 | 0 | if (mvEvents.empty()) |
77 | 0 | return; |
78 | | |
79 | | // copy events which need to be executed to a vector. Remove them from |
80 | | // the scheduler |
81 | 0 | ::std::vector< Event* > aToBeExecutedList; |
82 | |
|
83 | 0 | while(!mvEvents.empty() && mvEvents.front()->GetTime() <= mnTime) |
84 | 0 | { |
85 | 0 | Event* pNextEvent = mvEvents.front(); |
86 | 0 | mvEvents.erase(mvEvents.begin()); |
87 | 0 | aToBeExecutedList.push_back(pNextEvent); |
88 | 0 | } |
89 | | |
90 | | // execute events from the vector |
91 | 0 | for(auto& rpCandidate : aToBeExecutedList) |
92 | 0 | { |
93 | | // trigger event. This may re-insert the event to the scheduler again |
94 | 0 | rpCandidate->Trigger(mnTime); |
95 | 0 | } |
96 | 0 | } |
97 | | |
98 | | void Scheduler::checkTimeout() |
99 | 0 | { |
100 | | // re-start or stop timer according to event list |
101 | 0 | if(!IsPaused() && !mvEvents.empty()) |
102 | 0 | { |
103 | 0 | mnDeltaTime = mvEvents.front()->GetTime() - mnTime; |
104 | |
|
105 | 0 | if(0 != mnDeltaTime) |
106 | 0 | { |
107 | 0 | SetTimeout(mnDeltaTime); |
108 | 0 | Start(); |
109 | 0 | } |
110 | 0 | } |
111 | 0 | else |
112 | 0 | { |
113 | 0 | Stop(); |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | | |
118 | | // #i38135# |
119 | | void Scheduler::SetTime(sal_uInt32 nTime) |
120 | 0 | { |
121 | | // reset time |
122 | 0 | Stop(); |
123 | 0 | mnTime = nTime; |
124 | |
|
125 | 0 | if (mvEvents.empty()) |
126 | 0 | return; |
127 | | |
128 | | // reset event time points |
129 | 0 | for (auto & rEvent : mvEvents) |
130 | 0 | { |
131 | 0 | rEvent->SetTime(nTime); |
132 | 0 | } |
133 | |
|
134 | 0 | if(!IsPaused()) |
135 | 0 | { |
136 | | // without delta time, init events by triggering them. This will invalidate |
137 | | // painted objects and add them to the scheduler again |
138 | 0 | mnDeltaTime = 0; |
139 | 0 | triggerEvents(); |
140 | 0 | checkTimeout(); |
141 | 0 | } |
142 | 0 | } |
143 | | |
144 | | void Scheduler::InsertEvent(Event& rNew) |
145 | 0 | { |
146 | | // insert maintaining time ordering |
147 | 0 | auto it = std::find_if(mvEvents.begin(), mvEvents.end(), |
148 | 0 | [&rNew](const Event* pEvent) { return rNew.GetTime() < pEvent->GetTime(); }); |
149 | 0 | mvEvents.insert(it, &rNew); |
150 | 0 | checkTimeout(); |
151 | 0 | } |
152 | | |
153 | | void Scheduler::RemoveEvent(Event* pOld) |
154 | 0 | { |
155 | 0 | if(!mvEvents.empty()) |
156 | 0 | { |
157 | 0 | std::erase(mvEvents, pOld); |
158 | 0 | checkTimeout(); |
159 | 0 | } |
160 | 0 | } |
161 | | |
162 | | void Scheduler::SetPaused(bool bNew) |
163 | 0 | { |
164 | 0 | if(bNew != mbIsPaused) |
165 | 0 | { |
166 | 0 | mbIsPaused = bNew; |
167 | 0 | checkTimeout(); |
168 | 0 | } |
169 | 0 | } |
170 | | |
171 | | } // end of namespace |
172 | | |
173 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |