editor_debugger_inspector.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*************************************************************************/
  2. /* editor_debugger_inspector.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/editor_node.h"
  34. #include "scene/debugger/scene_debugger.h"
  35. bool EditorDebuggerRemoteObject::_set(const StringName &p_name, const Variant &p_value) {
  36. if (!editable || !prop_values.has(p_name) || String(p_name).begins_with("Constants/")) {
  37. return false;
  38. }
  39. prop_values[p_name] = p_value;
  40. emit_signal(SNAME("value_edited"), remote_object_id, p_name, p_value);
  41. return true;
  42. }
  43. bool EditorDebuggerRemoteObject::_get(const StringName &p_name, Variant &r_ret) const {
  44. if (!prop_values.has(p_name)) {
  45. return false;
  46. }
  47. r_ret = prop_values[p_name];
  48. return true;
  49. }
  50. void EditorDebuggerRemoteObject::_get_property_list(List<PropertyInfo> *p_list) const {
  51. p_list->clear(); // Sorry, no want category.
  52. for (const PropertyInfo &prop : prop_list) {
  53. if (prop.name == "script") {
  54. // Skip the script property, it's always added by the non-virtual method.
  55. continue;
  56. }
  57. p_list->push_back(prop);
  58. }
  59. }
  60. String EditorDebuggerRemoteObject::get_title() {
  61. if (remote_object_id.is_valid()) {
  62. return vformat(TTR("Remote %s:"), String(type_name)) + " " + itos(remote_object_id);
  63. } else {
  64. return "<null>";
  65. }
  66. }
  67. Variant EditorDebuggerRemoteObject::get_variant(const StringName &p_name) {
  68. Variant var;
  69. _get(p_name, var);
  70. return var;
  71. }
  72. void EditorDebuggerRemoteObject::_bind_methods() {
  73. ClassDB::bind_method(D_METHOD("get_title"), &EditorDebuggerRemoteObject::get_title);
  74. ClassDB::bind_method(D_METHOD("get_variant"), &EditorDebuggerRemoteObject::get_variant);
  75. ClassDB::bind_method(D_METHOD("clear"), &EditorDebuggerRemoteObject::clear);
  76. ClassDB::bind_method(D_METHOD("get_remote_object_id"), &EditorDebuggerRemoteObject::get_remote_object_id);
  77. ADD_SIGNAL(MethodInfo("value_edited", PropertyInfo(Variant::INT, "object_id"), PropertyInfo(Variant::STRING, "property"), PropertyInfo("value")));
  78. }
  79. EditorDebuggerInspector::EditorDebuggerInspector() {
  80. variables = memnew(EditorDebuggerRemoteObject);
  81. variables->editable = false;
  82. }
  83. EditorDebuggerInspector::~EditorDebuggerInspector() {
  84. clear_cache();
  85. memdelete(variables);
  86. }
  87. void EditorDebuggerInspector::_bind_methods() {
  88. ADD_SIGNAL(MethodInfo("object_selected", PropertyInfo(Variant::INT, "id")));
  89. ADD_SIGNAL(MethodInfo("object_edited", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "property"), PropertyInfo("value")));
  90. ADD_SIGNAL(MethodInfo("object_property_updated", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "property")));
  91. }
  92. void EditorDebuggerInspector::_notification(int p_what) {
  93. switch (p_what) {
  94. case NOTIFICATION_POSTINITIALIZE: {
  95. connect("object_id_selected", callable_mp(this, &EditorDebuggerInspector::_object_selected));
  96. } break;
  97. case NOTIFICATION_ENTER_TREE: {
  98. edit(variables);
  99. } break;
  100. }
  101. }
  102. void EditorDebuggerInspector::_object_edited(ObjectID p_id, const String &p_prop, const Variant &p_value) {
  103. emit_signal(SNAME("object_edited"), p_id, p_prop, p_value);
  104. }
  105. void EditorDebuggerInspector::_object_selected(ObjectID p_object) {
  106. emit_signal(SNAME("object_selected"), p_object);
  107. }
  108. ObjectID EditorDebuggerInspector::add_object(const Array &p_arr) {
  109. EditorDebuggerRemoteObject *debug_obj = nullptr;
  110. SceneDebuggerObject obj;
  111. obj.deserialize(p_arr);
  112. ERR_FAIL_COND_V(obj.id.is_null(), ObjectID());
  113. if (remote_objects.has(obj.id)) {
  114. debug_obj = remote_objects[obj.id];
  115. } else {
  116. debug_obj = memnew(EditorDebuggerRemoteObject);
  117. debug_obj->remote_object_id = obj.id;
  118. debug_obj->type_name = obj.class_name;
  119. remote_objects[obj.id] = debug_obj;
  120. debug_obj->connect("value_edited", callable_mp(this, &EditorDebuggerInspector::_object_edited));
  121. }
  122. int old_prop_size = debug_obj->prop_list.size();
  123. debug_obj->prop_list.clear();
  124. int new_props_added = 0;
  125. HashSet<String> changed;
  126. for (int i = 0; i < obj.properties.size(); i++) {
  127. PropertyInfo &pinfo = obj.properties[i].first;
  128. Variant &var = obj.properties[i].second;
  129. if (pinfo.type == Variant::OBJECT) {
  130. if (var.get_type() == Variant::STRING) {
  131. String path = var;
  132. if (path.contains("::")) {
  133. // built-in resource
  134. String base_path = path.get_slice("::", 0);
  135. Ref<Resource> dependency = ResourceLoader::load(base_path);
  136. if (dependency.is_valid()) {
  137. remote_dependencies.insert(dependency);
  138. }
  139. }
  140. var = ResourceLoader::load(path);
  141. if (pinfo.hint_string == "Script") {
  142. if (debug_obj->get_script() != var) {
  143. debug_obj->set_script(Ref<RefCounted>());
  144. Ref<Script> script(var);
  145. if (!script.is_null()) {
  146. ScriptInstance *script_instance = script->placeholder_instance_create(debug_obj);
  147. if (script_instance) {
  148. debug_obj->set_script_and_instance(var, script_instance);
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. //always add the property, since props may have been added or removed
  156. debug_obj->prop_list.push_back(pinfo);
  157. if (!debug_obj->prop_values.has(pinfo.name)) {
  158. new_props_added++;
  159. debug_obj->prop_values[pinfo.name] = var;
  160. } else {
  161. if (bool(Variant::evaluate(Variant::OP_NOT_EQUAL, debug_obj->prop_values[pinfo.name], var))) {
  162. debug_obj->prop_values[pinfo.name] = var;
  163. changed.insert(pinfo.name);
  164. }
  165. }
  166. }
  167. if (old_prop_size == debug_obj->prop_list.size() && new_props_added == 0) {
  168. //only some may have changed, if so, then update those, if exist
  169. for (const String &E : changed) {
  170. emit_signal(SNAME("object_property_updated"), debug_obj->remote_object_id, E);
  171. }
  172. } else {
  173. //full update, because props were added or removed
  174. debug_obj->update();
  175. }
  176. return obj.id;
  177. }
  178. void EditorDebuggerInspector::clear_cache() {
  179. for (const KeyValue<ObjectID, EditorDebuggerRemoteObject *> &E : remote_objects) {
  180. EditorNode *editor = EditorNode::get_singleton();
  181. if (editor->get_editor_selection_history()->get_current() == E.value->get_instance_id()) {
  182. editor->push_item(nullptr);
  183. }
  184. memdelete(E.value);
  185. }
  186. remote_objects.clear();
  187. remote_dependencies.clear();
  188. }
  189. Object *EditorDebuggerInspector::get_object(ObjectID p_id) {
  190. if (remote_objects.has(p_id)) {
  191. return remote_objects[p_id];
  192. }
  193. return nullptr;
  194. }
  195. void EditorDebuggerInspector::add_stack_variable(const Array &p_array) {
  196. DebuggerMarshalls::ScriptStackVariable var;
  197. var.deserialize(p_array);
  198. String n = var.name;
  199. Variant v = var.value;
  200. PropertyHint h = PROPERTY_HINT_NONE;
  201. String hs = String();
  202. if (v.get_type() == Variant::OBJECT) {
  203. v = Object::cast_to<EncodedObjectAsID>(v)->get_object_id();
  204. h = PROPERTY_HINT_OBJECT_ID;
  205. hs = "Object";
  206. }
  207. String type;
  208. switch (var.type) {
  209. case 0:
  210. type = "Locals/";
  211. break;
  212. case 1:
  213. type = "Members/";
  214. break;
  215. case 2:
  216. type = "Globals/";
  217. break;
  218. default:
  219. type = "Unknown/";
  220. }
  221. PropertyInfo pinfo;
  222. pinfo.name = type + n;
  223. pinfo.type = v.get_type();
  224. pinfo.hint = h;
  225. pinfo.hint_string = hs;
  226. variables->prop_list.push_back(pinfo);
  227. variables->prop_values[type + n] = v;
  228. variables->update();
  229. edit(variables);
  230. // To prevent constantly resizing when using filtering.
  231. int size_x = get_size().x;
  232. if (size_x > get_custom_minimum_size().x) {
  233. set_custom_minimum_size(Size2(size_x, 0));
  234. }
  235. }
  236. void EditorDebuggerInspector::clear_stack_variables() {
  237. variables->clear();
  238. variables->update();
  239. set_custom_minimum_size(Size2(0, 0));
  240. }
  241. String EditorDebuggerInspector::get_stack_variable(const String &p_var) {
  242. for (KeyValue<StringName, Variant> &E : variables->prop_values) {
  243. String v = E.key.operator String();
  244. if (v.get_slice("/", 1) == p_var) {
  245. return variables->get_variant(v);
  246. }
  247. }
  248. return String();
  249. }