return;
m_screensize_old = screensize;
- for(u32 i=0; i<m_slots.size(); i++)
- {
- m_slots[i]->remove();
- }
- m_slots.clear();
-
core::rect<s32> 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<s32> imgsize(0,0,48,48);
- v2s32 basepos(30, 30);
- for(s32 i=0; i<PLAYER_INVENTORY_SIZE; i++)
- {
- s32 x = (i%8) * 64;
- s32 y = (i/8) * 64;
- v2s32 p(x,y);
- core::rect<s32> 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; i<PLAYER_INVENTORY_SIZE; i++)
- {
- m_slots[i]->setItem(m_inventory->getItem(i));
- }
}
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<s32> imgsize(0,0,48,48);
+ v2s32 basepos(30, 210);
+ basepos += AbsoluteRect.UpperLeftCorner;
+ for(s32 i=0; i<PLAYER_INVENTORY_SIZE; i++)
+ {
+ s32 x = (i%8) * 64;
+ s32 y = (i/8) * 64;
+ v2s32 p(x,y);
+ core::rect<s32> rect = imgsize + basepos + p;
+ drawInventoryItem(Environment, ilist->getItem(i),
+ rect, &AbsoluteClippingRect);
+ }
+ }
+ }
+
+ {
+ InventoryList *ilist = m_inventory->getList("craft");
+ if(ilist != NULL)
+ {
+ core::rect<s32> 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<s32> rect = imgsize + basepos + p;
+ drawInventoryItem(Environment, ilist->getItem(i),
+ rect, &AbsoluteClippingRect);
+ }
+ }
+ }
+
+ /*
+ Call base class
+ */
gui::IGUIElement::draw();
}
void resizeGui();
// Updates stuff from inventory to screen
+ // TODO: Remove, not used
void update();
void draw();
private:
Inventory *m_inventory;
- core::array<GUIInventorySlot*> m_slots;
v2u32 m_screensize_old;
};
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(); i++)
{
- delete m_items[i];
+ if(m_items[i])
+ delete m_items[i];
}
}
-void Inventory::clearItems()
+void InventoryList::clearItems()
{
+ for(u32 i=0; i<m_items.size(); i++)
+ {
+ if(m_items[i])
+ delete m_items[i];
+ }
+
m_items.clear();
+
for(u32 i=0; i<m_size; i++)
{
m_items.push_back(NULL);
}
}
-void Inventory::serialize(std::ostream &os)
+void InventoryList::serialize(std::ostream &os)
{
//os.imbue(std::locale("C"));
os<<"end\n";
}
-void Inventory::deSerialize(std::istream &is)
+void InventoryList::deSerialize(std::istream &is)
{
//is.imbue(std::locale("C"));
}
}
-Inventory & Inventory::operator = (Inventory &other)
+InventoryList::InventoryList(const InventoryList &other)
+{
+ /*
+ Do this so that the items get cloned. Otherwise the pointers
+ in the array will just get copied.
+ */
+ *this = other;
+}
+
+InventoryList & InventoryList::operator = (const InventoryList &other)
{
+ m_name = other.m_name;
m_size = other.m_size;
clearItems();
for(u32 i=0; i<other.m_items.size(); i++)
return *this;
}
-u32 Inventory::getSize()
+std::string InventoryList::getName()
+{
+ return m_name;
+}
+
+u32 InventoryList::getSize()
{
return m_items.size();
}
-u32 Inventory::getUsedSlots()
+u32 InventoryList::getUsedSlots()
{
u32 num = 0;
for(u32 i=0; i<m_items.size(); i++)
return num;
}
-InventoryItem * Inventory::getItem(u32 i)
+InventoryItem * InventoryList::getItem(u32 i)
{
if(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());
return olditem;
}
-void Inventory::deleteItem(u32 i)
+void InventoryList::deleteItem(u32 i)
{
assert(i < m_items.size());
InventoryItem *item = changeItem(i, NULL);
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
return false;
}
-void Inventory::print(std::ostream &o)
+void InventoryList::print(std::ostream &o)
{
- o<<"Player inventory:"<<std::endl;
+ o<<"InventoryList:"<<std::endl;
for(u32 i=0; i<m_items.size(); i++)
{
InventoryItem *item = m_items[i];
}
}
}
+
+/*
+ Inventory
+*/
+
+Inventory::~Inventory()
+{
+ clear();
+}
+
+void Inventory::clear()
+{
+ for(u32 i=0; i<m_lists.size(); i++)
+ {
+ delete m_lists[i];
+ }
+ m_lists.clear();
+}
+
+Inventory::Inventory()
+{
+}
+
+Inventory::Inventory(const Inventory &other)
+{
+ *this = other;
+}
+
+Inventory & Inventory::operator = (const Inventory &other)
+{
+ clear();
+ for(u32 i=0; i<other.m_lists.size(); i++)
+ {
+ m_lists.push_back(new InventoryList(*other.m_lists[i]));
+ }
+ return *this;
+}
+
+void Inventory::serialize(std::ostream &os)
+{
+ for(u32 i=0; i<m_lists.size(); i++)
+ {
+ InventoryList *list = m_lists[i];
+ os<<"List "<<list->getName()<<" "<<list->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; i<m_lists.size(); i++)
+ {
+ if(m_lists[i]->getName() == name)
+ return i;
+ }
+ return -1;
+}
+
//END
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);
private:
core::array<InventoryItem*> 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<InventoryList*> m_lists;
};
#endif
Doing now:\r
======================================================================\r
\r
+TODO: Convert the text input system to use a modal menu\r
\r
======================================================================\r
\r
//u16 g_selected_material = 0;\r
u16 g_selected_item = 0;\r
\r
+/*\r
+ GUI Stuff\r
+*/\r
gui::IGUIEnvironment* guienv = NULL;\r
GUIPauseMenu *pauseMenu = NULL;\r
GUIInventoryMenu *inventoryMenu = NULL;\r
\r
+bool noMenuActive()\r
+{\r
+ if(pauseMenu && pauseMenu->isVisible())\r
+ return false;\r
+ if(inventoryMenu && inventoryMenu->isVisible())\r
+ return false;\r
+ return true;\r
+}\r
+\r
std::wstring g_text_buffer;\r
bool g_text_buffer_accepted = false;\r
\r
{\r
if(event.KeyInput.Key == irr::KEY_ESCAPE)\r
{\r
- if(g_game_focused == true\r
- && !pauseMenu->isVisible()\r
- && !inventoryMenu->isVisible())\r
+ if(g_game_focused == true && noMenuActive())\r
{\r
dstream<<DTIME<<"MyEventReceiver: "\r
<<"Launching pause menu"<<std::endl;\r
{\r
if(event.KeyInput.Key == irr::KEY_KEY_I)\r
{\r
- if(g_game_focused == true\r
- && !inventoryMenu->isVisible()\r
- && !pauseMenu->isVisible())\r
+ if(g_game_focused == true && noMenuActive())\r
{\r
dstream<<DTIME<<"MyEventReceiver: "\r
<<"Launching inventory"<<std::endl;\r
\r
if(event.EventType == irr::EET_MOUSE_INPUT_EVENT)\r
{\r
- //dstream<<"MyEventReceiver: mouse input"<<std::endl;\r
- left_active = event.MouseInput.isLeftPressed();\r
- middle_active = event.MouseInput.isMiddlePressed();\r
- right_active = event.MouseInput.isRightPressed();\r
-\r
- if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)\r
- {\r
- leftclicked = true;\r
- }\r
- if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)\r
- {\r
- rightclicked = true;\r
- }\r
- if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)\r
+ if(noMenuActive() == false)\r
{\r
- leftreleased = true;\r
+ left_active = false;\r
+ middle_active = false;\r
+ right_active = false;\r
}\r
- if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)\r
- {\r
- rightreleased = true;\r
- }\r
- if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)\r
+ else\r
{\r
- /*dstream<<"event.MouseInput.Wheel="\r
- <<event.MouseInput.Wheel<<std::endl;*/\r
- if(event.MouseInput.Wheel < 0)\r
+ //dstream<<"MyEventReceiver: mouse input"<<std::endl;\r
+ left_active = event.MouseInput.isLeftPressed();\r
+ middle_active = event.MouseInput.isMiddlePressed();\r
+ right_active = event.MouseInput.isRightPressed();\r
+\r
+ if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)\r
{\r
- if(g_selected_item < PLAYER_INVENTORY_SIZE-1)\r
- g_selected_item++;\r
- else\r
- g_selected_item = 0;\r
+ leftclicked = true;\r
}\r
- else if(event.MouseInput.Wheel > 0)\r
+ if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)\r
{\r
- if(g_selected_item > 0)\r
- g_selected_item--;\r
- else\r
- g_selected_item = PLAYER_INVENTORY_SIZE-1;\r
+ rightclicked = true;\r
+ }\r
+ if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)\r
+ {\r
+ leftreleased = true;\r
+ }\r
+ if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)\r
+ {\r
+ rightreleased = true;\r
+ }\r
+ if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)\r
+ {\r
+ /*dstream<<"event.MouseInput.Wheel="\r
+ <<event.MouseInput.Wheel<<std::endl;*/\r
+ if(event.MouseInput.Wheel < 0)\r
+ {\r
+ if(g_selected_item < PLAYER_INVENTORY_SIZE-1)\r
+ g_selected_item++;\r
+ else\r
+ g_selected_item = 0;\r
+ }\r
+ else if(event.MouseInput.Wheel > 0)\r
+ {\r
+ if(g_selected_item > 0)\r
+ g_selected_item--;\r
+ else\r
+ g_selected_item = PLAYER_INVENTORY_SIZE-1;\r
+ }\r
}\r
}\r
}\r
\r
start = m_selection - m_itemcount / 2;\r
\r
+ InventoryList *mainlist = m_inventory->getList("main");\r
+\r
for(s32 i=0; i<m_itemcount; i++)\r
{\r
s32 j = i + start;\r
\r
- if(j > (s32)m_inventory->getSize() - 1)\r
- j -= m_inventory->getSize();\r
+ if(j > (s32)mainlist->getSize() - 1)\r
+ j -= mainlist->getSize();\r
if(j < 0)\r
- j += m_inventory->getSize();\r
+ j += mainlist->getSize();\r
\r
- InventoryItem *item = m_inventory->getItem(j);\r
+ InventoryItem *item = mainlist->getItem(j);\r
// Null items\r
if(item == NULL)\r
{\r
*/\r
\r
// This is a copy of the inventory that the client's environment has\r
- Inventory local_inventory(PLAYER_INVENTORY_SIZE);\r
+ Inventory local_inventory;\r
\r
GUIQuickInventory *quick_inventory = new GUIQuickInventory\r
(guienv, NULL, v2s32(10, 70), 5, &local_inventory);\r
\r
if((device->isWindowActive()\r
&& g_game_focused\r
- && !pauseMenu->isVisible()\r
- && !inventoryMenu->isVisible()\r
+ && noMenuActive()\r
)\r
|| random_input)\r
{\r
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("<not set>");
+ inventory.addList("main", PLAYER_INVENTORY_SIZE);
+ inventory.addList("craft", 9);
}
Player::~Player()
{
// 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);
}
// 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"<<std::endl;
return;
// Add to inventory and send inventory
InventoryItem *item = new MapBlockObjectItem
(obj->getInventoryString());
- player->inventory.addItem(item);
+ ilist->addItem(item);
SendInventory(player->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)
/*
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
/*
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
//dout_server<<"Placed object"<<std::endl;
- 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
- player->inventory.deleteItem(item_i);
+ ilist->deleteItem(item_i);
// Send inventory
SendInventory(peer_id);
}
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);
}*/
}
// 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);
}*/
}