1 //
2 //  SuperTuxKart - a fun racing game with go-kart
3 //  Copyright (C) 2013-2015 SuperTuxKart-Team
4 //
5 //  This program is free software; you can redistribute it and/or
6 //  modify it under the terms of the GNU General Public License
7 //  as published by the Free Software Foundation; either version 3
8 //  of the License: or (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
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 #ifndef HEADER_SCREEN_KEYBOARD_HPP
20 #define HEADER_SCREEN_KEYBOARD_HPP
21 
22 #include <IGUIButton.h>
23 #include <IGUIWindow.h>
24 
25 #include "guiengine/abstract_top_level_container.hpp"
26 #include "guiengine/skin.hpp"
27 #include "input/input_manager.hpp"
28 #include "utils/leak_check.hpp"
29 
30 
31 class CGUIEditBox;
32 
33 /**
34  * \ingroup guiengine
35  */
36 namespace GUIEngine
37 {
38     class ButtonWidget;
39 
40     /**
41      * \brief Class representing a screen keyboard. Only once instance at a
42      * time (if you create a 2nd the first will be destroyed). You can call
43      * static function 'dismiss' to simply close the keyboard (so you don't
44      * need to keep track of instances yourself)
45      * \ingroup guiengine
46      */
47     class ScreenKeyboard : public SkinWidgetContainer,
48                            public AbstractTopLevelContainer
49     {
50     protected:
51         typedef std::vector<std::vector<std::string> > KeyboardLayout;
52         typedef std::vector<std::vector<int> > KeyboardLayoutProportions;
53         enum ButtonsType
54         {
55             BUTTONS_NONE,
56             BUTTONS_LOWER,
57             BUTTONS_UPPER,
58             BUTTONS_DIGITS,
59             BUTTONS_DIGITS2,
60             BUTTONS_EMOJI
61         };
62     private:
63         /** Global instance of the current screen keyboard */
64         static ScreenKeyboard* m_screen_keyboard;
65 
66         /** A value in range of 0.0 to 1.0 that determines width of the screen
67          *  that is used by the keyboard */
68         float m_percent_width;
69 
70         /** A value in range of 0.0 to 1.0 that determines height of the screen
71          *  that is used by the keyboard */
72         float m_percent_height;
73 
74         /** A time for repeat key feature */
75         unsigned int m_repeat_time;
76 
77         /** True if backspace button was pressed */
78         bool m_back_button_pressed;
79 
80         /** True if screen keyboard is going to be closed */
81         bool m_schedule_close;
82 
83         /** The edit box that is assigned to the keyboard */
84         CGUIEditBox* m_edit_box;
85 
86         /** A button that is used as backspace key */
87         irr::gui::IGUIButton* m_back_button;
88 
89         /** Remembers currently selected button type */
90         ButtonsType m_buttons_type;
91 
92         /** Irrlicht window used by the keyboard widget */
93         irr::gui::IGUIWindow* m_irrlicht_window;
94 
95         /** Contains position and dimensions of the keyboard */
96         irr::core::rect<irr::s32> m_area;
97 
98         /** Contans the pointers to all button widgets */
99         std::vector<ButtonWidget*> m_buttons;
100 
101         /** Remembered input mode that was used before keyboard creation */
102         InputManager::InputDriverMode m_previous_mode;
103 
104         void createButtons();
105         void assignButtons(ButtonsType buttons_type);
106         core::stringw getKeyName(std::string key_id);
107 
108     public:
109         LEAK_CHECK()
110 
111         ScreenKeyboard(float percent_width, float percent_height,
112                        CGUIEditBox* edit_box);
113         ~ScreenKeyboard();
114 
115         void init();
116 
117         virtual EventPropagation processEvent(const std::string& eventSource);
118 
119         static void dismiss();
120         static bool onEscapePressed();
121         /** Returns pointer to the created keyboard or NULL if keyboard was
122          *  not created */
getCurrent()123         static ScreenKeyboard* getCurrent() {return m_screen_keyboard;}
124 
125         /** Returns true if keyboard is created */
isActive()126         static bool isActive() {return m_screen_keyboard != NULL;}
127 
128         static bool shouldUseScreenKeyboard();
129         static bool hasSystemScreenKeyboard();
130 
131         /** Override to be notified of updates */
132         virtual void onUpdate(float dt);
133 
134         bool onEvent(const SEvent &event);
135 
136         /** Get irrlicht window used by the keyboard widget */
getIrrlichtElement()137         irr::gui::IGUIWindow* getIrrlichtElement() {return m_irrlicht_window;}
138 
139         /** Checks if the screen keyboard is a parent of the selected item
140          *  \param widget A widget that should be checked
141          *  \return True if keyboard is the parent
142          */
isMyIrrChild(irr::gui::IGUIElement * widget) const143         bool isMyIrrChild(irr::gui::IGUIElement* widget) const
144                                 {return m_irrlicht_window->isMyChild(widget);}
145 
146         /** Returns width of the screen keyboard */
getWidth()147         int getWidth()  {return m_area.getWidth();}
148 
149         /** Returns height of the screen keyboard */
getHeight()150         int getHeight() {return m_area.getHeight();}
151 
152         /** Returns assigned edit box */
getEditBox()153         CGUIEditBox* getEditBox() {return m_edit_box;}
154 
155         virtual KeyboardLayoutProportions getKeyboardLayoutProportions() const;
156 
157         virtual KeyboardLayout* getKeyboardLayout(ButtonsType bt) const;
158 
getDefaultButtonsType() const159         virtual ButtonsType getDefaultButtonsType() const
160         {
161             return BUTTONS_LOWER;
162         }
163     };
164 }
165 
166 #endif
167