]> git.lizzy.rs Git - dragonfireclient.git/blob - src/touchscreengui.h
Implement proper font handling
[dragonfireclient.git] / src / touchscreengui.h
1 /*
2 Copyright (C) 2014 sapier
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (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 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #ifndef TOUCHSCREENGUI_HEADER
20 #define TOUCHSCREENGUI_HEADER
21
22 #include <IGUIEnvironment.h>
23 #include <IGUIButton.h>
24 #include <IEventReceiver.h>
25
26 #include <vector>
27 #include <map>
28
29 #include "game.h"
30 #include "tile.h"
31
32 using namespace irr;
33 using namespace irr::core;
34 using namespace irr::gui;
35
36 typedef enum {
37         forward_id = 0,
38         backward_id,
39         left_id,
40         right_id,
41         jump_id,
42         crunch_id,
43         inventory_id,
44         chat_id,
45         after_last_element_id
46 } touch_gui_button_id;
47
48 #define MIN_DIG_TIME_MS 500
49 #define MAX_TOUCH_COUNT 64
50
51 extern const char** touchgui_button_imagenames;
52
53 class TouchScreenGUI
54 {
55 public:
56         TouchScreenGUI(IrrlichtDevice *device, IEventReceiver* receiver);
57         ~TouchScreenGUI();
58
59         void translateEvent(const SEvent &event);
60
61         void init(ISimpleTextureSource* tsrc,float density);
62
63         double getYaw() { return m_camera_yaw; }
64         double getPitch() { return m_camera_pitch; }
65         line3d<f32> getShootline() { return m_shootline; }
66
67         void step(float dtime);
68         void resetHud();
69         void registerHudItem(int index, const rect<s32> &rect);
70         void Toggle(bool visible);
71
72         void Hide();
73         void Show();
74
75 private:
76         IrrlichtDevice*         m_device;
77         IGUIEnvironment*        m_guienv;
78         IEventReceiver*         m_receiver;
79         ISimpleTextureSource*   m_texturesource;
80         v2u32                   m_screensize;
81         std::map<int,rect<s32> > m_hud_rects;
82         std::map<int,irr::EKEY_CODE> m_hud_ids;
83         bool                    m_visible; // is the gui visible
84
85         /* value in degree */
86         double                  m_camera_yaw;
87         double                  m_camera_pitch;
88
89         line3d<f32>             m_shootline;
90
91         rect<s32>               m_control_pad_rect;
92
93         int                     m_move_id;
94         bool                    m_move_has_really_moved;
95         s32                     m_move_downtime;
96         bool                    m_move_sent_as_mouse_event;
97         v2s32                   m_move_downlocation;
98
99         struct button_info {
100                 float            repeatcounter;
101                 irr::EKEY_CODE   keycode;
102                 std::vector<int> ids;
103                 IGUIButton*      guibutton;
104                 bool             immediate_release;
105         };
106
107         button_info m_buttons[after_last_element_id];
108
109         /* gui button detection */
110         touch_gui_button_id getButtonID(s32 x, s32 y);
111
112         /* gui button by eventID */
113         touch_gui_button_id getButtonID(int eventID);
114
115         /* check if a button has changed */
116         void handleChangedButton(const SEvent &event);
117
118         /* initialize a button */
119         void initButton(touch_gui_button_id id, rect<s32> button_rect,
120                         std::wstring caption, bool immediate_release );
121
122         /* load texture */
123         void loadButtonTexture(button_info* btn, const char* path);
124
125         struct id_status{
126                 int id;
127                 int X;
128                 int Y;
129         };
130
131         /* vector to store known ids and their initial touch positions*/
132         std::vector<id_status> m_known_ids;
133
134         /* handle a button event */
135         void ButtonEvent(touch_gui_button_id bID, int eventID, bool action);
136
137         /* handle pressed hud buttons */
138         bool isHUDButton(const SEvent &event);
139
140         /* handle released hud buttons */
141         bool isReleaseHUDButton(int eventID);
142
143         /* handle double taps */
144         bool doubleTapDetection();
145
146         /* doubleclick detection variables */
147         struct key_event {
148                 unsigned int down_time;
149                 s32 x;
150                 s32 y;
151         };
152
153         /* array for saving last known position of a pointer */
154         v2s32 m_pointerpos[MAX_TOUCH_COUNT];
155
156         /* array for doubletap detection */
157         key_event m_key_events[2];
158 };
159 extern TouchScreenGUI *g_touchscreengui;
160 #endif