]> git.lizzy.rs Git - minetest.git/blob - src/modalMenu.h
minecraft-like crafting
[minetest.git] / src / modalMenu.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 MODALMENU_HEADER
21 #define MODALMENU_HEADER
22
23 #include "common_irrlicht.h"
24
25 /*
26         Remember to drop() the menu after creating, so that it can
27         remove itself when it wants to.
28 */
29
30 class GUIModalMenu : public gui::IGUIElement
31 {
32 public:
33         GUIModalMenu(gui::IGUIEnvironment* env,
34                         gui::IGUIElement* parent, s32 id,
35                         int *active_menu_count):
36                 IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
37                                 core::rect<s32>(0,0,100,100))
38         {
39                 m_active_menu_count = active_menu_count;
40                 m_allow_focus_removal = false;
41                 m_screensize_old = v2u32(0,0);
42
43                 setVisible(true);
44                 Environment->setFocus(this);
45                 (*m_active_menu_count)++;
46         }
47         virtual ~GUIModalMenu()
48         {
49                 (*m_active_menu_count)--;
50         }
51
52         bool canTakeFocus(gui::IGUIElement *e)
53         {
54                 return (e && (e == this || isMyChild(e))) || m_allow_focus_removal;
55         }
56
57         void draw()
58         {
59                 if(!IsVisible)
60                         return;
61                         
62                 video::IVideoDriver* driver = Environment->getVideoDriver();
63                 v2u32 screensize = driver->getScreenSize();
64                 if(screensize != m_screensize_old)
65                 {
66                         m_screensize_old = screensize;
67                         regenerateGui(screensize);
68                 }
69
70                 drawMenu();
71         }
72         
73         /*
74                 This should be called when the menu wants to quit
75         */
76         void quitMenu()
77         {
78                 m_allow_focus_removal = true;
79                 // This removes Environment's grab on us
80                 Environment->removeFocus(this);
81                 this->remove();
82         }
83
84         virtual void regenerateGui(v2u32 screensize) = 0;
85         virtual void drawMenu() = 0;
86         virtual bool OnEvent(const SEvent& event) { return false; };
87         
88 private:
89         int *m_active_menu_count;
90         // This might be necessary to expose to the implementation if it
91         // wants to launch other menus
92         bool m_allow_focus_removal;
93         v2u32 m_screensize_old;
94 };
95
96
97 #endif
98