summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/guiInventoryMenu.cpp76
-rw-r--r--src/guiInventoryMenu.h2
-rw-r--r--src/inventory.cpp179
-rw-r--r--src/inventory.h45
-rw-r--r--src/main.cpp109
-rw-r--r--src/player.cpp3
-rw-r--r--src/server.cpp42
7 files changed, 344 insertions, 112 deletions
diff --git a/src/guiInventoryMenu.cpp b/src/guiInventoryMenu.cpp
index 370ebd5d1..7bc5ca9e0 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; 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()
@@ -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<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();
}
diff --git a/src/guiInventoryMenu.h b/src/guiInventoryMenu.h
index 8bfade4c4..b581a1166 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<GUIInventorySlot*> m_slots;
v2u32 m_screensize_old;
};
diff --git a/src/inventory.cpp b/src/inventory.cpp
index 3899c9394..069355aff 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(); 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"));
@@ -181,7 +190,7 @@ void Inventory::serialize(std::ostream &os)
os<<"end\n";
}
-void Inventory::deSerialize(std::istream &is)
+void InventoryList::deSerialize(std::istream &is)
{
//is.imbue(std::locale("C"));
@@ -223,8 +232,18 @@ void Inventory::deSerialize(std::istream &is)
}
}
-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++)
@@ -239,12 +258,17 @@ Inventory & Inventory::operator = (Inventory &other)
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++)
@@ -256,14 +280,14 @@ u32 Inventory::getUsedSlots()
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());
@@ -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:"<<std::endl;
+ o<<"InventoryList:"<<std::endl;
for(u32 i=0; i<m_items.size(); i++)
{
InventoryItem *item = m_items[i];
@@ -338,5 +362,130 @@ void Inventory::print(std::ostream &o)
}
}
}
+
+/*
+ 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
diff --git a/src/inventory.h b/src/inventory.h
index c18aa5dce..d37761cfe 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<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
diff --git a/src/main.cpp b/src/main.cpp
index 4b696236d..7fecbe3bd 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<<DTIME<<"MyEventReceiver: "
<<"Launching pause menu"<<std::endl;
@@ -369,9 +380,7 @@ public:
{
if(event.KeyInput.Key == irr::KEY_KEY_I)
{
- if(g_game_focused == true
- && !inventoryMenu->isVisible()
- && !pauseMenu->isVisible())
+ if(g_game_focused == true && noMenuActive())
{
dstream<<DTIME<<"MyEventReceiver: "
<<"Launching inventory"<<std::endl;
@@ -428,44 +437,53 @@ public:
if(event.EventType == irr::EET_MOUSE_INPUT_EVENT)
{
- //dstream<<"MyEventReceiver: mouse input"<<std::endl;
- left_active = event.MouseInput.isLeftPressed();
- middle_active = event.MouseInput.isMiddlePressed();
- right_active = event.MouseInput.isRightPressed();
-
- if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
- {
- leftclicked = true;
- }
- if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
- {
- rightclicked = true;
- }
- if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
+ if(noMenuActive() == false)
{
- leftreleased = true;
+ left_active = false;
+ middle_active = false;
+ right_active = false;
}
- if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
- {
- rightreleased = true;
- }
- if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
+ else
{
- /*dstream<<"event.MouseInput.Wheel="
- <<event.MouseInput.Wheel<<std::endl;*/
- if(event.MouseInput.Wheel < 0)
+ //dstream<<"MyEventReceiver: mouse input"<<std::endl;
+ left_active = event.MouseInput.isLeftPressed();
+ middle_active = event.MouseInput.isMiddlePressed();
+ right_active = event.MouseInput.isRightPressed();
+
+ if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
{
- if(g_selected_item < PLAYER_INVENTORY_SIZE-1)
- g_selected_item++;
- else
- g_selected_item = 0;
+ leftclicked = true;
}
- else if(event.MouseInput.Wheel > 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="
+ <<event.MouseInput.Wheel<<std::endl;*/
+ if(event.MouseInput.Wheel < 0)
+ {
+ if(g_selected_item < PLAYER_INVENTORY_SIZE-1)
+ g_selected_item++;
+ else
+ g_selected_item = 0;
+ }
+ else if(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<m_itemcount; i++)
{
s32 j = i + start;
- if(j > (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 d38d5eac8..e6caff03c 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("<not set>");
+ inventory.addList("main", PLAYER_INVENTORY_SIZE);
+ inventory.addList("craft", 9);
}
Player::~Player()
diff --git a/src/server.cpp b/src/server.cpp
index 9e809bb9a..9952cb8eb 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"<<std::endl;
return;
@@ -1645,7 +1646,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
// Add to inventory and send inventory
InventoryItem *item = new MapBlockObjectItem
(obj->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"<<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);
}
@@ -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);
}*/
}