guiEngine.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "modalMenu.h"
  22. #include "guiFormSpecMenu.h"
  23. #include "sound.h"
  24. #include "client/tile.h"
  25. #include "util/enriched_string.h"
  26. /******************************************************************************/
  27. /* Typedefs and macros */
  28. /******************************************************************************/
  29. /** texture layer ids */
  30. typedef enum {
  31. TEX_LAYER_BACKGROUND = 0,
  32. TEX_LAYER_OVERLAY,
  33. TEX_LAYER_HEADER,
  34. TEX_LAYER_FOOTER,
  35. TEX_LAYER_MAX
  36. } texture_layer;
  37. typedef struct {
  38. video::ITexture *texture = nullptr;
  39. bool tile;
  40. unsigned int minsize;
  41. } image_definition;
  42. /******************************************************************************/
  43. /* forward declarations */
  44. /******************************************************************************/
  45. class GUIEngine;
  46. class MainMenuScripting;
  47. class Clouds;
  48. struct MainMenuData;
  49. /******************************************************************************/
  50. /* declarations */
  51. /******************************************************************************/
  52. /** GUIEngine specific implementation of TextDest used within guiFormSpecMenu */
  53. class TextDestGuiEngine : public TextDest
  54. {
  55. public:
  56. /**
  57. * default constructor
  58. * @param engine the engine data is transmitted for further processing
  59. */
  60. TextDestGuiEngine(GUIEngine* engine) : m_engine(engine) {};
  61. /**
  62. * receive fields transmitted by guiFormSpecMenu
  63. * @param fields map containing formspec field elements currently active
  64. */
  65. void gotText(const StringMap &fields);
  66. /**
  67. * receive text/events transmitted by guiFormSpecMenu
  68. * @param text textual representation of event
  69. */
  70. void gotText(const std::wstring &text);
  71. private:
  72. /** target to transmit data to */
  73. GUIEngine *m_engine = nullptr;
  74. };
  75. /** GUIEngine specific implementation of ISimpleTextureSource */
  76. class MenuTextureSource : public ISimpleTextureSource
  77. {
  78. public:
  79. /**
  80. * default constructor
  81. * @param driver the video driver to load textures from
  82. */
  83. MenuTextureSource(video::IVideoDriver *driver) : m_driver(driver) {};
  84. /**
  85. * destructor, removes all loaded textures
  86. */
  87. virtual ~MenuTextureSource();
  88. /**
  89. * get a texture, loading it if required
  90. * @param name path to the texture
  91. * @param id receives the texture ID, always 0 in this implementation
  92. */
  93. video::ITexture *getTexture(const std::string &name, u32 *id = NULL);
  94. private:
  95. /** driver to get textures from */
  96. video::IVideoDriver *m_driver = nullptr;
  97. /** set of texture names to delete */
  98. std::set<std::string> m_to_delete;
  99. };
  100. /** GUIEngine specific implementation of OnDemandSoundFetcher */
  101. class MenuMusicFetcher: public OnDemandSoundFetcher
  102. {
  103. public:
  104. /**
  105. * get sound file paths according to sound name
  106. * @param name sound name
  107. * @param dst_paths receives possible paths to sound files
  108. * @param dst_datas receives binary sound data (not used here)
  109. */
  110. void fetchSounds(const std::string &name,
  111. std::set<std::string> &dst_paths,
  112. std::set<std::string> &dst_datas);
  113. private:
  114. /** set of fetched sound names */
  115. std::set<std::string> m_fetched;
  116. };
  117. /** implementation of main menu based uppon formspecs */
  118. class GUIEngine {
  119. /** grant ModApiMainMenu access to private members */
  120. friend class ModApiMainMenu;
  121. friend class ModApiSound;
  122. public:
  123. /**
  124. * default constructor
  125. * @param dev device to draw at
  126. * @param parent parent gui element
  127. * @param menumgr manager to add menus to
  128. * @param smgr scene manager to add scene elements to
  129. * @param data struct to transfer data to main game handling
  130. */
  131. GUIEngine(JoystickController *joystick,
  132. gui::IGUIElement *parent,
  133. IMenuManager *menumgr,
  134. MainMenuData *data,
  135. bool &kill);
  136. /** default destructor */
  137. virtual ~GUIEngine();
  138. /**
  139. * return MainMenuScripting interface
  140. */
  141. MainMenuScripting *getScriptIface()
  142. {
  143. return m_script;
  144. }
  145. /**
  146. * return dir of current menuscript
  147. */
  148. std::string getScriptDir()
  149. {
  150. return m_scriptdir;
  151. }
  152. /** pass async callback to scriptengine **/
  153. unsigned int queueAsync(const std::string &serialized_fct,
  154. const std::string &serialized_params);
  155. private:
  156. /** find and run the main menu script */
  157. bool loadMainMenuScript();
  158. /** run main menu loop */
  159. void run();
  160. /** update size of topleftext element */
  161. void updateTopLeftTextSize();
  162. /** parent gui element */
  163. gui::IGUIElement *m_parent = nullptr;
  164. /** manager to add menus to */
  165. IMenuManager *m_menumanager = nullptr;
  166. /** scene manager to add scene elements to */
  167. scene::ISceneManager *m_smgr = nullptr;
  168. /** pointer to data beeing transfered back to main game handling */
  169. MainMenuData *m_data = nullptr;
  170. /** pointer to texture source */
  171. ISimpleTextureSource *m_texture_source = nullptr;
  172. /** pointer to soundmanager*/
  173. ISoundManager *m_sound_manager = nullptr;
  174. /** representation of form source to be used in mainmenu formspec */
  175. FormspecFormSource *m_formspecgui = nullptr;
  176. /** formspec input receiver */
  177. TextDestGuiEngine *m_buttonhandler = nullptr;
  178. /** the formspec menu */
  179. GUIFormSpecMenu *m_menu = nullptr;
  180. /** reference to kill variable managed by SIGINT handler */
  181. bool &m_kill;
  182. /** variable used to abort menu and return back to main game handling */
  183. bool m_startgame = false;
  184. /** scripting interface */
  185. MainMenuScripting *m_script = nullptr;
  186. /** script basefolder */
  187. std::string m_scriptdir = "";
  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, 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();
  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(SimpleSoundSpec spec, bool looped);
  255. /** stop playing a sound started with playSound() */
  256. void stopSound(s32 handle);
  257. };