instance_placeholder.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**************************************************************************/
  2. /* instance_placeholder.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "instance_placeholder.h"
  31. #include "core/io/resource_loader.h"
  32. #include "scene/resources/packed_scene.h"
  33. bool InstancePlaceholder::_set(const StringName &p_name, const Variant &p_value) {
  34. PropSet ps;
  35. ps.name = p_name;
  36. ps.value = p_value;
  37. stored_values.push_back(ps);
  38. return true;
  39. }
  40. bool InstancePlaceholder::_get(const StringName &p_name, Variant &r_ret) const {
  41. for (const PropSet &E : stored_values) {
  42. if (E.name == p_name) {
  43. r_ret = E.value;
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. void InstancePlaceholder::_get_property_list(List<PropertyInfo> *p_list) const {
  50. for (const PropSet &E : stored_values) {
  51. PropertyInfo pi;
  52. pi.name = E.name;
  53. pi.type = E.value.get_type();
  54. pi.usage = PROPERTY_USAGE_STORAGE;
  55. p_list->push_back(pi);
  56. }
  57. }
  58. void InstancePlaceholder::set_instance_path(const String &p_name) {
  59. path = p_name;
  60. }
  61. String InstancePlaceholder::get_instance_path() const {
  62. return path;
  63. }
  64. Node *InstancePlaceholder::create_instance(bool p_replace, const Ref<PackedScene> &p_custom_scene) {
  65. ERR_FAIL_COND_V(!is_inside_tree(), nullptr);
  66. Node *base = get_parent();
  67. if (!base) {
  68. return nullptr;
  69. }
  70. Ref<PackedScene> ps;
  71. if (p_custom_scene.is_valid()) {
  72. ps = p_custom_scene;
  73. } else {
  74. ps = ResourceLoader::load(path, "PackedScene");
  75. }
  76. if (!ps.is_valid()) {
  77. return nullptr;
  78. }
  79. Node *scene = ps->instantiate();
  80. if (!scene) {
  81. return nullptr;
  82. }
  83. scene->set_name(get_name());
  84. scene->set_multiplayer_authority(get_multiplayer_authority());
  85. int pos = get_index();
  86. for (const PropSet &E : stored_values) {
  87. scene->set(E.name, E.value);
  88. }
  89. if (p_replace) {
  90. queue_free();
  91. base->remove_child(this);
  92. }
  93. base->add_child(scene);
  94. base->move_child(scene, pos);
  95. return scene;
  96. }
  97. Dictionary InstancePlaceholder::get_stored_values(bool p_with_order) {
  98. Dictionary ret;
  99. PackedStringArray order;
  100. for (const PropSet &E : stored_values) {
  101. ret[E.name] = E.value;
  102. if (p_with_order) {
  103. order.push_back(E.name);
  104. }
  105. };
  106. if (p_with_order) {
  107. ret[".order"] = order;
  108. }
  109. return ret;
  110. };
  111. void InstancePlaceholder::_bind_methods() {
  112. ClassDB::bind_method(D_METHOD("get_stored_values", "with_order"), &InstancePlaceholder::get_stored_values, DEFVAL(false));
  113. ClassDB::bind_method(D_METHOD("create_instance", "replace", "custom_scene"), &InstancePlaceholder::create_instance, DEFVAL(false), DEFVAL(Variant()));
  114. ClassDB::bind_method(D_METHOD("get_instance_path"), &InstancePlaceholder::get_instance_path);
  115. }
  116. InstancePlaceholder::InstancePlaceholder() {
  117. }