From 7bf446f671541f38d6dc89d207ee6effd3564483 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sun, 3 Jun 2012 16:03:19 +0300 Subject: Reshape inventory menu code --- src/game.cpp | 37 +++------- src/guiInventoryMenu.cpp | 177 ++++++++++++++++------------------------------- src/guiInventoryMenu.h | 44 ++---------- 3 files changed, 78 insertions(+), 180 deletions(-) (limited to 'src') diff --git a/src/game.cpp b/src/game.cpp index 815a5dd85..5bc3e7625 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1569,24 +1569,18 @@ void the_game( GUIInventoryMenu *menu = new GUIInventoryMenu(guienv, guiroot, -1, - &g_menumgr, v2s16(8,7), + &g_menumgr, &client, gamedef); InventoryLocation inventoryloc; inventoryloc.setCurrentPlayer(); - core::array draw_spec; - draw_spec.push_back(GUIInventoryMenu::DrawSpec( - "list", inventoryloc, "main", - v2s32(0, 3), v2s32(8, 4))); - draw_spec.push_back(GUIInventoryMenu::DrawSpec( - "list", inventoryloc, "craft", - v2s32(3, 0), v2s32(3, 3))); - draw_spec.push_back(GUIInventoryMenu::DrawSpec( - "list", inventoryloc, "craftpreview", - v2s32(7, 1), v2s32(1, 1))); - - menu->setDrawSpec(draw_spec); + menu->setFormSpec( + "invsize[8,7;]" + "list[current_player;main;0,3;8,4;]" + "list[current_player;craft;3,0;3,3;]" + "list[current_player;craftpreview;7,1;1,1;]" + , inventoryloc); menu->drop(); } @@ -2345,23 +2339,14 @@ void the_game( InventoryLocation inventoryloc; inventoryloc.setNodeMeta(nodepos); - - /* - Create menu - */ - - core::array draw_spec; - v2s16 invsize = - GUIInventoryMenu::makeDrawSpecArrayFromString( - draw_spec, - meta->getString("formspec"), - inventoryloc); + /* Create menu */ GUIInventoryMenu *menu = new GUIInventoryMenu(guienv, guiroot, -1, - &g_menumgr, invsize, + &g_menumgr, &client, gamedef); - menu->setDrawSpec(draw_spec); + menu->setFormSpec(meta->getString("formspec"), + inventoryloc); menu->drop(); } // Otherwise report right click to server diff --git a/src/guiInventoryMenu.cpp b/src/guiInventoryMenu.cpp index e637ab78a..859eba1be 100644 --- a/src/guiInventoryMenu.cpp +++ b/src/guiInventoryMenu.cpp @@ -123,12 +123,10 @@ void drawItemStack(video::IVideoDriver *driver, GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env, gui::IGUIElement* parent, s32 id, IMenuManager *menumgr, - v2s16 menu_size, InventoryManager *invmgr, IGameDef *gamedef ): GUIModalMenu(env, parent, id, menumgr), - m_menu_size(menu_size), m_invmgr(invmgr), m_gamedef(gamedef) { @@ -178,42 +176,72 @@ void GUIInventoryMenu::regenerateGui(v2u32 screensize) // Remove children removeChildren(); - /*padding = v2s32(24,24); - spacing = v2s32(60,56); - imgsize = v2s32(48,48);*/ - - padding = v2s32(screensize.Y/40, screensize.Y/40); - spacing = v2s32(screensize.Y/12, screensize.Y/13); - imgsize = v2s32(screensize.Y/15, screensize.Y/15); - + v2s32 size(100,100); s32 helptext_h = 15; - - v2s32 size( - padding.X*2+spacing.X*(m_menu_size.X-1)+imgsize.X, - padding.Y*2+spacing.Y*(m_menu_size.Y-1)+imgsize.Y + helptext_h - ); - - core::rect rect( - screensize.X/2 - size.X/2, - screensize.Y/2 - size.Y/2, - screensize.X/2 + size.X/2, - screensize.Y/2 + size.Y/2 - ); - - DesiredRect = rect; - recalculateAbsolutePosition(false); - + core::rect rect; v2s32 basepos = getBasePos(); + /* Convert m_init_draw_spec to m_draw_spec */ + m_draw_spec.clear(); - for(u16 i=0; i( + screensize.X/2 - size.X/2, + screensize.Y/2 - size.Y/2, + screensize.X/2 + size.X/2, + screensize.Y/2 + size.Y/2 + ); + DesiredRect = rect; + recalculateAbsolutePosition(false); + basepos = getBasePos(); + } + else if(type == "list") + { + std::string name = f.next(";"); + InventoryLocation loc; + if(name == "context" || name == "current_name") + loc = m_current_inventory_location; + else + loc.deSerialize(name); + std::string listname = f.next(";"); + s32 pos_x = stoi(f.next(",")); + s32 pos_y = stoi(f.next(";")); + s32 geom_w = stoi(f.next(",")); + s32 geom_h = stoi(f.next(";")); + infostream<<"list inv="<OnEvent(event) : false; } -/* - Here is an example traditional set-up sequence for a DrawSpec list: - - std::string furnace_inv_id = "nodemetadata:0,1,2"; - core::array draw_spec; - draw_spec.push_back(GUIInventoryMenu::DrawSpec( - "list", furnace_inv_id, "fuel", - v2s32(2, 3), v2s32(1, 1))); - draw_spec.push_back(GUIInventoryMenu::DrawSpec( - "list", furnace_inv_id, "src", - v2s32(2, 1), v2s32(1, 1))); - draw_spec.push_back(GUIInventoryMenu::DrawSpec( - "list", furnace_inv_id, "dst", - v2s32(5, 1), v2s32(2, 2))); - draw_spec.push_back(GUIInventoryMenu::DrawSpec( - "list", "current_player", "main", - v2s32(0, 5), v2s32(8, 4))); - setDrawSpec(draw_spec); - - Here is the string for creating the same DrawSpec list (a single line, - spread to multiple lines here): - - GUIInventoryMenu::makeDrawSpecArrayFromString( - draw_spec, - "nodemetadata:0,1,2", - "invsize[8,9;]" - "list[current_name;fuel;2,3;1,1;]" - "list[current_name;src;2,1;1,1;]" - "list[current_name;dst;5,1;2,2;]" - "list[current_player;main;0,5;8,4;]"); - - Returns inventory menu size defined by invsize[]. -*/ -v2s16 GUIInventoryMenu::makeDrawSpecArrayFromString( - core::array &draw_spec, - const std::string &data, - const InventoryLocation ¤t_location) -{ - v2s16 invsize(8,9); - Strfnd f(data); - while(f.atend() == false) - { - std::string type = trim(f.next("[")); - //infostream<<"type="< &draw_spec, - const std::string &data, - const InventoryLocation ¤t_location); - GUIInventoryMenu(gui::IGUIEnvironment* env, gui::IGUIElement* parent, s32 id, IMenuManager *menumgr, - v2s16 menu_size, InventoryManager *invmgr, IGameDef *gamedef ); ~GUIInventoryMenu(); - void setDrawSpec(core::array &init_draw_spec) + void setFormSpec(const std::string &formspec_string, + InventoryLocation current_inventory_location) { - m_init_draw_spec = init_draw_spec; + m_formspec_string = formspec_string; + m_current_inventory_location = current_inventory_location; + regenerateGui(m_screensize_old); } void removeChildren(); @@ -149,8 +120,6 @@ protected: return padding + AbsoluteRect.UpperLeftCorner; } - v2s16 m_menu_size; - v2s32 padding; v2s32 spacing; v2s32 imgsize; @@ -158,7 +127,8 @@ protected: InventoryManager *m_invmgr; IGameDef *m_gamedef; - core::array m_init_draw_spec; + std::string m_formspec_string; + InventoryLocation m_current_inventory_location; core::array m_draw_spec; ItemSpec *m_selected_item; -- cgit v1.2.3