Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ipv6/src/modules/notifications/NotificationQueue.cc @ 7677

Last change on this file since 7677 was 7163, checked in by dafrick, 14 years ago

Merged presentation3 branch into trunk.

  • Property svn:eol-style set to native
File size: 14.5 KB
Line 
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/**
30    @file
31    @brief Implementation of the NotificationQueue class.
32*/
33
34#include "NotificationQueue.h"
35
36#include "util/Convert.h"
37#include "core/CoreIncludes.h"
38#include "core/XMLPort.h"
39#include "NotificationOverlay.h"
40#include "NotificationManager.h"
41
42namespace orxonox
43{
44
45    CreateFactory(NotificationQueue);
46
47    const std::string NotificationQueue::DEFAULT_FONT("VeraMono");
48    const Vector2 NotificationQueue::DEFAULT_POSITION(0.0,0.0);
49    const float NotificationQueue::DEFAULT_FONT_SIZE = 0.025f;
50
51    /**
52    @brief
53        Constructor. Creates and initializes the object.
54    */
55    NotificationQueue::NotificationQueue(BaseObject* creator) : OverlayGroup(creator)
56    {
57        this->registered_ = false;
58
59        RegisterObject(NotificationQueue);
60        this->initialize();
61    }
62
63    /**
64    @brief
65        Destructor.
66    */
67    NotificationQueue::~NotificationQueue()
68    {
69        this->targets_.clear();
70        this->clear();
71
72        if(this->registered_)
73            NotificationManager::getInstance().unregisterListener(this);
74    }
75
76    /**
77    @brief
78        Initializes the object.
79        Registers the object, initializes variables, sets default values and registers the queue with the NotificationManager.
80    */
81    void NotificationQueue::initialize(void)
82    {
83        this->size_ = 0;
84        this->tickTime_ = 0.0;
85
86        NotificationManager::getInstance().registerListener(this);
87        this->registered_ = true;
88    }
89
90    /**
91    @brief
92        Sets the defaults.
93    */
94    void NotificationQueue::setDefaults(void)
95    {
96        this->setMaxSize(DEFAULT_SIZE);
97        this->setNotificationLength(DEFAULT_LENGTH);
98        this->setDisplayTime(DEFAULT_DISPLAY_TIME);
99        this->setPosition(DEFAULT_POSITION);
100
101        this->setTargets(NotificationManager::ALL);
102
103        this->setFontSize(DEFAULT_FONT_SIZE);
104        this->setFont(DEFAULT_FONT);
105    }
106
107    /**
108    @brief
109        Method for creating a NotificationQueue object through XML.
110    */
111    void NotificationQueue::XMLPort(Element& xmlElement, XMLPort::Mode mode)
112    {
113        SUPER(NotificationQueue, XMLPort, xmlElement, mode);
114
115        this->setDefaults();
116
117        XMLPortParam(NotificationQueue, "maxSize", setMaxSize, getMaxSize, xmlElement, mode);
118        XMLPortParam(NotificationQueue, "notificationLength", setNotificationLength, getNotificationLength, xmlElement, mode);
119        XMLPortParam(NotificationQueue, "displayTime", setDisplayTime, getDisplayTime, xmlElement, mode);
120        XMLPortParam(NotificationQueue, "targets", setTargets, getTargets, xmlElement, mode);
121        XMLPortParam(NotificationQueue, "font", setFont, getFont, xmlElement, mode);
122        XMLPortParam(NotificationQueue, "fontSize", setFontSize, getFontSize, xmlElement, mode);
123        XMLPortParam(NotificationQueue, "position", setPosition, getPosition, xmlElement, mode);
124
125        COUT(3) << "NotificationQueue '" << this->getName() << "' created." << std::endl;
126    }
127
128    /**
129    @brief
130        Updates the queue from time to time.
131    @param dt
132        The time interval that has passed since the last tick.
133    */
134    void NotificationQueue::tick(float dt)
135    {
136        this->tickTime_ += dt; //!< Add the time interval that has passed to the time counter.
137        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.
138        {
139            this->timeLimit_.time = std::time(0)-this->displayTime_; //!< Container containig the current time.
140
141            std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it;
142            it = this->containers_.begin();
143            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.
144            {
145                this->removeContainer(*it);
146                this->scroll(Vector2(0.0f,-(1.1f*this->getFontSize())));
147                it = this->containers_.begin(); //TODO: Needed?
148            }
149
150            this->tickTime_ = 0.0f; //!< Reset time counter.
151        }
152    }
153
154    /**
155    @brief
156        Updates the NotificationQueue.
157        Updates by clearing the queue and requesting all relevant Notifications from the NotificationManager and inserting the in the queue.
158    */
159    void NotificationQueue::update(void)
160    {
161        this->clear();
162
163        std::multimap<std::time_t,Notification*>* notifications = new std::multimap<std::time_t,Notification*>;
164        if(!NotificationManager::getInstance().getNotifications(this, notifications, this->displayTime_)) //!< Get the Notifications sent in the interval form now to minus the display time.
165        {
166            COUT(1) << "NotificationQueue update failed due to undetermined cause." << std::endl;
167            return;
168        }
169
170        if(notifications->empty())
171            return;
172
173        for(std::multimap<std::time_t,Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) //!> Add all Notifications.
174        {
175            this->addNotification(it->second, it->first);
176        }
177
178        delete notifications;
179
180        COUT(4) << "NotificationQueue '" << this->getName() << "' updated." << std::endl;
181    }
182
183    /**
184    @brief
185        Updates the NotificationQueue by adding an new Notification.
186    @param notification
187        Pointer to the Notification.
188    @param time
189        The time the Notification was sent.
190    */
191    void NotificationQueue::update(Notification* notification, const std::time_t & time)
192    {
193        this->addNotification(notification, time);
194
195        std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it;
196        while(this->getSize() > this->getMaxSize())
197        {
198            it = this->containers_.begin();
199            this->removeContainer(*it);
200            this->scroll(Vector2(0.0f,-(1.1f*this->getFontSize())));
201        }
202
203        COUT(4) << "NotificationQueue '" << this->getName() << "' updated. A new Notifications has been added." << std::endl;
204    }
205
206    /**
207    @brief
208        Sets the maximum number of displayed Notifications.
209    @param size
210        The size to be set.
211    @return
212        Returns true if successful.
213    */
214    bool NotificationQueue::setMaxSize(int size)
215    {
216        if(size < 0)
217            return false;
218        this->maxSize_ = size;
219        this->update();
220        return true;
221    }
222
223    /**
224    @brief
225        Sets the maximum number of characters a Notification message displayed by this queue is allowed to have.
226    @param length
227        The length to be set.
228    @return
229        Returns true if successful.
230    */
231    bool NotificationQueue::setNotificationLength(int length)
232    {
233        if(length < 0)
234            return false;
235        this->notificationLength_ = length;
236        this->update();
237        return true;
238    }
239
240    /**
241    @brief
242        Sets the maximum number of seconds a Notification is displayed.
243    @param time
244        The number of seconds the Notifications is displayed.
245    @return
246        Returns true if successful.
247    */
248    bool NotificationQueue::setDisplayTime(int time)
249    {
250        if(time < 0)
251            return false;
252        this->displayTime_ = time;
253        this->update();
254        return true;
255    }
256
257    /**
258    @brief
259        Produces all targets concatinated as string, with kommas (',') as seperators.
260    @param string
261        Pointer to a string which will be used by the method to fill with the concatination of the targets.
262    @return
263        Returns true if successful.
264    */
265    bool NotificationQueue::getTargets(std::string* string) const
266    {
267        if(string == NULL)
268        {
269            COUT(4) << "Input string must have memory allocated." << std::endl;
270            return false;
271        }
272        string->clear();
273        bool first = true;
274        for(std::set<std::string>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++) //!< Iterate through the set of targets.
275        {
276            if(!first)
277            {
278                *string += ',';
279            }
280            else
281            {
282                first = false;
283            }
284            *string += *it;
285        }
286
287        return true;
288    }
289
290    /**
291    @brief
292        Sets the targets of the queue.
293        The targets are the senders whose Notifications are displayed in this queue.
294    @param targets
295        Accepts a string of targets, each seperated by commas (','), spaces are ignored.
296    @return
297        Returns true if successful.
298    */
299    bool NotificationQueue::setTargets(const std::string & targets)
300    {
301        this->targets_.clear();
302
303        std::string* pTemp;
304        unsigned int index = 0;
305        while( index < targets.size() ) //!< Go through the string, character by character until the end is reached.
306        {
307            pTemp = new std::string();
308            while(index < targets.size() && targets[index] != ',' && targets[index] != ' ')
309            {
310                *pTemp += targets[index];
311                index++;
312            }
313            index++;
314            this->targets_.insert(*pTemp);
315        }
316
317        return true;
318    }
319
320    /**
321    @brief
322        Sets the font size.
323    @param size
324        The font size.
325    @return
326        Returns true if successful.
327    */
328    bool NotificationQueue::setFontSize(float size)
329    {
330        if(size <= 0)
331            return false;
332        this->fontSize_ = size;
333        for (std::map<Notification*, NotificationOverlayContainer*>::iterator it = this->overlays_.begin(); it != this->overlays_.end(); it++) //!< Set the font size for each overlay.
334        {
335            it->second->overlay->setFontSize(size);
336        }
337        return true;
338    }
339
340    /**
341    @brief
342        Sets the font.
343    @param font
344        The font.
345    @return
346        Returns true if successful.
347    */
348    bool NotificationQueue::setFont(const std::string & font)
349    {
350        this->font_ = font;
351        for (std::map<Notification*, NotificationOverlayContainer*>::iterator it = this->overlays_.begin(); it != this->overlays_.end(); it++) //!< Set the font for each overlay.
352        {
353            it->second->overlay->setFont(font);
354        }
355        return true;
356    }
357
358    /**
359    @brief
360        Scrolls the NotificationQueue, meaning all NotificationOverlays are moved the input vector.
361    @param pos
362        The vector the NotificationQueue is scrolled.
363    */
364    void NotificationQueue::scroll(const Vector2 pos)
365    {
366        for (std::map<Notification*, NotificationOverlayContainer*>::iterator it = this->overlays_.begin(); it != this->overlays_.end(); ++it) //!< Scroll each overlay.
367        {
368            it->second->overlay->scroll(pos);
369        }
370    }
371
372    /**
373    @brief
374        Aligns all the Notifications to the position of the NotificationQueue.
375    */
376    void NotificationQueue::positionChanged(void)
377    {
378        int counter = 0;
379        for (std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it = this->containers_.begin(); it != this->containers_.end(); it++) //!< Set the position for each overlay.
380        {
381            (*it)->overlay->setPosition(this->getPosition());
382            (*it)->overlay->scroll(Vector2(0.0f,(1.1f*this->getFontSize())*counter));
383            counter++;
384        }
385    }
386
387    /**
388    @brief
389        Adds a Notification, to the queue.
390        It inserts it into the storage containers, creates an corresponding overlay and a container.
391    @param notification
392        The Notification.
393    @param time
394        The time.
395    */
396    void NotificationQueue::addNotification(Notification* notification, const std::time_t & time)
397    {
398        NotificationOverlayContainer* container = new NotificationOverlayContainer;
399        container->overlay = new NotificationOverlay(this, notification);
400        container->notification = notification;
401        container->time = time;
402        std::string timeString = std::ctime(&time);
403        timeString.erase(timeString.length()-1);
404        const std::string& addressString = multi_cast<std::string>(reinterpret_cast<unsigned long>(notification));
405        container->name = "NotificationOverlay(" + timeString + ")&" + addressString;
406
407        this->containers_.insert(container);
408        this->overlays_[notification] = container;
409        this->addElement(container->overlay);
410        this->size_= this->size_+1;
411
412        container->overlay->scroll(Vector2(0.0f,(1.1f*this->getFontSize())*(this->getSize()-1)));
413    }
414
415    /**
416    @brief
417        Removes a container from the queue.
418    @param container
419        A pointer to the container.
420    @return
421        Returns true if successful.
422    */
423    bool NotificationQueue::removeContainer(NotificationOverlayContainer* container)
424    {
425        if(this->size_ == 0) //!< You cannot remove anything if the queue is empty.
426            return false;
427
428        // Unregister the NotificationQueue with the NotificationManager.
429        NotificationManager::getInstance().unregisterNotification(container->notification, this);
430
431        this->removeElement(container->overlay);
432        this->containers_.erase(container);
433        this->overlays_.erase(container->notification);
434        container->overlay->destroy();
435        delete container;
436        this->size_= this->size_-1;
437
438        return true;
439    }
440
441    /**
442    @brief
443        Clears the queue by removing all containers.
444    */
445    void NotificationQueue::clear(void)
446    {
447        std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it = this->containers_.begin();
448        while(it != this->containers_.end())
449        {
450            this->removeContainer(*it);
451            it = this->containers_.begin();
452        }
453    }
454
455}
Note: See TracBrowser for help on using the repository browser.