/*************************************************************************/ /* editor_inspector.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2021 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/doc_tools.h" #include "editor_feature_profile.h" #include "editor_node.h" #include "editor_scale.h" #include "multi_node_edit.h" #include "scene/resources/packed_scene.h" Size2 EditorProperty::get_minimum_size() const { Size2 ms; Ref font = get_theme_font("font", "Tree"); int font_size = get_theme_font_size("font_size", "Tree"); ms.height = font->get_height(font_size); 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_top_level()) { 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_theme_icon("Key", "EditorIcons"); ms.width += key->get_width() + get_theme_constant("hseparator", "Tree"); } if (deletable) { Ref key = get_theme_icon("Close", "EditorIcons"); ms.width += key->get_width() + get_theme_constant("hseparator", "Tree"); } if (checkable) { Ref check = get_theme_icon("checked", "CheckBox"); ms.width += check->get_width() + get_theme_constant("hseparation", "CheckBox") + get_theme_constant("hseparator", "Tree"); } if (bottom_editor != nullptr && bottom_editor->is_visible()) { ms.height += get_theme_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::emit_changed(const StringName &p_property, const Variant &p_value, const StringName &p_field, bool p_changing) { Variant args[4] = { p_property, p_value, p_field, p_changing }; const Variant *argptrs[4] = { &args[0], &args[1], &args[2], &args[3] }; cache[p_property] = p_value; emit_signal("property_changed", (const Variant **)argptrs, 4); } 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_theme_font("font", "Tree"); int font_size = get_theme_font_size("font_size", "Tree"); int height = font->get_height(font_size); 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_top_level()) { 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)); if (is_layout_rtl()) { rect = Rect2(1, 0, child_room, height); } else { 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_theme_constant("vseparation", "Tree"), size.width - m, bottom_editor->get_combined_minimum_size().height); } if (keying) { Ref key; if (use_keying_next()) { key = get_theme_icon("KeyNext", "EditorIcons"); } else { key = get_theme_icon("Key", "EditorIcons"); } rect.size.x -= key->get_width() + get_theme_constant("hseparator", "Tree"); if (is_layout_rtl()) { rect.position.x += key->get_width() + get_theme_constant("hseparator", "Tree"); } if (no_children) { text_size -= key->get_width() + 4 * EDSCALE; } } if (deletable) { Ref close; close = get_theme_icon("Close", "EditorIcons"); rect.size.x -= close->get_width() + get_theme_constant("hseparator", "Tree"); if (is_layout_rtl()) { rect.position.x += close->get_width() + get_theme_constant("hseparator", "Tree"); } if (no_children) { text_size -= close->get_width() + 4 * EDSCALE; } } } //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_top_level()) { 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_theme_font("font", "Tree"); int font_size = get_theme_font_size("font_size", "Tree"); Color dark_color = get_theme_color("dark_color_2", "Editor"); bool rtl = is_layout_rtl(); Size2 size = get_size(); if (bottom_editor) { size.height = bottom_editor->get_offset(SIDE_TOP); } else if (label_reference) { size.height = label_reference->get_size().height; } if (selected) { Ref sb = get_theme_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_theme_color("error_color", "Editor"); } else { color = get_theme_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; int text_limit = text_size; if (checkable) { Ref checkbox; if (checked) { checkbox = get_theme_icon("GuiChecked", "EditorIcons"); } else { checkbox = get_theme_icon("GuiUnchecked", "EditorIcons"); } Color color2(1, 1, 1); if (check_hover) { color2.r *= 1.2; color2.g *= 1.2; color2.b *= 1.2; } check_rect = Rect2(ofs, ((size.height - checkbox->get_height()) / 2), checkbox->get_width(), checkbox->get_height()); if (rtl) { draw_texture(checkbox, Vector2(size.width - check_rect.position.x - checkbox->get_width(), check_rect.position.y), color2); } else { draw_texture(checkbox, check_rect.position, color2); } ofs += get_theme_constant("hseparator", "Tree") + checkbox->get_width() + get_theme_constant("hseparation", "CheckBox"); text_limit -= ofs; } else { check_rect = Rect2(); } if (can_revert) { Ref reload_icon = get_theme_icon("ReloadSmall", "EditorIcons"); text_limit -= reload_icon->get_width() + get_theme_constant("hseparator", "Tree") * 2; revert_rect = Rect2(text_limit + get_theme_constant("hseparator", "Tree"), (size.height - reload_icon->get_height()) / 2, reload_icon->get_width(), reload_icon->get_height()); Color color2(1, 1, 1); if (revert_hover) { color2.r *= 1.2; color2.g *= 1.2; color2.b *= 1.2; } if (rtl) { draw_texture(reload_icon, Vector2(size.width - revert_rect.position.x - reload_icon->get_width(), revert_rect.position.y), color2); } else { draw_texture(reload_icon, revert_rect.position, color2); } } else { revert_rect = Rect2(); } int v_ofs = (size.height - font->get_height(font_size)) / 2; if (rtl) { draw_string(font, Point2(size.width - ofs - text_limit, v_ofs + font->get_ascent(font_size)), label, HALIGN_RIGHT, text_limit, font_size, color); } else { draw_string(font, Point2(ofs, v_ofs + font->get_ascent(font_size)), label, HALIGN_LEFT, text_limit, font_size, color); } if (keying) { Ref key; if (use_keying_next()) { key = get_theme_icon("KeyNext", "EditorIcons"); } else { key = get_theme_icon("Key", "EditorIcons"); } ofs = size.width - key->get_width() - get_theme_constant("hseparator", "Tree"); Color color2(1, 1, 1); if (keying_hover) { color2.r *= 1.2; color2.g *= 1.2; color2.b *= 1.2; } keying_rect = Rect2(ofs, ((size.height - key->get_height()) / 2), key->get_width(), key->get_height()); if (rtl) { draw_texture(key, Vector2(size.width - keying_rect.position.x - key->get_width(), keying_rect.position.y), color2); } else { draw_texture(key, keying_rect.position, color2); } } else { keying_rect = Rect2(); } if (deletable) { Ref close; close = get_theme_icon("Close", "EditorIcons"); ofs = size.width - close->get_width() - get_theme_constant("hseparator", "Tree"); Color color2(1, 1, 1); if (delete_hover) { color2.r *= 1.2; color2.g *= 1.2; color2.b *= 1.2; } delete_rect = Rect2(ofs, ((size.height - close->get_height()) / 2), close->get_width(), close->get_height()); if (rtl) { draw_texture(close, Vector2(size.width - delete_rect.position.x - close->get_width(), delete_rect.position.y), color2); } else { draw_texture(close, delete_rect.position, color2); } } else { delete_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 EditorPropertyRevert::may_node_be_in_instance(Node *p_node) { Node *edited_scene = EditorNode::get_singleton()->get_edited_scene(); bool might_be = false; Node *node = p_node; while (node) { if (node == edited_scene) { if (node->get_scene_inherited_state().is_valid()) { might_be = true; break; } might_be = false; break; } if (node->get_scene_instance_state().is_valid()) { might_be = true; break; } node = node->get_owner(); } return might_be; // or might not be } bool EditorPropertyRevert::get_instanced_node_original_property(Node *p_node, const StringName &p_prop, Variant &value) { Node *node = p_node; 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(); } if (!found && p_node) { //if not found, try default class value Variant attempt = ClassDB::class_get_default_property_value(p_node->get_class_name(), p_prop); if (attempt.get_type() != Variant::NIL) { found = true; value = attempt; } } return found; } bool EditorPropertyRevert::is_node_property_different(Node *p_node, const Variant &p_current, const Variant &p_orig) { // 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 //make sure there is an actual state { Node *node = p_node; 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_current.get_type() == Variant::FLOAT && p_orig.get_type() == Variant::FLOAT) { float a = p_current; float b = p_orig; return !Math::is_equal_approx(a, b); //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 EditorPropertyRevert::can_property_revert(Object *p_object, const StringName &p_property) { bool has_revert = false; Node *node = Object::cast_to(p_object); if (node && EditorPropertyRevert::may_node_be_in_instance(node)) { //check for difference including instantiation Variant vorig; if (EditorPropertyRevert::get_instanced_node_original_property(node, p_property, vorig)) { Variant v = p_object->get(p_property); if (EditorPropertyRevert::is_node_property_different(node, v, vorig)) { has_revert = true; } } } else { //check for difference against default class value instead Variant default_value = ClassDB::class_get_default_property_value(p_object->get_class_name(), p_property); if (default_value != Variant() && default_value != p_object->get(p_property)) { has_revert = true; } } // If the object implements property_can_revert, rely on that completely // (i.e. don't then try to revert to default value - the property_get_revert implementation // can do that if so desired) if (p_object->has_method("property_can_revert")) { has_revert = p_object->call("property_can_revert", p_property).operator bool(); } else { if (!has_revert && !p_object->get_script().is_null()) { Ref