Merge pull request #16995 from mrcdk/custom_resources_inspector
The inspector will show the custom resources added via plugin
This commit is contained in:
commit
2f4f8de459
4 changed files with 70 additions and 23 deletions
|
@ -433,26 +433,7 @@ Object *CreateDialog::instance_selected() {
|
||||||
custom = md;
|
custom = md;
|
||||||
|
|
||||||
if (custom != String()) {
|
if (custom != String()) {
|
||||||
if (EditorNode::get_editor_data().get_custom_types().has(custom)) {
|
return EditorNode::get_editor_data().instance_custom_type(selected->get_text(0), custom);
|
||||||
|
|
||||||
for (int i = 0; i < EditorNode::get_editor_data().get_custom_types()[custom].size(); i++) {
|
|
||||||
if (EditorNode::get_editor_data().get_custom_types()[custom][i].name == selected->get_text(0)) {
|
|
||||||
Ref<Texture> icon = EditorNode::get_editor_data().get_custom_types()[custom][i].icon;
|
|
||||||
Ref<Script> script = EditorNode::get_editor_data().get_custom_types()[custom][i].script;
|
|
||||||
String name = selected->get_text(0);
|
|
||||||
|
|
||||||
Object *ob = ClassDB::instance(custom);
|
|
||||||
ERR_FAIL_COND_V(!ob, NULL);
|
|
||||||
if (ob->is_class("Node")) {
|
|
||||||
ob->call("set_name", name);
|
|
||||||
}
|
|
||||||
ob->set_script(script.get_ref_ptr());
|
|
||||||
if (icon.is_valid())
|
|
||||||
ob->set_meta("_editor_icon", icon);
|
|
||||||
return ob;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return ClassDB::instance(selected->get_text(0));
|
return ClassDB::instance(selected->get_text(0));
|
||||||
}
|
}
|
||||||
|
|
|
@ -452,6 +452,31 @@ void EditorData::add_custom_type(const String &p_type, const String &p_inherits,
|
||||||
custom_types[p_inherits].push_back(ct);
|
custom_types[p_inherits].push_back(ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Object *EditorData::instance_custom_type(const String &p_type, const String &p_inherits) {
|
||||||
|
|
||||||
|
if (get_custom_types().has(p_inherits)) {
|
||||||
|
|
||||||
|
for (int i = 0; i < get_custom_types()[p_inherits].size(); i++) {
|
||||||
|
if (get_custom_types()[p_inherits][i].name == p_type) {
|
||||||
|
Ref<Texture> icon = get_custom_types()[p_inherits][i].icon;
|
||||||
|
Ref<Script> script = get_custom_types()[p_inherits][i].script;
|
||||||
|
|
||||||
|
Object *ob = ClassDB::instance(p_inherits);
|
||||||
|
ERR_FAIL_COND_V(!ob, NULL);
|
||||||
|
if (ob->is_class("Node")) {
|
||||||
|
ob->call("set_name", p_type);
|
||||||
|
}
|
||||||
|
ob->set_script(script.get_ref_ptr());
|
||||||
|
if (icon.is_valid())
|
||||||
|
ob->set_meta("_editor_icon", icon);
|
||||||
|
return ob;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
void EditorData::remove_custom_type(const String &p_type) {
|
void EditorData::remove_custom_type(const String &p_type) {
|
||||||
|
|
||||||
for (Map<String, Vector<CustomType> >::Element *E = custom_types.front(); E; E = E->next()) {
|
for (Map<String, Vector<CustomType> >::Element *E = custom_types.front(); E; E = E->next()) {
|
||||||
|
|
|
@ -171,6 +171,7 @@ public:
|
||||||
void restore_editor_global_states();
|
void restore_editor_global_states();
|
||||||
|
|
||||||
void add_custom_type(const String &p_type, const String &p_inherits, const Ref<Script> &p_script, const Ref<Texture> &p_icon);
|
void add_custom_type(const String &p_type, const String &p_inherits, const Ref<Script> &p_script, const Ref<Texture> &p_icon);
|
||||||
|
Object *instance_custom_type(const String &p_type, const String &p_inherits);
|
||||||
void remove_custom_type(const String &p_type);
|
void remove_custom_type(const String &p_type);
|
||||||
const Map<String, Vector<CustomType> > &get_custom_types() const { return custom_types; }
|
const Map<String, Vector<CustomType> > &get_custom_types() const { return custom_types; }
|
||||||
|
|
||||||
|
|
|
@ -285,6 +285,11 @@ void CustomPropertyEditor::_menu_option(int p_which) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Object *obj = ClassDB::instance(intype);
|
Object *obj = ClassDB::instance(intype);
|
||||||
|
|
||||||
|
if (!obj) {
|
||||||
|
obj = EditorNode::get_editor_data().instance_custom_type(intype, "Resource");
|
||||||
|
}
|
||||||
|
|
||||||
ERR_BREAK(!obj);
|
ERR_BREAK(!obj);
|
||||||
Resource *res = Object::cast_to<Resource>(obj);
|
Resource *res = Object::cast_to<Resource>(obj);
|
||||||
ERR_BREAK(!res);
|
ERR_BREAK(!res);
|
||||||
|
@ -877,6 +882,7 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant::
|
||||||
} else if (hint_text != "") {
|
} else if (hint_text != "") {
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
|
|
||||||
|
const Vector<EditorData::CustomType> custom_resources = EditorNode::get_editor_data().get_custom_types()["Resource"];
|
||||||
for (int i = 0; i < hint_text.get_slice_count(","); i++) {
|
for (int i = 0; i < hint_text.get_slice_count(","); i++) {
|
||||||
|
|
||||||
String base = hint_text.get_slice(",", i);
|
String base = hint_text.get_slice(",", i);
|
||||||
|
@ -885,6 +891,11 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant::
|
||||||
valid_inheritors.insert(base);
|
valid_inheritors.insert(base);
|
||||||
List<StringName> inheritors;
|
List<StringName> inheritors;
|
||||||
ClassDB::get_inheriters_from_class(base.strip_edges(), &inheritors);
|
ClassDB::get_inheriters_from_class(base.strip_edges(), &inheritors);
|
||||||
|
|
||||||
|
for (int i = 0; i < custom_resources.size(); i++) {
|
||||||
|
inheritors.push_back(custom_resources[i].name);
|
||||||
|
}
|
||||||
|
|
||||||
List<StringName>::Element *E = inheritors.front();
|
List<StringName>::Element *E = inheritors.front();
|
||||||
while (E) {
|
while (E) {
|
||||||
valid_inheritors.insert(E->get());
|
valid_inheritors.insert(E->get());
|
||||||
|
@ -893,14 +904,34 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant::
|
||||||
|
|
||||||
for (Set<String>::Element *E = valid_inheritors.front(); E; E = E->next()) {
|
for (Set<String>::Element *E = valid_inheritors.front(); E; E = E->next()) {
|
||||||
String t = E->get();
|
String t = E->get();
|
||||||
if (!ClassDB::can_instance(t))
|
|
||||||
|
bool is_custom_resource = false;
|
||||||
|
Ref<Texture> icon;
|
||||||
|
if (!custom_resources.empty()) {
|
||||||
|
for (int i = 0; i < custom_resources.size(); i++) {
|
||||||
|
if (custom_resources[i].name == t) {
|
||||||
|
is_custom_resource = true;
|
||||||
|
if (custom_resources[i].icon.is_valid())
|
||||||
|
icon = custom_resources[i].icon;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_custom_resource && !ClassDB::can_instance(t))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
inheritors_array.push_back(t);
|
inheritors_array.push_back(t);
|
||||||
|
|
||||||
int id = TYPE_BASE_ID + idx;
|
int id = TYPE_BASE_ID + idx;
|
||||||
if (has_icon(t, "EditorIcons")) {
|
|
||||||
|
|
||||||
menu->add_icon_item(get_icon(t, "EditorIcons"), vformat(TTR("New %s"), t), id);
|
if (!icon.is_valid() && has_icon(t, "EditorIcons")) {
|
||||||
|
icon = get_icon(t, "EditorIcons");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (icon.is_valid()) {
|
||||||
|
|
||||||
|
menu->add_icon_item(icon, vformat(TTR("New %s"), t), id);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
menu->add_item(vformat(TTR("New %s"), t), id);
|
menu->add_item(vformat(TTR("New %s"), t), id);
|
||||||
|
@ -1094,6 +1125,10 @@ void CustomPropertyEditor::_type_create_selected(int p_idx) {
|
||||||
|
|
||||||
Object *obj = ClassDB::instance(intype);
|
Object *obj = ClassDB::instance(intype);
|
||||||
|
|
||||||
|
if (!obj) {
|
||||||
|
obj = EditorNode::get_editor_data().instance_custom_type(intype, "Resource");
|
||||||
|
}
|
||||||
|
|
||||||
ERR_FAIL_COND(!obj);
|
ERR_FAIL_COND(!obj);
|
||||||
|
|
||||||
Resource *res = Object::cast_to<Resource>(obj);
|
Resource *res = Object::cast_to<Resource>(obj);
|
||||||
|
@ -1291,6 +1326,11 @@ void CustomPropertyEditor::_action_pressed(int p_which) {
|
||||||
if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
|
if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
|
||||||
|
|
||||||
Object *obj = ClassDB::instance(intype);
|
Object *obj = ClassDB::instance(intype);
|
||||||
|
|
||||||
|
if (!obj) {
|
||||||
|
obj = EditorNode::get_editor_data().instance_custom_type(intype, "Resource");
|
||||||
|
}
|
||||||
|
|
||||||
ERR_BREAK(!obj);
|
ERR_BREAK(!obj);
|
||||||
Resource *res = Object::cast_to<Resource>(obj);
|
Resource *res = Object::cast_to<Resource>(obj);
|
||||||
ERR_BREAK(!res);
|
ERR_BREAK(!res);
|
||||||
|
|
Loading…
Reference in a new issue