screen_button.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*************************************************************************/
  2. /* screen_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "screen_button.h"
  31. #include "input_map.h"
  32. #include "os/input.h"
  33. #include "os/os.h"
  34. void TouchScreenButton::set_texture(const Ref<Texture> &p_texture) {
  35. texture = p_texture;
  36. update();
  37. }
  38. Ref<Texture> TouchScreenButton::get_texture() const {
  39. return texture;
  40. }
  41. void TouchScreenButton::set_texture_pressed(const Ref<Texture> &p_texture_pressed) {
  42. texture_pressed = p_texture_pressed;
  43. update();
  44. }
  45. Ref<Texture> TouchScreenButton::get_texture_pressed() const {
  46. return texture_pressed;
  47. }
  48. void TouchScreenButton::set_bitmask(const Ref<BitMap> &p_bitmask) {
  49. bitmask = p_bitmask;
  50. }
  51. Ref<BitMap> TouchScreenButton::get_bitmask() const {
  52. return bitmask;
  53. }
  54. void TouchScreenButton::set_shape(const Ref<Shape2D> &p_shape) {
  55. if (shape.is_valid())
  56. shape->disconnect("changed", this, "update");
  57. shape = p_shape;
  58. if (shape.is_valid())
  59. shape->connect("changed", this, "update");
  60. update();
  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. update();
  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. update();
  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. if (!Engine::get_singleton()->is_editor_hint() && !OS::get_singleton()->has_touchscreen_ui_hint() && visibility == VISIBILITY_TOUCHSCREEN_ONLY)
  85. return;
  86. if (finger_pressed != -1) {
  87. if (texture_pressed.is_valid())
  88. draw_texture(texture_pressed, Point2());
  89. else if (texture.is_valid())
  90. draw_texture(texture, Point2());
  91. } else {
  92. if (texture.is_valid())
  93. draw_texture(texture, Point2());
  94. }
  95. if (!shape_visible)
  96. return;
  97. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint())
  98. return;
  99. if (shape.is_valid()) {
  100. Color draw_col = get_tree()->get_debug_collisions_color();
  101. Vector2 pos = shape_centered ? get_item_rect().size * 0.5f : Vector2();
  102. draw_set_transform_matrix(get_canvas_transform().translated(pos));
  103. shape->draw(get_canvas_item(), draw_col);
  104. }
  105. } break;
  106. case NOTIFICATION_ENTER_TREE: {
  107. if (!Engine::get_singleton()->is_editor_hint() && !OS::get_singleton()->has_touchscreen_ui_hint() && visibility == VISIBILITY_TOUCHSCREEN_ONLY)
  108. return;
  109. update();
  110. if (!Engine::get_singleton()->is_editor_hint())
  111. set_process_input(is_visible_in_tree());
  112. } break;
  113. case NOTIFICATION_EXIT_TREE: {
  114. if (is_pressed())
  115. _release(true);
  116. } break;
  117. case NOTIFICATION_VISIBILITY_CHANGED: {
  118. if (Engine::get_singleton()->is_editor_hint())
  119. break;
  120. if (is_visible_in_tree()) {
  121. set_process_input(true);
  122. } else {
  123. set_process_input(false);
  124. if (is_pressed())
  125. _release();
  126. }
  127. } break;
  128. case NOTIFICATION_PAUSED: {
  129. if (is_pressed())
  130. _release();
  131. } break;
  132. }
  133. }
  134. bool TouchScreenButton::is_pressed() const {
  135. return finger_pressed != -1;
  136. }
  137. void TouchScreenButton::set_action(const String &p_action) {
  138. action = p_action;
  139. }
  140. String TouchScreenButton::get_action() const {
  141. return action;
  142. }
  143. void TouchScreenButton::_input(const Ref<InputEvent> &p_event) {
  144. if (!get_tree())
  145. return;
  146. if (p_event->get_device() != 0)
  147. return;
  148. ERR_FAIL_COND(!is_visible_in_tree());
  149. const InputEventScreenTouch *st = Object::cast_to<InputEventScreenTouch>(*p_event);
  150. if (passby_press) {
  151. const InputEventScreenDrag *sd = Object::cast_to<InputEventScreenDrag>(*p_event);
  152. if (st && !st->is_pressed() && finger_pressed == st->get_index()) {
  153. _release();
  154. }
  155. if ((st && st->is_pressed()) || sd) {
  156. int index = st ? st->get_index() : sd->get_index();
  157. Point2 coord = st ? st->get_position() : sd->get_position();
  158. if (finger_pressed == -1 || index == finger_pressed) {
  159. if (_is_point_inside(coord)) {
  160. if (finger_pressed == -1) {
  161. _press(index);
  162. }
  163. } else {
  164. if (finger_pressed != -1) {
  165. _release();
  166. }
  167. }
  168. }
  169. }
  170. } else {
  171. if (st) {
  172. if (st->is_pressed()) {
  173. const bool can_press = finger_pressed == -1;
  174. if (!can_press)
  175. return; //already fingering
  176. if (_is_point_inside(st->get_position())) {
  177. _press(st->get_index());
  178. }
  179. } else {
  180. if (st->get_index() == finger_pressed) {
  181. _release();
  182. }
  183. }
  184. }
  185. }
  186. }
  187. bool TouchScreenButton::_is_point_inside(const Point2 &p_point) {
  188. Point2 coord = (get_global_transform_with_canvas()).affine_inverse().xform(p_point);
  189. Rect2 item_rect = get_item_rect();
  190. bool touched = false;
  191. bool check_rect = true;
  192. if (shape.is_valid()) {
  193. check_rect = false;
  194. Transform2D xform = shape_centered ? Transform2D().translated(item_rect.size * 0.5f) : Transform2D();
  195. touched = shape->collide(xform, unit_rect, Transform2D(0, coord + Vector2(0.5, 0.5)));
  196. }
  197. if (bitmask.is_valid()) {
  198. check_rect = false;
  199. if (!touched && Rect2(Point2(), bitmask->get_size()).has_point(coord)) {
  200. if (bitmask->get_bit(coord))
  201. touched = true;
  202. }
  203. }
  204. if (!touched && check_rect) {
  205. if (texture.is_valid())
  206. touched = item_rect.has_point(coord);
  207. }
  208. return touched;
  209. }
  210. void TouchScreenButton::_press(int p_finger_pressed) {
  211. finger_pressed = p_finger_pressed;
  212. if (action != StringName()) {
  213. Input::get_singleton()->action_press(action);
  214. Ref<InputEventAction> iea;
  215. iea.instance();
  216. iea->set_action(action);
  217. iea->set_pressed(true);
  218. get_tree()->input_event(iea);
  219. }
  220. emit_signal("pressed");
  221. update();
  222. }
  223. void TouchScreenButton::_release(bool p_exiting_tree) {
  224. finger_pressed = -1;
  225. if (action != StringName()) {
  226. Input::get_singleton()->action_release(action);
  227. if (!p_exiting_tree) {
  228. Ref<InputEventAction> iea;
  229. iea.instance();
  230. iea->set_action(action);
  231. iea->set_pressed(false);
  232. get_tree()->input_event(iea);
  233. }
  234. }
  235. if (!p_exiting_tree) {
  236. emit_signal("released");
  237. update();
  238. }
  239. }
  240. Rect2 TouchScreenButton::get_item_rect() const {
  241. if (texture.is_null())
  242. return Rect2(0, 0, 1, 1);
  243. /*
  244. if (texture.is_null())
  245. return CanvasItem::get_item_rect();
  246. */
  247. return Rect2(Size2(), texture->get_size());
  248. }
  249. void TouchScreenButton::set_visibility_mode(VisibilityMode p_mode) {
  250. visibility = p_mode;
  251. update();
  252. }
  253. TouchScreenButton::VisibilityMode TouchScreenButton::get_visibility_mode() const {
  254. return visibility;
  255. }
  256. void TouchScreenButton::set_passby_press(bool p_enable) {
  257. passby_press = p_enable;
  258. }
  259. bool TouchScreenButton::is_passby_press_enabled() const {
  260. return passby_press;
  261. }
  262. void TouchScreenButton::_bind_methods() {
  263. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TouchScreenButton::set_texture);
  264. ClassDB::bind_method(D_METHOD("get_texture"), &TouchScreenButton::get_texture);
  265. ClassDB::bind_method(D_METHOD("set_texture_pressed", "texture_pressed"), &TouchScreenButton::set_texture_pressed);
  266. ClassDB::bind_method(D_METHOD("get_texture_pressed"), &TouchScreenButton::get_texture_pressed);
  267. ClassDB::bind_method(D_METHOD("set_bitmask", "bitmask"), &TouchScreenButton::set_bitmask);
  268. ClassDB::bind_method(D_METHOD("get_bitmask"), &TouchScreenButton::get_bitmask);
  269. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &TouchScreenButton::set_shape);
  270. ClassDB::bind_method(D_METHOD("get_shape"), &TouchScreenButton::get_shape);
  271. ClassDB::bind_method(D_METHOD("set_shape_centered", "bool"), &TouchScreenButton::set_shape_centered);
  272. ClassDB::bind_method(D_METHOD("is_shape_centered"), &TouchScreenButton::is_shape_centered);
  273. ClassDB::bind_method(D_METHOD("set_shape_visible", "bool"), &TouchScreenButton::set_shape_visible);
  274. ClassDB::bind_method(D_METHOD("is_shape_visible"), &TouchScreenButton::is_shape_visible);
  275. ClassDB::bind_method(D_METHOD("set_action", "action"), &TouchScreenButton::set_action);
  276. ClassDB::bind_method(D_METHOD("get_action"), &TouchScreenButton::get_action);
  277. ClassDB::bind_method(D_METHOD("set_visibility_mode", "mode"), &TouchScreenButton::set_visibility_mode);
  278. ClassDB::bind_method(D_METHOD("get_visibility_mode"), &TouchScreenButton::get_visibility_mode);
  279. ClassDB::bind_method(D_METHOD("set_passby_press", "enabled"), &TouchScreenButton::set_passby_press);
  280. ClassDB::bind_method(D_METHOD("is_passby_press_enabled"), &TouchScreenButton::is_passby_press_enabled);
  281. ClassDB::bind_method(D_METHOD("is_pressed"), &TouchScreenButton::is_pressed);
  282. ClassDB::bind_method(D_METHOD("_input"), &TouchScreenButton::_input);
  283. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  284. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture_pressed", "get_texture_pressed");
  285. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "bitmask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_bitmask", "get_bitmask");
  286. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
  287. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_centered"), "set_shape_centered", "is_shape_centered");
  288. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shape_visible"), "set_shape_visible", "is_shape_visible");
  289. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "passby_press"), "set_passby_press", "is_passby_press_enabled");
  290. ADD_PROPERTY(PropertyInfo(Variant::STRING, "action"), "set_action", "get_action");
  291. ADD_PROPERTY(PropertyInfo(Variant::INT, "visibility_mode", PROPERTY_HINT_ENUM, "Always,TouchScreen Only"), "set_visibility_mode", "get_visibility_mode");
  292. ADD_SIGNAL(MethodInfo("pressed"));
  293. ADD_SIGNAL(MethodInfo("released"));
  294. BIND_ENUM_CONSTANT(VISIBILITY_ALWAYS);
  295. BIND_ENUM_CONSTANT(VISIBILITY_TOUCHSCREEN_ONLY);
  296. }
  297. TouchScreenButton::TouchScreenButton() {
  298. finger_pressed = -1;
  299. passby_press = false;
  300. visibility = VISIBILITY_ALWAYS;
  301. shape_centered = true;
  302. shape_visible = true;
  303. unit_rect = Ref<RectangleShape2D>(memnew(RectangleShape2D));
  304. unit_rect->set_extents(Vector2(0.5, 0.5));
  305. }