guiEngine.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. Minetest
  3. Copyright (C) 2013 sapier
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. /******************************************************************************/
  18. /* Includes */
  19. /******************************************************************************/
  20. #include "irrlichttypes.h"
  21. #include "guiFormSpecMenu.h"
  22. #include "client/sound.h"
  23. #include "client/tile.h"
  24. #include "util/enriched_string.h"
  25. /******************************************************************************/
  26. /* Structs and macros */
  27. /******************************************************************************/
  28. /** texture layer ids */
  29. enum texture_layer {
  30. TEX_LAYER_BACKGROUND = 0,
  31. TEX_LAYER_OVERLAY,
  32. TEX_LAYER_HEADER,
  33. TEX_LAYER_FOOTER,
  34. TEX_LAYER_MAX
  35. };
  36. struct image_definition {
  37. video::ITexture *texture = nullptr;
  38. bool tile;
  39. unsigned int minsize;
  40. };
  41. /******************************************************************************/
  42. /* forward declarations */
  43. /******************************************************************************/
  44. class GUIEngine;
  45. class MainMenuScripting;
  46. class Clouds;
  47. struct MainMenuData;
  48. /******************************************************************************/
  49. /* declarations */
  50. /******************************************************************************/
  51. /** GUIEngine specific implementation of TextDest used within guiFormSpecMenu */
  52. class TextDestGuiEngine : public TextDest
  53. {
  54. public:
  55. /**
  56. * default constructor
  57. * @param engine the engine data is transmitted for further processing
  58. */
  59. TextDestGuiEngine(GUIEngine* engine) : m_engine(engine) {};
  60. /**
  61. * receive fields transmitted by guiFormSpecMenu
  62. * @param fields map containing formspec field elements currently active
  63. */
  64. void gotText(const StringMap &fields);
  65. /**
  66. * receive text/events transmitted by guiFormSpecMenu
  67. * @param text textual representation of event
  68. */
  69. void gotText(const std::wstring &text);
  70. private:
  71. /** target to transmit data to */
  72. GUIEngine *m_engine = nullptr;
  73. };
  74. /** GUIEngine specific implementation of ISimpleTextureSource */
  75. class MenuTextureSource : public ISimpleTextureSource
  76. {
  77. public:
  78. /**
  79. * default constructor
  80. * @param driver the video driver to load textures from
  81. */
  82. MenuTextureSource(video::IVideoDriver *driver) : m_driver(driver) {};
  83. /**
  84. * destructor, removes all loaded textures
  85. */
  86. virtual ~MenuTextureSource();
  87. /**
  88. * get a texture, loading it if required
  89. * @param name path to the texture
  90. * @param id receives the texture ID, always 0 in this implementation
  91. */
  92. video::ITexture *getTexture(const std::string &name, u32 *id = NULL);
  93. private:
  94. /** driver to get textures from */
  95. video::IVideoDriver *m_driver = nullptr;
  96. /** set of texture names to delete */
  97. std::set<std::string> m_to_delete;
  98. };
  99. /** GUIEngine specific implementation of OnDemandSoundFetcher */
  100. class MenuMusicFetcher: public OnDemandSoundFetcher
  101. {
  102. public:
  103. /**
  104. * get sound file paths according to sound name
  105. * @param name sound name
  106. * @param dst_paths receives possible paths to sound files
  107. * @param dst_datas receives binary sound data (not used here)
  108. */
  109. void fetchSounds(const std::string &name,
  110. std::set<std::string> &dst_paths,
  111. std::set<std::string> &dst_datas);
  112. private:
  113. /** set of fetched sound names */
  114. std::set<std::string> m_fetched;
  115. };
  116. /** implementation of main menu based uppon formspecs */
  117. class GUIEngine {
  118. /** grant ModApiMainMenu access to private members */
  119. friend class ModApiMainMenu;
  120. friend class ModApiSound;
  121. public:
  122. /**
  123. * default constructor
  124. * @param dev device to draw at
  125. * @param parent parent gui element
  126. * @param menumgr manager to add menus to
  127. * @param smgr scene manager to add scene elements to
  128. * @param data struct to transfer data to main game handling
  129. */
  130. GUIEngine(JoystickController *joystick,
  131. gui::IGUIElement *parent,
  132. IMenuManager *menumgr,
  133. MainMenuData *data,
  134. bool &kill);
  135. /** default destructor */
  136. virtual ~GUIEngine();
  137. /**
  138. * return MainMenuScripting interface
  139. */
  140. MainMenuScripting *getScriptIface()
  141. {
  142. return m_script;
  143. }
  144. /**
  145. * return dir of current menuscript
  146. */
  147. std::string getScriptDir()
  148. {
  149. return m_scriptdir;
  150. }
  151. /** pass async callback to scriptengine **/
  152. unsigned int queueAsync(const std::string &serialized_fct,
  153. const std::string &serialized_params);
  154. private:
  155. /** find and run the main menu script */
  156. bool loadMainMenuScript();
  157. /** run main menu loop */
  158. void run();
  159. /** update size of topleftext element */
  160. void updateTopLeftTextSize();
  161. /** parent gui element */
  162. gui::IGUIElement *m_parent = nullptr;
  163. /** manager to add menus to */
  164. IMenuManager *m_menumanager = nullptr;
  165. /** scene manager to add scene elements to */
  166. scene::ISceneManager *m_smgr = nullptr;
  167. /** pointer to data beeing transfered back to main game handling */
  168. MainMenuData *m_data = nullptr;
  169. /** pointer to texture source */
  170. ISimpleTextureSource *m_texture_source = nullptr;
  171. /** pointer to soundmanager*/
  172. ISoundManager *m_sound_manager = nullptr;
  173. /** representation of form source to be used in mainmenu formspec */
  174. FormspecFormSource *m_formspecgui = nullptr;
  175. /** formspec input receiver */
  176. TextDestGuiEngine *m_buttonhandler = nullptr;
  177. /** the formspec menu */
  178. GUIFormSpecMenu *m_menu = nullptr;
  179. /** reference to kill variable managed by SIGINT handler */
  180. bool &m_kill;
  181. /** variable used to abort menu and return back to main game handling */
  182. bool m_startgame = false;
  183. /** scripting interface */
  184. MainMenuScripting *m_script = nullptr;
  185. /** script basefolder */
  186. std::string m_scriptdir = "";
  187. void setFormspecPrepend(const std::string &fs);
  188. /**
  189. * draw background layer
  190. * @param driver to use for drawing
  191. */
  192. void drawBackground(video::IVideoDriver *driver);
  193. /**
  194. * draw overlay layer
  195. * @param driver to use for drawing
  196. */
  197. void drawOverlay(video::IVideoDriver *driver);
  198. /**
  199. * draw header layer
  200. * @param driver to use for drawing
  201. */
  202. void drawHeader(video::IVideoDriver *driver);
  203. /**
  204. * draw footer layer
  205. * @param driver to use for drawing
  206. */
  207. void drawFooter(video::IVideoDriver *driver);
  208. /**
  209. * load a texture for a specified layer
  210. * @param layer draw layer to specify texture
  211. * @param texturepath full path of texture to load
  212. */
  213. bool setTexture(texture_layer layer, const std::string &texturepath,
  214. bool tile_image, unsigned int minsize);
  215. /**
  216. * download a file using curl
  217. * @param url url to download
  218. * @param target file to store to
  219. */
  220. static bool downloadFile(const std::string &url, const std::string &target);
  221. /** array containing pointers to current specified texture layers */
  222. image_definition m_textures[TEX_LAYER_MAX];
  223. /**
  224. * specify text to appear as top left string
  225. * @param text to set
  226. */
  227. void setTopleftText(const std::string &text);
  228. /** pointer to gui element shown at topleft corner */
  229. irr::gui::IGUIStaticText *m_irr_toplefttext = nullptr;
  230. /** and text that is in it */
  231. EnrichedString m_toplefttext;
  232. /** initialize cloud subsystem */
  233. void cloudInit();
  234. /** do preprocessing for cloud subsystem */
  235. void cloudPreProcess();
  236. /** do postprocessing for cloud subsystem */
  237. void cloudPostProcess(u32 frametime_min, IrrlichtDevice *device);
  238. /** internam data required for drawing clouds */
  239. struct clouddata {
  240. /** delta time since last cloud processing */
  241. f32 dtime;
  242. /** absolute time of last cloud processing */
  243. u32 lasttime;
  244. /** pointer to cloud class */
  245. Clouds *clouds = nullptr;
  246. /** camera required for drawing clouds */
  247. scene::ICameraSceneNode *camera = nullptr;
  248. };
  249. /** is drawing of clouds enabled atm */
  250. bool m_clouds_enabled = true;
  251. /** data used to draw clouds */
  252. clouddata m_cloud;
  253. /** start playing a sound and return handle */
  254. s32 playSound(const SimpleSoundSpec &spec, bool looped);
  255. /** stop playing a sound started with playSound() */
  256. void stopSound(s32 handle);
  257. };