Path: blob/master/editor/debugger/editor_debugger_inspector.cpp
9896 views
/**************************************************************************/1/* editor_debugger_inspector.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_debugger_inspector.h"3132#include "core/debugger/debugger_marshalls.h"33#include "core/io/marshalls.h"34#include "editor/docks/inspector_dock.h"35#include "editor/editor_node.h"36#include "editor/editor_undo_redo_manager.h"37#include "scene/debugger/scene_debugger.h"3839bool EditorDebuggerRemoteObjects::_set(const StringName &p_name, const Variant &p_value) {40return _set_impl(p_name, p_value, "");41}4243bool EditorDebuggerRemoteObjects::_set_impl(const StringName &p_name, const Variant &p_value, const String &p_field) {44String name = p_name;45if (!prop_values.has(name) || String(name).begins_with("Constants/")) {46return false;47}4849// Change it back to the real name when fetching.50if (name == "Script") {51name = "script";52} else if (name.begins_with("Metadata/")) {53name = name.replace_first("Metadata/", "metadata/");54}5556Dictionary &values = prop_values[p_name];57Dictionary old_values = values.duplicate();58for (const uint64_t key : values.keys()) {59values.set(key, p_value);60}6162EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();63const int size = remote_object_ids.size();64ur->create_action(size == 1 ? vformat(TTR("Set %s"), name) : vformat(TTR("Set %s on %d objects"), name, size), UndoRedo::MERGE_ENDS);6566ur->add_do_method(this, SNAME("emit_signal"), SNAME("values_edited"), name, values, p_field);67ur->add_undo_method(this, SNAME("emit_signal"), SNAME("values_edited"), name, old_values, p_field);68ur->commit_action();6970return true;71}7273bool EditorDebuggerRemoteObjects::_get(const StringName &p_name, Variant &r_ret) const {74String name = p_name;75if (!prop_values.has(name)) {76return false;77}7879// Change it back to the real name when fetching.80if (name == "Script") {81name = "script";82} else if (name.begins_with("Metadata/")) {83name = name.replace_first("Metadata/", "metadata/");84}8586r_ret = prop_values[p_name][remote_object_ids[0]];87return true;88}8990void EditorDebuggerRemoteObjects::_get_property_list(List<PropertyInfo> *p_list) const {91p_list->clear(); // Sorry, don't want any categories.92for (const PropertyInfo &prop : prop_list) {93p_list->push_back(prop);94}95}9697void EditorDebuggerRemoteObjects::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {98_set_impl(p_property, p_value, p_field);99}100101String EditorDebuggerRemoteObjects::get_title() {102if (!remote_object_ids.is_empty() && ObjectID(remote_object_ids[0].operator uint64_t()).is_valid()) {103const int size = remote_object_ids.size();104return size == 1 ? vformat(TTR("Remote %s: %d"), type_name, remote_object_ids[0]) : vformat(TTR("Remote %s (%d Selected)"), type_name, size);105}106107return "<null>";108}109110Variant EditorDebuggerRemoteObjects::get_variant(const StringName &p_name) {111Variant var;112_get(p_name, var);113return var;114}115116void EditorDebuggerRemoteObjects::_bind_methods() {117ClassDB::bind_method(D_METHOD("get_title"), &EditorDebuggerRemoteObjects::get_title);118ClassDB::bind_method("_hide_script_from_inspector", &EditorDebuggerRemoteObjects::_hide_script_from_inspector);119ClassDB::bind_method("_hide_metadata_from_inspector", &EditorDebuggerRemoteObjects::_hide_metadata_from_inspector);120121ADD_SIGNAL(MethodInfo("values_edited", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::DICTIONARY, "values", PROPERTY_HINT_DICTIONARY_TYPE, "uint64_t:Variant"), PropertyInfo(Variant::STRING, "field")));122}123124/// EditorDebuggerInspector125126EditorDebuggerInspector::EditorDebuggerInspector() {127variables = memnew(EditorDebuggerRemoteObjects);128}129130EditorDebuggerInspector::~EditorDebuggerInspector() {131clear_cache();132memdelete(variables);133}134135void EditorDebuggerInspector::_bind_methods() {136ADD_SIGNAL(MethodInfo("object_selected", PropertyInfo(Variant::INT, "id")));137ADD_SIGNAL(MethodInfo("objects_edited", PropertyInfo(Variant::ARRAY, "ids"), PropertyInfo(Variant::STRING, "property"), PropertyInfo("value"), PropertyInfo(Variant::STRING, "field")));138ADD_SIGNAL(MethodInfo("object_property_updated", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "property")));139}140141void EditorDebuggerInspector::_notification(int p_what) {142switch (p_what) {143case NOTIFICATION_POSTINITIALIZE: {144connect("object_id_selected", callable_mp(this, &EditorDebuggerInspector::_object_selected));145} break;146147case NOTIFICATION_ENTER_TREE: {148variables->remote_object_ids.append(0);149edit(variables);150} break;151}152}153154void EditorDebuggerInspector::_objects_edited(const String &p_prop, const TypedDictionary<uint64_t, Variant> &p_values, const String &p_field) {155emit_signal(SNAME("objects_edited"), p_prop, p_values, p_field);156}157158void EditorDebuggerInspector::_object_selected(ObjectID p_object) {159emit_signal(SNAME("object_selected"), p_object);160}161162EditorDebuggerRemoteObjects *EditorDebuggerInspector::set_objects(const Array &p_arr) {163ERR_FAIL_COND_V(p_arr.is_empty(), nullptr);164165TypedArray<uint64_t> ids;166LocalVector<SceneDebuggerObject> objects;167for (const Array arr : p_arr) {168SceneDebuggerObject obj;169obj.deserialize(arr);170if (obj.id.is_valid()) {171ids.push_back((uint64_t)obj.id);172objects.push_back(obj);173}174}175ERR_FAIL_COND_V(ids.is_empty(), nullptr);176177// Sorting is necessary, as selected nodes in the remote tree are ordered by index.178ids.sort();179180EditorDebuggerRemoteObjects *remote_objects = nullptr;181for (EditorDebuggerRemoteObjects *robjs : remote_objects_list) {182if (robjs->remote_object_ids == ids) {183remote_objects = robjs;184break;185}186}187188if (!remote_objects) {189remote_objects = memnew(EditorDebuggerRemoteObjects);190remote_objects->remote_object_ids = ids;191remote_objects->remote_object_ids.make_read_only();192remote_objects->connect("values_edited", callable_mp(this, &EditorDebuggerInspector::_objects_edited));193remote_objects_list.push_back(remote_objects);194}195196StringName class_name = objects[0].class_name;197if (class_name != SNAME("Object")) {198// Search for the common class between all selected objects.199bool check_type_again = true;200while (check_type_again) {201check_type_again = false;202203if (class_name == SNAME("Object") || class_name == StringName()) {204// All objects inherit from Object, so no need to continue checking.205class_name = SNAME("Object");206break;207}208209// Check that all objects inherit from type_name.210for (const SceneDebuggerObject &obj : objects) {211if (obj.class_name == class_name || ClassDB::is_parent_class(obj.class_name, class_name)) {212continue; // class_name is the same or a parent of the object's class.213}214215// class_name is not a parent of the node's class, so check again with the parent class.216class_name = ClassDB::get_parent_class(class_name);217check_type_again = true;218break;219}220}221}222remote_objects->type_name = class_name;223224// Search for properties that are present in all selected objects.225struct UsageData {226int qty = 0;227SceneDebuggerObject::SceneDebuggerProperty prop;228TypedDictionary<uint64_t, Variant> values;229};230HashMap<String, UsageData> usage;231int nc = 0;232for (const SceneDebuggerObject &obj : objects) {233for (const SceneDebuggerObject::SceneDebuggerProperty &prop : obj.properties) {234PropertyInfo pinfo = prop.first;235// Rename those variables, so they don't conflict with the ones from the resource itself.236if (pinfo.name == "script") {237pinfo.name = "Script";238} else if (pinfo.name.begins_with("metadata/")) {239pinfo.name = pinfo.name.replace_first("metadata/", "Metadata/");240}241242if (!usage.has(pinfo.name)) {243UsageData usage_dt;244usage_dt.prop = prop;245usage_dt.prop.first.name = pinfo.name;246usage_dt.values[obj.id] = prop.second;247usage[pinfo.name] = usage_dt;248}249250// Make sure only properties with the same exact PropertyInfo data will appear.251if (usage[pinfo.name].prop.first == pinfo) {252usage[pinfo.name].qty++;253usage[pinfo.name].values[obj.id] = prop.second;254}255}256257nc++;258}259for (HashMap<String, UsageData>::Iterator E = usage.begin(); E;) {260HashMap<String, UsageData>::Iterator next = E;261++next;262263UsageData usage_dt = E->value;264if (nc != usage_dt.qty) {265// Doesn't appear on all of them, remove it.266usage.erase(E->key);267}268269E = next;270}271272int old_prop_size = remote_objects->prop_list.size();273274remote_objects->prop_list.clear();275int new_props_added = 0;276HashSet<String> changed;277for (KeyValue<String, UsageData> &KV : usage) {278const PropertyInfo &pinfo = KV.value.prop.first;279Variant var = KV.value.values[remote_objects->remote_object_ids[0]];280281if (pinfo.type == Variant::OBJECT && var.is_string()) {282String path = var;283if (path.contains("::")) {284// Built-in resource.285String base_path = path.get_slice("::", 0);286Ref<Resource> dependency = ResourceLoader::load(base_path);287if (dependency.is_valid()) {288remote_dependencies.insert(dependency);289}290}291var = ResourceLoader::load(path);292KV.value.values[remote_objects->remote_object_ids[0]] = var;293}294295// Always add the property, since props may have been added or removed.296remote_objects->prop_list.push_back(pinfo);297298if (!remote_objects->prop_values.has(pinfo.name)) {299new_props_added++;300} else if (bool(Variant::evaluate(Variant::OP_NOT_EQUAL, remote_objects->prop_values[pinfo.name], var))) {301changed.insert(pinfo.name);302}303304remote_objects->prop_values[pinfo.name] = KV.value.values;305}306307if (old_prop_size == remote_objects->prop_list.size() && new_props_added == 0) {308// Only some may have changed, if so, then update those, if they exist.309for (const String &E : changed) {310emit_signal(SNAME("object_property_updated"), remote_objects->get_instance_id(), E);311}312} else {313// Full update, because props were added or removed.314remote_objects->update();315}316317return remote_objects;318}319320void EditorDebuggerInspector::clear_remote_inspector() {321if (remote_objects_list.is_empty()) {322return;323}324325const Object *obj = InspectorDock::get_inspector_singleton()->get_edited_object();326// Check if the inspector holds remote items, and take it out if so.327if (Object::cast_to<EditorDebuggerRemoteObjects>(obj)) {328EditorNode::get_singleton()->push_item(nullptr);329}330}331332void EditorDebuggerInspector::clear_cache() {333clear_remote_inspector();334335for (EditorDebuggerRemoteObjects *robjs : remote_objects_list) {336memdelete(robjs);337}338remote_objects_list.clear();339340remote_dependencies.clear();341}342343void EditorDebuggerInspector::invalidate_selection_from_cache(const TypedArray<uint64_t> &p_ids) {344for (EditorDebuggerRemoteObjects *robjs : remote_objects_list) {345if (robjs->remote_object_ids == p_ids) {346const Object *obj = InspectorDock::get_inspector_singleton()->get_edited_object();347if (obj == robjs) {348EditorNode::get_singleton()->push_item(nullptr);349}350351remote_objects_list.erase(robjs);352memdelete(robjs);353break;354}355}356}357358void EditorDebuggerInspector::add_stack_variable(const Array &p_array, int p_offset) {359DebuggerMarshalls::ScriptStackVariable var;360var.deserialize(p_array);361String n = var.name;362Variant v = var.value;363364PropertyHint h = PROPERTY_HINT_NONE;365String hs;366367if (var.var_type == Variant::OBJECT && v) {368v = Object::cast_to<EncodedObjectAsID>(v)->get_object_id();369h = PROPERTY_HINT_OBJECT_ID;370hs = "Object";371}372String type;373switch (var.type) {374case 0:375type = "Locals/";376break;377case 1:378type = "Members/";379break;380case 2:381type = "Globals/";382break;383case 3:384type = "Evaluated/";385break;386default:387type = "Unknown/";388}389390PropertyInfo pinfo;391// Encode special characters to avoid issues with expressions in Evaluator.392// Dots are skipped by uri_encode(), but uri_decode() process them correctly when replaced with "%2E".393pinfo.name = type + n.uri_encode().replace(".", "%2E");394pinfo.type = v.get_type();395pinfo.hint = h;396pinfo.hint_string = hs;397398if ((p_offset == -1) || variables->prop_list.is_empty()) {399variables->prop_list.push_back(pinfo);400} else {401List<PropertyInfo>::Element *current = variables->prop_list.front();402for (int i = 0; i < p_offset; i++) {403current = current->next();404}405variables->prop_list.insert_before(current, pinfo);406}407variables->prop_values[pinfo.name][0] = v;408variables->update();409edit(variables);410}411412void EditorDebuggerInspector::clear_stack_variables() {413variables->clear();414variables->update();415}416417String EditorDebuggerInspector::get_stack_variable(const String &p_var) {418for (KeyValue<StringName, TypedDictionary<uint64_t, Variant>> &E : variables->prop_values) {419String v = E.key.operator String();420if (v.get_slicec('/', 1) == p_var) {421return variables->get_variant(v);422}423}424return String();425}426427428