Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/overlays/notifications/NotificationManager.cc @ 2488

Last change on this file since 2488 was 2436, checked in by rgrieder, 16 years ago

Added svn:eol-style "native" to all files.

  • Property svn:eol-style set to native
File size: 5.3 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
29#include "OrxonoxStableHeaders.h"
30#include "NotificationManager.h"
31
32#include "core/CoreIncludes.h"
33
34#include "Notification.h"
35
36#include "NotificationQueue.h"
37
[2435]38namespace orxonox
39{
[2280]40    std::list<NotificationContainer*> NotificationManager::notifications_s;
41
42    NotificationManager::NotificationManager(BaseObject* creator) : BaseObject(creator)
43    {
44        RegisterObject(NotificationManager);
45    }
46
47    NotificationManager::~NotificationManager()
48    {
[2375]49        //TDO: Destroy the containers
[2280]50    }
51   
52    void NotificationManager::tick(float dt)
53    {
54        bool update = false;
55   
[2284]56        for (std::list<NotificationContainer*>::iterator notification = notifications_s.begin(); notification != notifications_s.end(); ++notification)
[2435]57        {
[2280]58            NotificationContainer* container = *notification;
59            if(container->remainingTime == 0)
60            {
61                continue;
[2435]62            }
63            else if(container->remainingTime - dt <= 0)
64            {
65                container->remainingTime = 0;
66                update = true;
67            }
68            else
69            {
[2280]70                container->remainingTime = container->remainingTime -dt;
[2435]71            }
72        }
73   
74        if(update)
[2280]75            updateQueue();
76    }
77   
78    bool NotificationManager::insertNotification(Notification* notification)
79    {
80        if(notification == NULL)
81            return false;
82           
[2435]83        NotificationContainer* container = new NotificationContainer;
84        container->notification = notification;
85        container->remainingTime = notification->getDisplayTime();
[2284]86        notifications_s.push_front(container);
[2280]87       
88        updateQueue();
89       
[2346]90        COUT(4) << "Notification inserted. Title: " << notification->getTitle() << std::endl;
[2281]91       
[2280]92        return true;
93    }
94   
95    void NotificationManager::updateQueue(void)
96    {
97        std::string text = "";
98       
99        int i = NotificationQueue::queue_s->getLength();
[2328]100        for (std::list<NotificationContainer*>::iterator notification = notifications_s.begin(); notification != notifications_s.end() && i > 0; ++notification)
[2284]101        {
[2280]102            i--;
103            NotificationContainer* container = *notification;
[2328]104            if(container->remainingTime == 0.0)
[2280]105                continue;
106           
[2435]107            text = text + "\n\n\n------------\n\n" + clipMessage(container->notification->getTitle()) + "\n\n" + clipMessage(container->notification->getMessage());
[2284]108        }
[2280]109       
110        NotificationQueue::queue_s->setQueueText(text);
111    }
[2346]112   
[2349]113    const std::string NotificationManager::clipMessage(const std::string & str)
[2346]114    {
[2349]115   
116        std::string message = str;
117        unsigned int i = 0;
118       
119        unsigned int found = message.find("\\n", i);
120        while(found != std::string::npos)
121        {
122            message.replace(found, 2, "\n");
123            i = found+2;
124            found = message.find("\\n", i);
125        }
126   
127        std::string clippedMessage = "";
[2346]128        int wordLength = 0;
[2349]129        i = 0;
[2346]130        int widthLeft = NotificationQueue::queue_s->getWidth();
131        while(i < message.length())
132        {
133            while(i < message.length() && message[i] != ' ' && message[i] != '\n')
134            {
135                i++;
136                wordLength++;
137            }
138           
139            if(wordLength <= widthLeft)
140            {
[2349]141                clippedMessage = clippedMessage + message.substr(i-wordLength, wordLength);
142                if(i < message.length())
143                {
144                    clippedMessage = clippedMessage + message.substr(i,1);
145                }
[2346]146                widthLeft -= (wordLength+1);
[2349]147                if(message[i] == '\n')
148                {
149                    widthLeft = NotificationQueue::queue_s->getWidth() - (wordLength+1);
150                }
[2346]151                wordLength = 0;
152                i++;
153            }
154            else
155            {
[2349]156                clippedMessage.push_back('\n');
157                clippedMessage = clippedMessage + message.substr(i-wordLength, wordLength);
158                if(i < message.length())
159                {
160                    clippedMessage = clippedMessage + message.substr(i,1);
161                }
[2346]162                widthLeft = NotificationQueue::queue_s->getWidth() - (wordLength+1);
163                i++;
164                wordLength = 0;
165            }
166        }
167       
168        return clippedMessage;
169    }
[2280]170
171}
Note: See TracBrowser for help on using the repository browser.