[8446] | 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 NotificationQueueCEGUI.cc |
---|
| 31 | @brief Implementation of the NotificationQueueCEGUI class. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "NotificationQueueCEGUI.h" |
---|
| 35 | |
---|
[8448] | 36 | #include <sstream> |
---|
| 37 | |
---|
[8446] | 38 | #include "core/CoreIncludes.h" |
---|
| 39 | #include "core/GameMode.h" |
---|
| 40 | #include "core/GUIManager.h" |
---|
| 41 | #include "core/LuaState.h" |
---|
| 42 | #include "util/Convert.h" |
---|
| 43 | |
---|
[8448] | 44 | #include "ToluaBindNotifications.h" |
---|
| 45 | |
---|
[8446] | 46 | namespace orxonox |
---|
| 47 | { |
---|
| 48 | |
---|
[8448] | 49 | // Register tolua_open function when loading the library. |
---|
| 50 | DeclareToluaInterface(Notifications); |
---|
| 51 | |
---|
[8446] | 52 | NotificationQueueCEGUI::NotificationQueueCEGUI(const std::string& name, const std::string& senders, unsigned int size, unsigned int displayTime) : NotificationQueue(name, senders, size, displayTime) |
---|
| 53 | { |
---|
| 54 | RegisterObject(NotificationQueueCEGUI); |
---|
[8448] | 55 | |
---|
| 56 | this->displaySize_ = Vector4(1.0, 0.0, 0.0, 0.0); |
---|
| 57 | this->position_ = Vector4(0.0, 0.0, 0.0, 0.0); |
---|
| 58 | this->alignment_ = "LeftAligned"; |
---|
| 59 | this->fontSize_ = 12; |
---|
| 60 | this->fontColor_ = Vector4(1.0, 1.0, 1.0, 1.0); |
---|
| 61 | this->fontColorStr_ = "FFFFFFFF"; |
---|
[8446] | 62 | |
---|
| 63 | // Create the NotificationQueueCEGUI in lua. |
---|
| 64 | this->create(); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | NotificationQueueCEGUI::~NotificationQueueCEGUI() |
---|
| 68 | { |
---|
| 69 | |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | @brief |
---|
| 74 | Destroys the NotificationQueueCEGUI. |
---|
| 75 | Used in lua and NotificationManager. |
---|
| 76 | @param noGraphics |
---|
| 77 | If this is set to true (false is default), then the queue is not removed in lua. This is used to destroy the queue, after the GUIManager has been destroyed. |
---|
| 78 | */ |
---|
| 79 | void NotificationQueueCEGUI::destroy(bool noGraphics) |
---|
| 80 | { |
---|
| 81 | // Remove the NotificationQueue in lua. |
---|
| 82 | if(GameMode::showsGraphics() && !noGraphics) |
---|
| 83 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeQueue(\"" + this->getName() + "\")"); |
---|
| 84 | |
---|
| 85 | NotificationQueue::destroy(); |
---|
| 86 | } |
---|
[8448] | 87 | |
---|
[8446] | 88 | /** |
---|
| 89 | @brief |
---|
[8448] | 90 | Set the size of the window that displays the NotificationQueue. |
---|
| 91 | @param size |
---|
| 92 | The size is a vector with components: |
---|
| 93 | - The relative width of the window. (A value between 0 and 1) |
---|
| 94 | - The absolute width in pixels. (Additional to the relative width, can be negative) |
---|
| 95 | - The relative height of the window. (A value between 0 and 1) |
---|
| 96 | - The absolute height in pixels. (Additional to the relative width, can be negative.) |
---|
| 97 | If both the 3rd and 4th component of size are set to 0 the height is set such that exactly as many Notifications fit as is the maximum size of the NotificationQueue (in terms of the number of Notifications). |
---|
| 98 | */ |
---|
| 99 | void NotificationQueueCEGUI::setDisplaySize(const Vector4& size) |
---|
| 100 | { |
---|
| 101 | if(this->displaySize_ == size) |
---|
| 102 | return; |
---|
| 103 | |
---|
| 104 | if(size.x < 0.0 || size.x > 1.0 || size.z < 0.0 || size.z > 1.0) |
---|
| 105 | { |
---|
| 106 | COUT(2) << "The display size of the NotificationQueueCEGUI " << this->getName() << " was trying to be set, but the relative size was not in [0,1]. Aborting..." << endl; |
---|
| 107 | return; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | this->displaySize_ = size; |
---|
| 111 | if(size.z == 0.0 && size.w == 0.0) |
---|
| 112 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.resizeQueue(\"" + this->getName() + "\", " + multi_cast<std::string>(this->displaySize_.x) + ", " + multi_cast<std::string>(this->displaySize_.y) + ")"); |
---|
| 113 | else |
---|
| 114 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.resizeQueue(\"" + this->getName() + "\", " + multi_cast<std::string>(this->displaySize_.x) + ", " + multi_cast<std::string>(this->displaySize_.y) + ", " + multi_cast<std::string>(this->displaySize_.z) + ", " + multi_cast<std::string>(this->displaySize_.w) + ")"); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | @brief |
---|
| 119 | Set the position of the window that displays the NotificationQueue. |
---|
| 120 | @param position |
---|
| 121 | The position is a vector with components: |
---|
| 122 | - The relative x-position of the window. (A value between 0 and 1) |
---|
| 123 | - The absolute x-position in pixels. (Additional to the relative x-position, can be negative) |
---|
| 124 | - The relative y-position of the window. (A value between 0 and 1) |
---|
| 125 | - The absolute y-position in pixels. (Additional to the relative y-position, can be negative.) |
---|
| 126 | */ |
---|
| 127 | void NotificationQueueCEGUI::setPosition(const Vector4& position) |
---|
| 128 | { |
---|
| 129 | if(this->position_ == position) |
---|
| 130 | return; |
---|
| 131 | |
---|
| 132 | if(position.x < 0.0 || position.x > 1.0 || position.z < 0.0 || position.z > 1.0) |
---|
| 133 | { |
---|
| 134 | COUT(2) << "The position the NotificationQueueCEGUI " << this->getName() << " was trying to be set, but the relative position was not in [0,1]. Aborting..." << endl; |
---|
| 135 | return; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | this->position_ = position; |
---|
| 139 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.moveQueue(\"" + this->getName() + "\", " + multi_cast<std::string>(this->position_.x) + ", " + multi_cast<std::string>(this->position_.y) + ", " + multi_cast<std::string>(this->position_.z) + ", " + multi_cast<std::string>(this->position_.w) + ")"); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
| 143 | @brief |
---|
| 144 | Set the horizontal alignment of the Notifications text. |
---|
| 145 | @param alignment |
---|
| 146 | The alignment of the Notifications, they are the possible string that the CEGUI Falagard StaticText HorzFormatting property can take. |
---|
| 147 | @see http://cegui.org.uk/api_reference/classCEGUI_1_1FalagardStaticTextProperties_1_1HorzFormatting.html |
---|
| 148 | */ |
---|
| 149 | void NotificationQueueCEGUI::setAlignment(const std::string& alignment) |
---|
| 150 | { |
---|
| 151 | if(this->alignment_ == alignment) |
---|
| 152 | return; |
---|
| 153 | |
---|
| 154 | // TODO: Check whether the alignment string is correct? |
---|
| 155 | this->alignment_ = alignment; |
---|
| 156 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.changeQueueAlignment(\"" + this->getName() + "\", \"" + this->alignment_ + "\")"); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
| 160 | @brief |
---|
| 161 | Set the font size of the text displayed by this NotificationQueue. |
---|
| 162 | @param size |
---|
| 163 | The font size. |
---|
| 164 | */ |
---|
| 165 | void NotificationQueueCEGUI::setFontSize(unsigned int size) |
---|
| 166 | { |
---|
| 167 | if(this->fontSize_ == size) |
---|
| 168 | return; |
---|
| 169 | |
---|
| 170 | this->fontSize_ = size; |
---|
| 171 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.changeQueueFontSize(\"" + this->getName() + "\", " + multi_cast<std::string>(this->fontSize_) + ")"); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | @brief |
---|
| 176 | Set the font color if the text displayed by this NotificationQueue. |
---|
| 177 | @param color |
---|
| 178 | The color is a vector with the components being RGBA and taking values from 0 to 1. |
---|
| 179 | */ |
---|
| 180 | void NotificationQueueCEGUI::setFontColor(const Vector4& color) |
---|
| 181 | { |
---|
| 182 | if(this->fontColor_ == color) |
---|
| 183 | return; |
---|
| 184 | |
---|
| 185 | this->fontColor_ = color; |
---|
| 186 | // Convert to ARGB format. |
---|
| 187 | std::stringstream stream; |
---|
| 188 | for(unsigned int i = 0; i < 4; i++) |
---|
| 189 | stream << std::hex << std::setw(2) << std::setfill('0') << int(this->fontColor_[(i+3)%4]*255); |
---|
| 190 | this->fontColorStr_ = stream.str(); |
---|
| 191 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.changeQueueFontColor(\"" + this->getName() + "\", \"" + this->fontColorStr_ + "\")"); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | /** |
---|
| 195 | @brief |
---|
| 196 | Get the NotificationQueueCEGUI with the input name. |
---|
| 197 | @param name |
---|
| 198 | The name of the NotificationQueueCEGUI to be got. |
---|
| 199 | @return |
---|
| 200 | Returns a pointer to the NotificationQueueCEGUI, or NULL if it doesn't exist. |
---|
| 201 | */ |
---|
| 202 | /*static*/ NotificationQueueCEGUI* NotificationQueueCEGUI::getQueue(const std::string& name) |
---|
| 203 | { |
---|
| 204 | NotificationQueue* queue = NotificationManager::getInstance().getQueue(name); |
---|
| 205 | if(queue == NULL || !queue->isA(Class(NotificationQueueCEGUI))) |
---|
| 206 | return NULL; |
---|
| 207 | return static_cast<NotificationQueueCEGUI*>(queue); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | /** |
---|
| 211 | @brief |
---|
[8446] | 212 | Is called by the NotificationQueue when a notification was pushed. |
---|
| 213 | @param notification |
---|
| 214 | The Notification that was pushed. |
---|
| 215 | */ |
---|
| 216 | void NotificationQueueCEGUI::notificationPushed(Notification* notification) |
---|
| 217 | { |
---|
| 218 | // Push the Notification to the GUI. |
---|
| 219 | if(GameMode::showsGraphics()) |
---|
| 220 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.pushNotification(\"" + this->getName() + "\", \"" + notification->getMessage() + "\")"); |
---|
| 221 | } |
---|
[8448] | 222 | |
---|
[8446] | 223 | /** |
---|
| 224 | @brief |
---|
| 225 | Is called by the NotificationQueue when a notification was popped. |
---|
| 226 | */ |
---|
| 227 | void NotificationQueueCEGUI::notificationPopped(void) |
---|
| 228 | { |
---|
| 229 | // Pops the Notification from the GUI. |
---|
| 230 | if(GameMode::showsGraphics()) |
---|
| 231 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.popNotification(\"" + this->getName() + "\")"); |
---|
| 232 | } |
---|
[8448] | 233 | |
---|
[8446] | 234 | /** |
---|
| 235 | @brief Is called when a notification was removed. |
---|
| 236 | @param index The index the removed notification was at. |
---|
| 237 | */ |
---|
| 238 | void NotificationQueueCEGUI::notificationRemoved(unsigned int index) |
---|
| 239 | { |
---|
| 240 | // Removes the Notification from the GUI. |
---|
| 241 | if(GameMode::showsGraphics()) |
---|
| 242 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeNotification(\"" + this->getName() + "\", " + multi_cast<std::string>(index) + ")"); |
---|
| 243 | } |
---|
[8448] | 244 | |
---|
[8446] | 245 | /** |
---|
| 246 | @brief |
---|
| 247 | Clears the NotificationQueue by removing all NotificationContainers. |
---|
| 248 | @param noGraphics |
---|
| 249 | If this is set to true the GUI is not informed of the clearing of the NotificationQueue. This is needed only internally. |
---|
| 250 | */ |
---|
| 251 | void NotificationQueueCEGUI::clear(bool noGraphics) |
---|
| 252 | { |
---|
| 253 | NotificationQueue::clear(noGraphics); |
---|
[8448] | 254 | |
---|
[8446] | 255 | // Clear the NotificationQueue in the GUI. |
---|
| 256 | if(GameMode::showsGraphics() && !noGraphics) |
---|
| 257 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.clearQueue(\"" + this->getName() + "\")"); |
---|
| 258 | } |
---|
[8448] | 259 | |
---|
[8446] | 260 | /** |
---|
| 261 | @brief |
---|
| 262 | Creates the NotificationQueue in lua. |
---|
| 263 | */ |
---|
| 264 | void NotificationQueueCEGUI::create(void) |
---|
| 265 | { |
---|
| 266 | if(GameMode::showsGraphics()) |
---|
| 267 | GUIManager::getInstance().getLuaState()->doString("NotificationLayer.createQueue(\"" + this->getName() + "\", " + multi_cast<std::string>(this->getMaxSize()) + ")"); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | } |
---|
| 271 | |
---|