3 Copyright (C) 2013 sapier
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include "guiMainMenu.h"
34 #include "guiEngine.h"
37 #include <curl/curl.h>
40 /******************************************************************************/
41 int menuscript_ErrorHandler(lua_State *L) {
42 lua_getfield(L, LUA_GLOBALSINDEX, "debug");
43 if (!lua_istable(L, -1)) {
47 lua_getfield(L, -1, "traceback");
48 if (!lua_isfunction(L, -1)) {
53 lua_pushinteger(L, 2);
58 /******************************************************************************/
59 TextDestGuiEngine::TextDestGuiEngine(GUIEngine* engine)
64 /******************************************************************************/
65 void TextDestGuiEngine::gotText(std::map<std::string, std::string> fields)
67 m_engine->handleButtons(fields);
70 /******************************************************************************/
71 void TextDestGuiEngine::gotText(std::wstring text)
73 m_engine->handleEvent(wide_to_narrow(text));
76 /******************************************************************************/
77 GUIEngine::GUIEngine( irr::IrrlichtDevice* dev,
78 gui::IGUIElement* parent,
79 IMenuManager *menumgr,
80 scene::ISceneManager* smgr,
84 m_menumanager(menumgr),
92 m_luaerrorhandler(-1),
95 m_clouds_enabled(true),
98 //initialize texture pointers
99 for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
102 // is deleted by guiformspec!
103 m_buttonhandler = new TextDestGuiEngine(this);
106 m_engineluastack = luaL_newstate();
108 //load basic lua modules
109 luaL_openlibs(m_engineluastack);
112 guiLuaApi::initialize(m_engineluastack,this);
115 if (m_data->errormessage != "")
117 lua_getglobal(m_engineluastack, "gamedata");
118 int gamedata_idx = lua_gettop(m_engineluastack);
119 lua_pushstring(m_engineluastack, "errormessage");
120 lua_pushstring(m_engineluastack,m_data->errormessage.c_str());
121 lua_settable(m_engineluastack, gamedata_idx);
122 m_data->errormessage = "";
125 //create topleft header
126 core::rect<s32> rect(0, 0, 500, 40);
128 std::string t = "Minetest " VERSION_STRING;
131 m_device->getGUIEnvironment()->addStaticText(narrow_to_wide(t).c_str(),
132 rect,false,true,0,-1);
134 //create formspecsource
135 m_formspecgui = new FormspecFormSource("",&m_formspecgui);
139 new GUIFormSpecMenu( m_device,
146 m_menu->allowClose(false);
147 m_menu->lockSize(true,v2u32(800,600));
148 m_menu->setFormSource(m_formspecgui);
149 m_menu->setTextDest(m_buttonhandler);
150 m_menu->useGettext(true);
152 std::string builtin_helpers
153 = porting::path_share + DIR_DELIM + "builtin"
154 + DIR_DELIM + "mainmenu_helper.lua";
156 if (!runScript(builtin_helpers)) {
158 << "GUIEngine::GUIEngine unable to load builtin helper script"
163 std::string menuscript = "";
164 if (g_settings->exists("main_menu_script"))
165 menuscript = g_settings->get("main_menu_script");
166 std::string builtin_menuscript =
167 porting::path_share + DIR_DELIM + "builtin"
168 + DIR_DELIM + "mainmenu.lua";
170 lua_pushcfunction(m_engineluastack, menuscript_ErrorHandler);
171 m_luaerrorhandler = lua_gettop(m_engineluastack);
173 m_scriptdir = menuscript.substr(0,menuscript.find_last_of(DIR_DELIM)-1);
174 if((menuscript == "") || (!runScript(menuscript))) {
176 << "GUIEngine::GUIEngine execution of custom menu failed!"
178 << "\tfalling back to builtin menu"
180 m_scriptdir = fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "builtin"+ DIR_DELIM);
181 if(!runScript(builtin_menuscript)) {
183 << "GUIEngine::GUIEngine unable to load builtin menu"
185 assert("no future without mainmenu" == 0);
191 m_menumanager->deletingMenu(m_menu);
196 /******************************************************************************/
197 bool GUIEngine::runScript(std::string script) {
199 int ret = luaL_loadfile(m_engineluastack, script.c_str()) ||
200 lua_pcall(m_engineluastack, 0, 0, m_luaerrorhandler);
202 errorstream<<"========== ERROR FROM LUA WHILE CREATING MAIN MENU ==========="<<std::endl;
203 errorstream<<"Failed to load and run script from "<<std::endl;
204 errorstream<<script<<":"<<std::endl;
205 errorstream<<std::endl;
206 errorstream<<lua_tostring(m_engineluastack, -1)<<std::endl;
207 errorstream<<std::endl;
208 errorstream<<"=================== END OF ERROR FROM LUA ===================="<<std::endl;
209 lua_pop(m_engineluastack, 1); // Pop error message from stack
210 lua_pop(m_engineluastack, 1); // Pop the error handler from stack
216 /******************************************************************************/
217 void GUIEngine::run()
220 // Always create clouds because they may or may not be
221 // needed based on the game selected
222 video::IVideoDriver* driver = m_device->getVideoDriver();
226 while(m_device->run() && (!m_startgame)) {
227 driver->beginScene(true, true, video::SColor(255,140,186,250));
229 if (m_clouds_enabled)
235 drawBackground(driver);
240 m_device->getGUIEnvironment()->drawAll();
244 if (m_clouds_enabled)
253 /******************************************************************************/
254 void GUIEngine::handleEvent(std::string text)
256 lua_getglobal(m_engineluastack, "engine");
258 lua_getfield(m_engineluastack, -1, "event_handler");
260 if(lua_isnil(m_engineluastack, -1))
263 luaL_checktype(m_engineluastack, -1, LUA_TFUNCTION);
265 lua_pushstring(m_engineluastack, text.c_str());
267 if(lua_pcall(m_engineluastack, 1, 0, m_luaerrorhandler))
268 scriptError("error: %s", lua_tostring(m_engineluastack, -1));
271 /******************************************************************************/
272 void GUIEngine::handleButtons(std::map<std::string, std::string> fields)
274 lua_getglobal(m_engineluastack, "engine");
276 lua_getfield(m_engineluastack, -1, "button_handler");
278 if(lua_isnil(m_engineluastack, -1))
281 luaL_checktype(m_engineluastack, -1, LUA_TFUNCTION);
283 lua_newtable(m_engineluastack);
284 for(std::map<std::string, std::string>::const_iterator
285 i = fields.begin(); i != fields.end(); i++){
286 const std::string &name = i->first;
287 const std::string &value = i->second;
288 lua_pushstring(m_engineluastack, name.c_str());
289 lua_pushlstring(m_engineluastack, value.c_str(), value.size());
290 lua_settable(m_engineluastack, -3);
293 if(lua_pcall(m_engineluastack, 1, 0, m_luaerrorhandler))
294 scriptError("error: %s", lua_tostring(m_engineluastack, -1));
297 /******************************************************************************/
298 GUIEngine::~GUIEngine()
300 video::IVideoDriver* driver = m_device->getVideoDriver();
303 //TODO: clean up m_menu here
305 lua_close(m_engineluastack);
307 m_irr_toplefttext->setText(L"");
309 //initialize texture pointers
310 for (unsigned int i = 0; i < TEX_LAYER_MAX; i++) {
311 if (m_textures[i] != 0)
312 driver->removeTexture(m_textures[i]);
316 m_cloud.clouds->drop();
319 /******************************************************************************/
320 void GUIEngine::cloudInit()
322 m_cloud.clouds = new Clouds(m_smgr->getRootSceneNode(),
323 m_smgr, -1, rand(), 100);
324 m_cloud.clouds->update(v2f(0, 0), video::SColor(255,200,200,255));
326 m_cloud.camera = m_smgr->addCameraSceneNode(0,
327 v3f(0,0,0), v3f(0, 60, 100));
328 m_cloud.camera->setFarValue(10000);
330 m_cloud.lasttime = m_device->getTimer()->getTime();
333 /******************************************************************************/
334 void GUIEngine::cloudPreProcess()
336 u32 time = m_device->getTimer()->getTime();
338 if(time > m_cloud.lasttime)
339 m_cloud.dtime = (time - m_cloud.lasttime) / 1000.0;
343 m_cloud.lasttime = time;
345 m_cloud.clouds->step(m_cloud.dtime*3);
346 m_cloud.clouds->render();
350 /******************************************************************************/
351 void GUIEngine::cloudPostProcess()
353 float fps_max = g_settings->getFloat("fps_max");
354 // Time of frame without fps limit
357 // not using getRealTime is necessary for wine
358 u32 time = m_device->getTimer()->getTime();
359 if(time > m_cloud.lasttime)
360 busytime_u32 = time - m_cloud.lasttime;
363 busytime = busytime_u32 / 1000.0;
366 u32 frametime_min = 1000./fps_max;
368 if(busytime_u32 < frametime_min) {
369 u32 sleeptime = frametime_min - busytime_u32;
370 m_device->sleep(sleeptime);
374 /******************************************************************************/
375 void GUIEngine::drawBackground(video::IVideoDriver* driver)
377 v2u32 screensize = driver->getScreenSize();
379 video::ITexture* texture = m_textures[TEX_LAYER_BACKGROUND];
381 /* If no texture, draw background of solid color */
383 video::SColor color(255,80,58,37);
384 core::rect<s32> rect(0, 0, screensize.X, screensize.Y);
385 driver->draw2DRectangle(color, rect, NULL);
389 /* Draw background texture */
390 v2u32 sourcesize = texture->getSize();
391 driver->draw2DImage(texture,
392 core::rect<s32>(0, 0, screensize.X, screensize.Y),
393 core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
397 /******************************************************************************/
398 void GUIEngine::drawOverlay(video::IVideoDriver* driver)
400 v2u32 screensize = driver->getScreenSize();
402 video::ITexture* texture = m_textures[TEX_LAYER_OVERLAY];
404 /* If no texture, draw background of solid color */
408 /* Draw background texture */
409 v2u32 sourcesize = texture->getSize();
410 driver->draw2DImage(texture,
411 core::rect<s32>(0, 0, screensize.X, screensize.Y),
412 core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
416 /******************************************************************************/
417 void GUIEngine::drawHeader(video::IVideoDriver* driver)
419 core::dimension2d<u32> screensize = driver->getScreenSize();
421 video::ITexture* texture = m_textures[TEX_LAYER_HEADER];
423 /* If no texture, draw nothing */
427 f32 mult = (((f32)screensize.Width / 2)) /
428 ((f32)texture->getOriginalSize().Width);
430 v2s32 splashsize(((f32)texture->getOriginalSize().Width) * mult,
431 ((f32)texture->getOriginalSize().Height) * mult);
433 // Don't draw the header is there isn't enough room
434 s32 free_space = (((s32)screensize.Height)-320)/2;
436 if (free_space > splashsize.Y) {
437 core::rect<s32> splashrect(0, 0, splashsize.X, splashsize.Y);
438 splashrect += v2s32((screensize.Width/2)-(splashsize.X/2),
439 ((free_space/2)-splashsize.Y/2)+10);
441 video::SColor bgcolor(255,50,50,50);
443 driver->draw2DImage(texture, splashrect,
444 core::rect<s32>(core::position2d<s32>(0,0),
445 core::dimension2di(texture->getSize())),
450 /******************************************************************************/
451 void GUIEngine::drawFooter(video::IVideoDriver* driver)
453 core::dimension2d<u32> screensize = driver->getScreenSize();
455 video::ITexture* texture = m_textures[TEX_LAYER_FOOTER];
457 /* If no texture, draw nothing */
461 f32 mult = (((f32)screensize.Width)) /
462 ((f32)texture->getOriginalSize().Width);
464 v2s32 footersize(((f32)texture->getOriginalSize().Width) * mult,
465 ((f32)texture->getOriginalSize().Height) * mult);
467 // Don't draw the footer if there isn't enough room
468 s32 free_space = (((s32)screensize.Height)-320)/2;
470 if (free_space > footersize.Y) {
471 core::rect<s32> rect(0,0,footersize.X,footersize.Y);
472 rect += v2s32(screensize.Width/2,screensize.Height-footersize.Y);
473 rect -= v2s32(footersize.X/2, 0);
475 driver->draw2DImage(texture, rect,
476 core::rect<s32>(core::position2d<s32>(0,0),
477 core::dimension2di(texture->getSize())),
482 /******************************************************************************/
483 bool GUIEngine::setTexture(texture_layer layer,std::string texturepath) {
485 video::IVideoDriver* driver = m_device->getVideoDriver();
488 if (m_textures[layer] != 0)
490 driver->removeTexture(m_textures[layer]);
491 m_textures[layer] = 0;
494 if ((texturepath == "") || !fs::PathExists(texturepath))
497 m_textures[layer] = driver->getTexture(texturepath.c_str());
499 if (m_textures[layer] == 0) return false;
504 /******************************************************************************/
506 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
508 FILE* targetfile = (FILE*) userp;
509 fwrite(contents,size,nmemb,targetfile);
513 bool GUIEngine::downloadFile(std::string url,std::string target) {
515 //download file via curl
518 curl = curl_easy_init();
525 FILE* targetfile = fopen(target.c_str(),"wb");
528 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
529 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
530 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
531 curl_easy_setopt(curl, CURLOPT_WRITEDATA, targetfile);
533 res = curl_easy_perform(curl);
534 if (res != CURLE_OK) {
535 errorstream << "File at url \"" << url
536 <<"\" not found (internet connection?)" <<std::endl;
545 curl_easy_cleanup(curl);
552 /******************************************************************************/
553 void GUIEngine::scriptError(const char *fmt, ...)
558 vsnprintf(buf, 10000, fmt, argp);
560 errorstream<<"MAINMENU ERROR: "<<buf;
563 /******************************************************************************/
564 void GUIEngine::setTopleftText(std::string append) {
565 std::string toset = "Minetest " VERSION_STRING;
572 m_irr_toplefttext->setText(narrow_to_wide(toset).c_str());