[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 | /** |
---|
[3196] | 30 | @file |
---|
[2911] | 31 | @brief Implementation of the NotificationQueue class. |
---|
| 32 | */ |
---|
| 33 | |
---|
[2280] | 34 | #include "NotificationQueue.h" |
---|
| 35 | |
---|
[7163] | 36 | #include "util/Convert.h" |
---|
[2280] | 37 | #include "core/CoreIncludes.h" |
---|
| 38 | #include "core/XMLPort.h" |
---|
[2911] | 39 | #include "NotificationOverlay.h" |
---|
[5722] | 40 | #include "NotificationManager.h" |
---|
[2280] | 41 | |
---|
[2435] | 42 | namespace orxonox |
---|
| 43 | { |
---|
[2910] | 44 | |
---|
[2280] | 45 | CreateFactory(NotificationQueue); |
---|
| 46 | |
---|
[6417] | 47 | const std::string NotificationQueue::DEFAULT_FONT("VeraMono"); |
---|
| 48 | const Vector2 NotificationQueue::DEFAULT_POSITION(0.0,0.0); |
---|
| 49 | const float NotificationQueue::DEFAULT_FONT_SIZE = 0.025f; |
---|
[2911] | 50 | |
---|
| 51 | /** |
---|
| 52 | @brief |
---|
| 53 | Constructor. Creates and initializes the object. |
---|
| 54 | */ |
---|
| 55 | NotificationQueue::NotificationQueue(BaseObject* creator) : OverlayGroup(creator) |
---|
[2280] | 56 | { |
---|
[7163] | 57 | this->registered_ = false; |
---|
| 58 | |
---|
[3196] | 59 | RegisterObject(NotificationQueue); |
---|
[2911] | 60 | this->initialize(); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | @brief |
---|
| 65 | Destructor. |
---|
| 66 | */ |
---|
| 67 | NotificationQueue::~NotificationQueue() |
---|
| 68 | { |
---|
| 69 | this->targets_.clear(); |
---|
| 70 | this->clear(); |
---|
[7163] | 71 | |
---|
| 72 | if(this->registered_) |
---|
| 73 | NotificationManager::getInstance().unregisterListener(this); |
---|
[2911] | 74 | } |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | @brief |
---|
| 78 | Initializes the object. |
---|
| 79 | Registers the object, initializes variables, sets default values and registers the queue with the NotificationManager. |
---|
| 80 | */ |
---|
| 81 | void NotificationQueue::initialize(void) |
---|
| 82 | { |
---|
| 83 | this->size_ = 0; |
---|
| 84 | this->tickTime_ = 0.0; |
---|
| 85 | |
---|
[5619] | 86 | NotificationManager::getInstance().registerListener(this); |
---|
[7163] | 87 | this->registered_ = true; |
---|
[2911] | 88 | } |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | @brief |
---|
| 92 | Sets the defaults. |
---|
| 93 | */ |
---|
| 94 | void NotificationQueue::setDefaults(void) |
---|
| 95 | { |
---|
| 96 | this->setMaxSize(DEFAULT_SIZE); |
---|
| 97 | this->setNotificationLength(DEFAULT_LENGTH); |
---|
| 98 | this->setDisplayTime(DEFAULT_DISPLAY_TIME); |
---|
| 99 | this->setPosition(DEFAULT_POSITION); |
---|
| 100 | |
---|
| 101 | this->setTargets(NotificationManager::ALL); |
---|
| 102 | |
---|
| 103 | this->setFontSize(DEFAULT_FONT_SIZE); |
---|
| 104 | this->setFont(DEFAULT_FONT); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /** |
---|
| 108 | @brief |
---|
| 109 | Method for creating a NotificationQueue object through XML. |
---|
| 110 | */ |
---|
[7297] | 111 | void NotificationQueue::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[2911] | 112 | { |
---|
[7297] | 113 | SUPER(NotificationQueue, XMLPort, xmlelement, mode); |
---|
[2911] | 114 | |
---|
| 115 | this->setDefaults(); |
---|
| 116 | |
---|
[7297] | 117 | XMLPortParam(NotificationQueue, "maxSize", setMaxSize, getMaxSize, xmlelement, mode); |
---|
| 118 | XMLPortParam(NotificationQueue, "notificationLength", setNotificationLength, getNotificationLength, xmlelement, mode); |
---|
| 119 | XMLPortParam(NotificationQueue, "displayTime", setDisplayTime, getDisplayTime, xmlelement, mode); |
---|
| 120 | XMLPortParam(NotificationQueue, "targets", setTargets, getTargets, xmlelement, mode); |
---|
| 121 | XMLPortParam(NotificationQueue, "font", setFont, getFont, xmlelement, mode); |
---|
| 122 | XMLPortParam(NotificationQueue, "fontSize", setFontSize, getFontSize, xmlelement, mode); |
---|
| 123 | XMLPortParam(NotificationQueue, "position", setPosition, getPosition, xmlelement, mode); |
---|
[2911] | 124 | |
---|
[7163] | 125 | COUT(3) << "NotificationQueue '" << this->getName() << "' created." << std::endl; |
---|
[2911] | 126 | } |
---|
| 127 | |
---|
| 128 | /** |
---|
| 129 | @brief |
---|
| 130 | Updates the queue from time to time. |
---|
| 131 | @param dt |
---|
| 132 | The time interval that has passed since the last tick. |
---|
| 133 | */ |
---|
| 134 | void NotificationQueue::tick(float dt) |
---|
| 135 | { |
---|
| 136 | this->tickTime_ += dt; //!< Add the time interval that has passed to the time counter. |
---|
| 137 | 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] | 138 | { |
---|
[2911] | 139 | this->timeLimit_.time = std::time(0)-this->displayTime_; //!< Container containig the current time. |
---|
| 140 | |
---|
| 141 | std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it; |
---|
| 142 | it = this->containers_.begin(); |
---|
| 143 | while(it != this->containers_.upper_bound(&this->timeLimit_)) //!< Iterate through all elements whose creation time is smaller than the current time minus the display time. |
---|
| 144 | { |
---|
| 145 | this->removeContainer(*it); |
---|
[6502] | 146 | this->scroll(Vector2(0.0f,-(1.1f*this->getFontSize()))); |
---|
[5748] | 147 | it = this->containers_.begin(); //TODO: Needed? |
---|
[2911] | 148 | } |
---|
| 149 | |
---|
[6502] | 150 | this->tickTime_ = 0.0f; //!< Reset time counter. |
---|
[2910] | 151 | } |
---|
[2911] | 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | @brief |
---|
| 156 | Updates the NotificationQueue. |
---|
| 157 | Updates by clearing the queue and requesting all relevant Notifications from the NotificationManager and inserting the in the queue. |
---|
| 158 | */ |
---|
| 159 | void NotificationQueue::update(void) |
---|
| 160 | { |
---|
| 161 | this->clear(); |
---|
| 162 | |
---|
| 163 | std::multimap<std::time_t,Notification*>* notifications = new std::multimap<std::time_t,Notification*>; |
---|
| 164 | if(!NotificationManager::getInstance().getNotifications(this, notifications, this->displayTime_)) //!< Get the Notifications sent in the interval form now to minus the display time. |
---|
[2910] | 165 | { |
---|
[2911] | 166 | COUT(1) << "NotificationQueue update failed due to undetermined cause." << std::endl; |
---|
| 167 | return; |
---|
[2910] | 168 | } |
---|
| 169 | |
---|
[2911] | 170 | if(notifications->empty()) |
---|
| 171 | return; |
---|
| 172 | |
---|
| 173 | for(std::multimap<std::time_t,Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) //!> Add all Notifications. |
---|
| 174 | { |
---|
| 175 | this->addNotification(it->second, it->first); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | delete notifications; |
---|
| 179 | |
---|
[7163] | 180 | COUT(4) << "NotificationQueue '" << this->getName() << "' updated." << std::endl; |
---|
[2280] | 181 | } |
---|
[2910] | 182 | |
---|
[2911] | 183 | /** |
---|
| 184 | @brief |
---|
| 185 | Updates the NotificationQueue by adding an new Notification. |
---|
| 186 | @param notification |
---|
| 187 | Pointer to the Notification. |
---|
| 188 | @param time |
---|
| 189 | The time the Notification was sent. |
---|
| 190 | */ |
---|
| 191 | void NotificationQueue::update(Notification* notification, const std::time_t & time) |
---|
[2280] | 192 | { |
---|
[2911] | 193 | this->addNotification(notification, time); |
---|
[2910] | 194 | |
---|
[2911] | 195 | std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it; |
---|
| 196 | while(this->getSize() > this->getMaxSize()) |
---|
| 197 | { |
---|
| 198 | it = this->containers_.begin(); |
---|
| 199 | this->removeContainer(*it); |
---|
[6502] | 200 | this->scroll(Vector2(0.0f,-(1.1f*this->getFontSize()))); |
---|
[2911] | 201 | } |
---|
| 202 | |
---|
[7163] | 203 | COUT(4) << "NotificationQueue '" << this->getName() << "' updated. A new Notifications has been added." << std::endl; |
---|
[2280] | 204 | } |
---|
[2910] | 205 | |
---|
[2911] | 206 | /** |
---|
| 207 | @brief |
---|
| 208 | Sets the maximum number of displayed Notifications. |
---|
| 209 | @param size |
---|
| 210 | The size to be set. |
---|
| 211 | @return |
---|
| 212 | Returns true if successful. |
---|
| 213 | */ |
---|
| 214 | bool NotificationQueue::setMaxSize(int size) |
---|
[2280] | 215 | { |
---|
[2911] | 216 | if(size < 0) |
---|
| 217 | return false; |
---|
| 218 | this->maxSize_ = size; |
---|
| 219 | this->update(); |
---|
| 220 | return true; |
---|
| 221 | } |
---|
[2500] | 222 | |
---|
[2911] | 223 | /** |
---|
| 224 | @brief |
---|
| 225 | Sets the maximum number of characters a Notification message displayed by this queue is allowed to have. |
---|
| 226 | @param length |
---|
| 227 | The length to be set. |
---|
| 228 | @return |
---|
| 229 | Returns true if successful. |
---|
| 230 | */ |
---|
| 231 | bool NotificationQueue::setNotificationLength(int length) |
---|
| 232 | { |
---|
| 233 | if(length < 0) |
---|
| 234 | return false; |
---|
| 235 | this->notificationLength_ = length; |
---|
| 236 | this->update(); |
---|
| 237 | return true; |
---|
[2280] | 238 | } |
---|
[2910] | 239 | |
---|
[2911] | 240 | /** |
---|
| 241 | @brief |
---|
| 242 | Sets the maximum number of seconds a Notification is displayed. |
---|
| 243 | @param time |
---|
| 244 | The number of seconds the Notifications is displayed. |
---|
| 245 | @return |
---|
| 246 | Returns true if successful. |
---|
| 247 | */ |
---|
| 248 | bool NotificationQueue::setDisplayTime(int time) |
---|
[2280] | 249 | { |
---|
[2911] | 250 | if(time < 0) |
---|
| 251 | return false; |
---|
| 252 | this->displayTime_ = time; |
---|
| 253 | this->update(); |
---|
| 254 | return true; |
---|
| 255 | } |
---|
[2910] | 256 | |
---|
[2911] | 257 | /** |
---|
| 258 | @brief |
---|
| 259 | Produces all targets concatinated as string, with kommas (',') as seperators. |
---|
| 260 | @param string |
---|
| 261 | Pointer to a string which will be used by the method to fill with the concatination of the targets. |
---|
| 262 | @return |
---|
| 263 | Returns true if successful. |
---|
| 264 | */ |
---|
| 265 | bool NotificationQueue::getTargets(std::string* string) const |
---|
| 266 | { |
---|
| 267 | if(string == NULL) |
---|
| 268 | { |
---|
| 269 | COUT(4) << "Input string must have memory allocated." << std::endl; |
---|
| 270 | return false; |
---|
| 271 | } |
---|
| 272 | string->clear(); |
---|
| 273 | bool first = true; |
---|
[2926] | 274 | for(std::set<std::string>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++) //!< Iterate through the set of targets. |
---|
[2911] | 275 | { |
---|
| 276 | if(!first) |
---|
| 277 | { |
---|
[6417] | 278 | *string += ','; |
---|
[2911] | 279 | } |
---|
| 280 | else |
---|
| 281 | { |
---|
| 282 | first = false; |
---|
| 283 | } |
---|
| 284 | *string += *it; |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | return true; |
---|
[2909] | 288 | } |
---|
[2500] | 289 | |
---|
[2911] | 290 | /** |
---|
| 291 | @brief |
---|
| 292 | Sets the targets of the queue. |
---|
| 293 | The targets are the senders whose Notifications are displayed in this queue. |
---|
| 294 | @param targets |
---|
| 295 | Accepts a string of targets, each seperated by commas (','), spaces are ignored. |
---|
| 296 | @return |
---|
| 297 | Returns true if successful. |
---|
| 298 | */ |
---|
| 299 | bool NotificationQueue::setTargets(const std::string & targets) |
---|
[2280] | 300 | { |
---|
[2911] | 301 | this->targets_.clear(); |
---|
| 302 | |
---|
| 303 | std::string* pTemp; |
---|
| 304 | unsigned int index = 0; |
---|
| 305 | while( index < targets.size() ) //!< Go through the string, character by character until the end is reached. |
---|
[2280] | 306 | { |
---|
[6417] | 307 | pTemp = new std::string(); |
---|
[2911] | 308 | while(index < targets.size() && targets[index] != ',' && targets[index] != ' ') |
---|
| 309 | { |
---|
| 310 | *pTemp += targets[index]; |
---|
| 311 | index++; |
---|
| 312 | } |
---|
| 313 | index++; |
---|
| 314 | this->targets_.insert(*pTemp); |
---|
[2280] | 315 | } |
---|
[2911] | 316 | |
---|
| 317 | return true; |
---|
[2280] | 318 | } |
---|
[2500] | 319 | |
---|
[2911] | 320 | /** |
---|
| 321 | @brief |
---|
| 322 | Sets the font size. |
---|
| 323 | @param size |
---|
| 324 | The font size. |
---|
| 325 | @return |
---|
| 326 | Returns true if successful. |
---|
| 327 | */ |
---|
| 328 | bool NotificationQueue::setFontSize(float size) |
---|
[2346] | 329 | { |
---|
[2911] | 330 | if(size <= 0) |
---|
| 331 | return false; |
---|
| 332 | this->fontSize_ = size; |
---|
| 333 | for (std::map<Notification*, NotificationOverlayContainer*>::iterator it = this->overlays_.begin(); it != this->overlays_.end(); it++) //!< Set the font size for each overlay. |
---|
[2346] | 334 | { |
---|
[2911] | 335 | it->second->overlay->setFontSize(size); |
---|
[2346] | 336 | } |
---|
[2911] | 337 | return true; |
---|
[2346] | 338 | } |
---|
[2500] | 339 | |
---|
[2911] | 340 | /** |
---|
| 341 | @brief |
---|
| 342 | Sets the font. |
---|
| 343 | @param font |
---|
| 344 | The font. |
---|
| 345 | @return |
---|
| 346 | Returns true if successful. |
---|
| 347 | */ |
---|
| 348 | bool NotificationQueue::setFont(const std::string & font) |
---|
[2280] | 349 | { |
---|
[2911] | 350 | this->font_ = font; |
---|
| 351 | for (std::map<Notification*, NotificationOverlayContainer*>::iterator it = this->overlays_.begin(); it != this->overlays_.end(); it++) //!< Set the font for each overlay. |
---|
| 352 | { |
---|
| 353 | it->second->overlay->setFont(font); |
---|
| 354 | } |
---|
| 355 | return true; |
---|
[2280] | 356 | } |
---|
[2500] | 357 | |
---|
[2911] | 358 | /** |
---|
| 359 | @brief |
---|
| 360 | Scrolls the NotificationQueue, meaning all NotificationOverlays are moved the input vector. |
---|
| 361 | @param pos |
---|
| 362 | The vector the NotificationQueue is scrolled. |
---|
| 363 | */ |
---|
| 364 | void NotificationQueue::scroll(const Vector2 pos) |
---|
[2280] | 365 | { |
---|
[2911] | 366 | for (std::map<Notification*, NotificationOverlayContainer*>::iterator it = this->overlays_.begin(); it != this->overlays_.end(); ++it) //!< Scroll each overlay. |
---|
| 367 | { |
---|
| 368 | it->second->overlay->scroll(pos); |
---|
| 369 | } |
---|
[2280] | 370 | } |
---|
[2911] | 371 | |
---|
| 372 | /** |
---|
| 373 | @brief |
---|
| 374 | Aligns all the Notifications to the position of the NotificationQueue. |
---|
| 375 | */ |
---|
| 376 | void NotificationQueue::positionChanged(void) |
---|
| 377 | { |
---|
| 378 | int counter = 0; |
---|
| 379 | for (std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it = this->containers_.begin(); it != this->containers_.end(); it++) //!< Set the position for each overlay. |
---|
| 380 | { |
---|
| 381 | (*it)->overlay->setPosition(this->getPosition()); |
---|
[6502] | 382 | (*it)->overlay->scroll(Vector2(0.0f,(1.1f*this->getFontSize())*counter)); |
---|
[2911] | 383 | counter++; |
---|
| 384 | } |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | /** |
---|
| 388 | @brief |
---|
| 389 | Adds a Notification, to the queue. |
---|
| 390 | It inserts it into the storage containers, creates an corresponding overlay and a container. |
---|
| 391 | @param notification |
---|
| 392 | The Notification. |
---|
| 393 | @param time |
---|
| 394 | The time. |
---|
| 395 | */ |
---|
| 396 | void NotificationQueue::addNotification(Notification* notification, const std::time_t & time) |
---|
| 397 | { |
---|
| 398 | NotificationOverlayContainer* container = new NotificationOverlayContainer; |
---|
| 399 | container->overlay = new NotificationOverlay(this, notification); |
---|
| 400 | container->notification = notification; |
---|
| 401 | container->time = time; |
---|
| 402 | std::string timeString = std::ctime(&time); |
---|
| 403 | timeString.erase(timeString.length()-1); |
---|
[7163] | 404 | const std::string& addressString = multi_cast<std::string>(reinterpret_cast<unsigned long>(notification)); |
---|
[2911] | 405 | container->name = "NotificationOverlay(" + timeString + ")&" + addressString; |
---|
| 406 | |
---|
| 407 | this->containers_.insert(container); |
---|
| 408 | this->overlays_[notification] = container; |
---|
| 409 | this->addElement(container->overlay); |
---|
| 410 | this->size_= this->size_+1; |
---|
| 411 | |
---|
[6502] | 412 | container->overlay->scroll(Vector2(0.0f,(1.1f*this->getFontSize())*(this->getSize()-1))); |
---|
[2911] | 413 | } |
---|
| 414 | |
---|
| 415 | /** |
---|
| 416 | @brief |
---|
| 417 | Removes a container from the queue. |
---|
| 418 | @param container |
---|
| 419 | A pointer to the container. |
---|
| 420 | @return |
---|
| 421 | Returns true if successful. |
---|
| 422 | */ |
---|
| 423 | bool NotificationQueue::removeContainer(NotificationOverlayContainer* container) |
---|
| 424 | { |
---|
| 425 | if(this->size_ == 0) //!< You cannot remove anything if the queue is empty. |
---|
| 426 | return false; |
---|
| 427 | |
---|
[7163] | 428 | // Unregister the NotificationQueue with the NotificationManager. |
---|
| 429 | NotificationManager::getInstance().unregisterNotification(container->notification, this); |
---|
| 430 | |
---|
[2911] | 431 | this->removeElement(container->overlay); |
---|
| 432 | this->containers_.erase(container); |
---|
| 433 | this->overlays_.erase(container->notification); |
---|
[5929] | 434 | container->overlay->destroy(); |
---|
[2911] | 435 | delete container; |
---|
| 436 | this->size_= this->size_-1; |
---|
| 437 | |
---|
| 438 | return true; |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | /** |
---|
| 442 | @brief |
---|
| 443 | Clears the queue by removing all containers. |
---|
| 444 | */ |
---|
| 445 | void NotificationQueue::clear(void) |
---|
| 446 | { |
---|
| 447 | std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it = this->containers_.begin(); |
---|
| 448 | while(it != this->containers_.end()) |
---|
| 449 | { |
---|
| 450 | this->removeContainer(*it); |
---|
[7163] | 451 | it = this->containers_.begin(); |
---|
[2911] | 452 | } |
---|
| 453 | } |
---|
| 454 | |
---|
[2280] | 455 | } |
---|