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