touchscreengui.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. Copyright (C) 2014 sapier
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License along
  12. with this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  14. */
  15. #pragma once
  16. #include "irrlichttypes.h"
  17. #include <IEventReceiver.h>
  18. #include <IGUIButton.h>
  19. #include <IGUIEnvironment.h>
  20. #include <IrrlichtDevice.h>
  21. #include <map>
  22. #include <vector>
  23. #include "client/tile.h"
  24. #include "client/game.h"
  25. using namespace irr;
  26. using namespace irr::core;
  27. using namespace irr::gui;
  28. typedef enum
  29. {
  30. jump_id = 0,
  31. crunch_id,
  32. zoom_id,
  33. aux1_id,
  34. after_last_element_id,
  35. settings_starter_id,
  36. rare_controls_starter_id,
  37. fly_id,
  38. noclip_id,
  39. fast_id,
  40. debug_id,
  41. camera_id,
  42. range_id,
  43. minimap_id,
  44. toggle_chat_id,
  45. chat_id,
  46. inventory_id,
  47. drop_id,
  48. forward_id,
  49. backward_id,
  50. left_id,
  51. right_id,
  52. joystick_off_id,
  53. joystick_bg_id,
  54. joystick_center_id
  55. } touch_gui_button_id;
  56. typedef enum
  57. {
  58. j_forward = 0,
  59. j_backward,
  60. j_left,
  61. j_right,
  62. j_aux1
  63. } touch_gui_joystick_move_id;
  64. typedef enum
  65. {
  66. AHBB_Dir_Top_Bottom,
  67. AHBB_Dir_Bottom_Top,
  68. AHBB_Dir_Left_Right,
  69. AHBB_Dir_Right_Left
  70. } autohide_button_bar_dir;
  71. #define MIN_DIG_TIME_MS 500
  72. #define BUTTON_REPEAT_DELAY 0.2f
  73. #define SETTINGS_BAR_Y_OFFSET 5
  74. #define RARE_CONTROLS_BAR_Y_OFFSET 5
  75. // Very slow button repeat frequency
  76. #define SLOW_BUTTON_REPEAT 1.0f
  77. extern const char **button_imagenames;
  78. extern const char **joystick_imagenames;
  79. struct button_info
  80. {
  81. float repeatcounter;
  82. float repeatdelay;
  83. irr::EKEY_CODE keycode;
  84. std::vector<size_t> ids;
  85. IGUIButton *guibutton = nullptr;
  86. bool immediate_release;
  87. // 0: false, 1: (true) first texture, 2: (true) second texture
  88. int togglable = 0;
  89. std::vector<const char *> textures;
  90. };
  91. class AutoHideButtonBar
  92. {
  93. public:
  94. AutoHideButtonBar(IrrlichtDevice *device, IEventReceiver *receiver);
  95. void init(ISimpleTextureSource *tsrc, const char *starter_img, int button_id,
  96. const v2s32 &UpperLeft, const v2s32 &LowerRight,
  97. autohide_button_bar_dir dir, float timeout);
  98. ~AutoHideButtonBar();
  99. // add button to be shown
  100. void addButton(touch_gui_button_id id, const wchar_t *caption,
  101. const char *btn_image);
  102. // add toggle button to be shown
  103. void addToggleButton(touch_gui_button_id id, const wchar_t *caption,
  104. const char *btn_image_1, const char *btn_image_2);
  105. // detect settings bar button events
  106. bool isButton(const SEvent &event);
  107. // step handler
  108. void step(float dtime);
  109. // deactivate button bar
  110. void deactivate();
  111. // hide the whole buttonbar
  112. void hide();
  113. // unhide the buttonbar
  114. void show();
  115. private:
  116. ISimpleTextureSource *m_texturesource = nullptr;
  117. irr::video::IVideoDriver *m_driver;
  118. IGUIEnvironment *m_guienv;
  119. IEventReceiver *m_receiver;
  120. button_info m_starter;
  121. std::vector<button_info *> m_buttons;
  122. v2s32 m_upper_left;
  123. v2s32 m_lower_right;
  124. // show settings bar
  125. bool m_active = false;
  126. bool m_visible = true;
  127. // settings bar timeout
  128. float m_timeout = 0.0f;
  129. float m_timeout_value = 3.0f;
  130. bool m_initialized = false;
  131. autohide_button_bar_dir m_dir = AHBB_Dir_Right_Left;
  132. };
  133. class TouchScreenGUI
  134. {
  135. public:
  136. TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver);
  137. ~TouchScreenGUI();
  138. void translateEvent(const SEvent &event);
  139. void init(ISimpleTextureSource *tsrc);
  140. double getYawChange()
  141. {
  142. double res = m_camera_yaw_change;
  143. m_camera_yaw_change = 0;
  144. return res;
  145. }
  146. double getPitch() { return m_camera_pitch; }
  147. /*
  148. * Returns a line which describes what the player is pointing at.
  149. * The starting point and looking direction are significant,
  150. * the line should be scaled to match its length to the actual distance
  151. * the player can reach.
  152. * The line starts at the camera and ends on the camera's far plane.
  153. * The coordinates do not contain the camera offset.
  154. */
  155. line3d<f32> getShootline() { return m_shootline; }
  156. void step(float dtime);
  157. void resetHud();
  158. void registerHudItem(int index, const rect<s32> &rect);
  159. void Toggle(bool visible);
  160. void hide();
  161. void show();
  162. private:
  163. IrrlichtDevice *m_device;
  164. IGUIEnvironment *m_guienv;
  165. IEventReceiver *m_receiver;
  166. ISimpleTextureSource *m_texturesource;
  167. v2u32 m_screensize;
  168. s32 button_size;
  169. double m_touchscreen_threshold;
  170. std::map<int, rect<s32>> m_hud_rects;
  171. std::map<size_t, irr::EKEY_CODE> m_hud_ids;
  172. bool m_visible; // is the gui visible
  173. // value in degree
  174. double m_camera_yaw_change = 0.0;
  175. double m_camera_pitch = 0.0;
  176. // forward, backward, left, right
  177. touch_gui_button_id m_joystick_names[5] = {
  178. forward_id, backward_id, left_id, right_id, aux1_id};
  179. bool m_joystick_status[5] = {false, false, false, false, false};
  180. /*
  181. * A line starting at the camera and pointing towards the
  182. * selected object.
  183. * The line ends on the camera's far plane.
  184. * The coordinates do not contain the camera offset.
  185. */
  186. line3d<f32> m_shootline;
  187. bool m_has_move_id = false;
  188. size_t m_move_id;
  189. bool m_move_has_really_moved = false;
  190. u64 m_move_downtime = 0;
  191. bool m_move_sent_as_mouse_event = false;
  192. v2s32 m_move_downlocation = v2s32(-10000, -10000);
  193. bool m_has_joystick_id = false;
  194. size_t m_joystick_id;
  195. bool m_joystick_has_really_moved = false;
  196. bool m_fixed_joystick = false;
  197. bool m_joystick_triggers_aux1 = false;
  198. button_info *m_joystick_btn_off = nullptr;
  199. button_info *m_joystick_btn_bg = nullptr;
  200. button_info *m_joystick_btn_center = nullptr;
  201. button_info m_buttons[after_last_element_id];
  202. // gui button detection
  203. touch_gui_button_id getButtonID(s32 x, s32 y);
  204. // gui button by eventID
  205. touch_gui_button_id getButtonID(size_t eventID);
  206. // check if a button has changed
  207. void handleChangedButton(const SEvent &event);
  208. // initialize a button
  209. void initButton(touch_gui_button_id id, const rect<s32> &button_rect,
  210. const std::wstring &caption, bool immediate_release,
  211. float repeat_delay = BUTTON_REPEAT_DELAY);
  212. // initialize a joystick button
  213. button_info *initJoystickButton(touch_gui_button_id id,
  214. const rect<s32> &button_rect, int texture_id,
  215. bool visible = true);
  216. struct id_status
  217. {
  218. size_t id;
  219. int X;
  220. int Y;
  221. };
  222. // vector to store known ids and their initial touch positions
  223. std::vector<id_status> m_known_ids;
  224. // handle a button event
  225. void handleButtonEvent(touch_gui_button_id bID, size_t eventID, bool action);
  226. // handle pressed hud buttons
  227. bool isHUDButton(const SEvent &event);
  228. // handle double taps
  229. bool doubleTapDetection();
  230. // handle release event
  231. void handleReleaseEvent(size_t evt_id);
  232. // apply joystick status
  233. void applyJoystickStatus();
  234. // double-click detection variables
  235. struct key_event
  236. {
  237. u64 down_time;
  238. s32 x;
  239. s32 y;
  240. };
  241. // array for saving last known position of a pointer
  242. std::map<size_t, v2s32> m_pointerpos;
  243. // array for double tap detection
  244. key_event m_key_events[2];
  245. // settings bar
  246. AutoHideButtonBar m_settingsbar;
  247. // rare controls bar
  248. AutoHideButtonBar m_rarecontrolsbar;
  249. };
  250. extern TouchScreenGUI *g_touchscreengui;