visibility_notifier_2d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*************************************************************************/
  2. /* visibility_notifier_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_2d.h"
  31. #include "core/engine.h"
  32. #include "particles_2d.h"
  33. #include "scene/2d/animated_sprite.h"
  34. #include "scene/2d/physics_body_2d.h"
  35. #include "scene/animation/animation_player.h"
  36. #include "scene/main/viewport.h"
  37. #include "scene/scene_string_names.h"
  38. #ifdef TOOLS_ENABLED
  39. Rect2 VisibilityNotifier2D::_edit_get_rect() const {
  40. return rect;
  41. }
  42. bool VisibilityNotifier2D::_edit_use_rect() const {
  43. return true;
  44. }
  45. #endif
  46. void VisibilityNotifier2D::_enter_viewport(Viewport *p_viewport) {
  47. ERR_FAIL_COND(viewports.has(p_viewport));
  48. viewports.insert(p_viewport);
  49. if (is_inside_tree() && Engine::get_singleton()->is_editor_hint())
  50. return;
  51. if (viewports.size() == 1) {
  52. emit_signal(SceneStringNames::get_singleton()->screen_entered);
  53. _screen_enter();
  54. }
  55. emit_signal(SceneStringNames::get_singleton()->viewport_entered, p_viewport);
  56. }
  57. void VisibilityNotifier2D::_exit_viewport(Viewport *p_viewport) {
  58. ERR_FAIL_COND(!viewports.has(p_viewport));
  59. viewports.erase(p_viewport);
  60. if (is_inside_tree() && Engine::get_singleton()->is_editor_hint())
  61. return;
  62. emit_signal(SceneStringNames::get_singleton()->viewport_exited, p_viewport);
  63. if (viewports.size() == 0) {
  64. emit_signal(SceneStringNames::get_singleton()->screen_exited);
  65. _screen_exit();
  66. }
  67. }
  68. void VisibilityNotifier2D::set_rect(const Rect2 &p_rect) {
  69. rect = p_rect;
  70. if (is_inside_tree()) {
  71. get_world_2d()->_update_notifier(this, get_global_transform().xform(rect));
  72. if (Engine::get_singleton()->is_editor_hint()) {
  73. update();
  74. item_rect_changed();
  75. }
  76. }
  77. _change_notify("rect");
  78. }
  79. Rect2 VisibilityNotifier2D::get_rect() const {
  80. return rect;
  81. }
  82. void VisibilityNotifier2D::_notification(int p_what) {
  83. switch (p_what) {
  84. case NOTIFICATION_ENTER_TREE: {
  85. //get_world_2d()->
  86. get_world_2d()->_register_notifier(this, get_global_transform().xform(rect));
  87. } break;
  88. case NOTIFICATION_TRANSFORM_CHANGED: {
  89. //get_world_2d()->
  90. get_world_2d()->_update_notifier(this, get_global_transform().xform(rect));
  91. } break;
  92. case NOTIFICATION_DRAW: {
  93. if (Engine::get_singleton()->is_editor_hint()) {
  94. draw_rect(rect, Color(1, 0.5, 1, 0.2));
  95. }
  96. } break;
  97. case NOTIFICATION_EXIT_TREE: {
  98. get_world_2d()->_remove_notifier(this);
  99. } break;
  100. }
  101. }
  102. bool VisibilityNotifier2D::is_on_screen() const {
  103. return viewports.size() > 0;
  104. }
  105. void VisibilityNotifier2D::_bind_methods() {
  106. ClassDB::bind_method(D_METHOD("set_rect", "rect"), &VisibilityNotifier2D::set_rect);
  107. ClassDB::bind_method(D_METHOD("get_rect"), &VisibilityNotifier2D::get_rect);
  108. ClassDB::bind_method(D_METHOD("is_on_screen"), &VisibilityNotifier2D::is_on_screen);
  109. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "rect"), "set_rect", "get_rect");
  110. ADD_SIGNAL(MethodInfo("viewport_entered", PropertyInfo(Variant::OBJECT, "viewport", PROPERTY_HINT_RESOURCE_TYPE, "Viewport")));
  111. ADD_SIGNAL(MethodInfo("viewport_exited", PropertyInfo(Variant::OBJECT, "viewport", PROPERTY_HINT_RESOURCE_TYPE, "Viewport")));
  112. ADD_SIGNAL(MethodInfo("screen_entered"));
  113. ADD_SIGNAL(MethodInfo("screen_exited"));
  114. }
  115. VisibilityNotifier2D::VisibilityNotifier2D() {
  116. rect = Rect2(-10, -10, 20, 20);
  117. set_notify_transform(true);
  118. }
  119. //////////////////////////////////////
  120. void VisibilityEnabler2D::_screen_enter() {
  121. for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {
  122. _change_node_state(E->key(), true);
  123. }
  124. if (enabler[ENABLER_PARENT_PHYSICS_PROCESS] && get_parent())
  125. get_parent()->set_physics_process(true);
  126. if (enabler[ENABLER_PARENT_PROCESS] && get_parent())
  127. get_parent()->set_process(true);
  128. visible = true;
  129. }
  130. void VisibilityEnabler2D::_screen_exit() {
  131. for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {
  132. _change_node_state(E->key(), false);
  133. }
  134. if (enabler[ENABLER_PARENT_PHYSICS_PROCESS] && get_parent())
  135. get_parent()->set_physics_process(false);
  136. if (enabler[ENABLER_PARENT_PROCESS] && get_parent())
  137. get_parent()->set_process(false);
  138. visible = false;
  139. }
  140. void VisibilityEnabler2D::_find_nodes(Node *p_node) {
  141. bool add = false;
  142. Variant meta;
  143. {
  144. RigidBody2D *rb2d = Object::cast_to<RigidBody2D>(p_node);
  145. if (rb2d && ((rb2d->get_mode() == RigidBody2D::MODE_CHARACTER || rb2d->get_mode() == RigidBody2D::MODE_RIGID))) {
  146. add = true;
  147. meta = rb2d->get_mode();
  148. }
  149. }
  150. {
  151. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);
  152. if (ap) {
  153. add = true;
  154. }
  155. }
  156. {
  157. AnimatedSprite *as = Object::cast_to<AnimatedSprite>(p_node);
  158. if (as) {
  159. add = true;
  160. }
  161. }
  162. {
  163. Particles2D *ps = Object::cast_to<Particles2D>(p_node);
  164. if (ps) {
  165. add = true;
  166. }
  167. }
  168. if (add) {
  169. p_node->connect(SceneStringNames::get_singleton()->tree_exiting, this, "_node_removed", varray(p_node), CONNECT_ONESHOT);
  170. nodes[p_node] = meta;
  171. _change_node_state(p_node, false);
  172. }
  173. for (int i = 0; i < p_node->get_child_count(); i++) {
  174. Node *c = p_node->get_child(i);
  175. if (c->get_filename() != String())
  176. continue; //skip, instance
  177. _find_nodes(c);
  178. }
  179. }
  180. void VisibilityEnabler2D::_notification(int p_what) {
  181. if (p_what == NOTIFICATION_ENTER_TREE) {
  182. if (Engine::get_singleton()->is_editor_hint())
  183. return;
  184. Node *from = this;
  185. //find where current scene starts
  186. while (from->get_parent() && from->get_filename() == String())
  187. from = from->get_parent();
  188. _find_nodes(from);
  189. // We need to defer the call of set_process and set_physics_process,
  190. // otherwise they are overwritten inside NOTIFICATION_READY.
  191. // We can't use call_deferred, because it happens after a physics frame.
  192. // The ready signal works as it's emitted immediately after NOTIFICATION_READY.
  193. if (enabler[ENABLER_PARENT_PHYSICS_PROCESS] && get_parent()) {
  194. get_parent()->connect(SceneStringNames::get_singleton()->ready,
  195. get_parent(), "set_physics_process", varray(false), CONNECT_ONESHOT);
  196. }
  197. if (enabler[ENABLER_PARENT_PROCESS] && get_parent()) {
  198. get_parent()->connect(SceneStringNames::get_singleton()->ready,
  199. get_parent(), "set_process", varray(false), CONNECT_ONESHOT);
  200. }
  201. }
  202. if (p_what == NOTIFICATION_EXIT_TREE) {
  203. if (Engine::get_singleton()->is_editor_hint())
  204. return;
  205. for (Map<Node *, Variant>::Element *E = nodes.front(); E; E = E->next()) {
  206. if (!visible)
  207. _change_node_state(E->key(), true);
  208. E->key()->disconnect(SceneStringNames::get_singleton()->tree_exiting, this, "_node_removed");
  209. }
  210. nodes.clear();
  211. }
  212. }
  213. void VisibilityEnabler2D::_change_node_state(Node *p_node, bool p_enabled) {
  214. ERR_FAIL_COND(!nodes.has(p_node));
  215. if (enabler[ENABLER_FREEZE_BODIES]) {
  216. RigidBody2D *rb = Object::cast_to<RigidBody2D>(p_node);
  217. if (rb) {
  218. rb->set_sleeping(!p_enabled);
  219. }
  220. }
  221. if (enabler[ENABLER_PAUSE_ANIMATIONS]) {
  222. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);
  223. if (ap) {
  224. ap->set_active(p_enabled);
  225. }
  226. }
  227. if (enabler[ENABLER_PAUSE_ANIMATED_SPRITES]) {
  228. AnimatedSprite *as = Object::cast_to<AnimatedSprite>(p_node);
  229. if (as) {
  230. if (p_enabled)
  231. as->play();
  232. else
  233. as->stop();
  234. }
  235. }
  236. if (enabler[ENABLER_PAUSE_PARTICLES]) {
  237. Particles2D *ps = Object::cast_to<Particles2D>(p_node);
  238. if (ps) {
  239. ps->set_emitting(p_enabled);
  240. }
  241. }
  242. }
  243. void VisibilityEnabler2D::_node_removed(Node *p_node) {
  244. if (!visible)
  245. _change_node_state(p_node, true);
  246. nodes.erase(p_node);
  247. }
  248. String VisibilityEnabler2D::get_configuration_warning() const {
  249. String warning = VisibilityNotifier2D::get_configuration_warning();
  250. #ifdef TOOLS_ENABLED
  251. if (is_inside_tree() && get_parent() && (get_parent()->get_filename() == String() && get_parent() != get_tree()->get_edited_scene_root())) {
  252. if (warning != String()) {
  253. warning += "\n\n";
  254. }
  255. warning += TTR("VisibilityEnabler2D works best when used with the edited scene root directly as parent.");
  256. }
  257. #endif
  258. return warning;
  259. }
  260. void VisibilityEnabler2D::_bind_methods() {
  261. ClassDB::bind_method(D_METHOD("set_enabler", "enabler", "enabled"), &VisibilityEnabler2D::set_enabler);
  262. ClassDB::bind_method(D_METHOD("is_enabler_enabled", "enabler"), &VisibilityEnabler2D::is_enabler_enabled);
  263. ClassDB::bind_method(D_METHOD("_node_removed"), &VisibilityEnabler2D::_node_removed);
  264. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "pause_animations"), "set_enabler", "is_enabler_enabled", ENABLER_PAUSE_ANIMATIONS);
  265. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "freeze_bodies"), "set_enabler", "is_enabler_enabled", ENABLER_FREEZE_BODIES);
  266. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "pause_particles"), "set_enabler", "is_enabler_enabled", ENABLER_PAUSE_PARTICLES);
  267. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "pause_animated_sprites"), "set_enabler", "is_enabler_enabled", ENABLER_PAUSE_ANIMATED_SPRITES);
  268. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "process_parent"), "set_enabler", "is_enabler_enabled", ENABLER_PARENT_PROCESS);
  269. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "physics_process_parent"), "set_enabler", "is_enabler_enabled", ENABLER_PARENT_PHYSICS_PROCESS);
  270. BIND_ENUM_CONSTANT(ENABLER_PAUSE_ANIMATIONS);
  271. BIND_ENUM_CONSTANT(ENABLER_FREEZE_BODIES);
  272. BIND_ENUM_CONSTANT(ENABLER_PAUSE_PARTICLES);
  273. BIND_ENUM_CONSTANT(ENABLER_PARENT_PROCESS);
  274. BIND_ENUM_CONSTANT(ENABLER_PARENT_PHYSICS_PROCESS);
  275. BIND_ENUM_CONSTANT(ENABLER_PAUSE_ANIMATED_SPRITES);
  276. BIND_ENUM_CONSTANT(ENABLER_MAX);
  277. }
  278. void VisibilityEnabler2D::set_enabler(Enabler p_enabler, bool p_enable) {
  279. ERR_FAIL_INDEX(p_enabler, ENABLER_MAX);
  280. enabler[p_enabler] = p_enable;
  281. }
  282. bool VisibilityEnabler2D::is_enabler_enabled(Enabler p_enabler) const {
  283. ERR_FAIL_INDEX_V(p_enabler, ENABLER_MAX, false);
  284. return enabler[p_enabler];
  285. }
  286. VisibilityEnabler2D::VisibilityEnabler2D() {
  287. for (int i = 0; i < ENABLER_MAX; i++)
  288. enabler[i] = true;
  289. enabler[ENABLER_PARENT_PROCESS] = false;
  290. enabler[ENABLER_PARENT_PHYSICS_PROCESS] = false;
  291. visible = false;
  292. }