visibility_notifier_2d.cpp 12 KB

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