Update inventory menu from formspec on-the-fly

This commit is contained in:
Perttu Ahola 2012-06-03 18:30:34 +03:00
parent 3ccb0f691b
commit a09d86dd3c
3 changed files with 55 additions and 10 deletions

@ -132,6 +132,28 @@ private:
Client *m_client; Client *m_client;
}; };
/* Form update callback */
class NodeMetadataFormSource: public IFormSource
{
public:
NodeMetadataFormSource(ClientMap *map, v3s16 p):
m_map(map),
m_p(p)
{
}
std::string getForm()
{
NodeMetadata *meta = m_map->getNodeMetadata(m_p);
if(!meta)
return "";
return meta->getString("formspec");
}
ClientMap *m_map;
v3s16 m_p;
};
/* /*
Hotbar draw routine Hotbar draw routine
*/ */
@ -2348,6 +2370,8 @@ void the_game(
&client, gamedef); &client, gamedef);
menu->setFormSpec(meta->getString("formspec"), menu->setFormSpec(meta->getString("formspec"),
inventoryloc); inventoryloc);
menu->setFormSource(new NodeMetadataFormSource(
&client.getEnv().getClientMap(), nodepos));
menu->drop(); menu->drop();
} }
// Otherwise report right click to server // Otherwise report right click to server

@ -126,23 +126,24 @@ GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
IMenuManager *menumgr, IMenuManager *menumgr,
InventoryManager *invmgr, InventoryManager *invmgr,
IGameDef *gamedef IGameDef *gamedef
): ):
GUIModalMenu(env, parent, id, menumgr), GUIModalMenu(env, parent, id, menumgr),
m_invmgr(invmgr), m_invmgr(invmgr),
m_gamedef(gamedef) m_gamedef(gamedef),
m_form_src(NULL),
m_selected_item(NULL),
m_selected_amount(0),
m_selected_dragging(false),
m_tooltip_element(NULL)
{ {
m_selected_item = NULL;
m_selected_amount = 0;
m_selected_dragging = false;
m_tooltip_element = NULL;
} }
GUIInventoryMenu::~GUIInventoryMenu() GUIInventoryMenu::~GUIInventoryMenu()
{ {
removeChildren(); removeChildren();
if(m_selected_item) delete m_selected_item;
delete m_selected_item; delete m_form_src;
} }
void GUIInventoryMenu::removeChildren() void GUIInventoryMenu::removeChildren()
@ -244,8 +245,6 @@ void GUIInventoryMenu::regenerateGui(v2u32 screensize)
pos.X += stof(f.next(",")) * (float)spacing.X; pos.X += stof(f.next(",")) * (float)spacing.X;
pos.Y += stof(f.next(";")) * (float)spacing.Y; pos.Y += stof(f.next(";")) * (float)spacing.Y;
v2s32 geom; v2s32 geom;
/*geom.X = imgsize.X + ((stoi(f.next(","))-1) * spacing.X);
geom.Y = imgsize.Y + ((stoi(f.next(";"))-1) * spacing.Y);*/
geom.X = stof(f.next(",")) * (float)imgsize.X; geom.X = stof(f.next(",")) * (float)imgsize.X;
geom.Y = stof(f.next(";")) * (float)imgsize.Y; geom.Y = stof(f.next(";")) * (float)imgsize.Y;
std::string name = f.next("]"); std::string name = f.next("]");
@ -430,6 +429,14 @@ void GUIInventoryMenu::drawSelectedItem()
void GUIInventoryMenu::drawMenu() void GUIInventoryMenu::drawMenu()
{ {
if(m_form_src){
std::string newform = m_form_src->getForm();
if(newform != m_formspec_string){
m_formspec_string = newform;
regenerateGui(m_screensize_old);
}
}
updateSelectedItem(); updateSelectedItem();
gui::IGUISkin* skin = Environment->getSkin(); gui::IGUISkin* skin = Environment->getSkin();

@ -30,6 +30,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class IGameDef; class IGameDef;
class InventoryManager; class InventoryManager;
class IFormSource
{
public:
virtual ~IFormSource(){}
virtual std::string getForm() = 0;
};
void drawItemStack(video::IVideoDriver *driver, void drawItemStack(video::IVideoDriver *driver,
gui::IGUIFont *font, gui::IGUIFont *font,
const ItemStack &item, const ItemStack &item,
@ -118,6 +125,12 @@ public:
regenerateGui(m_screensize_old); regenerateGui(m_screensize_old);
} }
// form_src is deleted by this GUIInventoryMenu
void setFormSource(IFormSource *form_src)
{
m_form_src = form_src;
}
void removeChildren(); void removeChildren();
/* /*
Remove and re-add (or reposition) stuff Remove and re-add (or reposition) stuff
@ -147,6 +160,7 @@ protected:
std::string m_formspec_string; std::string m_formspec_string;
InventoryLocation m_current_inventory_location; InventoryLocation m_current_inventory_location;
IFormSource *m_form_src;
core::array<ListDrawSpec> m_inventorylists; core::array<ListDrawSpec> m_inventorylists;
core::array<ImageDrawSpec> m_images; core::array<ImageDrawSpec> m_images;