Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem3/src/orxonox/overlays/notifications/NotificationManager.cc @ 2349

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