]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/guiFormSpecMenu.cpp
Send a on_receive_fields event when formspec is closed, with fields.quit = "true"
[dragonfireclient.git] / src / guiFormSpecMenu.cpp
index b162e8e992b2841e937e7e210604fd21260dbe3c..f3d4568ef6303e097bae3362560ff1fb0420167d 100644 (file)
@@ -69,12 +69,14 @@ GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
                gui::IGUIElement* parent, s32 id,
                IMenuManager *menumgr,
                InventoryManager *invmgr,
-               IGameDef *gamedef
+               IGameDef *gamedef,
+               ISimpleTextureSource *tsrc
 ):
        GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
        m_device(dev),
        m_invmgr(invmgr),
        m_gamedef(gamedef),
+       m_tsrc(tsrc),
        m_form_src(NULL),
        m_text_dst(NULL),
        m_selected_item(NULL),
@@ -124,11 +126,14 @@ void GUIFormSpecMenu::removeChildren()
                if(e != NULL)
                        e->remove();
        }*/
+
        if(m_tooltip_element)
        {
                m_tooltip_element->remove();
+               m_tooltip_element->drop();
                m_tooltip_element = NULL;
        }
+
 }
 
 void GUIFormSpecMenu::setInitialFocus()
@@ -480,7 +485,7 @@ void GUIFormSpecMenu::parseImage(parserData* data,std::string element) {
        if (parts.size() == 3) {
                std::vector<std::string> v_pos = split(parts[0],',');
                std::vector<std::string> v_geom = split(parts[1],',');
-               std::string name = parts[2];
+               std::string name = unescape_string(parts[2]);
 
                MY_CHECKPOS("image",0);
                MY_CHECKGEOM("image",1);
@@ -501,7 +506,7 @@ void GUIFormSpecMenu::parseImage(parserData* data,std::string element) {
 
        if (parts.size() == 2) {
                std::vector<std::string> v_pos = split(parts[0],',');
-               std::string name = parts[1];
+               std::string name = unescape_string(parts[1]);
 
                MY_CHECKPOS("image",0);
 
@@ -602,7 +607,7 @@ void GUIFormSpecMenu::parseBackground(parserData* data,std::string element) {
        if (parts.size() == 3) {
                std::vector<std::string> v_pos = split(parts[0],',');
                std::vector<std::string> v_geom = split(parts[1],',');
-               std::string name = parts[2];
+               std::string name = unescape_string(parts[2]);
 
                MY_CHECKPOS("background",0);
                MY_CHECKGEOM("background",1);
@@ -766,11 +771,6 @@ void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element) {
                if (str_initial_selection != "")
                        e->setSelected(stoi(str_initial_selection.c_str())-1);
 
-               //if (data->listbox_selections.find(fname_w) != data->listbox_selections.end()) {
-               //      e->setSelected(data->listbox_selections[fname_w]);
-               //}
-
-               //m_listboxes.push_back(std::pair<FieldSpec,gui::IGUIListBox*>(spec,e));
                m_fields.push_back(spec);
                return;
        }
@@ -1144,8 +1144,10 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,std:
                core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
 
                if(data->bp_set != 2)
-                       errorstream<<"WARNING: invalid use of item_image_button without a size[] element"<<std::endl;
+                       errorstream<<"WARNING: invalid use of image_button without a size[] element"<<std::endl;
 
+               image_name = unescape_string(image_name);
+               pressed_image_name = unescape_string(pressed_image_name);
                label = unescape_string(label);
 
                std::wstring wlabel = narrow_to_wide(label.c_str());
@@ -1162,24 +1164,10 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,std:
 
                video::ITexture *texture = 0;
                video::ITexture *pressed_texture = 0;
-               //if there's no gamedef specified try to get direct
-               //TODO check for possible texture leak
-               if (m_gamedef != 0) {
-                       texture = m_gamedef->tsrc()->getTexture(image_name);
-                       if ((parts.size() == 8)) {
-                               pressed_texture = m_gamedef->tsrc()->getTexture(pressed_image_name);
-                       }
-               } else {
-                       if (fs::PathExists(image_name)) {
-                               texture = Environment->getVideoDriver()->getTexture(image_name.c_str());
-                               m_Textures.push_back(texture);
-                       }
-                       if (fs::PathExists(pressed_image_name)) {
-                               pressed_texture = Environment->getVideoDriver()->getTexture(pressed_image_name.c_str());
-                               m_Textures.push_back(pressed_texture);
-                       }
-               }
-               if (parts.size() < 8)
+               texture = m_tsrc->getTexture(image_name);
+               if (parts.size() == 8)
+                       pressed_texture = m_tsrc->getTexture(pressed_image_name);
+               else
                        pressed_texture = texture;
 
                gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
@@ -1606,6 +1594,8 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
                m_tooltip_element->setOverrideColor(video::SColor(255,255,255,255));
                m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
                m_tooltip_element->setWordWrap(false);
+               //we're not parent so no autograb for this one!
+               m_tooltip_element->grab();
        }
 
        //set initial focus if parser didn't set it
@@ -1792,15 +1782,7 @@ void GUIFormSpecMenu::drawMenu()
        for(u32 i=0; i<m_backgrounds.size(); i++)
        {
                const ImageDrawSpec &spec = m_backgrounds[i];
-               video::ITexture *texture = 0;
-
-               if (m_gamedef != 0)
-                       texture = m_gamedef->tsrc()->getTexture(spec.name);
-               else
-               {
-                       texture = driver->getTexture(spec.name.c_str());
-                       m_Textures.push_back(texture);
-               }
+               video::ITexture *texture = m_tsrc->getTexture(spec.name);
 
                if (texture != 0) {
                        // Image size on screen
@@ -1842,15 +1824,8 @@ void GUIFormSpecMenu::drawMenu()
        for(u32 i=0; i<m_images.size(); i++)
        {
                const ImageDrawSpec &spec = m_images[i];
-               video::ITexture *texture = 0;
+               video::ITexture *texture = m_tsrc->getTexture(spec.name);
 
-               if (m_gamedef != 0)
-                       texture = m_gamedef->tsrc()->getTexture(spec.name);
-               else
-               {
-                       texture = driver->getTexture(spec.name.c_str());
-                       m_Textures.push_back(texture);
-               }
                if (texture != 0) {
                        const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
                        // Image size on screen
@@ -2066,12 +2041,16 @@ ItemStack GUIFormSpecMenu::verifySelectedItem()
        return ItemStack();
 }
 
-void GUIFormSpecMenu::acceptInput()
+void GUIFormSpecMenu::acceptInput(bool quit=false)
 {
        if(m_text_dst)
        {
                std::map<std::string, std::string> fields;
 
+               if (quit) {
+                       fields["quit"] = "true";
+               }
+
                if (current_keys_pending.key_down) {
                        fields["key_down"] = "true";
                        current_keys_pending.key_down = false;
@@ -2213,10 +2192,12 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
                if (event.KeyInput.PressedDown && (kp == EscapeKey ||
                        kp == getKeySetting("keymap_inventory")))
                {
-                       if (m_allowclose)
+                       if (m_allowclose) {
+                               acceptInput(true);
                                quitMenu();
-                       else
+                        } else {
                                m_text_dst->gotText(narrow_to_wide("MenuQuit"));
+                       }
                        return true;
                }
                if (event.KeyInput.PressedDown &&
@@ -2229,7 +2210,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
                        switch (event.KeyInput.Key) {
                                case KEY_RETURN:
                                        if (m_allowclose) {
-                                               acceptInput();
+                                               acceptInput(true);
                                                quitMenu();
                                        }
                                        else
@@ -2576,11 +2557,13 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
                        unsigned int btn_id = event.GUIEvent.Caller->getID();
 
                        if (btn_id == 257) {
-                               acceptInput();
-                               if (m_allowclose)
+                               if (m_allowclose) {
+                                       acceptInput(true);
                                        quitMenu();
-                               else
+                               } else {
+                                       acceptInput();
                                        m_text_dst->gotText(narrow_to_wide("ExitButton"));
+                               }
                                // quitMenu deallocates menu
                                return true;
                        }
@@ -2597,10 +2580,12 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
                                        s.send = true;
                                        acceptInput();
                                        if(s.is_exit){
-                                               if (m_allowclose)
+                                               if (m_allowclose) {
+                                                       acceptInput(true);
                                                        quitMenu();
-                                               else
+                                               } else {
                                                        m_text_dst->gotText(narrow_to_wide("ExitButton"));
+                                               }
                                                return true;
                                        }else{
                                                s.send = false;
@@ -2615,7 +2600,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
                        {
 
                                if (m_allowclose) {
-                                       acceptInput();
+                                       acceptInput(true);
                                        quitMenu();
                                }
                                else {