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 | |
31 | #include "instance_placeholder.h" |
32 | |
33 | #include "core/io/resource_loader.h" |
34 | #include "scene/resources/packed_scene.h" |
35 | |
36 | bool InstancePlaceholder::_set(const StringName &p_name, const Variant &p_value) { |
37 | PropSet ps; |
38 | ps.name = p_name; |
39 | ps.value = p_value; |
40 | stored_values.push_back(ps); |
41 | return true; |
42 | } |
43 | |
44 | bool InstancePlaceholder::_get(const StringName &p_name, Variant &r_ret) const { |
45 | for (const PropSet &E : stored_values) { |
46 | if (E.name == p_name) { |
47 | r_ret = E.value; |
48 | return true; |
49 | } |
50 | } |
51 | return false; |
52 | } |
53 | |
54 | void InstancePlaceholder::_get_property_list(List<PropertyInfo> *p_list) const { |
55 | for (const PropSet &E : stored_values) { |
56 | PropertyInfo pi; |
57 | pi.name = E.name; |
58 | pi.type = E.value.get_type(); |
59 | pi.usage = PROPERTY_USAGE_STORAGE; |
60 | |
61 | p_list->push_back(pi); |
62 | } |
63 | } |
64 | |
65 | void InstancePlaceholder::set_instance_path(const String &p_name) { |
66 | path = p_name; |
67 | } |
68 | |
69 | String InstancePlaceholder::get_instance_path() const { |
70 | return path; |
71 | } |
72 | |
73 | Node *InstancePlaceholder::create_instance(bool p_replace, const Ref<PackedScene> &p_custom_scene) { |
74 | ERR_FAIL_COND_V(!is_inside_tree(), nullptr); |
75 | |
76 | Node *base = get_parent(); |
77 | if (!base) { |
78 | return nullptr; |
79 | } |
80 | |
81 | Ref<PackedScene> ps; |
82 | if (p_custom_scene.is_valid()) { |
83 | ps = p_custom_scene; |
84 | } else { |
85 | ps = ResourceLoader::load(path, "PackedScene" ); |
86 | } |
87 | |
88 | if (!ps.is_valid()) { |
89 | return nullptr; |
90 | } |
91 | Node *scene = ps->instantiate(); |
92 | if (!scene) { |
93 | return nullptr; |
94 | } |
95 | scene->set_name(get_name()); |
96 | int pos = get_index(); |
97 | |
98 | for (const PropSet &E : stored_values) { |
99 | scene->set(E.name, E.value); |
100 | } |
101 | |
102 | if (p_replace) { |
103 | queue_free(); |
104 | base->remove_child(this); |
105 | } |
106 | |
107 | base->add_child(scene); |
108 | base->move_child(scene, pos); |
109 | |
110 | return scene; |
111 | } |
112 | |
113 | Dictionary InstancePlaceholder::get_stored_values(bool p_with_order) { |
114 | Dictionary ret; |
115 | PackedStringArray order; |
116 | |
117 | for (const PropSet &E : stored_values) { |
118 | ret[E.name] = E.value; |
119 | if (p_with_order) { |
120 | order.push_back(E.name); |
121 | } |
122 | }; |
123 | |
124 | if (p_with_order) { |
125 | ret[".order" ] = order; |
126 | } |
127 | |
128 | return ret; |
129 | }; |
130 | |
131 | void InstancePlaceholder::_bind_methods() { |
132 | ClassDB::bind_method(D_METHOD("get_stored_values" , "with_order" ), &InstancePlaceholder::get_stored_values, DEFVAL(false)); |
133 | ClassDB::bind_method(D_METHOD("create_instance" , "replace" , "custom_scene" ), &InstancePlaceholder::create_instance, DEFVAL(false), DEFVAL(Variant())); |
134 | ClassDB::bind_method(D_METHOD("get_instance_path" ), &InstancePlaceholder::get_instance_path); |
135 | } |
136 | |
137 | InstancePlaceholder::InstancePlaceholder() { |
138 | } |
139 | |