You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
5.6KB

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