- Timestamp:
- Sep 3, 2010, 3:55:32 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/notifications/src/modules/notifications/NotificationQueue.cc
r7163 r7338 37 37 #include "core/CoreIncludes.h" 38 38 #include "core/XMLPort.h" 39 #include "NotificationOverlay.h" 40 #include "NotificationManager.h" 39 #include "Notification.h" 40 #include "core/GUIManager.h" 41 #include "core/LuaState.h" 42 #include <algorithm> 41 43 42 44 namespace orxonox 43 45 { 44 45 CreateFactory(NotificationQueue);46 46 47 47 const std::string NotificationQueue::DEFAULT_FONT("VeraMono"); … … 53 53 Constructor. Creates and initializes the object. 54 54 */ 55 NotificationQueue::NotificationQueue( BaseObject* creator) : OverlayGroup(creator)55 NotificationQueue::NotificationQueue(const std::string& name, const std::string& senders, unsigned int size, const Vector2& position, unsigned int length, unsigned int displayTime) 56 56 { 57 57 this->registered_ = false; 58 58 59 RegisterObject(NotificationQueue); 59 RegisterRootObject(NotificationQueue); 60 60 61 this->initialize(); 62 63 this->setTargets(senders); 64 this->name_ = name; 65 this->maxSize_ = size; 66 this->position_ = position; 67 this->notificationLength_ = length; 68 this->setDisplayTime(displayTime); 69 70 this->create(); 71 72 NotificationManager::getInstance().registerListener(this); 73 this->registered_ = true; 74 75 COUT(3) << "NotificationQueue '" << this->getName() << "' created." << std::endl; 61 76 } 62 77 … … 83 98 this->size_ = 0; 84 99 this->tickTime_ = 0.0; 85 86 NotificationManager::getInstance().registerListener(this); 87 this->registered_ = true; 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 */ 111 void NotificationQueue::XMLPort(Element& xmlElement, XMLPort::Mode mode) 112 { 113 SUPER(NotificationQueue, XMLPort, xmlElement, mode); 114 115 this->setDefaults(); 116 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); 124 125 COUT(3) << "NotificationQueue '" << this->getName() << "' created." << std::endl; 100 } 101 102 /** 103 //TODO: Document. 104 */ 105 void NotificationQueue::create(void) 106 { 107 //TODO: Also transfer font and fontsize. 108 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.createQueue(\"" + this->getName() + "\", " + multi_cast<std::string>(this->getMaxSize()) + ")"); 109 this->positionChanged(); 126 110 } 127 111 … … 139 123 this->timeLimit_.time = std::time(0)-this->displayTime_; //!< Container containig the current time. 140 124 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. 125 std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator it = this->ordering_.begin(); 126 while(it != this->ordering_.upper_bound(&this->timeLimit_)) //!< Iterate through all elements whose creation time is smaller than the current time minus the display time. 144 127 { 145 this->removeContainer(*it);146 this->scroll(Vector2(0.0f,-(1.1f*this->getFontSize())));147 it = this->containers_.begin(); //TODO: Needed?128 NotificationContainer* temp = *it; 129 it++; 130 this->remove(temp); 148 131 } 149 132 150 this->tickTime_ = 0.0f; //!< Reset time counter.133 this->tickTime_ = this->tickTime_ - (int)this->tickTime_; //!< Reset time counter. 151 134 } 152 135 } … … 161 144 this->clear(); 162 145 163 std::multimap<std::time_t, Notification*>* notifications = new std::multimap<std::time_t,Notification*>;146 std::multimap<std::time_t, Notification*>* notifications = new std::multimap<std::time_t, Notification*>; 164 147 if(!NotificationManager::getInstance().getNotifications(this, notifications, this->displayTime_)) //!< Get the Notifications sent in the interval form now to minus the display time. 165 148 { … … 171 154 return; 172 155 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 } 156 for(std::multimap<std::time_t, Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) //!> Add all Notifications. 157 this->push(it->second, it->first); 177 158 178 159 delete notifications; … … 191 172 void NotificationQueue::update(Notification* notification, const std::time_t & time) 192 173 { 193 this->addNotification(notification, time); 194 195 std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it; 196 while(this->getSize() > this->getMaxSize()) 197 { 198 it = this->containers_.begin(); 199 this->removeContainer(*it); 200 this->scroll(Vector2(0.0f,-(1.1f*this->getFontSize()))); 201 } 202 203 COUT(4) << "NotificationQueue '" << this->getName() << "' updated. A new Notifications has been added." << std::endl; 174 this->push(notification, time); 175 176 COUT(4) << "NotificationQueue '" << this->getName() << "' updated. A new Notification has been added." << std::endl; 177 } 178 179 /** 180 @brief 181 Sets the name of the NotificationQueue. 182 @param name 183 The name to be set. 184 @return 185 returns true if successful. 186 */ 187 bool NotificationQueue::setName(const std::string& name) 188 { 189 //TODO: Test uniqueness of name. 190 this->name_ = name; 191 return true; 204 192 } 205 193 … … 212 200 Returns true if successful. 213 201 */ 214 bool NotificationQueue::setMaxSize(int size) 215 { 216 if(size < 0) 217 return false; 202 void NotificationQueue::setMaxSize(unsigned int size) 203 { 218 204 this->maxSize_ = size; 219 205 this->update(); 220 return true;221 206 } 222 207 … … 229 214 Returns true if successful. 230 215 */ 231 bool NotificationQueue::setNotificationLength(int length) 232 { 233 if(length < 0) 234 return false; 216 void NotificationQueue::setNotificationLength(unsigned int length) 217 { 235 218 this->notificationLength_ = length; 236 219 this->update(); 237 return true;238 220 } 239 221 … … 246 228 Returns true if successful. 247 229 */ 248 bool NotificationQueue::setDisplayTime(int time) 249 { 250 if(time < 0) 251 return false; 230 void NotificationQueue::setDisplayTime(unsigned int time) 231 { 252 232 this->displayTime_ = time; 253 233 this->update(); 254 return true;255 234 } 256 235 … … 330 309 if(size <= 0) 331 310 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. 334 { 335 it->second->overlay->setFontSize(size); 336 } 311 337 312 return true; 338 313 } … … 348 323 bool NotificationQueue::setFont(const std::string & font) 349 324 { 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 } 325 355 326 return true; 356 327 } … … 358 329 /** 359 330 @brief 360 Scrolls the NotificationQueue, meaning all NotificationOverlays are moved the input vector.361 @param pos362 The vector the NotificationQueue is scrolled.363 */364 void NotificationQueue::scroll(const Vector2 pos)365 {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 }370 }371 372 /**373 @brief374 331 Aligns all the Notifications to the position of the NotificationQueue. 375 332 */ 376 333 void NotificationQueue::positionChanged(void) 377 334 { 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()); 382 (*it)->overlay->scroll(Vector2(0.0f,(1.1f*this->getFontSize())*counter)); 383 counter++; 384 } 335 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.changePosition(\"" + this->getName() + "\", " + multi_cast<std::string>(this->getPosition().x) + ", " + multi_cast<std::string>(this->getPosition().y) + ")"); 385 336 } 386 337 … … 388 339 @brief 389 340 Adds a Notification, to the queue. 390 It inserts it into the storage containers, creates a n corresponding overlay and a container.341 It inserts it into the storage containers, creates a corresponding container and pushes the Notification message to the GUI. 391 342 @param notification 392 343 The Notification. … … 394 345 The time. 395 346 */ 396 void NotificationQueue::addNotification(Notification* notification, const std::time_t & time) 397 { 398 NotificationOverlayContainer* container = new NotificationOverlayContainer; 399 container->overlay = new NotificationOverlay(this, notification); 347 void NotificationQueue::push(Notification* notification, const std::time_t & time) 348 { 349 NotificationContainer* container = new NotificationContainer; 400 350 container->notification = notification; 401 351 container->time = time; 402 std::string timeString = std::ctime(&time); 403 timeString.erase(timeString.length()-1); 404 const std::string& addressString = multi_cast<std::string>(reinterpret_cast<unsigned long>(notification)); 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 412 container->overlay->scroll(Vector2(0.0f,(1.1f*this->getFontSize())*(this->getSize()-1))); 413 } 414 415 /** 416 @brief 417 Removes a container from the queue. 352 353 // If the maximum size of the NotificationQueue has been reached the last (least recently added) Notification is removed. 354 if(this->getSize() >= this->getMaxSize()) 355 this->pop(); 356 357 this->size_++; 358 359 this->ordering_.insert(container); 360 this->notifications_.insert(this->notifications_.begin(), container); 361 362 //TODO: Clip message if necessary. 363 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.pushNotification(\"" + this->getName() + "\", \"" + notification->getMessage() + "\")"); 364 } 365 366 /** 367 @brief 368 Removes the least recently added Notification form the NotificationQueue. 369 */ 370 void NotificationQueue::pop(void) 371 { 372 NotificationContainer* container = this->notifications_.back(); 373 this->ordering_.erase(container); 374 this->notifications_.pop_back(); 375 this->size_--; 376 delete container; 377 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.popNotification(\"" + this->getName() + "\")"); 378 } 379 380 /** 381 @brief 382 Removes the Notification that is stored in the input container. 418 383 @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 428 // Unregister the NotificationQueue with the NotificationManager. 429 NotificationManager::getInstance().unregisterNotification(container->notification, this); 430 431 this->removeElement(container->overlay); 432 this->containers_.erase(container); 433 this->overlays_.erase(container->notification); 434 container->overlay->destroy(); 384 The NotificationContainer with the Notification to be removed. 385 */ 386 void NotificationQueue::remove(NotificationContainer* container) 387 { 388 std::vector<NotificationContainer*>::iterator it = std::find(this->notifications_.begin(), this->notifications_.end(), container); 389 std::vector<NotificationContainer*>::difference_type index = it - this->notifications_.begin (); 390 this->ordering_.erase(container); 391 this->notifications_.erase(it); 392 this->size_--; 435 393 delete container; 436 this->size_= this->size_-1; 437 438 return true; 394 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeNotification(\"" + this->getName() + "\", " + multi_cast<std::string>(index) + ")"); 439 395 } 440 396 … … 445 401 void NotificationQueue::clear(void) 446 402 { 447 std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it = this->containers_.begin(); 448 while(it != this->containers_.end()) 449 { 450 this->removeContainer(*it); 451 it = this->containers_.begin(); 452 } 403 this->ordering_.clear(); 404 for(std::vector<NotificationContainer*>::iterator it = this->notifications_.begin(); it != this->notifications_.end(); it++) 405 { 406 delete *it; 407 } 408 this->notifications_.clear(); 409 this->size_ = 0; 410 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.clearQueue(\"" + this->getName() + "\")"); 453 411 } 454 412
Note: See TracChangeset
for help on using the changeset viewer.