touch_screen_button.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 "core/input_map.h"
  32. #include "core/os/input.h"
  33. #include "core/os/os.h"
  34. #include "scene/scene_string_names.h"
  35. void TouchScreenButton::set_texture(const Ref<Texture> &p_texture) {
  36. if (texture == p_texture) {
  37. return;
  38. }
  39. if (texture.is_valid()) {
  40. texture->disconnect(SceneStringNames::get_singleton()->changed, this, "update");
  41. }
  42. texture = p_texture;
  43. if (texture.is_valid()) {
  44. texture->connect(SceneStringNames::get_singleton()->changed, this, "update", varray(), CONNECT_REFERENCE_COUNTED);
  45. }
  46. update();
  47. }
  48. Ref<Texture> TouchScreenButton::get_texture() const {
  49. return texture;
  50. }
  51. void TouchScreenButton::set_texture_pressed(const Ref<Texture> &p_texture_pressed) {
  52. if (texture_pressed == p_texture_pressed) {
  53. return;
  54. }
  55. if (texture_pressed.is_valid()) {
  56. texture_pressed->disconnect(SceneStringNames::get_singleton()->changed, this, "update");
  57. }
  58. texture_pressed = p_texture_pressed;
  59. if (texture_pressed.is_valid()) {
  60. texture_pressed->connect(SceneStringNames::get_singleton()->changed, this, "update", varray(), CONNECT_REFERENCE_COUNTED);
  61. }
  62. update();
  63. }
  64. Ref<Texture> TouchScreenButton::get_texture_pressed() const {
  65. return texture_pressed;
  66. }
  67. void TouchScreenButton::set_bitmask(const Ref<BitMap> &p_bitmask) {
  68. bitmask = p_bitmask;
  69. }
  70. Ref<BitMap> TouchScreenButton::get_bitmask() const {
  71. return bitmask;
  72. }
  73. void TouchScreenButton::set_shape(const Ref<Shape2D> &p_shape) {
  74. if (shape == p_shape) {
  75. return;
  76. }
  77. if (shape.is_valid()) {
  78. shape->disconnect(SceneStringNames::get_singleton()->changed, this, "update");
  79. }
  80. shape = p_shape;
  81. if (shape.is_valid()) {
  82. shape->connect(SceneStringNames::get_singleton()->changed, this, "update");
  83. }
  84. update();
  85. }
  86. Ref<Shape2D> TouchScreenButton::get_shape() const {
  87. return shape;
  88. }
  89. void TouchScreenButton::set_shape_centered(bool p_shape_centered) {
  90. shape_centered = p_shape_centered;
  91. update();
  92. }
  93. bool TouchScreenButton::is_shape_visible() const {
  94. return shape_visible;
  95. }
  96. void TouchScreenButton::set_shape_visible(bool p_shape_visible) {
  97. shape_visible = p_shape_visible;
  98. update();
  99. }
  100. bool TouchScreenButton::is_shape_centered() const {
  101. return shape_centered;
  102. }
  103. void TouchScreenButton::_notification(int p_what) {
  104. switch (p_what) {
  105. case NOTIFICATION_DRAW: {
  106. if (!is_inside_tree()) {
  107. return;
  108. }
  109. if (!Engine::get_singleton()->is_editor_hint() && !OS::get_singleton()->has_touchscreen_ui_hint() && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {
  110. return;
  111. }
  112. if (finger_pressed != -1) {
  113. if (texture_pressed.is_valid()) {
  114. draw_texture(texture_pressed, Point2());
  115. } else if (texture.is_valid()) {
  116. draw_texture(texture, Point2());
  117. }
  118. } else {
  119. if (texture.is_valid()) {
  120. draw_texture(texture, Point2());
  121. }
  122. }
  123. if (!shape_visible) {
  124. return;
  125. }
  126. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  127. return;
  128. }
  129. if (shape.is_valid()) {
  130. Color draw_col = get_tree()->get_debug_collisions_color();
  131. Vector2 size = texture.is_null() ? shape->get_rect().size : texture->get_size();
  132. Vector2 pos = shape_centered ? size * 0.5f : Vector2();
  133. draw_set_transform_matrix(get_canvas_transform().translated(pos));
  134. shape->draw(get_canvas_item(), draw_col);
  135. }
  136. } break;
  137. case NOTIFICATION_ENTER_TREE: {
  138. if (!Engine::get_singleton()->is_editor_hint() && !OS::get_singleton()->has_touchscreen_ui_hint() && visibility == VISIBILITY_TOUCHSCREEN_ONLY) {
  139. return;
  140. }
  141. update();
  142. if (!Engine::get_singleton()->is_editor_hint()) {
  143. set_process_input(is_visible_in_tree());
  144. }
  145. } break;
  146. case NOTIFICATION_EXIT_TREE: {
  147. if (is_pressed()) {
  148. _release(true);
  149. }
  150. } break;
  151. case NOTIFICATION_VISIBILITY_CHANGED: {
  152. if (Engine::get_singleton()->is_editor_hint()) {
  153. break;
  154. }
  155. if (is_visible_in_tree()) {
  156. set_process_input(true);
  157. } else {
  158. set_process_input(false);
  159. if (is_pressed()) {
  160. _release();
  161. }
  162. }
  163. } break;
  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. if (p_event->get_device() != 0) {
  186. return;
  187. }
  188. const InputEventScreenTouch *st = Object::cast_to<InputEventScreenTouch>(*p_event);
  189. if (passby_press) {
  190. const InputEventScreenDrag *sd = Object::cast_to<InputEventScreenDrag>(*p_event);
  191. if (st && !st->is_pressed() && finger_pressed == st->get_index()) {
  192. _release();
  193. }
  194. if ((st && st->is_pressed()) || sd) {
  195. int index = st ? st->get_index() : sd->get_index();
  196. Point2 coord = st ? st->get_position() : sd->get_position();
  197. if (finger_pressed == -1 || index == finger_pressed) {
  198. if (_is_point_inside(coord)) {
  199. if (finger_pressed == -1) {
  200. _press(index);
  201. }
  202. } else {
  203. if (finger_pressed != -1) {
  204. _release();
  205. }
  206. }
  207. }
  208. }
  209. } else {
  210. if (st) {
  211. if (st->is_pressed()) {
  212. const bool can_press = finger_pressed == -1;
  213. if (!can_press) {
  214. return; //already fingering
  215. }
  216. if (_is_point_inside(st->get_position())) {
  217. _press(st->get_index());
  218. }
  219. } else {
  220. if (st->get_index() == finger_pressed) {
  221. _release();
  222. }
  223. }
  224. }
  225. }
  226. }
  227. bool TouchScreenButton::_is_point_inside(const Point2 &p_point) {
  228. Point2 coord = (get_global_transform_with_canvas()).affine_inverse().xform(p_point);
  229. bool touched = false;
  230. bool check_rect = true;
  231. if (shape.is_valid()) {
  232. check_rect = false;
  233. Vector2 size = texture.is_null() ? shape->get_rect().size : texture->get_size();
  234. Transform2D xform = shape_centered ? Transform2D().translated(size * 0.5f) : Transform2D();
  235. touched = shape->collide(xform, unit_rect, Transform2D(0, coord + Vector2(0.5, 0.5)));
  236. }
  237. if (bitmask.is_valid()) {
  238. check_rect = false;
  239. if (!touched && Rect2(Point2(), bitmask->get_size()).has_point(coord)) {
  240. if (bitmask->get_bit(coord)) {
  241. touched = true;
  242. }
  243. }
  244. }
  245. if (!touched && check_rect) {
  246. if (texture.is_valid()) {
  247. touched = Rect2(Size2(), texture->get_size()).has_point(coord);
  248. }
  249. }
  250. return touched;
  251. }
  252. void TouchScreenButton::_press(int p_finger_pressed) {
  253. finger_pressed = p_finger_pressed;
  254. if (action != StringName()) {
  255. Input::get_singleton()->action_press(action);
  256. Ref<InputEventAction> iea;
  257. iea.instance();
  258. iea->set_action(action);
  259. iea->set_pressed(true);
  260. get_tree()->input_event(iea);
  261. }
  262. emit_signal("pressed");
  263. update();
  264. }
  265. void TouchScreenButton::_release(bool p_exiting_tree) {
  266. finger_pressed = -1;
  267. if (action != StringName()) {
  268. Input::get_singleton()->action_release(action);
  269. if (!p_exiting_tree) {
  270. Ref<InputEventAction> iea;
  271. iea.instance();
  272. iea->set_action(action);
  273. iea->set_pressed(false);
  274. get_tree()->input_event(iea);
  275. }
  276. }
  277. if (!p_exiting_tree) {
  278. emit_signal("released");
  279. update();
  280. }
  281. }
  282. #ifdef TOOLS_ENABLED
  283. Rect2 TouchScreenButton::_edit_get_rect() const {
  284. if (texture.is_null()) {
  285. return CanvasItem::_edit_get_rect();
  286. }
  287. return Rect2(Size2(), texture->get_size());
  288. }
  289. bool TouchScreenButton::_edit_use_rect() const {
  290. return !texture.is_null();
  291. }
  292. #endif
  293. Rect2 TouchScreenButton::get_anchorable_rect() const {
  294. if (texture.is_null()) {
  295. return CanvasItem::get_anchorable_rect();
  296. }
  297. return Rect2(Size2(), texture->get_size());
  298. }
  299. void TouchScreenButton::set_visibility_mode(VisibilityMode p_mode) {
  300. visibility = p_mode;
  301. update();
  302. }
  303. TouchScreenButton::VisibilityMode TouchScreenButton::get_visibility_mode() const {
  304. return visibility;
  305. }
  306. void TouchScreenButton::set_passby_press(bool p_enable) {
  307. passby_press = p_enable;
  308. }
  309. bool TouchScreenButton::is_passby_press_enabled() const {
  310. return passby_press;
  311. }
  312. void TouchScreenButton::_bind_methods() {
  313. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TouchScreenButton::set_texture);
  314. ClassDB::bind_method(D_METHOD("get_texture"), &TouchScreenButton::get_texture);
  315. ClassDB::bind_method(D_METHOD("set_texture_pressed", "texture_pressed"), &TouchScreenButton::set_texture_pressed);
  316. ClassDB::bind_method(D_METHOD("get_texture_pressed"), &TouchScreenButton::get_texture_pressed);
  317. ClassDB::bind_method(D_METHOD("set_bitmask", "bitmask"), &TouchScreenButton::set_bitmask);
  318. ClassDB::bind_method(D_METHOD("get_bitmask"), &TouchScreenButton::get_bitmask);
  319. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &TouchScreenButton::set_shape);
  320. ClassDB::bind_method(D_METHOD("get_shape"), &TouchScreenButton::get_shape);
  321. ClassDB::bind_method(D_METHOD("set_shape_centered", "bool"), &TouchScreenButton::set_shape_centered);
  322. ClassDB::bind_method(D_METHOD("is_shape_centered"), &TouchScreenButton::is_shape_centered);
  323. ClassDB::bind_method(D_METHOD("set_shape_visible", "bool"), &TouchScreenButton::set_shape_visible);
  324. ClassDB::bind_method(D_METHOD("is_shape_visible"), &TouchScreenButton::is_shape_visible);
  325. ClassDB::bind_method(D_METHOD("set_action", "action"), &TouchScreenButton::set_action);
  326. ClassDB::bind_method(D_METHOD("get_action"), &TouchScreenButton::get_action);
  327. ClassDB::bind_method(D_METHOD("set_visibility_mode", "mode"), &TouchScreenButton::set_visibility_mode);
  328. ClassDB::bind_method(D_METHOD("get_visibility_mode"), &TouchScreenButton::get_visibility_mode);
  329. ClassDB::bind_method(D_METHOD("set_passby_press", "enabled"), &TouchScreenButton::set_passby_press);
  330. ClassDB::bind_method(D_METHOD("is_passby_press_enabled"), &TouchScreenButton::is_passby_press_enabled);
  331. ClassDB::bind_method(D_METHOD("is_pressed"), &TouchScreenButton::is_pressed);
  332. ClassDB::bind_method(D_METHOD("_input"), &TouchScreenButton::_input);
  333. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  334. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture_pressed", "get_texture_pressed");
  335. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "bitmask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_bitmask", "get_bitmask");
  336. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
  337. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_centered"), "set_shape_centered", "is_shape_centered");
  338. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_visible"), "set_shape_visible", "is_shape_visible");
  339. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "passby_press"), "set_passby_press", "is_passby_press_enabled");
  340. ADD_PROPERTY(PropertyInfo(Variant::STRING, "action"), "set_action", "get_action");
  341. ADD_PROPERTY(PropertyInfo(Variant::INT, "visibility_mode", PROPERTY_HINT_ENUM, "Always,TouchScreen Only"), "set_visibility_mode", "get_visibility_mode");
  342. ADD_SIGNAL(MethodInfo("pressed"));
  343. ADD_SIGNAL(MethodInfo("released"));
  344. BIND_ENUM_CONSTANT(VISIBILITY_ALWAYS);
  345. BIND_ENUM_CONSTANT(VISIBILITY_TOUCHSCREEN_ONLY);
  346. }
  347. TouchScreenButton::TouchScreenButton() {
  348. finger_pressed = -1;
  349. passby_press = false;
  350. visibility = VISIBILITY_ALWAYS;
  351. shape_centered = true;
  352. shape_visible = true;
  353. unit_rect = Ref<RectangleShape2D>(memnew(RectangleShape2D));
  354. unit_rect->set_extents(Vector2(0.5, 0.5));
  355. }