- Timestamp:
- Jun 14, 2011, 8:53:28 PM (13 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/modules/notifications/NotificationQueue.cc
r8079 r8706 38 38 39 39 #include "core/CoreIncludes.h" 40 #include "core/GameMode.h" 41 #include "core/GUIManager.h" 42 #include "core/LuaState.h" 43 #include "util/Convert.h" 40 #include "core/XMLPort.h" 44 41 #include "util/SubString.h" 45 46 #include "Notification.h"47 42 48 43 namespace orxonox 49 44 { 50 45 51 /** 52 @brief 53 Constructor. Creates and initializes the object. 46 CreateFactory(NotificationQueue); 47 48 /** 49 @brief 50 Default constructor. Registers and initializes the object. 51 @param creator 52 The creator of the NotificationQueue. 53 */ 54 NotificationQueue::NotificationQueue(BaseObject* creator) : BaseObject(creator), Synchronisable(creator), registered_(false) 55 { 56 RegisterObject(NotificationQueue); 57 58 this->size_ = 0; 59 this->tickTime_ = 0.0f; 60 this->maxSize_ = NotificationQueue::DEFAULT_SIZE; 61 this->displayTime_ = NotificationQueue::DEFAULT_DISPLAY_TIME; 62 63 this->creationTime_ = std::time(0); 64 65 this->registerVariables(); 66 } 67 68 // TODO move to docu. 69 /** 70 @brief 71 Constructor. Registers and initializes the object. 72 @param creator 73 The creator of the NotificationQueue 54 74 @param name 55 75 The name of the new NotificationQueue. It needs to be unique … … 62 82 The time during which a Notification is (at most) displayed. 63 83 */ 64 NotificationQueue::NotificationQueue(const std::string& name, const std::string& senders, unsigned int size, unsigned int displayTime) 65 { 66 this->registered_ = false; 67 68 RegisterRootObject(NotificationQueue); 69 70 // Initialize. 71 this->size_ = 0; 72 this->tickTime_ = 0.0f; 73 74 // Sets the input values. 75 this->setTargets(senders); 76 this->name_ = name; 77 this->maxSize_ = size; 78 this->setDisplayTime(displayTime); 79 84 85 /** 86 @brief 87 Destructor. 88 */ 89 NotificationQueue::~NotificationQueue() 90 { 91 this->targets_.clear(); 92 93 if(this->isRegistered()) // If the NotificationQueue is registered. 94 { 95 this->clear(true); 96 97 // Unregister with the NotificationManager. 98 NotificationManager::getInstance().unregisterQueue(this); 99 } 100 } 101 102 /** 103 @brief 104 Is called when the name of the NotificationQueue has changed. 105 Clears and re-creates the NotificationQueue. 106 */ 107 void NotificationQueue::changedName(void) 108 { 109 SUPER(NotificationQueue, changedName); 110 111 if(this->isRegistered()) 112 this->clear(); 113 114 this->create(); 115 116 this->targetsChanged(); 117 this->maxSizeChanged(); 118 this->displayTimeChanged(); 119 } 120 121 /** 122 @brief 123 Creates the NotificationQueue. 124 */ 125 void NotificationQueue::create(void) 126 { 80 127 // Register the NotificationQueue with the NotificationManager. 81 128 bool queueRegistered = NotificationManager::getInstance().registerQueue(this); … … 88 135 } 89 136 90 this->create(); // Creates the NotificationQueue in lua.91 92 // Register the NotificationQueue as NotificationListener with the NotificationManager.93 bool listenerRegistered = NotificationManager::getInstance().registerListener(this);94 if(!listenerRegistered) // If the registration has failed.95 {96 this->registered_ = false;97 // Remove the NotificationQueue in lua.98 if(GameMode::showsGraphics())99 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeQueue(\"" + this->getName() + "\")");100 NotificationManager::getInstance().unregisterQueue(this);101 COUT(1) << "Error: NotificationQueue '" << this->getName() << "' could not be registered." << std::endl;102 return;103 }104 105 137 COUT(3) << "NotificationQueue '" << this->getName() << "' created." << std::endl; 106 }107 108 /**109 @brief110 Destructor.111 */112 NotificationQueue::~NotificationQueue()113 {114 this->targets_.clear();115 116 if(this->registered_) // If the NotificationQueue is registered.117 {118 this->clear(true);119 120 // Unregister with the NotificationManager.121 NotificationManager::getInstance().unregisterListener(this);122 NotificationManager::getInstance().unregisterQueue(this);123 }124 }125 126 /**127 @brief128 Destroys the NotificationQueue.129 Used in lua and NotificationManager.130 @param noGraphics131 If this is set to true (false is default), then the queue is not removed in lua. This is used to destroy the queue, after the GUIManager has been destroyed.132 */133 void NotificationQueue::destroy(bool noGraphics)134 {135 // Remove the NotificationQueue in lua.136 if(GameMode::showsGraphics() && !noGraphics)137 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeQueue(\"" + this->getName() + "\")");138 139 COUT(3) << "NotificationQueue '" << this->getName() << "' destroyed." << std::endl;140 141 this->OrxonoxClass::destroy();142 }143 144 /**145 @brief146 Creates the NotificationQueue in lua.147 */148 void NotificationQueue::create(void)149 {150 if(GameMode::showsGraphics())151 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.createQueue(\"" + this->getName() + "\", " + multi_cast<std::string>(this->getMaxSize()) + ")");152 138 } 153 139 … … 161 147 { 162 148 this->tickTime_ += dt; // Add the time interval that has passed to the time counter. 163 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.164 { 165 this->timeLimit_.time = std::time(0)-this->displayTime_; // Container containi g the current time.149 if(this->displayTime_ != INF && this->tickTime_ >= 1.0) // If the time counter is greater than 1 s all Notifications that have expired are removed, if it is smaller we wait to the next tick. 150 { 151 this->timeLimit_.time = std::time(0)-this->displayTime_; // Container containing the current time. 166 152 167 153 std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator it = this->ordering_.begin(); … … 177 163 } 178 164 165 void NotificationQueue::XMLPort(Element& xmlelement, XMLPort::Mode mode) 166 { 167 SUPER(NotificationQueue, XMLPort, xmlelement, mode); 168 169 XMLPortParam(NotificationQueue, "targets", setTargets, getTargets, xmlelement, mode).defaultValues(NotificationListener::ALL); 170 XMLPortParam(NotificationQueue, "size", setMaxSize, getMaxSize, xmlelement, mode); 171 XMLPortParam(NotificationQueue, "displayTime", setDisplayTime, getDisplayTime, xmlelement, mode); 172 } 173 174 175 /** 176 @brief 177 Registers Variables to be Synchronised. 178 Registers Variables which have to be synchronised to the network system. 179 */ 180 void NotificationQueue::registerVariables() 181 { 182 registerVariable( this->name_, VariableDirection::ToClient, new NetworkCallback<NotificationQueue>(this, &NotificationQueue::changedName)); 183 registerVariable( this->maxSize_, VariableDirection::ToClient, new NetworkCallback<NotificationQueue>(this, &NotificationQueue::maxSizeChanged)); 184 registerVariable( this->targets_, VariableDirection::ToClient, new NetworkCallback<NotificationQueue>(this, &NotificationQueue::targetsChanged)); 185 registerVariable( this->displayTime_, VariableDirection::ToClient, new NetworkCallback<NotificationQueue>(this, &NotificationQueue::displayTimeChanged)); 186 } 187 179 188 /** 180 189 @brief 181 190 Updates the NotificationQueue. 182 191 Updates by clearing the queue and requesting all relevant Notifications from the NotificationManager and inserting them into the queue. 192 This is called by the NotificationManager when the Notifications have changed so much, that the NotificationQueue may have to re-initialize his operations. 183 193 */ 184 194 void NotificationQueue::update(void) … … 188 198 std::multimap<std::time_t, Notification*>* notifications = new std::multimap<std::time_t, Notification*>; 189 199 // Get the Notifications sent in the interval from now to now minus the display time. 190 NotificationManager::getInstance().getNotifications(this, notifications, this->displayTime_); 200 if(this->displayTime_ == INF) 201 NotificationManager::getInstance().getNewestNotifications(this, notifications, this->getMaxSize()); 202 else 203 NotificationManager::getInstance().getNotifications(this, notifications, this->displayTime_); 191 204 192 205 if(!notifications->empty()) 193 206 { 194 // Add all Notifications .207 // Add all Notifications that have been created after this NotificationQueue was created. 195 208 for(std::multimap<std::time_t, Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) 196 this->push(it->second, it->first); 209 { 210 if(it->first >= this->creationTime_) 211 this->push(it->second, it->first); 212 } 197 213 } 198 214 … … 222 238 @brief 223 239 Adds (pushes) a Notification to the NotificationQueue. 224 It inserts it into the storage containers, creates a corresponding container and pushes the Notification message to the GUI.240 It inserts it into the storage containers, creates a corresponding container and pushes the notification message to the GUI. 225 241 @param notification 226 242 The Notification to be pushed. … … 246 262 this->notifications_.insert(this->notifications_.begin(), container); 247 263 248 // Push the Notification to the GUI. 249 if(GameMode::showsGraphics()) 250 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.pushNotification(\"" + this->getName() + "\", \"" + notification->getMessage() + "\")"); 264 // Inform that a Notification was pushed. 265 this->notificationPushed(notification); 251 266 252 267 COUT(5) << "Notification \"" << notification->getMessage() << "\" pushed to NotificationQueue '" << this->getName() << "'" << endl; 268 COUT(3) << "NotificationQueue \"" << this->getName() << "\": " << notification->getMessage() << endl; 253 269 } 254 270 … … 279 295 delete container; 280 296 281 // Pops the Notification from the GUI. 282 if(GameMode::showsGraphics()) 283 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.popNotification(\"" + this->getName() + "\")"); 297 // Inform that a Notification was popped. 298 this->notificationPopped(); 284 299 } 285 300 … … 305 320 delete *containerIterator; 306 321 307 // Removes the Notification from the GUI.308 if(GameMode::showsGraphics())309 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeNotification(\"" + this->getName() + "\", " + multi_cast<std::string>(index) + ")");322 // TODO: index automatically cast? 323 // Inform that a Notification was removed. 324 this->notificationRemoved(index); 310 325 } 311 326 … … 314 329 Clears the NotificationQueue by removing all NotificationContainers. 315 330 @param noGraphics 316 If this is eset to true the GUI is not informed of the clearing of the NotificationQueue. This is needed only internally.331 If this is set to true the GUI is not informed of the clearing of the NotificationQueue. This is needed only internally. 317 332 */ 318 333 void NotificationQueue::clear(bool noGraphics) … … 326 341 this->notifications_.clear(); 327 342 this->size_ = 0; 328 329 // Clear the NotificationQueue in the GUI.330 if(GameMode::showsGraphics() && !noGraphics)331 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.clearQueue(\"" + this->getName() + "\")");332 343 } 333 344 … … 354 365 return; 355 366 367 if(size == 0) 368 { 369 COUT(2) << "Trying to set maximal size of NotificationQueue '" << this->getName() << "' to 0. Ignoring..." << endl; 370 return; 371 } 372 356 373 this->maxSize_ = size; 357 358 if(this->registered_) 374 this->maxSizeChanged(); 375 } 376 377 /** 378 @brief 379 Is called when the maximum number of displayed Notifications has changed. 380 */ 381 void NotificationQueue::maxSizeChanged(void) 382 { 383 if(this->isRegistered()) 359 384 this->update(); 360 385 } … … 364 389 Sets the maximum number of seconds a Notification is displayed. 365 390 @param time 366 The number of seconds the Notifications is displayed. 367 @return 368 Returns true if successful. 369 */ 370 void NotificationQueue::setDisplayTime(unsigned int time) 391 The number of seconds a Notification is displayed. 392 */ 393 void NotificationQueue::setDisplayTime(int time) 371 394 { 372 395 if(this->displayTime_ == time) 373 396 return; 374 397 398 if(time != NotificationQueue::INF && time <= 0) 399 { 400 COUT(2) << "Trying to set display time of NotificationQueue '" << this->getName() << "' to non-positive value. Ignoring..." << endl; 401 } 402 375 403 this->displayTime_ = time; 376 377 if(this->registered_) 404 this->displayTimeChanged(); 405 } 406 407 /** 408 @brief 409 Is called when the maximum number of seconds a Notification is displayed has changed. 410 */ 411 void NotificationQueue::displayTimeChanged(void) 412 { 413 if(this->isRegistered()) 378 414 this->update(); 379 415 } … … 381 417 /** 382 418 @brief 383 Produces all targets of the NotificationQueue concat inated as string, with commas (',') as seperators.419 Produces all targets of the NotificationQueue concatenated as string, with commas (',') as separators. 384 420 @return 385 421 Returns the targets as a string. … … 407 443 The targets are the senders whose Notifications are displayed in this queue. 408 444 @param targets 409 Accepts a string of targets, each sep erated by commas (','), spaces are ignored.445 Accepts a string of targets, each separated by commas (','), spaces are ignored. 410 446 */ 411 447 void NotificationQueue::setTargets(const std::string & targets) … … 417 453 this->targets_.insert(string[i]); 418 454 419 if(this->registered_) 420 { 421 NotificationManager::getInstance().unregisterListener(this); 422 NotificationManager::getInstance().registerListener(this); 423 } 455 this->targetsChanged(); 456 } 457 458 /** 459 @brief 460 Is called when the NotificationQueue's targets have changed. 461 */ 462 void NotificationQueue::targetsChanged(void) 463 { 464 // TODO: Why? 465 if(this->isRegistered()) 466 { 467 NotificationManager::getInstance().unregisterQueue(this); 468 NotificationManager::getInstance().registerQueue(this); 469 } 470 } 471 472 /** 473 @brief 474 Pops all Notifications from the NotificationQueue. 475 @return 476 Returns true if successful, false if not. 477 */ 478 bool NotificationQueue::tidy(void) 479 { 480 while(this->size_ > 0) 481 this->pop(); 482 return true; 424 483 } 425 484
Note: See TracChangeset
for help on using the changeset viewer.