diff --git a/editor/editor_inspector.cpp b/editor/editor_inspector.cpp index 7a6d2727069..929028c499c 100644 --- a/editor/editor_inspector.cpp +++ b/editor/editor_inspector.cpp @@ -38,7 +38,6 @@ // TODO: // arrays and dictionary -// replace property editor in sectionedpropertyeditor Size2 EditorProperty::get_minimum_size() const { diff --git a/editor/property_editor.cpp b/editor/property_editor.cpp index d9812f7425e..1c2eedfc9c5 100644 --- a/editor/property_editor.cpp +++ b/editor/property_editor.cpp @@ -1910,6 +1910,7 @@ void CustomPropertyEditor::_bind_methods() { ADD_SIGNAL(MethodInfo("variant_field_changed", PropertyInfo(Variant::STRING, "field"))); ADD_SIGNAL(MethodInfo("resource_edit_request")); } + CustomPropertyEditor::CustomPropertyEditor() { read_only = false; @@ -2034,2691 +2035,8 @@ CustomPropertyEditor::CustomPropertyEditor() { property_select = NULL; } -bool PropertyEditor::_might_be_in_instance() { - - if (!obj) - return false; - - Node *node = Object::cast_to(obj); - - Node *edited_scene = EditorNode::get_singleton()->get_edited_scene(); - - bool might_be = false; - - while (node) { - - if (node->get_scene_instance_state().is_valid()) { - might_be = true; - break; - } - if (node == edited_scene) { - if (node->get_scene_inherited_state().is_valid()) { - might_be = true; - break; - } - might_be = false; - break; - } - node = node->get_owner(); - } - - return might_be; -} - -bool PropertyEditor::_get_instanced_node_original_property(const StringName &p_prop, Variant &value) { - - Node *node = Object::cast_to(obj); - - if (!node) - return false; - - Node *orig = node; - - Node *edited_scene = EditorNode::get_singleton()->get_edited_scene(); - - bool found = false; - - while (node) { - - Ref ss; - - if (node == edited_scene) { - ss = node->get_scene_inherited_state(); - - } else { - ss = node->get_scene_instance_state(); - } - - if (ss.is_valid()) { - - NodePath np = node->get_path_to(orig); - int node_idx = ss->find_node_by_path(np); - if (node_idx >= 0) { - bool lfound = false; - Variant lvar; - lvar = ss->get_property_value(node_idx, p_prop, lfound); - if (lfound) { - - found = true; - value = lvar; - } - } - } - if (node == edited_scene) { - //just in case - break; - } - node = node->get_owner(); - } - - return found; -} - -bool PropertyEditor::_is_property_different(const Variant &p_current, const Variant &p_orig, int p_usage) { - - { - Node *node = Object::cast_to(obj); - if (!node) - return false; - - Node *edited_scene = EditorNode::get_singleton()->get_edited_scene(); - bool found_state = false; - - while (node) { - - Ref ss; - - if (node == edited_scene) { - ss = node->get_scene_inherited_state(); - - } else { - ss = node->get_scene_instance_state(); - } - - if (ss.is_valid()) { - found_state = true; - } - if (node == edited_scene) { - //just in case - break; - } - node = node->get_owner(); - } - - if (!found_state) - return false; //pointless to check if we are not comparing against anything. - } - - if (p_orig.get_type() == Variant::NIL) { - - //special cases - if (p_current.is_zero() && p_usage & PROPERTY_USAGE_STORE_IF_NONZERO) - return false; - if (p_current.is_one() && p_usage & PROPERTY_USAGE_STORE_IF_NONONE) - return false; - } - - if (p_current.get_type() == Variant::REAL && p_orig.get_type() == Variant::REAL) { - float a = p_current; - float b = p_orig; - - return Math::abs(a - b) > CMP_EPSILON; //this must be done because, as some scenes save as text, there might be a tiny difference in floats due to numerical error - } - - return bool(Variant::evaluate(Variant::OP_NOT_EQUAL, p_current, p_orig)); -} - -bool PropertyEditor::_is_instanced_node_with_original_property_different(const String &p_name, TreeItem *item) { - bool mbi = _might_be_in_instance(); - if (mbi) { - Variant vorig; - Dictionary d = item->get_metadata(0); - int usage = d.has("usage") ? int(int(d["usage"]) & (PROPERTY_USAGE_STORE_IF_NONONE | PROPERTY_USAGE_STORE_IF_NONZERO)) : 0; - if (_get_instanced_node_original_property(p_name, vorig) || usage) { - Variant v = obj->get(p_name); - - if (_is_property_different(v, vorig, usage)) { - return true; - } - } - } - return false; -} - -TreeItem *PropertyEditor::find_item(TreeItem *p_item, const String &p_name) { - - if (!p_item) - return NULL; - - String name = p_item->get_metadata(1); - - if (name == p_name) { - - return p_item; - } - - TreeItem *c = p_item->get_children(); - - while (c) { - - TreeItem *found = find_item(c, p_name); - if (found) - return found; - c = c->get_next(); - } - - return NULL; -} - -void PropertyEditor::_changed_callback(Object *p_changed, const char *p_prop) { - - _changed_callbacks(p_changed, p_prop); -} - -void PropertyEditor::_changed_callbacks(Object *p_changed, const String &p_prop) { - - if (p_changed != obj) - return; - - if (changing) - return; - - if (p_prop == String()) - update_tree_pending = true; - else { - - pending[p_prop] = p_prop; - } -} - -void PropertyEditor::update_property(const String &p_prop) { - - if (obj) - _changed_callbacks(obj, p_prop); -} - -void PropertyEditor::set_item_text(TreeItem *p_item, int p_type, const String &p_name, int p_hint, const String &p_hint_text) { - - switch (p_type) { - - case Variant::BOOL: { - - p_item->set_checked(1, obj->get(p_name)); - } break; - case Variant::REAL: - case Variant::INT: { - - if (p_hint == PROPERTY_HINT_LAYERS_2D_PHYSICS || p_hint == PROPERTY_HINT_LAYERS_2D_RENDER || p_hint == PROPERTY_HINT_LAYERS_3D_PHYSICS || p_hint == PROPERTY_HINT_LAYERS_3D_RENDER) { - tree->update(); - break; - } - - if (p_hint == PROPERTY_HINT_FLAGS) { - Vector values = p_hint_text.split(","); - String flags; - int val = obj->get(p_name); - for (int i = 0; i < values.size(); i++) { - - String v = values[i]; - if (v == "") - continue; - if (!(val & (1 << i))) - continue; - - if (flags != "") - flags += ", "; - flags += v; - } - p_item->set_text(1, flags); - break; - } - - if (p_hint == PROPERTY_HINT_EXP_EASING) { - - p_item->set_text(1, String::num(obj->get(p_name), 2)); - break; - } - - if (p_type == Variant::REAL) { - p_item->set_range(1, obj->get(p_name)); - - } else { - /* FIXME: Why are both statements equal? */ - p_item->set_range(1, obj->get(p_name)); - } - - p_item->set_editable(1, !read_only); - - } break; - case Variant::STRING: - - if (p_hint == PROPERTY_HINT_TYPE_STRING) { - - p_item->set_text(1, obj->get(p_name)); - } - - if (p_hint == PROPERTY_HINT_METHOD_OF_VARIANT_TYPE || - p_hint == PROPERTY_HINT_METHOD_OF_BASE_TYPE || - p_hint == PROPERTY_HINT_METHOD_OF_INSTANCE || - p_hint == PROPERTY_HINT_METHOD_OF_SCRIPT || - p_hint == PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE || - p_hint == PROPERTY_HINT_PROPERTY_OF_BASE_TYPE || - p_hint == PROPERTY_HINT_PROPERTY_OF_INSTANCE || - p_hint == PROPERTY_HINT_PROPERTY_OF_SCRIPT) { - - p_item->set_text(1, obj->get(p_name)); - } - - if (p_hint == PROPERTY_HINT_ENUM) { - - Vector strings = p_hint_text.split(","); - String current = obj->get(p_name); - int idx = 0; - for (int x = 0; x < strings.size(); x++) { - if (strings[x] == current) { - idx = x; - break; - } - } - p_item->set_text(1, p_hint_text); - p_item->set_range(1, idx); - break; - } - - case Variant::VECTOR3: - case Variant::QUAT: - case Variant::VECTOR2: - case Variant::AABB: - case Variant::RECT2: - case Variant::TRANSFORM2D: - case Variant::BASIS: - case Variant::TRANSFORM: { - - p_item->set_text(1, obj->get(p_name)); - - } break; - case Variant::COLOR: { - - tree->update(); - - } break; - case Variant::NODE_PATH: { - - p_item->set_text(1, obj->get(p_name)); - } break; - case Variant::OBJECT: { - - Ref encoded = obj->get(p_name); //for debugger and remote tools - - if (encoded.is_valid()) { - - p_item->set_text(1, "Object: " + itos(encoded->get_object_id())); - p_item->set_icon(1, Ref()); - p_item->set_custom_as_button(1, true); - - } else if (obj->get(p_name).get_type() == Variant::NIL || obj->get(p_name).operator RefPtr().is_null()) { - p_item->set_text(1, ""); - p_item->set_icon(1, Ref()); - p_item->set_custom_as_button(1, false); - - Dictionary d = p_item->get_metadata(0); - int hint = d.has("hint") ? d["hint"].operator int() : -1; - String hint_text = d.has("hint_text") ? d["hint_text"] : ""; - if (hint == PROPERTY_HINT_RESOURCE_TYPE && hint_text == "Texture") { - p_item->set_icon(1, NULL); - } - - } else { - p_item->set_custom_as_button(1, true); - RES res = obj->get(p_name).operator RefPtr(); - if (res->is_class("Texture")) { - int tw = EditorSettings::get_singleton()->get("docks/property_editor/texture_preview_width"); - Vector2 size(res->call("get_width"), res->call("get_height")); - if (size.width < size.height) { - tw = MAX((size.width / size.height) * tw, 1); - } - p_item->set_icon_max_width(1, tw); - p_item->set_icon(1, res); - p_item->set_text(1, ""); - - } else if (res->get_name() != "") { - - p_item->set_text(1, res->get_name()); - } else if (res->get_path() != "" && !res->get_path().begins_with("local://")) { - p_item->set_text(1, res->get_path().get_file()); - } else { - p_item->set_text(1, "<" + res->get_class() + ">"); - }; - - if (res.is_valid() && res->get_path().is_resource_file()) { - p_item->set_tooltip(1, res->get_path()); - } else if (res.is_valid()) { - p_item->set_tooltip(1, res->get_name() + " (" + res->get_class() + ")"); - } - - if (has_icon(res->get_class(), "EditorIcons")) { - - p_item->set_icon(0, get_icon(res->get_class(), "EditorIcons")); - } else { - - Dictionary d = p_item->get_metadata(0); - int hint = d.has("hint") ? d["hint"].operator int() : -1; - String hint_text = d.has("hint_text") ? d["hint_text"] : ""; - if (hint == PROPERTY_HINT_RESOURCE_TYPE) { - - if (has_icon(hint_text, "EditorIcons")) { - - p_item->set_icon(0, get_icon(hint_text, "EditorIcons")); - - } else { - p_item->set_icon(0, get_icon("Object", "EditorIcons")); - } - } - } - - if (res->is_class("Script")) { - p_item->set_text(1, res->get_path().get_file()); - } else if (!res->is_class("Texture")) { - //texture already previews via itself - EditorResourcePreview::get_singleton()->queue_edited_resource_preview(res, this, "_resource_preview_done", p_item->get_instance_id()); - } - } - - } break; - default: {}; - } -} - -void PropertyEditor::_check_reload_status(const String &p_name, TreeItem *item) { - - bool has_reload = false; - int found = -1; - bool is_disabled = false; - - for (int i = 0; i < item->get_button_count(1); i++) { - - if (item->get_button_id(1, i) == 3) { - found = i; - is_disabled = item->is_button_disabled(1, i); - break; - } - } - - if (_is_instanced_node_with_original_property_different(p_name, item)) { - has_reload = true; - } - - if (obj->call("property_can_revert", p_name).operator bool()) { - - has_reload = true; - } - - if (!has_reload && !obj->get_script().is_null()) { - Ref