]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/guiEngine.cpp
make formspec textarea wordwrap
[dragonfireclient.git] / src / guiEngine.cpp
index 37f570acf1d469d1d59c2525564a40f3a705ed78..bba0006403a5e859a84617e8208469162b8277b5 100644 (file)
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "scripting_mainmenu.h"
 #include "config.h"
+#include "version.h"
 #include "porting.h"
 #include "filesys.h"
 #include "main.h"
@@ -28,6 +29,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "guiMainMenu.h"
 #include "sound.h"
 #include "sound_openal.h"
+#include "clouds.h"
+#include "httpfetch.h"
 
 #include <IGUIStaticText.h>
 #include <ICameraSceneNode.h>
@@ -36,6 +39,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <curl/curl.h>
 #endif
 
+/******************************************************************************/
+/** TextDestGuiEngine                                                         */
 /******************************************************************************/
 TextDestGuiEngine::TextDestGuiEngine(GUIEngine* engine)
 {
@@ -54,6 +59,38 @@ void TextDestGuiEngine::gotText(std::wstring text)
        m_engine->getScriptIface()->handleMainMenuEvent(wide_to_narrow(text));
 }
 
+/******************************************************************************/
+/** MenuTextureSource                                                         */
+/******************************************************************************/
+MenuTextureSource::MenuTextureSource(video::IVideoDriver *driver)
+{
+       m_driver = driver;
+}
+
+/******************************************************************************/
+MenuTextureSource::~MenuTextureSource()
+{
+       for (std::set<std::string>::iterator it = m_to_delete.begin();
+                       it != m_to_delete.end(); ++it) {
+               const char *tname = (*it).c_str();
+               video::ITexture *texture = m_driver->getTexture(tname);
+               m_driver->removeTexture(texture);
+       }
+}
+
+/******************************************************************************/
+video::ITexture* MenuTextureSource::getTexture(const std::string &name, u32 *id)
+{
+       if(id)
+               *id = 0;
+       if(name.empty())
+               return NULL;
+       m_to_delete.insert(name);
+       return m_driver->getTexture(name.c_str());
+}
+
+/******************************************************************************/
+/** MenuMusicFetcher                                                          */
 /******************************************************************************/
 void MenuMusicFetcher::fetchSounds(const std::string &name,
                        std::set<std::string> &dst_paths,
@@ -74,21 +111,26 @@ void MenuMusicFetcher::fetchSounds(const std::string &name,
                dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
 }
 
+/******************************************************************************/
+/** GUIEngine                                                                 */
 /******************************************************************************/
 GUIEngine::GUIEngine(  irr::IrrlichtDevice* dev,
                                                gui::IGUIElement* parent,
                                                IMenuManager *menumgr,
                                                scene::ISceneManager* smgr,
-                                               MainMenuData* data) :
+                                               MainMenuData* data,
+                                               bool& kill) :
        m_device(dev),
        m_parent(parent),
        m_menumanager(menumgr),
        m_smgr(smgr),
        m_data(data),
+       m_texture_source(NULL),
        m_sound_manager(NULL),
        m_formspecgui(0),
        m_buttonhandler(0),
        m_menu(0),
+       m_kill(kill),
        m_startgame(false),
        m_script(0),
        m_scriptdir(""),
@@ -103,6 +145,9 @@ GUIEngine::GUIEngine(       irr::IrrlichtDevice* dev,
        // is deleted by guiformspec!
        m_buttonhandler = new TextDestGuiEngine(this);
 
+       //create texture source
+       m_texture_source = new MenuTextureSource(m_device->getVideoDriver());
+
        //create soundmanager
        MenuMusicFetcher soundfetcher;
 #if USE_SOUND
@@ -112,9 +157,9 @@ GUIEngine::GUIEngine(       irr::IrrlichtDevice* dev,
                m_sound_manager = &dummySoundManager;
 
        //create topleft header
-       core::rect<s32> rect(0, 0, 500, 40);
+       core::rect<s32> rect(0, 0, 500, 20);
        rect += v2s32(4, 0);
-       std::string t = "Minetest " VERSION_STRING;
+       std::string t = std::string("Minetest ") + minetest_version_hash;
 
        m_irr_toplefttext =
                m_device->getGUIEnvironment()->addStaticText(narrow_to_wide(t).c_str(),
@@ -130,13 +175,13 @@ GUIEngine::GUIEngine(     irr::IrrlichtDevice* dev,
                                                                -1,
                                                                m_menumanager,
                                                                0 /* &client */,
-                                                               0 /* gamedef */);
+                                                               0 /* gamedef */,
+                                                               m_texture_source);
 
        m_menu->allowClose(false);
        m_menu->lockSize(true,v2u32(800,600));
        m_menu->setFormSource(m_formspecgui);
        m_menu->setTextDest(m_buttonhandler);
-       m_menu->useGettext(true);
 
        // Initialize scripting
 
@@ -181,7 +226,8 @@ bool GUIEngine::loadMainMenuScript()
                }
                else {
                        infostream
-                               << "GUIEngine: execution of custom menu failed!"
+                               << "GUIEngine: execution of custom menu: \""
+                               << menuscript << "\" failed!"
                                << std::endl
                                << "\tfalling back to builtin menu"
                                << std::endl;
@@ -220,7 +266,7 @@ void GUIEngine::run()
 
        cloudInit();
 
-       while(m_device->run() && (!m_startgame)) {
+       while(m_device->run() && (!m_startgame) && (!m_kill)) {
                driver->beginScene(true, true, video::SColor(255,140,186,250));
 
                if (m_clouds_enabled)
@@ -242,6 +288,8 @@ void GUIEngine::run()
                        cloudPostProcess();
                else
                        sleep_ms(25);
+
+               m_script->Step();
        }
 }
 
@@ -263,11 +311,13 @@ GUIEngine::~GUIEngine()
 
        m_irr_toplefttext->setText(L"");
 
-       //initialize texture pointers
+       //clean up texture pointers
        for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
                if (m_textures[i] != 0)
                        driver->removeTexture(m_textures[i]);
        }
+
+       delete m_texture_source;
        
        if (m_cloud.clouds)
                m_cloud.clouds->drop();
@@ -344,7 +394,7 @@ void GUIEngine::drawBackground(video::IVideoDriver* driver)
        }
 
        /* Draw background texture */
-       v2u32 sourcesize = texture->getSize();
+       v2u32 sourcesize = texture->getOriginalSize();
        driver->draw2DImage(texture,
                core::rect<s32>(0, 0, screensize.X, screensize.Y),
                core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
@@ -363,7 +413,7 @@ void GUIEngine::drawOverlay(video::IVideoDriver* driver)
                return;
 
        /* Draw background texture */
-       v2u32 sourcesize = texture->getSize();
+       v2u32 sourcesize = texture->getOriginalSize();
        driver->draw2DImage(texture,
                core::rect<s32>(0, 0, screensize.X, screensize.Y),
                core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
@@ -381,7 +431,7 @@ void GUIEngine::drawHeader(video::IVideoDriver* driver)
        if(!texture)
                return;
 
-       f32 mult = (((f32)screensize.Width / 2)) /
+       f32 mult = (((f32)screensize.Width / 2.0)) /
                        ((f32)texture->getOriginalSize().Width);
 
        v2s32 splashsize(((f32)texture->getOriginalSize().Width) * mult,
@@ -399,7 +449,7 @@ void GUIEngine::drawHeader(video::IVideoDriver* driver)
 
        driver->draw2DImage(texture, splashrect,
                core::rect<s32>(core::position2d<s32>(0,0),
-               core::dimension2di(texture->getSize())),
+               core::dimension2di(texture->getOriginalSize())),
                NULL, NULL, true);
        }
 }
@@ -431,7 +481,7 @@ void GUIEngine::drawFooter(video::IVideoDriver* driver)
 
                driver->draw2DImage(texture, rect,
                        core::rect<s32>(core::position2d<s32>(0,0),
-                       core::dimension2di(texture->getSize())),
+                       core::dimension2di(texture->getOriginalSize())),
                        NULL, NULL, true);
        }
 }
@@ -459,56 +509,42 @@ bool GUIEngine::setTexture(texture_layer layer,std::string texturepath) {
 }
 
 /******************************************************************************/
-#if USE_CURL
-static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
-{
-       FILE* targetfile = (FILE*) userp;
-       fwrite(contents,size,nmemb,targetfile);
-       return size * nmemb;
-}
-#endif
 bool GUIEngine::downloadFile(std::string url,std::string target) {
 #if USE_CURL
-       //download file via curl
-       CURL *curl;
-
-       curl = curl_easy_init();
-
-       if (curl)
-       {
-               CURLcode res;
-               bool retval = true;
+       bool retval = true;
 
-               FILE* targetfile = fopen(target.c_str(),"wb");
+       FILE* targetfile = fopen(target.c_str(),"wb");
 
-               if (targetfile) {
-                       curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
-                       curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
-                       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
-                       curl_easy_setopt(curl, CURLOPT_WRITEDATA, targetfile);
+       if (targetfile) {
+               HTTPFetchRequest fetchrequest;
+               HTTPFetchResult fetchresult;
+               fetchrequest.url = url;
+               fetchrequest.caller = HTTPFETCH_SYNC;
+               httpfetch_sync(fetchrequest,fetchresult);
 
-                       res = curl_easy_perform(curl);
-                       if (res != CURLE_OK) {
-                               errorstream << "File at url \"" << url
-                                       <<"\" not found (" << curl_easy_strerror(res) << ")" <<std::endl;
+               if (fetchresult.succeeded) {
+                       if (fwrite(fetchresult.data.c_str(),1,fetchresult.data.size(),targetfile) != fetchresult.data.size()) {
                                retval = false;
                        }
-                       fclose(targetfile);
                }
                else {
                        retval = false;
                }
-
-               curl_easy_cleanup(curl);
-               return retval;
+               fclose(targetfile);
        }
-#endif
+       else {
+               retval = false;
+       }
+
+       return retval;
+#else
        return false;
+#endif
 }
 
 /******************************************************************************/
 void GUIEngine::setTopleftText(std::string append) {
-       std::string toset = "Minetest " VERSION_STRING;
+       std::string toset = std::string("Minetest ") + minetest_version_hash;
 
        if (append != "") {
                toset += " / ";
@@ -530,3 +566,9 @@ void GUIEngine::stopSound(s32 handle)
 {
        m_sound_manager->stopSound(handle);
 }
+
+/******************************************************************************/
+unsigned int GUIEngine::DoAsync(std::string serialized_fct,
+               std::string serialized_params) {
+       return m_script->DoAsync(serialized_fct,serialized_params);
+}