visible_on_screen_notifier_3d.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*************************************************************************/
  2. /* visible_on_screen_notifier_3d.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_3d.h"
  31. #include "scene/scene_string_names.h"
  32. void VisibleOnScreenNotifier3D::_visibility_enter() {
  33. if (!is_inside_tree() || Engine::get_singleton()->is_editor_hint()) {
  34. return;
  35. }
  36. on_screen = true;
  37. emit_signal(SceneStringNames::get_singleton()->screen_entered);
  38. _screen_enter();
  39. }
  40. void VisibleOnScreenNotifier3D::_visibility_exit() {
  41. if (!is_inside_tree() || Engine::get_singleton()->is_editor_hint()) {
  42. return;
  43. }
  44. on_screen = false;
  45. emit_signal(SceneStringNames::get_singleton()->screen_exited);
  46. _screen_exit();
  47. }
  48. void VisibleOnScreenNotifier3D::set_aabb(const AABB &p_aabb) {
  49. if (aabb == p_aabb) {
  50. return;
  51. }
  52. aabb = p_aabb;
  53. RS::get_singleton()->visibility_notifier_set_aabb(get_base(), aabb);
  54. update_gizmos();
  55. }
  56. AABB VisibleOnScreenNotifier3D::get_aabb() const {
  57. return aabb;
  58. }
  59. bool VisibleOnScreenNotifier3D::is_on_screen() const {
  60. return on_screen;
  61. }
  62. void VisibleOnScreenNotifier3D::_notification(int p_what) {
  63. switch (p_what) {
  64. case NOTIFICATION_ENTER_TREE:
  65. case NOTIFICATION_EXIT_TREE: {
  66. on_screen = false;
  67. } break;
  68. }
  69. }
  70. void VisibleOnScreenNotifier3D::_bind_methods() {
  71. ClassDB::bind_method(D_METHOD("set_aabb", "rect"), &VisibleOnScreenNotifier3D::set_aabb);
  72. ClassDB::bind_method(D_METHOD("is_on_screen"), &VisibleOnScreenNotifier3D::is_on_screen);
  73. ADD_PROPERTY(PropertyInfo(Variant::AABB, "aabb", PROPERTY_HINT_NONE, "suffix:m"), "set_aabb", "get_aabb");
  74. ADD_SIGNAL(MethodInfo("screen_entered"));
  75. ADD_SIGNAL(MethodInfo("screen_exited"));
  76. }
  77. VisibleOnScreenNotifier3D::VisibleOnScreenNotifier3D() {
  78. RID notifier = RS::get_singleton()->visibility_notifier_create();
  79. RS::get_singleton()->visibility_notifier_set_aabb(notifier, aabb);
  80. RS::get_singleton()->visibility_notifier_set_callbacks(notifier, callable_mp(this, &VisibleOnScreenNotifier3D::_visibility_enter), callable_mp(this, &VisibleOnScreenNotifier3D::_visibility_exit));
  81. set_base(notifier);
  82. }
  83. VisibleOnScreenNotifier3D::~VisibleOnScreenNotifier3D() {
  84. RID base = get_base();
  85. set_base(RID());
  86. RS::get_singleton()->free(base);
  87. }
  88. //////////////////////////////////////
  89. void VisibleOnScreenEnabler3D::_screen_enter() {
  90. _update_enable_mode(true);
  91. }
  92. void VisibleOnScreenEnabler3D::_screen_exit() {
  93. _update_enable_mode(false);
  94. }
  95. void VisibleOnScreenEnabler3D::set_enable_mode(EnableMode p_mode) {
  96. enable_mode = p_mode;
  97. if (is_inside_tree()) {
  98. _update_enable_mode(is_on_screen());
  99. }
  100. }
  101. VisibleOnScreenEnabler3D::EnableMode VisibleOnScreenEnabler3D::get_enable_mode() {
  102. return enable_mode;
  103. }
  104. void VisibleOnScreenEnabler3D::set_enable_node_path(NodePath p_path) {
  105. if (enable_node_path == p_path) {
  106. return;
  107. }
  108. enable_node_path = p_path;
  109. if (is_inside_tree()) {
  110. node_id = ObjectID();
  111. Node *node = get_node(enable_node_path);
  112. if (node) {
  113. node_id = node->get_instance_id();
  114. _update_enable_mode(is_on_screen());
  115. }
  116. }
  117. }
  118. NodePath VisibleOnScreenEnabler3D::get_enable_node_path() {
  119. return enable_node_path;
  120. }
  121. void VisibleOnScreenEnabler3D::_update_enable_mode(bool p_enable) {
  122. Node *node = static_cast<Node *>(ObjectDB::get_instance(node_id));
  123. if (node) {
  124. if (p_enable) {
  125. switch (enable_mode) {
  126. case ENABLE_MODE_INHERIT: {
  127. node->set_process_mode(PROCESS_MODE_INHERIT);
  128. } break;
  129. case ENABLE_MODE_ALWAYS: {
  130. node->set_process_mode(PROCESS_MODE_ALWAYS);
  131. } break;
  132. case ENABLE_MODE_WHEN_PAUSED: {
  133. node->set_process_mode(PROCESS_MODE_WHEN_PAUSED);
  134. } break;
  135. }
  136. } else {
  137. node->set_process_mode(PROCESS_MODE_DISABLED);
  138. }
  139. }
  140. }
  141. void VisibleOnScreenEnabler3D::_notification(int p_what) {
  142. switch (p_what) {
  143. case NOTIFICATION_ENTER_TREE: {
  144. if (Engine::get_singleton()->is_editor_hint()) {
  145. return;
  146. }
  147. node_id = ObjectID();
  148. Node *node = get_node(enable_node_path);
  149. if (node) {
  150. node_id = node->get_instance_id();
  151. node->set_process_mode(PROCESS_MODE_DISABLED);
  152. }
  153. } break;
  154. case NOTIFICATION_EXIT_TREE: {
  155. node_id = ObjectID();
  156. } break;
  157. }
  158. }
  159. void VisibleOnScreenEnabler3D::_bind_methods() {
  160. ClassDB::bind_method(D_METHOD("set_enable_mode", "mode"), &VisibleOnScreenEnabler3D::set_enable_mode);
  161. ClassDB::bind_method(D_METHOD("get_enable_mode"), &VisibleOnScreenEnabler3D::get_enable_mode);
  162. ClassDB::bind_method(D_METHOD("set_enable_node_path", "path"), &VisibleOnScreenEnabler3D::set_enable_node_path);
  163. ClassDB::bind_method(D_METHOD("get_enable_node_path"), &VisibleOnScreenEnabler3D::get_enable_node_path);
  164. ADD_GROUP("Enabling", "enable_");
  165. ADD_PROPERTY(PropertyInfo(Variant::INT, "enable_mode", PROPERTY_HINT_ENUM, "Inherit,Always,WhenPaused"), "set_enable_mode", "get_enable_mode");
  166. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "enable_node_path"), "set_enable_node_path", "get_enable_node_path");
  167. BIND_ENUM_CONSTANT(ENABLE_MODE_INHERIT);
  168. BIND_ENUM_CONSTANT(ENABLE_MODE_ALWAYS);
  169. BIND_ENUM_CONSTANT(ENABLE_MODE_WHEN_PAUSED);
  170. }
  171. VisibleOnScreenEnabler3D::VisibleOnScreenEnabler3D() {
  172. }