2
0

touchscreengui.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 <IEventReceiver.h>
  17. #include <IGUIButton.h>
  18. #include <IGUIEnvironment.h>
  19. #include <map>
  20. #include <vector>
  21. #include "client/tile.h"
  22. #include "game.h"
  23. using namespace irr;
  24. using namespace irr::core;
  25. using namespace irr::gui;
  26. typedef enum {
  27. forward_id = 0,
  28. backward_id,
  29. left_id,
  30. right_id,
  31. jump_id,
  32. crunch_id,
  33. after_last_element_id,
  34. settings_starter_id,
  35. rare_controls_starter_id,
  36. fly_id,
  37. noclip_id,
  38. fast_id,
  39. debug_id,
  40. camera_id,
  41. range_id,
  42. chat_id,
  43. inventory_id,
  44. drop_id
  45. } touch_gui_button_id;
  46. typedef enum {
  47. AHBB_Dir_Top_Bottom,
  48. AHBB_Dir_Bottom_Top,
  49. AHBB_Dir_Left_Right,
  50. AHBB_Dir_Right_Left
  51. } autohide_button_bar_dir;
  52. #define MIN_DIG_TIME_MS 500
  53. #define MAX_TOUCH_COUNT 64
  54. #define BUTTON_REPEAT_DELAY 0.2f
  55. #define SETTINGS_BAR_Y_OFFSET 6.5
  56. #define RARE_CONTROLS_BAR_Y_OFFSET 4
  57. extern const char **touchgui_button_imagenames;
  58. struct button_info
  59. {
  60. float repeatcounter;
  61. float repeatdelay;
  62. irr::EKEY_CODE keycode;
  63. std::vector<int> ids;
  64. IGUIButton *guibutton = nullptr;
  65. bool immediate_release;
  66. };
  67. class AutoHideButtonBar
  68. {
  69. public:
  70. AutoHideButtonBar(IrrlichtDevice *device, IEventReceiver *receiver);
  71. void init(ISimpleTextureSource *tsrc, const char *starter_img, int button_id,
  72. v2s32 UpperLeft, v2s32 LowerRight, autohide_button_bar_dir dir,
  73. float timeout);
  74. ~AutoHideButtonBar();
  75. /* add button to be shown */
  76. void addButton(touch_gui_button_id id, const wchar_t *caption,
  77. const char *btn_image);
  78. /* detect settings bar button events */
  79. bool isButton(const SEvent &event);
  80. /* handle released hud buttons */
  81. bool isReleaseButton(int eventID);
  82. /* step handler */
  83. void step(float dtime);
  84. /* deactivate button bar */
  85. void deactivate();
  86. /* hide the whole buttonbar */
  87. void hide();
  88. /* unhide the buttonbar */
  89. void show();
  90. private:
  91. ISimpleTextureSource *m_texturesource = nullptr;
  92. irr::video::IVideoDriver *m_driver;
  93. IGUIEnvironment *m_guienv;
  94. IEventReceiver *m_receiver;
  95. button_info m_starter;
  96. std::vector<button_info *> m_buttons;
  97. v2s32 m_upper_left;
  98. v2s32 m_lower_right;
  99. /* show settings bar */
  100. bool m_active = false;
  101. bool m_visible = true;
  102. /* settings bar timeout */
  103. float m_timeout = 0.0f;
  104. float m_timeout_value = 3.0f;
  105. bool m_initialized = false;
  106. autohide_button_bar_dir m_dir = AHBB_Dir_Right_Left;
  107. };
  108. class TouchScreenGUI
  109. {
  110. public:
  111. TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver);
  112. ~TouchScreenGUI();
  113. void translateEvent(const SEvent &event);
  114. void init(ISimpleTextureSource *tsrc);
  115. double getYawChange()
  116. {
  117. double res = m_camera_yaw_change;
  118. m_camera_yaw_change = 0;
  119. return res;
  120. }
  121. double getPitch() { return m_camera_pitch; }
  122. /*!
  123. * Returns a line which describes what the player is pointing at.
  124. * The starting point and looking direction are significant,
  125. * the line should be scaled to match its length to the actual distance
  126. * the player can reach.
  127. * The line starts at the camera and ends on the camera's far plane.
  128. * The coordinates do not contain the camera offset.
  129. */
  130. line3d<f32> getShootline() { return m_shootline; }
  131. void step(float dtime);
  132. void resetHud();
  133. void registerHudItem(int index, const rect<s32> &rect);
  134. void Toggle(bool visible);
  135. void hide();
  136. void show();
  137. private:
  138. IrrlichtDevice *m_device;
  139. IGUIEnvironment *m_guienv;
  140. IEventReceiver *m_receiver;
  141. ISimpleTextureSource *m_texturesource;
  142. v2u32 m_screensize;
  143. std::map<int, rect<s32>> m_hud_rects;
  144. std::map<int, irr::EKEY_CODE> m_hud_ids;
  145. bool m_visible; // is the gui visible
  146. /* value in degree */
  147. double m_camera_yaw_change = 0.0;
  148. double m_camera_pitch = 0.0;
  149. /*!
  150. * A line starting at the camera and pointing towards the
  151. * selected object.
  152. * The line ends on the camera's far plane.
  153. * The coordinates do not contain the camera offset.
  154. */
  155. line3d<f32> m_shootline;
  156. int m_move_id = -1;
  157. bool m_move_has_really_moved = false;
  158. s64 m_move_downtime = 0;
  159. bool m_move_sent_as_mouse_event = false;
  160. v2s32 m_move_downlocation = v2s32(-10000, -10000);
  161. button_info m_buttons[after_last_element_id];
  162. /* gui button detection */
  163. touch_gui_button_id getButtonID(s32 x, s32 y);
  164. /* gui button by eventID */
  165. touch_gui_button_id getButtonID(int eventID);
  166. /* check if a button has changed */
  167. void handleChangedButton(const SEvent &event);
  168. /* initialize a button */
  169. void initButton(touch_gui_button_id id, rect<s32> button_rect,
  170. std::wstring caption, bool immediate_release,
  171. float repeat_delay = BUTTON_REPEAT_DELAY);
  172. struct id_status
  173. {
  174. int id;
  175. int X;
  176. int Y;
  177. };
  178. /* vector to store known ids and their initial touch positions*/
  179. std::vector<id_status> m_known_ids;
  180. /* handle a button event */
  181. void handleButtonEvent(touch_gui_button_id bID, int eventID, bool action);
  182. /* handle pressed hud buttons */
  183. bool isHUDButton(const SEvent &event);
  184. /* handle released hud buttons */
  185. bool isReleaseHUDButton(int eventID);
  186. /* handle double taps */
  187. bool doubleTapDetection();
  188. /* handle release event */
  189. void handleReleaseEvent(int evt_id);
  190. /* get size of regular gui control button */
  191. int getGuiButtonSize();
  192. /* doubleclick detection variables */
  193. struct key_event
  194. {
  195. unsigned int down_time;
  196. s32 x;
  197. s32 y;
  198. };
  199. /* array for saving last known position of a pointer */
  200. v2s32 m_pointerpos[MAX_TOUCH_COUNT];
  201. /* array for doubletap detection */
  202. key_event m_key_events[2];
  203. /* settings bar */
  204. AutoHideButtonBar m_settingsbar;
  205. /* rare controls bar */
  206. AutoHideButtonBar m_rarecontrolsbar;
  207. };
  208. extern TouchScreenGUI *g_touchscreengui;