Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestates3/src/modules/questsystem/QuestGUI.cc @ 10240

Last change on this file since 10240 was 6746, checked in by rgrieder, 15 years ago

Merged gamestates2 branch back to trunk.
This brings in some heavy changes in the GUI framework.
It should also fix problems with triggered asserts in the InputManager.

Note: PickupInventory does not seem to work —> Segfault when showing because before, the owner in GUIOverlay::setGUIName is already NULL.
I haven't tested it before, so I can't tell whether it's my changes.

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