Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3016


Ignore:
Timestamp:
May 23, 2009, 9:53:13 AM (15 years ago)
Author:
danielh
Message:
  • minor fix in PickupCollection
  • Made PickupInventory into a singleton
  • PickupInventory now only creates new CEGUI windows if needed, and does not destroy unused ones
Location:
code/branches/pickups2/src/orxonox/objects/pickup
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/pickups2/src/orxonox/objects/pickup/PickupCollection.cc

    r3001 r3016  
    158158            return;
    159159
     160        bool getNewUsable = false;
    160161        if (item == this->currentUsable_ || (this->currentUsable_ && removeAllOfType && this->currentUsable_->getPickupIdentifier() == item->getPickupIdentifier()))
     162        {
     163            getNewUsable = true;
     164        }
     165
     166        if (removeAllOfType)
     167        {
     168            std::multimap<std::string, BaseItem*>::iterator it;
     169            while ((it = this->items_.find(item->getPickupIdentifier())) != this->items_.end())
     170            {
     171                this->items_.erase(it);
     172            }
     173        }
     174        else
     175        {
     176            std::multimap<std::string, BaseItem*>::_Pairii bounds = this->items_.equal_range(item->getPickupIdentifier());
     177            for (std::multimap<std::string, BaseItem*>::iterator it = bounds.first; it != bounds.second && it != this->items_.end(); it++)
     178            {
     179                if ((*it).second == item)
     180                {
     181                    this->items_.erase(it);
     182                    break;
     183                }
     184            }
     185        }
     186
     187        if (getNewUsable)
    161188        {
    162189            std::deque<UsableItem*> usables = this->getUsableItems();
     
    166193            else
    167194                this->currentUsable_ = NULL;
    168         }
    169         if (removeAllOfType)
    170         {
    171             std::multimap<std::string, BaseItem*>::iterator it;
    172             while ((it = this->items_.find(item->getPickupIdentifier())) != this->items_.end())
    173             {
    174                 this->items_.erase(it);
    175             }
    176         }
    177         else
    178         {
    179             std::multimap<std::string, BaseItem*>::_Pairii bounds = this->items_.equal_range(item->getPickupIdentifier());
    180             for (std::multimap<std::string, BaseItem*>::iterator it = bounds.first; it != bounds.second && it != this->items_.end(); it++)
    181             {
    182                 if ((*it).second == item)
    183                 {
    184                     this->items_.erase(it);
    185                     return;
    186                 }
    187             }
     195           
    188196        }
    189197    }
  • code/branches/pickups2/src/orxonox/objects/pickup/PickupInventory.cc

    r3001 r3016  
    5353        orxonox::CommandExecutor::addConsoleCommandShortcut(orxonox::createConsoleCommand(orxonox::createFunctor(&PickupInventory::toggleInventory), "toggleInventory"), true);
    5454
    55     static bool bInventoryVisible_ = false;
     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
    5676    void PickupInventory::toggleInventory()
    5777    {
    58         if(bInventoryVisible_) {
     78        if(PickupInventory::getSingleton()->isVisible()) {
    5979            GUIManager::getInstancePtr()->executeCode("hideGUI(\"PickupInventory\")");
    6080            GUIManager::getInstancePtr()->executeCode("hideCursor()");
     
    6787            InputManager::getInstance().requestEnterState("guiMouseOnly");
    6888        }
    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         }
     89        PickupInventory::getSingleton()->setVisible(!PickupInventory::getSingleton()->isVisible());
    8290    }
    8391
     
    193201    }
    194202
    195     void PickupInventory::clearInventory(CEGUI::WindowManager* winMgr, int equipCount, int usableCount)
    196     {
    197         for(int i = 0; i < equipCount; i++)
     203    void PickupInventory::clearInventory(CEGUI::WindowManager* winMgr, CEGUI::Window* equipPane, CEGUI::Window* usablePane)
     204    {
     205        for(unsigned int i = 0; i < this->visibleEquipmentWindows_; i++)
    198206        {
    199207            std::ostringstream id;
    200208            id << i;
    201209
    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++)
     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++)
    207219        {
    208220            std::ostringstream id;
    209221            id << i;
    210222
    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());
     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());*/
    215230        }
    216231    }
    217232    void PickupInventory::updateTabs(CEGUI::WindowManager *winMgr, CEGUI::Window *equipWindow, CEGUI::Window *usableWindow)
    218233    {
    219         PickupInventory::updateEquipment(winMgr, equipWindow);
    220         PickupInventory::updateUsable(winMgr, usableWindow);
     234        this->updateEquipment(winMgr, equipWindow);
     235        this->updateUsable(winMgr, usableWindow);
    221236    }
    222237
     
    234249                EquipmentItem* item = items.at(i);
    235250
    236                 PickupInventory::addItem(winMgr, target, id.str(), item, "FFFFFFFF", i % 5, i / 5);
     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");
    237258            }
     259            this->visibleEquipmentWindows_ = items.size();
    238260        }
    239261    }
     
    257279                    colour = "FFFFFFFF";
    258280
    259                 PickupInventory::addItem(winMgr, target, id.str(), item, colour, i % 5, i / 5);
     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);
    260288            }
    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)
     289            this->visibleUsableWIndows_ = items.size();
     290        }
     291    }
     292
     293    void PickupInventory::createItemWindows(CEGUI::WindowManager* winMgr, const std::string& id, int x, int y)
    265294    {
    266         if(!winMgr || !target || !item) { return; }
    267 
    268         std::string image = PickupInventory::getImageForItem(item);
     295        if(!winMgr) { return; }
    269296
    270297        CEGUI::Window* frame = winMgr->createWindow("TaharezLook/StaticImage", "orxonox/Inventory/Frame/" + id);
     
    275302        text->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 5 + x * 70), CEGUI::UDim(0, 70 + y * 90)));
    276303        text->setSize(CEGUI::UVector2(CEGUI::UDim(0, 65), CEGUI::UDim(0, 20)));
    277         text->setProperty("Text", item->getGUIText());
    278304        text->setProperty("FrameEnabled", "False");
    279305        text->setProperty("BackgroundEnabled", "False");
    280306        text->setProperty("HorzFormatting", "HorzCentred");
    281307        text->setProperty("VertFormatting", "VertCentred");
    282         text->setProperty("TextColours", "tl:" + titleColour + " tr:" + titleColour + " bl:" + titleColour + " br:" + titleColour + "");
     308        text->setProperty("TextColours", "tl:FFFFFFFF tr:FFFFFFFF bl:FFFFFFFF br:FFFFFFFF");
    283309
    284310        CEGUI::Window* btn = winMgr->createWindow("TaharezLook/Button", "orxonox/Inventory/Items/" + id);
    285311        btn->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 8 + x * 70), CEGUI::UDim(0, 8 + y * 90)));
    286312        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);
    287329        btn->setProperty("NormalImage", image);
    288330        btn->setProperty("HoverImage", image);
     
    290332        btn->setProperty("DisabledImage", image);
    291333        btn->setProperty("Tooltip", item->getGUIText());
    292         btn->subscribeScriptedEvent("Clicked", "itemClicked");
    293 
    294         target->addChildWindow(text);
    295         target->addChildWindow(frame);
     334
     335        target->addChildWindow(frm);
     336        target->addChildWindow(txt);
    296337        target->addChildWindow(btn);
    297338    }
  • code/branches/pickups2/src/orxonox/objects/pickup/PickupInventory.h

    r3001 r3016  
    5656// tolua_end
    5757    public:
    58         //PickupInventory(BaseObject* creator);
    59         //virtual ~PickupInventory();
     58        PickupInventory();
     59        virtual ~PickupInventory();
    6060
    61         /**
    62             @brief Toggle visiblity of inventory.
    63         */
     61        static PickupInventory* getSingleton(); // tolua_export
     62
    6463        static void toggleInventory(); // tolua_export
    65 
    66         static void tabChanged(CEGUI::Window* tab); // tolua_export
    6764
    6865        static unsigned int getEquipmentCount(); // tolua_export
     
    8077        static std::string getImageForItem(const BaseItem* item); // tolua_export
    8178
    82         static void clearInventory(CEGUI::WindowManager* winMgr, int equipCount, int usableCount); // tolua_export
    83         static void updateTabs(CEGUI::WindowManager* winMgr, CEGUI::Window* equipWindow, CEGUI::Window* usableWindow); // tolua_export
     79        void clearInventory(CEGUI::WindowManager* winMgr, CEGUI::Window* equipPane, CEGUI::Window* usablePane); // tolua_export
     80        void updateTabs(CEGUI::WindowManager* winMgr, CEGUI::Window* equipWindow, CEGUI::Window* usableWindow); // tolua_export
    8481
    85         static void updateEquipment(CEGUI::WindowManager* winMgr, CEGUI::Window* target);
    86         static void updateUsable(CEGUI::WindowManager* winMgr, CEGUI::Window* target);
     82        void updateEquipment(CEGUI::WindowManager* winMgr, CEGUI::Window* target);
     83        void updateUsable(CEGUI::WindowManager* winMgr, CEGUI::Window* target);
    8784
    88         static void addItem(CEGUI::WindowManager* winMgr, CEGUI::Window* target, const std::string& id, BaseItem* item, const std::string& titleColour, int x, int y); // tolua_export
     85        static void createItemWindows(CEGUI::WindowManager* winMgr, const std::string& id, int x, int y);
     86        static void setWindowProperties(CEGUI::WindowManager* winMgr, CEGUI::Window* target, const std::string& id, const BaseItem* item, const std::string& textColour);
     87
     88        const bool isVisible() const
     89            { return this->bInventoryVisible_; }
     90        void setVisible(bool visible)
     91            { this->bInventoryVisible_ = visible; }
     92    private:
     93        bool bInventoryVisible_;
     94        unsigned int createdEquipmentWindows_;
     95        unsigned int createdUsableWindows_;
     96        unsigned int visibleEquipmentWindows_;
     97        unsigned int visibleUsableWIndows_;
     98
     99        static PickupInventory* pickupInventory_s;
    89100    }; // tolua_export
    90101} // tolua_export
Note: See TracChangeset for help on using the changeset viewer.