Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 9, 2010, 10:59:01 PM (14 years ago)
Author:
dafrick
Message:

Finished EditMode, needs some polish (no not the language…) though.
Took better advantage of lua tables in NotificationLayer.lua
Fixed a lot of bugs, it really is a miracle that the Notifications worked as well as they did.

Location:
code/branches/notifications/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • code/branches/notifications/src/modules/notifications/CMakeLists.txt

    r7354 r7395  
    1313  TOLUA_FILES
    1414    NotificationManager.h
     15    NotificationQueue.h
    1516  PCH_FILE
    1617    NotificationsPrecompiledHeaders.h
  • code/branches/notifications/src/modules/notifications/NotificationManager.cc

    r7362 r7395  
    2828
    2929/**
    30     @file
     30    @file NotificationManager.cc
    3131    @brief Implementation of the NotificationManager class.
    3232*/
     
    4343#include "NotificationQueue.h"
    4444
     45#include "ToluaBindNotifications.h"
     46
    4547namespace orxonox
    4648{
     
    4951    const std::string NotificationManager::NONE("none");
    5052
     53    // Register tolua_open function when loading the library.
     54    DeclareToluaInterface(Notifications);
     55
    5156    ManageScopedSingleton(NotificationManager, ScopeID::Graphics, false);
    5257
     
    6570
    6671        ModifyConsoleCommand("enterEditMode").setObject(this);
    67 
    68         if(GameMode::showsGraphics())
    69         {
    70             GUIManager::getInstance().loadGUI("NotificationLayer");
    71 
    72             // Create first queue:
    73             this->queues_.push_back(new NotificationQueue("all"));
    74         }
     72       
     73        COUT(3) << "NotificatioManager created." << std::endl;
    7574    }
    7675
     
    8281    {
    8382        ModifyConsoleCommand("enterEditMode").setObject(NULL);
     83
     84        for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
     85            it->second->destroy();
     86       
     87        COUT(3) << "NotificationManager destroyed." << std::endl;
    8488    }
    8589
    8690    void NotificationManager::preDestroy(void)
    8791    {
    88         for(std::vector<NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); it++)
    89             (*it)->destroy();
     92        for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); )
     93        {
     94            NotificationQueue* queue = (*it).second;
     95            it++;
     96            queue->destroy();
     97        }
    9098        this->queues_.clear();
    9199    }
     
    101109    bool NotificationManager::registerNotification(Notification* notification)
    102110    {
    103 
    104         if(notification == NULL) //!< A NULL-Notification cannot be registered.
     111        if(notification == NULL) // A NULL-Notification cannot be registered.
    105112            return false;
    106113
    107114        std::time_t time = std::time(0); //TODO: Doesn't this expire? //!< Get current time.
    108115
    109         this->allNotificationsList_.insert(std::pair<std::time_t,Notification*>(time,notification));
    110 
    111         if(notification->getSender() == NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications.
     116        this->allNotificationsList_.insert(std::pair<std::time_t, Notification*>(time, notification));
     117
     118        if(notification->getSender() == NotificationManager::NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications.
    112119            return true;
    113120
    114121        bool all = false;
    115         if(notification->getSender() == ALL) // If all are the sender, then the Notifications is added to every NotificationListener.
     122        if(notification->getSender() == NotificationManager::ALL) // If all are the sender, then the Notifications is added to every NotificationListener.
    116123            all = true;
    117124
     
    119126        for(std::map<NotificationListener*,int>::iterator it = this->listenerList_.begin(); it != this->listenerList_.end(); it++) // Iterate through all listeners.
    120127        {
    121             std::set<std::string> set = it->first->getTargetsSet();
    122             if(all || set.find(notification->getSender()) != set.end() || set.find(ALL) != set.end()) //TODO: Make sure this works.
     128            std::set<std::string, NotificationListenerStringCompare> set = it->first->getTargetsSet();
     129            bool bAll = set.find(NotificationManager::ALL) != set.end();
     130            if(all || bAll || set.find(notification->getSender()) != set.end()) //TODO: Make sure this works.
    123131            {
    124                 this->notificationLists_[it->second]->insert(std::pair<std::time_t,Notification*>(time,notification)); // Insert the Notification in the Notifications list of the current NotificationListener.
     132                if(!bAll)
     133                {
     134                    this->notificationLists_[it->second]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the Notifications list of the current NotificationListener.
     135                }
    125136                it->first->update(notification, time); // Update the listener.
    126137                std::map<Notification*, unsigned int>::iterator counterIt = this->listenerCounter_.find(notification);
     
    132143        }
    133144
    134         COUT(4) << "Notification registered with the NotificationManager." << std::endl;
     145        COUT(4) << "Notification (&" << notification << ") registered with the NotificationManager." << std::endl;
    135146
    136147        return true;
     
    154165            this->listenerCounter_[notification] = this->listenerCounter_[notification] - 1;
    155166
    156         // If the Notification is no longer present in any of the NotificationListeners it can be removed from the map of all Notifications and be destroyed.
    157         if(this->listenerCounter_[notification] == (unsigned int) 0)
    158         {
    159             this->removeNotification(notification, this->allNotificationsList_);
    160             this->listenerCounter_.erase(notification);
    161             notification->destroy();
    162         }
    163 
    164         COUT(4) << "Notification unregistered with the NotificationManager." << std::endl;
     167        COUT(4) << "Notification (&" << notification << ")unregistered with the NotificationManager from listener (&" << listener << ")" << std::endl;
    165168    }
    166169
     
    175178        Returns true if successful.
    176179    */
    177     //TODO: Needed?
    178180    bool NotificationManager::removeNotification(Notification* notification, std::multimap<std::time_t, Notification*>& map)
    179181    {
     
    206208        this->listenerList_[listener] = index; // Add the NotificationListener to the list of listeners.
    207209
    208         std::set<std::string> set = listener->getTargetsSet(); //TODO: Does this work?
     210        std::set<std::string, NotificationListenerStringCompare> set = listener->getTargetsSet();
    209211
    210212        // If all senders are the target of the listener, then the list of notification for that specific listener is te same as the list of all Notifications.
    211         if(set.find(ALL) != set.end())
    212         {
     213        bool bAll = set.find(NotificationManager::ALL) != set.end();
     214        std::multimap<std::time_t, Notification*> map;
     215        if(bAll)
    213216            this->notificationLists_[index] = &this->allNotificationsList_;
    214             COUT(4) << "NotificationListener registered with the NotificationManager." << std::endl;
    215             return true;
    216         }
    217 
    218         this->notificationLists_[index] = new std::multimap<std::time_t,Notification*>;
    219         std::multimap<std::time_t,Notification*> map = *this->notificationLists_[index];
     217        else
     218        {
     219            this->notificationLists_[index] = new std::multimap<std::time_t, Notification*>;
     220            map = *this->notificationLists_[index];
     221        }
    220222
    221223        // Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationListener.
    222         for(std::multimap<std::time_t,Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
    223         {
    224             if(set.find(it->second->getSender()) != set.end()) // Checks whether the overlay has the sender of the current notification as target.
     224        for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
     225        {
     226            if(bAll || set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current notification as target.
    225227            {
    226                 map.insert(std::pair<std::time_t, Notification*>(it->first, it->second));
     228                if(!bAll)
     229                    map.insert(std::pair<std::time_t, Notification*>(it->first, it->second));
    227230                std::map<Notification*, unsigned int>::iterator counterIt = this->listenerCounter_.find(it->second);
    228231                if(counterIt == this->listenerCounter_.end())
     
    288291            return false;
    289292
    290         std::multimap<std::time_t,Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; // The Notifications for the input NotificationListener.
    291 
    292         if(notifications == NULL) // Returns NULL, if there are no Notifications.
    293             return true;
     293        std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; // The Notifications for the input NotificationListener.
     294
     295        if(notifications == NULL) // Returns false, if there are no Notifications.
     296            return false;
    294297
    295298        std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest;
    296299        itLowest = notifications->lower_bound(timeFrameStart);
    297         itHighest = notifications->upper_bound(timeFrameStart);
     300        itHighest = notifications->upper_bound(timeFrameEnd);
    298301
    299302        for(it = itLowest; it != itHighest; it++) // Iterate through the Notifications from the start of the time Frame to the end of it.
    300         {
    301             map->insert(std::pair<std::time_t,Notification*>(it->first,it->second)); // Add the found Notifications to the map.
    302         }
     303            map->insert(std::pair<std::time_t, Notification*>(it->first,it->second)); // Add the found Notifications to the map.
    303304
    304305        return true;
    305306    }
    306307
    307     void NotificationManager::createQueue(const std::string& name, const std::string& targets, unsigned int size, unsigned int displayTime)
    308     {
    309         this->queues_.push_back(new NotificationQueue(name, targets, size, displayTime));
     308    void NotificationManager::loadQueues(void)
     309    {
     310        new NotificationQueue("all");
     311    }
     312
     313    void NotificationManager::createQueue(const std::string& name)
     314    {
     315        new NotificationQueue(name);
     316    }
     317
     318    NotificationQueue* NotificationManager::getQueue(const std::string & name)
     319    {
     320        std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.find(name);
     321        if(it == this->queues_.end())
     322            return NULL;
     323
     324        return (*it).second;
     325    }
     326
     327    bool NotificationManager::registerQueue(NotificationQueue* queue)
     328    {
     329        return this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)).second;
     330    }
     331   
     332    void NotificationManager::unregisterQueue(NotificationQueue* queue)
     333    {
     334        this->queues_.erase(queue->getName());
    310335    }
    311336
  • code/branches/notifications/src/modules/notifications/NotificationManager.h

    r7354 r7395  
    2828
    2929/**
    30     @file
     30    @file NotificationManager.h
    3131    @brief Definition of the NotificationManager class.
    3232*/
     
    4040#include <map>
    4141#include <string>
    42 #include <vector>
    4342
    4443#include "util/Singleton.h"
     
    5554        Damian 'Mozork' Frick
    5655    */
    57     class _NotificationsExport NotificationManager  // tolua_export
     56    class _NotificationsExport NotificationManager // tolua_export
    5857        : public Singleton<NotificationManager>, public OrxonoxClass
    5958    { // tolua_export
     
    7574            void unregisterListener(NotificationListener* listener); //!< Unregisters a NotificationListener withing the NotificationManager.
    7675
    77             bool getNotifications(NotificationListener* listener, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd); //!< Returns the Notifications for a specific NotificationListener in a specified timeframe.
     76            bool getNotifications(NotificationListener* listener, std::multimap<std::time_t, Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd); //!< Returns the Notifications for a specific NotificationListener in a specified timeframe.
    7877
    7978            /**
     
    8483            @return Returns true if successful.
    8584            */
    86             bool getNotifications(NotificationListener* listener, std::multimap<std::time_t,Notification*>* map, int timeDelay)
     85            bool getNotifications(NotificationListener* listener, std::multimap<std::time_t, Notification*>* map, int timeDelay)
    8786                { return this->getNotifications(listener, map, std::time(0)-timeDelay, std::time(0)); }
    8887
    8988            void enterEditMode(void);
    9089
    91             void createQueue(const std::string& name, const std::string& targets, unsigned int size, unsigned int displayTime); // tolua_export
     90            // tolua_begin
     91            void loadQueues(void);
     92           
     93            void createQueue(const std::string& name);
     94            orxonox::NotificationQueue* getQueue(const std::string & name);
     95            // tolua_end
     96
     97            bool registerQueue(NotificationQueue* queue);
     98            void unregisterQueue(NotificationQueue* queue);
    9299
    93100        private:
    94101            static NotificationManager* singletonPtr_s;
    95102
    96             std::vector<NotificationQueue*> queues_; //!< The list of NotificationQueues created by the NotificationManager.
     103            std::map<const std::string, NotificationQueue*> queues_; //!< The list of NotificationQueues created by the NotificationManager.
    97104
    98105            int highestIndex_; //!< This variable holds the highest index (resp. key) in notificationLists_s, to secure that no key appears twice.
    99106
    100             std::multimap<std::time_t,Notification*> allNotificationsList_; //!< Container where all notifications are stored.
    101             std::map<NotificationListener*,int> listenerList_; //!< Container where all NotificationListeners are stored with a number as identifier.
    102             std::map<int,std::multimap<std::time_t,Notification*>*> notificationLists_; //!< Container where all Notifications, for each identifier (associated with a NotificationListener), are stored.
     107            std::multimap<std::time_t, Notification*> allNotificationsList_; //!< Container where all notifications are stored.
     108            std::map<NotificationListener*, int> listenerList_; //!< Container where all NotificationListeners are stored with a number as identifier.
     109            std::map<int,std::multimap<std::time_t, Notification*>*> notificationLists_; //!< Container where all Notifications, for each identifier (associated with a NotificationListener), are stored.
    103110            std::map<Notification*, unsigned int> listenerCounter_; //!< A container to store the number of NotificationListeners a Notification is registered with.
    104111
  • code/branches/notifications/src/modules/notifications/NotificationQueue.cc

    r7354 r7395  
    3535
    3636#include <map>
     37#include <sstream>
    3738
    3839#include "core/CoreIncludes.h"
     
    6263        this->setDisplayTime(displayTime);
    6364
     65        bool queueRegistered = NotificationManager::getInstance().registerQueue(this);
     66        this->registered_ = true;
     67        if(!queueRegistered)
     68        {
     69            this->registered_ = false;
     70            COUT(1) << "Error: Notification Queue '" << this->getName() << "' could not be registered." << std::endl;
     71            return;
     72        }
     73
    6474        this->create();
    65 
    66         NotificationManager::getInstance().registerListener(this);
    67         this->registered_ = true;
     75       
     76        bool listenerRegistered = NotificationManager::getInstance().registerListener(this);
     77        if(!listenerRegistered)
     78        {
     79            this->registered_ = false;
     80            NotificationManager::getInstance().unregisterQueue(this);
     81            COUT(1) << "Error: Notification Queue '" << this->getName() << "' could not be registered." << std::endl;
     82            return;
     83        }
    6884
    6985        COUT(3) << "NotificationQueue '" << this->getName() << "' created." << std::endl;
     
    7793    {
    7894        this->targets_.clear();
    79         this->clear();
    8095
    8196        if(this->registered_)
     97        {
     98            this->clear();
     99           
    82100            NotificationManager::getInstance().unregisterListener(this);
     101            NotificationManager::getInstance().unregisterQueue(this);
     102        }
    83103    }
    84104
     
    100120    void NotificationQueue::create(void)
    101121    {
    102         GUIManager::getInstance().getLuaState()->doString("NotificationLayer.createQueue(\"" + this->getName() + "\", " + multi_cast<std::string>(this->getMaxSize()) + ")");
     122        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.createQueue(\"" + this->getName() +  "\", " + multi_cast<std::string>(this->getMaxSize()) + ")");
    103123    }
    104124
     
    144164        }
    145165
    146         if(notifications->empty())
    147             return;
    148 
    149         for(std::multimap<std::time_t, Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) // Add all Notifications.
    150             this->push(it->second, it->first);
     166        if(!notifications->empty())
     167        {
     168            for(std::multimap<std::time_t, Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) // Add all Notifications.
     169            {
     170                this->push(it->second, it->first);
     171            }
     172        }
    151173
    152174        delete notifications;
    153175
    154         COUT(4) << "NotificationQueue '" << this->getName() << "' updated." << std::endl;
     176        COUT(3) << "NotificationQueue '" << this->getName() << "' updated." << std::endl; //TODO: Level 4.
    155177    }
    156178
     
    167189        this->push(notification, time);
    168190
    169         COUT(4) << "NotificationQueue '" << this->getName() << "' updated. A new Notification has been added." << std::endl;
     191        COUT(3) << "NotificationQueue '" << this->getName() << "' updated. A new Notification has been added." << std::endl; //TODO: Level 4.
    170192    }
    171193
     
    253275    bool NotificationQueue::setName(const std::string& name)
    254276    {
    255         //TODO: Test uniqueness of name.
    256277        this->name_ = name;
    257278        return true;
     
    268289    void NotificationQueue::setMaxSize(unsigned int size)
    269290    {
     291        if(this->maxSize_ == size)
     292            return;
     293       
    270294        this->maxSize_ = size;
    271295        this->sizeChanged();
     
    292316    void NotificationQueue::setDisplayTime(unsigned int time)
    293317    {
     318        if(this->displayTime_ == time)
     319            return;
     320
    294321        this->displayTime_ = time;
    295         this->update();
     322
     323        if(this->registered_)
     324            this->update();
    296325    }
    297326
     
    299328    @brief
    300329        Produces all targets concatinated as string, with kommas (',') as seperators.
    301     @param string
    302         Pointer to a string which will be used by the method to fill with the concatination of the targets.
    303     @return
    304         Returns true if successful.
    305     */
    306     bool NotificationQueue::getTargets(std::string* string) const
    307     {
    308         if(string == NULL)
    309         {
    310             COUT(4) << "Input string must have memory allocated." << std::endl;
    311             return false;
    312         }
    313         string->clear();
     330    @return
     331        Returns the targets as a string.
     332    */
     333    const std::string& NotificationQueue::getTargets(void) const
     334    {
     335        std::stringstream stream;
    314336        bool first = true;
    315         for(std::set<std::string>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++) // Iterate through the set of targets.
     337        for(std::set<std::string, NotificationListenerStringCompare>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++) // Iterate through the set of targets.
    316338        {
    317339            if(!first)
    318                 *string += ',';
     340                stream << ',';
    319341            else
    320342                first = false;
    321             *string += *it;
    322         }
    323 
    324         return true;
     343            stream << *it;
     344        }
     345
     346        return *(new std::string(stream.str()));
    325347    }
    326348
     
    352374        }
    353375
     376        if(this->registered_)
     377        {
     378            NotificationManager::getInstance().unregisterListener(this);
     379            NotificationManager::getInstance().registerListener(this);
     380        }
     381
    354382        return true;
    355383    }
  • code/branches/notifications/src/modules/notifications/NotificationQueue.h

    r7354 r7395  
    4646#include "NotificationManager.h"
    4747
    48 namespace orxonox
    49 {
     48namespace orxonox // tolua_export
     49{ // tolua_export
    5050
    5151    //! Container to allow easy handling.
     
    6868        Damian 'Mozork' Frick
    6969    */
    70     class _NotificationsExport NotificationQueue : public Tickable, public NotificationListener
    71     {
     70    class _NotificationsExport NotificationQueue // tolua_export
     71        : public Tickable, public NotificationListener
     72    { // tolua_export
    7273
    7374        public:
     
    8081            void update(Notification* notification, const std::time_t & time); //!< Adds a Notification to the queue.
    8182
     83            // tolua_begin
    8284            /**
    8385            @brief Get the name of the NotificationQueue.
     
    8789                { return this->name_; }
    8890
     91            void setMaxSize(unsigned int size); //!< Sets the maximum number of displayed Notifications.
    8992            /**
    9093            @brief Returns the maximum number of Notifications displayed.
     
    9396            inline unsigned int getMaxSize() const
    9497                { return this->maxSize_; }
     98
     99            void setDisplayTime(unsigned int time); //!< Sets the maximum number of seconds a Notification is displayed.
     100            /**
     101            @brief Returns the time interval the Notification is displayed.
     102            @return Returns the display time.
     103            */
     104            inline float getDisplayTime() const
     105                { return this->displayTime_; }
     106            // tolua_end
     107
    95108            /**
    96109            @brief Returns the current number of Notifications displayed.
     
    99112            inline unsigned int getSize() const
    100113                { return this->size_; }
    101             /**
    102             @brief Returns the time interval the Notification is displayed.
    103             @return Returns the display time.
    104             */
    105             inline float getDisplayTime() const
    106                 { return this->displayTime_; }
    107114
    108115            /**
     
    110117            @return Retuns a set of string holding the different targets.
    111118            */
    112             inline const std::set<std::string> & getTargetsSet()
     119            inline const std::set<std::string, NotificationListenerStringCompare> & getTargetsSet()
    113120                { return this->targets_; }
    114             bool getTargets(std::string* string) const; //!< Returns a string consisting of the concatination of the targets.
     121
     122            // tolua_begin
     123            bool setTargets(const std::string & targets); //!< Set the targets of this NotificationQueue.
     124            const std::string& getTargets(void) const; //!< Returns a string consisting of the concatination of the targets.
     125            // tolua_end
    115126
    116127        private:
     
    124135            unsigned int displayTime_; //!< The time a Notification is displayed.
    125136
    126             std::set<std::string> targets_; //!< The targets the Queue displays Notifications of.
     137            std::set<std::string, NotificationListenerStringCompare> targets_; //!< The targets the Queue displays Notifications of.
    127138
    128139            std::multiset<NotificationContainer*, NotificationContainerCompare> ordering_; //!< The NotificationContainers ordered by the time they were registered. //TODO: Would set work as well?
     
    139150            bool setName(const std::string& name); //!< Sets the name of the NotificationQueue.
    140151
    141             void setMaxSize(unsigned int size); //!< Sets the maximum number of displayed Notifications.
    142             void setDisplayTime(unsigned int time); //!< Sets the maximum number of seconds a Notification is displayed.
    143 
    144             bool setTargets(const std::string & targets); //!< Set the targets of this NotificationQueue.
    145 
    146152            void sizeChanged(void); //!< Adjusts the NotificationQueue, when the maximum size has changed.
    147153
     
    152158            void clear(void); //!< Clears the queue by removing all Notifications.
    153159
    154     };
     160    }; // tolua_export
    155161
    156 }
     162} // tolua_export
    157163
    158164#endif /* _NotificationOverlay_H__ */
  • code/branches/notifications/src/orxonox/interfaces/NotificationListener.h

    r7163 r7395  
    4949    /**
    5050    @brief
     51        Struct that overloads the compare operation between two PickupIdentifier pointers.
     52    */
     53    //TODO:
     54    struct NotificationListenerStringCompare
     55    {
     56        bool operator() (const std::string& lhs, const std::string& rhs) const
     57            { return lhs.compare(rhs) < 0; }
     58    };
     59
     60    /**
     61    @brief
    5162        NotificationListener interface.
    5263    @author
     
    5970            virtual ~NotificationListener() {}
    6071
    61             virtual const std::set<std::string> & getTargetsSet() = 0;
     72            virtual const std::set<std::string, NotificationListenerStringCompare> & getTargetsSet() = 0;
    6273            virtual void update(void) = 0;
    6374            virtual void update(Notification* notification, const std::time_t & time) = 0;
Note: See TracChangeset for help on using the changeset viewer.