visible_on_screen_notifier_2d.cpp 7.6 KB

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