visible_on_screen_notifier_2d.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #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. set_hide_clip_children(true);
  97. }
  98. //////////////////////////////////////
  99. void VisibleOnScreenEnabler2D::_screen_enter() {
  100. _update_enable_mode(true);
  101. }
  102. void VisibleOnScreenEnabler2D::_screen_exit() {
  103. _update_enable_mode(false);
  104. }
  105. void VisibleOnScreenEnabler2D::set_enable_mode(EnableMode p_mode) {
  106. enable_mode = p_mode;
  107. if (is_inside_tree()) {
  108. _update_enable_mode(is_on_screen());
  109. }
  110. }
  111. VisibleOnScreenEnabler2D::EnableMode VisibleOnScreenEnabler2D::get_enable_mode() {
  112. return enable_mode;
  113. }
  114. void VisibleOnScreenEnabler2D::set_enable_node_path(NodePath p_path) {
  115. if (enable_node_path == p_path) {
  116. return;
  117. }
  118. enable_node_path = p_path;
  119. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
  120. node_id = ObjectID();
  121. Node *node = get_node(enable_node_path);
  122. if (node) {
  123. node_id = node->get_instance_id();
  124. _update_enable_mode(is_on_screen());
  125. }
  126. }
  127. }
  128. NodePath VisibleOnScreenEnabler2D::get_enable_node_path() {
  129. return enable_node_path;
  130. }
  131. void VisibleOnScreenEnabler2D::_update_enable_mode(bool p_enable) {
  132. Node *node = static_cast<Node *>(ObjectDB::get_instance(node_id));
  133. if (node) {
  134. if (p_enable) {
  135. switch (enable_mode) {
  136. case ENABLE_MODE_INHERIT: {
  137. node->set_process_mode(PROCESS_MODE_INHERIT);
  138. } break;
  139. case ENABLE_MODE_ALWAYS: {
  140. node->set_process_mode(PROCESS_MODE_ALWAYS);
  141. } break;
  142. case ENABLE_MODE_WHEN_PAUSED: {
  143. node->set_process_mode(PROCESS_MODE_WHEN_PAUSED);
  144. } break;
  145. }
  146. } else {
  147. node->set_process_mode(PROCESS_MODE_DISABLED);
  148. }
  149. }
  150. }
  151. void VisibleOnScreenEnabler2D::_notification(int p_what) {
  152. switch (p_what) {
  153. case NOTIFICATION_ENTER_TREE: {
  154. if (Engine::get_singleton()->is_editor_hint()) {
  155. return;
  156. }
  157. node_id = ObjectID();
  158. Node *node = get_node(enable_node_path);
  159. if (node) {
  160. node_id = node->get_instance_id();
  161. node->set_process_mode(PROCESS_MODE_DISABLED);
  162. }
  163. } break;
  164. case NOTIFICATION_EXIT_TREE: {
  165. node_id = ObjectID();
  166. } break;
  167. }
  168. }
  169. void VisibleOnScreenEnabler2D::_bind_methods() {
  170. ClassDB::bind_method(D_METHOD("set_enable_mode", "mode"), &VisibleOnScreenEnabler2D::set_enable_mode);
  171. ClassDB::bind_method(D_METHOD("get_enable_mode"), &VisibleOnScreenEnabler2D::get_enable_mode);
  172. ClassDB::bind_method(D_METHOD("set_enable_node_path", "path"), &VisibleOnScreenEnabler2D::set_enable_node_path);
  173. ClassDB::bind_method(D_METHOD("get_enable_node_path"), &VisibleOnScreenEnabler2D::get_enable_node_path);
  174. ADD_GROUP("Enabling", "enable_");
  175. ADD_PROPERTY(PropertyInfo(Variant::INT, "enable_mode", PROPERTY_HINT_ENUM, "Inherit,Always,When Paused"), "set_enable_mode", "get_enable_mode");
  176. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "enable_node_path"), "set_enable_node_path", "get_enable_node_path");
  177. BIND_ENUM_CONSTANT(ENABLE_MODE_INHERIT);
  178. BIND_ENUM_CONSTANT(ENABLE_MODE_ALWAYS);
  179. BIND_ENUM_CONSTANT(ENABLE_MODE_WHEN_PAUSED);
  180. }
  181. VisibleOnScreenEnabler2D::VisibleOnScreenEnabler2D() {
  182. }