[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 | |
---|
[2911] | 29 | /** |
---|
[7403] | 30 | @file NotificationManager.cc |
---|
[2911] | 31 | @brief Implementation of the NotificationManager class. |
---|
| 32 | */ |
---|
| 33 | |
---|
[2280] | 34 | #include "NotificationManager.h" |
---|
| 35 | |
---|
[7403] | 36 | #include "core/command/ConsoleCommand.h" |
---|
| 37 | #include "core/CoreIncludes.h" |
---|
| 38 | #include "core/LuaState.h" |
---|
| 39 | #include "util/ScopedSingletonManager.h" |
---|
[2911] | 40 | |
---|
[5619] | 41 | #include "interfaces/NotificationListener.h" |
---|
[2280] | 42 | |
---|
[7403] | 43 | #include "Notification.h" |
---|
| 44 | #include "NotificationQueue.h" |
---|
[8446] | 45 | #include "NotificationQueueCEGUI.h" |
---|
[7403] | 46 | |
---|
[2435] | 47 | namespace orxonox |
---|
| 48 | { |
---|
[2280] | 49 | |
---|
[7488] | 50 | ManageScopedSingleton(NotificationManager, ScopeID::Root, false); |
---|
[7403] | 51 | |
---|
[2911] | 52 | /** |
---|
| 53 | @brief |
---|
| 54 | Constructor. Registers the Object. |
---|
| 55 | */ |
---|
| 56 | NotificationManager::NotificationManager() |
---|
[2280] | 57 | { |
---|
[2911] | 58 | RegisterRootObject(NotificationManager); |
---|
| 59 | |
---|
[7403] | 60 | COUT(3) << "NotificatioManager created." << std::endl; |
---|
[2280] | 61 | } |
---|
| 62 | |
---|
[2911] | 63 | /** |
---|
| 64 | @brief |
---|
| 65 | Destructor. |
---|
| 66 | */ |
---|
[2280] | 67 | NotificationManager::~NotificationManager() |
---|
| 68 | { |
---|
[7488] | 69 | // Destroys all Notifications. |
---|
| 70 | for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it!= this->allNotificationsList_.end(); it++) |
---|
| 71 | it->second->destroy(); |
---|
| 72 | this->allNotificationsList_.clear(); |
---|
| 73 | |
---|
[7403] | 74 | COUT(3) << "NotificationManager destroyed." << std::endl; |
---|
[2280] | 75 | } |
---|
[2501] | 76 | |
---|
[2911] | 77 | /** |
---|
| 78 | @brief |
---|
[7403] | 79 | Is called before the object is destroyed. |
---|
| 80 | */ |
---|
| 81 | void NotificationManager::preDestroy(void) |
---|
| 82 | { |
---|
| 83 | // Destroys all NotificationQueues that have been registered with the NotificationManager. |
---|
[8079] | 84 | std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); |
---|
| 85 | while(it != this->queues_.end()) |
---|
| 86 | { |
---|
[8446] | 87 | it->second->destroy(); |
---|
[8079] | 88 | it = this->queues_.begin(); |
---|
| 89 | } |
---|
[7489] | 90 | |
---|
[7403] | 91 | this->queues_.clear(); |
---|
| 92 | } |
---|
[8445] | 93 | |
---|
| 94 | /** |
---|
| 95 | @brief |
---|
| 96 | Creates and registers a Notification with the input message from the input sender. |
---|
| 97 | This is called by the NotificationListener, whenever a new notification arrives. |
---|
| 98 | @param message |
---|
| 99 | The message of the new Notification. |
---|
| 100 | @param sender |
---|
| 101 | The name of the entity (of the collective) that sent the new Notification. |
---|
[8448] | 102 | @param type |
---|
| 103 | The type of the new Notification. |
---|
[8445] | 104 | @return |
---|
| 105 | Returns true if successful. |
---|
| 106 | */ |
---|
| 107 | bool NotificationManager::registerNotification(const std::string& message, const std::string& sender, notificationMessageType::Value type) |
---|
[7474] | 108 | { |
---|
[8445] | 109 | // TODO: Do something with the type. |
---|
| 110 | Notification* notification = new Notification(message, sender, type); |
---|
[8374] | 111 | return this->registerNotification(notification); |
---|
[7474] | 112 | } |
---|
| 113 | |
---|
[7403] | 114 | /** |
---|
| 115 | @brief |
---|
[8445] | 116 | Executes the input command from the input sender. |
---|
| 117 | This is called by the NotificationListener, whenever a new command arrives. |
---|
| 118 | @param command |
---|
| 119 | The command to be executed, |
---|
| 120 | @param sender |
---|
| 121 | The The name of the entity (of the collective) that sent the command. |
---|
[8448] | 122 | @return |
---|
| 123 | Returns true if the command was successfully executed. |
---|
[8445] | 124 | */ |
---|
| 125 | bool NotificationManager::executeCommand(notificationCommand::Value command, const std::string& sender) |
---|
| 126 | { |
---|
[8448] | 127 | bool commandExecuted = false; |
---|
[8445] | 128 | if(command == notificationCommand::clear) |
---|
| 129 | { |
---|
[8448] | 130 | if(this->commandClear(sender)) |
---|
| 131 | commandExecuted = true; |
---|
[8445] | 132 | } |
---|
| 133 | |
---|
[8448] | 134 | if(commandExecuted) |
---|
| 135 | COUT(3) << "Notification command \"" << NotificationListener::command2Str(command) << "\" executed." << endl; |
---|
| 136 | |
---|
| 137 | return commandExecuted; |
---|
[8445] | 138 | } |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | @brief |
---|
| 142 | The clear command. Clears all NotificationQueues that have its sender as a target. |
---|
| 143 | @param sender |
---|
| 144 | The sender of the clear command. |
---|
[8448] | 145 | @return |
---|
| 146 | Returns true if the command was successfully executed by at least one NotificationQueue, false if it was not executed. |
---|
[8445] | 147 | */ |
---|
[8448] | 148 | bool NotificationManager::commandClear(const std::string& sender) |
---|
[8445] | 149 | { |
---|
| 150 | bool all = (sender == NotificationListener::ALL); |
---|
[8448] | 151 | bool executed = false; |
---|
[8445] | 152 | // Clear all NotificationQueues that have the input sender as target. |
---|
| 153 | for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); it++) // Iterate through all NotificationQueues. |
---|
| 154 | { |
---|
| 155 | const std::set<std::string>& set = it->second->getTargetsSet(); |
---|
| 156 | // If either the sender is 'all', the NotificationQueue has as target all or the NotificationQueue has the input sender as a target. |
---|
| 157 | if(all || set.find(NotificationListener::ALL) != set.end() || set.find(sender) != set.end()) |
---|
[8448] | 158 | executed = it->second->tidy() || executed; |
---|
[8445] | 159 | } |
---|
[8448] | 160 | |
---|
| 161 | return executed; |
---|
[8445] | 162 | } |
---|
| 163 | |
---|
| 164 | /** |
---|
| 165 | @brief |
---|
[8374] | 166 | Registers a Notification within the NotificationManager and makes sure that the Notification is sent to all the NotificationQueues associated with its sender. |
---|
[2911] | 167 | @param notification |
---|
| 168 | The Notification to be registered. |
---|
| 169 | @return |
---|
| 170 | Returns true if successful. |
---|
| 171 | */ |
---|
| 172 | bool NotificationManager::registerNotification(Notification* notification) |
---|
| 173 | { |
---|
[7403] | 174 | assert(notification); |
---|
[5619] | 175 | |
---|
[7552] | 176 | std::time_t time = std::time(0); // Get current time. |
---|
[5619] | 177 | |
---|
[7403] | 178 | // Add the Notification to the list that holds all Notifications. |
---|
| 179 | this->allNotificationsList_.insert(std::pair<std::time_t, Notification*>(time, notification)); |
---|
[5619] | 180 | |
---|
[8374] | 181 | if(notification->getSender() == NotificationListener::NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications. |
---|
[2911] | 182 | return true; |
---|
[5619] | 183 | |
---|
[8445] | 184 | // If all are the sender, then the Notifications is added to every NotificationQueue. |
---|
| 185 | bool all = (notification->getSender() == NotificationListener::ALL); |
---|
[5619] | 186 | |
---|
[8374] | 187 | // Insert the Notification in all NotificationQueues that have its sender as target. |
---|
| 188 | for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); it++) // Iterate through all NotificationQueues. |
---|
[2435] | 189 | { |
---|
[8374] | 190 | const std::set<std::string>& set = it->second->getTargetsSet(); |
---|
| 191 | bool bAll = set.find(NotificationListener::ALL) != set.end(); |
---|
| 192 | // If either the Notification has as sender 'all', the NotificationQueue displays all Notifications or the NotificationQueue has the sender of the Notification as target. |
---|
[7403] | 193 | if(all || bAll || set.find(notification->getSender()) != set.end()) |
---|
[2280] | 194 | { |
---|
[7403] | 195 | if(!bAll) |
---|
[8374] | 196 | this->notificationLists_[it->second->getName()]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the notifications list of the current NotificationQueue. |
---|
| 197 | it->second->update(notification, time); // Update the NotificationQueue. |
---|
[2435] | 198 | } |
---|
| 199 | } |
---|
[5619] | 200 | |
---|
[7403] | 201 | COUT(4) << "Notification (&" << notification << ") registered with the NotificationManager." << std::endl; |
---|
[5619] | 202 | |
---|
[2280] | 203 | return true; |
---|
| 204 | } |
---|
[5619] | 205 | |
---|
[2911] | 206 | /** |
---|
| 207 | @brief |
---|
[8374] | 208 | Unregisters a Notification within the NotificationManager for a given NotificationQueue. |
---|
[7163] | 209 | @param notification |
---|
| 210 | A pointer to the Notification to be unregistered. |
---|
[8374] | 211 | @param queue |
---|
| 212 | A pointer to the NotificationQueue the Notification is unregistered for. |
---|
[7163] | 213 | */ |
---|
[8374] | 214 | void NotificationManager::unregisterNotification(Notification* notification, NotificationQueue* queue) |
---|
[7163] | 215 | { |
---|
| 216 | assert(notification); |
---|
[8374] | 217 | assert(queue); |
---|
[7163] | 218 | |
---|
[8374] | 219 | // Remove the Notification from the list of Notifications of the input NotificationQueue. |
---|
| 220 | this->removeNotification(notification, *(this->notificationLists_.find(queue->getName())->second)); |
---|
[7163] | 221 | |
---|
[8374] | 222 | COUT(4) << "Notification (&" << notification << ") unregistered with the NotificationManager from NotificationQueue " << queue->getName() << "." << std::endl; |
---|
[7163] | 223 | } |
---|
| 224 | |
---|
| 225 | /** |
---|
| 226 | @brief |
---|
[7403] | 227 | Helper method that removes an input Notification form an input map. |
---|
[7163] | 228 | @param notification |
---|
[7403] | 229 | A pointer to the Notification to be removed. |
---|
[7163] | 230 | @param map |
---|
[7403] | 231 | The map the Notification should be removed from. |
---|
[7163] | 232 | @return |
---|
| 233 | Returns true if successful. |
---|
| 234 | */ |
---|
| 235 | bool NotificationManager::removeNotification(Notification* notification, std::multimap<std::time_t, Notification*>& map) |
---|
| 236 | { |
---|
| 237 | // Iterates through all items in the map until the Notification is found. |
---|
| 238 | //TODO: Do more efficiently? |
---|
| 239 | for(std::multimap<std::time_t, Notification*>::iterator it = map.begin(); it != map.end(); it++) |
---|
| 240 | { |
---|
| 241 | if(it->second == notification) |
---|
| 242 | { |
---|
| 243 | map.erase(it); |
---|
| 244 | return true; |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | return false; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | /** |
---|
| 251 | @brief |
---|
[8374] | 252 | Fetches the Notifications for a specific NotificationQueue in a specified timeframe and stores them in the input map. |
---|
| 253 | @param queue |
---|
| 254 | The NotificationQueue the Notifications are fetched for. |
---|
[2911] | 255 | @param map |
---|
[7403] | 256 | A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated. |
---|
[2911] | 257 | @param timeFrameStart |
---|
| 258 | The start time of the timeframe. |
---|
| 259 | @param timeFrameEnd |
---|
| 260 | The end time of the timeframe. |
---|
| 261 | @return |
---|
| 262 | Returns true if successful. |
---|
| 263 | */ |
---|
[8374] | 264 | void NotificationManager::getNotifications(NotificationQueue* queue, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd) |
---|
[2346] | 265 | { |
---|
[8374] | 266 | assert(queue); |
---|
[7403] | 267 | assert(map); |
---|
[2501] | 268 | |
---|
[8374] | 269 | std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[queue->getName()]; // All the Notifications for the input NotificationQueue. |
---|
[5619] | 270 | |
---|
[2911] | 271 | std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest; |
---|
[7403] | 272 | // Iterators pointing to the bounds specified by the specified start and end times of the time frame. |
---|
[2911] | 273 | itLowest = notifications->lower_bound(timeFrameStart); |
---|
[7403] | 274 | itHighest = notifications->upper_bound(timeFrameEnd); |
---|
[5619] | 275 | |
---|
[7403] | 276 | for(it = itLowest; it != itHighest; it++) // Iterate through the Notifications from the start of the time frame to the end of it. |
---|
| 277 | map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); // Add the found Notifications to the map. |
---|
| 278 | } |
---|
[5619] | 279 | |
---|
[7403] | 280 | /** |
---|
| 281 | @brief |
---|
[8374] | 282 | Fetches the newest Notifications for a specific NotificationQueue and stores them in the input map. |
---|
| 283 | @param queue |
---|
| 284 | The NotificationQueue the Notifications are fetched for. |
---|
[8371] | 285 | @param map |
---|
| 286 | A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated. |
---|
| 287 | @param numberOfNotifications |
---|
| 288 | The number of newest Notifications to be got. |
---|
| 289 | @return |
---|
| 290 | Returns true if successful. |
---|
| 291 | */ |
---|
[8374] | 292 | void NotificationManager::getNewestNotifications(NotificationQueue* queue, std::multimap<std::time_t, Notification*>* map, int numberOfNotifications) |
---|
[8371] | 293 | { |
---|
[8374] | 294 | assert(queue); |
---|
[8371] | 295 | assert(map); |
---|
| 296 | |
---|
[8374] | 297 | std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[queue->getName()]; // All the Notifications for the input NotificationQueue. |
---|
[8371] | 298 | |
---|
| 299 | if(!notifications->empty()) // If the list of Notifications is not empty. |
---|
| 300 | { |
---|
| 301 | std::multimap<std::time_t,Notification*>::iterator it = notifications->end(); |
---|
| 302 | for(int i = 0; i < numberOfNotifications; i++) // Iterate through the Notifications from the newest until we have the specified number of notifications. |
---|
| 303 | { |
---|
| 304 | it--; |
---|
| 305 | map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); // Add the found Notifications to the map. |
---|
| 306 | if(it == notifications->begin()) |
---|
| 307 | break; |
---|
| 308 | } |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | /** |
---|
| 313 | @brief |
---|
[7403] | 314 | Registers a NotificationQueue. |
---|
[8374] | 315 | This makes sure that the NotificationQueue can be accessed through lua by name. It also makes sure that the NotificationQueue is destroyed upon destruction of the NotificationManager. |
---|
[7403] | 316 | @param queue |
---|
| 317 | A pointer to the NotificationQueue to be registered. |
---|
| 318 | @return |
---|
| 319 | Returns true if successful. If e.g. the a NotificationQueue with that name already exists this returns false. |
---|
| 320 | */ |
---|
| 321 | bool NotificationManager::registerQueue(NotificationQueue* queue) |
---|
| 322 | { |
---|
[8374] | 323 | assert(queue); |
---|
| 324 | |
---|
| 325 | // If the NotificationQueue is already registered. |
---|
| 326 | if(this->queues_.find(queue->getName()) != this->queues_.end()) |
---|
| 327 | return false; |
---|
| 328 | |
---|
| 329 | this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)); // Add the NotificationQueue to the list of NotificationQueues. |
---|
| 330 | |
---|
| 331 | const std::set<std::string>& set = queue->getTargetsSet(); |
---|
| 332 | |
---|
| 333 | // If all senders are the target of the NotificationQueue, then the list of Notifications for that specific NotificationQueue is the same as the list of all Notifications. |
---|
| 334 | bool bAll = set.find(NotificationListener::ALL) != set.end(); |
---|
| 335 | std::multimap<std::time_t, Notification*>* map = NULL; |
---|
| 336 | if(bAll) |
---|
| 337 | this->notificationLists_[queue->getName()] = &this->allNotificationsList_; |
---|
| 338 | // Else a new list (resp. multimap) is created and added to the list of Notification lists for NotificationQueues. |
---|
| 339 | else |
---|
| 340 | { |
---|
| 341 | this->notificationLists_[queue->getName()] = new std::multimap<std::time_t, Notification*>; |
---|
| 342 | map = this->notificationLists_[queue->getName()]; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | // Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationQueue. |
---|
| 346 | for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++) |
---|
| 347 | { |
---|
| 348 | if(!bAll && set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current Notification as target. |
---|
| 349 | map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | queue->update(); // Update the queue. |
---|
| 353 | |
---|
[8079] | 354 | COUT(4) << "NotificationQueue '" << queue->getName() << "' registered with the NotificationManager." << std::endl; |
---|
[8374] | 355 | return true; |
---|
[7403] | 356 | } |
---|
| 357 | |
---|
| 358 | /** |
---|
| 359 | @brief |
---|
| 360 | Unregisters a NotificationQueue. |
---|
| 361 | @param queue |
---|
| 362 | A pointer to the NotificationQueue to be unregistered. |
---|
| 363 | */ |
---|
| 364 | void NotificationManager::unregisterQueue(NotificationQueue* queue) |
---|
| 365 | { |
---|
[8374] | 366 | assert(queue); |
---|
| 367 | |
---|
| 368 | std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(queue->getName())->second; |
---|
| 369 | |
---|
| 370 | // If the map is not the map of all Notifications, make sure all Notifications are unregistered. |
---|
| 371 | std::multimap<std::time_t, Notification*>::iterator it = map->begin(); |
---|
| 372 | if(map != &this->allNotificationsList_) |
---|
| 373 | { |
---|
| 374 | while(it != map->end()) |
---|
| 375 | { |
---|
| 376 | this->unregisterNotification(it->second, queue); |
---|
| 377 | it = map->begin(); |
---|
| 378 | } |
---|
| 379 | delete map; |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | // Remove the NotificationQueue from the list of NotificationQueues. |
---|
| 383 | this->queues_.erase(queue->getName()); |
---|
| 384 | // Remove the Notifications list that was associated with the input NotificationQueue. |
---|
| 385 | this->notificationLists_.erase(queue->getName()); |
---|
| 386 | |
---|
[8079] | 387 | COUT(4) << "NotificationQueue '" << queue->getName() << "' unregistered with the NotificationManager." << std::endl; |
---|
[7403] | 388 | } |
---|
| 389 | |
---|
| 390 | /** |
---|
| 391 | @brief |
---|
| 392 | Get the NotificationQueue with the input name. |
---|
| 393 | @param name |
---|
| 394 | The name of the NotificationQueue. |
---|
| 395 | @return |
---|
| 396 | Returns a pointer to the NotificationQueue with the input name. Returns NULL if no NotificationQueue with such a name exists. |
---|
| 397 | */ |
---|
| 398 | NotificationQueue* NotificationManager::getQueue(const std::string & name) |
---|
| 399 | { |
---|
| 400 | std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.find(name); |
---|
| 401 | // Returns NULL if no such NotificationQueue exists. |
---|
| 402 | if(it == this->queues_.end()) |
---|
| 403 | return NULL; |
---|
| 404 | |
---|
| 405 | return (*it).second; |
---|
| 406 | } |
---|
| 407 | |
---|
[8448] | 408 | /** |
---|
| 409 | @brief |
---|
| 410 | Loads all the NotificationQueues that should exist. |
---|
| 411 | */ |
---|
| 412 | void NotificationManager::loadQueues(void) |
---|
| 413 | { |
---|
| 414 | NotificationQueueCEGUI* allQueue = new NotificationQueueCEGUI("all"); |
---|
| 415 | allQueue->setDisplaySize(Vector2(0.5, 0)); |
---|
| 416 | allQueue->setPosition(Vector4(0.0, 10, 0.3, 0)); |
---|
| 417 | |
---|
| 418 | NotificationQueueCEGUI* infoQueue = new NotificationQueueCEGUI("info", NotificationListener::ALL, 1, -1); |
---|
| 419 | infoQueue->setPosition(Vector4(0.2, 0, 0.8, 0)); |
---|
| 420 | infoQueue->setFontSize(24); |
---|
| 421 | infoQueue->setFontColor(Vector4(1.0, 1.0, 0.0, 0.8)); |
---|
| 422 | infoQueue->setAlignment("HorzCentred"); |
---|
| 423 | infoQueue->setDisplaySize(Vector2(0.6, 0.0)); |
---|
| 424 | } |
---|
| 425 | |
---|
[2280] | 426 | } |
---|