Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/toonshader/src/orxonox/overlays/notifications/NotificationQueue.cc @ 3221

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