scenario_fx.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*************************************************************************/
  2. /* scenario_fx.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 "scenario_fx.h"
  31. #include "scene/main/viewport.h"
  32. void WorldEnvironment::_notification(int p_what) {
  33. if (p_what == Spatial::NOTIFICATION_ENTER_WORLD || p_what == Spatial::NOTIFICATION_ENTER_TREE) {
  34. if (environment.is_valid()) {
  35. if (get_viewport()->find_world()->get_environment().is_valid()) {
  36. WARN_PRINT("World already has an environment (Another WorldEnvironment?), overriding.");
  37. }
  38. get_viewport()->find_world()->set_environment(environment);
  39. add_to_group("_world_environment_" + itos(get_viewport()->find_world()->get_scenario().get_id()));
  40. }
  41. } else if (p_what == Spatial::NOTIFICATION_EXIT_WORLD || p_what == Spatial::NOTIFICATION_EXIT_TREE) {
  42. if (environment.is_valid() && get_viewport()->find_world()->get_environment() == environment) {
  43. get_viewport()->find_world()->set_environment(Ref<Environment>());
  44. remove_from_group("_world_environment_" + itos(get_viewport()->find_world()->get_scenario().get_id()));
  45. }
  46. }
  47. }
  48. void WorldEnvironment::set_environment(const Ref<Environment> &p_environment) {
  49. if (is_inside_tree() && environment.is_valid() && get_viewport()->find_world()->get_environment() == environment) {
  50. get_viewport()->find_world()->set_environment(Ref<Environment>());
  51. remove_from_group("_world_environment_" + itos(get_viewport()->find_world()->get_scenario().get_id()));
  52. //clean up
  53. }
  54. environment = p_environment;
  55. if (is_inside_tree() && environment.is_valid()) {
  56. if (get_viewport()->find_world()->get_environment().is_valid()) {
  57. WARN_PRINT("World already has an environment (Another WorldEnvironment?), overriding.");
  58. }
  59. get_viewport()->find_world()->set_environment(environment);
  60. add_to_group("_world_environment_" + itos(get_viewport()->find_world()->get_scenario().get_id()));
  61. }
  62. update_configuration_warning();
  63. }
  64. Ref<Environment> WorldEnvironment::get_environment() const {
  65. return environment;
  66. }
  67. String WorldEnvironment::get_configuration_warning() const {
  68. if (!environment.is_valid()) {
  69. return TTR("WorldEnvironment needs an Environment resource.");
  70. }
  71. if (/*!is_visible_in_tree() ||*/ !is_inside_tree())
  72. return String();
  73. List<Node *> nodes;
  74. get_tree()->get_nodes_in_group("_world_environment_" + itos(get_viewport()->find_world()->get_scenario().get_id()), &nodes);
  75. if (nodes.size() > 1) {
  76. return TTR("Only one WorldEnvironment is allowed per scene (or set of instanced scenes).");
  77. }
  78. // Commenting this warning for now, I think it makes no sense. If anyone can figure out what its supposed to do, feedback welcome. Else it should be deprecated.
  79. //if (environment.is_valid() && get_viewport() && !get_viewport()->get_camera() && environment->get_background() != Environment::BG_CANVAS) {
  80. // return TTR("This WorldEnvironment is ignored. Either add a Camera (for 3D scenes) or set this environment's Background Mode to Canvas (for 2D scenes).");
  81. //}
  82. return String();
  83. }
  84. void WorldEnvironment::_bind_methods() {
  85. ClassDB::bind_method(D_METHOD("set_environment", "env"), &WorldEnvironment::set_environment);
  86. ClassDB::bind_method(D_METHOD("get_environment"), &WorldEnvironment::get_environment);
  87. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_environment", "get_environment");
  88. }
  89. WorldEnvironment::WorldEnvironment() {
  90. }