1 //  SuperTuxKart - a fun racing game with go-kart
2 //  Copyright (C) 2009-2015 Marianne Gagnon
3 //
4 //  This program is free software; you can redistribute it and/or
5 //  modify it under the terms of the GNU General Public License
6 //  as published by the Free Software Foundation; either version 3
7 //  of the License, or (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; if not, write to the Free Software
16 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 
18 #ifndef HEADER_MODAL_DIALOG_HPP
19 #define HEADER_MODAL_DIALOG_HPP
20 
21 #include <IGUIWindow.h>
22 
23 #include "utils/ptr_vector.hpp"
24 #include "guiengine/abstract_top_level_container.hpp"
25 //#include "guiengine/event_handler.hpp"
26 //#include "guiengine/layout_manager.hpp"
27 #include "guiengine/skin.hpp"
28 #include "input/input_manager.hpp"
29 #include "utils/leak_check.hpp"
30 
31 //class PlayerProfile;
32 
33 /**
34  * \ingroup guiengine
35  */
36 namespace GUIEngine
37 {
38     class Widget;
39     class TextBoxWidget;
40     class ButtonWidget;
41 
42     enum ModalDialogLocation
43     {
44         MODAL_DIALOG_LOCATION_CENTER = 0,
45         MODAL_DIALOG_LOCATION_BOTTOM = 1
46     };
47 
48     /**
49      * \brief Abstract base class representing a modal dialog.
50      * Only once instance at a time (if you create a 2nd the first will be destroyed).
51      * You can call static function 'dismiss' to simply close current dialog (so you don't
52      * need to keep track of instances yourself)
53      * \ingroup guiengine
54      */
55     class ModalDialog : public SkinWidgetContainer, public AbstractTopLevelContainer
56     {
57     private:
58 
59         ModalDialogLocation m_dialog_location;
60 
61         float m_percent_width, m_percent_height;
62         bool m_init;
63         bool m_was_resizable;
64     protected:
65         irr::gui::IGUIWindow* m_irrlicht_window;
66         irr::core::rect< irr::s32 > m_area;
67         bool m_fade_background;
68 
69         InputManager::InputDriverMode m_previous_mode;
70 
71         /**
72          * \brief Creates a modal dialog with given percentage of screen width and height
73          */
74         ModalDialog(const float percentWidth, const float percentHeight,
75                     ModalDialogLocation location = MODAL_DIALOG_LOCATION_CENTER);
76 
77         /** \brief Load a XML file to create the dialog from
78           * \note  This method automatically calls Widget::add() on each widget
79           */
80         void loadFromFile(const char* xmlFile);
81 
82         virtual void onEnterPressedInternal();
83         void clearWindow();
84 
85         /** \brief Callback invoked when the dialog was loaded from the XML file (if the constructor
86           *        that takes a XML file as argument is used)
87           */
loadedFromFile()88         virtual void loadedFromFile() {}
89         void doInit();
90 
91     public:
LEAK_CHECK()92         LEAK_CHECK()
93 
94         /** Because C++ doesn't support constructor delegation... */
95 
96         bool isInited() {return m_init;}
97 
98         virtual ~ModalDialog();
99 
100         /** Returns whether to block event propagation (usually, you will want to block events you processed) */
processEvent(const std::string & eventSource)101         virtual EventPropagation processEvent(const std::string& eventSource){ return EVENT_LET; }
102 
getIrrlichtElement()103         irr::gui::IGUIWindow* getIrrlichtElement()
104         {
105             return m_irrlicht_window;
106         }
107 
108         static void dismiss();
109         static void onEnterPressed();
110         static ModalDialog* getCurrent();
111         static bool isADialogActive();
112 
113         /** Override to change what happens on escape pressed */
onEscapePressed()114         virtual bool onEscapePressed() { return true; }
115 
116         /** Override to be notified of updates */
onUpdate(float dt)117         virtual void onUpdate(float dt) { }
118 
119         /**
120          * \brief Optional callback invoked very early, before widgets have been added (contrast with
121          *        init(), which is invoked afer widgets were added)
122          */
beforeAddingWidgets()123         virtual void beforeAddingWidgets() {}
load()124         virtual void load() {}
125 
126         /** \brief Optional callback invoked after widgets have been add()ed */
init()127         virtual void init() {}
128 
129         /**
130           * \brief Implementing callback from AbstractTopLevelContainer
131           */
getWidth()132         virtual int getWidth()  { return m_area.getWidth(); }
133 
134         /**
135           * \brief Implementing callback from AbstractTopLevelContainer
136           */
getHeight()137         virtual int getHeight() { return m_area.getHeight(); }
138 
fadeBackground() const139         bool fadeBackground() const { return m_fade_background; }
140 
isMyIrrChild(irr::gui::IGUIElement * widget) const141         bool isMyIrrChild(irr::gui::IGUIElement* widget) const { return m_irrlicht_window->isMyChild(widget); }
142     };
143 
144 }
145 #endif
146