[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 | |
---|
| 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 | } |
---|
| 50 | |
---|
| 51 | void NotificationManager::tick(float dt) |
---|
| 52 | { |
---|
| 53 | bool update = false; |
---|
| 54 | |
---|
[2284] | 55 | for (std::list<NotificationContainer*>::iterator notification = notifications_s.begin(); notification != notifications_s.end(); ++notification) |
---|
[2280] | 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(); |
---|
[2284] | 85 | notifications_s.push_front(container); |
---|
[2280] | 86 | |
---|
| 87 | updateQueue(); |
---|
| 88 | |
---|
[2346] | 89 | COUT(4) << "Notification inserted. Title: " << notification->getTitle() << std::endl; |
---|
[2281] | 90 | |
---|
[2280] | 91 | return true; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void NotificationManager::updateQueue(void) |
---|
| 95 | { |
---|
| 96 | std::string text = ""; |
---|
| 97 | |
---|
| 98 | int i = NotificationQueue::queue_s->getLength(); |
---|
[2328] | 99 | for (std::list<NotificationContainer*>::iterator notification = notifications_s.begin(); notification != notifications_s.end() && i > 0; ++notification) |
---|
[2284] | 100 | { |
---|
[2280] | 101 | i--; |
---|
| 102 | NotificationContainer* container = *notification; |
---|
[2328] | 103 | if(container->remainingTime == 0.0) |
---|
[2280] | 104 | continue; |
---|
| 105 | |
---|
[2349] | 106 | text = text + "\n\n\n------------\n\n" + clipMessage(container->notification->getTitle()) + "\n\n" + clipMessage(container->notification->getMessage()); |
---|
[2284] | 107 | } |
---|
[2280] | 108 | |
---|
| 109 | NotificationQueue::queue_s->setQueueText(text); |
---|
| 110 | } |
---|
[2346] | 111 | |
---|
[2349] | 112 | const std::string NotificationManager::clipMessage(const std::string & str) |
---|
[2346] | 113 | { |
---|
[2349] | 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 = ""; |
---|
[2346] | 127 | int wordLength = 0; |
---|
[2349] | 128 | i = 0; |
---|
[2346] | 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 | { |
---|
[2349] | 140 | clippedMessage = clippedMessage + message.substr(i-wordLength, wordLength); |
---|
| 141 | if(i < message.length()) |
---|
| 142 | { |
---|
| 143 | clippedMessage = clippedMessage + message.substr(i,1); |
---|
| 144 | } |
---|
[2346] | 145 | widthLeft -= (wordLength+1); |
---|
[2349] | 146 | if(message[i] == '\n') |
---|
| 147 | { |
---|
| 148 | widthLeft = NotificationQueue::queue_s->getWidth() - (wordLength+1); |
---|
| 149 | } |
---|
[2346] | 150 | wordLength = 0; |
---|
| 151 | i++; |
---|
| 152 | } |
---|
| 153 | else |
---|
| 154 | { |
---|
[2349] | 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 | } |
---|
[2346] | 161 | widthLeft = NotificationQueue::queue_s->getWidth() - (wordLength+1); |
---|
| 162 | i++; |
---|
| 163 | wordLength = 0; |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | return clippedMessage; |
---|
| 168 | } |
---|
[2280] | 169 | |
---|
| 170 | } |
---|