/*************************************************************************/ /* editor_inspector.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "editor_inspector.h" #include "array_property_edit.h" #include "dictionary_property_edit.h" #include "editor_node.h" #include "editor_scale.h" #include "multi_node_edit.h" #include "scene/resources/packed_scene.h" EditorDefaultClassValueCache *EditorDefaultClassValueCache::singleton = NULL; EditorDefaultClassValueCache *EditorDefaultClassValueCache::get_singleton() { return singleton; } Variant EditorDefaultClassValueCache::get_default_value(const StringName &p_class, const StringName &p_property) { if (!default_values.has(p_class)) { default_values[p_class] = Map(); if (ClassDB::can_instance(p_class)) { Object *c = ClassDB::instance(p_class); List plist; c->get_property_list(&plist); for (List::Element *E = plist.front(); E; E = E->next()) { if (E->get().usage & PROPERTY_USAGE_EDITOR) { Variant v = c->get(E->get().name); default_values[p_class][E->get().name] = v; } } memdelete(c); } } if (!default_values.has(p_class)) { return Variant(); } if (!default_values[p_class].has(p_property)) { return Variant(); } return default_values[p_class][p_property]; } EditorDefaultClassValueCache::EditorDefaultClassValueCache() { ERR_FAIL_COND(singleton != NULL); singleton = this; } Size2 EditorProperty::get_minimum_size() const { Size2 ms; Ref font = get_font("font", "Tree"); ms.height = font->get_height(); for (int i = 0; i < get_child_count(); i++) { Control *c = Object::cast_to(get_child(i)); if (!c) continue; if (c->is_set_as_toplevel()) continue; if (!c->is_visible()) continue; if (c == bottom_editor) continue; Size2 minsize = c->get_combined_minimum_size(); ms.width = MAX(ms.width, minsize.width); ms.height = MAX(ms.height, minsize.height); } if (keying) { Ref key = get_icon("Key", "EditorIcons"); ms.width += key->get_width() + get_constant("hseparator", "Tree"); } if (checkable) { Ref check = get_icon("checked", "CheckBox"); ms.width += check->get_width() + get_constant("hseparator", "Tree"); } if (bottom_editor != NULL && bottom_editor->is_visible()) { ms.height += get_constant("vseparation", "Tree"); Size2 bems = bottom_editor->get_combined_minimum_size(); //bems.width += get_constant("item_margin", "Tree"); ms.height += bems.height; ms.width = MAX(ms.width, bems.width); } return ms; } void EditorProperty::_notification(int p_what) { if (p_what == NOTIFICATION_SORT_CHILDREN) { Size2 size = get_size(); Rect2 rect; Rect2 bottom_rect; right_child_rect = Rect2(); bottom_child_rect = Rect2(); { int child_room = size.width * (1.0 - split_ratio); Ref font = get_font("font", "Tree"); int height = font->get_height(); bool no_children = true; //compute room needed for (int i = 0; i < get_child_count(); i++) { Control *c = Object::cast_to(get_child(i)); if (!c) continue; if (c->is_set_as_toplevel()) continue; if (c == bottom_editor) continue; Size2 minsize = c->get_combined_minimum_size(); child_room = MAX(child_room, minsize.width); height = MAX(height, minsize.height); no_children = false; } if (no_children) { text_size = size.width; rect = Rect2(size.width - 1, 0, 1, height); } else { text_size = MAX(0, size.width - (child_room + 4 * EDSCALE)); rect = Rect2(size.width - child_room, 0, child_room, height); } if (bottom_editor) { int m = 0; //get_constant("item_margin", "Tree"); bottom_rect = Rect2(m, rect.size.height + get_constant("vseparation", "Tree"), size.width - m, bottom_editor->get_combined_minimum_size().height); } } if (keying) { Ref key; if (use_keying_next()) { key = get_icon("KeyNext", "EditorIcons"); } else { key = get_icon("Key", "EditorIcons"); } rect.size.x -= key->get_width() + get_constant("hseparator", "Tree"); } //set children for (int i = 0; i < get_child_count(); i++) { Control *c = Object::cast_to(get_child(i)); if (!c) continue; if (c->is_set_as_toplevel()) continue; if (c == bottom_editor) continue; fit_child_in_rect(c, rect); right_child_rect = rect; } if (bottom_editor) { fit_child_in_rect(bottom_editor, bottom_rect); bottom_child_rect = bottom_rect; } update(); //need to redraw text } if (p_what == NOTIFICATION_DRAW) { Ref font = get_font("font", "Tree"); Color dark_color = get_color("dark_color_2", "Editor"); Size2 size = get_size(); if (bottom_editor) { size.height = bottom_editor->get_margin(MARGIN_TOP); } else if (label_reference) { size.height = label_reference->get_size().height; } if (selected) { Ref sb = get_stylebox("selected", "Tree"); draw_style_box(sb, Rect2(Vector2(), size)); } if (draw_top_bg && right_child_rect != Rect2()) { draw_rect(right_child_rect, dark_color); } if (bottom_child_rect != Rect2()) { draw_rect(bottom_child_rect, dark_color); } Color color; if (draw_red) { color = get_color("error_color", "Editor"); } else { color = get_color("property_color", "Editor"); } if (label.find(".") != -1) { color.a = 0.5; //this should be un-hacked honestly, as it's used for editor overrides } int ofs = 0; if (checkable) { Ref checkbox; if (checked) checkbox = get_icon("checked", "CheckBox"); else checkbox = get_icon("unchecked", "CheckBox"); Color color(1, 1, 1); if (check_hover) { color.r *= 1.2; color.g *= 1.2; color.b *= 1.2; } check_rect = Rect2(ofs, ((size.height - checkbox->get_height()) / 2), checkbox->get_width(), checkbox->get_height()); draw_texture(checkbox, check_rect.position, color); ofs += get_constant("hseparator", "Tree"); ofs += checkbox->get_width(); } else { check_rect = Rect2(); } int text_limit = text_size; if (can_revert) { Ref reload_icon = get_icon("ReloadSmall", "EditorIcons"); text_limit -= reload_icon->get_width() + get_constant("hseparator", "Tree") * 2; revert_rect = Rect2(text_limit + get_constant("hseparator", "Tree"), (size.height - reload_icon->get_height()) / 2, reload_icon->get_width(), reload_icon->get_height()); Color color(1, 1, 1); if (revert_hover) { color.r *= 1.2; color.g *= 1.2; color.b *= 1.2; } draw_texture(reload_icon, revert_rect.position, color); } else { revert_rect = Rect2(); } int v_ofs = (size.height - font->get_height()) / 2; draw_string(font, Point2(ofs, v_ofs + font->get_ascent()), label, color, text_limit); if (keying) { Ref key; if (use_keying_next()) { key = get_icon("KeyNext", "EditorIcons"); } else { key = get_icon("Key", "EditorIcons"); } ofs = size.width - key->get_width() - get_constant("hseparator", "Tree"); Color color(1, 1, 1); if (keying_hover) { color.r *= 1.2; color.g *= 1.2; color.b *= 1.2; } keying_rect = Rect2(ofs, ((size.height - key->get_height()) / 2), key->get_width(), key->get_height()); draw_texture(key, keying_rect.position, color); } else { keying_rect = Rect2(); } } } void EditorProperty::set_label(const String &p_label) { label = p_label; update(); } String EditorProperty::get_label() const { return label; } Object *EditorProperty::get_edited_object() { return object; } StringName EditorProperty::get_edited_property() { return property; } void EditorProperty::update_property() { if (get_script_instance()) get_script_instance()->call("update_property"); } void EditorProperty::set_read_only(bool p_read_only) { read_only = p_read_only; } bool EditorProperty::is_read_only() const { return read_only; } bool EditorProperty::_might_be_in_instance() { if (!object) return false; Node *node = Object::cast_to(object); 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; // or might not be } bool EditorProperty::_get_instanced_node_original_property(const StringName &p_prop, Variant &value) { Node *node = Object::cast_to(object); 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 EditorProperty::_is_property_different(const Variant &p_current, const Variant &p_orig, int p_usage) { // this is a pretty difficult function, because a property may not be saved but may have // the flag to not save if one or if zero { Node *node = Object::cast_to(object); 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) { // not found (was not saved) // check if it was not saved due to being zero or one if (p_current.is_zero() && property_usage & PROPERTY_USAGE_STORE_IF_NONZERO) return false; if (p_current.is_one() && property_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 EditorProperty::_is_instanced_node_with_original_property_different() { bool mbi = _might_be_in_instance(); if (mbi) { Variant vorig; int usage = property_usage & (PROPERTY_USAGE_STORE_IF_NONONE | PROPERTY_USAGE_STORE_IF_NONZERO); if (_get_instanced_node_original_property(property, vorig) || usage) { Variant v = object->get(property); if (_is_property_different(v, vorig, usage)) { return true; } } } return false; } void EditorProperty::update_reload_status() { if (property == StringName()) return; //no property, so nothing to do bool has_reload = false; if (EditorDefaultClassValueCache::get_singleton()) { Variant default_value = EditorDefaultClassValueCache::get_singleton()->get_default_value(object->get_class_name(), property); if (default_value != Variant() && default_value != object->get(property)) { has_reload = true; } } if (_is_instanced_node_with_original_property_different()) { has_reload = true; } if (object->call("property_can_revert", property).operator bool()) { has_reload = true; } if (!has_reload && !object->get_script().is_null()) { Ref