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