touchscreengui.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 <IGUIEnvironment.h>
  18. #include <IGUIButton.h>
  19. #include <IEventReceiver.h>
  20. #include <vector>
  21. #include <map>
  22. #include "game.h"
  23. #include "tile.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. inventory_id,
  35. chat_id,
  36. after_last_element_id
  37. } touch_gui_button_id;
  38. #define MIN_DIG_TIME_MS 500
  39. #define MAX_TOUCH_COUNT 64
  40. extern const char** touchgui_button_imagenames;
  41. class TouchScreenGUI
  42. {
  43. public:
  44. TouchScreenGUI(IrrlichtDevice *device, IEventReceiver* receiver);
  45. ~TouchScreenGUI();
  46. void translateEvent(const SEvent &event);
  47. void init(ISimpleTextureSource* tsrc,float density);
  48. double getYaw() { return m_camera_yaw; }
  49. double getPitch() { return m_camera_pitch; }
  50. line3d<f32> getShootline() { return m_shootline; }
  51. void step(float dtime);
  52. void resetHud();
  53. void registerHudItem(int index, const rect<s32> &rect);
  54. void Toggle(bool visible);
  55. void Hide();
  56. void Show();
  57. private:
  58. IrrlichtDevice* m_device;
  59. IGUIEnvironment* m_guienv;
  60. IEventReceiver* m_receiver;
  61. ISimpleTextureSource* m_texturesource;
  62. v2u32 m_screensize;
  63. std::map<int,rect<s32> > m_hud_rects;
  64. std::map<int,irr::EKEY_CODE> m_hud_ids;
  65. bool m_visible; // is the gui visible
  66. /* value in degree */
  67. double m_camera_yaw;
  68. double m_camera_pitch;
  69. line3d<f32> m_shootline;
  70. rect<s32> m_control_pad_rect;
  71. int m_move_id;
  72. bool m_move_has_really_moved;
  73. s32 m_move_downtime;
  74. bool m_move_sent_as_mouse_event;
  75. v2s32 m_move_downlocation;
  76. struct button_info {
  77. float repeatcounter;
  78. irr::EKEY_CODE keycode;
  79. std::vector<int> ids;
  80. IGUIButton* guibutton;
  81. bool immediate_release;
  82. };
  83. button_info m_buttons[after_last_element_id];
  84. /* gui button detection */
  85. touch_gui_button_id getButtonID(s32 x, s32 y);
  86. /* gui button by eventID */
  87. touch_gui_button_id getButtonID(int eventID);
  88. /* check if a button has changed */
  89. void handleChangedButton(const SEvent &event);
  90. /* initialize a button */
  91. void initButton(touch_gui_button_id id, rect<s32> button_rect,
  92. std::wstring caption, bool immediate_release );
  93. /* load texture */
  94. void loadButtonTexture(button_info* btn, const char* path);
  95. struct id_status{
  96. int id;
  97. int X;
  98. int Y;
  99. };
  100. /* vector to store known ids and their initial touch positions*/
  101. std::vector<id_status> m_known_ids;
  102. /* handle a button event */
  103. void ButtonEvent(touch_gui_button_id bID, int eventID, bool action);
  104. /* handle pressed hud buttons */
  105. bool isHUDButton(const SEvent &event);
  106. /* handle released hud buttons */
  107. bool isReleaseHUDButton(int eventID);
  108. /* handle double taps */
  109. bool doubleTapDetection();
  110. /* doubleclick detection variables */
  111. struct key_event {
  112. unsigned int down_time;
  113. s32 x;
  114. s32 y;
  115. };
  116. /* array for saving last known position of a pointer */
  117. v2s32 m_pointerpos[MAX_TOUCH_COUNT];
  118. /* array for doubletap detection */
  119. key_event m_key_events[2];
  120. };
  121. extern TouchScreenGUI *g_touchscreengui;
  122. #endif