instance_placeholder.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "instance_placeholder.h"
  2. #include "scene/resources/packed_scene.h"
  3. #include "io/resource_loader.h"
  4. bool InstancePlaceholder::_set(const StringName& p_name, const Variant& p_value) {
  5. PropSet ps;
  6. ps.name=p_name;
  7. ps.value=p_value;
  8. stored_values.push_back(ps);
  9. return true;
  10. }
  11. bool InstancePlaceholder::_get(const StringName& p_name,Variant &r_ret) const{
  12. for (const List<PropSet>::Element *E=stored_values.front();E;E=E->next()) {
  13. if (E->get().name==p_name) {
  14. r_ret=E->get().value;
  15. return true;
  16. }
  17. }
  18. return false;
  19. }
  20. void InstancePlaceholder::_get_property_list( List<PropertyInfo> *p_list) const{
  21. for (const List<PropSet>::Element *E=stored_values.front();E;E=E->next()) {
  22. PropertyInfo pi;
  23. pi.name=E->get().name;
  24. pi.type=E->get().value.get_type();
  25. pi.usage=PROPERTY_USAGE_STORAGE;
  26. p_list->push_back(pi);
  27. }
  28. }
  29. void InstancePlaceholder::set_instance_path(const String& p_name) {
  30. path=p_name;
  31. }
  32. String InstancePlaceholder::get_instance_path() const {
  33. return path;
  34. }
  35. void InstancePlaceholder::replace_by_instance(const Ref<PackedScene> &p_custom_scene){
  36. ERR_FAIL_COND(!is_inside_tree());
  37. Node *base = get_parent();
  38. if (!base)
  39. return;
  40. Ref<PackedScene> ps;
  41. if (p_custom_scene.is_valid())
  42. ps = p_custom_scene;
  43. else
  44. ps = ResourceLoader::load(path,"PackedScene");
  45. if (!ps.is_valid())
  46. return;
  47. Node *scene = ps->instance();
  48. scene->set_name(get_name());
  49. int pos = get_position_in_parent();
  50. for(List<PropSet>::Element *E=stored_values.front();E;E=E->next()) {
  51. scene->set(E->get().name,E->get().value);
  52. }
  53. queue_delete();
  54. base->remove_child(this);
  55. base->add_child(scene);
  56. base->move_child(scene,pos);
  57. }
  58. void InstancePlaceholder::_bind_methods() {
  59. ObjectTypeDB::bind_method(_MD("replace_by_instance","custom_scene:PackedScene"),&InstancePlaceholder::replace_by_instance,DEFVAL(Variant()));
  60. ObjectTypeDB::bind_method(_MD("get_instance_path"),&InstancePlaceholder::get_instance_path);
  61. }
  62. InstancePlaceholder::InstancePlaceholder() {
  63. }