From 2e41a5e304d9c35ece851b8a65482bca8784b582 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Wed, 22 Dec 2010 11:29:06 +0200 Subject: [PATCH] just savin' --- src/guiInventoryMenu.cpp | 76 ++++++++++------- src/guiInventoryMenu.h | 2 +- src/inventory.cpp | 179 +++++++++++++++++++++++++++++++++++---- src/inventory.h | 45 +++++++++- src/main.cpp | 109 ++++++++++++++---------- src/player.cpp | 3 +- src/server.cpp | 42 +++++---- 7 files changed, 344 insertions(+), 112 deletions(-) diff --git a/src/guiInventoryMenu.cpp b/src/guiInventoryMenu.cpp index 370ebd5d..7bc5ca9e 100644 --- a/src/guiInventoryMenu.cpp +++ b/src/guiInventoryMenu.cpp @@ -149,46 +149,19 @@ void GUIInventoryMenu::resizeGui() return; m_screensize_old = screensize; - for(u32 i=0; iremove(); - } - m_slots.clear(); - core::rect rect( screensize.X/2 - 560/2, - screensize.Y/2 - 300/2, + screensize.Y/2 - 480/2, screensize.X/2 + 560/2, - screensize.Y/2 + 300/2 + screensize.Y/2 + 480/2 ); DesiredRect = rect; recalculateAbsolutePosition(false); - - //v2s32 size = rect.getSize(); - - core::rect imgsize(0,0,48,48); - v2s32 basepos(30, 30); - for(s32 i=0; i rect = imgsize + basepos + p; - GUIInventorySlot *item = - new GUIInventorySlot(Environment, this, -1, rect); - m_slots.push_back(item); - } - - update(); } void GUIInventoryMenu::update() { - for(s32 i=0; isetItem(m_inventory->getItem(i)); - } } void GUIInventoryMenu::draw() @@ -204,6 +177,51 @@ void GUIInventoryMenu::draw() video::SColor bgcolor(140,0,0,0); driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect); + /* + Draw items + */ + + { + InventoryList *ilist = m_inventory->getList("main"); + if(ilist != NULL) + { + core::rect imgsize(0,0,48,48); + v2s32 basepos(30, 210); + basepos += AbsoluteRect.UpperLeftCorner; + for(s32 i=0; i rect = imgsize + basepos + p; + drawInventoryItem(Environment, ilist->getItem(i), + rect, &AbsoluteClippingRect); + } + } + } + + { + InventoryList *ilist = m_inventory->getList("craft"); + if(ilist != NULL) + { + core::rect imgsize(0,0,48,48); + v2s32 basepos(30, 30); + basepos += AbsoluteRect.UpperLeftCorner; + for(s32 i=0; i<9; i++) + { + s32 x = (i%3) * 64; + s32 y = (i/3) * 64; + v2s32 p(x,y); + core::rect rect = imgsize + basepos + p; + drawInventoryItem(Environment, ilist->getItem(i), + rect, &AbsoluteClippingRect); + } + } + } + + /* + Call base class + */ gui::IGUIElement::draw(); } diff --git a/src/guiInventoryMenu.h b/src/guiInventoryMenu.h index 8bfade4c..b581a116 100644 --- a/src/guiInventoryMenu.h +++ b/src/guiInventoryMenu.h @@ -61,6 +61,7 @@ public: void resizeGui(); // Updates stuff from inventory to screen + // TODO: Remove, not used void update(); void draw(); @@ -80,7 +81,6 @@ public: private: Inventory *m_inventory; - core::array m_slots; v2u32 m_screensize_old; }; diff --git a/src/inventory.cpp b/src/inventory.cpp index 3899c939..069355af 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -136,30 +136,39 @@ MapBlockObject * MapBlockObjectItem::createObject Inventory */ -Inventory::Inventory(u32 size) +InventoryList::InventoryList(std::string name, u32 size) { + m_name = name; m_size = size; clearItems(); } -Inventory::~Inventory() +InventoryList::~InventoryList() { for(u32 i=0; i m_items.size() - 1) return NULL; return m_items[i]; } -InventoryItem * Inventory::changeItem(u32 i, InventoryItem *newitem) +InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem) { assert(i < m_items.size()); @@ -272,7 +296,7 @@ InventoryItem * Inventory::changeItem(u32 i, InventoryItem *newitem) return olditem; } -void Inventory::deleteItem(u32 i) +void InventoryList::deleteItem(u32 i) { assert(i < m_items.size()); InventoryItem *item = changeItem(i, NULL); @@ -280,7 +304,7 @@ void Inventory::deleteItem(u32 i) delete item; } -bool Inventory::addItem(InventoryItem *newitem) +bool InventoryList::addItem(InventoryItem *newitem) { // If it is a MaterialItem, try to find an already existing one // and just increment the counter @@ -324,9 +348,9 @@ bool Inventory::addItem(InventoryItem *newitem) return false; } -void Inventory::print(std::ostream &o) +void InventoryList::print(std::ostream &o) { - o<<"Player inventory:"<getName()<<" "<getSize()<<"\n"; + list->serialize(os); + } + + os<<"end\n"; +} + +void Inventory::deSerialize(std::istream &is) +{ + clear(); + + for(;;) + { + std::string line; + std::getline(is, line, '\n'); + + std::istringstream iss(line); + + std::string name; + std::getline(iss, name, ' '); + + if(name == "end") + { + break; + } + else if(name == "List") + { + std::string listname; + u32 listsize; + + std::getline(iss, listname, ' '); + iss>>listsize; + + InventoryList *list = new InventoryList(listname, listsize); + list->deSerialize(is); + + m_lists.push_back(list); + } + else + { + throw SerializationError("Unknown inventory identifier"); + } + } +} + +InventoryList * Inventory::addList(const std::string &name, u32 size) +{ + s32 i = getListIndex(name); + if(i != -1) + { + if(m_lists[i]->getSize() != size) + { + delete m_lists[i]; + m_lists[i] = new InventoryList(name, size); + } + return m_lists[i]; + } + else + { + m_lists.push_back(new InventoryList(name, size)); + return m_lists.getLast(); + } +} + +InventoryList * Inventory::getList(const std::string &name) +{ + s32 i = getListIndex(name); + if(i == -1) + return NULL; + return m_lists[i]; +} + +s32 Inventory::getListIndex(const std::string &name) +{ + for(u32 i=0; igetName() == name) + return i; + } + return -1; +} + //END diff --git a/src/inventory.h b/src/inventory.h index c18aa5dc..d37761cf 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -196,18 +196,21 @@ private: std::string m_inventorystring; }; -class Inventory +class InventoryList { public: - Inventory(u32 size); - ~Inventory(); + InventoryList(std::string name, u32 size); + ~InventoryList(); void clearItems(); void serialize(std::ostream &os); void deSerialize(std::istream &is); - Inventory & operator = (Inventory &other); + InventoryList(const InventoryList &other); + InventoryList & operator = (const InventoryList &other); + std::string getName(); u32 getSize(); + // Count used slots u32 getUsedSlots(); InventoryItem * getItem(u32 i); @@ -222,6 +225,40 @@ public: private: core::array m_items; u32 m_size; + std::string m_name; +}; + +class Inventory +{ +public: + ~Inventory(); + + void clear(); + + Inventory(); + Inventory(const Inventory &other); + Inventory & operator = (const Inventory &other); + + void serialize(std::ostream &os); + void deSerialize(std::istream &is); + + InventoryList * addList(const std::string &name, u32 size); + InventoryList * getList(const std::string &name); + bool deleteList(const std::string &name); + // A shorthand for adding items + bool addItem(const std::string &listname, InventoryItem *newitem) + { + InventoryList *list = getList(listname); + if(list == NULL) + return false; + return list->addItem(newitem); + } + +private: + // -1 if not found + s32 getListIndex(const std::string &name); + + core::array m_lists; }; #endif diff --git a/src/main.cpp b/src/main.cpp index 4b696236..7fecbe3b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -172,6 +172,7 @@ SUGG: Split Inventory into ClientInventory and ServerInventory Doing now: ====================================================================== +TODO: Convert the text input system to use a modal menu ====================================================================== @@ -269,10 +270,22 @@ extern void set_default_settings(); //u16 g_selected_material = 0; u16 g_selected_item = 0; +/* + GUI Stuff +*/ gui::IGUIEnvironment* guienv = NULL; GUIPauseMenu *pauseMenu = NULL; GUIInventoryMenu *inventoryMenu = NULL; +bool noMenuActive() +{ + if(pauseMenu && pauseMenu->isVisible()) + return false; + if(inventoryMenu && inventoryMenu->isVisible()) + return false; + return true; +} + std::wstring g_text_buffer; bool g_text_buffer_accepted = false; @@ -353,9 +366,7 @@ public: { if(event.KeyInput.Key == irr::KEY_ESCAPE) { - if(g_game_focused == true - && !pauseMenu->isVisible() - && !inventoryMenu->isVisible()) + if(g_game_focused == true && noMenuActive()) { dstream<isVisible() - && !pauseMenu->isVisible()) + if(g_game_focused == true && noMenuActive()) { dstream< 0) + if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN) { - if(g_selected_item > 0) - g_selected_item--; - else - g_selected_item = PLAYER_INVENTORY_SIZE-1; + rightclicked = true; + } + if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) + { + leftreleased = true; + } + if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP) + { + rightreleased = true; + } + if(event.MouseInput.Event == EMIE_MOUSE_WHEEL) + { + /*dstream<<"event.MouseInput.Wheel=" + < 0) + { + if(g_selected_item > 0) + g_selected_item--; + else + g_selected_item = PLAYER_INVENTORY_SIZE-1; + } } } } @@ -933,16 +951,18 @@ public: start = m_selection - m_itemcount / 2; + InventoryList *mainlist = m_inventory->getList("main"); + for(s32 i=0; i (s32)m_inventory->getSize() - 1) - j -= m_inventory->getSize(); + if(j > (s32)mainlist->getSize() - 1) + j -= mainlist->getSize(); if(j < 0) - j += m_inventory->getSize(); + j += mainlist->getSize(); - InventoryItem *item = m_inventory->getItem(j); + InventoryItem *item = mainlist->getItem(j); // Null items if(item == NULL) { @@ -1475,7 +1495,7 @@ int main(int argc, char *argv[]) */ // This is a copy of the inventory that the client's environment has - Inventory local_inventory(PLAYER_INVENTORY_SIZE); + Inventory local_inventory; GUIQuickInventory *quick_inventory = new GUIQuickInventory (guienv, NULL, v2s32(10, 70), 5, &local_inventory); @@ -1806,8 +1826,7 @@ int main(int argc, char *argv[]) if((device->isWindowActive() && g_game_focused - && !pauseMenu->isVisible() - && !inventoryMenu->isVisible() + && noMenuActive() ) || random_input) { diff --git a/src/player.cpp b/src/player.cpp index d38d5eac..e6caff03 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -29,12 +29,13 @@ with this program; if not, write to the Free Software Foundation, Inc., Player::Player(): touching_ground(false), in_water(false), - inventory(PLAYER_INVENTORY_SIZE), peer_id(PEER_ID_NEW), m_speed(0,0,0), m_position(0,0,0) { updateName(""); + inventory.addList("main", PLAYER_INVENTORY_SIZE); + inventory.addList("craft", 9); } Player::~Player() diff --git a/src/server.cpp b/src/server.cpp index 9e809bb9..9952cb8e 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1252,7 +1252,7 @@ void Server::AsyncRunStep() { // Add to inventory and send inventory InventoryItem *item = new MaterialItem(material, 1); - player->inventory.addItem(item); + player->inventory.addItem("main", item); SendInventory(player->peer_id); } @@ -1632,11 +1632,12 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) // Left click if(button == 0) { - if(g_settings.getBool("creative_mode") == false) + InventoryList *ilist = player->inventory.getList("main"); + if(g_settings.getBool("creative_mode") == false && ilist != NULL) { // Skip if inventory has no free space - if(player->inventory.getUsedSlots() == player->inventory.getSize()) + if(ilist->getUsedSlots() == ilist->getSize()) { dout_server<<"Player inventory has no free space"<getInventoryString()); - player->inventory.addItem(item); + ilist->addItem(item); SendInventory(player->peer_id); } @@ -1746,8 +1747,12 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) else if(action == 1) { + InventoryList *ilist = player->inventory.getList("main"); + if(ilist == NULL) + return; + // Get item - InventoryItem *item = player->inventory.getItem(item_i); + InventoryItem *item = ilist->getItem(item_i); // If there is no item, it is not possible to add it anywhere if(item == NULL) @@ -1796,11 +1801,12 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) /* Handle inventory */ - if(g_settings.getBool("creative_mode") == false) + InventoryList *ilist = player->inventory.getList("main"); + if(g_settings.getBool("creative_mode") == false && ilist) { // Remove from inventory and send inventory if(mitem->getCount() == 1) - player->inventory.deleteItem(item_i); + ilist->deleteItem(item_i); else mitem->remove(1); // Send inventory @@ -1819,11 +1825,12 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) /* Handle inventory */ - if(g_settings.getBool("creative_mode") == false) + InventoryList *ilist = player->inventory.getList("main"); + if(g_settings.getBool("creative_mode") == false && ilist) { // Remove from inventory and send inventory if(mitem->getCount() == 1) - player->inventory.deleteItem(item_i); + ilist->deleteItem(item_i); else mitem->remove(1); // Send inventory @@ -1930,10 +1937,11 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) //dout_server<<"Placed object"<inventory.getList("main"); + if(g_settings.getBool("creative_mode") == false && ilist) { // Remove from inventory and send inventory - player->inventory.deleteItem(item_i); + ilist->deleteItem(item_i); // Send inventory SendInventory(peer_id); } @@ -2190,18 +2198,18 @@ void Server::peerAdded(con::Peer *peer) continue; InventoryItem *item = new MaterialItem(i, 1); - player->inventory.addItem(item); + player->inventory.addItem("main", item); } // Sign { InventoryItem *item = new MapBlockObjectItem("Sign Example text"); - bool r = player->inventory.addItem(item); + bool r = player->inventory.addItem("main", item); assert(r == true); } /*// Rat { InventoryItem *item = new MapBlockObjectItem("Rat"); - bool r = player->inventory.addItem(item); + bool r = player->inventory.addItem("main", item); assert(r == true); }*/ } @@ -2210,21 +2218,21 @@ void Server::peerAdded(con::Peer *peer) // Give some lights { InventoryItem *item = new MaterialItem(3, 999); - bool r = player->inventory.addItem(item); + bool r = player->inventory.addItem("main", item); assert(r == true); } // and some signs for(u16 i=0; i<4; i++) { InventoryItem *item = new MapBlockObjectItem("Sign Example text"); - bool r = player->inventory.addItem(item); + bool r = player->inventory.addItem("main", item); assert(r == true); } /*// and some rats for(u16 i=0; i<4; i++) { InventoryItem *item = new MapBlockObjectItem("Rat"); - bool r = player->inventory.addItem(item); + bool r = player->inventory.addItem("main", item); assert(r == true); }*/ } -- 2.30.2