[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 NotificationQueue.cc |
---|
[2911] | 31 | @brief Implementation of the NotificationQueue class. |
---|
| 32 | */ |
---|
| 33 | |
---|
[2280] | 34 | #include "NotificationQueue.h" |
---|
| 35 | |
---|
[7403] | 36 | #include <map> |
---|
| 37 | #include <sstream> |
---|
| 38 | |
---|
| 39 | #include "core/CoreIncludes.h" |
---|
| 40 | #include "core/GUIManager.h" |
---|
| 41 | #include "core/LuaState.h" |
---|
[7163] | 42 | #include "util/Convert.h" |
---|
[7403] | 43 | #include "util/SubString.h" |
---|
[2280] | 44 | |
---|
[7403] | 45 | #include "Notification.h" |
---|
| 46 | |
---|
[2435] | 47 | namespace orxonox |
---|
| 48 | { |
---|
[2910] | 49 | |
---|
[2911] | 50 | /** |
---|
| 51 | @brief |
---|
| 52 | Constructor. Creates and initializes the object. |
---|
[7403] | 53 | @param name |
---|
| 54 | The name of the new NotificationQueue. |
---|
| 55 | @param senders |
---|
| 56 | The senders that are targets of this NotificationQueue, i.e. the names of senders whose Notifications this NotificationQueue displays. |
---|
| 57 | The senders need to be seperated by commas. |
---|
| 58 | @param size |
---|
| 59 | The size (the maximum number of displayed Notifications) of this NotificationQueue. |
---|
| 60 | @param displayTime |
---|
| 61 | The time during which a Notification is (at most) displayed. |
---|
[2911] | 62 | */ |
---|
[7403] | 63 | NotificationQueue::NotificationQueue(const std::string& name, const std::string& senders, unsigned int size, unsigned int displayTime) |
---|
[2280] | 64 | { |
---|
[7163] | 65 | this->registered_ = false; |
---|
| 66 | |
---|
[7403] | 67 | RegisterRootObject(NotificationQueue); |
---|
[2911] | 68 | |
---|
[7403] | 69 | // Initialize. |
---|
[2911] | 70 | this->size_ = 0; |
---|
[7413] | 71 | this->tickTime_ = 0.0f; |
---|
[2911] | 72 | |
---|
[7403] | 73 | // Sets the input values. |
---|
| 74 | this->setTargets(senders); |
---|
| 75 | this->name_ = name; |
---|
| 76 | this->maxSize_ = size; |
---|
| 77 | this->setDisplayTime(displayTime); |
---|
| 78 | |
---|
| 79 | // Register the NotificationQueue with the NotificationManager. |
---|
| 80 | bool queueRegistered = NotificationManager::getInstance().registerQueue(this); |
---|
[7163] | 81 | this->registered_ = true; |
---|
[7403] | 82 | if(!queueRegistered) // If the registration has failed. |
---|
| 83 | { |
---|
| 84 | this->registered_ = false; |
---|
[7413] | 85 | COUT(1) << "Error: NotificationQueue '" << this->getName() << "' could not be registered." << std::endl; |
---|
[7403] | 86 | return; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | this->create(); // Creates the NotificationQueue in lua. |
---|
| 90 | |
---|
| 91 | // register the NotificationQueue as NotificationListener with the NotificationManager. |
---|
| 92 | bool listenerRegistered = NotificationManager::getInstance().registerListener(this); |
---|
| 93 | if(!listenerRegistered) // If the registration has failed. |
---|
| 94 | { |
---|
| 95 | this->registered_ = false; |
---|
| 96 | // Remove the NotificationQueue in lua. |
---|
| 97 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeQueue(\"" + this->getName() + "\")"); |
---|
| 98 | NotificationManager::getInstance().unregisterQueue(this); |
---|
[7413] | 99 | COUT(1) << "Error: NotificationQueue '" << this->getName() << "' could not be registered." << std::endl; |
---|
[7403] | 100 | return; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | COUT(3) << "NotificationQueue '" << this->getName() << "' created." << std::endl; |
---|
[2911] | 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | @brief |
---|
[7403] | 108 | Destructor. |
---|
[2911] | 109 | */ |
---|
[7403] | 110 | NotificationQueue::~NotificationQueue() |
---|
[2911] | 111 | { |
---|
[7403] | 112 | this->targets_.clear(); |
---|
[2911] | 113 | |
---|
[7403] | 114 | if(this->registered_) // If the |
---|
| 115 | { |
---|
| 116 | this->clear(); |
---|
[2911] | 117 | |
---|
[7403] | 118 | // Unregister with the NotificationManager. |
---|
| 119 | NotificationManager::getInstance().unregisterListener(this); |
---|
| 120 | NotificationManager::getInstance().unregisterQueue(this); |
---|
| 121 | |
---|
| 122 | // Remove the NotificationQueue in lua. |
---|
| 123 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeQueue(\"" + this->getName() + "\")"); |
---|
| 124 | } |
---|
[2911] | 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | @brief |
---|
[7403] | 129 | Creates the NotificationQueue in lua. |
---|
[2911] | 130 | */ |
---|
[7403] | 131 | void NotificationQueue::create(void) |
---|
[2911] | 132 | { |
---|
[7403] | 133 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.createQueue(\"" + this->getName() + "\", " + multi_cast<std::string>(this->getMaxSize()) + ")"); |
---|
[2911] | 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
| 137 | @brief |
---|
| 138 | Updates the queue from time to time. |
---|
| 139 | @param dt |
---|
| 140 | The time interval that has passed since the last tick. |
---|
| 141 | */ |
---|
| 142 | void NotificationQueue::tick(float dt) |
---|
| 143 | { |
---|
[7403] | 144 | this->tickTime_ += dt; // Add the time interval that has passed to the time counter. |
---|
| 145 | if(this->tickTime_ >= 1.0) // If the time counter is greater than 1s all Notifications that have expired are removed, if it is smaller we wait to the next tick. |
---|
[2910] | 146 | { |
---|
[7403] | 147 | this->timeLimit_.time = std::time(0)-this->displayTime_; // Container containig the current time. |
---|
[2911] | 148 | |
---|
[7403] | 149 | std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator it = this->ordering_.begin(); |
---|
| 150 | // Iterate through all elements whose creation time is smaller than the current time minus the display time. |
---|
[7410] | 151 | while(it != this->ordering_.upper_bound(&this->timeLimit_)) |
---|
[2911] | 152 | { |
---|
[7412] | 153 | std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator temp = it; |
---|
[7403] | 154 | it++; |
---|
| 155 | this->remove(temp); // Remove the Notifications that have expired. |
---|
[2911] | 156 | } |
---|
| 157 | |
---|
[7403] | 158 | this->tickTime_ = this->tickTime_ - (int)this->tickTime_; // Reset time counter. |
---|
[2910] | 159 | } |
---|
[2911] | 160 | } |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | @brief |
---|
| 164 | Updates the NotificationQueue. |
---|
[7403] | 165 | Updates by clearing the queue and requesting all relevant Notifications from the NotificationManager and inserting them into the queue. |
---|
[2911] | 166 | */ |
---|
| 167 | void NotificationQueue::update(void) |
---|
| 168 | { |
---|
| 169 | this->clear(); |
---|
| 170 | |
---|
[7403] | 171 | std::multimap<std::time_t, Notification*>* notifications = new std::multimap<std::time_t, Notification*>; |
---|
| 172 | // Get the Notifications sent in the interval from now to now minus the display time. |
---|
| 173 | NotificationManager::getInstance().getNotifications(this, notifications, this->displayTime_); |
---|
[2910] | 174 | |
---|
[7403] | 175 | if(!notifications->empty()) |
---|
[2911] | 176 | { |
---|
[7403] | 177 | // Add all Notifications. |
---|
| 178 | for(std::multimap<std::time_t, Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) |
---|
| 179 | this->push(it->second, it->first); |
---|
[2911] | 180 | } |
---|
| 181 | |
---|
| 182 | delete notifications; |
---|
| 183 | |
---|
[7163] | 184 | COUT(4) << "NotificationQueue '" << this->getName() << "' updated." << std::endl; |
---|
[2280] | 185 | } |
---|
[2910] | 186 | |
---|
[2911] | 187 | /** |
---|
| 188 | @brief |
---|
| 189 | Updates the NotificationQueue by adding an new Notification. |
---|
| 190 | @param notification |
---|
| 191 | Pointer to the Notification. |
---|
| 192 | @param time |
---|
| 193 | The time the Notification was sent. |
---|
| 194 | */ |
---|
| 195 | void NotificationQueue::update(Notification* notification, const std::time_t & time) |
---|
[2280] | 196 | { |
---|
[7403] | 197 | this->push(notification, time); |
---|
[2910] | 198 | |
---|
[7403] | 199 | COUT(4) << "NotificationQueue '" << this->getName() << "' updated. A new Notification has been added." << std::endl; |
---|
[2280] | 200 | } |
---|
[2910] | 201 | |
---|
[2911] | 202 | /** |
---|
| 203 | @brief |
---|
[7403] | 204 | Adds (pushes) a Notification to the NotificationQueue. |
---|
| 205 | It inserts it into the storage containers, creates a corresponding container and pushes the Notification message to the GUI. |
---|
| 206 | @param notification |
---|
| 207 | The Notification to be pushed. |
---|
| 208 | @param time |
---|
| 209 | The time when the Notification has been sent. |
---|
[2911] | 210 | */ |
---|
[7403] | 211 | void NotificationQueue::push(Notification* notification, const std::time_t & time) |
---|
[2280] | 212 | { |
---|
[7403] | 213 | NotificationContainer* container = new NotificationContainer; |
---|
| 214 | container->notification = notification; |
---|
| 215 | container->time = time; |
---|
| 216 | |
---|
| 217 | // If the maximum size of the NotificationQueue has been reached the last (least recently added) Notification is removed. |
---|
| 218 | if(this->getSize() >= this->getMaxSize()) |
---|
| 219 | this->pop(); |
---|
| 220 | |
---|
| 221 | this->size_++; |
---|
| 222 | |
---|
| 223 | this->ordering_.insert(container); |
---|
| 224 | // Insert the Notification at the begin of the list (vector, actually). |
---|
| 225 | this->notifications_.insert(this->notifications_.begin(), container); |
---|
| 226 | |
---|
| 227 | // Push the Notification to the GUI. |
---|
| 228 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.pushNotification(\"" + this->getName() + "\", \"" + notification->getMessage() + "\")"); |
---|
[2911] | 229 | } |
---|
[2500] | 230 | |
---|
[2911] | 231 | /** |
---|
| 232 | @brief |
---|
[7403] | 233 | Removes (pops) the least recently added Notification form the NotificationQueue. |
---|
[2911] | 234 | */ |
---|
[7403] | 235 | void NotificationQueue::pop(void) |
---|
[2911] | 236 | { |
---|
[7403] | 237 | NotificationContainer* container = this->notifications_.back(); |
---|
[7412] | 238 | // Get all the NotificationContainers that were sent the same time the NotificationContainer we want to pop was sent. |
---|
| 239 | std::pair<std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator, std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator> iterators = this->ordering_.equal_range(container); |
---|
| 240 | // Iterate thourgh all suspects and remove the container as soon as we find it. |
---|
| 241 | for(std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator it = iterators.first; it != iterators.second; it++) |
---|
| 242 | { |
---|
| 243 | if(container == *it) |
---|
| 244 | { |
---|
| 245 | this->ordering_.erase(it); |
---|
| 246 | break; |
---|
| 247 | } |
---|
| 248 | } |
---|
[7403] | 249 | this->notifications_.pop_back(); |
---|
[2910] | 250 | |
---|
[7403] | 251 | this->size_--; |
---|
| 252 | |
---|
| 253 | delete container; |
---|
| 254 | |
---|
| 255 | // Pops the Notification from the GUI. |
---|
| 256 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.popNotification(\"" + this->getName() + "\")"); |
---|
[2911] | 257 | } |
---|
[2910] | 258 | |
---|
[2911] | 259 | /** |
---|
| 260 | @brief |
---|
[7403] | 261 | Removes the Notification that is stored in the input NotificationContainer. |
---|
[7412] | 262 | @param containerIterator |
---|
| 263 | An iterator to the NotificationContainer to be removed. |
---|
[2911] | 264 | */ |
---|
[7412] | 265 | void NotificationQueue::remove(const std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator& containerIterator) |
---|
[2911] | 266 | { |
---|
[7412] | 267 | std::vector<NotificationContainer*>::iterator it = std::find(this->notifications_.begin(), this->notifications_.end(), *containerIterator); |
---|
[7403] | 268 | // Get the index at which the Notification is. |
---|
| 269 | std::vector<NotificationContainer*>::difference_type index = it - this->notifications_.begin (); |
---|
[7412] | 270 | this->ordering_.erase(containerIterator); |
---|
[7403] | 271 | this->notifications_.erase(it); |
---|
[2911] | 272 | |
---|
[7403] | 273 | this->size_--; |
---|
| 274 | |
---|
[7412] | 275 | delete *containerIterator; |
---|
[7403] | 276 | |
---|
| 277 | // Removes the Notification from the GUI. |
---|
| 278 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeNotification(\"" + this->getName() + "\", " + multi_cast<std::string>(index) + ")"); |
---|
[2909] | 279 | } |
---|
[2500] | 280 | |
---|
[2911] | 281 | /** |
---|
| 282 | @brief |
---|
[7403] | 283 | Clears the NotificationQueue by removing all NotificationContainers. |
---|
[2911] | 284 | */ |
---|
[7403] | 285 | void NotificationQueue::clear(void) |
---|
[2280] | 286 | { |
---|
[7403] | 287 | this->ordering_.clear(); |
---|
| 288 | // Delete all NotificationContainers in the list. |
---|
| 289 | for(std::vector<NotificationContainer*>::iterator it = this->notifications_.begin(); it != this->notifications_.end(); it++) |
---|
| 290 | delete *it; |
---|
[2911] | 291 | |
---|
[7403] | 292 | this->notifications_.clear(); |
---|
[2911] | 293 | |
---|
[7403] | 294 | this->size_ = 0; |
---|
[2500] | 295 | |
---|
[7403] | 296 | // Clear the NotificationQueue in the GUI. |
---|
| 297 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.clearQueue(\"" + this->getName() + "\")"); |
---|
[2346] | 298 | } |
---|
[2500] | 299 | |
---|
[2911] | 300 | /** |
---|
| 301 | @brief |
---|
[7403] | 302 | Sets the name of the NotificationQueue. |
---|
| 303 | @param name |
---|
| 304 | The name to be set. |
---|
[2911] | 305 | */ |
---|
[7403] | 306 | void NotificationQueue::setName(const std::string& name) |
---|
[2280] | 307 | { |
---|
[7403] | 308 | this->name_ = name; |
---|
[2280] | 309 | } |
---|
[2500] | 310 | |
---|
[2911] | 311 | /** |
---|
| 312 | @brief |
---|
[7403] | 313 | Sets the maximum number of displayed Notifications. |
---|
| 314 | @param size |
---|
| 315 | The size to be set. |
---|
[2911] | 316 | */ |
---|
[7403] | 317 | void NotificationQueue::setMaxSize(unsigned int size) |
---|
[2280] | 318 | { |
---|
[7403] | 319 | if(this->maxSize_ == size) |
---|
| 320 | return; |
---|
[2911] | 321 | |
---|
[7403] | 322 | this->maxSize_ = size; |
---|
| 323 | |
---|
| 324 | if(this->registered_) |
---|
| 325 | this->update(); |
---|
[2911] | 326 | } |
---|
| 327 | |
---|
| 328 | /** |
---|
| 329 | @brief |
---|
[7403] | 330 | Sets the maximum number of seconds a Notification is displayed. |
---|
[2911] | 331 | @param time |
---|
[7403] | 332 | The number of seconds the Notifications is displayed. |
---|
| 333 | @return |
---|
| 334 | Returns true if successful. |
---|
[2911] | 335 | */ |
---|
[7403] | 336 | void NotificationQueue::setDisplayTime(unsigned int time) |
---|
[2911] | 337 | { |
---|
[7403] | 338 | if(this->displayTime_ == time) |
---|
| 339 | return; |
---|
[2911] | 340 | |
---|
[7403] | 341 | this->displayTime_ = time; |
---|
[2911] | 342 | |
---|
[7403] | 343 | if(this->registered_) |
---|
| 344 | this->update(); |
---|
[2911] | 345 | } |
---|
| 346 | |
---|
| 347 | /** |
---|
| 348 | @brief |
---|
[7403] | 349 | Produces all targets of the NotificationQueue concatinated as string, with kommas (',') as seperators. |
---|
[2911] | 350 | @return |
---|
[7403] | 351 | Returns the targets as a string. |
---|
[2911] | 352 | */ |
---|
[7403] | 353 | const std::string& NotificationQueue::getTargets(void) const |
---|
[2911] | 354 | { |
---|
[7403] | 355 | std::stringstream stream; |
---|
| 356 | bool first = true; |
---|
| 357 | // Iterate through the set of targets. |
---|
[7417] | 358 | for(std::set<std::string>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++) |
---|
[7403] | 359 | { |
---|
| 360 | if(!first) |
---|
| 361 | stream << ", "; |
---|
| 362 | else |
---|
| 363 | first = false; |
---|
| 364 | stream << *it; |
---|
| 365 | } |
---|
[2911] | 366 | |
---|
[7403] | 367 | return *(new std::string(stream.str())); |
---|
[2911] | 368 | } |
---|
| 369 | |
---|
| 370 | /** |
---|
| 371 | @brief |
---|
[7403] | 372 | Sets the targets of the NotificationQueue. |
---|
| 373 | The targets are the senders whose Notifications are displayed in this queue. |
---|
| 374 | @param targets |
---|
| 375 | Accepts a string of targets, each seperated by commas (','), spaces are ignored. |
---|
[2911] | 376 | */ |
---|
[7403] | 377 | void NotificationQueue::setTargets(const std::string & targets) |
---|
[2911] | 378 | { |
---|
[7403] | 379 | this->targets_.clear(); |
---|
| 380 | |
---|
| 381 | SubString string = SubString(targets, ",", " ", false); |
---|
| 382 | for(unsigned int i = 0; i < string.size(); i++) |
---|
| 383 | this->targets_.insert(string[i]); |
---|
| 384 | |
---|
| 385 | if(this->registered_) |
---|
[2911] | 386 | { |
---|
[7403] | 387 | NotificationManager::getInstance().unregisterListener(this); |
---|
| 388 | NotificationManager::getInstance().registerListener(this); |
---|
[2911] | 389 | } |
---|
| 390 | } |
---|
| 391 | |
---|
[2280] | 392 | } |
---|
[7403] | 393 | |
---|