[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/GUIManager.h" |
---|
| 39 | #include "core/LuaState.h" |
---|
| 40 | #include "util/ScopedSingletonManager.h" |
---|
[2911] | 41 | |
---|
[5619] | 42 | #include "interfaces/NotificationListener.h" |
---|
[2280] | 43 | |
---|
[7403] | 44 | #include "Notification.h" |
---|
| 45 | #include "NotificationQueue.h" |
---|
| 46 | |
---|
| 47 | #include "ToluaBindNotifications.h" |
---|
| 48 | |
---|
[2435] | 49 | namespace orxonox |
---|
| 50 | { |
---|
[2280] | 51 | |
---|
[6417] | 52 | const std::string NotificationManager::ALL("all"); |
---|
| 53 | const std::string NotificationManager::NONE("none"); |
---|
[2911] | 54 | |
---|
[7403] | 55 | // Register tolua_open function when loading the library. |
---|
| 56 | DeclareToluaInterface(Notifications); |
---|
[2911] | 57 | |
---|
[7403] | 58 | ManageScopedSingleton(NotificationManager, ScopeID::Graphics, false); |
---|
| 59 | |
---|
| 60 | SetConsoleCommand("enterEditMode", &NotificationManager::enterEditMode); |
---|
| 61 | |
---|
[2911] | 62 | /** |
---|
| 63 | @brief |
---|
| 64 | Constructor. Registers the Object. |
---|
| 65 | */ |
---|
| 66 | NotificationManager::NotificationManager() |
---|
[2280] | 67 | { |
---|
[2911] | 68 | RegisterRootObject(NotificationManager); |
---|
| 69 | |
---|
| 70 | this->highestIndex_ = 0; |
---|
[7403] | 71 | |
---|
| 72 | ModifyConsoleCommand("enterEditMode").setObject(this); |
---|
| 73 | |
---|
| 74 | COUT(3) << "NotificatioManager created." << std::endl; |
---|
[2280] | 75 | } |
---|
| 76 | |
---|
[2911] | 77 | /** |
---|
| 78 | @brief |
---|
| 79 | Destructor. |
---|
| 80 | */ |
---|
[2280] | 81 | NotificationManager::~NotificationManager() |
---|
| 82 | { |
---|
[7403] | 83 | ModifyConsoleCommand("enterEditMode").setObject(NULL); |
---|
[7163] | 84 | |
---|
[7403] | 85 | COUT(3) << "NotificationManager destroyed." << std::endl; |
---|
[2280] | 86 | } |
---|
[2501] | 87 | |
---|
[2911] | 88 | /** |
---|
| 89 | @brief |
---|
[7403] | 90 | Is called before the object is destroyed. |
---|
| 91 | */ |
---|
| 92 | void NotificationManager::preDestroy(void) |
---|
| 93 | { |
---|
| 94 | // Destroys all NotificationQueues that have been registered with the NotificationManager. |
---|
| 95 | for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); ) |
---|
| 96 | { |
---|
| 97 | NotificationQueue* queue = (*it).second; |
---|
| 98 | it++; |
---|
| 99 | queue->destroy(); |
---|
| 100 | } |
---|
| 101 | this->queues_.clear(); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | @brief |
---|
[5619] | 106 | Registers a Notification within the NotificationManager and makes sure that the Notification is sent to all the NotificationListeners associated with its sender. |
---|
[2911] | 107 | @param notification |
---|
| 108 | The Notification to be registered. |
---|
| 109 | @return |
---|
| 110 | Returns true if successful. |
---|
| 111 | */ |
---|
| 112 | bool NotificationManager::registerNotification(Notification* notification) |
---|
| 113 | { |
---|
[7403] | 114 | assert(notification); |
---|
[5619] | 115 | |
---|
[5748] | 116 | std::time_t time = std::time(0); //TODO: Doesn't this expire? //!< Get current time. |
---|
[5619] | 117 | |
---|
[7403] | 118 | // Add the Notification to the list that holds all Notifications. |
---|
| 119 | this->allNotificationsList_.insert(std::pair<std::time_t, Notification*>(time, notification)); |
---|
[5619] | 120 | |
---|
[7403] | 121 | if(notification->getSender() == NotificationManager::NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications. |
---|
[2911] | 122 | return true; |
---|
[5619] | 123 | |
---|
[2911] | 124 | bool all = false; |
---|
[7403] | 125 | if(notification->getSender() == NotificationManager::ALL) // If all are the sender, then the Notifications is added to every NotificationListener. |
---|
[2911] | 126 | all = true; |
---|
[5619] | 127 | |
---|
[7403] | 128 | // Insert the Notification in all NotificationListeners that have its sender as target. |
---|
| 129 | for(std::map<NotificationListener*, unsigned int>::iterator it = this->listenerList_.begin(); it != this->listenerList_.end(); it++) // Iterate through all NotificationListeners. |
---|
[2435] | 130 | { |
---|
[7403] | 131 | std::set<std::string, NotificationListenerStringCompare> set = it->first->getTargetsSet(); |
---|
| 132 | bool bAll = set.find(NotificationManager::ALL) != set.end(); |
---|
| 133 | // If either the Notification has as sender 'all', the NotificationListener displays all Notifications or the NotificationListener has the sender of the Notification as target. |
---|
| 134 | if(all || bAll || set.find(notification->getSender()) != set.end()) |
---|
[2280] | 135 | { |
---|
[7403] | 136 | if(!bAll) |
---|
| 137 | this->notificationLists_[it->second]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the notifications list of the current NotificationListener. |
---|
| 138 | it->first->update(notification, time); // Update the NotificationListener. |
---|
[2435] | 139 | } |
---|
| 140 | } |
---|
[5619] | 141 | |
---|
[7403] | 142 | COUT(4) << "Notification (&" << notification << ") registered with the NotificationManager." << std::endl; |
---|
[5619] | 143 | |
---|
[2280] | 144 | return true; |
---|
| 145 | } |
---|
[5619] | 146 | |
---|
[2911] | 147 | /** |
---|
| 148 | @brief |
---|
[7403] | 149 | Unregisters a Notification within the NotificationManager for a given NotificationListener. |
---|
[7163] | 150 | @param notification |
---|
| 151 | A pointer to the Notification to be unregistered. |
---|
| 152 | @param listener |
---|
| 153 | A pointer to the NotificationListener the Notification is unregistered for. |
---|
| 154 | */ |
---|
| 155 | void NotificationManager::unregisterNotification(Notification* notification, NotificationListener* listener) |
---|
| 156 | { |
---|
| 157 | assert(notification); |
---|
| 158 | assert(listener); |
---|
| 159 | |
---|
[7403] | 160 | // Remove the Notification from the list of Notifications of the input NotificationListener. |
---|
| 161 | this->removeNotification(notification, *(this->notificationLists_.find(this->listenerList_.find(listener)->second)->second)); |
---|
[7163] | 162 | |
---|
[7403] | 163 | COUT(4) << "Notification (&" << notification << ") unregistered with the NotificationManager from listener (&" << listener << ")" << std::endl; |
---|
[7163] | 164 | } |
---|
| 165 | |
---|
| 166 | /** |
---|
| 167 | @brief |
---|
[7403] | 168 | Helper method that removes an input Notification form an input map. |
---|
[7163] | 169 | @param notification |
---|
[7403] | 170 | A pointer to the Notification to be removed. |
---|
[7163] | 171 | @param map |
---|
[7403] | 172 | The map the Notification should be removed from. |
---|
[7163] | 173 | @return |
---|
| 174 | Returns true if successful. |
---|
| 175 | */ |
---|
| 176 | bool NotificationManager::removeNotification(Notification* notification, std::multimap<std::time_t, Notification*>& map) |
---|
| 177 | { |
---|
| 178 | // Iterates through all items in the map until the Notification is found. |
---|
| 179 | //TODO: Do more efficiently? |
---|
| 180 | for(std::multimap<std::time_t, Notification*>::iterator it = map.begin(); it != map.end(); it++) |
---|
| 181 | { |
---|
| 182 | if(it->second == notification) |
---|
| 183 | { |
---|
| 184 | map.erase(it); |
---|
| 185 | return true; |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | return false; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /** |
---|
| 192 | @brief |
---|
[5619] | 193 | Registers a NotificationListener within the NotificationManager. |
---|
| 194 | @param listener |
---|
| 195 | The NotificationListener to be registered. |
---|
[2911] | 196 | @return |
---|
[7403] | 197 | Returns true if successful. Fales if the NotificationListener is already registered. |
---|
[2911] | 198 | */ |
---|
[5619] | 199 | bool NotificationManager::registerListener(NotificationListener* listener) |
---|
[2280] | 200 | { |
---|
[7403] | 201 | assert(listener); |
---|
| 202 | |
---|
| 203 | // If the NotificationListener is already registered. |
---|
| 204 | if(this->listenerList_.find(listener) != this->listenerList_.end()) |
---|
| 205 | return false; |
---|
| 206 | |
---|
[2911] | 207 | this->highestIndex_ += 1; |
---|
[7403] | 208 | unsigned int index = this->highestIndex_; // An identifier that identifies each registered NotificationListener uniquely. |
---|
[5619] | 209 | |
---|
[7403] | 210 | this->listenerList_[listener] = index; // Add the NotificationListener to the list of NotificationListeners. |
---|
[5619] | 211 | |
---|
[7403] | 212 | std::set<std::string, NotificationListenerStringCompare> set = listener->getTargetsSet(); |
---|
[5619] | 213 | |
---|
[7403] | 214 | // If all senders are the target of the NotificationListener, then the list of Notifications for that specific NotificationListener is the same as the list of all Notifications. |
---|
| 215 | bool bAll = set.find(NotificationManager::ALL) != set.end(); |
---|
| 216 | std::multimap<std::time_t, Notification*>* map; |
---|
| 217 | if(bAll) |
---|
| 218 | this->notificationLists_[index] = &this->allNotificationsList_; |
---|
| 219 | // Else a new list (resp. multimap) is created and added to the list of Notification lists for NotificationListeners. |
---|
| 220 | else |
---|
[2284] | 221 | { |
---|
[7403] | 222 | this->notificationLists_[index] = new std::multimap<std::time_t, Notification*>; |
---|
| 223 | map = this->notificationLists_[index]; |
---|
[2284] | 224 | } |
---|
[5619] | 225 | |
---|
[7403] | 226 | // Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationListener. |
---|
| 227 | for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++) |
---|
[2911] | 228 | { |
---|
[7403] | 229 | if(!bAll && set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current Notification as target. |
---|
| 230 | map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); |
---|
[2911] | 231 | } |
---|
[2501] | 232 | |
---|
[7403] | 233 | listener->update(); // Update the listener. |
---|
[5619] | 234 | |
---|
[7163] | 235 | COUT(4) << "NotificationListener registered with the NotificationManager." << std::endl; |
---|
[5619] | 236 | |
---|
[2911] | 237 | return true; |
---|
[2280] | 238 | } |
---|
[5619] | 239 | |
---|
[2911] | 240 | /** |
---|
| 241 | @brief |
---|
[7403] | 242 | Unregisters a NotificationListener within the NotificationManager. |
---|
| 243 | @param listener |
---|
| 244 | The NotificationListener to be unregistered. |
---|
[7163] | 245 | */ |
---|
| 246 | void NotificationManager::unregisterListener(NotificationListener* listener) |
---|
| 247 | { |
---|
| 248 | assert(listener); |
---|
| 249 | |
---|
[7403] | 250 | //TODO: Make unsigned int. |
---|
| 251 | unsigned int identifier = this->listenerList_.find(listener)->second; |
---|
[7163] | 252 | std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(identifier)->second; |
---|
| 253 | |
---|
[7403] | 254 | // If the map is not the map of all Notifications, make sure all Notifications are unregistered. |
---|
[7163] | 255 | std::multimap<std::time_t, Notification*>::iterator it = map->begin(); |
---|
[7403] | 256 | if(map != &this->allNotificationsList_) |
---|
[7163] | 257 | { |
---|
[7403] | 258 | while(it != map->end()) |
---|
| 259 | { |
---|
| 260 | this->unregisterNotification(it->second, listener); |
---|
| 261 | it = map->begin(); |
---|
| 262 | } |
---|
| 263 | delete map; |
---|
[7163] | 264 | } |
---|
| 265 | |
---|
[7403] | 266 | // Remove the NotificationListener from the list of NotificationListeners. |
---|
[7163] | 267 | this->listenerList_.erase(listener); |
---|
[7403] | 268 | // Remove the Notifications list that was associated with the input NotificationListener. |
---|
[7163] | 269 | this->notificationLists_.erase(identifier); |
---|
| 270 | |
---|
| 271 | COUT(4) << "NotificationListener unregistered with the NotificationManager." << std::endl; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | /** |
---|
| 275 | @brief |
---|
[7403] | 276 | Fetches the Notifications for a specific NotificationListener in a specified timeframe and stores them in the input map. |
---|
[5619] | 277 | @param listener |
---|
| 278 | The NotificationListener the Notifications are fetched for. |
---|
[2911] | 279 | @param map |
---|
[7403] | 280 | A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated. |
---|
[2911] | 281 | @param timeFrameStart |
---|
| 282 | The start time of the timeframe. |
---|
| 283 | @param timeFrameEnd |
---|
| 284 | The end time of the timeframe. |
---|
| 285 | @return |
---|
| 286 | Returns true if successful. |
---|
| 287 | */ |
---|
[7403] | 288 | void NotificationManager::getNotifications(NotificationListener* listener, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd) |
---|
[2346] | 289 | { |
---|
[7403] | 290 | assert(listener); |
---|
| 291 | assert(map); |
---|
[2501] | 292 | |
---|
[7403] | 293 | std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; // All the Notifications for the input NotificationListener. |
---|
[5619] | 294 | |
---|
[2911] | 295 | std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest; |
---|
[7403] | 296 | // Iterators pointing to the bounds specified by the specified start and end times of the time frame. |
---|
[2911] | 297 | itLowest = notifications->lower_bound(timeFrameStart); |
---|
[7403] | 298 | itHighest = notifications->upper_bound(timeFrameEnd); |
---|
[5619] | 299 | |
---|
[7403] | 300 | for(it = itLowest; it != itHighest; it++) // Iterate through the Notifications from the start of the time frame to the end of it. |
---|
| 301 | map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); // Add the found Notifications to the map. |
---|
| 302 | } |
---|
[5619] | 303 | |
---|
[7403] | 304 | /** |
---|
| 305 | @brief |
---|
| 306 | Enters the edit mode of the NotificationLayer. |
---|
| 307 | */ |
---|
| 308 | void NotificationManager::enterEditMode(void) |
---|
| 309 | { |
---|
| 310 | GUIManager::getInstance().hideGUI("NotificationLayer"); |
---|
| 311 | GUIManager::getInstance().showGUI("NotificationLayer", false, false); |
---|
| 312 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.enterEditMode()"); |
---|
[2346] | 313 | } |
---|
[2280] | 314 | |
---|
[7403] | 315 | /** |
---|
| 316 | @brief |
---|
| 317 | Registers a NotificationQueue. |
---|
| 318 | This makes sure that the NotificationQueue can be attained through lua by name. It also makes sure that the NotificationQueue is destroyed upon destruction of the NotificationManager. |
---|
| 319 | @param queue |
---|
| 320 | A pointer to the NotificationQueue to be registered. |
---|
| 321 | @return |
---|
| 322 | Returns true if successful. If e.g. the a NotificationQueue with that name already exists this returns false. |
---|
| 323 | */ |
---|
| 324 | bool NotificationManager::registerQueue(NotificationQueue* queue) |
---|
| 325 | { |
---|
| 326 | return this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)).second; |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | /** |
---|
| 330 | @brief |
---|
| 331 | Unregisters a NotificationQueue. |
---|
| 332 | @param queue |
---|
| 333 | A pointer to the NotificationQueue to be unregistered. |
---|
| 334 | */ |
---|
| 335 | void NotificationManager::unregisterQueue(NotificationQueue* queue) |
---|
| 336 | { |
---|
| 337 | this->queues_.erase(queue->getName()); |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | /** |
---|
| 341 | @brief |
---|
| 342 | Loads all the NotificationQueues that should exist. |
---|
| 343 | */ |
---|
| 344 | void NotificationManager::loadQueues(void) |
---|
| 345 | { |
---|
| 346 | new NotificationQueue("all"); |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | /** |
---|
| 350 | @brief |
---|
| 351 | Creates a new NotificationQueue. |
---|
| 352 | This is used in lua. |
---|
| 353 | @param name |
---|
| 354 | The name of the new NotificationQueue. |
---|
| 355 | */ |
---|
| 356 | void NotificationManager::createQueue(const std::string& name) |
---|
| 357 | { |
---|
| 358 | new NotificationQueue(name); |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | /** |
---|
| 362 | @brief |
---|
| 363 | Get the NotificationQueue with the input name. |
---|
| 364 | @param name |
---|
| 365 | The name of the NotificationQueue. |
---|
| 366 | @return |
---|
| 367 | Returns a pointer to the NotificationQueue with the input name. Returns NULL if no NotificationQueue with such a name exists. |
---|
| 368 | */ |
---|
| 369 | NotificationQueue* NotificationManager::getQueue(const std::string & name) |
---|
| 370 | { |
---|
| 371 | std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.find(name); |
---|
| 372 | // Returns NULL if no such NotificationQueue exists. |
---|
| 373 | if(it == this->queues_.end()) |
---|
| 374 | return NULL; |
---|
| 375 | |
---|
| 376 | return (*it).second; |
---|
| 377 | } |
---|
| 378 | |
---|
[2280] | 379 | } |
---|