just savin'

stable-0.2
Perttu Ahola 2010-12-22 11:29:06 +02:00
parent 4ec61b0ccd
commit 2e41a5e304
7 changed files with 346 additions and 114 deletions

View File

@ -149,46 +149,19 @@ void GUIInventoryMenu::resizeGui()
return; return;
m_screensize_old = screensize; m_screensize_old = screensize;
for(u32 i=0; i<m_slots.size(); i++)
{
m_slots[i]->remove();
}
m_slots.clear();
core::rect<s32> rect( core::rect<s32> rect(
screensize.X/2 - 560/2, screensize.X/2 - 560/2,
screensize.Y/2 - 300/2, screensize.Y/2 - 480/2,
screensize.X/2 + 560/2, screensize.X/2 + 560/2,
screensize.Y/2 + 300/2 screensize.Y/2 + 480/2
); );
DesiredRect = rect; DesiredRect = rect;
recalculateAbsolutePosition(false); 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() void GUIInventoryMenu::update()
{ {
for(s32 i=0; i<PLAYER_INVENTORY_SIZE; i++)
{
m_slots[i]->setItem(m_inventory->getItem(i));
}
} }
void GUIInventoryMenu::draw() void GUIInventoryMenu::draw()
@ -204,6 +177,51 @@ void GUIInventoryMenu::draw()
video::SColor bgcolor(140,0,0,0); video::SColor bgcolor(140,0,0,0);
driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect); 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(); gui::IGUIElement::draw();
} }

View File

@ -61,6 +61,7 @@ public:
void resizeGui(); void resizeGui();
// Updates stuff from inventory to screen // Updates stuff from inventory to screen
// TODO: Remove, not used
void update(); void update();
void draw(); void draw();
@ -80,7 +81,6 @@ public:
private: private:
Inventory *m_inventory; Inventory *m_inventory;
core::array<GUIInventorySlot*> m_slots;
v2u32 m_screensize_old; v2u32 m_screensize_old;
}; };

View File

@ -136,30 +136,39 @@ MapBlockObject * MapBlockObjectItem::createObject
Inventory Inventory
*/ */
Inventory::Inventory(u32 size) InventoryList::InventoryList(std::string name, u32 size)
{ {
m_name = name;
m_size = size; m_size = size;
clearItems(); clearItems();
} }
Inventory::~Inventory() InventoryList::~InventoryList()
{ {
for(u32 i=0; i<m_items.size(); i++) for(u32 i=0; i<m_items.size(); i++)
{ {
if(m_items[i])
delete 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(); m_items.clear();
for(u32 i=0; i<m_size; i++) for(u32 i=0; i<m_size; i++)
{ {
m_items.push_back(NULL); m_items.push_back(NULL);
} }
} }
void Inventory::serialize(std::ostream &os) void InventoryList::serialize(std::ostream &os)
{ {
//os.imbue(std::locale("C")); //os.imbue(std::locale("C"));
@ -181,7 +190,7 @@ void Inventory::serialize(std::ostream &os)
os<<"end\n"; os<<"end\n";
} }
void Inventory::deSerialize(std::istream &is) void InventoryList::deSerialize(std::istream &is)
{ {
//is.imbue(std::locale("C")); //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; m_size = other.m_size;
clearItems(); clearItems();
for(u32 i=0; i<other.m_items.size(); i++) for(u32 i=0; i<other.m_items.size(); i++)
@ -239,12 +258,17 @@ Inventory & Inventory::operator = (Inventory &other)
return *this; return *this;
} }
u32 Inventory::getSize() std::string InventoryList::getName()
{
return m_name;
}
u32 InventoryList::getSize()
{ {
return m_items.size(); return m_items.size();
} }
u32 Inventory::getUsedSlots() u32 InventoryList::getUsedSlots()
{ {
u32 num = 0; u32 num = 0;
for(u32 i=0; i<m_items.size(); i++) for(u32 i=0; i<m_items.size(); i++)
@ -256,14 +280,14 @@ u32 Inventory::getUsedSlots()
return num; return num;
} }
InventoryItem * Inventory::getItem(u32 i) InventoryItem * InventoryList::getItem(u32 i)
{ {
if(i > m_items.size() - 1) if(i > m_items.size() - 1)
return NULL; return NULL;
return m_items[i]; return m_items[i];
} }
InventoryItem * Inventory::changeItem(u32 i, InventoryItem *newitem) InventoryItem * InventoryList::changeItem(u32 i, InventoryItem *newitem)
{ {
assert(i < m_items.size()); assert(i < m_items.size());
@ -272,7 +296,7 @@ InventoryItem * Inventory::changeItem(u32 i, InventoryItem *newitem)
return olditem; return olditem;
} }
void Inventory::deleteItem(u32 i) void InventoryList::deleteItem(u32 i)
{ {
assert(i < m_items.size()); assert(i < m_items.size());
InventoryItem *item = changeItem(i, NULL); InventoryItem *item = changeItem(i, NULL);
@ -280,7 +304,7 @@ void Inventory::deleteItem(u32 i)
delete item; delete item;
} }
bool Inventory::addItem(InventoryItem *newitem) bool InventoryList::addItem(InventoryItem *newitem)
{ {
// If it is a MaterialItem, try to find an already existing one // If it is a MaterialItem, try to find an already existing one
// and just increment the counter // and just increment the counter
@ -324,9 +348,9 @@ bool Inventory::addItem(InventoryItem *newitem)
return false; 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++) for(u32 i=0; i<m_items.size(); i++)
{ {
InventoryItem *item = m_items[i]; InventoryItem *item = m_items[i];
@ -339,4 +363,129 @@ 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 //END

View File

@ -196,18 +196,21 @@ private:
std::string m_inventorystring; std::string m_inventorystring;
}; };
class Inventory class InventoryList
{ {
public: public:
Inventory(u32 size); InventoryList(std::string name, u32 size);
~Inventory(); ~InventoryList();
void clearItems(); void clearItems();
void serialize(std::ostream &os); void serialize(std::ostream &os);
void deSerialize(std::istream &is); void deSerialize(std::istream &is);
Inventory & operator = (Inventory &other); InventoryList(const InventoryList &other);
InventoryList & operator = (const InventoryList &other);
std::string getName();
u32 getSize(); u32 getSize();
// Count used slots
u32 getUsedSlots(); u32 getUsedSlots();
InventoryItem * getItem(u32 i); InventoryItem * getItem(u32 i);
@ -222,6 +225,40 @@ public:
private: private:
core::array<InventoryItem*> m_items; core::array<InventoryItem*> m_items;
u32 m_size; 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 #endif

View File

@ -172,6 +172,7 @@ SUGG: Split Inventory into ClientInventory and ServerInventory
Doing now: 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_material = 0;
u16 g_selected_item = 0; u16 g_selected_item = 0;
/*
GUI Stuff
*/
gui::IGUIEnvironment* guienv = NULL; gui::IGUIEnvironment* guienv = NULL;
GUIPauseMenu *pauseMenu = NULL; GUIPauseMenu *pauseMenu = NULL;
GUIInventoryMenu *inventoryMenu = 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; std::wstring g_text_buffer;
bool g_text_buffer_accepted = false; bool g_text_buffer_accepted = false;
@ -353,9 +366,7 @@ public:
{ {
if(event.KeyInput.Key == irr::KEY_ESCAPE) if(event.KeyInput.Key == irr::KEY_ESCAPE)
{ {
if(g_game_focused == true if(g_game_focused == true && noMenuActive())
&& !pauseMenu->isVisible()
&& !inventoryMenu->isVisible())
{ {
dstream<<DTIME<<"MyEventReceiver: " dstream<<DTIME<<"MyEventReceiver: "
<<"Launching pause menu"<<std::endl; <<"Launching pause menu"<<std::endl;
@ -369,9 +380,7 @@ public:
{ {
if(event.KeyInput.Key == irr::KEY_KEY_I) if(event.KeyInput.Key == irr::KEY_KEY_I)
{ {
if(g_game_focused == true if(g_game_focused == true && noMenuActive())
&& !inventoryMenu->isVisible()
&& !pauseMenu->isVisible())
{ {
dstream<<DTIME<<"MyEventReceiver: " dstream<<DTIME<<"MyEventReceiver: "
<<"Launching inventory"<<std::endl; <<"Launching inventory"<<std::endl;
@ -427,6 +436,14 @@ public:
} }
if(event.EventType == irr::EET_MOUSE_INPUT_EVENT) if(event.EventType == irr::EET_MOUSE_INPUT_EVENT)
{
if(noMenuActive() == false)
{
left_active = false;
middle_active = false;
right_active = false;
}
else
{ {
//dstream<<"MyEventReceiver: mouse input"<<std::endl; //dstream<<"MyEventReceiver: mouse input"<<std::endl;
left_active = event.MouseInput.isLeftPressed(); left_active = event.MouseInput.isLeftPressed();
@ -469,6 +486,7 @@ public:
} }
} }
} }
}
return false; return false;
} }
@ -933,16 +951,18 @@ public:
start = m_selection - m_itemcount / 2; start = m_selection - m_itemcount / 2;
InventoryList *mainlist = m_inventory->getList("main");
for(s32 i=0; i<m_itemcount; i++) for(s32 i=0; i<m_itemcount; i++)
{ {
s32 j = i + start; s32 j = i + start;
if(j > (s32)m_inventory->getSize() - 1) if(j > (s32)mainlist->getSize() - 1)
j -= m_inventory->getSize(); j -= mainlist->getSize();
if(j < 0) if(j < 0)
j += m_inventory->getSize(); j += mainlist->getSize();
InventoryItem *item = m_inventory->getItem(j); InventoryItem *item = mainlist->getItem(j);
// Null items // Null items
if(item == NULL) 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 // 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 GUIQuickInventory *quick_inventory = new GUIQuickInventory
(guienv, NULL, v2s32(10, 70), 5, &local_inventory); (guienv, NULL, v2s32(10, 70), 5, &local_inventory);
@ -1806,8 +1826,7 @@ int main(int argc, char *argv[])
if((device->isWindowActive() if((device->isWindowActive()
&& g_game_focused && g_game_focused
&& !pauseMenu->isVisible() && noMenuActive()
&& !inventoryMenu->isVisible()
) )
|| random_input) || random_input)
{ {

View File

@ -29,12 +29,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
Player::Player(): Player::Player():
touching_ground(false), touching_ground(false),
in_water(false), in_water(false),
inventory(PLAYER_INVENTORY_SIZE),
peer_id(PEER_ID_NEW), peer_id(PEER_ID_NEW),
m_speed(0,0,0), m_speed(0,0,0),
m_position(0,0,0) m_position(0,0,0)
{ {
updateName("<not set>"); updateName("<not set>");
inventory.addList("main", PLAYER_INVENTORY_SIZE);
inventory.addList("craft", 9);
} }
Player::~Player() Player::~Player()

View File

@ -1252,7 +1252,7 @@ void Server::AsyncRunStep()
{ {
// Add to inventory and send inventory // Add to inventory and send inventory
InventoryItem *item = new MaterialItem(material, 1); InventoryItem *item = new MaterialItem(material, 1);
player->inventory.addItem(item); player->inventory.addItem("main", item);
SendInventory(player->peer_id); SendInventory(player->peer_id);
} }
@ -1632,11 +1632,12 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
// Left click // Left click
if(button == 0) 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 // 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; dout_server<<"Player inventory has no free space"<<std::endl;
return; return;
@ -1645,7 +1646,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
// Add to inventory and send inventory // Add to inventory and send inventory
InventoryItem *item = new MapBlockObjectItem InventoryItem *item = new MapBlockObjectItem
(obj->getInventoryString()); (obj->getInventoryString());
player->inventory.addItem(item); ilist->addItem(item);
SendInventory(player->peer_id); SendInventory(player->peer_id);
} }
@ -1746,8 +1747,12 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
else if(action == 1) else if(action == 1)
{ {
InventoryList *ilist = player->inventory.getList("main");
if(ilist == NULL)
return;
// Get item // 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 there is no item, it is not possible to add it anywhere
if(item == NULL) if(item == NULL)
@ -1796,11 +1801,12 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
/* /*
Handle 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 // Remove from inventory and send inventory
if(mitem->getCount() == 1) if(mitem->getCount() == 1)
player->inventory.deleteItem(item_i); ilist->deleteItem(item_i);
else else
mitem->remove(1); mitem->remove(1);
// Send inventory // Send inventory
@ -1819,11 +1825,12 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
/* /*
Handle 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 // Remove from inventory and send inventory
if(mitem->getCount() == 1) if(mitem->getCount() == 1)
player->inventory.deleteItem(item_i); ilist->deleteItem(item_i);
else else
mitem->remove(1); mitem->remove(1);
// Send inventory // Send inventory
@ -1930,10 +1937,11 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
//dout_server<<"Placed object"<<std::endl; //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 // Remove from inventory and send inventory
player->inventory.deleteItem(item_i); ilist->deleteItem(item_i);
// Send inventory // Send inventory
SendInventory(peer_id); SendInventory(peer_id);
} }
@ -2190,18 +2198,18 @@ void Server::peerAdded(con::Peer *peer)
continue; continue;
InventoryItem *item = new MaterialItem(i, 1); InventoryItem *item = new MaterialItem(i, 1);
player->inventory.addItem(item); player->inventory.addItem("main", item);
} }
// Sign // Sign
{ {
InventoryItem *item = new MapBlockObjectItem("Sign Example text"); InventoryItem *item = new MapBlockObjectItem("Sign Example text");
bool r = player->inventory.addItem(item); bool r = player->inventory.addItem("main", item);
assert(r == true); assert(r == true);
} }
/*// Rat /*// Rat
{ {
InventoryItem *item = new MapBlockObjectItem("Rat"); InventoryItem *item = new MapBlockObjectItem("Rat");
bool r = player->inventory.addItem(item); bool r = player->inventory.addItem("main", item);
assert(r == true); assert(r == true);
}*/ }*/
} }
@ -2210,21 +2218,21 @@ void Server::peerAdded(con::Peer *peer)
// Give some lights // Give some lights
{ {
InventoryItem *item = new MaterialItem(3, 999); InventoryItem *item = new MaterialItem(3, 999);
bool r = player->inventory.addItem(item); bool r = player->inventory.addItem("main", item);
assert(r == true); assert(r == true);
} }
// and some signs // and some signs
for(u16 i=0; i<4; i++) for(u16 i=0; i<4; i++)
{ {
InventoryItem *item = new MapBlockObjectItem("Sign Example text"); InventoryItem *item = new MapBlockObjectItem("Sign Example text");
bool r = player->inventory.addItem(item); bool r = player->inventory.addItem("main", item);
assert(r == true); assert(r == true);
} }
/*// and some rats /*// and some rats
for(u16 i=0; i<4; i++) for(u16 i=0; i<4; i++)
{ {
InventoryItem *item = new MapBlockObjectItem("Rat"); InventoryItem *item = new MapBlockObjectItem("Rat");
bool r = player->inventory.addItem(item); bool r = player->inventory.addItem("main", item);
assert(r == true); assert(r == true);
}*/ }*/
} }