touch_screen_button.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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/window.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::_notification(int p_what) {
  101. switch (p_what) {
  102. case NOTIFICATION_DRAW: {
  103. if (!is_inside_tree()) {
  104. return;
  105. }
  106. if (!Engine::get_singleton()->is_editor_hint() && !DisplayServer::get_singleton()->is_touchscreen_available() && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {
  107. return;
  108. }
  109. if (finger_pressed != -1) {
  110. if (texture_pressed.is_valid()) {
  111. draw_texture(texture_pressed, Point2());
  112. } else if (texture_normal.is_valid()) {
  113. draw_texture(texture_normal, Point2());
  114. }
  115. } else {
  116. if (texture_normal.is_valid()) {
  117. draw_texture(texture_normal, Point2());
  118. }
  119. }
  120. if (!shape_visible) {
  121. return;
  122. }
  123. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  124. return;
  125. }
  126. if (shape.is_valid()) {
  127. Color draw_col = get_tree()->get_debug_collisions_color();
  128. Vector2 pos;
  129. if (shape_centered && texture_normal.is_valid()) {
  130. pos = texture_normal->get_size() * 0.5;
  131. }
  132. draw_set_transform_matrix(get_canvas_transform().translated_local(pos));
  133. shape->draw(get_canvas_item(), draw_col);
  134. }
  135. } break;
  136. case NOTIFICATION_ENTER_TREE: {
  137. if (!Engine::get_singleton()->is_editor_hint() && !DisplayServer::get_singleton()->is_touchscreen_available() && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {
  138. return;
  139. }
  140. queue_redraw();
  141. if (!Engine::get_singleton()->is_editor_hint()) {
  142. set_process_input(is_visible_in_tree());
  143. }
  144. } break;
  145. case NOTIFICATION_EXIT_TREE: {
  146. if (is_pressed()) {
  147. _release(true);
  148. }
  149. } break;
  150. case NOTIFICATION_VISIBILITY_CHANGED: {
  151. if (Engine::get_singleton()->is_editor_hint()) {
  152. break;
  153. }
  154. if (is_visible_in_tree()) {
  155. set_process_input(true);
  156. } else {
  157. set_process_input(false);
  158. if (is_pressed()) {
  159. _release();
  160. }
  161. }
  162. } break;
  163. case NOTIFICATION_SUSPENDED:
  164. case NOTIFICATION_PAUSED: {
  165. if (is_pressed()) {
  166. _release();
  167. }
  168. } break;
  169. }
  170. }
  171. bool TouchScreenButton::is_pressed() const {
  172. return finger_pressed != -1;
  173. }
  174. void TouchScreenButton::set_action(const String &p_action) {
  175. action = p_action;
  176. }
  177. String TouchScreenButton::get_action() const {
  178. return action;
  179. }
  180. void TouchScreenButton::input(const Ref<InputEvent> &p_event) {
  181. ERR_FAIL_COND(p_event.is_null());
  182. if (!is_visible_in_tree()) {
  183. return;
  184. }
  185. const InputEventScreenTouch *st = Object::cast_to<InputEventScreenTouch>(*p_event);
  186. if (passby_press) {
  187. const InputEventScreenDrag *sd = Object::cast_to<InputEventScreenDrag>(*p_event);
  188. if (st && !st->is_pressed() && finger_pressed == st->get_index()) {
  189. _release();
  190. }
  191. if ((st && st->is_pressed()) || sd) {
  192. int index = st ? st->get_index() : sd->get_index();
  193. Point2 coord = st ? st->get_position() : sd->get_position();
  194. if (finger_pressed == -1 || index == finger_pressed) {
  195. if (_is_point_inside(coord)) {
  196. if (finger_pressed == -1) {
  197. _press(index);
  198. }
  199. } else {
  200. if (finger_pressed != -1) {
  201. _release();
  202. }
  203. }
  204. }
  205. }
  206. } else {
  207. if (st) {
  208. if (st->is_pressed()) {
  209. const bool can_press = finger_pressed == -1;
  210. if (!can_press) {
  211. return; //already fingering
  212. }
  213. if (_is_point_inside(st->get_position())) {
  214. _press(st->get_index());
  215. }
  216. } else {
  217. if (st->get_index() == finger_pressed) {
  218. _release();
  219. }
  220. }
  221. }
  222. }
  223. }
  224. bool TouchScreenButton::_is_point_inside(const Point2 &p_point) {
  225. Point2 coord = (get_global_transform_with_canvas()).affine_inverse().xform(p_point);
  226. bool touched = false;
  227. bool check_rect = true;
  228. if (shape.is_valid()) {
  229. check_rect = false;
  230. Vector2 pos;
  231. if (shape_centered && texture_normal.is_valid()) {
  232. pos = texture_normal->get_size() * 0.5;
  233. }
  234. touched = shape->collide(Transform2D().translated_local(pos), unit_rect, Transform2D(0, coord + Vector2(0.5, 0.5)));
  235. }
  236. if (bitmask.is_valid()) {
  237. check_rect = false;
  238. if (!touched && Rect2(Point2(), bitmask->get_size()).has_point(coord)) {
  239. if (bitmask->get_bitv(coord)) {
  240. touched = true;
  241. }
  242. }
  243. }
  244. if (!touched && check_rect) {
  245. if (texture_normal.is_valid()) {
  246. touched = Rect2(Size2(), texture_normal->get_size()).has_point(coord);
  247. }
  248. }
  249. return touched;
  250. }
  251. void TouchScreenButton::_press(int p_finger_pressed) {
  252. finger_pressed = p_finger_pressed;
  253. if (action != StringName()) {
  254. Input::get_singleton()->action_press(action);
  255. Ref<InputEventAction> iea;
  256. iea.instantiate();
  257. iea->set_action(action);
  258. iea->set_pressed(true);
  259. get_viewport()->push_input(iea, true);
  260. }
  261. emit_signal(SceneStringName(pressed));
  262. queue_redraw();
  263. }
  264. void TouchScreenButton::_release(bool p_exiting_tree) {
  265. finger_pressed = -1;
  266. if (action != StringName()) {
  267. Input::get_singleton()->action_release(action);
  268. if (!p_exiting_tree) {
  269. Ref<InputEventAction> iea;
  270. iea.instantiate();
  271. iea->set_action(action);
  272. iea->set_pressed(false);
  273. get_viewport()->push_input(iea, true);
  274. }
  275. }
  276. if (!p_exiting_tree) {
  277. emit_signal(SNAME("released"));
  278. queue_redraw();
  279. }
  280. }
  281. #ifdef DEBUG_ENABLED
  282. Rect2 TouchScreenButton::_edit_get_rect() const {
  283. if (texture_normal.is_null()) {
  284. return CanvasItem::_edit_get_rect();
  285. }
  286. return Rect2(Size2(), texture_normal->get_size());
  287. }
  288. bool TouchScreenButton::_edit_use_rect() const {
  289. return !texture_normal.is_null();
  290. }
  291. #endif // DEBUG_ENABLED
  292. Rect2 TouchScreenButton::get_anchorable_rect() const {
  293. if (texture_normal.is_null()) {
  294. return CanvasItem::get_anchorable_rect();
  295. }
  296. return Rect2(Size2(), texture_normal->get_size());
  297. }
  298. void TouchScreenButton::set_visibility_mode(VisibilityMode p_mode) {
  299. visibility = p_mode;
  300. queue_redraw();
  301. }
  302. TouchScreenButton::VisibilityMode TouchScreenButton::get_visibility_mode() const {
  303. return visibility;
  304. }
  305. void TouchScreenButton::set_passby_press(bool p_enable) {
  306. passby_press = p_enable;
  307. }
  308. bool TouchScreenButton::is_passby_press_enabled() const {
  309. return passby_press;
  310. }
  311. #ifndef DISABLE_DEPRECATED
  312. bool TouchScreenButton::_set(const StringName &p_name, const Variant &p_value) {
  313. if (p_name == CoreStringName(normal)) { // Compatibility with Godot 3.x.
  314. set_texture_normal(p_value);
  315. return true;
  316. } else if (p_name == SceneStringName(pressed)) { // Compatibility with Godot 3.x.
  317. set_texture_pressed(p_value);
  318. return true;
  319. }
  320. return false;
  321. }
  322. #endif // DISABLE_DEPRECATED
  323. void TouchScreenButton::_bind_methods() {
  324. ClassDB::bind_method(D_METHOD("set_texture_normal", "texture"), &TouchScreenButton::set_texture_normal);
  325. ClassDB::bind_method(D_METHOD("get_texture_normal"), &TouchScreenButton::get_texture_normal);
  326. ClassDB::bind_method(D_METHOD("set_texture_pressed", "texture"), &TouchScreenButton::set_texture_pressed);
  327. ClassDB::bind_method(D_METHOD("get_texture_pressed"), &TouchScreenButton::get_texture_pressed);
  328. ClassDB::bind_method(D_METHOD("set_bitmask", "bitmask"), &TouchScreenButton::set_bitmask);
  329. ClassDB::bind_method(D_METHOD("get_bitmask"), &TouchScreenButton::get_bitmask);
  330. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &TouchScreenButton::set_shape);
  331. ClassDB::bind_method(D_METHOD("get_shape"), &TouchScreenButton::get_shape);
  332. ClassDB::bind_method(D_METHOD("set_shape_centered", "bool"), &TouchScreenButton::set_shape_centered);
  333. ClassDB::bind_method(D_METHOD("is_shape_centered"), &TouchScreenButton::is_shape_centered);
  334. ClassDB::bind_method(D_METHOD("set_shape_visible", "bool"), &TouchScreenButton::set_shape_visible);
  335. ClassDB::bind_method(D_METHOD("is_shape_visible"), &TouchScreenButton::is_shape_visible);
  336. ClassDB::bind_method(D_METHOD("set_action", "action"), &TouchScreenButton::set_action);
  337. ClassDB::bind_method(D_METHOD("get_action"), &TouchScreenButton::get_action);
  338. ClassDB::bind_method(D_METHOD("set_visibility_mode", "mode"), &TouchScreenButton::set_visibility_mode);
  339. ClassDB::bind_method(D_METHOD("get_visibility_mode"), &TouchScreenButton::get_visibility_mode);
  340. ClassDB::bind_method(D_METHOD("set_passby_press", "enabled"), &TouchScreenButton::set_passby_press);
  341. ClassDB::bind_method(D_METHOD("is_passby_press_enabled"), &TouchScreenButton::is_passby_press_enabled);
  342. ClassDB::bind_method(D_METHOD("is_pressed"), &TouchScreenButton::is_pressed);
  343. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_normal", "get_texture_normal");
  344. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_pressed", "get_texture_pressed");
  345. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "bitmask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_bitmask", "get_bitmask");
  346. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
  347. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_centered"), "set_shape_centered", "is_shape_centered");
  348. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_visible"), "set_shape_visible", "is_shape_visible");
  349. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "passby_press"), "set_passby_press", "is_passby_press_enabled");
  350. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "action"), "set_action", "get_action");
  351. ADD_PROPERTY(PropertyInfo(Variant::INT, "visibility_mode", PROPERTY_HINT_ENUM, "Always,TouchScreen Only"), "set_visibility_mode", "get_visibility_mode");
  352. ADD_SIGNAL(MethodInfo("pressed"));
  353. ADD_SIGNAL(MethodInfo("released"));
  354. BIND_ENUM_CONSTANT(VISIBILITY_ALWAYS);
  355. BIND_ENUM_CONSTANT(VISIBILITY_TOUCHSCREEN_ONLY);
  356. }
  357. TouchScreenButton::TouchScreenButton() {
  358. unit_rect.instantiate();
  359. unit_rect->set_size(Vector2(1, 1));
  360. }