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