1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Damian 'Mozork' Frick |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "OrxonoxStableHeaders.h" |
---|
30 | #include "NotificationQueue.h" |
---|
31 | |
---|
32 | #include <OgreOverlayManager.h> |
---|
33 | #include <OgreTextAreaOverlayElement.h> |
---|
34 | #include <list> |
---|
35 | |
---|
36 | #include "core/CoreIncludes.h" |
---|
37 | #include "core/XMLPort.h" |
---|
38 | |
---|
39 | #include "Notification.h" |
---|
40 | #include "NotificationOverlay.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | |
---|
45 | CreateFactory(NotificationQueue); |
---|
46 | |
---|
47 | const std::string NotificationQueue::DEFAULT_FONT = "VeraMono"; |
---|
48 | |
---|
49 | /** |
---|
50 | @brief |
---|
51 | Constructor. Creates and initializes the object. |
---|
52 | */ |
---|
53 | NotificationQueue::NotificationQueue(BaseObject* creator) : OverlayGroup(creator) |
---|
54 | { |
---|
55 | this->initialize(); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | @brief |
---|
60 | Destructor. |
---|
61 | @todo |
---|
62 | I'm pretty sure that there are some thing that have to be distroyed. |
---|
63 | */ |
---|
64 | NotificationQueue::~NotificationQueue() |
---|
65 | { |
---|
66 | |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | @brief |
---|
71 | Initializes the object. |
---|
72 | Registers the object, initializes variables, sets default values and registers the queue with the NotificationManager. |
---|
73 | */ |
---|
74 | void NotificationQueue::initialize(void) |
---|
75 | { |
---|
76 | RegisterObject(NotificationQueue); |
---|
77 | |
---|
78 | this->setDefaults(); |
---|
79 | this->size_ = 0; |
---|
80 | this->tickTime_ = 0.0; |
---|
81 | |
---|
82 | NotificationManager::registerQueue(this); |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | @brief |
---|
87 | Sets the defaults. |
---|
88 | */ |
---|
89 | void NotificationQueue::setDefaults(void) |
---|
90 | { |
---|
91 | this->setMaxSize(DEFAULT_SIZE); |
---|
92 | this->setNotificationLength(DEFAULT_LENGTH); |
---|
93 | this->setDisplayTime(DEFAULT_DISPLAY_TIME); |
---|
94 | |
---|
95 | this->setTargets(NotificationManager::ALL); |
---|
96 | |
---|
97 | this->setFontSize(DEFAULT_FONT_SIZE); |
---|
98 | this->setFont(DEFAULT_FONT); |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | @brief |
---|
103 | Method for creating a NotificationQueue object through XML. |
---|
104 | */ |
---|
105 | void NotificationQueue::XMLPort(Element& xmlElement, XMLPort::Mode mode) |
---|
106 | { |
---|
107 | SUPER(NotificationQueue, XMLPort, xmlElement, mode); |
---|
108 | |
---|
109 | XMLPortParam(NotificationQueue, "maxSize", setMaxSize, getMaxSize, xmlElement, mode); |
---|
110 | XMLPortParam(NotificationQueue, "notificationLength", setNotificationLength, getNotificationLength, xmlElement, mode); |
---|
111 | XMLPortParam(NotificationQueue, "displayTime", setDisplayTime, getDisplayTime, xmlElement, mode); |
---|
112 | XMLPortParam(NotificationQueue, "targets", setTargets, getTargets, xmlElement, mode); |
---|
113 | XMLPortParam(NotificationQueue, "font", setFont, getFont, xmlElement, mode); |
---|
114 | XMLPortParam(NotificationQueue, "fontSize", setFontSize, getFontSize, xmlElement, mode); |
---|
115 | |
---|
116 | COUT(3) << "NotificationQueue created." << std::endl; |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | @brief |
---|
121 | Updates the queue from time to time. |
---|
122 | @param dt |
---|
123 | The time interval that has passed since the last tick. |
---|
124 | */ |
---|
125 | void NotificationQueue::tick(float dt) |
---|
126 | { |
---|
127 | this->tickTime_ += dt; //!< Add the time interval that has passed to the time counter. |
---|
128 | if(this->tickTime_ >= 1.0) //!< If the time counter is greater than 1s all Notifications that have expired are removed, if it is smaller we wait to the next tick. |
---|
129 | { |
---|
130 | this->timeLimit_.time = std::time(0)-this->displayTime_; //!< Container containig the current time. |
---|
131 | |
---|
132 | std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it; |
---|
133 | it = this->containers_.begin(); |
---|
134 | while(it != this->containers_.upper_bound(&this->timeLimit_)) //!< Iterate through all elements whose creation time is smaller than the current time minus the display time. |
---|
135 | { |
---|
136 | this->removeContainer(*it); |
---|
137 | it = this->containers_.begin(); //TDO: Needed? |
---|
138 | } |
---|
139 | |
---|
140 | this->tickTime_ = 0.0; //!< Reset time counter. |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | @brief |
---|
146 | Updates the NotificationQueue. |
---|
147 | Updates by clearing the queue and requesting all relevant Notifications from the NotificationManager and inserting the in the queue. |
---|
148 | */ |
---|
149 | void NotificationQueue::update(void) |
---|
150 | { |
---|
151 | this->clear(); |
---|
152 | |
---|
153 | std::multimap<std::time_t,Notification*>* notifications = NotificationManager::getNotifications(this, this->displayTime_); |
---|
154 | |
---|
155 | if(notifications == NULL) |
---|
156 | return; |
---|
157 | |
---|
158 | for(std::multimap<std::time_t,Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) |
---|
159 | { |
---|
160 | this->addNotification(it->second, it->first); |
---|
161 | } |
---|
162 | |
---|
163 | COUT(3) << "NotificationQueue updated." << std::endl; |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | @brief |
---|
168 | Updates the NotificationQueue by adding an new Notification. |
---|
169 | @param notification |
---|
170 | Pointer to the Notification. |
---|
171 | @param time |
---|
172 | The time the Notification was sent. |
---|
173 | */ |
---|
174 | void NotificationQueue::update(Notification* notification, const std::time_t & time) |
---|
175 | { |
---|
176 | this->addNotification(notification, time); |
---|
177 | |
---|
178 | //TDO: Position! |
---|
179 | |
---|
180 | std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it; |
---|
181 | while(this->getSize() > this->getMaxSize()) |
---|
182 | { |
---|
183 | it = this->containers_.begin(); |
---|
184 | this->removeContainer(*it); |
---|
185 | } |
---|
186 | |
---|
187 | COUT(3) << "NotificationQueue updated. A new Notifications has been added." << std::endl; |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | @brief |
---|
192 | Sets the maximum number of displayed Notifications. |
---|
193 | @param size |
---|
194 | The size to be set. |
---|
195 | @return |
---|
196 | Returns true if successful. |
---|
197 | */ |
---|
198 | bool NotificationQueue::setMaxSize(int size) |
---|
199 | { |
---|
200 | if(size < 0) |
---|
201 | return false; |
---|
202 | this->maxSize_ = size; |
---|
203 | this->update(); |
---|
204 | return true; |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | @brief |
---|
209 | Sets the maximum number of characters a Notification message displayed by this queue is allowed to have. |
---|
210 | @param length |
---|
211 | The length to be set. |
---|
212 | @return |
---|
213 | Returns true if successful. |
---|
214 | */ |
---|
215 | bool NotificationQueue::setNotificationLength(int length) |
---|
216 | { |
---|
217 | if(length < 0) |
---|
218 | return false; |
---|
219 | this->notificationLength_ = length; |
---|
220 | this->update(); |
---|
221 | return true; |
---|
222 | } |
---|
223 | |
---|
224 | /** |
---|
225 | @brief |
---|
226 | Sets the maximum number of seconds a Notification is displayed. |
---|
227 | @param time |
---|
228 | The number of seconds the Notifications is displayed. |
---|
229 | @return |
---|
230 | Returns true if successful. |
---|
231 | */ |
---|
232 | bool NotificationQueue::setDisplayTime(int time) |
---|
233 | { |
---|
234 | if(time < 0) |
---|
235 | return false; |
---|
236 | this->displayTime_ = time; |
---|
237 | this->update(); |
---|
238 | return true; |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | @brief |
---|
243 | Returns all targets concatinated as string, with kommas (',') as seperators. |
---|
244 | @return |
---|
245 | Returns all targets concatinated as string, with kommas (',') as seperators. |
---|
246 | @todo |
---|
247 | Where is the string deleted? |
---|
248 | */ |
---|
249 | const std::string & NotificationQueue::getTargets() const |
---|
250 | { |
---|
251 | std::string* pTemp = new std::string(""); |
---|
252 | bool first = true; |
---|
253 | for(std::set<std::string>::iterator it = this->targets_.begin(); it != this->targets_.end(); it++) //!< Iterate through the set of targets. |
---|
254 | { |
---|
255 | if(!first) |
---|
256 | { |
---|
257 | *pTemp += ","; |
---|
258 | } |
---|
259 | else |
---|
260 | { |
---|
261 | first = false; |
---|
262 | } |
---|
263 | *pTemp += *it; |
---|
264 | } |
---|
265 | |
---|
266 | return *pTemp; |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | @brief |
---|
271 | Sets the targets of the queue. |
---|
272 | The targets are the senders whose Notifications are displayed in this queue. |
---|
273 | @param targets |
---|
274 | Accepts a string of targets, each seperated by commas (','), spaces are ignored. |
---|
275 | @return |
---|
276 | Returns true if successful. |
---|
277 | */ |
---|
278 | bool NotificationQueue::setTargets(const std::string & targets) |
---|
279 | { |
---|
280 | std::string* pTemp; |
---|
281 | unsigned int index = 0; |
---|
282 | while( index < targets.size() ) //!< Go through the string, character by character until the end is reached. |
---|
283 | { |
---|
284 | pTemp = new std::string(""); |
---|
285 | while(targets[index] != ',' && targets[index] != ' ' && index < targets.size()) |
---|
286 | { |
---|
287 | *pTemp += targets[index]; |
---|
288 | index++; |
---|
289 | } |
---|
290 | this->targets_.insert(*pTemp); |
---|
291 | } |
---|
292 | |
---|
293 | return true; |
---|
294 | } |
---|
295 | |
---|
296 | /** |
---|
297 | @brief |
---|
298 | Sets the font size. |
---|
299 | @param size |
---|
300 | The font size. |
---|
301 | @return |
---|
302 | Returns true if successful. |
---|
303 | */ |
---|
304 | bool NotificationQueue::setFontSize(float size) |
---|
305 | { |
---|
306 | if(size <= 0) |
---|
307 | return false; |
---|
308 | this->fontSize_ = size; |
---|
309 | for (std::map<Notification*, NotificationOverlayContainer*>::iterator it = this->overlays_.begin(); it != this->overlays_.end(); ++it) //!< Set the font size for each overlay. |
---|
310 | { |
---|
311 | it->second->overlay->setFontSize(size); |
---|
312 | } |
---|
313 | return true; |
---|
314 | } |
---|
315 | |
---|
316 | /** |
---|
317 | @brief |
---|
318 | Sets the font. |
---|
319 | @param font |
---|
320 | The font. |
---|
321 | @return |
---|
322 | Returns true if successful. |
---|
323 | */ |
---|
324 | bool NotificationQueue::setFont(const std::string & font) |
---|
325 | { |
---|
326 | this->font_ = font; |
---|
327 | for (std::map<Notification*, NotificationOverlayContainer*>::iterator it = this->overlays_.begin(); it != this->overlays_.end(); ++it) //!< Set the font for each overlay. |
---|
328 | { |
---|
329 | it->second->overlay->setFont(font); |
---|
330 | } |
---|
331 | return true; |
---|
332 | } |
---|
333 | |
---|
334 | /** |
---|
335 | @brief |
---|
336 | Adds a Notification, to the queue. |
---|
337 | It inserts it into the storage containers, creates an corresponding overlay and a container. |
---|
338 | @param notification |
---|
339 | The Notification. |
---|
340 | @param time |
---|
341 | The time. |
---|
342 | */ |
---|
343 | void NotificationQueue::addNotification(Notification* notification, const std::time_t & time) |
---|
344 | { |
---|
345 | NotificationOverlayContainer* container = new NotificationOverlayContainer; |
---|
346 | container->overlay = new NotificationOverlay(this, notification); |
---|
347 | container->notification = notification; |
---|
348 | container->time = time; |
---|
349 | std::string timeString = std::ctime(&time); |
---|
350 | timeString.erase(timeString.length()-1); |
---|
351 | char buffer[64]; //TDO: Very un-nice. |
---|
352 | std::sprintf(buffer,"%x",(unsigned long)notification); //TDO: Use other conversion to avoid 64bit problems. |
---|
353 | std::string addressString = buffer; |
---|
354 | container->name = "NotificationOverlay(" + timeString + ")&" + addressString; |
---|
355 | |
---|
356 | this->containers_.insert(container); |
---|
357 | this->overlays_[notification] = container; |
---|
358 | this->insertElement(container->overlay, container->name); |
---|
359 | this->size_= this->size_+1; |
---|
360 | } |
---|
361 | |
---|
362 | /** |
---|
363 | @brief |
---|
364 | Removes a container from the queue. |
---|
365 | @param container |
---|
366 | A pointer to the container. |
---|
367 | @return |
---|
368 | Returns true if successful. |
---|
369 | */ |
---|
370 | bool NotificationQueue::removeContainer(NotificationOverlayContainer* container) |
---|
371 | { |
---|
372 | if(this->size_ == 0) //!< You cannot remove anything if the queue is empty. |
---|
373 | return false; |
---|
374 | |
---|
375 | this->removeElement(container->name); |
---|
376 | this->containers_.erase(container); |
---|
377 | this->overlays_.erase(container->notification); |
---|
378 | delete container->overlay; |
---|
379 | delete container; |
---|
380 | this->size_= this->size_-1; |
---|
381 | |
---|
382 | return true; |
---|
383 | } |
---|
384 | |
---|
385 | /** |
---|
386 | @brief |
---|
387 | Clears the queue by removing all containers. |
---|
388 | */ |
---|
389 | void NotificationQueue::clear(void) |
---|
390 | { |
---|
391 | std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it = this->containers_.begin(); |
---|
392 | while(it != this->containers_.end()) |
---|
393 | { |
---|
394 | this->removeContainer(*it); |
---|
395 | it = this->containers_.begin(); //TDO: Needed? |
---|
396 | } |
---|
397 | } |
---|
398 | |
---|
399 | } |
---|