Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Adding network callbacks. Still doesn't work yet, though.

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