Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 8, 2009, 1:07:27 AM (15 years ago)
Author:
dafrick
Message:

Merging the QuestSystem branch to the trunk. Let's hope, this isn't a 'third time's the charm'-thing…

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/orxonox/overlays/notifications/NotificationManager.cc

    r2662 r2909  
    2727 */
    2828
     29/**
     30    @file NotificationManager.cc
     31    @brief Implementation of the NotificationManager class.
     32*/
     33
    2934#include "OrxonoxStableHeaders.h"
    3035#include "NotificationManager.h"
     
    3237#include "core/CoreIncludes.h"
    3338
     39#include <set>
     40
    3441#include "Notification.h"
    35 
    3642#include "NotificationQueue.h"
    3743
    3844namespace orxonox
    3945{
    40     std::list<NotificationContainer*> NotificationManager::notifications_s;
    41 
    42     NotificationManager::NotificationManager(BaseObject* creator) : BaseObject(creator)
    43     {
    44         RegisterObject(NotificationManager);
    45     }
    46 
     46
     47    const std::string NotificationManager::ALL = "all";
     48    const std::string NotificationManager::NONE = "none";
     49
     50    NotificationManager* NotificationManager::singletonRef_s = NULL;
     51
     52    /**
     53    @brief
     54        Constructor. Registers the Object.
     55    */
     56    NotificationManager::NotificationManager()
     57    {
     58        RegisterRootObject(NotificationManager);
     59
     60        assert(singletonRef_s == 0);
     61        singletonRef_s = this;
     62
     63        this->highestIndex_ = 0;
     64    }
     65
     66    /**
     67    @brief
     68        Destructor.
     69    */
    4770    NotificationManager::~NotificationManager()
    4871    {
    49         //TDO: Destroy the containers
    50     }
    51 
    52     void NotificationManager::tick(float dt)
    53     {
    54         bool update = false;
    55 
    56         for (std::list<NotificationContainer*>::iterator notification = notifications_s.begin(); notification != notifications_s.end(); ++notification)
    57         {
    58             NotificationContainer* container = *notification;
    59             if(container->remainingTime == 0)
     72    }
     73
     74    /**
     75    @brief
     76        Returns the current (and single) instance of the NotificationManager. Creates one, if there isn't one to begin with.
     77    @return
     78        Returns a reference to the single instance of the NotificationManager.
     79    */
     80    /*static*/ NotificationManager & NotificationManager::getInstance()
     81    {
     82        assert(singletonRef_s);
     83        return *singletonRef_s;
     84    }
     85   
     86    /**
     87    @brief
     88        Registers a Notification within the NotificationManager and makes sure that the Notification is displayed in all the NotificationQueues associated with its sender.
     89    @param notification
     90        The Notification to be registered.
     91    @return
     92        Returns true if successful.
     93    */
     94    bool NotificationManager::registerNotification(Notification* notification)
     95    {
     96   
     97        if(notification == NULL) //!< A NULL-Notification cannot be registered.
     98            return false;
     99       
     100        std::time_t time = std::time(0); //TDO: Doesn't this expire? //!< Get current time.
     101       
     102        this->allNotificationsList_.insert(std::pair<std::time_t,Notification*>(time,notification));
     103       
     104        if(notification->getSender() == NONE) //!< If the sender has no specific name, then the Notification is only added to the list of all Notifications.
     105            return true;
     106       
     107        bool all = false;
     108        if(notification->getSender() == ALL) //!< If all are the sender, then the Notifications is added to every NotificationQueue.
     109            all = true;
     110       
     111        //!< Insert the notification in all queues that have its sender as target.
     112        for(std::map<NotificationQueue*,int>::iterator it = this->queueList_.begin(); it != this->queueList_.end(); it++) //!< Iterate through all queues.
     113        {
     114            std::set<std::string> set = it->first->getTargetsSet();
     115            if(all || set.find(notification->getSender()) != set.end() || set.find(ALL) != set.end()) //TDO: Make sure this works.
    60116            {
    61                 continue;
     117                this->notificationLists_[it->second]->insert(std::pair<std::time_t,Notification*>(time,notification)); //!< Insert the Notification in the Notifications list of the current NotificationQueue.
     118                it->first->update(notification, time); //!< Update the queue.
    62119            }
    63             else if(container->remainingTime - dt <= 0)
     120        }
     121       
     122        COUT(3) << "Notification registered with the NotificationManager." << std::endl;
     123       
     124        return true;
     125    }
     126   
     127    /**
     128    @brief
     129        Registers a NotificationQueue within the NotificationManager.
     130    @param queue
     131        The NotificationQueue to be registered.
     132    @return
     133        Returns true if successful.
     134    */
     135    bool NotificationManager::registerQueue(NotificationQueue* queue)
     136    {
     137        this->highestIndex_ += 1;
     138        int index = this->highestIndex_;
     139       
     140        this->queueList_[queue] = index; //!< Add the NotificationQueue to the list of queues.
     141       
     142        std::set<std::string> set = queue->getTargetsSet(); //TDO: Works this?
     143       
     144        //! If all senders are the target of the queue, then the list of notification for that specific queue is te same as the list of all Notifications.
     145        if(set.find(ALL) != set.end())
     146        {
     147            this->notificationLists_[index] = &this->allNotificationsList_;
     148            COUT(3) << "NotificationQueue registered with the NotificationManager." << std::endl;
     149            return true;
     150        }
     151       
     152        this->notificationLists_[index] = new std::multimap<std::time_t,Notification*>;
     153        std::multimap<std::time_t,Notification*> map = *this->notificationLists_[index];
     154       
     155        //! Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationQueue.
     156        for(std::multimap<std::time_t,Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
     157        {
     158            if(set.find(it->second->getSender()) != set.end()) //!< Checks whether the overlay has the sender of the current notification as target.
    64159            {
    65                 container->remainingTime = 0;
    66                 update = true;
     160                map.insert(std::pair<std::time_t,Notification*>(it->first, it->second));
    67161            }
    68             else
    69             {
    70                 container->remainingTime = container->remainingTime -dt;
    71             }
    72         }
    73 
    74         if(update)
    75             updateQueue();
    76     }
    77 
    78     bool NotificationManager::insertNotification(Notification* notification)
    79     {
    80         if(notification == NULL)
     162        }
     163       
     164        queue->update(); //!< Update the queue.
     165
     166        COUT(3) << "NotificationQueue registered with the NotificationManager." << std::endl;
     167       
     168        return true;
     169    }
     170   
     171    /**
     172    @brief
     173        Fetches the Notifications for a specific NotificationQueue in a specified timeframe.
     174    @param queue
     175        The NotificationQueue the Notifications are fetched for.
     176    @param map
     177        A multimap, in which the notifications are stored.
     178    @param timeFrameStart
     179        The start time of the timeframe.
     180    @param timeFrameEnd
     181        The end time of the timeframe.
     182    @return
     183        Returns true if successful.
     184    */
     185    bool NotificationManager::getNotifications(NotificationQueue* queue, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd)
     186    {
     187        if(queue == NULL || map == NULL)
    81188            return false;
    82189
    83         NotificationContainer* container = new NotificationContainer;
    84         container->notification = notification;
    85         container->remainingTime = notification->getDisplayTime();
    86         notifications_s.push_front(container);
    87 
    88         updateQueue();
    89 
    90         COUT(4) << "Notification inserted. Title: " << notification->getTitle() << std::endl;
    91 
     190        std::multimap<std::time_t,Notification*>* notifications = this->notificationLists_[this->queueList_[queue]]; //!< The Notifications for the input NotificationQueue.
     191       
     192        if(notifications == NULL) //!< Returns NULL, if there are no Notifications.
     193            return true;
     194   
     195        std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest;
     196        itLowest = notifications->lower_bound(timeFrameStart);
     197        itHighest = notifications->upper_bound(timeFrameStart);
     198       
     199        for(it = itLowest; it != itHighest; it++) //!< Iterate through the Notifications from the start of the time Frame to the end of it.
     200        {
     201            map->insert(std::pair<std::time_t,Notification*>(it->first,it->second)); //!< Add the found Notifications to the map.
     202        }
     203       
    92204        return true;
    93205    }
    94206
    95     void NotificationManager::updateQueue(void)
    96     {
    97         std::string text = "";
    98 
    99         if (!NotificationQueue::queue_s)
    100             return;
    101 
    102         int i = NotificationQueue::queue_s->getLength();
    103         for (std::list<NotificationContainer*>::iterator notification = notifications_s.begin(); notification != notifications_s.end() && i > 0; ++notification)
    104         {
    105             i--;
    106             NotificationContainer* container = *notification;
    107             if(container->remainingTime == 0.0)
    108                 continue;
    109 
    110             text = text + "\n\n\n------------\n\n" + clipMessage(container->notification->getTitle()) + "\n\n" + clipMessage(container->notification->getMessage());
    111         }
    112 
    113         NotificationQueue::queue_s->setQueueText(text);
    114     }
    115 
    116     const std::string NotificationManager::clipMessage(const std::string & str)
    117     {
    118 
    119         std::string message = str;
    120         unsigned int i = 0;
    121 
    122         unsigned int found = message.find("\\n", i);
    123         while(found != std::string::npos)
    124         {
    125             message.replace(found, 2, "\n");
    126             i = found+2;
    127             found = message.find("\\n", i);
    128         }
    129 
    130         std::string clippedMessage = "";
    131         int wordLength = 0;
    132         i = 0;
    133         int widthLeft = NotificationQueue::queue_s->getWidth();
    134         while(i < message.length())
    135         {
    136             while(i < message.length() && message[i] != ' ' && message[i] != '\n')
    137             {
    138                 i++;
    139                 wordLength++;
    140             }
    141 
    142             if(wordLength <= widthLeft)
    143             {
    144                 clippedMessage = clippedMessage + message.substr(i-wordLength, wordLength);
    145                 if(i < message.length())
    146                 {
    147                     clippedMessage = clippedMessage + message.substr(i,1);
    148                 }
    149                 widthLeft -= (wordLength+1);
    150                 if(message[i] == '\n')
    151                 {
    152                     widthLeft = NotificationQueue::queue_s->getWidth() - (wordLength+1);
    153                 }
    154                 wordLength = 0;
    155                 i++;
    156             }
    157             else
    158             {
    159                 clippedMessage.push_back('\n');
    160                 clippedMessage = clippedMessage + message.substr(i-wordLength, wordLength);
    161                 if(i < message.length())
    162                 {
    163                     clippedMessage = clippedMessage + message.substr(i,1);
    164                 }
    165                 widthLeft = NotificationQueue::queue_s->getWidth() - (wordLength+1);
    166                 i++;
    167                 wordLength = 0;
    168             }
    169         }
    170 
    171         return clippedMessage;
    172     }
    173 
    174207}
Note: See TracChangeset for help on using the changeset viewer.