| 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 | 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) |
|---|
| 104 | { |
|---|
| 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()); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | NotificationQueue::queue_s->setQueueText(text); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | const std::string NotificationManager::clipMessage(const std::string & str) |
|---|
| 117 | { |
|---|
| 118 | |
|---|
| 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) |
|---|
| 124 | { |
|---|
| 125 | message.replace(found, 2, "\n"); |
|---|
| 126 | i = found+2; |
|---|
| 127 | found = message.find("\\n", i); |
|---|
| 128 | } |
|---|
| 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; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | } |
|---|