Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem5/src/orxonox/overlays/notifications/NotificationQueue.cc @ 2907

Last change on this file since 2907 was 2907, checked in by dafrick, 16 years ago

Merging of the current QuestSystem branch.

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