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