- Timestamp:
- May 28, 2011, 5:10:01 PM (13 years ago)
- Location:
- code/branches/presentation
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation
- Property svn:mergeinfo changed
/code/branches/tutoriallevel (added) merged: 7828-7831,7894,8370 /code/branches/tutoriallevel2 (added) merged: 8371,8374,8376-8378,8444-8451 /code/branches/tutoriallevel3 (added) merged: 8453,8634,8636
- Property svn:mergeinfo changed
-
code/branches/presentation/src/modules/notifications/NotificationQueue.cc
r8079 r8637 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), registered_(false) 55 { 56 RegisterObject(NotificationQueue); 57 58 this->initialize(); 59 } 60 61 // TODO move to docu. 62 /** 63 @brief 64 Constructor. Registers and initializes the object. 65 @param creator 66 The creator of the NotificationQueue 54 67 @param name 55 68 The name of the new NotificationQueue. It needs to be unique … … 62 75 The time during which a Notification is (at most) displayed. 63 76 */ 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. 77 78 /** 79 @brief 80 Destructor. 81 */ 82 NotificationQueue::~NotificationQueue() 83 { 84 this->targets_.clear(); 85 86 if(this->isRegistered()) // If the NotificationQueue is registered. 87 { 88 this->clear(true); 89 90 // Unregister with the NotificationManager. 91 NotificationManager::getInstance().unregisterQueue(this); 92 } 93 } 94 95 /** 96 @brief 97 Initializes the NotificationQueue. 98 */ 99 void NotificationQueue::initialize(void) 100 { 71 101 this->size_ = 0; 72 102 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 103 this->maxSize_ = NotificationQueue::DEFAULT_SIZE; 104 this->displayTime_ = NotificationQueue::DEFAULT_DISPLAY_TIME; 105 106 this->creationTime_ = std::time(0); 107 } 108 109 /** 110 @brief 111 Creates the NotificationQueue. 112 */ 113 void NotificationQueue::create(void) 114 { 80 115 // Register the NotificationQueue with the NotificationManager. 81 116 bool queueRegistered = NotificationManager::getInstance().registerQueue(this); … … 88 123 } 89 124 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 125 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 126 } 153 127 … … 161 135 { 162 136 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.137 if(this->displayTime_ != INF && 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. 138 { 139 this->timeLimit_.time = std::time(0)-this->displayTime_; // Container containing the current time. 166 140 167 141 std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator it = this->ordering_.begin(); … … 177 151 } 178 152 153 void NotificationQueue::XMLPort(Element& xmlelement, XMLPort::Mode mode) 154 { 155 SUPER(NotificationQueue, XMLPort, xmlelement, mode); 156 157 XMLPortParam(NotificationQueue, "targets", setTargets, getTargets, xmlelement, mode).defaultValues(NotificationListener::ALL); 158 XMLPortParam(NotificationQueue, "size", setMaxSize, getMaxSize, xmlelement, mode); 159 XMLPortParam(NotificationQueue, "displayTime", setDisplayTime, getDisplayTime, xmlelement, mode); 160 161 this->create(); 162 } 163 179 164 /** 180 165 @brief 181 166 Updates the NotificationQueue. 182 167 Updates by clearing the queue and requesting all relevant Notifications from the NotificationManager and inserting them into the queue. 168 This is called by the NotificationManager when the Notifications have changed so much, that the NotificationQueue may have to re-initialize his operations. 183 169 */ 184 170 void NotificationQueue::update(void) … … 188 174 std::multimap<std::time_t, Notification*>* notifications = new std::multimap<std::time_t, Notification*>; 189 175 // Get the Notifications sent in the interval from now to now minus the display time. 190 NotificationManager::getInstance().getNotifications(this, notifications, this->displayTime_); 176 if(this->displayTime_ == INF) 177 NotificationManager::getInstance().getNewestNotifications(this, notifications, this->getMaxSize()); 178 else 179 NotificationManager::getInstance().getNotifications(this, notifications, this->displayTime_); 191 180 192 181 if(!notifications->empty()) 193 182 { 194 // Add all Notifications .183 // Add all Notifications that have been created after this NotificationQueue was created. 195 184 for(std::multimap<std::time_t, Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) 196 this->push(it->second, it->first); 185 { 186 if(it->first >= this->creationTime_) 187 this->push(it->second, it->first); 188 } 197 189 } 198 190 … … 246 238 this->notifications_.insert(this->notifications_.begin(), container); 247 239 248 // Push the Notification to the GUI. 249 if(GameMode::showsGraphics()) 250 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.pushNotification(\"" + this->getName() + "\", \"" + notification->getMessage() + "\")"); 240 // Inform that a Notification was pushed. 241 this->notificationPushed(notification); 251 242 252 243 COUT(5) << "Notification \"" << notification->getMessage() << "\" pushed to NotificationQueue '" << this->getName() << "'" << endl; 244 COUT(3) << "NotificationQueue \"" << this->getName() << "\": " << notification->getMessage() << endl; 253 245 } 254 246 … … 279 271 delete container; 280 272 281 // Pops the Notification from the GUI. 282 if(GameMode::showsGraphics()) 283 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.popNotification(\"" + this->getName() + "\")"); 273 // Inform that a Notification was popped. 274 this->notificationPopped(); 284 275 } 285 276 … … 305 296 delete *containerIterator; 306 297 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) + ")");298 // TODO: index automatically cast? 299 // Inform that a Notification was removed. 300 this->notificationRemoved(index); 310 301 } 311 302 … … 314 305 Clears the NotificationQueue by removing all NotificationContainers. 315 306 @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.307 If this is set to true the GUI is not informed of the clearing of the NotificationQueue. This is needed only internally. 317 308 */ 318 309 void NotificationQueue::clear(bool noGraphics) … … 326 317 this->notifications_.clear(); 327 318 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 319 } 333 320 … … 354 341 return; 355 342 343 if(size == 0) 344 { 345 COUT(2) << "Trying to set maximal size of NotificationQueue '" << this->getName() << "' to 0. Ignoring..." << endl; 346 return; 347 } 348 356 349 this->maxSize_ = size; 357 350 358 if(this-> registered_)351 if(this->isRegistered()) 359 352 this->update(); 360 353 } … … 364 357 Sets the maximum number of seconds a Notification is displayed. 365 358 @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) 359 The number of seconds a Notification is displayed. 360 */ 361 void NotificationQueue::setDisplayTime(int time) 371 362 { 372 363 if(this->displayTime_ == time) 373 364 return; 374 365 366 if(time != NotificationQueue::INF && time <= 0) 367 { 368 COUT(2) << "Trying to set display time of NotificationQueue '" << this->getName() << "' to non-positive value. Ignoring..." << endl; 369 } 370 375 371 this->displayTime_ = time; 376 372 377 if(this-> registered_)373 if(this->isRegistered()) 378 374 this->update(); 379 375 } … … 381 377 /** 382 378 @brief 383 Produces all targets of the NotificationQueue concat inated as string, with commas (',') as seperators.379 Produces all targets of the NotificationQueue concatenated as string, with commas (',') as separators. 384 380 @return 385 381 Returns the targets as a string. … … 407 403 The targets are the senders whose Notifications are displayed in this queue. 408 404 @param targets 409 Accepts a string of targets, each sep erated by commas (','), spaces are ignored.405 Accepts a string of targets, each separated by commas (','), spaces are ignored. 410 406 */ 411 407 void NotificationQueue::setTargets(const std::string & targets) … … 417 413 this->targets_.insert(string[i]); 418 414 419 if(this->registered_) 420 { 421 NotificationManager::getInstance().unregisterListener(this); 422 NotificationManager::getInstance().registerListener(this); 423 } 415 // TODO: Why? 416 if(this->isRegistered()) 417 { 418 NotificationManager::getInstance().unregisterQueue(this); 419 NotificationManager::getInstance().registerQueue(this); 420 } 421 } 422 423 /** 424 @brief 425 Pops all Notifications from the NotificationQueue. 426 @return 427 Returns true if successful, false if not. 428 */ 429 bool NotificationQueue::tidy(void) 430 { 431 while(this->size_ > 0) 432 this->pop(); 433 return true; 424 434 } 425 435
Note: See TracChangeset
for help on using the changeset viewer.