[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] | 38 | namespace 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 | } |
---|
[2501] | 51 | |
---|
[2280] | 52 | void NotificationManager::tick(float dt) |
---|
| 53 | { |
---|
| 54 | bool update = false; |
---|
[2501] | 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 | } |
---|
[2501] | 73 | |
---|
[2435] | 74 | if(update) |
---|
[2280] | 75 | updateQueue(); |
---|
| 76 | } |
---|
[2501] | 77 | |
---|
[2280] | 78 | bool NotificationManager::insertNotification(Notification* notification) |
---|
| 79 | { |
---|
| 80 | if(notification == NULL) |
---|
| 81 | return false; |
---|
[2501] | 82 | |
---|
[2435] | 83 | NotificationContainer* container = new NotificationContainer; |
---|
| 84 | container->notification = notification; |
---|
| 85 | container->remainingTime = notification->getDisplayTime(); |
---|
[2284] | 86 | notifications_s.push_front(container); |
---|
[2501] | 87 | |
---|
[2280] | 88 | updateQueue(); |
---|
[2501] | 89 | |
---|
[2346] | 90 | COUT(4) << "Notification inserted. Title: " << notification->getTitle() << std::endl; |
---|
[2501] | 91 | |
---|
[2280] | 92 | return true; |
---|
| 93 | } |
---|
[2501] | 94 | |
---|
[2280] | 95 | void NotificationManager::updateQueue(void) |
---|
| 96 | { |
---|
| 97 | std::string text = ""; |
---|
[2501] | 98 | |
---|
| 99 | if (!NotificationQueue::queue_s) |
---|
| 100 | return; |
---|
| 101 | |
---|
[2280] | 102 | int i = NotificationQueue::queue_s->getLength(); |
---|
[2328] | 103 | for (std::list<NotificationContainer*>::iterator notification = notifications_s.begin(); notification != notifications_s.end() && i > 0; ++notification) |
---|
[2284] | 104 | { |
---|
[2280] | 105 | i--; |
---|
| 106 | NotificationContainer* container = *notification; |
---|
[2328] | 107 | if(container->remainingTime == 0.0) |
---|
[2280] | 108 | continue; |
---|
[2501] | 109 | |
---|
[2435] | 110 | text = text + "\n\n\n------------\n\n" + clipMessage(container->notification->getTitle()) + "\n\n" + clipMessage(container->notification->getMessage()); |
---|
[2284] | 111 | } |
---|
[2501] | 112 | |
---|
[2280] | 113 | NotificationQueue::queue_s->setQueueText(text); |
---|
| 114 | } |
---|
[2501] | 115 | |
---|
[2349] | 116 | const std::string NotificationManager::clipMessage(const std::string & str) |
---|
[2346] | 117 | { |
---|
[2501] | 118 | |
---|
[2349] | 119 | std::string message = str; |
---|
| 120 | unsigned int i = 0; |
---|
[2501] | 121 | |
---|
[2349] | 122 | unsigned int found = message.find("\\n", i); |
---|
| 123 | while(found != std::string::npos) |
---|
| 124 | { |
---|
| 125 | message.replace(found, 2, "\n"); |
---|
| 126 | i = found+2; |
---|
| 127 | found = message.find("\\n", i); |
---|
| 128 | } |
---|
[2501] | 129 | |
---|
[2349] | 130 | std::string clippedMessage = ""; |
---|
[2346] | 131 | int wordLength = 0; |
---|
[2349] | 132 | i = 0; |
---|
[2346] | 133 | int widthLeft = NotificationQueue::queue_s->getWidth(); |
---|
| 134 | while(i < message.length()) |
---|
| 135 | { |
---|
| 136 | while(i < message.length() && message[i] != ' ' && message[i] != '\n') |
---|
| 137 | { |
---|
| 138 | i++; |
---|
| 139 | wordLength++; |
---|
| 140 | } |
---|
[2501] | 141 | |
---|
[2346] | 142 | if(wordLength <= widthLeft) |
---|
| 143 | { |
---|
[2349] | 144 | clippedMessage = clippedMessage + message.substr(i-wordLength, wordLength); |
---|
| 145 | if(i < message.length()) |
---|
| 146 | { |
---|
| 147 | clippedMessage = clippedMessage + message.substr(i,1); |
---|
| 148 | } |
---|
[2346] | 149 | widthLeft -= (wordLength+1); |
---|
[2349] | 150 | if(message[i] == '\n') |
---|
| 151 | { |
---|
| 152 | widthLeft = NotificationQueue::queue_s->getWidth() - (wordLength+1); |
---|
| 153 | } |
---|
[2346] | 154 | wordLength = 0; |
---|
| 155 | i++; |
---|
| 156 | } |
---|
| 157 | else |
---|
| 158 | { |
---|
[2349] | 159 | clippedMessage.push_back('\n'); |
---|
| 160 | clippedMessage = clippedMessage + message.substr(i-wordLength, wordLength); |
---|
| 161 | if(i < message.length()) |
---|
| 162 | { |
---|
| 163 | clippedMessage = clippedMessage + message.substr(i,1); |
---|
| 164 | } |
---|
[2346] | 165 | widthLeft = NotificationQueue::queue_s->getWidth() - (wordLength+1); |
---|
| 166 | i++; |
---|
| 167 | wordLength = 0; |
---|
| 168 | } |
---|
| 169 | } |
---|
[2501] | 170 | |
---|
[2346] | 171 | return clippedMessage; |
---|
| 172 | } |
---|
[2280] | 173 | |
---|
| 174 | } |
---|