]> git.lizzy.rs Git - minetest.git/blobdiff - src/guiEngine.cpp
Implement proper font handling
[minetest.git] / src / guiEngine.cpp
index ef018021e8fa150a2e5717f8c37df89fe1b00dfa..e941003fadb4ca799dfd0786aac38f0005753717 100644 (file)
@@ -19,7 +19,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "guiEngine.h"
 
+#include <IGUIStaticText.h>
+#include <ICameraSceneNode.h>
 #include "scripting_mainmenu.h"
+#include "util/numeric.h"
 #include "config.h"
 #include "version.h"
 #include "porting.h"
@@ -31,14 +34,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "sound_openal.h"
 #include "clouds.h"
 #include "httpfetch.h"
+#include "log.h"
+#include "fontengine.h"
 
-#include <IGUIStaticText.h>
-#include <ICameraSceneNode.h>
-
-#if USE_CURL
-#include <curl/curl.h>
+#ifdef __ANDROID__
+#include "tile.h"
+#include <GLES/gl.h>
 #endif
 
+
 /******************************************************************************/
 /** TextDestGuiEngine                                                         */
 /******************************************************************************/
@@ -86,6 +90,16 @@ video::ITexture* MenuTextureSource::getTexture(const std::string &name, u32 *id)
        if(name.empty())
                return NULL;
        m_to_delete.insert(name);
+
+#ifdef __ANDROID__
+       video::IImage *image = m_driver->createImageFromFile(name.c_str());
+       if (image) {
+               image = Align2Npot2(image, m_driver);
+               video::ITexture* retval = m_driver->addTexture(name.c_str(), image);
+               image->drop();
+               return retval;
+       }
+#endif
        return m_driver->getTexture(name.c_str());
 }
 
@@ -140,7 +154,7 @@ GUIEngine::GUIEngine(       irr::IrrlichtDevice* dev,
 {
        //initialize texture pointers
        for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
-               m_textures[i] = 0;
+               m_textures[i].texture = NULL;
        }
        // is deleted by guiformspec!
        m_buttonhandler = new TextDestGuiEngine(this);
@@ -157,12 +171,14 @@ GUIEngine::GUIEngine(     irr::IrrlichtDevice* dev,
                m_sound_manager = &dummySoundManager;
 
        //create topleft header
-       core::rect<s32> rect(0, 0, 500, 20);
+       std::wstring t = narrow_to_wide(std::string("Minetest ") +
+                       minetest_version_hash);
+
+       core::rect<s32> rect(0, 0, glb_fontengine->getTextWidth(t), glb_fontengine->getTextHeight());
        rect += v2s32(4, 0);
-       std::string t = std::string("Minetest ") + minetest_version_hash;
 
        m_irr_toplefttext =
-               m_device->getGUIEnvironment()->addStaticText(narrow_to_wide(t).c_str(),
+               m_device->getGUIEnvironment()->addStaticText(t.c_str(),
                rect,false,true,0,-1);
 
        //create formspecsource
@@ -209,7 +225,7 @@ GUIEngine::GUIEngine(       irr::IrrlichtDevice* dev,
 
        m_menu->quitMenu();
        m_menu->drop();
-       m_menu = 0;
+       m_menu = NULL;
 }
 
 /******************************************************************************/
@@ -238,14 +254,22 @@ bool GUIEngine::loadMainMenuScript()
 /******************************************************************************/
 void GUIEngine::run()
 {
-
        // Always create clouds because they may or may not be
        // needed based on the game selected
        video::IVideoDriver* driver = m_device->getVideoDriver();
 
        cloudInit();
 
-       while(m_device->run() && (!m_startgame) && (!m_kill)) {
+       unsigned int text_height = glb_fontengine->getTextHeight();
+
+       while(m_device->run() && (!m_startgame) && (!m_kill))
+       {
+               //check if we need to update the "upper left corner"-text
+               if (text_height != glb_fontengine->getTextHeight()) {
+                       updateTopLeftTextSize();
+                       text_height = glb_fontengine->getTextHeight();
+               }
+
                driver->beginScene(true, true, video::SColor(255,140,186,250));
 
                if (m_clouds_enabled)
@@ -269,6 +293,10 @@ void GUIEngine::run()
                        sleep_ms(25);
 
                m_script->step();
+
+#ifdef __ANDROID__
+               m_menu->getAndroidUIInput();
+#endif
        }
 }
 
@@ -283,8 +311,6 @@ GUIEngine::~GUIEngine()
                m_sound_manager = NULL;
        }
 
-       //TODO: clean up m_menu here
-
        infostream<<"GUIEngine: Deinitializing scripting"<<std::endl;
        delete m_script;
 
@@ -292,12 +318,12 @@ GUIEngine::~GUIEngine()
 
        //clean up texture pointers
        for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
-               if (m_textures[i] != 0)
-                       driver->removeTexture(m_textures[i]);
+               if (m_textures[i].texture != NULL)
+                       driver->removeTexture(m_textures[i].texture);
        }
 
        delete m_texture_source;
-       
+
        if (m_cloud.clouds)
                m_cloud.clouds->drop();
 }
@@ -362,7 +388,7 @@ void GUIEngine::drawBackground(video::IVideoDriver* driver)
 {
        v2u32 screensize = driver->getScreenSize();
 
-       video::ITexture* texture = m_textures[TEX_LAYER_BACKGROUND];
+       video::ITexture* texture = m_textures[TEX_LAYER_BACKGROUND].texture;
 
        /* If no texture, draw background of solid color */
        if(!texture){
@@ -372,8 +398,27 @@ void GUIEngine::drawBackground(video::IVideoDriver* driver)
                return;
        }
 
-       /* Draw background texture */
        v2u32 sourcesize = texture->getOriginalSize();
+
+       if (m_textures[TEX_LAYER_BACKGROUND].tile)
+       {
+               v2u32 tilesize(
+                               MYMAX(sourcesize.X,m_textures[TEX_LAYER_BACKGROUND].minsize),
+                               MYMAX(sourcesize.Y,m_textures[TEX_LAYER_BACKGROUND].minsize));
+               for (unsigned int x = 0; x < screensize.X; x += tilesize.X )
+               {
+                       for (unsigned int y = 0; y < screensize.Y; y += tilesize.Y )
+                       {
+                               driver->draw2DImage(texture,
+                                       core::rect<s32>(x, y, x+tilesize.X, y+tilesize.Y),
+                                       core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
+                                       NULL, NULL, true);
+                       }
+               }
+               return;
+       }
+
+       /* Draw background texture */
        driver->draw2DImage(texture,
                core::rect<s32>(0, 0, screensize.X, screensize.Y),
                core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
@@ -385,7 +430,7 @@ void GUIEngine::drawOverlay(video::IVideoDriver* driver)
 {
        v2u32 screensize = driver->getScreenSize();
 
-       video::ITexture* texture = m_textures[TEX_LAYER_OVERLAY];
+       video::ITexture* texture = m_textures[TEX_LAYER_OVERLAY].texture;
 
        /* If no texture, draw background of solid color */
        if(!texture)
@@ -404,7 +449,7 @@ void GUIEngine::drawHeader(video::IVideoDriver* driver)
 {
        core::dimension2d<u32> screensize = driver->getScreenSize();
 
-       video::ITexture* texture = m_textures[TEX_LAYER_HEADER];
+       video::ITexture* texture = m_textures[TEX_LAYER_HEADER].texture;
 
        /* If no texture, draw nothing */
        if(!texture)
@@ -438,7 +483,7 @@ void GUIEngine::drawFooter(video::IVideoDriver* driver)
 {
        core::dimension2d<u32> screensize = driver->getScreenSize();
 
-       video::ITexture* texture = m_textures[TEX_LAYER_FOOTER];
+       video::ITexture* texture = m_textures[TEX_LAYER_FOOTER].texture;
 
        /* If no texture, draw nothing */
        if(!texture)
@@ -466,47 +511,56 @@ void GUIEngine::drawFooter(video::IVideoDriver* driver)
 }
 
 /******************************************************************************/
-bool GUIEngine::setTexture(texture_layer layer,std::string texturepath) {
-
+bool GUIEngine::setTexture(texture_layer layer, std::string texturepath,
+               bool tile_image, unsigned int minsize)
+{
        video::IVideoDriver* driver = m_device->getVideoDriver();
        assert(driver != 0);
 
-       if (m_textures[layer] != 0)
+       if (m_textures[layer].texture != NULL)
        {
-               driver->removeTexture(m_textures[layer]);
-               m_textures[layer] = 0;
+               driver->removeTexture(m_textures[layer].texture);
+               m_textures[layer].texture = NULL;
        }
 
        if ((texturepath == "") || !fs::PathExists(texturepath))
+       {
                return false;
+       }
 
-       m_textures[layer] = driver->getTexture(texturepath.c_str());
+       m_textures[layer].texture = driver->getTexture(texturepath.c_str());
+       m_textures[layer].tile    = tile_image;
+       m_textures[layer].minsize = minsize;
 
-       if (m_textures[layer] == 0) return false;
+       if (m_textures[layer].texture == NULL)
+       {
+               return false;
+       }
 
        return true;
 }
 
 /******************************************************************************/
-bool GUIEngine::downloadFile(std::string url,std::string target) {
+bool GUIEngine::downloadFile(std::string url, std::string target)
+{
 #if USE_CURL
-       std::ofstream targetfile(target.c_str(), std::ios::out | std::ios::binary);
+       std::ofstream target_file(target.c_str(), std::ios::out | std::ios::binary);
 
-       if (!targetfile.good()) {
+       if (!target_file.good()) {
                return false;
        }
 
-       HTTPFetchRequest fetchrequest;
-       HTTPFetchResult fetchresult;
-       fetchrequest.url = url;
-       fetchrequest.caller = HTTPFETCH_SYNC;
-       httpfetch_sync(fetchrequest, fetchresult);
+       HTTPFetchRequest fetch_request;
+       HTTPFetchResult fetch_result;
+       fetch_request.url = url;
+       fetch_request.caller = HTTPFETCH_SYNC;
+       fetch_request.timeout = g_settings->getS32("curl_file_download_timeout");
+       httpfetch_sync(fetch_request, fetch_result);
 
-       if (fetchresult.succeeded) {
-               targetfile << fetchresult.data;
-       } else {
+       if (!fetch_result.succeeded) {
                return false;
        }
+       target_file << fetch_result.data;
 
        return true;
 #else
@@ -515,15 +569,34 @@ bool GUIEngine::downloadFile(std::string url,std::string target) {
 }
 
 /******************************************************************************/
-void GUIEngine::setTopleftText(std::string append) {
-       std::string toset = std::string("Minetest ") + minetest_version_hash;
+void GUIEngine::setTopleftText(std::string append)
+{
+       std::wstring toset = narrow_to_wide( std::string("Minetest ") +
+                       minetest_version_hash);
 
-       if (append != "") {
-               toset += " / ";
-               toset += append;
+       if (append != "")
+       {
+               toset += L" / ";
+               toset += narrow_to_wide(append);
        }
 
-       m_irr_toplefttext->setText(narrow_to_wide(toset).c_str());
+       m_irr_toplefttext->setText(toset.c_str());
+
+       updateTopLeftTextSize();
+}
+
+/******************************************************************************/
+void GUIEngine::updateTopLeftTextSize()
+{
+       std::wstring text = m_irr_toplefttext->getText();
+
+       core::rect<s32> rect(0, 0, glb_fontengine->getTextWidth(text), glb_fontengine->getTextHeight());
+               rect += v2s32(4, 0);
+
+       m_irr_toplefttext->remove();
+       m_irr_toplefttext =
+               m_device->getGUIEnvironment()->addStaticText(text.c_str(),
+               rect,false,true,0,-1);
 }
 
 /******************************************************************************/
@@ -541,7 +614,8 @@ void GUIEngine::stopSound(s32 handle)
 
 /******************************************************************************/
 unsigned int GUIEngine::queueAsync(std::string serialized_func,
-               std::string serialized_params) {
+               std::string serialized_params)
+{
        return m_script->queueAsync(serialized_func, serialized_params);
 }