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