]> git.lizzy.rs Git - dragonfireclient.git/blob - src/touchscreengui.h
Add extra buttons to Android GUI. All icons are licensed by freepik.com under CC...
[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         inventory_id,
42         drop_id,
43         jump_id,
44         crunch_id,
45         fly_id,
46         noclip_id,
47         fast_id,
48         debug_id,
49         chat_id,
50         camera_id,
51         range_id,
52         after_last_element_id
53 } touch_gui_button_id;
54
55 #define MIN_DIG_TIME_MS 500
56 #define MAX_TOUCH_COUNT 64
57
58 extern const char** touchgui_button_imagenames;
59
60 class TouchScreenGUI
61 {
62 public:
63         TouchScreenGUI(IrrlichtDevice *device, IEventReceiver* receiver);
64         ~TouchScreenGUI();
65
66         void translateEvent(const SEvent &event);
67
68         void init(ISimpleTextureSource* tsrc,float density);
69
70         double getYaw() { return m_camera_yaw; }
71         double getPitch() { return m_camera_pitch; }
72         line3d<f32> getShootline() { return m_shootline; }
73
74         void step(float dtime);
75         void resetHud();
76         void registerHudItem(int index, const rect<s32> &rect);
77         void Toggle(bool visible);
78
79         void Hide();
80         void Show();
81
82 private:
83         IrrlichtDevice*         m_device;
84         IGUIEnvironment*        m_guienv;
85         IEventReceiver*         m_receiver;
86         ISimpleTextureSource*   m_texturesource;
87         v2u32                   m_screensize;
88         std::map<int,rect<s32> > m_hud_rects;
89         std::map<int,irr::EKEY_CODE> m_hud_ids;
90         bool                    m_visible; // is the gui visible
91
92         /* value in degree */
93         double                  m_camera_yaw;
94         double                  m_camera_pitch;
95
96         line3d<f32>             m_shootline;
97
98         rect<s32>               m_control_pad_rect;
99
100         int                     m_move_id;
101         bool                    m_move_has_really_moved;
102         s32                     m_move_downtime;
103         bool                    m_move_sent_as_mouse_event;
104         v2s32                   m_move_downlocation;
105
106         struct button_info {
107                 float            repeatcounter;
108                 irr::EKEY_CODE   keycode;
109                 std::vector<int> ids;
110                 IGUIButton*      guibutton;
111                 bool             immediate_release;
112         };
113
114         button_info m_buttons[after_last_element_id];
115
116         /* gui button detection */
117         touch_gui_button_id getButtonID(s32 x, s32 y);
118
119         /* gui button by eventID */
120         touch_gui_button_id getButtonID(int eventID);
121
122         /* check if a button has changed */
123         void handleChangedButton(const SEvent &event);
124
125         /* initialize a button */
126         void initButton(touch_gui_button_id id, rect<s32> button_rect,
127                         std::wstring caption, bool immediate_release );
128
129         /* load texture */
130         void loadButtonTexture(button_info* btn, const char* path);
131
132         struct id_status{
133                 int id;
134                 int X;
135                 int Y;
136         };
137
138         /* vector to store known ids and their initial touch positions*/
139         std::vector<id_status> m_known_ids;
140
141         /* handle a button event */
142         void ButtonEvent(touch_gui_button_id bID, int eventID, bool action);
143
144         /* handle pressed hud buttons */
145         bool isHUDButton(const SEvent &event);
146
147         /* handle released hud buttons */
148         bool isReleaseHUDButton(int eventID);
149
150         /* handle double taps */
151         bool doubleTapDetection();
152
153         /* doubleclick detection variables */
154         struct key_event {
155                 unsigned int down_time;
156                 s32 x;
157                 s32 y;
158         };
159
160         /* array for saving last known position of a pointer */
161         v2s32 m_pointerpos[MAX_TOUCH_COUNT];
162
163         /* array for doubletap detection */
164         key_event m_key_events[2];
165 };
166 extern TouchScreenGUI *g_touchscreengui;
167 #endif