[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" |
---|
[8659] | 42 | #include "core/XMLPort.h" |
---|
[8446] | 43 | #include "util/Convert.h" |
---|
| 44 | |
---|
[8448] | 45 | #include "ToluaBindNotifications.h" |
---|
| 46 | |
---|
[8446] | 47 | namespace orxonox |
---|
| 48 | { |
---|
| 49 | |
---|
[8636] | 50 | CreateFactory(NotificationQueueCEGUI); |
---|
| 51 | |
---|
[8448] | 52 | // Register tolua_open function when loading the library. |
---|
| 53 | DeclareToluaInterface(Notifications); |
---|
| 54 | |
---|
[8450] | 55 | /*static*/ const std::string NotificationQueueCEGUI::NOTIFICATION_LAYER("NotificationLayer"); |
---|
| 56 | |
---|
[8636] | 57 | NotificationQueueCEGUI::NotificationQueueCEGUI(BaseObject* creator) : NotificationQueue(creator) |
---|
[8446] | 58 | { |
---|
| 59 | RegisterObject(NotificationQueueCEGUI); |
---|
[8448] | 60 | |
---|
[8636] | 61 | this->initialize(); |
---|
[8651] | 62 | this->registerVariables(); |
---|
[8636] | 63 | } |
---|
| 64 | |
---|
| 65 | NotificationQueueCEGUI::~NotificationQueueCEGUI() |
---|
| 66 | { |
---|
[8662] | 67 | if(this->isRegistered() && GameMode::showsGraphics()) |
---|
[8636] | 68 | GUIManager::getInstance().getLuaState()->doString(NotificationQueueCEGUI::NOTIFICATION_LAYER + ".removeQueue(\"" + this->getName() + "\")"); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | @brief |
---|
| 73 | Initializes The NotificationQueueCEGUI. |
---|
| 74 | */ |
---|
| 75 | void NotificationQueueCEGUI::initialize(void) |
---|
| 76 | { |
---|
[8448] | 77 | this->displaySize_ = Vector4(1.0, 0.0, 0.0, 0.0); |
---|
| 78 | this->position_ = Vector4(0.0, 0.0, 0.0, 0.0); |
---|
| 79 | this->alignment_ = "LeftAligned"; |
---|
| 80 | this->fontSize_ = 12; |
---|
| 81 | this->fontColor_ = Vector4(1.0, 1.0, 1.0, 1.0); |
---|
| 82 | this->fontColorStr_ = "FFFFFFFF"; |
---|
[8446] | 83 | } |
---|
[8636] | 84 | |
---|
| 85 | void NotificationQueueCEGUI::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[8446] | 86 | { |
---|
[8636] | 87 | SUPER(NotificationQueueCEGUI, XMLPort, xmlelement, mode); |
---|
| 88 | |
---|
| 89 | XMLPortParam(NotificationQueueCEGUI, "position", setPosition, getPosition, xmlelement, mode); |
---|
| 90 | XMLPortParam(NotificationQueueCEGUI, "fontSize", setFontSize, getFontSize, xmlelement, mode); |
---|
| 91 | XMLPortParam(NotificationQueueCEGUI, "fontColor", setFontColor, getFontColor, xmlelement, mode); |
---|
| 92 | XMLPortParam(NotificationQueueCEGUI, "alignment", setAlignment, getAlignment, xmlelement, mode); |
---|
| 93 | XMLPortParam(NotificationQueueCEGUI, "displaySize", setDisplaySize, getDisplaySize, xmlelement, mode); |
---|
[8446] | 94 | } |
---|
[8651] | 95 | |
---|
| 96 | void NotificationQueueCEGUI::registerVariables() |
---|
| 97 | { |
---|
[8656] | 98 | registerVariable( this->position_, VariableDirection::ToClient, new NetworkCallback<NotificationQueueCEGUI>(this, &NotificationQueueCEGUI::positionChanged)); |
---|
| 99 | registerVariable( this->fontSize_, VariableDirection::ToClient, new NetworkCallback<NotificationQueueCEGUI>(this, &NotificationQueueCEGUI::fontSizeChanged)); |
---|
| 100 | registerVariable( this->fontColor_, VariableDirection::ToClient, new NetworkCallback<NotificationQueueCEGUI>(this, &NotificationQueueCEGUI::fontColorChanged)); |
---|
| 101 | registerVariable( this->alignment_, VariableDirection::ToClient, new NetworkCallback<NotificationQueueCEGUI>(this, &NotificationQueueCEGUI::alignmentChanged)); |
---|
| 102 | registerVariable( this->displaySize_, VariableDirection::ToClient, new NetworkCallback<NotificationQueueCEGUI>(this, &NotificationQueueCEGUI::displaySizeChanged)); |
---|
[8651] | 103 | } |
---|
[8446] | 104 | |
---|
[8662] | 105 | void NotificationQueueCEGUI::changedName(void) |
---|
| 106 | { |
---|
| 107 | SUPER(NotificationQueueCEGUI, changedName); |
---|
| 108 | |
---|
| 109 | this->positionChanged(); |
---|
| 110 | this->fontSizeChanged(); |
---|
| 111 | this->fontColorChanged(); |
---|
| 112 | this->alignmentChanged(); |
---|
| 113 | this->displaySizeChanged(); |
---|
| 114 | } |
---|
| 115 | |
---|
[8446] | 116 | /** |
---|
| 117 | @brief |
---|
| 118 | Destroys the NotificationQueueCEGUI. |
---|
| 119 | Used in lua and NotificationManager. |
---|
| 120 | @param noGraphics |
---|
| 121 | 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. |
---|
| 122 | */ |
---|
| 123 | void NotificationQueueCEGUI::destroy(bool noGraphics) |
---|
| 124 | { |
---|
| 125 | // Remove the NotificationQueue in lua. |
---|
[8662] | 126 | if(this->isRegistered() && GameMode::showsGraphics() && !noGraphics) |
---|
[8450] | 127 | GUIManager::getInstance().getLuaState()->doString(NotificationQueueCEGUI::NOTIFICATION_LAYER + ".removeQueue(\"" + this->getName() + "\")"); |
---|
[8446] | 128 | |
---|
| 129 | NotificationQueue::destroy(); |
---|
| 130 | } |
---|
[8448] | 131 | |
---|
[8446] | 132 | /** |
---|
| 133 | @brief |
---|
[8448] | 134 | Set the size of the window that displays the NotificationQueue. |
---|
| 135 | @param size |
---|
| 136 | The size is a vector with components: |
---|
| 137 | - The relative width of the window. (A value between 0 and 1) |
---|
| 138 | - The absolute width in pixels. (Additional to the relative width, can be negative) |
---|
| 139 | - The relative height of the window. (A value between 0 and 1) |
---|
| 140 | - The absolute height in pixels. (Additional to the relative width, can be negative.) |
---|
| 141 | 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). |
---|
| 142 | */ |
---|
| 143 | void NotificationQueueCEGUI::setDisplaySize(const Vector4& size) |
---|
| 144 | { |
---|
| 145 | if(this->displaySize_ == size) |
---|
| 146 | return; |
---|
| 147 | |
---|
| 148 | if(size.x < 0.0 || size.x > 1.0 || size.z < 0.0 || size.z > 1.0) |
---|
| 149 | { |
---|
| 150 | 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; |
---|
| 151 | return; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | this->displaySize_ = size; |
---|
[8656] | 155 | this->displaySizeChanged(); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | /** |
---|
| 159 | @brief |
---|
| 160 | Is called when the display size has changed. |
---|
| 161 | */ |
---|
| 162 | void NotificationQueueCEGUI::displaySizeChanged(void) |
---|
| 163 | { |
---|
[8662] | 164 | if(this->isRegistered() && GameMode::showsGraphics()) |
---|
[8640] | 165 | { |
---|
[8656] | 166 | if(this->displaySize_.z == 0.0 && this->displaySize_.w == 0.0) |
---|
[8640] | 167 | GUIManager::getInstance().getLuaState()->doString(NotificationQueueCEGUI::NOTIFICATION_LAYER + ".resizeQueue(\"" + this->getName() + "\", " + multi_cast<std::string>(this->displaySize_.x) + ", " + multi_cast<std::string>(this->displaySize_.y) + ")"); |
---|
| 168 | else |
---|
| 169 | GUIManager::getInstance().getLuaState()->doString(NotificationQueueCEGUI::NOTIFICATION_LAYER + ".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) + ")"); |
---|
| 170 | } |
---|
[8448] | 171 | } |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | @brief |
---|
| 175 | Set the position of the window that displays the NotificationQueue. |
---|
| 176 | @param position |
---|
| 177 | The position is a vector with components: |
---|
| 178 | - The relative x-position of the window. (A value between 0 and 1) |
---|
| 179 | - The absolute x-position in pixels. (Additional to the relative x-position, can be negative) |
---|
| 180 | - The relative y-position of the window. (A value between 0 and 1) |
---|
| 181 | - The absolute y-position in pixels. (Additional to the relative y-position, can be negative.) |
---|
| 182 | */ |
---|
| 183 | void NotificationQueueCEGUI::setPosition(const Vector4& position) |
---|
| 184 | { |
---|
| 185 | if(this->position_ == position) |
---|
| 186 | return; |
---|
| 187 | |
---|
| 188 | if(position.x < 0.0 || position.x > 1.0 || position.z < 0.0 || position.z > 1.0) |
---|
| 189 | { |
---|
| 190 | COUT(2) << "The position the NotificationQueueCEGUI " << this->getName() << " was trying to be set, but the relative position was not in [0,1]. Aborting..." << endl; |
---|
| 191 | return; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | this->position_ = position; |
---|
[8656] | 195 | this->positionChanged(); |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /** |
---|
| 199 | @brief |
---|
| 200 | Is called when the NotificationQueue's position has changed. |
---|
| 201 | */ |
---|
| 202 | void NotificationQueueCEGUI::positionChanged(void) |
---|
| 203 | { |
---|
[8662] | 204 | if(this->isRegistered() && GameMode::showsGraphics()) |
---|
[8640] | 205 | GUIManager::getInstance().getLuaState()->doString(NotificationQueueCEGUI::NOTIFICATION_LAYER + ".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) + ")"); |
---|
[8448] | 206 | } |
---|
| 207 | |
---|
| 208 | /** |
---|
| 209 | @brief |
---|
| 210 | Set the horizontal alignment of the Notifications text. |
---|
| 211 | @param alignment |
---|
| 212 | The alignment of the Notifications, they are the possible string that the CEGUI Falagard StaticText HorzFormatting property can take. |
---|
| 213 | @see http://cegui.org.uk/api_reference/classCEGUI_1_1FalagardStaticTextProperties_1_1HorzFormatting.html |
---|
| 214 | */ |
---|
| 215 | void NotificationQueueCEGUI::setAlignment(const std::string& alignment) |
---|
| 216 | { |
---|
| 217 | if(this->alignment_ == alignment) |
---|
| 218 | return; |
---|
| 219 | |
---|
| 220 | // TODO: Check whether the alignment string is correct? |
---|
| 221 | this->alignment_ = alignment; |
---|
[8656] | 222 | this->alignmentChanged(); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | /** |
---|
| 226 | @brief |
---|
| 227 | Is called when the horizontal alignment of the Notifications text has changed. |
---|
| 228 | */ |
---|
| 229 | void NotificationQueueCEGUI::alignmentChanged(void) |
---|
| 230 | { |
---|
[8662] | 231 | if(this->isRegistered() && GameMode::showsGraphics()) |
---|
[8640] | 232 | GUIManager::getInstance().getLuaState()->doString(NotificationQueueCEGUI::NOTIFICATION_LAYER + ".changeQueueAlignment(\"" + this->getName() + "\", \"" + this->alignment_ + "\")"); |
---|
[8448] | 233 | } |
---|
| 234 | |
---|
| 235 | /** |
---|
| 236 | @brief |
---|
| 237 | Set the font size of the text displayed by this NotificationQueue. |
---|
| 238 | @param size |
---|
| 239 | The font size. |
---|
| 240 | */ |
---|
| 241 | void NotificationQueueCEGUI::setFontSize(unsigned int size) |
---|
| 242 | { |
---|
| 243 | if(this->fontSize_ == size) |
---|
| 244 | return; |
---|
| 245 | |
---|
| 246 | this->fontSize_ = size; |
---|
[8656] | 247 | this->fontSizeChanged(); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | /** |
---|
| 251 | @brief |
---|
| 252 | Is called when the font size of the text displayed by this NotificationQueue has changed. |
---|
| 253 | */ |
---|
| 254 | void NotificationQueueCEGUI::fontSizeChanged(void) |
---|
| 255 | { |
---|
[8662] | 256 | if(this->isRegistered() && GameMode::showsGraphics()) |
---|
[8640] | 257 | GUIManager::getInstance().getLuaState()->doString(NotificationQueueCEGUI::NOTIFICATION_LAYER + ".changeQueueFontSize(\"" + this->getName() + "\", " + multi_cast<std::string>(this->fontSize_) + ")"); |
---|
[8448] | 258 | } |
---|
| 259 | |
---|
| 260 | /** |
---|
| 261 | @brief |
---|
| 262 | Set the font color if the text displayed by this NotificationQueue. |
---|
| 263 | @param color |
---|
| 264 | The color is a vector with the components being RGBA and taking values from 0 to 1. |
---|
| 265 | */ |
---|
| 266 | void NotificationQueueCEGUI::setFontColor(const Vector4& color) |
---|
| 267 | { |
---|
| 268 | if(this->fontColor_ == color) |
---|
| 269 | return; |
---|
| 270 | |
---|
| 271 | this->fontColor_ = color; |
---|
[8656] | 272 | this->fontColorChanged(); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | /** |
---|
| 276 | @brief |
---|
| 277 | Is called when the font color if the text displayed by this NotificationQueue. |
---|
| 278 | */ |
---|
| 279 | void NotificationQueueCEGUI::fontColorChanged(void) |
---|
| 280 | { |
---|
[8448] | 281 | // Convert to ARGB format. |
---|
| 282 | std::stringstream stream; |
---|
| 283 | for(unsigned int i = 0; i < 4; i++) |
---|
| 284 | stream << std::hex << std::setw(2) << std::setfill('0') << int(this->fontColor_[(i+3)%4]*255); |
---|
| 285 | this->fontColorStr_ = stream.str(); |
---|
[8656] | 286 | |
---|
[8662] | 287 | if(this->isRegistered() && GameMode::showsGraphics()) |
---|
[8640] | 288 | GUIManager::getInstance().getLuaState()->doString(NotificationQueueCEGUI::NOTIFICATION_LAYER + ".changeQueueFontColor(\"" + this->getName() + "\", \"" + this->fontColorStr_ + "\")"); |
---|
[8448] | 289 | } |
---|
| 290 | |
---|
| 291 | /** |
---|
| 292 | @brief |
---|
| 293 | Get the NotificationQueueCEGUI with the input name. |
---|
| 294 | @param name |
---|
| 295 | The name of the NotificationQueueCEGUI to be got. |
---|
| 296 | @return |
---|
| 297 | Returns a pointer to the NotificationQueueCEGUI, or NULL if it doesn't exist. |
---|
| 298 | */ |
---|
| 299 | /*static*/ NotificationQueueCEGUI* NotificationQueueCEGUI::getQueue(const std::string& name) |
---|
| 300 | { |
---|
| 301 | NotificationQueue* queue = NotificationManager::getInstance().getQueue(name); |
---|
| 302 | if(queue == NULL || !queue->isA(Class(NotificationQueueCEGUI))) |
---|
| 303 | return NULL; |
---|
| 304 | return static_cast<NotificationQueueCEGUI*>(queue); |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | /** |
---|
| 308 | @brief |
---|
[8656] | 309 | Is called by the NotificationQueue when a Notification was pushed. |
---|
[8446] | 310 | @param notification |
---|
| 311 | The Notification that was pushed. |
---|
| 312 | */ |
---|
| 313 | void NotificationQueueCEGUI::notificationPushed(Notification* notification) |
---|
| 314 | { |
---|
| 315 | // Push the Notification to the GUI. |
---|
[8662] | 316 | if(this->isRegistered() && GameMode::showsGraphics()) |
---|
[8450] | 317 | GUIManager::getInstance().getLuaState()->doString(NotificationQueueCEGUI::NOTIFICATION_LAYER + ".pushNotification(\"" + this->getName() + "\", \"" + notification->getMessage() + "\")"); |
---|
[8446] | 318 | } |
---|
[8448] | 319 | |
---|
[8446] | 320 | /** |
---|
| 321 | @brief |
---|
[8656] | 322 | Is called by the NotificationQueue when a Notification was popped. |
---|
[8446] | 323 | */ |
---|
| 324 | void NotificationQueueCEGUI::notificationPopped(void) |
---|
| 325 | { |
---|
| 326 | // Pops the Notification from the GUI. |
---|
[8662] | 327 | if(this->isRegistered() && GameMode::showsGraphics()) |
---|
[8450] | 328 | GUIManager::getInstance().getLuaState()->doString(NotificationQueueCEGUI::NOTIFICATION_LAYER + ".popNotification(\"" + this->getName() + "\")"); |
---|
[8446] | 329 | } |
---|
[8448] | 330 | |
---|
[8446] | 331 | /** |
---|
| 332 | @brief Is called when a notification was removed. |
---|
[8656] | 333 | @param index The index the removed Notification was at. |
---|
[8446] | 334 | */ |
---|
| 335 | void NotificationQueueCEGUI::notificationRemoved(unsigned int index) |
---|
| 336 | { |
---|
| 337 | // Removes the Notification from the GUI. |
---|
[8662] | 338 | if(this->isRegistered() && GameMode::showsGraphics()) |
---|
[8450] | 339 | GUIManager::getInstance().getLuaState()->doString(NotificationQueueCEGUI::NOTIFICATION_LAYER + ".removeNotification(\"" + this->getName() + "\", " + multi_cast<std::string>(index) + ")"); |
---|
[8446] | 340 | } |
---|
[8448] | 341 | |
---|
[8446] | 342 | /** |
---|
| 343 | @brief |
---|
| 344 | Clears the NotificationQueue by removing all NotificationContainers. |
---|
| 345 | @param noGraphics |
---|
| 346 | If this is set to true the GUI is not informed of the clearing of the NotificationQueue. This is needed only internally. |
---|
| 347 | */ |
---|
| 348 | void NotificationQueueCEGUI::clear(bool noGraphics) |
---|
| 349 | { |
---|
| 350 | NotificationQueue::clear(noGraphics); |
---|
[8448] | 351 | |
---|
[8446] | 352 | // Clear the NotificationQueue in the GUI. |
---|
[8662] | 353 | if(this->isRegistered() && GameMode::showsGraphics() && !noGraphics) |
---|
[8450] | 354 | GUIManager::getInstance().getLuaState()->doString(NotificationQueueCEGUI::NOTIFICATION_LAYER + ".clearQueue(\"" + this->getName() + "\")"); |
---|
[8446] | 355 | } |
---|
[8448] | 356 | |
---|
[8446] | 357 | /** |
---|
| 358 | @brief |
---|
| 359 | Creates the NotificationQueue in lua. |
---|
| 360 | */ |
---|
| 361 | void NotificationQueueCEGUI::create(void) |
---|
| 362 | { |
---|
[8662] | 363 | if(this->isRegistered() && GameMode::showsGraphics()) |
---|
| 364 | GUIManager::getInstance().getLuaState()->doString(NotificationQueueCEGUI::NOTIFICATION_LAYER + ".removeQueue(\"" + this->getName() + "\")"); |
---|
| 365 | |
---|
[8636] | 366 | this->NotificationQueue::create(); |
---|
| 367 | |
---|
| 368 | if(this->isRegistered() && GameMode::showsGraphics()) |
---|
[8450] | 369 | GUIManager::getInstance().getLuaState()->doString(NotificationQueueCEGUI::NOTIFICATION_LAYER + ".createQueue(\"" + this->getName() + "\", " + multi_cast<std::string>(this->getMaxSize()) + ")"); |
---|
[8446] | 370 | } |
---|
| 371 | |
---|
| 372 | } |
---|
| 373 | |
---|