visible_on_screen_notifier_2d.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**************************************************************************/
  2. /* visible_on_screen_notifier_2d.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 "visible_on_screen_notifier_2d.h"
  31. #ifdef TOOLS_ENABLED
  32. Dictionary VisibleOnScreenNotifier2D::_edit_get_state() const {
  33. Dictionary state = Node2D::_edit_get_state();
  34. state["rect"] = rect;
  35. return state;
  36. }
  37. void VisibleOnScreenNotifier2D::_edit_set_state(const Dictionary &p_state) {
  38. ERR_FAIL_COND(p_state.is_empty() || !p_state.has("rect"));
  39. set_rect(p_state["rect"]);
  40. Node2D::_edit_set_state(p_state);
  41. }
  42. void VisibleOnScreenNotifier2D::_edit_set_rect(const Rect2 &p_edit_rect) {
  43. set_rect(p_edit_rect);
  44. }
  45. #endif // TOOLS_ENABLED
  46. #ifdef DEBUG_ENABLED
  47. Rect2 VisibleOnScreenNotifier2D::_edit_get_rect() const {
  48. return rect;
  49. }
  50. bool VisibleOnScreenNotifier2D::_edit_use_rect() const {
  51. return show_rect;
  52. }
  53. #endif // DEBUG_ENABLED
  54. void VisibleOnScreenNotifier2D::_visibility_enter() {
  55. if (!is_inside_tree() || Engine::get_singleton()->is_editor_hint()) {
  56. return;
  57. }
  58. on_screen = true;
  59. emit_signal(SceneStringName(screen_entered));
  60. _screen_enter();
  61. }
  62. void VisibleOnScreenNotifier2D::_visibility_exit() {
  63. if (!is_inside_tree() || Engine::get_singleton()->is_editor_hint()) {
  64. return;
  65. }
  66. on_screen = false;
  67. emit_signal(SceneStringName(screen_exited));
  68. _screen_exit();
  69. }
  70. void VisibleOnScreenNotifier2D::set_rect(const Rect2 &p_rect) {
  71. rect = p_rect;
  72. if (is_inside_tree()) {
  73. RS::get_singleton()->canvas_item_set_visibility_notifier(get_canvas_item(), true, rect, callable_mp(this, &VisibleOnScreenNotifier2D::_visibility_enter), callable_mp(this, &VisibleOnScreenNotifier2D::_visibility_exit));
  74. }
  75. queue_redraw();
  76. }
  77. Rect2 VisibleOnScreenNotifier2D::get_rect() const {
  78. return rect;
  79. }
  80. void VisibleOnScreenNotifier2D::set_show_rect(bool p_show_rect) {
  81. if (show_rect == p_show_rect) {
  82. return;
  83. }
  84. show_rect = p_show_rect;
  85. queue_redraw();
  86. }
  87. bool VisibleOnScreenNotifier2D::is_showing_rect() const {
  88. return show_rect;
  89. }
  90. void VisibleOnScreenNotifier2D::_notification(int p_what) {
  91. switch (p_what) {
  92. case NOTIFICATION_ENTER_TREE: {
  93. on_screen = false;
  94. RS::get_singleton()->canvas_item_set_visibility_notifier(get_canvas_item(), true, rect, callable_mp(this, &VisibleOnScreenNotifier2D::_visibility_enter), callable_mp(this, &VisibleOnScreenNotifier2D::_visibility_exit));
  95. } break;
  96. case NOTIFICATION_DRAW: {
  97. if (show_rect && Engine::get_singleton()->is_editor_hint()) {
  98. draw_rect(rect, Color(1, 0.5, 1, 0.2));
  99. }
  100. } break;
  101. case NOTIFICATION_EXIT_TREE: {
  102. on_screen = false;
  103. RS::get_singleton()->canvas_item_set_visibility_notifier(get_canvas_item(), false, Rect2(), Callable(), Callable());
  104. } break;
  105. }
  106. }
  107. bool VisibleOnScreenNotifier2D::is_on_screen() const {
  108. return on_screen;
  109. }
  110. void VisibleOnScreenNotifier2D::_bind_methods() {
  111. ClassDB::bind_method(D_METHOD("set_rect", "rect"), &VisibleOnScreenNotifier2D::set_rect);
  112. ClassDB::bind_method(D_METHOD("get_rect"), &VisibleOnScreenNotifier2D::get_rect);
  113. ClassDB::bind_method(D_METHOD("set_show_rect", "show_rect"), &VisibleOnScreenNotifier2D::set_show_rect);
  114. ClassDB::bind_method(D_METHOD("is_showing_rect"), &VisibleOnScreenNotifier2D::is_showing_rect);
  115. ClassDB::bind_method(D_METHOD("is_on_screen"), &VisibleOnScreenNotifier2D::is_on_screen);
  116. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "rect", PROPERTY_HINT_NONE, "suffix:px"), "set_rect", "get_rect");
  117. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_rect"), "set_show_rect", "is_showing_rect");
  118. ADD_SIGNAL(MethodInfo("screen_entered"));
  119. ADD_SIGNAL(MethodInfo("screen_exited"));
  120. }
  121. VisibleOnScreenNotifier2D::VisibleOnScreenNotifier2D() {
  122. rect = Rect2(-10, -10, 20, 20);
  123. set_hide_clip_children(true);
  124. }
  125. //////////////////////////////////////
  126. void VisibleOnScreenEnabler2D::_screen_enter() {
  127. _update_enable_mode(true);
  128. }
  129. void VisibleOnScreenEnabler2D::_screen_exit() {
  130. _update_enable_mode(false);
  131. }
  132. void VisibleOnScreenEnabler2D::set_enable_mode(EnableMode p_mode) {
  133. enable_mode = p_mode;
  134. if (is_inside_tree()) {
  135. _update_enable_mode(is_on_screen());
  136. }
  137. }
  138. VisibleOnScreenEnabler2D::EnableMode VisibleOnScreenEnabler2D::get_enable_mode() {
  139. return enable_mode;
  140. }
  141. void VisibleOnScreenEnabler2D::set_enable_node_path(NodePath p_path) {
  142. if (enable_node_path == p_path) {
  143. return;
  144. }
  145. enable_node_path = p_path;
  146. if (enable_node_path.is_empty()) {
  147. node_id = ObjectID();
  148. return;
  149. }
  150. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
  151. node_id = ObjectID();
  152. Node *node = get_node(enable_node_path);
  153. if (node) {
  154. node_id = node->get_instance_id();
  155. _update_enable_mode(is_on_screen());
  156. }
  157. }
  158. }
  159. NodePath VisibleOnScreenEnabler2D::get_enable_node_path() {
  160. return enable_node_path;
  161. }
  162. void VisibleOnScreenEnabler2D::_update_enable_mode(bool p_enable) {
  163. Node *node = ObjectDB::get_instance<Node>(node_id);
  164. if (node) {
  165. if (p_enable) {
  166. switch (enable_mode) {
  167. case ENABLE_MODE_INHERIT: {
  168. node->set_process_mode(PROCESS_MODE_INHERIT);
  169. } break;
  170. case ENABLE_MODE_ALWAYS: {
  171. node->set_process_mode(PROCESS_MODE_ALWAYS);
  172. } break;
  173. case ENABLE_MODE_WHEN_PAUSED: {
  174. node->set_process_mode(PROCESS_MODE_WHEN_PAUSED);
  175. } break;
  176. }
  177. } else {
  178. node->set_process_mode(PROCESS_MODE_DISABLED);
  179. }
  180. }
  181. }
  182. void VisibleOnScreenEnabler2D::_notification(int p_what) {
  183. switch (p_what) {
  184. case NOTIFICATION_ENTER_TREE: {
  185. if (Engine::get_singleton()->is_editor_hint()) {
  186. return;
  187. }
  188. node_id = ObjectID();
  189. if (enable_node_path.is_empty()) {
  190. return;
  191. }
  192. Node *node = get_node(enable_node_path);
  193. if (node) {
  194. node_id = node->get_instance_id();
  195. node->set_process_mode(PROCESS_MODE_DISABLED);
  196. }
  197. } break;
  198. case NOTIFICATION_EXIT_TREE: {
  199. node_id = ObjectID();
  200. } break;
  201. }
  202. }
  203. void VisibleOnScreenEnabler2D::_bind_methods() {
  204. ClassDB::bind_method(D_METHOD("set_enable_mode", "mode"), &VisibleOnScreenEnabler2D::set_enable_mode);
  205. ClassDB::bind_method(D_METHOD("get_enable_mode"), &VisibleOnScreenEnabler2D::get_enable_mode);
  206. ClassDB::bind_method(D_METHOD("set_enable_node_path", "path"), &VisibleOnScreenEnabler2D::set_enable_node_path);
  207. ClassDB::bind_method(D_METHOD("get_enable_node_path"), &VisibleOnScreenEnabler2D::get_enable_node_path);
  208. ADD_GROUP("Enabling", "enable_");
  209. ADD_PROPERTY(PropertyInfo(Variant::INT, "enable_mode", PROPERTY_HINT_ENUM, "Inherit,Always,When Paused"), "set_enable_mode", "get_enable_mode");
  210. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "enable_node_path"), "set_enable_node_path", "get_enable_node_path");
  211. BIND_ENUM_CONSTANT(ENABLE_MODE_INHERIT);
  212. BIND_ENUM_CONSTANT(ENABLE_MODE_ALWAYS);
  213. BIND_ENUM_CONSTANT(ENABLE_MODE_WHEN_PAUSED);
  214. }
  215. VisibleOnScreenEnabler2D::VisibleOnScreenEnabler2D() {
  216. }