Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Make nice with dedicated server.

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