utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interf...
[oweals/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 class GUIModalMenu;
26
27 class IMenuManager
28 {
29 public:
30         // A GUIModalMenu calls these when this class is passed as a parameter
31         virtual void createdMenu(GUIModalMenu *menu) = 0;
32         virtual void deletingMenu(GUIModalMenu *menu) = 0;
33 };
34
35 /*
36         Remember to drop() the menu after creating, so that it can
37         remove itself when it wants to.
38 */
39
40 class GUIModalMenu : public gui::IGUIElement
41 {
42 public:
43         GUIModalMenu(gui::IGUIEnvironment* env,
44                         gui::IGUIElement* parent, s32 id,
45                         IMenuManager *menumgr):
46                 IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
47                                 core::rect<s32>(0,0,100,100))
48         {
49                 //m_force_regenerate_gui = false;
50                 
51                 m_menumgr = menumgr;
52                 m_allow_focus_removal = false;
53                 m_screensize_old = v2u32(0,0);
54
55                 setVisible(true);
56                 Environment->setFocus(this);
57                 m_menumgr->createdMenu(this);
58         }
59         virtual ~GUIModalMenu()
60         {
61                 m_menumgr->deletingMenu(this);
62         }
63
64         void allowFocusRemoval(bool allow)
65         {
66                 m_allow_focus_removal = allow;
67         }
68
69         bool canTakeFocus(gui::IGUIElement *e)
70         {
71                 return (e && (e == this || isMyChild(e))) || m_allow_focus_removal;
72         }
73
74         void draw()
75         {
76                 if(!IsVisible)
77                         return;
78                         
79                 video::IVideoDriver* driver = Environment->getVideoDriver();
80                 v2u32 screensize = driver->getScreenSize();
81                 if(screensize != m_screensize_old /*|| m_force_regenerate_gui*/)
82                 {
83                         m_screensize_old = screensize;
84                         regenerateGui(screensize);
85                         //m_force_regenerate_gui = false;
86                 }
87
88                 drawMenu();
89         }
90         
91         /*
92                 This should be called when the menu wants to quit.
93
94                 WARNING: THIS DEALLOCATES THE MENU FROM MEMORY. Return
95                 immediately if you call this from the menu itself.
96         */
97         void quitMenu()
98         {
99                 allowFocusRemoval(true);
100                 // This removes Environment's grab on us
101                 Environment->removeFocus(this);
102                 this->remove();
103         }
104
105         void removeChildren()
106         {
107                 const core::list<gui::IGUIElement*> &children = getChildren();
108                 core::list<gui::IGUIElement*> children_copy;
109                 for(core::list<gui::IGUIElement*>::ConstIterator
110                                 i = children.begin(); i != children.end(); i++)
111                 {
112                         children_copy.push_back(*i);
113                 }
114                 for(core::list<gui::IGUIElement*>::Iterator
115                                 i = children_copy.begin();
116                                 i != children_copy.end(); i++)
117                 {
118                         (*i)->remove();
119                 }
120         }
121
122         virtual void regenerateGui(v2u32 screensize) = 0;
123         virtual void drawMenu() = 0;
124         virtual bool OnEvent(const SEvent& event) { return false; };
125
126 protected:
127         //bool m_force_regenerate_gui;
128 private:
129         IMenuManager *m_menumgr;
130         // This might be necessary to expose to the implementation if it
131         // wants to launch other menus
132         bool m_allow_focus_removal;
133         v2u32 m_screensize_old;
134 };
135
136
137 #endif
138