Changeset 2779 for code/branches/questsystem5/src/orxonox/overlays/notifications/NotificationQueue.cc
- Timestamp:
- Mar 12, 2009, 5:13:34 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/questsystem5/src/orxonox/overlays/notifications/NotificationQueue.cc
r2662 r2779 30 30 #include "NotificationQueue.h" 31 31 32 #include <OgreOverlayManager.h> 33 #include <OgreTextAreaOverlayElement.h> 34 #include <list> 35 32 36 #include "core/CoreIncludes.h" 33 37 #include "core/XMLPort.h" 34 38 35 #include "NotificationManager.h" 39 #include "Notification.h" 40 #include "NotificationOverlay.h" 36 41 37 42 namespace orxonox 38 43 { 39 NotificationQueue* NotificationQueue::queue_s = 0; 40 44 41 45 CreateFactory(NotificationQueue); 42 43 NotificationQueue::NotificationQueue(BaseObject* creator) : OverlayText(creator) 46 47 const std::string NotificationQueue::DEFAULT_FONT = "VeraMono"; 48 49 /** 50 @brief 51 Constructor. Creates and initializes the object. 52 */ 53 NotificationQueue::NotificationQueue(BaseObject* creator) : OverlayGroup(creator) 54 { 55 this->initialize(); 56 } 57 58 /** 59 @brief 60 Destructor. 61 @todo 62 I'm pretty sure that there are some thing that have to be distroyed. 63 */ 64 NotificationQueue::~NotificationQueue() 65 { 66 67 } 68 69 /** 70 @brief 71 Initializes the object. 72 Registers the object, initializes variables, sets default values and registers the queue with the NotificationManager. 73 */ 74 void NotificationQueue::initialize(void) 44 75 { 45 76 RegisterObject(NotificationQueue); 46 //TDO: Does this work? 47 if(queue_s != NULL) 48 { 49 COUT(2) << "There is now more than one NotificationQueue, this shouldn't happen, since only the first NotificationQueue will be targeted by the NotificationManager." << std::endl; 50 } 51 else 52 { 53 queue_s = this; 54 } 55 56 this->length_ = 3; 57 this->width_ = 50; 58 } 59 60 NotificationQueue::~NotificationQueue() 61 { 62 63 } 64 77 78 this->setDefaults(); 79 this->size_ = 0; 80 this->tickTime_ = 0.0; 81 82 NotificationManager::registerQueue(this); 83 } 84 85 /** 86 @brief 87 Sets the defaults. 88 */ 89 void NotificationQueue::setDefaults(void) 90 { 91 this->setMaxSize(DEFAULT_SIZE); 92 this->setNotificationLength(DEFAULT_LENGTH); 93 this->setDisplayTime(DEFAULT_DISPLAY_TIME); 94 95 this->setTargets(NotificationManager::ALL); 96 97 this->setFontSize(DEFAULT_FONT_SIZE); 98 this->setFont(DEFAULT_FONT); 99 } 100 101 /** 102 @brief 103 Method for creating a NotificationQueue object through XML. 104 */ 65 105 void NotificationQueue::XMLPort(Element& xmlElement, XMLPort::Mode mode) 66 106 { 67 107 SUPER(NotificationQueue, XMLPort, xmlElement, mode); 68 69 XMLPortParam(NotificationQueue, "length", setLength, getLength, xmlElement, mode); 70 XMLPortParam(NotificationQueue, "width", setWidth, getWidth, xmlElement, mode); 71 } 72 108 109 XMLPortParam(NotificationQueue, "maxSize", setMaxSize, getMaxSize, xmlElement, mode); 110 XMLPortParam(NotificationQueue, "notificationLength", setNotificationLength, getNotificationLength, xmlElement, mode); 111 XMLPortParam(NotificationQueue, "displayTime", setDisplayTime, getDisplayTime, xmlElement, mode); 112 XMLPortParam(NotificationQueue, "targets", setTargets, getTargets, xmlElement, mode); 113 XMLPortParam(NotificationQueue, "font", setFont, getFont, xmlElement, mode); 114 XMLPortParam(NotificationQueue, "fontSize", setFontSize, getFontSize, xmlElement, mode); 115 116 COUT(3) << "NotificationQueue created." << std::endl; 117 } 118 119 /** 120 @brief 121 Updates the queue from time to time. 122 @param dt 123 The time interval that has passed since the last tick. 124 */ 73 125 void NotificationQueue::tick(float dt) 74 126 { 75 NotificationManager::tick(dt); 76 77 update(); 78 } 79 80 bool NotificationQueue::setLength(int length) 81 { 82 if(length > 0) 83 { 84 this->length_ = length; 85 return true; 86 } 87 return false; 88 } 89 90 bool NotificationQueue::setWidth(int width) 91 { 92 if(width > 0) 93 { 94 this->width_ = width; 95 return true; 96 } 97 return false; 98 } 99 100 void NotificationQueue::setQueueText(const std::string & text) 101 { 102 this->queueText_ = text; 103 } 104 127 this->tickTime_ += dt; //!< Add the time interval that has passed to the time counter. 128 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. 129 { 130 this->timeLimit_.time = std::time(0)-this->displayTime_; //!< Container containig the current time. 131 132 std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it; 133 it = this->containers_.begin(); 134 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. 135 { 136 this->removeContainer(*it); 137 it = this->containers_.begin(); //TDO: Needed? 138 } 139 140 this->tickTime_ = 0.0; //!< Reset time counter. 141 } 142 } 143 144 /** 145 @brief 146 Updates the NotificationQueue. 147 Updates by clearing the queue and requesting all relevant Notifications from the NotificationManager and inserting the in the queue. 148 */ 105 149 void NotificationQueue::update(void) 106 150 { 107 this->text_->setCaption(queueText_); 108 } 151 this->clear(); 152 153 std::multimap<std::time_t,Notification*>* notifications = NotificationManager::getNotifications(this, this->displayTime_); 154 155 if(notifications == NULL) 156 return; 157 158 for(std::multimap<std::time_t,Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) 159 { 160 this->addNotification(it->second, it->first); 161 } 162 163 COUT(3) << "NotificationQueue updated." << std::endl; 164 } 165 166 /** 167 @brief 168 Updates the NotificationQueue by adding an new Notification. 169 @param notification 170 Pointer to the Notification. 171 @param time 172 The time the Notification was sent. 173 */ 174 void NotificationQueue::update(Notification* notification, const std::time_t & time) 175 { 176 this->addNotification(notification, time); 177 178 //TDO: Position! 179 180 std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it; 181 while(this->getSize() > this->getMaxSize()) 182 { 183 it = this->containers_.begin(); 184 this->removeContainer(*it); 185 } 186 187 COUT(3) << "NotificationQueue updated. A new Notifications has been added." << std::endl; 188 } 189 190 /** 191 @brief 192 Sets the maximum number of displayed Notifications. 193 @param size 194 The size to be set. 195 @return 196 Returns true if successful. 197 */ 198 bool NotificationQueue::setMaxSize(int size) 199 { 200 if(size < 0) 201 return false; 202 this->maxSize_ = size; 203 this->update(); 204 return true; 205 } 206 207 /** 208 @brief 209 Sets the maximum number of characters a Notification message displayed by this queue is allowed to have. 210 @param length 211 The length to be set. 212 @return 213 Returns true if successful. 214 */ 215 bool NotificationQueue::setNotificationLength(int length) 216 { 217 if(length < 0) 218 return false; 219 this->notificationLength_ = length; 220 this->update(); 221 return true; 222 } 223 224 /** 225 @brief 226 Sets the maximum number of seconds a Notification is displayed. 227 @param time 228 The number of seconds the Notifications is displayed. 229 @return 230 Returns true if successful. 231 */ 232 bool NotificationQueue::setDisplayTime(int time) 233 { 234 if(time < 0) 235 return false; 236 this->displayTime_ = time; 237 this->update(); 238 return true; 239 } 240 241 /** 242 @brief 243 Returns all targets concatinated as string, with kommas (',') as seperators. 244 @return 245 Returns all targets concatinated as string, with kommas (',') as seperators. 246 @todo 247 Where is the string deleted? 248 */ 249 const std::string & NotificationQueue::getTargets() const 250 { 251 std::string* pTemp = new std::string(""); 252 bool first = true; 253 for(std::set<std::string>::iterator it = this->targets_.begin(); it != this->targets_.end(); it++) //!< Iterate through the set of targets. 254 { 255 if(!first) 256 { 257 *pTemp += ","; 258 } 259 else 260 { 261 first = false; 262 } 263 *pTemp += *it; 264 } 265 266 return *pTemp; 267 } 268 269 /** 270 @brief 271 Sets the targets of the queue. 272 The targets are the senders whose Notifications are displayed in this queue. 273 @param targets 274 Accepts a string of targets, each seperated by commas (','), spaces are ignored. 275 @return 276 Returns true if successful. 277 */ 278 bool NotificationQueue::setTargets(const std::string & targets) 279 { 280 std::string* pTemp; 281 unsigned int index = 0; 282 while( index < targets.size() ) //!< Go through the string, character by character until the end is reached. 283 { 284 pTemp = new std::string(""); 285 while(targets[index] != ',' && targets[index] != ' ' && index < targets.size()) 286 { 287 *pTemp += targets[index]; 288 index++; 289 } 290 this->targets_.insert(*pTemp); 291 } 292 293 return true; 294 } 295 296 /** 297 @brief 298 Sets the font size. 299 @param size 300 The font size. 301 @return 302 Returns true if successful. 303 */ 304 bool NotificationQueue::setFontSize(float size) 305 { 306 if(size <= 0) 307 return false; 308 this->fontSize_ = size; 309 for (std::map<Notification*, NotificationOverlayContainer*>::iterator it = this->overlays_.begin(); it != this->overlays_.end(); ++it) //!< Set the font size for each overlay. 310 { 311 it->second->overlay->setFontSize(size); 312 } 313 return true; 314 } 315 316 /** 317 @brief 318 Sets the font. 319 @param font 320 The font. 321 @return 322 Returns true if successful. 323 */ 324 bool NotificationQueue::setFont(const std::string & font) 325 { 326 this->font_ = font; 327 for (std::map<Notification*, NotificationOverlayContainer*>::iterator it = this->overlays_.begin(); it != this->overlays_.end(); ++it) //!< Set the font for each overlay. 328 { 329 it->second->overlay->setFont(font); 330 } 331 return true; 332 } 333 334 /** 335 @brief 336 Adds a Notification, to the queue. 337 It inserts it into the storage containers, creates an corresponding overlay and a container. 338 @param notification 339 The Notification. 340 @param time 341 The time. 342 */ 343 void NotificationQueue::addNotification(Notification* notification, const std::time_t & time) 344 { 345 NotificationOverlayContainer* container = new NotificationOverlayContainer; 346 container->overlay = new NotificationOverlay(this, notification); 347 container->notification = notification; 348 container->time = time; 349 std::string timeString = std::ctime(&time); 350 timeString.erase(timeString.length()-1); 351 char buffer[64]; //TDO: Very un-nice. 352 std::sprintf(buffer,"%x",(unsigned long)notification); //TDO: Use other conversion to avoid 64bit problems. 353 std::string addressString = buffer; 354 container->name = "NotificationOverlay(" + timeString + ")&" + addressString; 355 356 this->containers_.insert(container); 357 this->overlays_[notification] = container; 358 this->insertElement(container->overlay, container->name); 359 this->size_= this->size_+1; 360 } 361 362 /** 363 @brief 364 Removes a container from the queue. 365 @param container 366 A pointer to the container. 367 @return 368 Returns true if successful. 369 */ 370 bool NotificationQueue::removeContainer(NotificationOverlayContainer* container) 371 { 372 if(this->size_ == 0) //!< You cannot remove anything if the queue is empty. 373 return false; 374 375 this->removeElement(container->name); 376 this->containers_.erase(container); 377 this->overlays_.erase(container->notification); 378 delete container->overlay; 379 delete container; 380 this->size_= this->size_-1; 381 382 return true; 383 } 384 385 /** 386 @brief 387 Clears the queue by removing all containers. 388 */ 389 void NotificationQueue::clear(void) 390 { 391 std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it = this->containers_.begin(); 392 while(it != this->containers_.end()) 393 { 394 this->removeContainer(*it); 395 it = this->containers_.begin(); //TDO: Needed? 396 } 397 } 398 109 399 }
Note: See TracChangeset
for help on using the changeset viewer.