visibility_notifier.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*************************************************************************/
  2. /* visibility_notifier.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "visibility_notifier.h"
  31. #include "engine.h"
  32. #include "scene/3d/camera.h"
  33. #include "scene/3d/physics_body.h"
  34. #include "scene/animation/animation_player.h"
  35. #include "scene/scene_string_names.h"
  36. void VisibilityNotifier::_enter_camera(Camera *p_camera) {
  37. ERR_FAIL_COND(cameras.has(p_camera));
  38. cameras.insert(p_camera);
  39. if (cameras.size() == 1) {
  40. emit_signal(SceneStringNames::get_singleton()->screen_entered);
  41. _screen_enter();
  42. }
  43. emit_signal(SceneStringNames::get_singleton()->camera_entered, p_camera);
  44. }
  45. void VisibilityNotifier::_exit_camera(Camera *p_camera) {
  46. ERR_FAIL_COND(!cameras.has(p_camera));
  47. cameras.erase(p_camera);
  48. emit_signal(SceneStringNames::get_singleton()->camera_exited, p_camera);
  49. if (cameras.size() == 0) {
  50. emit_signal(SceneStringNames::get_singleton()->screen_exited);
  51. _screen_exit();
  52. }
  53. }
  54. void VisibilityNotifier::set_aabb(const Rect3 &p_aabb) {
  55. if (aabb == p_aabb)
  56. return;
  57. aabb = p_aabb;
  58. if (is_inside_world()) {
  59. get_world()->_update_notifier(this, get_global_transform().xform(aabb));
  60. }
  61. _change_notify("aabb");
  62. update_gizmo();
  63. }
  64. Rect3 VisibilityNotifier::get_aabb() const {
  65. return aabb;
  66. }
  67. void VisibilityNotifier::_notification(int p_what) {
  68. switch (p_what) {
  69. case NOTIFICATION_ENTER_WORLD: {
  70. get_world()->_register_notifier(this, get_global_transform().xform(aabb));
  71. } break;
  72. case NOTIFICATION_TRANSFORM_CHANGED: {
  73. get_world()->_update_notifier(this, get_global_transform().xform(aabb));
  74. } break;
  75. case NOTIFICATION_EXIT_WORLD: {
  76. get_world()->_remove_notifier(this);
  77. } break;
  78. }
  79. }
  80. bool VisibilityNotifier::is_on_screen() const {
  81. return cameras.size() != 0;
  82. }
  83. void VisibilityNotifier::_bind_methods() {
  84. ClassDB::bind_method(D_METHOD("set_aabb", "rect"), &VisibilityNotifier::set_aabb);
  85. ClassDB::bind_method(D_METHOD("get_aabb"), &VisibilityNotifier::get_aabb);
  86. ClassDB::bind_method(D_METHOD("is_on_screen"), &VisibilityNotifier::is_on_screen);
  87. ADD_PROPERTY(PropertyInfo(Variant::RECT3, "aabb"), "set_aabb", "get_aabb");
  88. ADD_SIGNAL(MethodInfo("camera_entered", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera")));
  89. ADD_SIGNAL(MethodInfo("camera_exited", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera")));
  90. ADD_SIGNAL(MethodInfo("screen_entered"));
  91. ADD_SIGNAL(MethodInfo("screen_exited"));
  92. }
  93. VisibilityNotifier::VisibilityNotifier() {
  94. aabb = Rect3(Vector3(-1, -1, -1), Vector3(2, 2, 2));
  95. set_notify_transform(true);
  96. }
  97. //////////////////////////////////////
  98. void VisibilityEnabler::_screen_enter() {
  99. for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {
  100. _change_node_state(E->key(), true);
  101. }
  102. visible = true;
  103. }
  104. void VisibilityEnabler::_screen_exit() {
  105. for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {
  106. _change_node_state(E->key(), false);
  107. }
  108. visible = false;
  109. }
  110. void VisibilityEnabler::_find_nodes(Node *p_node) {
  111. bool add = false;
  112. Variant meta;
  113. if (enabler[ENABLER_FREEZE_BODIES]) {
  114. RigidBody *rb = Object::cast_to<RigidBody>(p_node);
  115. if (rb && ((rb->get_mode() == RigidBody::MODE_CHARACTER || (rb->get_mode() == RigidBody::MODE_RIGID && !rb->is_able_to_sleep())))) {
  116. add = true;
  117. meta = rb->get_mode();
  118. }
  119. }
  120. if (enabler[ENABLER_PAUSE_ANIMATIONS]) {
  121. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);
  122. if (ap) {
  123. add = true;
  124. }
  125. }
  126. if (add) {
  127. p_node->connect(SceneStringNames::get_singleton()->tree_exited, this, "_node_removed", varray(p_node), CONNECT_ONESHOT);
  128. nodes[p_node] = meta;
  129. _change_node_state(p_node, false);
  130. }
  131. for (int i = 0; i < p_node->get_child_count(); i++) {
  132. Node *c = p_node->get_child(i);
  133. if (c->get_filename() != String())
  134. continue; //skip, instance
  135. _find_nodes(c);
  136. }
  137. }
  138. void VisibilityEnabler::_notification(int p_what) {
  139. if (p_what == NOTIFICATION_ENTER_TREE) {
  140. if (Engine::get_singleton()->is_editor_hint())
  141. return;
  142. Node *from = this;
  143. //find where current scene starts
  144. while (from->get_parent() && from->get_filename() == String())
  145. from = from->get_parent();
  146. _find_nodes(from);
  147. }
  148. if (p_what == NOTIFICATION_EXIT_TREE) {
  149. if (Engine::get_singleton()->is_editor_hint())
  150. return;
  151. for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {
  152. if (!visible)
  153. _change_node_state(E->key(), true);
  154. E->key()->disconnect(SceneStringNames::get_singleton()->tree_exited, this, "_node_removed");
  155. }
  156. nodes.clear();
  157. }
  158. }
  159. void VisibilityEnabler::_change_node_state(Node *p_node, bool p_enabled) {
  160. ERR_FAIL_COND(!nodes.has(p_node));
  161. {
  162. RigidBody *rb = Object::cast_to<RigidBody>(p_node);
  163. if (rb)
  164. rb->set_sleeping(!p_enabled);
  165. }
  166. {
  167. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);
  168. if (ap) {
  169. ap->set_active(p_enabled);
  170. }
  171. }
  172. }
  173. void VisibilityEnabler::_node_removed(Node *p_node) {
  174. if (!visible)
  175. _change_node_state(p_node, true);
  176. p_node->disconnect(SceneStringNames::get_singleton()->tree_exited, this, "_node_removed");
  177. nodes.erase(p_node);
  178. }
  179. void VisibilityEnabler::_bind_methods() {
  180. ClassDB::bind_method(D_METHOD("set_enabler", "enabler", "enabled"), &VisibilityEnabler::set_enabler);
  181. ClassDB::bind_method(D_METHOD("is_enabler_enabled", "enabler"), &VisibilityEnabler::is_enabler_enabled);
  182. ClassDB::bind_method(D_METHOD("_node_removed"), &VisibilityEnabler::_node_removed);
  183. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "pause_animations"), "set_enabler", "is_enabler_enabled", ENABLER_PAUSE_ANIMATIONS);
  184. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "freeze_bodies"), "set_enabler", "is_enabler_enabled", ENABLER_FREEZE_BODIES);
  185. BIND_ENUM_CONSTANT(ENABLER_PAUSE_ANIMATIONS);
  186. BIND_ENUM_CONSTANT(ENABLER_FREEZE_BODIES);
  187. BIND_ENUM_CONSTANT(ENABLER_MAX);
  188. }
  189. void VisibilityEnabler::set_enabler(Enabler p_enabler, bool p_enable) {
  190. ERR_FAIL_INDEX(p_enabler, ENABLER_MAX);
  191. enabler[p_enabler] = p_enable;
  192. }
  193. bool VisibilityEnabler::is_enabler_enabled(Enabler p_enabler) const {
  194. ERR_FAIL_INDEX_V(p_enabler, ENABLER_MAX, false);
  195. return enabler[p_enabler];
  196. }
  197. VisibilityEnabler::VisibilityEnabler() {
  198. for (int i = 0; i < ENABLER_MAX; i++)
  199. enabler[i] = true;
  200. visible = false;
  201. }