instance_placeholder.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*************************************************************************/
  2. /* instance_placeholder.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 "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 List<PropSet>::Element *E = stored_values.front(); E; E = E->next()) {
  42. if (E->get().name == p_name) {
  43. r_ret = E->get().value;
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. void InstancePlaceholder::_get_property_list(List<PropertyInfo> *p_list) const {
  50. for (const List<PropSet>::Element *E = stored_values.front(); E; E = E->next()) {
  51. PropertyInfo pi;
  52. pi.name = E->get().name;
  53. pi.type = E->get().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(), NULL);
  66. Node *base = get_parent();
  67. if (!base)
  68. return NULL;
  69. Ref<PackedScene> ps;
  70. if (p_custom_scene.is_valid())
  71. ps = p_custom_scene;
  72. else
  73. ps = ResourceLoader::load(path, "PackedScene");
  74. if (!ps.is_valid())
  75. return NULL;
  76. Node *scene = ps->instance();
  77. scene->set_name(get_name());
  78. int pos = get_position_in_parent();
  79. for (List<PropSet>::Element *E = stored_values.front(); E; E = E->next()) {
  80. scene->set(E->get().name, E->get().value);
  81. }
  82. if (p_replace) {
  83. queue_delete();
  84. base->remove_child(this);
  85. }
  86. base->add_child(scene);
  87. base->move_child(scene, pos);
  88. return scene;
  89. }
  90. void InstancePlaceholder::replace_by_instance(const Ref<PackedScene> &p_custom_scene) {
  91. //Deprecated by
  92. create_instance(true, p_custom_scene);
  93. }
  94. Dictionary InstancePlaceholder::get_stored_values(bool p_with_order) {
  95. Dictionary ret;
  96. PoolStringArray order;
  97. for (List<PropSet>::Element *E = stored_values.front(); E; E = E->next()) {
  98. ret[E->get().name] = E->get().value;
  99. if (p_with_order)
  100. order.push_back(E->get().name);
  101. };
  102. if (p_with_order)
  103. ret[".order"] = order;
  104. return ret;
  105. };
  106. void InstancePlaceholder::_bind_methods() {
  107. ClassDB::bind_method(D_METHOD("get_stored_values", "with_order"), &InstancePlaceholder::get_stored_values, DEFVAL(false));
  108. ClassDB::bind_method(D_METHOD("create_instance", "replace", "custom_scene"), &InstancePlaceholder::create_instance, DEFVAL(false), DEFVAL(Variant()));
  109. ClassDB::bind_method(D_METHOD("replace_by_instance", "custom_scene"), &InstancePlaceholder::replace_by_instance, DEFVAL(Variant()));
  110. ClassDB::bind_method(D_METHOD("get_instance_path"), &InstancePlaceholder::get_instance_path);
  111. }
  112. InstancePlaceholder::InstancePlaceholder() {
  113. }