LCOV - code coverage report
Current view: top level - src - modalMenu.h (source / functions) Hit Total Coverage
Test: report Lines: 34 37 91.9 %
Date: 2015-07-11 18:23:49 Functions: 8 10 80.0 %

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

Generated by: LCOV version 1.11