editor_debugger_inspector.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /**************************************************************************/
  2. /* editor_debugger_inspector.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "editor_debugger_inspector.h"
  31. #include "core/debugger/debugger_marshalls.h"
  32. #include "core/io/marshalls.h"
  33. #include "editor/docks/inspector_dock.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "scene/debugger/scene_debugger.h"
  37. bool EditorDebuggerRemoteObjects::_set(const StringName &p_name, const Variant &p_value) {
  38. return _set_impl(p_name, p_value, "");
  39. }
  40. bool EditorDebuggerRemoteObjects::_set_impl(const StringName &p_name, const Variant &p_value, const String &p_field) {
  41. String name = p_name;
  42. if (!prop_values.has(name) || String(name).begins_with("Constants/")) {
  43. return false;
  44. }
  45. // Change it back to the real name when fetching.
  46. if (name == "Script") {
  47. name = "script";
  48. } else if (name.begins_with("Metadata/")) {
  49. name = name.replace_first("Metadata/", "metadata/");
  50. }
  51. Dictionary &values = prop_values[p_name];
  52. Dictionary old_values = values.duplicate();
  53. for (const uint64_t key : values.keys()) {
  54. values.set(key, p_value);
  55. }
  56. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  57. const int size = remote_object_ids.size();
  58. ur->create_action(size == 1 ? vformat(TTR("Set %s"), name) : vformat(TTR("Set %s on %d objects"), name, size), UndoRedo::MERGE_ENDS);
  59. ur->add_do_method(this, SNAME("emit_signal"), SNAME("values_edited"), name, values, p_field);
  60. ur->add_undo_method(this, SNAME("emit_signal"), SNAME("values_edited"), name, old_values, p_field);
  61. ur->commit_action();
  62. return true;
  63. }
  64. bool EditorDebuggerRemoteObjects::_get(const StringName &p_name, Variant &r_ret) const {
  65. String name = p_name;
  66. if (!prop_values.has(name)) {
  67. return false;
  68. }
  69. // Change it back to the real name when fetching.
  70. if (name == "Script") {
  71. name = "script";
  72. } else if (name.begins_with("Metadata/")) {
  73. name = name.replace_first("Metadata/", "metadata/");
  74. }
  75. r_ret = prop_values[p_name][remote_object_ids[0]];
  76. return true;
  77. }
  78. void EditorDebuggerRemoteObjects::_get_property_list(List<PropertyInfo> *p_list) const {
  79. p_list->clear(); // Sorry, don't want any categories.
  80. for (const PropertyInfo &prop : prop_list) {
  81. p_list->push_back(prop);
  82. }
  83. }
  84. void EditorDebuggerRemoteObjects::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {
  85. // Ignore the field with arrays and dictionaries, as they are passed whole when edited.
  86. Variant::Type type = p_value.get_type();
  87. if (type == Variant::ARRAY || type == Variant::DICTIONARY) {
  88. _set_impl(p_property, p_value, "");
  89. } else {
  90. _set_impl(p_property, p_value, p_field);
  91. }
  92. }
  93. String EditorDebuggerRemoteObjects::get_title() {
  94. if (!remote_object_ids.is_empty() && ObjectID(remote_object_ids[0].operator uint64_t()).is_valid()) {
  95. const int size = remote_object_ids.size();
  96. return size == 1 ? vformat(TTR("Remote %s: %d"), type_name, remote_object_ids[0]) : vformat(TTR("Remote %s (%d Selected)"), type_name, size);
  97. }
  98. return "<null>";
  99. }
  100. Variant EditorDebuggerRemoteObjects::get_variant(const StringName &p_name) {
  101. Variant var;
  102. _get(p_name, var);
  103. return var;
  104. }
  105. void EditorDebuggerRemoteObjects::_bind_methods() {
  106. ClassDB::bind_method(D_METHOD("get_title"), &EditorDebuggerRemoteObjects::get_title);
  107. ClassDB::bind_method("_hide_script_from_inspector", &EditorDebuggerRemoteObjects::_hide_script_from_inspector);
  108. ClassDB::bind_method("_hide_metadata_from_inspector", &EditorDebuggerRemoteObjects::_hide_metadata_from_inspector);
  109. ADD_SIGNAL(MethodInfo("values_edited", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::DICTIONARY, "values", PROPERTY_HINT_DICTIONARY_TYPE, "uint64_t:Variant"), PropertyInfo(Variant::STRING, "field")));
  110. }
  111. /// EditorDebuggerInspector
  112. EditorDebuggerInspector::EditorDebuggerInspector() {
  113. variables = memnew(EditorDebuggerRemoteObjects);
  114. }
  115. EditorDebuggerInspector::~EditorDebuggerInspector() {
  116. clear_cache();
  117. memdelete(variables);
  118. }
  119. void EditorDebuggerInspector::_bind_methods() {
  120. ADD_SIGNAL(MethodInfo("object_selected", PropertyInfo(Variant::INT, "id")));
  121. ADD_SIGNAL(MethodInfo("objects_edited", PropertyInfo(Variant::ARRAY, "ids"), PropertyInfo(Variant::STRING, "property"), PropertyInfo("value"), PropertyInfo(Variant::STRING, "field")));
  122. ADD_SIGNAL(MethodInfo("object_property_updated", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "property")));
  123. }
  124. void EditorDebuggerInspector::_notification(int p_what) {
  125. switch (p_what) {
  126. case NOTIFICATION_POSTINITIALIZE: {
  127. connect("object_id_selected", callable_mp(this, &EditorDebuggerInspector::_object_selected));
  128. } break;
  129. case NOTIFICATION_ENTER_TREE: {
  130. variables->remote_object_ids.append(0);
  131. edit(variables);
  132. } break;
  133. }
  134. }
  135. void EditorDebuggerInspector::_objects_edited(const String &p_prop, const TypedDictionary<uint64_t, Variant> &p_values, const String &p_field) {
  136. emit_signal(SNAME("objects_edited"), p_prop, p_values, p_field);
  137. }
  138. void EditorDebuggerInspector::_object_selected(ObjectID p_object) {
  139. emit_signal(SNAME("object_selected"), p_object);
  140. }
  141. EditorDebuggerRemoteObjects *EditorDebuggerInspector::set_objects(const Array &p_arr) {
  142. ERR_FAIL_COND_V(p_arr.is_empty(), nullptr);
  143. TypedArray<uint64_t> ids;
  144. LocalVector<SceneDebuggerObject> objects;
  145. for (const Array arr : p_arr) {
  146. SceneDebuggerObject obj;
  147. obj.deserialize(arr);
  148. if (obj.id.is_valid()) {
  149. ids.push_back((uint64_t)obj.id);
  150. objects.push_back(obj);
  151. }
  152. }
  153. ERR_FAIL_COND_V(ids.is_empty(), nullptr);
  154. // Sorting is necessary, as selected nodes in the remote tree are ordered by index.
  155. ids.sort();
  156. EditorDebuggerRemoteObjects *remote_objects = nullptr;
  157. for (EditorDebuggerRemoteObjects *robjs : remote_objects_list) {
  158. if (robjs->remote_object_ids == ids) {
  159. remote_objects = robjs;
  160. break;
  161. }
  162. }
  163. if (!remote_objects) {
  164. remote_objects = memnew(EditorDebuggerRemoteObjects);
  165. remote_objects->remote_object_ids = ids;
  166. remote_objects->remote_object_ids.make_read_only();
  167. remote_objects->connect("values_edited", callable_mp(this, &EditorDebuggerInspector::_objects_edited));
  168. remote_objects_list.push_back(remote_objects);
  169. }
  170. StringName class_name = objects[0].class_name;
  171. if (class_name != SNAME("Object")) {
  172. // Search for the common class between all selected objects.
  173. bool check_type_again = true;
  174. while (check_type_again) {
  175. check_type_again = false;
  176. if (class_name == SNAME("Object") || class_name == StringName()) {
  177. // All objects inherit from Object, so no need to continue checking.
  178. class_name = SNAME("Object");
  179. break;
  180. }
  181. // Check that all objects inherit from type_name.
  182. for (const SceneDebuggerObject &obj : objects) {
  183. if (obj.class_name == class_name || ClassDB::is_parent_class(obj.class_name, class_name)) {
  184. continue; // class_name is the same or a parent of the object's class.
  185. }
  186. // class_name is not a parent of the node's class, so check again with the parent class.
  187. class_name = ClassDB::get_parent_class(class_name);
  188. check_type_again = true;
  189. break;
  190. }
  191. }
  192. }
  193. remote_objects->type_name = class_name;
  194. // Search for properties that are present in all selected objects.
  195. struct UsageData {
  196. int qty = 0;
  197. SceneDebuggerObject::SceneDebuggerProperty prop;
  198. TypedDictionary<uint64_t, Variant> values;
  199. };
  200. HashMap<String, UsageData> usage;
  201. int nc = 0;
  202. for (const SceneDebuggerObject &obj : objects) {
  203. for (const SceneDebuggerObject::SceneDebuggerProperty &prop : obj.properties) {
  204. PropertyInfo pinfo = prop.first;
  205. // Rename those variables, so they don't conflict with the ones from the resource itself.
  206. if (pinfo.name == "script") {
  207. pinfo.name = "Script";
  208. } else if (pinfo.name.begins_with("metadata/")) {
  209. pinfo.name = pinfo.name.replace_first("metadata/", "Metadata/");
  210. }
  211. if (!usage.has(pinfo.name)) {
  212. UsageData usage_dt;
  213. usage_dt.prop = prop;
  214. usage_dt.prop.first.name = pinfo.name;
  215. usage_dt.values[obj.id] = prop.second;
  216. usage[pinfo.name] = usage_dt;
  217. }
  218. // Make sure only properties with the same exact PropertyInfo data will appear.
  219. if (usage[pinfo.name].prop.first == pinfo) {
  220. usage[pinfo.name].qty++;
  221. usage[pinfo.name].values[obj.id] = prop.second;
  222. }
  223. }
  224. nc++;
  225. }
  226. for (HashMap<String, UsageData>::Iterator E = usage.begin(); E;) {
  227. HashMap<String, UsageData>::Iterator next = E;
  228. ++next;
  229. UsageData usage_dt = E->value;
  230. if (nc != usage_dt.qty) {
  231. // Doesn't appear on all of them, remove it.
  232. usage.erase(E->key);
  233. }
  234. E = next;
  235. }
  236. int old_prop_size = remote_objects->prop_list.size();
  237. remote_objects->prop_list.clear();
  238. int new_props_added = 0;
  239. HashSet<String> changed;
  240. for (KeyValue<String, UsageData> &KV : usage) {
  241. const PropertyInfo &pinfo = KV.value.prop.first;
  242. Variant var = KV.value.values[remote_objects->remote_object_ids[0]];
  243. if (pinfo.type == Variant::OBJECT && var.is_string()) {
  244. String path = var;
  245. if (path.contains("::")) {
  246. // Built-in resource.
  247. String base_path = path.get_slice("::", 0);
  248. Ref<Resource> dependency = ResourceLoader::load(base_path);
  249. if (dependency.is_valid()) {
  250. remote_dependencies.insert(dependency);
  251. }
  252. }
  253. var = ResourceLoader::load(path);
  254. KV.value.values[remote_objects->remote_object_ids[0]] = var;
  255. }
  256. // Always add the property, since props may have been added or removed.
  257. remote_objects->prop_list.push_back(pinfo);
  258. if (!remote_objects->prop_values.has(pinfo.name)) {
  259. new_props_added++;
  260. } else if (bool(Variant::evaluate(Variant::OP_NOT_EQUAL, remote_objects->prop_values[pinfo.name], var))) {
  261. changed.insert(pinfo.name);
  262. }
  263. remote_objects->prop_values[pinfo.name] = KV.value.values;
  264. }
  265. if (old_prop_size == remote_objects->prop_list.size() && new_props_added == 0) {
  266. // Only some may have changed, if so, then update those, if they exist.
  267. for (const String &E : changed) {
  268. emit_signal(SNAME("object_property_updated"), remote_objects->get_instance_id(), E);
  269. }
  270. } else {
  271. // Full update, because props were added or removed.
  272. remote_objects->update();
  273. }
  274. return remote_objects;
  275. }
  276. void EditorDebuggerInspector::clear_remote_inspector() {
  277. if (remote_objects_list.is_empty()) {
  278. return;
  279. }
  280. const Object *obj = InspectorDock::get_inspector_singleton()->get_edited_object();
  281. // Check if the inspector holds remote items, and take it out if so.
  282. if (Object::cast_to<EditorDebuggerRemoteObjects>(obj)) {
  283. EditorNode::get_singleton()->push_item(nullptr);
  284. }
  285. }
  286. void EditorDebuggerInspector::clear_cache() {
  287. clear_remote_inspector();
  288. for (EditorDebuggerRemoteObjects *robjs : remote_objects_list) {
  289. memdelete(robjs);
  290. }
  291. remote_objects_list.clear();
  292. remote_dependencies.clear();
  293. }
  294. void EditorDebuggerInspector::invalidate_selection_from_cache(const TypedArray<uint64_t> &p_ids) {
  295. for (EditorDebuggerRemoteObjects *robjs : remote_objects_list) {
  296. if (robjs->remote_object_ids == p_ids) {
  297. const Object *obj = InspectorDock::get_inspector_singleton()->get_edited_object();
  298. if (obj == robjs) {
  299. EditorNode::get_singleton()->push_item(nullptr);
  300. }
  301. remote_objects_list.erase(robjs);
  302. memdelete(robjs);
  303. break;
  304. }
  305. }
  306. }
  307. void EditorDebuggerInspector::add_stack_variable(const Array &p_array, int p_offset) {
  308. DebuggerMarshalls::ScriptStackVariable var;
  309. var.deserialize(p_array);
  310. String n = var.name;
  311. Variant v = var.value;
  312. PropertyHint h = PROPERTY_HINT_NONE;
  313. String hs;
  314. if (var.var_type == Variant::OBJECT && v) {
  315. v = Object::cast_to<EncodedObjectAsID>(v)->get_object_id();
  316. h = PROPERTY_HINT_OBJECT_ID;
  317. hs = "Object";
  318. }
  319. String type;
  320. switch (var.type) {
  321. case 0:
  322. type = "Locals/";
  323. break;
  324. case 1:
  325. type = "Members/";
  326. break;
  327. case 2:
  328. type = "Globals/";
  329. break;
  330. case 3:
  331. type = "Evaluated/";
  332. break;
  333. default:
  334. type = "Unknown/";
  335. }
  336. PropertyInfo pinfo;
  337. // Encode special characters to avoid issues with expressions in Evaluator.
  338. // Dots are skipped by uri_encode(), but uri_decode() process them correctly when replaced with "%2E".
  339. pinfo.name = type + n.uri_encode().replace(".", "%2E");
  340. pinfo.type = v.get_type();
  341. pinfo.hint = h;
  342. pinfo.hint_string = hs;
  343. if ((p_offset == -1) || variables->prop_list.is_empty()) {
  344. variables->prop_list.push_back(pinfo);
  345. } else {
  346. List<PropertyInfo>::Element *current = variables->prop_list.front();
  347. for (int i = 0; i < p_offset; i++) {
  348. current = current->next();
  349. }
  350. variables->prop_list.insert_before(current, pinfo);
  351. }
  352. variables->prop_values[pinfo.name][0] = v;
  353. variables->update();
  354. edit(variables);
  355. }
  356. void EditorDebuggerInspector::clear_stack_variables() {
  357. variables->clear();
  358. variables->update();
  359. }
  360. String EditorDebuggerInspector::get_stack_variable(const String &p_var) {
  361. for (KeyValue<StringName, TypedDictionary<uint64_t, Variant>> &E : variables->prop_values) {
  362. String v = E.key.operator String();
  363. if (v.get_slicec('/', 1) == p_var) {
  364. return variables->get_variant(v);
  365. }
  366. }
  367. return String();
  368. }