Changeset 2779 for code/branches/questsystem5/src/orxonox/overlays/notifications/NotificationManager.cc
- Timestamp:
- Mar 12, 2009, 5:13:34 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/questsystem5/src/orxonox/overlays/notifications/NotificationManager.cc
r2662 r2779 27 27 */ 28 28 29 /** 30 @file NotificationManager.cc 31 @brief Implementation of the NotificationManager class. 32 */ 33 29 34 #include "OrxonoxStableHeaders.h" 30 35 #include "NotificationManager.h" … … 32 37 #include "core/CoreIncludes.h" 33 38 39 #include <set> 40 34 41 #include "Notification.h" 35 36 42 #include "NotificationQueue.h" 37 43 38 44 namespace orxonox 39 45 { 40 std::list<NotificationContainer*> NotificationManager::notifications_s; 41 46 47 const std::string NotificationManager::ALL = "all"; 48 const std::string NotificationManager::NONE = "none"; 49 50 std::multimap<std::time_t,Notification*> NotificationManager::allNotificationsList_s; 51 std::map<int,std::multimap<std::time_t,Notification*>*> NotificationManager::notificationLists_s; 52 std::map<NotificationQueue*,int> NotificationManager::queueList_s; 53 54 int NotificationManager::highestIndex_s = 0; 55 56 /** 57 @brief 58 Constructor. Registers the Object. 59 */ 42 60 NotificationManager::NotificationManager(BaseObject* creator) : BaseObject(creator) 43 61 { … … 45 63 } 46 64 65 /** 66 @brief 67 Destructor. 68 */ 47 69 NotificationManager::~NotificationManager() 48 70 { 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) 71 } 72 73 /** 74 @brief 75 Registers a Notification within the NotificationManager and makes sure that the Notification is displayed in all the NotificationQueues associated with its sender. 76 @param notification 77 The Notification to be registered. 78 @return 79 Returns true if successful. 80 */ 81 /*static*/ bool NotificationManager::registerNotification(Notification* notification) 82 { 83 84 if(notification == NULL) //!< A NULL-Notification cannot be registered. 85 return false; 86 87 std::time_t time = std::time(0); //TDO: Doesn't this expire? //!< Get current time. 88 89 allNotificationsList_s.insert(std::pair<std::time_t,Notification*>(time,notification)); 90 91 if(notification->getSender() == NONE) //!< If the sender has no specific name, then the Notification is only added to the list of all Notifications. 92 return true; 93 94 bool all = false; 95 if(notification->getSender() == ALL) //!< If all are the sender, then the Notifications is added to every NotificationQueue. 96 all = true; 97 98 //!< Insert the notification in all queues that have its sender as target. 99 for(std::map<NotificationQueue*,int>::iterator it = queueList_s.begin(); it != queueList_s.end(); it++) 100 { 101 std::set<std::string> set = it->first->getTargetsSet(); 102 if(all || set.find(notification->getSender()) != set.end() || set.find(ALL) != set.end()) //TDO: Make sure this works. 60 103 { 61 continue; 104 notificationLists_s[it->second]->insert(std::pair<std::time_t,Notification*>(time,notification)); //!< Insert the Notification in the Notifications list of the current NotificationQueue. 105 it->first->update(notification, time); //!< Update the queue. 62 106 } 63 else if(container->remainingTime - dt <= 0) 107 } 108 109 COUT(3) << "NotificationQueue registered with the NotificationManager." << std::endl; 110 111 return true; 112 } 113 114 /** 115 @brief 116 Registers a NotificationQueue within the NotificationManager. 117 @param queue 118 The NotificationQueue to be registered. 119 @return 120 Returns true if successful. 121 */ 122 /*static*/ bool NotificationManager::registerQueue(NotificationQueue* queue) 123 { 124 NotificationManager::highestIndex_s += 1; 125 int index = NotificationManager::highestIndex_s; 126 127 queueList_s[queue] = index; //!< Add the NotificationQueue to the list of queues. 128 129 std::set<std::string> set = queue->getTargetsSet(); //TDO: Works this? 130 131 //! 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. 132 if(set.find(ALL) != set.end()) 133 { 134 notificationLists_s[index] = &allNotificationsList_s; 135 return true; 136 } 137 138 notificationLists_s[index] = new std::multimap<std::time_t,Notification*>; 139 std::multimap<std::time_t,Notification*> map = *notificationLists_s[index]; 140 141 //! Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationQueue. 142 for(std::multimap<std::time_t,Notification*>::iterator it = allNotificationsList_s.begin(); it != allNotificationsList_s.end(); it++) 143 { 144 if(set.find(it->second->getSender()) != set.end()) //!< Checks whether the overlay has the sender of the current notification as target. 64 145 { 65 container->remainingTime = 0; 66 update = true; 146 map.insert(std::pair<std::time_t,Notification*>(it->first, it->second)); 67 147 } 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) 81 return false; 82 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 148 } 149 150 COUT(3) << "Notification registered with the NotificationManager." << std::endl; 151 152 queue->update(); //!< Update the queue. 153 92 154 return true; 93 155 } 94 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; 156 157 /** 158 @brief 159 Fetches the Notifications for a specific NotificationQueue in a specified timeframe. 160 @param queue 161 The NotificationQueue the Notifications are fetched for. 162 @param timeFrameStart 163 The start time of the timeframe. 164 @param timeFrameEnd 165 The end time of the timeframe. 166 @return 167 Returns a time-ordered list of Notifications. 168 @todo 169 Make sure the map is deleted. 170 */ 171 /*static*/ std::multimap<std::time_t,Notification*>* NotificationManager::getNotifications(NotificationQueue* queue, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd) 172 { 173 COUT(1) << "Queue: " << queue << ", timeFrameStart: " << timeFrameStart << ", timeFrameEnd: " << timeFrameEnd << std::endl; 174 175 std::multimap<std::time_t,Notification*>* notifications = NotificationManager::notificationLists_s[NotificationManager::queueList_s[queue]]; 176 177 if(notifications == NULL) 178 return NULL; 179 180 std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest; 181 itLowest = notifications->lower_bound(timeFrameStart); 182 itHighest = notifications->upper_bound(timeFrameStart); 183 184 std::multimap<std::time_t,Notification*>* map = new std::multimap<std::time_t,Notification*>(); 185 186 for(it = itLowest; it != itHighest; it++) 187 { 188 map->insert(std::pair<std::time_t,Notification*>(it->first,it->second)); 189 } 190 191 if(map->size() == 0) 192 { 193 delete map; 194 return NULL; 195 } 196 197 return map; 172 198 } 173 199
Note: See TracChangeset
for help on using the changeset viewer.