visible_on_screen_notifier_2d.cpp 7.4 KB

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