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