editor_debugger_inspector.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/editor_node.h"
  34. #include "scene/debugger/scene_debugger.h"
  35. bool EditorDebuggerRemoteObject::_set(const StringName &p_name, const Variant &p_value) {
  36. if (!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. }
  82. EditorDebuggerInspector::~EditorDebuggerInspector() {
  83. clear_cache();
  84. memdelete(variables);
  85. }
  86. void EditorDebuggerInspector::_bind_methods() {
  87. ADD_SIGNAL(MethodInfo("object_selected", PropertyInfo(Variant::INT, "id")));
  88. ADD_SIGNAL(MethodInfo("object_edited", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "property"), PropertyInfo("value")));
  89. ADD_SIGNAL(MethodInfo("object_property_updated", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "property")));
  90. }
  91. void EditorDebuggerInspector::_notification(int p_what) {
  92. switch (p_what) {
  93. case NOTIFICATION_POSTINITIALIZE: {
  94. connect("object_id_selected", callable_mp(this, &EditorDebuggerInspector::_object_selected));
  95. } break;
  96. case NOTIFICATION_ENTER_TREE: {
  97. edit(variables);
  98. } break;
  99. }
  100. }
  101. void EditorDebuggerInspector::_object_edited(ObjectID p_id, const String &p_prop, const Variant &p_value) {
  102. emit_signal(SNAME("object_edited"), p_id, p_prop, p_value);
  103. }
  104. void EditorDebuggerInspector::_object_selected(ObjectID p_object) {
  105. emit_signal(SNAME("object_selected"), p_object);
  106. }
  107. ObjectID EditorDebuggerInspector::add_object(const Array &p_arr) {
  108. EditorDebuggerRemoteObject *debug_obj = nullptr;
  109. SceneDebuggerObject obj;
  110. obj.deserialize(p_arr);
  111. ERR_FAIL_COND_V(obj.id.is_null(), ObjectID());
  112. if (remote_objects.has(obj.id)) {
  113. debug_obj = remote_objects[obj.id];
  114. } else {
  115. debug_obj = memnew(EditorDebuggerRemoteObject);
  116. debug_obj->remote_object_id = obj.id;
  117. debug_obj->type_name = obj.class_name;
  118. remote_objects[obj.id] = debug_obj;
  119. debug_obj->connect("value_edited", callable_mp(this, &EditorDebuggerInspector::_object_edited));
  120. }
  121. int old_prop_size = debug_obj->prop_list.size();
  122. debug_obj->prop_list.clear();
  123. int new_props_added = 0;
  124. HashSet<String> changed;
  125. for (SceneDebuggerObject::SceneDebuggerProperty &property : obj.properties) {
  126. PropertyInfo &pinfo = property.first;
  127. Variant &var = property.second;
  128. if (pinfo.type == Variant::OBJECT) {
  129. if (var.is_string()) {
  130. String path = var;
  131. if (path.contains("::")) {
  132. // built-in resource
  133. String base_path = path.get_slice("::", 0);
  134. Ref<Resource> dependency = ResourceLoader::load(base_path);
  135. if (dependency.is_valid()) {
  136. remote_dependencies.insert(dependency);
  137. }
  138. }
  139. var = ResourceLoader::load(path);
  140. if (pinfo.hint_string == "Script") {
  141. if (debug_obj->get_script() != var) {
  142. debug_obj->set_script(Ref<RefCounted>());
  143. Ref<Script> scr(var);
  144. if (!scr.is_null()) {
  145. ScriptInstance *scr_instance = scr->placeholder_instance_create(debug_obj);
  146. if (scr_instance) {
  147. debug_obj->set_script_and_instance(var, scr_instance);
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }
  154. //always add the property, since props may have been added or removed
  155. debug_obj->prop_list.push_back(pinfo);
  156. if (!debug_obj->prop_values.has(pinfo.name)) {
  157. new_props_added++;
  158. debug_obj->prop_values[pinfo.name] = var;
  159. } else {
  160. if (bool(Variant::evaluate(Variant::OP_NOT_EQUAL, debug_obj->prop_values[pinfo.name], var))) {
  161. debug_obj->prop_values[pinfo.name] = var;
  162. changed.insert(pinfo.name);
  163. }
  164. }
  165. }
  166. if (old_prop_size == debug_obj->prop_list.size() && new_props_added == 0) {
  167. //only some may have changed, if so, then update those, if exist
  168. for (const String &E : changed) {
  169. emit_signal(SNAME("object_property_updated"), debug_obj->remote_object_id, E);
  170. }
  171. } else {
  172. //full update, because props were added or removed
  173. debug_obj->update();
  174. }
  175. return obj.id;
  176. }
  177. void EditorDebuggerInspector::clear_cache() {
  178. for (const KeyValue<ObjectID, EditorDebuggerRemoteObject *> &E : remote_objects) {
  179. EditorNode *editor = EditorNode::get_singleton();
  180. if (editor->get_editor_selection_history()->get_current() == E.value->get_instance_id()) {
  181. editor->push_item(nullptr);
  182. }
  183. memdelete(E.value);
  184. }
  185. remote_objects.clear();
  186. remote_dependencies.clear();
  187. }
  188. Object *EditorDebuggerInspector::get_object(ObjectID p_id) {
  189. if (remote_objects.has(p_id)) {
  190. return remote_objects[p_id];
  191. }
  192. return nullptr;
  193. }
  194. void EditorDebuggerInspector::add_stack_variable(const Array &p_array, int p_offset) {
  195. DebuggerMarshalls::ScriptStackVariable var;
  196. var.deserialize(p_array);
  197. String n = var.name;
  198. Variant v = var.value;
  199. PropertyHint h = PROPERTY_HINT_NONE;
  200. String hs;
  201. if (var.var_type == Variant::OBJECT && v) {
  202. v = Object::cast_to<EncodedObjectAsID>(v)->get_object_id();
  203. h = PROPERTY_HINT_OBJECT_ID;
  204. hs = "Object";
  205. }
  206. String type;
  207. switch (var.type) {
  208. case 0:
  209. type = "Locals/";
  210. break;
  211. case 1:
  212. type = "Members/";
  213. break;
  214. case 2:
  215. type = "Globals/";
  216. break;
  217. case 3:
  218. type = "Evaluated/";
  219. break;
  220. default:
  221. type = "Unknown/";
  222. }
  223. PropertyInfo pinfo;
  224. pinfo.name = type + n;
  225. pinfo.type = v.get_type();
  226. pinfo.hint = h;
  227. pinfo.hint_string = hs;
  228. if ((p_offset == -1) || variables->prop_list.is_empty()) {
  229. variables->prop_list.push_back(pinfo);
  230. } else {
  231. List<PropertyInfo>::Element *current = variables->prop_list.front();
  232. for (int i = 0; i < p_offset; i++) {
  233. current = current->next();
  234. }
  235. variables->prop_list.insert_before(current, pinfo);
  236. }
  237. variables->prop_values[type + n] = v;
  238. variables->update();
  239. edit(variables);
  240. // To prevent constantly resizing when using filtering.
  241. int size_x = get_size().x;
  242. if (size_x > get_custom_minimum_size().x) {
  243. set_custom_minimum_size(Size2(size_x, 0));
  244. }
  245. }
  246. void EditorDebuggerInspector::clear_stack_variables() {
  247. variables->clear();
  248. variables->update();
  249. set_custom_minimum_size(Size2(0, 0));
  250. }
  251. String EditorDebuggerInspector::get_stack_variable(const String &p_var) {
  252. for (KeyValue<StringName, Variant> &E : variables->prop_values) {
  253. String v = E.key.operator String();
  254. if (v.get_slice("/", 1) == p_var) {
  255. return variables->get_variant(v);
  256. }
  257. }
  258. return String();
  259. }