Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/questsystem/QuestGUI.cc @ 5746

Last change on this file since 5746 was 5746, checked in by dafrick, 15 years ago

Forgot to add files…

File size: 8.8 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#include "QuestGUI.h"
30
31#include <sstream>
32
33#include "core/CoreIncludes.h"
34#include "core/GUIManager.h"
35#include "infos/PlayerInfo.h"
36#include "Quest.h"
37#include "QuestHint.h"
38#include "QuestDescription.h" //Debug
39#include "QuestItem.h"
40#include "QuestGUINode.h"
41#include "QuestManager.h"
42
43#include <CEGUIWindow.h>
44#include <CEGUIWindowManager.h>
45
46namespace orxonox {
47
48    /**
49    @brief
50        Constructor. Registers and initializes the object.
51    @param
52        The player the GUI is for.
53    */
54    QuestGUI::QuestGUI(PlayerInfo* player)
55    {
56        RegisterRootObject(QuestGUI);
57
58        this->player_ = player;
59        this->windowManager_ = CEGUI::WindowManager::getSingletonPtr(); //!< Get CEGUI WindowManager.
60        this->rootWindow_ = NULL;
61        this->root_ = new QuestGUINode(); //!< Create empty root node.
62
63        COUT(3) << "New QuestGUI created." << std::endl;
64    }
65
66    /**
67    @brief
68        Destructor.
69    */
70    QuestGUI::~QuestGUI()
71    {
72        COUT(3) << "Destroying QuestGUI..." << std::endl;
73
74        this->clear(); //!< Clearing the GUI and in the process destroying all QuestGUINodes.
75
76        //! Destroying the windows in the this->windows_ list.
77        for(std::list<CEGUI::Window*>::iterator it = this->windows_.begin(); it != this->windows_.end(); it++)
78        {
79            if(*it != NULL)
80                (*it)->destroy();
81        }
82        this->windows_.clear();
83       
84        if(this->root_ != NULL)
85            delete this->root_;
86    }
87
88    /**
89    @brief
90        Get the root CEGUI Window of the GUI.
91    @return
92        Returns the root CEGUI Window of the GUI.
93    */
94    CEGUI::Window* QuestGUI::getGUI(void)
95    {
96        this->update(); //!< Update the GUI.
97
98        return this->rootWindow_;
99    }
100
101    /**
102    @brief
103        Update the GUI.
104    */
105    void QuestGUI::update(void)
106    {
107        COUT(3) << "Updating QuestGUI..." << std::endl;
108
109        this->clear(); //!< Clear the GUI.
110       
111        int depth = 0;
112        int index = 0;
113
114        //! Create root window.
115        this->rootWindow_ = this->windowManager_->createWindow("TaharezLook/ScrollablePane", "QuestGUI/Quests");
116        this->rootWindow_->setSize(CEGUI::UVector2(CEGUI::UDim(1, 0),CEGUI::UDim(1, 0)));
117
118        //! Iterate through all Quests.
119        std::map<std::string, Quest*> quests = QuestManager::getInstance().getQuests();
120        for(std::map<std::string, Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
121        {
122            Quest* quest = (*it).second;
123            if(quest->getParentQuest() == NULL && !quest->isInactive(this->player_)) //!< If the Quest isn't inactive and a root Quest (meaning it has no parent.), create a Node.
124            {
125                index = createNode(this->root_, quest, depth, index);
126            }
127        }
128        COUT(3) << "Updating QuestGUI done." << std::endl;
129    }
130
131    /**
132    @brief
133        Clear the QuestGUI.
134    */
135    void QuestGUI::clear(void)
136    {
137        COUT(3) << "Clearing QuestGUI..." << std::endl;
138
139        //! Clear all nodes.
140        for(std::map<CEGUI::Window*, QuestGUINode*>::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++)
141        {
142            QuestGUINode* node = (*it).second;
143            if(node == NULL)
144            {
145                COUT(1) << "Node is NULL!";
146                continue;
147            }
148            std::string* str = new std::string();
149            node->getName(*str);
150            COUT(3) << "Clearing Node '" << *str << "' ..." << std::endl;
151            delete str;
152            delete node;
153        }
154        this->nodes_.clear();
155
156        //! Clear root window.
157        if(this->rootWindow_ != NULL)
158            this->rootWindow_->destroy();
159
160        COUT(3) << "Clearing QuestGUI done." << std::endl;
161    }
162
163    /**
164    @brief
165        Get a CEGUI Window to use.
166        Windows that are no longer used are collected with giveWindow, and are given out again with getWindow, so save some time recreating new windows everytime.
167        The retreived window is of type "TaharezLook/TabButton".
168    @return
169        Returns a CEGUI Window of type "TaharezLook/TabButton".
170    */
171    CEGUI::Window* QuestGUI::getWindow(void)
172    {
173        if(!this->windows_.empty()) //!< If there are windows in the list.
174        {
175            CEGUI::Window* window = this->windows_.back();
176            this->windows_.pop_back();
177            return window;
178        }
179
180        //!< Else create a new one.
181        std::ostringstream stream;
182        stream << "QuestGUI/Quests/EmptyWindows/" << this->windows_.size()+1;
183        return this->windowManager_->createWindow("TaharezLook/TabButton", stream.str());
184    }
185
186    /**
187    @brief
188        Return a no longer needed CEGUI Window for reuse.
189    @param window
190        The CEGUI window ot be returned.
191    */
192    void QuestGUI::giveWindow(CEGUI::Window* window)
193    {
194        if(window == NULL)
195            return;
196        this->windows_.push_back(window);
197        this->rootWindow_->removeChildWindow(window); //!< Remove the window as child of the rootWindow.
198        std::ostringstream stream;
199        stream << "QuestGUI/Quests/EmptyWindows/" << this->windows_.size();
200        window->rename(stream.str());
201    }
202
203    /**
204    @brief
205        Finde the QuestGUINode belonging to the input CEGUI Window.
206    @param window
207        A pointer to a CEGUI Window.
208    @return
209        A pointer to the QuestGUI Node belonging to the input CEGUI Window.
210    */
211    /*static*/ QuestGUINode* QuestGUI::findNode(CEGUI::Window* window)
212    {
213        for(std::map<PlayerInfo*, QuestGUI*>::iterator it = QuestManager::getInstance().questGUIs_.begin(); it != QuestManager::getInstance().questGUIs_.end(); it++)
214        {
215            QuestGUI* gui = (*it).second;
216            std::map<CEGUI::Window*, QuestGUINode*>::iterator node = gui->nodes_.find(window);
217            if(node != gui->nodes_.end()) return node->second;
218        }
219        return NULL;
220    }
221
222    /**
223    @brief
224        Recursive method to create Nodes for all Quests an Hints the given Quest is a parent to.
225    @param parent
226        Pointer to the parent QuestGUINode.
227    @param item
228        The QuestItem the QuestGUINode is created for.
229    @param depth
230        Parameter to define how much the list item has to be indented.
231    @param index
232        "Counter" for Quests and Hints.
233    @return
234        Returns the index.
235    */
236    int QuestGUI::createNode(QuestGUINode* parent, QuestItem* item, int depth, int index)
237    {
238        QuestGUINode* node = new QuestGUINode(this, parent, item, depth, index); //!< Create a new QuestGUINode.
239
240        this->nodes_.insert(std::pair<CEGUI::Window*, QuestGUINode*>(node->getWindow(),node)); //!< Insert the node and its window in the nodes_ map.
241       
242        index++;
243
244        //! Check if the QuestItem is a Quest, if not (it's a QuestHint) it just returns.
245        Quest* quest = dynamic_cast<Quest*>(item);
246        if(quest == NULL)
247          return index;
248
249        //! Iterate through all subQuests.
250        std::list<Quest*> quests = quest->getSubQuestList();
251        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
252        {
253            Quest* quest = *it;
254            if(!quest->isInactive(this->player_)) //!< Add node if the subQuest is not inactive.
255            {
256                index = createNode(node, quest, depth+1, index);
257            }
258        }
259
260        //! Iterate through all hints.
261        std::list<QuestHint*> hints = quest->getHintsList();
262        int tempIndex = index; //!< Preserve the index, since for the hints we start anew with index 0.
263        index = 0;
264        for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++)
265        {
266            QuestHint* hint = *it;
267            if(hint->isActive(this->player_)) //!< Add node if the hint is active.
268            {
269                index = createNode(node, hint, depth+1, index);
270            }
271        }
272        index = tempIndex; //!< Reset the index to the original level.
273
274        return index;       
275    }
276
277}
278
Note: See TracBrowser for help on using the repository browser.