instance_placeholder.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*************************************************************************/
  2. /* instance_placeholder.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 "instance_placeholder.h"
  31. #include "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. void InstancePlaceholder::replace_by_instance(const Ref<PackedScene> &p_custom_scene) {
  65. ERR_FAIL_COND(!is_inside_tree());
  66. Node *base = get_parent();
  67. if (!base)
  68. return;
  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;
  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. queue_delete();
  83. base->remove_child(this);
  84. base->add_child(scene);
  85. base->move_child(scene, pos);
  86. }
  87. Dictionary InstancePlaceholder::get_stored_values(bool p_with_order) {
  88. Dictionary ret;
  89. PoolStringArray order;
  90. for (List<PropSet>::Element *E = stored_values.front(); E; E = E->next()) {
  91. ret[E->get().name] = E->get().value;
  92. if (p_with_order)
  93. order.push_back(E->get().name);
  94. };
  95. if (p_with_order)
  96. ret[".order"] = order;
  97. return ret;
  98. };
  99. void InstancePlaceholder::_bind_methods() {
  100. ClassDB::bind_method(D_METHOD("get_stored_values", "with_order"), &InstancePlaceholder::get_stored_values, DEFVAL(false));
  101. ClassDB::bind_method(D_METHOD("replace_by_instance", "custom_scene"), &InstancePlaceholder::replace_by_instance, DEFVAL(Variant()));
  102. ClassDB::bind_method(D_METHOD("get_instance_path"), &InstancePlaceholder::get_instance_path);
  103. }
  104. InstancePlaceholder::InstancePlaceholder() {
  105. }