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 | |
---|
50 | namespace orxonox |
---|
51 | { |
---|
52 | orxonox::ConsoleCommand& function_PickupInventory_ToggleInventory_0 = |
---|
53 | orxonox::CommandExecutor::addConsoleCommandShortcut(orxonox::createConsoleCommand(orxonox::createFunctor(&PickupInventory::toggleInventory), "toggleInventory"), true); |
---|
54 | |
---|
55 | static bool bInventoryVisible_ = false; |
---|
56 | void PickupInventory::toggleInventory() |
---|
57 | { |
---|
58 | if(bInventoryVisible_) { |
---|
59 | GUIManager::getInstancePtr()->executeCode("hideGUI(\"PickupInventory\")"); |
---|
60 | GUIManager::getInstancePtr()->executeCode("hideCursor()"); |
---|
61 | InputManager::getInstance().requestLeaveState("guiMouseOnly"); |
---|
62 | } |
---|
63 | else |
---|
64 | { |
---|
65 | GUIManager::getInstancePtr()->showGUI("PickupInventory"); |
---|
66 | GUIManager::getInstancePtr()->executeCode("showCursor()"); |
---|
67 | InputManager::getInstance().requestEnterState("guiMouseOnly"); |
---|
68 | } |
---|
69 | bInventoryVisible_ = !bInventoryVisible_; |
---|
70 | } |
---|
71 | void PickupInventory::tabChanged(CEGUI::Window *tab) |
---|
72 | { |
---|
73 | CEGUI::TabControl* tabCtrl = dynamic_cast<CEGUI::TabControl*>(tab); |
---|
74 | if(tabCtrl != NULL) |
---|
75 | { |
---|
76 | COUT(1) << "tabChanged() tab index: " << tabCtrl->getSelectedTabIndex() << std::endl; |
---|
77 | } |
---|
78 | else |
---|
79 | { |
---|
80 | COUT(1) << "tabChanged() argument is no CEGUI::TabControl!" << std::endl; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | unsigned int PickupInventory::getCurrentUsableIndex() |
---|
85 | { |
---|
86 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
87 | if(pawn && pawn->getPickups().getCurrentUsable()) |
---|
88 | { |
---|
89 | UsableItem* use = pawn->getPickups().getCurrentUsable(); |
---|
90 | std::deque<UsableItem*> items = pawn->getPickups().getUsableItems(); |
---|
91 | for(unsigned int i = 0; i < items.size(); i++) |
---|
92 | { |
---|
93 | if(items.at(i) == use) |
---|
94 | return i; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | return 0; |
---|
99 | } |
---|
100 | bool PickupInventory::isCurrentUsable(const BaseItem* item) |
---|
101 | { |
---|
102 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
103 | if(pawn) |
---|
104 | return (pawn->getPickups().getCurrentUsable() == item); |
---|
105 | else |
---|
106 | return false; |
---|
107 | } |
---|
108 | void PickupInventory::selectUsable(unsigned int i) |
---|
109 | { |
---|
110 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
111 | if(pawn) |
---|
112 | { |
---|
113 | std::deque<UsableItem*> items = pawn->getPickups().getUsableItems(); |
---|
114 | if(i < items.size()) |
---|
115 | pawn->getPickups().setCurrentUsable(items.at(i)); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | unsigned int PickupInventory::getEquipmentCount() |
---|
120 | { |
---|
121 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
122 | if(pawn) |
---|
123 | return pawn->getPickups().getEquipmentItems().size(); |
---|
124 | else |
---|
125 | return 0; |
---|
126 | } |
---|
127 | unsigned int PickupInventory::getUsableCount() |
---|
128 | { |
---|
129 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
130 | if(pawn) |
---|
131 | return pawn->getPickups().getUsableItems().size(); |
---|
132 | else |
---|
133 | return 0; |
---|
134 | } |
---|
135 | unsigned int PickupInventory::getPassiveCount() |
---|
136 | { |
---|
137 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
138 | if(pawn) |
---|
139 | return pawn->getPickups().getPassiveItems().size(); |
---|
140 | else |
---|
141 | return 0; |
---|
142 | } |
---|
143 | BaseItem* PickupInventory::getEquipmentItem(unsigned int i) |
---|
144 | { |
---|
145 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
146 | if(pawn) |
---|
147 | { |
---|
148 | std::deque<EquipmentItem*> l = pawn->getPickups().getEquipmentItems(); |
---|
149 | if (i >= l.size()) { return NULL; } |
---|
150 | return l.at(i); |
---|
151 | } |
---|
152 | else |
---|
153 | return NULL; |
---|
154 | } |
---|
155 | BaseItem* PickupInventory::getUsableItem(unsigned int i) |
---|
156 | { |
---|
157 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
158 | if(pawn) |
---|
159 | { |
---|
160 | std::deque<UsableItem*> l = pawn->getPickups().getUsableItems(); |
---|
161 | if (i >= l.size()) { return NULL; } |
---|
162 | return l.at(i); |
---|
163 | } |
---|
164 | else |
---|
165 | return NULL; |
---|
166 | } |
---|
167 | BaseItem* PickupInventory::getPassiveItem(unsigned int i) |
---|
168 | { |
---|
169 | Pawn* pawn = HumanController::getLocalControllerEntityAsPawn(); |
---|
170 | if(pawn) |
---|
171 | { |
---|
172 | std::deque<PassiveItem*> l = pawn->getPickups().getPassiveItems(); |
---|
173 | if (i >= l.size()) { return NULL; } |
---|
174 | return l.at(i); |
---|
175 | } |
---|
176 | else |
---|
177 | return NULL; |
---|
178 | } |
---|
179 | |
---|
180 | std::string PickupInventory::getImageForItem(const BaseItem* item) |
---|
181 | { |
---|
182 | if(!item) |
---|
183 | return ""; |
---|
184 | |
---|
185 | std::string name = "pickup_" + item->getGUIImage(); |
---|
186 | |
---|
187 | if(!CEGUI::ImagesetManager::getSingletonPtr()->isImagesetPresent(name)) |
---|
188 | { |
---|
189 | CEGUI::ImagesetManager::getSingletonPtr()->createImagesetFromImageFile(name, item->getGUIImage(), ""); |
---|
190 | } |
---|
191 | |
---|
192 | return "set:" + name + " image:full_image"; |
---|
193 | } |
---|
194 | |
---|
195 | void PickupInventory::clearInventory(CEGUI::WindowManager* winMgr, int equipCount, int usableCount) |
---|
196 | { |
---|
197 | for(int i = 0; i < equipCount; i++) |
---|
198 | { |
---|
199 | std::ostringstream id; |
---|
200 | id << i; |
---|
201 | |
---|
202 | winMgr->destroyWindow("orxonox/Inventory/Frame/equ/" + id.str()); |
---|
203 | winMgr->destroyWindow("orxonox/Inventory/Title/equ/" + id.str()); |
---|
204 | winMgr->destroyWindow("orxonox/Inventory/Items/equ/" + id.str()); |
---|
205 | } |
---|
206 | for(int i = 0; i < usableCount; i++) |
---|
207 | { |
---|
208 | std::ostringstream id; |
---|
209 | id << i; |
---|
210 | |
---|
211 | std::string s = id.str(); |
---|
212 | winMgr->destroyWindow("orxonox/Inventory/Frame/use/" + id.str()); |
---|
213 | winMgr->destroyWindow("orxonox/Inventory/Title/use/" + id.str()); |
---|
214 | winMgr->destroyWindow("orxonox/Inventory/Items/use/" + id.str()); |
---|
215 | } |
---|
216 | } |
---|
217 | void PickupInventory::updateTabs(CEGUI::WindowManager *winMgr, CEGUI::Window *equipWindow, CEGUI::Window *usableWindow) |
---|
218 | { |
---|
219 | PickupInventory::updateEquipment(winMgr, equipWindow); |
---|
220 | PickupInventory::updateUsable(winMgr, usableWindow); |
---|
221 | } |
---|
222 | |
---|
223 | void PickupInventory::updateEquipment(CEGUI::WindowManager* winMgr, CEGUI::Window* target) |
---|
224 | { |
---|
225 | Pawn* pawn; |
---|
226 | if(pawn = HumanController::getLocalControllerEntityAsPawn()) |
---|
227 | { |
---|
228 | std::deque<EquipmentItem*> items = pawn->getPickups().getEquipmentItems(); |
---|
229 | for(unsigned int i = 0; i < items.size(); i++) |
---|
230 | { |
---|
231 | std::ostringstream id; |
---|
232 | id << "equ/" << i; |
---|
233 | |
---|
234 | EquipmentItem* item = items.at(i); |
---|
235 | |
---|
236 | PickupInventory::addItem(winMgr, target, id.str(), item, "FFFFFFFF", i % 5, i / 5); |
---|
237 | } |
---|
238 | } |
---|
239 | } |
---|
240 | void PickupInventory::updateUsable(CEGUI::WindowManager* winMgr, CEGUI::Window* target) |
---|
241 | { |
---|
242 | Pawn* pawn; |
---|
243 | if(pawn = HumanController::getLocalControllerEntityAsPawn()) |
---|
244 | { |
---|
245 | std::deque<UsableItem*> items = pawn->getPickups().getUsableItems(); |
---|
246 | for(unsigned int i = 0; i < items.size(); i++) |
---|
247 | { |
---|
248 | std::ostringstream id; |
---|
249 | id << "use/" << i; |
---|
250 | |
---|
251 | UsableItem* item = items.at(i); |
---|
252 | std::string colour; |
---|
253 | |
---|
254 | if(PickupInventory::isCurrentUsable(item)) |
---|
255 | colour = "FFFF5555"; |
---|
256 | else |
---|
257 | colour = "FFFFFFFF"; |
---|
258 | |
---|
259 | PickupInventory::addItem(winMgr, target, id.str(), item, colour, i % 5, i / 5); |
---|
260 | } |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | void PickupInventory::addItem(CEGUI::WindowManager* winMgr, CEGUI::Window* target, const std::string& id, BaseItem* item, const std::string& titleColour, int x, int y) |
---|
265 | { |
---|
266 | if(!winMgr || !target || !item) { return; } |
---|
267 | |
---|
268 | std::string image = PickupInventory::getImageForItem(item); |
---|
269 | |
---|
270 | CEGUI::Window* frame = winMgr->createWindow("TaharezLook/StaticImage", "orxonox/Inventory/Frame/" + id); |
---|
271 | frame->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 5 + x * 70), CEGUI::UDim(0, 5 + y * 90))); |
---|
272 | frame->setSize(CEGUI::UVector2(CEGUI::UDim(0, 65), CEGUI::UDim(0, 65))); |
---|
273 | |
---|
274 | CEGUI::Window* text = winMgr->createWindow("TaharezLook/StaticText", "orxonox/Inventory/Title/" + id); |
---|
275 | text->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 5 + x * 70), CEGUI::UDim(0, 70 + y * 90))); |
---|
276 | text->setSize(CEGUI::UVector2(CEGUI::UDim(0, 65), CEGUI::UDim(0, 20))); |
---|
277 | text->setProperty("Text", item->getGUIText()); |
---|
278 | text->setProperty("FrameEnabled", "False"); |
---|
279 | text->setProperty("BackgroundEnabled", "False"); |
---|
280 | text->setProperty("HorzFormatting", "HorzCentred"); |
---|
281 | text->setProperty("VertFormatting", "VertCentred"); |
---|
282 | text->setProperty("TextColours", "tl:" + titleColour + " tr:" + titleColour + " bl:" + titleColour + " br:" + titleColour + ""); |
---|
283 | |
---|
284 | CEGUI::Window* btn = winMgr->createWindow("TaharezLook/Button", "orxonox/Inventory/Items/" + id); |
---|
285 | btn->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 8 + x * 70), CEGUI::UDim(0, 8 + y * 90))); |
---|
286 | btn->setSize(CEGUI::UVector2(CEGUI::UDim(0, 59), CEGUI::UDim(0, 59))); |
---|
287 | btn->setProperty("NormalImage", image); |
---|
288 | btn->setProperty("HoverImage", image); |
---|
289 | btn->setProperty("PushedImage", image); |
---|
290 | btn->setProperty("DisabledImage", image); |
---|
291 | btn->setProperty("Tooltip", item->getGUIText()); |
---|
292 | btn->subscribeScriptedEvent("Clicked", "itemClicked"); |
---|
293 | |
---|
294 | target->addChildWindow(text); |
---|
295 | target->addChildWindow(frame); |
---|
296 | target->addChildWindow(btn); |
---|
297 | } |
---|
298 | } |
---|