Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickups2/src/orxonox/objects/pickup/PickupInventory.cc @ 3041

Last change on this file since 3041 was 3041, checked in by danielh, 15 years ago

Now creating some CEGUI windows for items as soon as the inventory is first loaded → less delay later on

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