Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup2/src/orxonox/pickup/PickupInventory.cc @ 5947

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

Merged pickup branch revisions to pickup2.

  • Property svn:eol-style set to native
File size: 13.5 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 *      Daniel 'Huty' Haggenmueller
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "PickupInventory.h"
30
31#include <CEGUIImage.h>
32#include <CEGUIImageset.h>
33#include <CEGUIImagesetManager.h>
34#include <CEGUIWindow.h>
35#include <CEGUIWindowManager.h>
36#include <elements/CEGUITabControl.h>
37
38#include "core/ConsoleCommand.h"
39#include "core/GUIManager.h"
40#include "core/input/InputManager.h"
41#include "controllers/HumanController.h"
42#include "worldentities/pawns/Pawn.h"
43
44#include "EquipmentItem.h"
45#include "PassiveItem.h"
46#include "UsableItem.h"
47
48
49namespace orxonox
50{
51    SetConsoleCommandShortcut(PickupInventory, toggleInventory);
52
53    PickupInventory* PickupInventory::pickupInventory_s = NULL;
54
55    //TODO: Comment.
56
57    /**
58    @brief
59        Get a Pointer to the PickupInventory Singleton.
60    @return
61        A Pointer to the PickupInventory.
62    */
63    //TODO: Make SingeltonPtr?
64    PickupInventory* PickupInventory::getSingleton()
65    {
66        if(!PickupInventory::pickupInventory_s)
67            PickupInventory::pickupInventory_s = new PickupInventory();
68
69        return PickupInventory::pickupInventory_s;
70    }
71
72    /**
73    @brief
74        Constructor.
75    */
76    PickupInventory::PickupInventory()
77    {
78        //TODO: Maybe some abstraction for the usableWindows, e.g. push and pop...
79        //RegisterObject() ? In some other Class, too. Which?
80        this->bInventoryVisible_ = false; //TODO: If OrxonoxClass, this should already be there...
81        this->visibleEquipmentWindows_ = this->visibleUsableWindows_ = 0;
82
83        // Create some windows to avoid creating them while playing
84        CEGUI::WindowManager* winMgr = CEGUI::WindowManager::getSingletonPtr();
85        for(int i = 0; i < 10; i++)
86        {
87            std::ostringstream id;
88            id << i;
89
90            PickupInventory::createItemWindows(winMgr, "equ/" + id.str(), i % 5, i / 5);
91            PickupInventory::createItemWindows(winMgr, "use/" + id.str(), i % 5, i / 5);
92        }
93        this->createdEquipmentWindows_ = this->createdUsableWindows_ = 10;
94    }
95
96    /**
97    @brief
98        Destructor.
99    */
100    //TODO: Destroy something?
101    PickupInventory::~PickupInventory()
102    {
103    }
104
105    /**
106    @brief
107        Toggles the visibility of the inventory.
108    */
109    /*static*/ void PickupInventory::toggleInventory()
110    {
111        if(PickupInventory::getSingleton()->isVisible()) {
112            GUIManager::getInstance().executeCode("hideGUI(\"PickupInventory\")");
113            GUIManager::getInstance().executeCode("hideCursor()");
114            InputManager::getInstance().leaveState("guiMouseOnly");
115        }
116        else
117        {
118            GUIManager::getInstance().showGUI("PickupInventory");
119            GUIManager::getInstance().executeCode("showCursor()");
120            InputManager::getInstance().enterState("guiMouseOnly");
121        }
122        PickupInventory::getSingleton()->setVisible(!PickupInventory::getSingleton()->isVisible());
123    }
124
125    /**
126    @brief
127       
128    */
129    unsigned int PickupInventory::getCurrentUsableIndex()
130    {
131        Pawn* pawn = HumanController::getLocalControllerEntityAsPawn();
132        if(pawn && pawn->getPickups().getCurrentUsable())
133        {
134            UsableItem* use = pawn->getPickups().getCurrentUsable();
135            std::deque<UsableItem*> items = pawn->getPickups().getUsableItems();
136            for(unsigned int i = 0; i < items.size(); i++)
137            {
138                if(items.at(i) == use)
139                    return i;
140            }
141        }
142
143        return 0;
144    }
145
146    bool PickupInventory::isCurrentUsable(const BaseItem* item)
147    {
148        Pawn* pawn = HumanController::getLocalControllerEntityAsPawn();
149        if(pawn)
150            return (pawn->getPickups().getCurrentUsable() == item);
151        else
152            return false;
153    }
154
155    void PickupInventory::selectUsable(unsigned int i)
156    {
157        Pawn* pawn = HumanController::getLocalControllerEntityAsPawn();
158        if(pawn)
159        {
160            std::deque<UsableItem*> items = pawn->getPickups().getUsableItems();
161            if(i < items.size())
162                pawn->getPickups().setCurrentUsable(items.at(i));
163        }
164    }
165
166    unsigned int PickupInventory::getEquipmentCount()
167    {
168        Pawn* pawn = HumanController::getLocalControllerEntityAsPawn();
169        if(pawn)
170            return pawn->getPickups().getEquipmentItems().size();
171        else
172            return 0;
173    }
174
175    unsigned int PickupInventory::getUsableCount()
176    {
177        Pawn* pawn = HumanController::getLocalControllerEntityAsPawn();
178        if(pawn)
179            return pawn->getPickups().getUsableItems().size();
180        else
181            return 0;
182    }
183
184    unsigned int PickupInventory::getPassiveCount()
185    {
186        Pawn* pawn = HumanController::getLocalControllerEntityAsPawn();
187        if(pawn)
188            return pawn->getPickups().getPassiveItems().size();
189        else
190            return 0;
191    }
192
193    BaseItem* PickupInventory::getEquipmentItem(unsigned int i)
194    {
195        Pawn* pawn = HumanController::getLocalControllerEntityAsPawn();
196        if(pawn)
197        {
198            std::deque<EquipmentItem*> l = pawn->getPickups().getEquipmentItems();
199            if (i >= l.size()) { return NULL; }
200            return l.at(i);
201        }
202        else
203            return NULL;
204    }
205
206    BaseItem* PickupInventory::getUsableItem(unsigned int i)
207    {
208        Pawn* pawn = HumanController::getLocalControllerEntityAsPawn();
209        if(pawn)
210        {
211            std::deque<UsableItem*> l = pawn->getPickups().getUsableItems();
212            if (i >= l.size()) { return NULL; }
213            return l.at(i);
214        }
215        else
216            return NULL;
217    }
218
219    BaseItem* PickupInventory::getPassiveItem(unsigned int i)
220    {
221        Pawn* pawn = HumanController::getLocalControllerEntityAsPawn();
222        if(pawn)
223        {
224            std::deque<PassiveItem*> l = pawn->getPickups().getPassiveItems();
225            if (i >= l.size()) { return NULL; }
226            return l.at(i);
227        }
228        else
229            return NULL;
230    }
231
232    std::string PickupInventory::getImageForItem(const BaseItem* item)
233    {
234        if(!item)
235            return "";
236
237        std::string name = "pickup_" + item->getGUIImage();
238
239        if(!CEGUI::ImagesetManager::getSingletonPtr()->isImagesetPresent(name))
240        {
241            CEGUI::ImagesetManager::getSingletonPtr()->createImagesetFromImageFile(name, item->getGUIImage(), "");
242        }
243
244        return "set:" + name + " image:full_image";
245    }
246
247    void PickupInventory::clearInventory(CEGUI::WindowManager* winMgr, CEGUI::Window* equipPane, CEGUI::Window* usablePane)
248    {
249        for(unsigned int i = 0; i < this->visibleEquipmentWindows_; i++)
250        {
251            std::ostringstream id;
252            id << i;
253
254            winMgr->getWindow("orxonox/Inventory/Frame/equ/" + id.str())->setVisible(false);
255            winMgr->getWindow("orxonox/Inventory/Title/equ/" + id.str())->setVisible(false);
256            winMgr->getWindow("orxonox/Inventory/Items/equ/" + id.str())->setVisible(false);
257
258            /*equipPane->removeChildWindow("orxonox/Inventory/Frame/equ/" + id.str());
259            equipPane->removeChildWindow("orxonox/Inventory/Title/equ/" + id.str());
260            equipPane->removeChildWindow("orxonox/Inventory/Items/equ/" + id.str());*/
261        }
262        for(unsigned int i = 0; i < this->visibleUsableWindows_; i++)
263        {
264            std::ostringstream id;
265            id << i;
266
267            winMgr->getWindow("orxonox/Inventory/Frame/use/" + id.str())->setVisible(false);
268            winMgr->getWindow("orxonox/Inventory/Title/use/" + id.str())->setVisible(false);
269            winMgr->getWindow("orxonox/Inventory/Items/use/" + id.str())->setVisible(false);
270
271            /*usablePane->removeChildWindow("orxonox/Inventory/Frame/use/" + id.str());
272            usablePane->removeChildWindow("orxonox/Inventory/Title/use/" + id.str());
273            usablePane->removeChildWindow("orxonox/Inventory/Items/use/" + id.str());*/
274        }
275    }
276
277    void PickupInventory::updateTabs(CEGUI::WindowManager *winMgr, CEGUI::Window *equipWindow, CEGUI::Window *usableWindow)
278    {
279        this->updateEquipment(winMgr, equipWindow);
280        this->updateUsable(winMgr, usableWindow);
281    }
282
283    void PickupInventory::updateEquipment(CEGUI::WindowManager* winMgr, CEGUI::Window* target)
284    {
285        Pawn* pawn;
286        if((pawn = HumanController::getLocalControllerEntityAsPawn()))
287        {
288            std::deque<EquipmentItem*> items = pawn->getPickups().getEquipmentItems();
289            for(unsigned int i = 0; i < items.size(); i++)
290            {
291                std::ostringstream id;
292                id << "equ/" << i;
293
294                EquipmentItem* item = items.at(i);
295
296                if(this->createdEquipmentWindows_ <= i)
297                {
298                    PickupInventory::createItemWindows(winMgr, id.str(), i % 5, i / 5);
299                    this->createdEquipmentWindows_++;
300                }
301
302                PickupInventory::setWindowProperties(winMgr, target, id.str(), item, "FFFFFFFF");
303            }
304            this->visibleEquipmentWindows_ = items.size();
305        }
306    }
307
308    void PickupInventory::updateUsable(CEGUI::WindowManager* winMgr, CEGUI::Window* target)
309    {
310        Pawn* pawn;
311        if((pawn = HumanController::getLocalControllerEntityAsPawn()))
312        {
313            std::deque<UsableItem*> items = pawn->getPickups().getUsableItems();
314            for(unsigned int i = 0; i < items.size(); i++)
315            {
316                std::ostringstream id;
317                id << "use/" << i;
318
319                UsableItem* item = items.at(i);
320                std::string colour;
321
322                if(PickupInventory::isCurrentUsable(item))
323                    colour = "FFFF5555";
324                else
325                    colour = "FFFFFFFF";
326
327                if(this->createdUsableWindows_ <= i)
328                {
329                    PickupInventory::createItemWindows(winMgr, id.str(), i % 5, i / 5);
330                    this->createdUsableWindows_++;
331                }
332
333                PickupInventory::setWindowProperties(winMgr, target, id.str(), item, colour);
334            }
335            this->visibleUsableWindows_ = items.size();
336        }
337    }
338
339    void PickupInventory::createItemWindows(CEGUI::WindowManager* winMgr, const std::string& id, int x, int y)
340    {
341        if(!winMgr) { return; }
342
343        CEGUI::Window* frame = winMgr->createWindow("TaharezLook/StaticImage", "orxonox/Inventory/Frame/" + id);
344        frame->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 5 + x * 70), CEGUI::UDim(0, 5 + y * 90)));
345        frame->setSize(CEGUI::UVector2(CEGUI::UDim(0, 65), CEGUI::UDim(0, 65)));
346        frame->setRiseOnClickEnabled(false);
347        frame->setVisible(false);
348
349        CEGUI::Window* text = winMgr->createWindow("TaharezLook/StaticText", "orxonox/Inventory/Title/" + id);
350        text->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 5 + x * 70), CEGUI::UDim(0, 70 + y * 90)));
351        text->setSize(CEGUI::UVector2(CEGUI::UDim(0, 65), CEGUI::UDim(0, 20)));
352        text->setProperty("FrameEnabled", "False");
353        text->setProperty("BackgroundEnabled", "False");
354        text->setProperty("HorzFormatting", "HorzCentred");
355        text->setProperty("VertFormatting", "VertCentred");
356        text->setProperty("TextColours", "tl:FFFFFFFF tr:FFFFFFFF bl:FFFFFFFF br:FFFFFFFF");
357        text->setVisible(false);
358
359        CEGUI::Window* btn = winMgr->createWindow("TaharezLook/Button", "orxonox/Inventory/Items/" + id);
360        btn->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 8 + x * 70), CEGUI::UDim(0, 8 + y * 90)));
361        btn->setSize(CEGUI::UVector2(CEGUI::UDim(0, 59), CEGUI::UDim(0, 59)));
362        btn->subscribeScriptedEvent("Clicked", "PickupInventory.itemClicked");
363        btn->setVisible(false);
364    }
365
366    void PickupInventory::setWindowProperties(CEGUI::WindowManager* winMgr, CEGUI::Window* target, const std::string& id, const BaseItem* item, const std::string& textColour)
367    {
368        CEGUI::Window* txt = winMgr->getWindow("orxonox/Inventory/Title/" + id);
369        CEGUI::Window* btn = winMgr->getWindow("orxonox/Inventory/Items/" + id);
370        CEGUI::Window* frm = winMgr->getWindow("orxonox/Inventory/Frame/" + id);
371
372        frm->setVisible(true);
373
374        txt->setVisible(true);
375        txt->setProperty("Text", item->getGUIText());
376        txt->setProperty("TextColours", "tl:" + textColour + " tr:" + textColour + " bl:" + textColour + " br:" + textColour + "");
377
378        std::string image = PickupInventory::getImageForItem(item);
379        btn->setVisible(true);
380        btn->setProperty("NormalImage", image);
381        btn->setProperty("HoverImage", image);
382        btn->setProperty("PushedImage", image);
383        btn->setProperty("DisabledImage", image);
384        btn->setProperty("Tooltip", item->getGUIText());
385
386        target->addChildWindow(frm);
387        target->addChildWindow(txt);
388        target->addChildWindow(btn);
389    }
390
391}
Note: See TracBrowser for help on using the repository browser.