Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 12, 2011, 12:31:23 AM (13 years ago)
Author:
dafrick
Message:

Merging tutoriallevel2 branch into tutoriallevel3 branch.

Location:
code/branches/tutoriallevel3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/tutoriallevel3

  • code/branches/tutoriallevel3/src/modules/notifications/NotificationManager.cc

    r8079 r8453  
    3636#include "core/command/ConsoleCommand.h"
    3737#include "core/CoreIncludes.h"
    38 #include "core/GUIManager.h"
    3938#include "core/LuaState.h"
    40 #include "network/Host.h"
    41 #include "network/NetworkFunction.h"
    4239#include "util/ScopedSingletonManager.h"
    4340
    4441#include "interfaces/NotificationListener.h"
    4542
    46 #include "Notification.h"
    4743#include "NotificationQueue.h"
    48 
    49 #include "ToluaBindNotifications.h"
     44#include "NotificationQueueCEGUI.h"
    5045
    5146namespace orxonox
    5247{
    5348
    54     const std::string NotificationManager::ALL("all");
    55     const std::string NotificationManager::NONE("none");
    56 
    57     // Register tolua_open function when loading the library.
    58     DeclareToluaInterface(Notifications);
    59 
    6049    ManageScopedSingleton(NotificationManager, ScopeID::Root, false);
    6150
    62     // Setting console command to enter the edit mode.
    63     SetConsoleCommand("enterEditMode", &NotificationManager::enterEditMode);
    64 
    65     registerStaticNetworkFunction(NotificationManager::sendNotification);
    66 
    6751    /**
    6852    @brief
     
    7357        RegisterRootObject(NotificationManager);
    7458
    75         this->highestIndex_ = 0;
    76 
    77         ModifyConsoleCommand("enterEditMode").setObject(this);
    78 
    7959        COUT(3) << "NotificatioManager created." << std::endl;
    8060    }
     
    8666    NotificationManager::~NotificationManager()
    8767    {
    88         ModifyConsoleCommand("enterEditMode").setObject(NULL);
    89 
    9068        // Destroys all Notifications.
    9169        for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it!= this->allNotificationsList_.end(); it++)
     
    10684        while(it != this->queues_.end())
    10785        {
    108             it->second->destroy(true);
     86            it->second->destroy();
    10987            it = this->queues_.begin();
    11088        }
     
    11593    /**
    11694    @brief
    117         Sends a Notification with the specified message to the specified client from the specified sender.
     95        Creates and registers a Notification with the input message from the input sender.
     96        This is called by the NotificationListener, whenever a new notification arrives.
    11897    @param message
    119         The message that should be sent.
    120     @param clientId
    121         The id of the client the notification should be sent to.
     98        The message of the new Notification.
    12299    @param sender
    123         The sender that sent the notification.
    124     @param isLocal
    125         If this is set to true (false is default), then the Notification is sent to the client where this function is executed, meaning the Notification is sent locally.
    126     */
    127     /*static*/ void NotificationManager::sendNotification(const std::string& message, unsigned int clientId, const std::string& sender, bool isLocal)
    128     {
    129         // If we're in standalone mode or we're already no the right client we create and send the Notification.
    130         if(GameMode::isStandalone() || isLocal || Host::getPlayerID() == clientId)
    131         {
    132             Notification* notification = new Notification(message, sender);
    133             if(NotificationManager::getInstance().registerNotification(notification))
    134                 COUT(3) << "Notification \"" << notification->getMessage() << "\" sent." << std::endl;
    135         }
    136         // If we're on the server (and the server is not the intended recipient of the Notification) we send it over the network.
    137         else if(GameMode::isServer())
    138         {
    139             callStaticNetworkFunction(NotificationManager::sendNotification, clientId, message, clientId, sender);
    140         }
    141     }
    142 
    143     /**
    144     @brief
    145         Registers a Notification within the NotificationManager and makes sure that the Notification is sent to all the NotificationListeners associated with its sender.
     100        The name of the entity (of the collective) that sent the new Notification.
     101    @param type
     102        The type of the new Notification.
     103    @return
     104        Returns true if successful.
     105    */
     106    bool NotificationManager::registerNotification(const std::string& message, const std::string& sender, notificationMessageType::Value type)
     107    {
     108        // TODO: Do something with the type.
     109        Notification* notification = new Notification(message, sender, type);
     110        return this->registerNotification(notification);
     111    }
     112
     113    /**
     114    @brief
     115        Executes the input command from the input sender.
     116        This is called by the NotificationListener, whenever a new command arrives.
     117    @param command
     118        The command to be executed,
     119    @param sender
     120        The The name of the entity (of the collective) that sent the command.
     121    @return
     122        Returns true if the command was successfully executed.
     123    */
     124    bool NotificationManager::executeCommand(notificationCommand::Value command, const std::string& sender)
     125    {
     126        bool commandExecuted = false;
     127        if(command == notificationCommand::clear)
     128        {
     129            if(this->commandClear(sender))
     130                commandExecuted = true;
     131        }
     132
     133        if(commandExecuted)
     134            COUT(3) << "Notification command \"" << NotificationListener::command2Str(command) << "\" executed." << endl;
     135
     136        return commandExecuted;
     137    }
     138
     139    /**
     140    @brief
     141        The clear command. Clears all NotificationQueues that have its sender as a target.
     142    @param sender
     143        The sender of the clear command.
     144    @return
     145        Returns true if the command was successfully executed by at least one NotificationQueue, false if it was not executed.
     146    */
     147    bool NotificationManager::commandClear(const std::string& sender)
     148    {
     149        bool all = (sender == NotificationListener::ALL);
     150        bool executed = false;
     151        // Clear all NotificationQueues that have the input sender as target.
     152        for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); it++) // Iterate through all NotificationQueues.
     153        {
     154            const std::set<std::string>& set = it->second->getTargetsSet();
     155            // If either the sender is 'all', the NotificationQueue has as target all or the NotificationQueue has the input sender as a target.
     156            if(all || set.find(NotificationListener::ALL) != set.end() || set.find(sender) != set.end())
     157                executed = it->second->tidy() || executed;
     158        }
     159
     160        return executed;
     161    }
     162   
     163    /**
     164    @brief
     165        Registers a Notification within the NotificationManager and makes sure that the Notification is sent to all the NotificationQueues associated with its sender.
    146166    @param notification
    147167        The Notification to be registered.
     
    158178        this->allNotificationsList_.insert(std::pair<std::time_t, Notification*>(time, notification));
    159179
    160         if(notification->getSender() == NotificationManager::NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications.
     180        if(notification->getSender() == NotificationListener::NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications.
    161181            return true;
    162182
    163         bool all = false;
    164         if(notification->getSender() == NotificationManager::ALL) // If all are the sender, then the Notifications is added to every NotificationListener.
    165             all = true;
    166 
    167         // Insert the Notification in all NotificationListeners that have its sender as target.
    168         for(std::map<NotificationListener*, unsigned int>::iterator it = this->listenerList_.begin(); it != this->listenerList_.end(); it++) // Iterate through all NotificationListeners.
    169         {
    170             const std::set<std::string>& set = it->first->getTargetsSet();
    171             bool bAll = set.find(NotificationManager::ALL) != set.end();
    172             // If either the Notification has as sender 'all', the NotificationListener displays all Notifications or the NotificationListener has the sender of the Notification as target.
     183        // If all are the sender, then the Notifications is added to every NotificationQueue.
     184        bool all = (notification->getSender() == NotificationListener::ALL);
     185
     186        // Insert the Notification in all NotificationQueues that have its sender as target.
     187        for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); it++) // Iterate through all NotificationQueues.
     188        {
     189            const std::set<std::string>& set = it->second->getTargetsSet();
     190            bool bAll = set.find(NotificationListener::ALL) != set.end();
     191            // If either the Notification has as sender 'all', the NotificationQueue displays all Notifications or the NotificationQueue has the sender of the Notification as target.
    173192            if(all || bAll || set.find(notification->getSender()) != set.end())
    174193            {
    175194                if(!bAll)
    176                     this->notificationLists_[it->second]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the notifications list of the current NotificationListener.
    177                 it->first->update(notification, time); // Update the NotificationListener.
     195                    this->notificationLists_[it->second->getName()]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the notifications list of the current NotificationQueue.
     196                it->second->update(notification, time); // Update the NotificationQueue.
    178197            }
    179198        }
     
    186205    /**
    187206    @brief
    188         Unregisters a Notification within the NotificationManager for a given NotificationListener.
     207        Unregisters a Notification within the NotificationManager for a given NotificationQueue.
    189208    @param notification
    190209        A pointer to the Notification to be unregistered.
    191     @param listener
    192         A pointer to the NotificationListener the Notification is unregistered for.
    193     */
    194     void NotificationManager::unregisterNotification(Notification* notification, NotificationListener* listener)
     210    @param queue
     211        A pointer to the NotificationQueue the Notification is unregistered for.
     212    */
     213    void NotificationManager::unregisterNotification(Notification* notification, NotificationQueue* queue)
    195214    {
    196215        assert(notification);
    197         assert(listener);
    198 
    199         // Remove the Notification from the list of Notifications of the input NotificationListener.
    200         this->removeNotification(notification, *(this->notificationLists_.find(this->listenerList_.find(listener)->second)->second));
    201 
    202         COUT(4) << "Notification (&" << notification << ") unregistered with the NotificationManager from listener (&" << listener << ")" << std::endl;
     216        assert(queue);
     217
     218        // Remove the Notification from the list of Notifications of the input NotificationQueue.
     219        this->removeNotification(notification, *(this->notificationLists_.find(queue->getName())->second));
     220
     221        COUT(4) << "Notification (&" << notification << ") unregistered with the NotificationManager from NotificationQueue " << queue->getName() << "." << std::endl;
    203222    }
    204223
     
    230249    /**
    231250    @brief
    232         Registers a NotificationListener within the NotificationManager.
    233     @param listener
    234         The NotificationListener to be registered.
    235     @return
    236         Returns true if successful.  Fales if the NotificationListener is already registered.
    237     */
    238     bool NotificationManager::registerListener(NotificationListener* listener)
    239     {
    240         assert(listener);
    241 
    242         // If the NotificationListener is already registered.
    243         if(this->listenerList_.find(listener) != this->listenerList_.end())
    244             return false;
    245 
    246         this->highestIndex_ += 1;
    247         unsigned int index = this->highestIndex_; // An identifier that identifies each registered NotificationListener uniquely.
    248 
    249         this->listenerList_[listener] = index; // Add the NotificationListener to the list of NotificationListeners.
    250 
    251         const std::set<std::string>& set = listener->getTargetsSet();
    252 
    253         // If all senders are the target of the NotificationListener, then the list of Notifications for that specific NotificationListener is the same as the list of all Notifications.
    254         bool bAll = set.find(NotificationManager::ALL) != set.end();
    255         std::multimap<std::time_t, Notification*>* map = NULL;
    256         if(bAll)
    257             this->notificationLists_[index] = &this->allNotificationsList_;
    258         // Else a new list (resp. multimap) is created and added to the list of Notification lists for NotificationListeners.
    259         else
    260         {
    261             this->notificationLists_[index] = new std::multimap<std::time_t, Notification*>;
    262             map = this->notificationLists_[index];
    263         }
    264 
    265         // Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationListener.
    266         for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
    267         {
    268             if(!bAll && set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current Notification as target.
    269                 map->insert(std::pair<std::time_t, Notification*>(it->first, it->second));
    270         }
    271 
    272         listener->update(); // Update the listener.
    273 
    274         COUT(4) << "NotificationListener registered with the NotificationManager." << std::endl;
    275 
    276         return true;
    277     }
    278 
    279     /**
    280     @brief
    281         Unregisters a NotificationListener within the NotificationManager.
    282     @param listener
    283         The NotificationListener to be unregistered.
    284     */
    285     void NotificationManager::unregisterListener(NotificationListener* listener)
    286     {
    287         assert(listener);
    288 
    289         unsigned int identifier = this->listenerList_.find(listener)->second;
    290         std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(identifier)->second;
    291 
    292         // If the map is not the map of all Notifications, make sure all Notifications are unregistered.
    293         std::multimap<std::time_t, Notification*>::iterator it = map->begin();
    294         if(map != &this->allNotificationsList_)
    295         {
    296             while(it != map->end())
    297             {
    298                 this->unregisterNotification(it->second, listener);
    299                 it = map->begin();
    300             }
    301             delete map;
    302         }
    303 
    304         COUT(4) << "NotificationListener '" << identifier << "' unregistered with the NotificationManager." << std::endl;
    305 
    306         // Remove the NotificationListener from the list of NotificationListeners.
    307         this->listenerList_.erase(listener);
    308         // Remove the Notifications list that was associated with the input NotificationListener.
    309         this->notificationLists_.erase(identifier);
    310     }
    311 
    312     /**
    313     @brief
    314         Fetches the Notifications for a specific NotificationListener in a specified timeframe and stores them in the input map.
    315     @param listener
    316         The NotificationListener the Notifications are fetched for.
     251        Fetches the Notifications for a specific NotificationQueue in a specified timeframe and stores them in the input map.
     252    @param queue
     253        The NotificationQueue the Notifications are fetched for.
    317254    @param map
    318255        A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated.
     
    324261        Returns true if successful.
    325262    */
    326     void NotificationManager::getNotifications(NotificationListener* listener, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd)
    327     {
    328         assert(listener);
     263    void NotificationManager::getNotifications(NotificationQueue* queue, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd)
     264    {
     265        assert(queue);
    329266        assert(map);
    330267
    331         std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; // All the Notifications for the input NotificationListener.
     268        std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[queue->getName()]; // All the Notifications for the input NotificationQueue.
    332269
    333270        std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest;
     
    342279    /**
    343280    @brief
    344         Enters the edit mode of the NotificationLayer.
    345     */
    346     void NotificationManager::enterEditMode(void)
    347     {
    348         if(GameMode::showsGraphics())
    349         {
    350             GUIManager::getInstance().hideGUI("NotificationLayer");
    351             GUIManager::getInstance().showGUI("NotificationLayer", false, false);
    352             GUIManager::getInstance().getLuaState()->doString("NotificationLayer.enterEditMode()");
     281        Fetches the newest Notifications for a specific NotificationQueue and stores them in the input map.
     282    @param queue
     283        The NotificationQueue the Notifications are fetched for.
     284    @param map
     285        A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated.
     286    @param numberOfNotifications
     287        The number of newest Notifications to be got.
     288    @return
     289        Returns true if successful.
     290    */
     291    void NotificationManager::getNewestNotifications(NotificationQueue* queue, std::multimap<std::time_t, Notification*>* map, int numberOfNotifications)
     292    {
     293        assert(queue);
     294        assert(map);
     295
     296        std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[queue->getName()]; // All the Notifications for the input NotificationQueue.
     297
     298        if(!notifications->empty()) // If the list of Notifications is not empty.
     299        {
     300            std::multimap<std::time_t,Notification*>::iterator it = notifications->end();
     301            for(int i = 0; i < numberOfNotifications; i++) // Iterate through the Notifications from the newest until we have the specified number of notifications.
     302            {
     303                it--;
     304                map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); // Add the found Notifications to the map.
     305                if(it == notifications->begin())
     306                    break;
     307            }
    353308        }
    354309    }
     
    357312    @brief
    358313        Registers a NotificationQueue.
    359         This makes sure that the NotificationQueue can be attained through lua by name. It also makes sure that the NotificationQueue is destroyed upon destruction of the NotificationManager.
     314        This makes sure that the NotificationQueue can be accessed through lua by name. It also makes sure that the NotificationQueue is destroyed upon destruction of the NotificationManager.
    360315    @param queue
    361316        A pointer to the NotificationQueue to be registered.
     
    365320    bool NotificationManager::registerQueue(NotificationQueue* queue)
    366321    {
     322        assert(queue);
     323
     324        // If the NotificationQueue is already registered.
     325        if(this->queues_.find(queue->getName()) != this->queues_.end())
     326            return false;
     327
     328        this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)); // Add the NotificationQueue to the list of NotificationQueues.
     329
     330        const std::set<std::string>& set = queue->getTargetsSet();
     331
     332        // If all senders are the target of the NotificationQueue, then the list of Notifications for that specific NotificationQueue is the same as the list of all Notifications.
     333        bool bAll = set.find(NotificationListener::ALL) != set.end();
     334        std::multimap<std::time_t, Notification*>* map = NULL;
     335        if(bAll)
     336            this->notificationLists_[queue->getName()] = &this->allNotificationsList_;
     337        // Else a new list (resp. multimap) is created and added to the list of Notification lists for NotificationQueues.
     338        else
     339        {
     340            this->notificationLists_[queue->getName()] = new std::multimap<std::time_t, Notification*>;
     341            map = this->notificationLists_[queue->getName()];
     342        }
     343
     344        // Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationQueue.
     345        for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
     346        {
     347            if(!bAll && set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current Notification as target.
     348                map->insert(std::pair<std::time_t, Notification*>(it->first, it->second));
     349        }
     350
     351        queue->update(); // Update the queue.
     352       
    367353        COUT(4) << "NotificationQueue '" << queue->getName() << "' registered with the NotificationManager." << std::endl;
    368         return this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)).second;
     354        return true;
    369355    }
    370356
     
    377363    void NotificationManager::unregisterQueue(NotificationQueue* queue)
    378364    {
     365        assert(queue);
     366
     367        std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(queue->getName())->second;
     368
     369        // If the map is not the map of all Notifications, make sure all Notifications are unregistered.
     370        std::multimap<std::time_t, Notification*>::iterator it = map->begin();
     371        if(map != &this->allNotificationsList_)
     372        {
     373            while(it != map->end())
     374            {
     375                this->unregisterNotification(it->second, queue);
     376                it = map->begin();
     377            }
     378            delete map;
     379        }
     380
     381        // Remove the NotificationQueue from the list of NotificationQueues.
     382        this->queues_.erase(queue->getName());
     383        // Remove the Notifications list that was associated with the input NotificationQueue.
     384        this->notificationLists_.erase(queue->getName());
     385       
    379386        COUT(4) << "NotificationQueue '" << queue->getName() << "' unregistered with the NotificationManager." << std::endl;
    380         this->queues_.erase(queue->getName());
    381     }
    382 
    383     /**
    384     @brief
    385         Loads all the NotificationQueues that should exist.
    386     */
    387     void NotificationManager::loadQueues(void)
    388     {
    389         new NotificationQueue("all");
    390     }
    391 
    392     /**
    393     @brief
    394         Creates a new NotificationQueue.
    395         This is used in lua.
    396     @param name
    397         The name of the new NotificationQueue.
    398     */
    399     void NotificationManager::createQueue(const std::string& name)
    400     {
    401         new NotificationQueue(name);
    402387    }
    403388
     
    420405    }
    421406
     407    /**
     408    @brief
     409        Loads all the NotificationQueues that should exist.
     410    */
     411    void NotificationManager::loadQueues(void)
     412    {
     413        NotificationQueueCEGUI* allQueue = new NotificationQueueCEGUI("all");
     414        allQueue->setDisplaySize(Vector2(0.5, 0));
     415        allQueue->setPosition(Vector4(0.0, 10, 0.3, 0));
     416
     417        NotificationQueueCEGUI* infoQueue = new NotificationQueueCEGUI("info", "gameinfo", 1, -1);
     418        infoQueue->setPosition(Vector4(0.2, 0, 0.8, 0));
     419        infoQueue->setFontSize(24);
     420        infoQueue->setFontColor(Vector4(1.0, 1.0, 0.0, 0.8));
     421        infoQueue->setAlignment("HorzCentred");
     422        infoQueue->setDisplaySize(Vector2(0.6, 0.0));
     423    }
     424
     425    // Notification class
     426
     427    /**
     428    @brief
     429        Constructor. Creates a Notification with the input message and sender.
     430    @param message
     431        The message of the Notification.
     432    @param sender
     433        The sender of the Notification.
     434    @param type
     435
     436    */
     437    Notification::Notification(const std::string& message, const std::string& sender, notificationMessageType::Value type)
     438    {
     439        this->initialize();
     440        this->message_ = message;
     441        this->sender_ = sender;
     442        this->type_ = type;
     443    }
     444
     445    /**
     446    @brief
     447        Destructor.
     448    */
     449    Notification::~Notification()
     450    {
     451
     452    }
     453
     454    /**
     455    @brief
     456        Registers the object and sets some default values.
     457    */
     458    void Notification::initialize(void)
     459    {
     460        this->message_.clear();
     461        this->sender_ = NotificationListener::NONE;
     462    }
     463
    422464}
Note: See TracChangeset for help on using the changeset viewer.