visibility_notifier_2d.cpp 11 KB

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