touch_screen_button.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*************************************************************************/
  2. /* touch_screen_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "touch_screen_button.h"
  31. #include "scene/main/window.h"
  32. void TouchScreenButton::set_texture_normal(const Ref<Texture2D> &p_texture) {
  33. texture_normal = p_texture;
  34. queue_redraw();
  35. }
  36. Ref<Texture2D> TouchScreenButton::get_texture_normal() const {
  37. return texture_normal;
  38. }
  39. void TouchScreenButton::set_texture_pressed(const Ref<Texture2D> &p_texture_pressed) {
  40. texture_pressed = p_texture_pressed;
  41. queue_redraw();
  42. }
  43. Ref<Texture2D> TouchScreenButton::get_texture_pressed() const {
  44. return texture_pressed;
  45. }
  46. void TouchScreenButton::set_bitmask(const Ref<BitMap> &p_bitmask) {
  47. bitmask = p_bitmask;
  48. }
  49. Ref<BitMap> TouchScreenButton::get_bitmask() const {
  50. return bitmask;
  51. }
  52. void TouchScreenButton::set_shape(const Ref<Shape2D> &p_shape) {
  53. if (shape.is_valid()) {
  54. shape->disconnect("changed", callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
  55. }
  56. shape = p_shape;
  57. if (shape.is_valid()) {
  58. shape->connect("changed", callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
  59. }
  60. queue_redraw();
  61. }
  62. Ref<Shape2D> TouchScreenButton::get_shape() const {
  63. return shape;
  64. }
  65. void TouchScreenButton::set_shape_centered(bool p_shape_centered) {
  66. shape_centered = p_shape_centered;
  67. queue_redraw();
  68. }
  69. bool TouchScreenButton::is_shape_visible() const {
  70. return shape_visible;
  71. }
  72. void TouchScreenButton::set_shape_visible(bool p_shape_visible) {
  73. shape_visible = p_shape_visible;
  74. queue_redraw();
  75. }
  76. bool TouchScreenButton::is_shape_centered() const {
  77. return shape_centered;
  78. }
  79. void TouchScreenButton::_notification(int p_what) {
  80. switch (p_what) {
  81. case NOTIFICATION_DRAW: {
  82. if (!is_inside_tree()) {
  83. return;
  84. }
  85. if (!Engine::get_singleton()->is_editor_hint() && !!DisplayServer::get_singleton()->screen_is_touchscreen(DisplayServer::get_singleton()->window_get_current_screen(get_viewport()->get_window_id())) && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {
  86. return;
  87. }
  88. if (finger_pressed != -1) {
  89. if (texture_pressed.is_valid()) {
  90. draw_texture(texture_pressed, Point2());
  91. } else if (texture_normal.is_valid()) {
  92. draw_texture(texture_normal, Point2());
  93. }
  94. } else {
  95. if (texture_normal.is_valid()) {
  96. draw_texture(texture_normal, Point2());
  97. }
  98. }
  99. if (!shape_visible) {
  100. return;
  101. }
  102. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  103. return;
  104. }
  105. if (shape.is_valid()) {
  106. Color draw_col = get_tree()->get_debug_collisions_color();
  107. Vector2 pos;
  108. if (shape_centered && texture_normal.is_valid()) {
  109. pos = texture_normal->get_size() * 0.5;
  110. }
  111. draw_set_transform_matrix(get_canvas_transform().translated_local(pos));
  112. shape->draw(get_canvas_item(), draw_col);
  113. }
  114. } break;
  115. case NOTIFICATION_ENTER_TREE: {
  116. if (!Engine::get_singleton()->is_editor_hint() && !!DisplayServer::get_singleton()->screen_is_touchscreen(DisplayServer::get_singleton()->window_get_current_screen(get_viewport()->get_window_id())) && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {
  117. return;
  118. }
  119. queue_redraw();
  120. if (!Engine::get_singleton()->is_editor_hint()) {
  121. set_process_input(is_visible_in_tree());
  122. }
  123. } break;
  124. case NOTIFICATION_EXIT_TREE: {
  125. if (is_pressed()) {
  126. _release(true);
  127. }
  128. } break;
  129. case NOTIFICATION_VISIBILITY_CHANGED: {
  130. if (Engine::get_singleton()->is_editor_hint()) {
  131. break;
  132. }
  133. if (is_visible_in_tree()) {
  134. set_process_input(true);
  135. } else {
  136. set_process_input(false);
  137. if (is_pressed()) {
  138. _release();
  139. }
  140. }
  141. } break;
  142. case NOTIFICATION_PAUSED: {
  143. if (is_pressed()) {
  144. _release();
  145. }
  146. } break;
  147. }
  148. }
  149. bool TouchScreenButton::is_pressed() const {
  150. return finger_pressed != -1;
  151. }
  152. void TouchScreenButton::set_action(const String &p_action) {
  153. action = p_action;
  154. }
  155. String TouchScreenButton::get_action() const {
  156. return action;
  157. }
  158. void TouchScreenButton::input(const Ref<InputEvent> &p_event) {
  159. ERR_FAIL_COND(p_event.is_null());
  160. if (!is_visible_in_tree()) {
  161. return;
  162. }
  163. if (p_event->get_device() != 0) {
  164. return;
  165. }
  166. const InputEventScreenTouch *st = Object::cast_to<InputEventScreenTouch>(*p_event);
  167. if (passby_press) {
  168. const InputEventScreenDrag *sd = Object::cast_to<InputEventScreenDrag>(*p_event);
  169. if (st && !st->is_pressed() && finger_pressed == st->get_index()) {
  170. _release();
  171. }
  172. if ((st && st->is_pressed()) || sd) {
  173. int index = st ? st->get_index() : sd->get_index();
  174. Point2 coord = st ? st->get_position() : sd->get_position();
  175. if (finger_pressed == -1 || index == finger_pressed) {
  176. if (_is_point_inside(coord)) {
  177. if (finger_pressed == -1) {
  178. _press(index);
  179. }
  180. } else {
  181. if (finger_pressed != -1) {
  182. _release();
  183. }
  184. }
  185. }
  186. }
  187. } else {
  188. if (st) {
  189. if (st->is_pressed()) {
  190. const bool can_press = finger_pressed == -1;
  191. if (!can_press) {
  192. return; //already fingering
  193. }
  194. if (_is_point_inside(st->get_position())) {
  195. _press(st->get_index());
  196. }
  197. } else {
  198. if (st->get_index() == finger_pressed) {
  199. _release();
  200. }
  201. }
  202. }
  203. }
  204. }
  205. bool TouchScreenButton::_is_point_inside(const Point2 &p_point) {
  206. Point2 coord = (get_global_transform_with_canvas()).affine_inverse().xform(p_point);
  207. bool touched = false;
  208. bool check_rect = true;
  209. if (shape.is_valid()) {
  210. check_rect = false;
  211. Vector2 pos;
  212. if (shape_centered && texture_normal.is_valid()) {
  213. pos = texture_normal->get_size() * 0.5;
  214. }
  215. touched = shape->collide(Transform2D().translated_local(pos), unit_rect, Transform2D(0, coord + Vector2(0.5, 0.5)));
  216. }
  217. if (bitmask.is_valid()) {
  218. check_rect = false;
  219. if (!touched && Rect2(Point2(), bitmask->get_size()).has_point(coord)) {
  220. if (bitmask->get_bit(coord)) {
  221. touched = true;
  222. }
  223. }
  224. }
  225. if (!touched && check_rect) {
  226. if (texture_normal.is_valid()) {
  227. touched = Rect2(Size2(), texture_normal->get_size()).has_point(coord);
  228. }
  229. }
  230. return touched;
  231. }
  232. void TouchScreenButton::_press(int p_finger_pressed) {
  233. finger_pressed = p_finger_pressed;
  234. if (action != StringName()) {
  235. Input::get_singleton()->action_press(action);
  236. Ref<InputEventAction> iea;
  237. iea.instantiate();
  238. iea->set_action(action);
  239. iea->set_pressed(true);
  240. get_viewport()->push_input(iea, true);
  241. }
  242. emit_signal(SNAME("pressed"));
  243. queue_redraw();
  244. }
  245. void TouchScreenButton::_release(bool p_exiting_tree) {
  246. finger_pressed = -1;
  247. if (action != StringName()) {
  248. Input::get_singleton()->action_release(action);
  249. if (!p_exiting_tree) {
  250. Ref<InputEventAction> iea;
  251. iea.instantiate();
  252. iea->set_action(action);
  253. iea->set_pressed(false);
  254. get_viewport()->push_input(iea, true);
  255. }
  256. }
  257. if (!p_exiting_tree) {
  258. emit_signal(SNAME("released"));
  259. queue_redraw();
  260. }
  261. }
  262. #ifdef TOOLS_ENABLED
  263. Rect2 TouchScreenButton::_edit_get_rect() const {
  264. if (texture_normal.is_null()) {
  265. return CanvasItem::_edit_get_rect();
  266. }
  267. return Rect2(Size2(), texture_normal->get_size());
  268. }
  269. bool TouchScreenButton::_edit_use_rect() const {
  270. return !texture_normal.is_null();
  271. }
  272. #endif
  273. Rect2 TouchScreenButton::get_anchorable_rect() const {
  274. if (texture_normal.is_null()) {
  275. return CanvasItem::get_anchorable_rect();
  276. }
  277. return Rect2(Size2(), texture_normal->get_size());
  278. }
  279. void TouchScreenButton::set_visibility_mode(VisibilityMode p_mode) {
  280. visibility = p_mode;
  281. queue_redraw();
  282. }
  283. TouchScreenButton::VisibilityMode TouchScreenButton::get_visibility_mode() const {
  284. return visibility;
  285. }
  286. void TouchScreenButton::set_passby_press(bool p_enable) {
  287. passby_press = p_enable;
  288. }
  289. bool TouchScreenButton::is_passby_press_enabled() const {
  290. return passby_press;
  291. }
  292. void TouchScreenButton::_bind_methods() {
  293. ClassDB::bind_method(D_METHOD("set_texture_normal", "texture"), &TouchScreenButton::set_texture_normal);
  294. ClassDB::bind_method(D_METHOD("get_texture_normal"), &TouchScreenButton::get_texture_normal);
  295. ClassDB::bind_method(D_METHOD("set_texture_pressed", "texture"), &TouchScreenButton::set_texture_pressed);
  296. ClassDB::bind_method(D_METHOD("get_texture_pressed"), &TouchScreenButton::get_texture_pressed);
  297. ClassDB::bind_method(D_METHOD("set_bitmask", "bitmask"), &TouchScreenButton::set_bitmask);
  298. ClassDB::bind_method(D_METHOD("get_bitmask"), &TouchScreenButton::get_bitmask);
  299. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &TouchScreenButton::set_shape);
  300. ClassDB::bind_method(D_METHOD("get_shape"), &TouchScreenButton::get_shape);
  301. ClassDB::bind_method(D_METHOD("set_shape_centered", "bool"), &TouchScreenButton::set_shape_centered);
  302. ClassDB::bind_method(D_METHOD("is_shape_centered"), &TouchScreenButton::is_shape_centered);
  303. ClassDB::bind_method(D_METHOD("set_shape_visible", "bool"), &TouchScreenButton::set_shape_visible);
  304. ClassDB::bind_method(D_METHOD("is_shape_visible"), &TouchScreenButton::is_shape_visible);
  305. ClassDB::bind_method(D_METHOD("set_action", "action"), &TouchScreenButton::set_action);
  306. ClassDB::bind_method(D_METHOD("get_action"), &TouchScreenButton::get_action);
  307. ClassDB::bind_method(D_METHOD("set_visibility_mode", "mode"), &TouchScreenButton::set_visibility_mode);
  308. ClassDB::bind_method(D_METHOD("get_visibility_mode"), &TouchScreenButton::get_visibility_mode);
  309. ClassDB::bind_method(D_METHOD("set_passby_press", "enabled"), &TouchScreenButton::set_passby_press);
  310. ClassDB::bind_method(D_METHOD("is_passby_press_enabled"), &TouchScreenButton::is_passby_press_enabled);
  311. ClassDB::bind_method(D_METHOD("is_pressed"), &TouchScreenButton::is_pressed);
  312. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_normal", "get_texture_normal");
  313. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_pressed", "get_texture_pressed");
  314. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "bitmask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_bitmask", "get_bitmask");
  315. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
  316. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_centered"), "set_shape_centered", "is_shape_centered");
  317. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_visible"), "set_shape_visible", "is_shape_visible");
  318. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "passby_press"), "set_passby_press", "is_passby_press_enabled");
  319. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "action"), "set_action", "get_action");
  320. ADD_PROPERTY(PropertyInfo(Variant::INT, "visibility_mode", PROPERTY_HINT_ENUM, "Always,TouchScreen Only"), "set_visibility_mode", "get_visibility_mode");
  321. ADD_SIGNAL(MethodInfo("pressed"));
  322. ADD_SIGNAL(MethodInfo("released"));
  323. BIND_ENUM_CONSTANT(VISIBILITY_ALWAYS);
  324. BIND_ENUM_CONSTANT(VISIBILITY_TOUCHSCREEN_ONLY);
  325. }
  326. TouchScreenButton::TouchScreenButton() {
  327. unit_rect = Ref<RectangleShape2D>(memnew(RectangleShape2D));
  328. unit_rect->set_size(Vector2(1, 1));
  329. }