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 | |
---|
49 | namespace 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::hideGUI("PickupInventory"); |
---|
113 | } |
---|
114 | else |
---|
115 | { |
---|
116 | GUIManager::showGUI("PickupInventory"); |
---|
117 | } |
---|
118 | PickupInventory::getSingleton()->setVisible(!PickupInventory::getSingleton()->isVisible()); |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | @brief |
---|
123 | |
---|
124 | */ |
---|
125 | unsigned int PickupInventory::getCurrentUsableIndex() |
---|
126 | { |
---|
127 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
128 | if(pawn && pawn->getPickups().getCurrentUsable()) |
---|
129 | { |
---|
130 | UsableItem* use = pawn->getPickups().getCurrentUsable(); |
---|
131 | std::deque<UsableItem*> items = pawn->getPickups().getUsableItems(); |
---|
132 | for(unsigned int i = 0; i < items.size(); i++) |
---|
133 | { |
---|
134 | if(items.at(i) == use) |
---|
135 | return i; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | return 0; |
---|
140 | } |
---|
141 | |
---|
142 | bool PickupInventory::isCurrentUsable(const BaseItem* item) |
---|
143 | { |
---|
144 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
145 | if(pawn) |
---|
146 | return (pawn->getPickups().getCurrentUsable() == item); |
---|
147 | else |
---|
148 | return false; |
---|
149 | } |
---|
150 | |
---|
151 | void PickupInventory::selectUsable(unsigned int i) |
---|
152 | { |
---|
153 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
154 | if(pawn) |
---|
155 | { |
---|
156 | std::deque<UsableItem*> items = pawn->getPickups().getUsableItems(); |
---|
157 | if(i < items.size()) |
---|
158 | pawn->getPickups().setCurrentUsable(items.at(i)); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | unsigned int PickupInventory::getEquipmentCount() |
---|
163 | { |
---|
164 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
165 | if(pawn) |
---|
166 | return pawn->getPickups().getEquipmentItems().size(); |
---|
167 | else |
---|
168 | return 0; |
---|
169 | } |
---|
170 | |
---|
171 | unsigned int PickupInventory::getUsableCount() |
---|
172 | { |
---|
173 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
174 | if(pawn) |
---|
175 | return pawn->getPickups().getUsableItems().size(); |
---|
176 | else |
---|
177 | return 0; |
---|
178 | } |
---|
179 | |
---|
180 | unsigned int PickupInventory::getPassiveCount() |
---|
181 | { |
---|
182 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
183 | if(pawn) |
---|
184 | return pawn->getPickups().getPassiveItems().size(); |
---|
185 | else |
---|
186 | return 0; |
---|
187 | } |
---|
188 | |
---|
189 | BaseItem* PickupInventory::getEquipmentItem(unsigned int i) |
---|
190 | { |
---|
191 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
192 | if(pawn) |
---|
193 | { |
---|
194 | std::deque<EquipmentItem*> l = pawn->getPickups().getEquipmentItems(); |
---|
195 | if (i >= l.size()) { return NULL; } |
---|
196 | return l.at(i); |
---|
197 | } |
---|
198 | else |
---|
199 | return NULL; |
---|
200 | } |
---|
201 | |
---|
202 | BaseItem* PickupInventory::getUsableItem(unsigned int i) |
---|
203 | { |
---|
204 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
205 | if(pawn) |
---|
206 | { |
---|
207 | std::deque<UsableItem*> l = pawn->getPickups().getUsableItems(); |
---|
208 | if (i >= l.size()) { return NULL; } |
---|
209 | return l.at(i); |
---|
210 | } |
---|
211 | else |
---|
212 | return NULL; |
---|
213 | } |
---|
214 | |
---|
215 | BaseItem* PickupInventory::getPassiveItem(unsigned int i) |
---|
216 | { |
---|
217 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
218 | if(pawn) |
---|
219 | { |
---|
220 | std::deque<PassiveItem*> l = pawn->getPickups().getPassiveItems(); |
---|
221 | if (i >= l.size()) { return NULL; } |
---|
222 | return l.at(i); |
---|
223 | } |
---|
224 | else |
---|
225 | return NULL; |
---|
226 | } |
---|
227 | |
---|
228 | std::string PickupInventory::getImageForItem(const BaseItem* item) |
---|
229 | { |
---|
230 | if(!item) |
---|
231 | return ""; |
---|
232 | |
---|
233 | const std::string& name = "pickup_" + item->getGUIImage(); |
---|
234 | |
---|
235 | if(!CEGUI::ImagesetManager::getSingletonPtr()->isImagesetPresent(name)) |
---|
236 | { |
---|
237 | CEGUI::ImagesetManager::getSingletonPtr()->createImagesetFromImageFile(name, item->getGUIImage(), ""); |
---|
238 | } |
---|
239 | |
---|
240 | return ("set:" + name + " image:full_image"); |
---|
241 | } |
---|
242 | |
---|
243 | void PickupInventory::clearInventory(CEGUI::WindowManager* winMgr, CEGUI::Window* equipPane, CEGUI::Window* usablePane) |
---|
244 | { |
---|
245 | for(unsigned int i = 0; i < this->visibleEquipmentWindows_; i++) |
---|
246 | { |
---|
247 | std::ostringstream id; |
---|
248 | id << i; |
---|
249 | |
---|
250 | winMgr->getWindow("orxonox/Inventory/Frame/equ/" + id.str())->setVisible(false); |
---|
251 | winMgr->getWindow("orxonox/Inventory/Title/equ/" + id.str())->setVisible(false); |
---|
252 | winMgr->getWindow("orxonox/Inventory/Items/equ/" + id.str())->setVisible(false); |
---|
253 | |
---|
254 | /*equipPane->removeChildWindow("orxonox/Inventory/Frame/equ/" + id.str()); |
---|
255 | equipPane->removeChildWindow("orxonox/Inventory/Title/equ/" + id.str()); |
---|
256 | equipPane->removeChildWindow("orxonox/Inventory/Items/equ/" + id.str());*/ |
---|
257 | } |
---|
258 | for(unsigned int i = 0; i < this->visibleUsableWindows_; i++) |
---|
259 | { |
---|
260 | std::ostringstream id; |
---|
261 | id << i; |
---|
262 | |
---|
263 | winMgr->getWindow("orxonox/Inventory/Frame/use/" + id.str())->setVisible(false); |
---|
264 | winMgr->getWindow("orxonox/Inventory/Title/use/" + id.str())->setVisible(false); |
---|
265 | winMgr->getWindow("orxonox/Inventory/Items/use/" + id.str())->setVisible(false); |
---|
266 | |
---|
267 | /*usablePane->removeChildWindow("orxonox/Inventory/Frame/use/" + id.str()); |
---|
268 | usablePane->removeChildWindow("orxonox/Inventory/Title/use/" + id.str()); |
---|
269 | usablePane->removeChildWindow("orxonox/Inventory/Items/use/" + id.str());*/ |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | void PickupInventory::updateTabs(CEGUI::WindowManager *winMgr, CEGUI::Window *equipWindow, CEGUI::Window *usableWindow) |
---|
274 | { |
---|
275 | this->updateEquipment(winMgr, equipWindow); |
---|
276 | this->updateUsable(winMgr, usableWindow); |
---|
277 | } |
---|
278 | |
---|
279 | void PickupInventory::updateEquipment(CEGUI::WindowManager* winMgr, CEGUI::Window* target) |
---|
280 | { |
---|
281 | Pawn* pawn; |
---|
282 | if((pawn = HumanController::getLocalControllerEntityAsPawn())) |
---|
283 | { |
---|
284 | std::deque<EquipmentItem*> items = pawn->getPickups().getEquipmentItems(); |
---|
285 | for(unsigned int i = 0; i < items.size(); i++) |
---|
286 | { |
---|
287 | std::ostringstream id; |
---|
288 | id << "equ/" << i; |
---|
289 | |
---|
290 | EquipmentItem* item = items.at(i); |
---|
291 | |
---|
292 | if(this->createdEquipmentWindows_ <= i) |
---|
293 | { |
---|
294 | PickupInventory::createItemWindows(winMgr, id.str(), i % 5, i / 5); |
---|
295 | this->createdEquipmentWindows_++; |
---|
296 | } |
---|
297 | |
---|
298 | PickupInventory::setWindowProperties(winMgr, target, id.str(), item, "FFFFFFFF"); |
---|
299 | } |
---|
300 | this->visibleEquipmentWindows_ = items.size(); |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | void PickupInventory::updateUsable(CEGUI::WindowManager* winMgr, CEGUI::Window* target) |
---|
305 | { |
---|
306 | Pawn* pawn; |
---|
307 | if((pawn = HumanController::getLocalControllerEntityAsPawn())) |
---|
308 | { |
---|
309 | std::deque<UsableItem*> items = pawn->getPickups().getUsableItems(); |
---|
310 | for(unsigned int i = 0; i < items.size(); i++) |
---|
311 | { |
---|
312 | std::ostringstream id; |
---|
313 | id << "use/" << i; |
---|
314 | |
---|
315 | UsableItem* item = items.at(i); |
---|
316 | std::string colour; |
---|
317 | |
---|
318 | if(PickupInventory::isCurrentUsable(item)) |
---|
319 | colour = "FFFF5555"; |
---|
320 | else |
---|
321 | colour = "FFFFFFFF"; |
---|
322 | |
---|
323 | if(this->createdUsableWindows_ <= i) |
---|
324 | { |
---|
325 | PickupInventory::createItemWindows(winMgr, id.str(), i % 5, i / 5); |
---|
326 | this->createdUsableWindows_++; |
---|
327 | } |
---|
328 | |
---|
329 | PickupInventory::setWindowProperties(winMgr, target, id.str(), item, colour); |
---|
330 | } |
---|
331 | this->visibleUsableWindows_ = items.size(); |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | void PickupInventory::createItemWindows(CEGUI::WindowManager* winMgr, const std::string& id, int x, int y) |
---|
336 | { |
---|
337 | if(!winMgr) { return; } |
---|
338 | |
---|
339 | CEGUI::Window* frame = winMgr->createWindow("TaharezLook/StaticImage", "orxonox/Inventory/Frame/" + id); |
---|
340 | frame->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 5 + x * 70), CEGUI::UDim(0, 5 + y * 90))); |
---|
341 | frame->setSize(CEGUI::UVector2(CEGUI::UDim(0, 65), CEGUI::UDim(0, 65))); |
---|
342 | frame->setRiseOnClickEnabled(false); |
---|
343 | frame->setVisible(false); |
---|
344 | |
---|
345 | CEGUI::Window* text = winMgr->createWindow("TaharezLook/StaticText", "orxonox/Inventory/Title/" + id); |
---|
346 | text->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 5 + x * 70), CEGUI::UDim(0, 70 + y * 90))); |
---|
347 | text->setSize(CEGUI::UVector2(CEGUI::UDim(0, 65), CEGUI::UDim(0, 20))); |
---|
348 | text->setProperty("FrameEnabled", "False"); |
---|
349 | text->setProperty("BackgroundEnabled", "False"); |
---|
350 | text->setProperty("HorzFormatting", "HorzCentred"); |
---|
351 | text->setProperty("VertFormatting", "VertCentred"); |
---|
352 | text->setProperty("TextColours", "tl:FFFFFFFF tr:FFFFFFFF bl:FFFFFFFF br:FFFFFFFF"); |
---|
353 | text->setVisible(false); |
---|
354 | |
---|
355 | CEGUI::Window* btn = winMgr->createWindow("TaharezLook/Button", "orxonox/Inventory/Items/" + id); |
---|
356 | btn->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 8 + x * 70), CEGUI::UDim(0, 8 + y * 90))); |
---|
357 | btn->setSize(CEGUI::UVector2(CEGUI::UDim(0, 59), CEGUI::UDim(0, 59))); |
---|
358 | btn->subscribeScriptedEvent("Clicked", "PickupInventory.itemClicked"); |
---|
359 | btn->setVisible(false); |
---|
360 | } |
---|
361 | |
---|
362 | void PickupInventory::setWindowProperties(CEGUI::WindowManager* winMgr, CEGUI::Window* target, const std::string& id, const BaseItem* item, const std::string& textColour) |
---|
363 | { |
---|
364 | CEGUI::Window* txt = winMgr->getWindow("orxonox/Inventory/Title/" + id); |
---|
365 | CEGUI::Window* btn = winMgr->getWindow("orxonox/Inventory/Items/" + id); |
---|
366 | CEGUI::Window* frm = winMgr->getWindow("orxonox/Inventory/Frame/" + id); |
---|
367 | |
---|
368 | frm->setVisible(true); |
---|
369 | |
---|
370 | txt->setVisible(true); |
---|
371 | txt->setProperty("Text", item->getGUIText()); |
---|
372 | txt->setProperty("TextColours", "tl:" + textColour + " tr:" + textColour + " bl:" + textColour + " br:" + textColour); |
---|
373 | |
---|
374 | const std::string& image = PickupInventory::getImageForItem(item); |
---|
375 | btn->setVisible(true); |
---|
376 | btn->setProperty("NormalImage", image); |
---|
377 | btn->setProperty("HoverImage", image); |
---|
378 | btn->setProperty("PushedImage", image); |
---|
379 | btn->setProperty("DisabledImage", image); |
---|
380 | btn->setProperty("Tooltip", item->getGUIText()); |
---|
381 | |
---|
382 | target->addChildWindow(frm); |
---|
383 | target->addChildWindow(txt); |
---|
384 | target->addChildWindow(btn); |
---|
385 | } |
---|
386 | |
---|
387 | } |
---|