touch_screen_button.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /**************************************************************************/
  2. /* touch_screen_button.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/viewport.h"
  32. void TouchScreenButton::set_texture_normal(const Ref<Texture2D> &p_texture) {
  33. if (texture_normal == p_texture) {
  34. return;
  35. }
  36. if (texture_normal.is_valid()) {
  37. texture_normal->disconnect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
  38. }
  39. texture_normal = p_texture;
  40. if (texture_normal.is_valid()) {
  41. texture_normal->connect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw), CONNECT_REFERENCE_COUNTED);
  42. }
  43. queue_redraw();
  44. }
  45. Ref<Texture2D> TouchScreenButton::get_texture_normal() const {
  46. return texture_normal;
  47. }
  48. void TouchScreenButton::set_texture_pressed(const Ref<Texture2D> &p_texture_pressed) {
  49. if (texture_pressed == p_texture_pressed) {
  50. return;
  51. }
  52. if (texture_pressed.is_valid()) {
  53. texture_pressed->disconnect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
  54. }
  55. texture_pressed = p_texture_pressed;
  56. if (texture_pressed.is_valid()) {
  57. texture_pressed->connect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw), CONNECT_REFERENCE_COUNTED);
  58. }
  59. queue_redraw();
  60. }
  61. Ref<Texture2D> TouchScreenButton::get_texture_pressed() const {
  62. return texture_pressed;
  63. }
  64. void TouchScreenButton::set_bitmask(const Ref<BitMap> &p_bitmask) {
  65. bitmask = p_bitmask;
  66. }
  67. Ref<BitMap> TouchScreenButton::get_bitmask() const {
  68. return bitmask;
  69. }
  70. void TouchScreenButton::set_shape(const Ref<Shape2D> &p_shape) {
  71. if (shape == p_shape) {
  72. return;
  73. }
  74. if (shape.is_valid()) {
  75. shape->disconnect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
  76. }
  77. shape = p_shape;
  78. if (shape.is_valid()) {
  79. shape->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
  80. }
  81. queue_redraw();
  82. }
  83. Ref<Shape2D> TouchScreenButton::get_shape() const {
  84. return shape;
  85. }
  86. void TouchScreenButton::set_shape_centered(bool p_shape_centered) {
  87. shape_centered = p_shape_centered;
  88. queue_redraw();
  89. }
  90. bool TouchScreenButton::is_shape_visible() const {
  91. return shape_visible;
  92. }
  93. void TouchScreenButton::set_shape_visible(bool p_shape_visible) {
  94. shape_visible = p_shape_visible;
  95. queue_redraw();
  96. }
  97. bool TouchScreenButton::is_shape_centered() const {
  98. return shape_centered;
  99. }
  100. void TouchScreenButton::_accessibility_action_click(const Variant &p_data) {
  101. _press(0);
  102. _release();
  103. }
  104. void TouchScreenButton::_notification(int p_what) {
  105. switch (p_what) {
  106. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  107. RID ae = get_accessibility_element();
  108. ERR_FAIL_COND(ae.is_null());
  109. Rect2 dst_rect(Point2(), texture_normal.is_valid() ? texture_normal->get_size() : Size2());
  110. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
  111. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &TouchScreenButton::_accessibility_action_click));
  112. DisplayServer::get_singleton()->accessibility_update_set_transform(ae, get_transform());
  113. DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, dst_rect);
  114. } break;
  115. case NOTIFICATION_DRAW: {
  116. if (!is_inside_tree()) {
  117. return;
  118. }
  119. if (!Engine::get_singleton()->is_editor_hint() && !DisplayServer::get_singleton()->is_touchscreen_available() && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {
  120. return;
  121. }
  122. if (finger_pressed != -1) {
  123. if (texture_pressed.is_valid()) {
  124. draw_texture(texture_pressed, Point2());
  125. } else if (texture_normal.is_valid()) {
  126. draw_texture(texture_normal, Point2());
  127. }
  128. } else {
  129. if (texture_normal.is_valid()) {
  130. draw_texture(texture_normal, Point2());
  131. }
  132. }
  133. if (!shape_visible) {
  134. return;
  135. }
  136. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  137. return;
  138. }
  139. if (shape.is_valid()) {
  140. Color draw_col = get_tree()->get_debug_collisions_color();
  141. Vector2 pos;
  142. if (shape_centered && texture_normal.is_valid()) {
  143. pos = texture_normal->get_size() * 0.5;
  144. }
  145. draw_set_transform_matrix(get_canvas_transform().translated_local(pos));
  146. shape->draw(get_canvas_item(), draw_col);
  147. }
  148. } break;
  149. case NOTIFICATION_ENTER_TREE: {
  150. if (!Engine::get_singleton()->is_editor_hint() && !DisplayServer::get_singleton()->is_touchscreen_available() && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {
  151. return;
  152. }
  153. queue_redraw();
  154. if (!Engine::get_singleton()->is_editor_hint()) {
  155. set_process_input(is_visible_in_tree());
  156. }
  157. } break;
  158. case NOTIFICATION_EXIT_TREE: {
  159. if (is_pressed()) {
  160. _release(true);
  161. }
  162. } break;
  163. case NOTIFICATION_VISIBILITY_CHANGED: {
  164. if (Engine::get_singleton()->is_editor_hint()) {
  165. break;
  166. }
  167. if (is_visible_in_tree()) {
  168. set_process_input(true);
  169. } else {
  170. set_process_input(false);
  171. if (is_pressed()) {
  172. _release();
  173. }
  174. }
  175. } break;
  176. case NOTIFICATION_SUSPENDED:
  177. case NOTIFICATION_PAUSED: {
  178. if (is_pressed()) {
  179. _release();
  180. }
  181. } break;
  182. }
  183. }
  184. bool TouchScreenButton::is_pressed() const {
  185. return finger_pressed != -1;
  186. }
  187. void TouchScreenButton::set_action(const String &p_action) {
  188. action = p_action;
  189. }
  190. String TouchScreenButton::get_action() const {
  191. return action;
  192. }
  193. void TouchScreenButton::input(const Ref<InputEvent> &p_event) {
  194. ERR_FAIL_COND(p_event.is_null());
  195. if (!is_visible_in_tree()) {
  196. return;
  197. }
  198. const InputEventScreenTouch *st = Object::cast_to<InputEventScreenTouch>(*p_event);
  199. if (passby_press) {
  200. const InputEventScreenDrag *sd = Object::cast_to<InputEventScreenDrag>(*p_event);
  201. if (st && !st->is_pressed() && finger_pressed == st->get_index()) {
  202. _release();
  203. }
  204. if ((st && st->is_pressed()) || sd) {
  205. int index = st ? st->get_index() : sd->get_index();
  206. Point2 coord = st ? st->get_position() : sd->get_position();
  207. if (finger_pressed == -1 || index == finger_pressed) {
  208. if (_is_point_inside(coord)) {
  209. if (finger_pressed == -1) {
  210. _press(index);
  211. }
  212. } else {
  213. if (finger_pressed != -1) {
  214. _release();
  215. }
  216. }
  217. }
  218. }
  219. } else {
  220. if (st) {
  221. if (st->is_pressed()) {
  222. const bool can_press = finger_pressed == -1;
  223. if (!can_press) {
  224. return; //already fingering
  225. }
  226. if (_is_point_inside(st->get_position())) {
  227. _press(st->get_index());
  228. }
  229. } else {
  230. if (st->get_index() == finger_pressed) {
  231. _release();
  232. }
  233. }
  234. }
  235. }
  236. }
  237. bool TouchScreenButton::_is_point_inside(const Point2 &p_point) {
  238. Point2 coord = (get_global_transform_with_canvas()).affine_inverse().xform(p_point);
  239. bool touched = false;
  240. bool check_rect = true;
  241. if (shape.is_valid()) {
  242. check_rect = false;
  243. Vector2 pos;
  244. if (shape_centered && texture_normal.is_valid()) {
  245. pos = texture_normal->get_size() * 0.5;
  246. }
  247. touched = shape->collide(Transform2D().translated_local(pos), unit_rect, Transform2D(0, coord + Vector2(0.5, 0.5)));
  248. }
  249. if (bitmask.is_valid()) {
  250. check_rect = false;
  251. if (!touched && Rect2(Point2(), bitmask->get_size()).has_point(coord)) {
  252. if (bitmask->get_bitv(coord)) {
  253. touched = true;
  254. }
  255. }
  256. }
  257. if (!touched && check_rect) {
  258. if (texture_normal.is_valid()) {
  259. touched = Rect2(Size2(), texture_normal->get_size()).has_point(coord);
  260. }
  261. }
  262. return touched;
  263. }
  264. void TouchScreenButton::_press(int p_finger_pressed) {
  265. finger_pressed = p_finger_pressed;
  266. if (action != StringName()) {
  267. Input::get_singleton()->action_press(action);
  268. Ref<InputEventAction> iea;
  269. iea.instantiate();
  270. iea->set_action(action);
  271. iea->set_pressed(true);
  272. get_viewport()->push_input(iea, true);
  273. }
  274. emit_signal(SceneStringName(pressed));
  275. queue_redraw();
  276. }
  277. void TouchScreenButton::_release(bool p_exiting_tree) {
  278. finger_pressed = -1;
  279. if (action != StringName()) {
  280. Input::get_singleton()->action_release(action);
  281. if (!p_exiting_tree) {
  282. Ref<InputEventAction> iea;
  283. iea.instantiate();
  284. iea->set_action(action);
  285. iea->set_pressed(false);
  286. get_viewport()->push_input(iea, true);
  287. }
  288. }
  289. if (!p_exiting_tree) {
  290. emit_signal(SNAME("released"));
  291. queue_redraw();
  292. }
  293. }
  294. #ifdef DEBUG_ENABLED
  295. Rect2 TouchScreenButton::_edit_get_rect() const {
  296. if (texture_normal.is_null()) {
  297. return CanvasItem::_edit_get_rect();
  298. }
  299. return Rect2(Size2(), texture_normal->get_size());
  300. }
  301. bool TouchScreenButton::_edit_use_rect() const {
  302. return texture_normal.is_valid();
  303. }
  304. #endif // DEBUG_ENABLED
  305. Rect2 TouchScreenButton::get_anchorable_rect() const {
  306. if (texture_normal.is_null()) {
  307. return CanvasItem::get_anchorable_rect();
  308. }
  309. return Rect2(Size2(), texture_normal->get_size());
  310. }
  311. void TouchScreenButton::set_visibility_mode(VisibilityMode p_mode) {
  312. visibility = p_mode;
  313. queue_redraw();
  314. }
  315. TouchScreenButton::VisibilityMode TouchScreenButton::get_visibility_mode() const {
  316. return visibility;
  317. }
  318. void TouchScreenButton::set_passby_press(bool p_enable) {
  319. passby_press = p_enable;
  320. }
  321. bool TouchScreenButton::is_passby_press_enabled() const {
  322. return passby_press;
  323. }
  324. #ifndef DISABLE_DEPRECATED
  325. bool TouchScreenButton::_set(const StringName &p_name, const Variant &p_value) {
  326. if (p_name == CoreStringName(normal)) { // Compatibility with Godot 3.x.
  327. set_texture_normal(p_value);
  328. return true;
  329. } else if (p_name == SceneStringName(pressed)) { // Compatibility with Godot 3.x.
  330. set_texture_pressed(p_value);
  331. return true;
  332. }
  333. return false;
  334. }
  335. #endif // DISABLE_DEPRECATED
  336. void TouchScreenButton::_bind_methods() {
  337. ClassDB::bind_method(D_METHOD("set_texture_normal", "texture"), &TouchScreenButton::set_texture_normal);
  338. ClassDB::bind_method(D_METHOD("get_texture_normal"), &TouchScreenButton::get_texture_normal);
  339. ClassDB::bind_method(D_METHOD("set_texture_pressed", "texture"), &TouchScreenButton::set_texture_pressed);
  340. ClassDB::bind_method(D_METHOD("get_texture_pressed"), &TouchScreenButton::get_texture_pressed);
  341. ClassDB::bind_method(D_METHOD("set_bitmask", "bitmask"), &TouchScreenButton::set_bitmask);
  342. ClassDB::bind_method(D_METHOD("get_bitmask"), &TouchScreenButton::get_bitmask);
  343. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &TouchScreenButton::set_shape);
  344. ClassDB::bind_method(D_METHOD("get_shape"), &TouchScreenButton::get_shape);
  345. ClassDB::bind_method(D_METHOD("set_shape_centered", "bool"), &TouchScreenButton::set_shape_centered);
  346. ClassDB::bind_method(D_METHOD("is_shape_centered"), &TouchScreenButton::is_shape_centered);
  347. ClassDB::bind_method(D_METHOD("set_shape_visible", "bool"), &TouchScreenButton::set_shape_visible);
  348. ClassDB::bind_method(D_METHOD("is_shape_visible"), &TouchScreenButton::is_shape_visible);
  349. ClassDB::bind_method(D_METHOD("set_action", "action"), &TouchScreenButton::set_action);
  350. ClassDB::bind_method(D_METHOD("get_action"), &TouchScreenButton::get_action);
  351. ClassDB::bind_method(D_METHOD("set_visibility_mode", "mode"), &TouchScreenButton::set_visibility_mode);
  352. ClassDB::bind_method(D_METHOD("get_visibility_mode"), &TouchScreenButton::get_visibility_mode);
  353. ClassDB::bind_method(D_METHOD("set_passby_press", "enabled"), &TouchScreenButton::set_passby_press);
  354. ClassDB::bind_method(D_METHOD("is_passby_press_enabled"), &TouchScreenButton::is_passby_press_enabled);
  355. ClassDB::bind_method(D_METHOD("is_pressed"), &TouchScreenButton::is_pressed);
  356. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_normal", "get_texture_normal");
  357. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_pressed", "get_texture_pressed");
  358. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "bitmask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_bitmask", "get_bitmask");
  359. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
  360. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_centered"), "set_shape_centered", "is_shape_centered");
  361. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_visible"), "set_shape_visible", "is_shape_visible");
  362. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "passby_press"), "set_passby_press", "is_passby_press_enabled");
  363. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "action"), "set_action", "get_action");
  364. ADD_PROPERTY(PropertyInfo(Variant::INT, "visibility_mode", PROPERTY_HINT_ENUM, "Always,TouchScreen Only"), "set_visibility_mode", "get_visibility_mode");
  365. ADD_SIGNAL(MethodInfo("pressed"));
  366. ADD_SIGNAL(MethodInfo("released"));
  367. BIND_ENUM_CONSTANT(VISIBILITY_ALWAYS);
  368. BIND_ENUM_CONSTANT(VISIBILITY_TOUCHSCREEN_ONLY);
  369. }
  370. TouchScreenButton::TouchScreenButton() {
  371. unit_rect.instantiate();
  372. unit_rect->set_size(Vector2(1, 1));
  373. }