| 1 | /* |
|---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
|---|
| 3 | * > www.orxonox.net < |
|---|
| 4 | * |
|---|
| 5 | * |
|---|
| 6 | * License notice: |
|---|
| 7 | * |
|---|
| 8 | * This program is free software; you can redistribute it and/or |
|---|
| 9 | * modify it under the terms of the GNU General Public License |
|---|
| 10 | * as published by the Free Software Foundation; either version 2 |
|---|
| 11 | * of the License, or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | * GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with this program; if not, write to the Free Software |
|---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 21 | * |
|---|
| 22 | * Author: |
|---|
| 23 | * Damian 'Mozork' Frick |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * ... |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | @file NotificationQueue.h |
|---|
| 31 | @brief Definition of the NotificationQueue class. |
|---|
| 32 | @ingroup Notifications |
|---|
| 33 | */ |
|---|
| 34 | |
|---|
| 35 | #ifndef _NotificationOueue_H__ |
|---|
| 36 | #define _NotificationOueue_H__ |
|---|
| 37 | |
|---|
| 38 | #include "notifications/NotificationsPrereqs.h" |
|---|
| 39 | |
|---|
| 40 | #include <ctime> |
|---|
| 41 | #include <set> |
|---|
| 42 | #include <string> |
|---|
| 43 | #include <vector> |
|---|
| 44 | |
|---|
| 45 | #include "NotificationManager.h" |
|---|
| 46 | |
|---|
| 47 | #include "tools/interfaces/Tickable.h" |
|---|
| 48 | |
|---|
| 49 | namespace orxonox |
|---|
| 50 | { |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | @brief |
|---|
| 54 | Container to allow easy handling of the @ref orxonox::Notification "Notifications". |
|---|
| 55 | |
|---|
| 56 | @ingroup Notifications |
|---|
| 57 | */ |
|---|
| 58 | struct NotificationContainer |
|---|
| 59 | { |
|---|
| 60 | Notification* notification; //!< The Notification displayed. |
|---|
| 61 | time_t time; //!< The time the Notification was sent and thus first displayed. |
|---|
| 62 | }; |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | @brief |
|---|
| 66 | Struct to allow ordering of @ref orxonox::NotificationContainer "NotificationContainers". |
|---|
| 67 | |
|---|
| 68 | @ingroup Notifications |
|---|
| 69 | */ |
|---|
| 70 | struct NotificationContainerCompare { |
|---|
| 71 | bool operator() (const NotificationContainer* const & a, const NotificationContainer* const & b) const |
|---|
| 72 | { return a->time < b->time; } //!< Ordering by time. |
|---|
| 73 | }; |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | @brief |
|---|
| 77 | Displays @ref orxonox::Notification "Notifications" from specific senders. |
|---|
| 78 | |
|---|
| 79 | There are quite some parameters that influence the behavior of the NotificationQueue: |
|---|
| 80 | - @b name The name of the NotificationQueue. It needs to be unique. |
|---|
| 81 | - @b senders The senders that are targets of this NotificationQueue, i.e. the names of senders whose Notifications this NotificationQueue displays. |
|---|
| 82 | - @b size The size of the NotificationQueue, it specifies how many @ref orxonox::Notification "Notifications" are displayed at once at the most. |
|---|
| 83 | - @b displayTime The time a @ref orxonox::Notification "Notification" is displayed with this NotificationQueue. |
|---|
| 84 | |
|---|
| 85 | @author |
|---|
| 86 | Damian 'Mozork' Frick |
|---|
| 87 | |
|---|
| 88 | @ingroup Notifications |
|---|
| 89 | */ |
|---|
| 90 | class _NotificationsExport NotificationQueue : public Tickable |
|---|
| 91 | { |
|---|
| 92 | |
|---|
| 93 | public: |
|---|
| 94 | NotificationQueue(const std::string& name, const std::string& senders = NotificationListener::ALL, unsigned int size = NotificationQueue::DEFAULT_SIZE, unsigned int displayTime = NotificationQueue::DEFAULT_DISPLAY_TIME); |
|---|
| 95 | virtual ~NotificationQueue(); |
|---|
| 96 | |
|---|
| 97 | virtual void tick(float dt); //!< To update from time to time. |
|---|
| 98 | |
|---|
| 99 | void update(void); //!< Updates the NotificationQueue. |
|---|
| 100 | void update(Notification* notification, const std::time_t & time); //!< Updates the NotificationQueue by adding an new Notification. |
|---|
| 101 | |
|---|
| 102 | // tolua_begin |
|---|
| 103 | /** |
|---|
| 104 | @brief Get the name of the NotificationQueue. |
|---|
| 105 | @return Returns the name. |
|---|
| 106 | */ |
|---|
| 107 | inline const std::string& getName() const |
|---|
| 108 | { return this->name_; } |
|---|
| 109 | |
|---|
| 110 | void setMaxSize(unsigned int size); //!< Sets the maximum number of displayed Notifications. |
|---|
| 111 | /** |
|---|
| 112 | @brief Returns the maximum number of Notifications displayed. |
|---|
| 113 | @return Returns maximum size. |
|---|
| 114 | */ |
|---|
| 115 | inline unsigned int getMaxSize() const |
|---|
| 116 | { return this->maxSize_; } |
|---|
| 117 | |
|---|
| 118 | void setDisplayTime(int time); //!< Sets the maximum number of seconds a Notification is displayed. |
|---|
| 119 | /** |
|---|
| 120 | @brief Returns the time interval the Notification is displayed. |
|---|
| 121 | @return Returns the display time. |
|---|
| 122 | */ |
|---|
| 123 | inline int getDisplayTime() const |
|---|
| 124 | { return this->displayTime_; } |
|---|
| 125 | // tolua_end |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | @brief Returns the current number of Notifications displayed. |
|---|
| 129 | @return Returns the size of the NotificationQueue. |
|---|
| 130 | */ |
|---|
| 131 | inline unsigned int getSize() const |
|---|
| 132 | { return this->size_; } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | @brief Returns the targets of this NotificationQueue, reps. the senders which Notifications are displayed in this NotificationQueue. |
|---|
| 136 | @return Returns a set of strings holding the different targets. |
|---|
| 137 | */ |
|---|
| 138 | inline const std::set<std::string> & getTargetsSet() |
|---|
| 139 | { return this->targets_; } |
|---|
| 140 | |
|---|
| 141 | void setTargets(const std::string & targets); //!< Set the targets of this NotificationQueue. |
|---|
| 142 | const std::string& getTargets(void) const; //!< Returns a string consisting of the concatenation of the targets. |
|---|
| 143 | |
|---|
| 144 | bool tidy(void); // Pops all Notifications from the NotificationQueue. |
|---|
| 145 | |
|---|
| 146 | protected: |
|---|
| 147 | /** |
|---|
| 148 | @brief Is called when a notification was pushed. |
|---|
| 149 | @param notification The Notification that was pushed. |
|---|
| 150 | */ |
|---|
| 151 | virtual void notificationPushed(Notification* notification) {} |
|---|
| 152 | /** |
|---|
| 153 | @brief Is called when a notification was popped. |
|---|
| 154 | */ |
|---|
| 155 | virtual void notificationPopped(void) {} |
|---|
| 156 | /** |
|---|
| 157 | @brief Is called when a notification was removed. |
|---|
| 158 | @param index The index the removed notification was at. |
|---|
| 159 | */ |
|---|
| 160 | virtual void notificationRemoved(unsigned int index) {} |
|---|
| 161 | |
|---|
| 162 | virtual void clear(bool noGraphics = false); //!< Clears the NotificationQueue by removing all NotificationContainers. |
|---|
| 163 | |
|---|
| 164 | protected: |
|---|
| 165 | static const unsigned int DEFAULT_SIZE = 5; //!< The default maximum number of Notifications displayed. |
|---|
| 166 | static const unsigned int DEFAULT_DISPLAY_TIME = 30; //!< The default display time. |
|---|
| 167 | static const int INF = -1; //!< Constant denoting infinity. |
|---|
| 168 | |
|---|
| 169 | private: |
|---|
| 170 | std::string name_; //!< The name of the NotificationQueue. |
|---|
| 171 | |
|---|
| 172 | unsigned int maxSize_; //!< The maximal number of Notifications displayed. |
|---|
| 173 | unsigned int size_; //!< The number of Notifications displayed. |
|---|
| 174 | int displayTime_; //!< The time a Notification is displayed. |
|---|
| 175 | |
|---|
| 176 | bool registered_; //!< Helper variable to remember whether the NotificationQueue is registered already. |
|---|
| 177 | |
|---|
| 178 | std::set<std::string> targets_; //!< The targets the NotificationQueue displays Notifications of. |
|---|
| 179 | |
|---|
| 180 | std::multiset<NotificationContainer*, NotificationContainerCompare> ordering_; //!< The NotificationContainers ordered by the time they were registered. |
|---|
| 181 | std::vector<NotificationContainer*> notifications_; //!< The NotificationContainers in the order they were added to the NotificationQueue. |
|---|
| 182 | |
|---|
| 183 | float tickTime_; //!< Helper variable, to not have to check for Notifications that have been displayed too long, every tick. |
|---|
| 184 | NotificationContainer timeLimit_; //!< Helper object to check against to determine whether Notifications have expired. |
|---|
| 185 | |
|---|
| 186 | void setName(const std::string& name); //!< Sets the name of the NotificationQueue. |
|---|
| 187 | |
|---|
| 188 | void push(Notification* notification, const std::time_t & time); //!< Adds (pushes) a Notification to the NotificationQueue. |
|---|
| 189 | void pop(void); //!< Removes (pops) the least recently added Notification form the NotificationQueue. |
|---|
| 190 | void remove(const std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator& containerIterator); //!< Removes the Notification that is stored in the input NotificationContainer. |
|---|
| 191 | |
|---|
| 192 | }; |
|---|
| 193 | |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | #endif /* _NotificationQueue_H__ */ |
|---|