[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 | |
---|
[2910] | 34 | #include "Notification.h" |
---|
[2909] | 35 | |
---|
[2280] | 36 | #include "NotificationQueue.h" |
---|
| 37 | |
---|
[2435] | 38 | namespace orxonox |
---|
| 39 | { |
---|
[2910] | 40 | std::list<NotificationContainer*> NotificationManager::notifications_s; |
---|
[2280] | 41 | |
---|
[2910] | 42 | NotificationManager::NotificationManager(BaseObject* creator) : BaseObject(creator) |
---|
[2280] | 43 | { |
---|
[2910] | 44 | RegisterObject(NotificationManager); |
---|
[2280] | 45 | } |
---|
| 46 | |
---|
| 47 | NotificationManager::~NotificationManager() |
---|
| 48 | { |
---|
[2910] | 49 | //TDO: Destroy the containers |
---|
[2280] | 50 | } |
---|
[2501] | 51 | |
---|
[2910] | 52 | void NotificationManager::tick(float dt) |
---|
[2280] | 53 | { |
---|
[2910] | 54 | bool update = false; |
---|
| 55 | |
---|
| 56 | for (std::list<NotificationContainer*>::iterator notification = notifications_s.begin(); notification != notifications_s.end(); ++notification) |
---|
[2435] | 57 | { |
---|
[2910] | 58 | NotificationContainer* container = *notification; |
---|
| 59 | if(container->remainingTime == 0) |
---|
[2280] | 60 | { |
---|
[2910] | 61 | continue; |
---|
[2435] | 62 | } |
---|
[2910] | 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 | } |
---|
[2435] | 72 | } |
---|
[2910] | 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 | |
---|
[2280] | 92 | return true; |
---|
| 93 | } |
---|
[2910] | 94 | |
---|
| 95 | void NotificationManager::updateQueue(void) |
---|
[2280] | 96 | { |
---|
[2910] | 97 | std::string text = ""; |
---|
| 98 | |
---|
| 99 | if (!NotificationQueue::queue_s) |
---|
| 100 | return; |
---|
| 101 | |
---|
| 102 | int i = NotificationQueue::queue_s->getLength(); |
---|
| 103 | for (std::list<NotificationContainer*>::iterator notification = notifications_s.begin(); notification != notifications_s.end() && i > 0; ++notification) |
---|
[2284] | 104 | { |
---|
[2910] | 105 | i--; |
---|
| 106 | NotificationContainer* container = *notification; |
---|
| 107 | if(container->remainingTime == 0.0) |
---|
| 108 | continue; |
---|
| 109 | |
---|
| 110 | text = text + "\n\n\n------------\n\n" + clipMessage(container->notification->getTitle()) + "\n\n" + clipMessage(container->notification->getMessage()); |
---|
[2284] | 111 | } |
---|
[2501] | 112 | |
---|
[2910] | 113 | NotificationQueue::queue_s->setQueueText(text); |
---|
[2280] | 114 | } |
---|
[2910] | 115 | |
---|
| 116 | const std::string NotificationManager::clipMessage(const std::string & str) |
---|
[2346] | 117 | { |
---|
[2501] | 118 | |
---|
[2910] | 119 | std::string message = str; |
---|
| 120 | unsigned int i = 0; |
---|
| 121 | |
---|
| 122 | unsigned int found = message.find("\\n", i); |
---|
| 123 | while(found != std::string::npos) |
---|
[2349] | 124 | { |
---|
[2910] | 125 | message.replace(found, 2, "\n"); |
---|
| 126 | i = found+2; |
---|
| 127 | found = message.find("\\n", i); |
---|
[2349] | 128 | } |
---|
[2910] | 129 | |
---|
| 130 | std::string clippedMessage = ""; |
---|
| 131 | int wordLength = 0; |
---|
| 132 | i = 0; |
---|
| 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 | } |
---|
| 141 | |
---|
| 142 | if(wordLength <= widthLeft) |
---|
| 143 | { |
---|
| 144 | clippedMessage = clippedMessage + message.substr(i-wordLength, wordLength); |
---|
| 145 | if(i < message.length()) |
---|
| 146 | { |
---|
| 147 | clippedMessage = clippedMessage + message.substr(i,1); |
---|
| 148 | } |
---|
| 149 | widthLeft -= (wordLength+1); |
---|
| 150 | if(message[i] == '\n') |
---|
| 151 | { |
---|
| 152 | widthLeft = NotificationQueue::queue_s->getWidth() - (wordLength+1); |
---|
| 153 | } |
---|
| 154 | wordLength = 0; |
---|
| 155 | i++; |
---|
| 156 | } |
---|
| 157 | else |
---|
| 158 | { |
---|
| 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 | } |
---|
| 165 | widthLeft = NotificationQueue::queue_s->getWidth() - (wordLength+1); |
---|
| 166 | i++; |
---|
| 167 | wordLength = 0; |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | return clippedMessage; |
---|
[2346] | 172 | } |
---|
[2280] | 173 | |
---|
| 174 | } |
---|