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