Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 3, 2010, 9:17:51 PM (14 years ago)
Author:
dafrick
Message:

Some documenting, cleaning up. Also: Now the NotificationQueue has a height corresponding to its input size.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/notifications/src/modules/notifications/NotificationQueue.cc

    r7338 r7342  
    2828
    2929/**
    30     @file
     30    @file NotificationQueue.cc
    3131    @brief Implementation of the NotificationQueue class.
    3232*/
     
    3434#include "NotificationQueue.h"
    3535
     36#include <algorithm>
     37
    3638#include "util/Convert.h"
    3739#include "core/CoreIncludes.h"
    38 #include "core/XMLPort.h"
    39 #include "Notification.h"
    4040#include "core/GUIManager.h"
    4141#include "core/LuaState.h"
    42 #include <algorithm>
     42#include "Notification.h"
    4343
    4444namespace orxonox
    4545{
    4646
    47     const std::string NotificationQueue::DEFAULT_FONT("VeraMono");
    4847    const Vector2 NotificationQueue::DEFAULT_POSITION(0.0,0.0);
    49     const float NotificationQueue::DEFAULT_FONT_SIZE = 0.025f;
    5048
    5149    /**
     
    5351        Constructor. Creates and initializes the object.
    5452    */
    55     NotificationQueue::NotificationQueue(const std::string& name, const std::string& senders, unsigned int size, const Vector2& position, unsigned int length, unsigned int displayTime)
     53    NotificationQueue::NotificationQueue(const std::string& name, const std::string& senders, unsigned int size, const Vector2& position, unsigned int displayTime)
    5654    {
    5755        this->registered_ = false;
     
    6563        this->maxSize_ = size;
    6664        this->position_ = position;
    67         this->notificationLength_ = length;
    6865        this->setDisplayTime(displayTime);
    6966
    7067        this->create();
     68        this->positionChanged();
    7169
    7270        NotificationManager::getInstance().registerListener(this);
     
    10199
    102100    /**
    103     //TODO: Document.
     101    @brief
     102        Creates the NotificationQueue in lua.
    104103    */
    105104    void NotificationQueue::create(void)
    106105    {
    107         //TODO: Also transfer font and fontsize.
    108106        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.createQueue(\"" + this->getName() + "\", " + multi_cast<std::string>(this->getMaxSize()) + ")");
    109         this->positionChanged();
    110107    }
    111108
     
    118115    void NotificationQueue::tick(float dt)
    119116    {
    120         this->tickTime_ += dt; //!< Add the time interval that has passed to the time counter.
    121         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.
    122         {
    123             this->timeLimit_.time = std::time(0)-this->displayTime_; //!< Container containig the current time.
     117        this->tickTime_ += dt; // Add the time interval that has passed to the time counter.
     118        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.
     119        {
     120            this->timeLimit_.time = std::time(0)-this->displayTime_; // Container containig the current time.
    124121
    125122            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.
     123            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.
    127124            {
    128125                NotificationContainer* temp = *it;
     
    131128            }
    132129
    133             this->tickTime_ = this->tickTime_ - (int)this->tickTime_; //!< Reset time counter.
     130            this->tickTime_ = this->tickTime_ - (int)this->tickTime_; // Reset time counter.
    134131        }
    135132    }
     
    138135    @brief
    139136        Updates the NotificationQueue.
    140         Updates by clearing the queue and requesting all relevant Notifications from the NotificationManager and inserting the in the queue.
     137        Updates by clearing the queue and requesting all relevant Notifications from the NotificationManager and inserting them into the queue.
    141138    */
    142139    void NotificationQueue::update(void)
     
    145142
    146143        std::multimap<std::time_t, Notification*>* notifications = new std::multimap<std::time_t, Notification*>;
    147         if(!NotificationManager::getInstance().getNotifications(this, notifications, this->displayTime_)) //!< Get the Notifications sent in the interval form now to minus the display time.
     144        if(!NotificationManager::getInstance().getNotifications(this, notifications, this->displayTime_)) // Get the Notifications sent in the interval form now to minus the display time.
    148145        {
    149146            COUT(1) << "NotificationQueue update failed due to undetermined cause." << std::endl;
     
    154151            return;
    155152
    156         for(std::multimap<std::time_t, Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) //!> Add all Notifications.
     153        for(std::multimap<std::time_t, Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) // Add all Notifications.
    157154            this->push(it->second, it->first);
    158155
     
    179176    /**
    180177    @brief
     178        Adds a Notification, to the queue.
     179        It inserts it into the storage containers, creates a corresponding container and pushes the Notification message to the GUI.
     180    @param notification
     181        The Notification.
     182    @param time
     183        The time.
     184    */
     185    void NotificationQueue::push(Notification* notification, const std::time_t & time)
     186    {
     187        NotificationContainer* container = new NotificationContainer;
     188        container->notification = notification;
     189        container->time = time;
     190
     191        // If the maximum size of the NotificationQueue has been reached the last (least recently added) Notification is removed.
     192        if(this->getSize() >= this->getMaxSize())
     193            this->pop();
     194
     195        this->size_++;
     196
     197        this->ordering_.insert(container);
     198        this->notifications_.insert(this->notifications_.begin(), container);
     199
     200        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.pushNotification(\"" + this->getName() + "\", \"" + notification->getMessage() + "\")");
     201    }
     202
     203    /**
     204    @brief
     205        Removes the least recently added Notification form the NotificationQueue.
     206    */
     207    void NotificationQueue::pop(void)
     208    {
     209        NotificationContainer* container = this->notifications_.back();
     210        this->ordering_.erase(container);
     211        this->notifications_.pop_back();
     212        this->size_--;
     213        delete container;
     214        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.popNotification(\"" + this->getName() + "\")");
     215    }
     216
     217    /**
     218    @brief
     219        Removes the Notification that is stored in the input container.
     220    @param container
     221        The NotificationContainer with the Notification to be removed.
     222    */
     223    void NotificationQueue::remove(NotificationContainer* container)
     224    {
     225        std::vector<NotificationContainer*>::iterator it = std::find(this->notifications_.begin(), this->notifications_.end(), container);
     226        std::vector<NotificationContainer*>::difference_type index = it - this->notifications_.begin ();
     227        this->ordering_.erase(container);
     228        this->notifications_.erase(it);
     229        this->size_--;
     230        delete container;
     231        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeNotification(\"" + this->getName() + "\", " + multi_cast<std::string>(index) + ")");
     232    }
     233
     234    /**
     235    @brief
     236        Clears the queue by removing all containers.
     237    */
     238    void NotificationQueue::clear(void)
     239    {
     240        this->ordering_.clear();
     241        for(std::vector<NotificationContainer*>::iterator it = this->notifications_.begin(); it != this->notifications_.end(); it++)
     242        {
     243            delete *it;
     244        }
     245        this->notifications_.clear();
     246        this->size_ = 0;
     247        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.clearQueue(\"" + this->getName() + "\")");
     248    }
     249
     250    /**
     251    @brief
    181252        Sets the name of the NotificationQueue.
    182253    @param name
     
    203274    {
    204275        this->maxSize_ = size;
    205         this->update();
    206     }
    207 
    208     /**
    209     @brief
    210         Sets the maximum number of characters a Notification message displayed by this queue is allowed to have.
    211     @param length
    212         The length to be set.
    213     @return
    214         Returns true if successful.
    215     */
    216     void NotificationQueue::setNotificationLength(unsigned int length)
    217     {
    218         this->notificationLength_ = length;
    219276        this->update();
    220277    }
     
    299356    /**
    300357    @brief
    301         Sets the font size.
    302     @param size
    303         The font size.
    304     @return
    305         Returns true if successful.
    306     */
    307     bool NotificationQueue::setFontSize(float size)
    308     {
    309         if(size <= 0)
    310             return false;
    311        
    312         return true;
    313     }
    314 
    315     /**
    316     @brief
    317         Sets the font.
    318     @param font
    319         The font.
    320     @return
    321         Returns true if successful.
    322     */
    323     bool NotificationQueue::setFont(const std::string & font)
    324     {
    325        
    326         return true;
    327     }
    328 
    329     /**
    330     @brief
    331358        Aligns all the Notifications to the position of the NotificationQueue.
    332359    */
     
    336363    }
    337364
    338     /**
    339     @brief
    340         Adds a Notification, to the queue.
    341         It inserts it into the storage containers, creates a corresponding container and pushes the Notification message to the GUI.
    342     @param notification
    343         The Notification.
    344     @param time
    345         The time.
    346     */
    347     void NotificationQueue::push(Notification* notification, const std::time_t & time)
    348     {
    349         NotificationContainer* container = new NotificationContainer;
    350         container->notification = notification;
    351         container->time = time;
    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.
    383     @param container
    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_--;
    393         delete container;
    394         GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeNotification(\"" + this->getName() + "\", " + multi_cast<std::string>(index) + ")");
    395     }
    396 
    397     /**
    398     @brief
    399         Clears the queue by removing all containers.
    400     */
    401     void NotificationQueue::clear(void)
    402     {
    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() + "\")");
    411     }
    412 
    413365}
Note: See TracChangeset for help on using the changeset viewer.