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 | |
---|
38 | namespace 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 | //TDO: Destroy the containers |
---|
50 | } |
---|
51 | |
---|
52 | void NotificationManager::tick(float dt) |
---|
53 | { |
---|
54 | bool update = false; |
---|
55 | |
---|
56 | for (std::list<NotificationContainer*>::iterator notification = notifications_s.begin(); notification != notifications_s.end(); ++notification) |
---|
57 | { |
---|
58 | NotificationContainer* container = *notification; |
---|
59 | if(container->remainingTime == 0) |
---|
60 | { |
---|
61 | continue; |
---|
62 | } |
---|
63 | else if(container->remainingTime - dt <= 0) |
---|
64 | { |
---|
65 | container->remainingTime = 0; |
---|
66 | update = true; |
---|
67 | } |
---|
68 | else |
---|
69 | { |
---|
70 | container->remainingTime = container->remainingTime -dt; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | if(update) |
---|
75 | updateQueue(); |
---|
76 | } |
---|
77 | |
---|
78 | bool NotificationManager::insertNotification(Notification* notification) |
---|
79 | { |
---|
80 | if(notification == NULL) |
---|
81 | return false; |
---|
82 | |
---|
83 | NotificationContainer* container = new NotificationContainer; |
---|
84 | container->notification = notification; |
---|
85 | container->remainingTime = notification->getDisplayTime(); |
---|
86 | notifications_s.push_front(container); |
---|
87 | |
---|
88 | updateQueue(); |
---|
89 | |
---|
90 | COUT(4) << "Notification inserted. Title: " << notification->getTitle() << std::endl; |
---|
91 | |
---|
92 | return true; |
---|
93 | } |
---|
94 | |
---|
95 | void NotificationManager::updateQueue(void) |
---|
96 | { |
---|
97 | std::string text = ""; |
---|
98 | |
---|
99 | int i = NotificationQueue::queue_s->getLength(); |
---|
100 | for (std::list<NotificationContainer*>::iterator notification = notifications_s.begin(); notification != notifications_s.end() && i > 0; ++notification) |
---|
101 | { |
---|
102 | i--; |
---|
103 | NotificationContainer* container = *notification; |
---|
104 | if(container->remainingTime == 0.0) |
---|
105 | continue; |
---|
106 | |
---|
107 | text = text + "\n\n\n------------\n\n" + clipMessage(container->notification->getTitle()) + "\n\n" + clipMessage(container->notification->getMessage()); |
---|
108 | } |
---|
109 | |
---|
110 | NotificationQueue::queue_s->setQueueText(text); |
---|
111 | } |
---|
112 | |
---|
113 | const std::string NotificationManager::clipMessage(const std::string & str) |
---|
114 | { |
---|
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 = ""; |
---|
128 | int wordLength = 0; |
---|
129 | i = 0; |
---|
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 | { |
---|
141 | clippedMessage = clippedMessage + message.substr(i-wordLength, wordLength); |
---|
142 | if(i < message.length()) |
---|
143 | { |
---|
144 | clippedMessage = clippedMessage + message.substr(i,1); |
---|
145 | } |
---|
146 | widthLeft -= (wordLength+1); |
---|
147 | if(message[i] == '\n') |
---|
148 | { |
---|
149 | widthLeft = NotificationQueue::queue_s->getWidth() - (wordLength+1); |
---|
150 | } |
---|
151 | wordLength = 0; |
---|
152 | i++; |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
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 | } |
---|
162 | widthLeft = NotificationQueue::queue_s->getWidth() - (wordLength+1); |
---|
163 | i++; |
---|
164 | wordLength = 0; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | return clippedMessage; |
---|
169 | } |
---|
170 | |
---|
171 | } |
---|