Play sounds/main_menu.ogg in menu
[oweals/minetest.git] / src / guiEngine.h
1 /*
2 Minetest
3 Copyright (C) 2013 sapier
4
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.
9
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.
14
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.
18 */
19
20 #ifndef GUI_ENGINE_H_
21 #define GUI_ENGINE_H_
22
23 /******************************************************************************/
24 /* Includes                                                                   */
25 /******************************************************************************/
26 #include "irrlichttypes.h"
27 #include "modalMenu.h"
28 #include "clouds.h"
29 #include "guiLuaApi.h"
30 #include "guiFormSpecMenu.h"
31 #include "sound.h"
32
33 /******************************************************************************/
34 /* Typedefs and macros                                                        */
35 /******************************************************************************/
36 #define MAX_MENUBAR_BTN_COUNT   10
37 #define MAX_MENUBAR_BTN_ID              256
38 #define MIN_MENUBAR_BTN_ID              (MAX_MENUBAR_BTN_ID - MAX_MENUBAR_BTN_COUNT)
39
40 /** texture layer ids */
41 typedef enum {
42         TEX_LAYER_BACKGROUND = 0,
43         TEX_LAYER_OVERLAY,
44         TEX_LAYER_HEADER,
45         TEX_LAYER_FOOTER,
46         TEX_LAYER_MAX
47 } texture_layer;
48
49 /******************************************************************************/
50 /* forward declarations                                                       */
51 /******************************************************************************/
52 class GUIEngine;
53 struct MainMenuData;
54 struct SimpleSoundSpec;
55
56 /******************************************************************************/
57 /* declarations                                                               */
58 /******************************************************************************/
59
60 /** GUIEngine specific implementation of TextDest used within guiFormSpecMenu */
61 class TextDestGuiEngine : public TextDest
62 {
63 public:
64         /**
65          * default constructor
66          * @param engine the engine data is transmitted for further processing
67          */
68         TextDestGuiEngine(GUIEngine* engine);
69         /**
70          * receive fields transmitted by guiFormSpecMenu
71          * @param fields map containing formspec field elements currently active
72          */
73         void gotText(std::map<std::string, std::string> fields);
74
75         /**
76          * receive text/events transmitted by guiFormSpecMenu
77          * @param text textual representation of event
78          */
79         void gotText(std::wstring text);
80 private:
81         /** target to transmit data to */
82         GUIEngine* m_engine;
83 };
84
85 class MenuMusicFetcher: public OnDemandSoundFetcher
86 {
87         std::set<std::string> m_fetched;
88 public:
89
90         void fetchSounds(const std::string &name,
91                         std::set<std::string> &dst_paths,
92                         std::set<std::string> &dst_datas)
93         {
94                 if(m_fetched.count(name))
95                         return;
96                 m_fetched.insert(name);
97                 std::string base;
98                 base = porting::path_share + DIR_DELIM + "sounds";
99                 dst_paths.insert(base + DIR_DELIM + name + ".ogg");
100                 int i;
101                 for(i=0; i<10; i++)
102                         dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
103                 base = porting::path_user + DIR_DELIM + "sounds";
104                 dst_paths.insert(base + DIR_DELIM + name + ".ogg");
105                 for(i=0; i<10; i++)
106                         dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
107                 }
108 };
109
110 /** implementation of main menu based uppon formspecs */
111 class GUIEngine {
112 public:
113         /** TextDestGuiEngine needs to transfer data to engine */
114         friend class TextDestGuiEngine;
115         /** guiLuaApi is used to initialize contained stack */
116         friend class guiLuaApi;
117
118         /**
119          * default constructor
120          * @param dev device to draw at
121          * @param parent parent gui element
122          * @param menumgr manager to add menus to
123          * @param smgr scene manager to add scene elements to
124          * @param data struct to transfer data to main game handling
125          */
126         GUIEngine(      irr::IrrlichtDevice* dev,
127                                 gui::IGUIElement* parent,
128                                 IMenuManager *menumgr,
129                                 scene::ISceneManager* smgr,
130                                 MainMenuData* data);
131
132         /** default destructor */
133         virtual ~GUIEngine();
134
135         s32 playSound(SimpleSoundSpec spec, bool looped);
136         void stopSound(s32 handle);
137
138 protected:
139         /**
140          * process field data recieved from formspec
141          * @param fields data in field format
142          */
143         void handleButtons(std::map<std::string, std::string> fields);
144         /**
145          * process events received from formspec
146          * @param text events in textual form
147          */
148         void handleEvent(std::string text);
149
150         /**
151          * return dir of current menuscript
152          */
153         std::string getScriptDir() {
154                 return m_scriptdir;
155         }
156
157 private:
158
159         /* run main menu loop */
160         void run();
161
162         /** handler to limit frame rate within main menu */
163         void limitFrameRate();
164
165         /** device to draw at */
166         irr::IrrlichtDevice*    m_device;
167         /** parent gui element */
168         gui::IGUIElement*               m_parent;
169         /** manager to add menus to */
170         IMenuManager*                   m_menumanager;
171         /** scene manager to add scene elements to */
172         scene::ISceneManager*   m_smgr;
173         /** pointer to data beeing transfered back to main game handling */
174         MainMenuData*                   m_data;
175         /** pointer to soundmanager*/
176         ISoundManager*                  m_sound_manager;
177
178         /** representation of form source to be used in mainmenu formspec */
179         FormspecFormSource*             m_formspecgui;
180         /** formspec input receiver */
181         TextDestGuiEngine*              m_buttonhandler;
182         /** the formspec menu */
183         GUIFormSpecMenu*                m_menu;
184
185         /** variable used to abort menu and return back to main game handling */
186         bool                                    m_startgame;
187
188         /**
189          * initialize lua stack
190          * @param L stack to initialize
191          */
192         void initalize_api(lua_State * L);
193
194         /**
195          * run a lua script
196          * @param script full path to script to run
197          */
198         bool runScript(std::string script);
199
200         /**
201          * script error handler to process errors within lua
202          */
203         void scriptError(const char *fmt, ...);
204
205         /** lua stack */
206         lua_State*                              m_engineluastack;
207         /** lua internal stack number of error handler*/
208         int                                             m_luaerrorhandler;
209
210         /** script basefolder */
211         std::string                             m_scriptdir;
212
213         /**
214          * draw background layer
215          * @param driver to use for drawing
216          */
217         void drawBackground(video::IVideoDriver* driver);
218         /**
219          * draw overlay layer
220          * @param driver to use for drawing
221          */
222         void drawOverlay(video::IVideoDriver* driver);
223         /**
224          * draw header layer
225          * @param driver to use for drawing
226          */
227         void drawHeader(video::IVideoDriver* driver);
228         /**
229          * draw footer layer
230          * @param driver to use for drawing
231          */
232         void drawFooter(video::IVideoDriver* driver);
233
234         /**
235          * load a texture for a specified layer
236          * @param layer draw layer to specify texture
237          * @param texturepath full path of texture to load
238          */
239         bool setTexture(texture_layer layer,std::string texturepath);
240
241         /**
242          * download a file using curl
243          * @param url url to download
244          * @param target file to store to
245          */
246         bool downloadFile(std::string url,std::string target);
247
248         /** array containing pointers to current specified texture layers */
249         video::ITexture*                m_textures[TEX_LAYER_MAX];
250
251         /** draw version string in topleft corner */
252         void drawVersion();
253
254         /**
255          * specify text to be appended to version string
256          * @param text to set
257          */
258         void setTopleftText(std::string append);
259
260         /** pointer to gui element shown at topleft corner */
261         irr::gui::IGUIStaticText*       m_irr_toplefttext;
262
263         /** initialize cloud subsystem */
264         void cloudInit();
265         /** do preprocessing for cloud subsystem */
266         void cloudPreProcess();
267         /** do postprocessing for cloud subsystem */
268         void cloudPostProcess();
269
270         /** internam data required for drawing clouds */
271         struct clouddata {
272                 /** delta time since last cloud processing */
273                 f32             dtime;
274                 /** absolute time of last cloud processing */
275                 u32             lasttime;
276                 /** pointer to cloud class */
277                 Clouds* clouds;
278                 /** camera required for drawing clouds */
279                 scene::ICameraSceneNode* camera;
280         };
281
282         /** is drawing of clouds enabled atm */
283         bool                                    m_clouds_enabled;
284         /** data used to draw clouds */
285         clouddata                               m_cloud;
286
287 };
288
289
290
291 #endif /* GUI_ENGINE_H_ */