Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/modules/notifications/NotificationQueueCEGUI.cc @ 8659

Last change on this file since 8659 was 8659, checked in by dafrick, 13 years ago

Adding missing include.

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