visibility_notifier_2d.cpp 11 KB

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