Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 14, 2011, 8:53:28 PM (13 years ago)
Author:
dafrick
Message:

Merging presentation branch back into trunk.
There are many new features and also a lot of other changes and bugfixes, if you want to know, digg through the svn log.
Not everything is yet working as it should, but it should be fairly stable. If you habe any bug reports, just send me an email.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/modules/notifications/NotificationQueue.cc

    r8079 r8706  
    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), 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
    5474    @param name
    5575        The name of the new NotificationQueue. It needs to be unique
     
    6282        The time during which a Notification is (at most) displayed.
    6383    */
    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    {
    80127        // Register the NotificationQueue with the NotificationManager.
    81128        bool queueRegistered = NotificationManager::getInstance().registerQueue(this);
     
    88135        }
    89136
    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 
    105137        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()) + ")");
    152138    }
    153139
     
    161147    {
    162148        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.
     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.
    166152
    167153            std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator it = this->ordering_.begin();
     
    177163    }
    178164
     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
    179188    /**
    180189    @brief
    181190        Updates the NotificationQueue.
    182191        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.
    183193    */
    184194    void NotificationQueue::update(void)
     
    188198        std::multimap<std::time_t, Notification*>* notifications = new std::multimap<std::time_t, Notification*>;
    189199        // 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_);
    191204
    192205        if(!notifications->empty())
    193206        {
    194             // Add all Notifications.
     207            // Add all Notifications that have been created after this NotificationQueue was created.
    195208            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            }
    197213        }
    198214
     
    222238    @brief
    223239        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.
    225241    @param notification
    226242        The Notification to be pushed.
     
    246262        this->notifications_.insert(this->notifications_.begin(), container);
    247263
    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);
    251266
    252267        COUT(5) << "Notification \"" << notification->getMessage() << "\" pushed to NotificationQueue '" << this->getName() << "'" << endl;
     268        COUT(3) << "NotificationQueue \"" << this->getName() << "\": " << notification->getMessage() << endl;
    253269    }
    254270
     
    279295        delete container;
    280296
    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();
    284299    }
    285300
     
    305320        delete *containerIterator;
    306321
    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);
    310325    }
    311326
     
    314329        Clears the NotificationQueue by removing all NotificationContainers.
    315330    @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.
    317332    */
    318333    void NotificationQueue::clear(bool noGraphics)
     
    326341        this->notifications_.clear();
    327342        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() + "\")");
    332343    }
    333344
     
    354365            return;
    355366
     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       
    356373        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())
    359384            this->update();
    360385    }
     
    364389        Sets the maximum number of seconds a Notification is displayed.
    365390    @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)
    371394    {
    372395        if(this->displayTime_ == time)
    373396            return;
    374397
     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           
    375403        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())
    378414            this->update();
    379415    }
     
    381417    /**
    382418    @brief
    383         Produces all targets of the NotificationQueue concatinated as string, with commas (',') as seperators.
     419        Produces all targets of the NotificationQueue concatenated as string, with commas (',') as separators.
    384420    @return
    385421        Returns the targets as a string.
     
    407443        The targets are the senders whose Notifications are displayed in this queue.
    408444    @param targets
    409         Accepts a string of targets, each seperated by commas (','), spaces are ignored.
     445        Accepts a string of targets, each separated by commas (','), spaces are ignored.
    410446    */
    411447    void NotificationQueue::setTargets(const std::string & targets)
     
    417453            this->targets_.insert(string[i]);
    418454
    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;
    424483    }
    425484
Note: See TracChangeset for help on using the changeset viewer.