Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 28, 2011, 5:10:01 PM (13 years ago)
Author:
dafrick
Message:

Merging tutoriallevel3 branch into presentation branch.

Location:
code/branches/presentation
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation

  • code/branches/presentation/src/modules/notifications/NotificationQueue.cc

    r8079 r8637  
    3838
    3939#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"
    4441#include "util/SubString.h"
    45 
    46 #include "Notification.h"
    4742
    4843namespace orxonox
    4944{
    5045
    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
    5467    @param name
    5568        The name of the new NotificationQueue. It needs to be unique
     
    6275        The time during which a Notification is (at most) displayed.
    6376    */
    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    {
    71101        this->size_ = 0;
    72102        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    {
    80115        // Register the NotificationQueue with the NotificationManager.
    81116        bool queueRegistered = NotificationManager::getInstance().registerQueue(this);
     
    88123        }
    89124
    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 
    105125        COUT(3) << "NotificationQueue '" << this->getName() << "' created." << std::endl;
    106     }
    107 
    108     /**
    109     @brief
    110         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     @brief
    128         Destroys the NotificationQueue.
    129         Used in lua and NotificationManager.
    130     @param noGraphics
    131         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     @brief
    146         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()) + ")");
    152126    }
    153127
     
    161135    {
    162136        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 containig 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.
    166140
    167141            std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator it = this->ordering_.begin();
     
    177151    }
    178152
     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
    179164    /**
    180165    @brief
    181166        Updates the NotificationQueue.
    182167        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.
    183169    */
    184170    void NotificationQueue::update(void)
     
    188174        std::multimap<std::time_t, Notification*>* notifications = new std::multimap<std::time_t, Notification*>;
    189175        // 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_);
    191180
    192181        if(!notifications->empty())
    193182        {
    194             // Add all Notifications.
     183            // Add all Notifications that have been created after this NotificationQueue was created.
    195184            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            }
    197189        }
    198190
     
    246238        this->notifications_.insert(this->notifications_.begin(), container);
    247239
    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);
    251242
    252243        COUT(5) << "Notification \"" << notification->getMessage() << "\" pushed to NotificationQueue '" << this->getName() << "'" << endl;
     244        COUT(3) << "NotificationQueue \"" << this->getName() << "\": " << notification->getMessage() << endl;
    253245    }
    254246
     
    279271        delete container;
    280272
    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();
    284275    }
    285276
     
    305296        delete *containerIterator;
    306297
    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);
    310301    }
    311302
     
    314305        Clears the NotificationQueue by removing all NotificationContainers.
    315306    @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.
    317308    */
    318309    void NotificationQueue::clear(bool noGraphics)
     
    326317        this->notifications_.clear();
    327318        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() + "\")");
    332319    }
    333320
     
    354341            return;
    355342
     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       
    356349        this->maxSize_ = size;
    357350
    358         if(this->registered_)
     351        if(this->isRegistered())
    359352            this->update();
    360353    }
     
    364357        Sets the maximum number of seconds a Notification is displayed.
    365358    @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)
    371362    {
    372363        if(this->displayTime_ == time)
    373364            return;
    374365
     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           
    375371        this->displayTime_ = time;
    376372
    377         if(this->registered_)
     373        if(this->isRegistered())
    378374            this->update();
    379375    }
     
    381377    /**
    382378    @brief
    383         Produces all targets of the NotificationQueue concatinated as string, with commas (',') as seperators.
     379        Produces all targets of the NotificationQueue concatenated as string, with commas (',') as separators.
    384380    @return
    385381        Returns the targets as a string.
     
    407403        The targets are the senders whose Notifications are displayed in this queue.
    408404    @param targets
    409         Accepts a string of targets, each seperated by commas (','), spaces are ignored.
     405        Accepts a string of targets, each separated by commas (','), spaces are ignored.
    410406    */
    411407    void NotificationQueue::setTargets(const std::string & targets)
     
    417413            this->targets_.insert(string[i]);
    418414
    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;
    424434    }
    425435
Note: See TracChangeset for help on using the changeset viewer.