inspector_dock.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /**************************************************************************/
  2. /* inspector_dock.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 "inspector_dock.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_scale.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/editor_undo_redo_manager.h"
  35. #include "editor/filesystem_dock.h"
  36. #include "editor/gui/editor_file_dialog.h"
  37. #include "editor/gui/editor_object_selector.h"
  38. #include "editor/plugins/script_editor_plugin.h"
  39. InspectorDock *InspectorDock::singleton = nullptr;
  40. void InspectorDock::_prepare_menu() {
  41. PopupMenu *menu = object_menu->get_popup();
  42. for (int i = EditorPropertyNameProcessor::STYLE_RAW; i <= EditorPropertyNameProcessor::STYLE_LOCALIZED; i++) {
  43. menu->set_item_checked(menu->get_item_index(PROPERTY_NAME_STYLE_RAW + i), i == property_name_style);
  44. }
  45. }
  46. void InspectorDock::_menu_option(int p_option) {
  47. _menu_option_confirm(p_option, false);
  48. }
  49. void InspectorDock::_menu_confirm_current() {
  50. _menu_option_confirm(current_option, true);
  51. }
  52. void InspectorDock::_menu_option_confirm(int p_option, bool p_confirmed) {
  53. if (!p_confirmed) {
  54. current_option = p_option;
  55. }
  56. switch (p_option) {
  57. case EXPAND_ALL: {
  58. _menu_expandall();
  59. } break;
  60. case COLLAPSE_ALL: {
  61. _menu_collapseall();
  62. } break;
  63. case EXPAND_REVERTABLE: {
  64. _menu_expand_revertable();
  65. } break;
  66. case RESOURCE_SAVE: {
  67. _save_resource(false);
  68. } break;
  69. case RESOURCE_SAVE_AS: {
  70. _save_resource(true);
  71. } break;
  72. case RESOURCE_MAKE_BUILT_IN: {
  73. _unref_resource();
  74. } break;
  75. case RESOURCE_COPY: {
  76. _copy_resource();
  77. } break;
  78. case RESOURCE_EDIT_CLIPBOARD: {
  79. _paste_resource();
  80. } break;
  81. case RESOURCE_SHOW_IN_FILESYSTEM: {
  82. Ref<Resource> current_res = _get_current_resource();
  83. ERR_FAIL_COND(current_res.is_null());
  84. FileSystemDock::get_singleton()->navigate_to_path(current_res->get_path());
  85. } break;
  86. case OBJECT_REQUEST_HELP: {
  87. if (current) {
  88. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  89. emit_signal(SNAME("request_help"), current->get_class());
  90. }
  91. } break;
  92. case OBJECT_COPY_PARAMS: {
  93. editor_data->apply_changes_in_editors();
  94. if (current) {
  95. editor_data->copy_object_params(current);
  96. }
  97. } break;
  98. case OBJECT_PASTE_PARAMS: {
  99. editor_data->apply_changes_in_editors();
  100. if (current) {
  101. editor_data->paste_object_params(current);
  102. }
  103. } break;
  104. case OBJECT_UNIQUE_RESOURCES: {
  105. if (!p_confirmed) {
  106. Vector<String> resource_propnames;
  107. if (current) {
  108. List<PropertyInfo> props;
  109. current->get_property_list(&props);
  110. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  111. if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
  112. continue;
  113. }
  114. Variant v = current->get(E->get().name);
  115. Ref<RefCounted> ref = v;
  116. Ref<Resource> res = ref;
  117. if (v.is_ref_counted() && ref.is_valid() && res.is_valid()) {
  118. // Valid resource which would be duplicated if action is confirmed.
  119. resource_propnames.append(E->get().name);
  120. }
  121. }
  122. }
  123. if (resource_propnames.size()) {
  124. unique_resources_list_tree->clear();
  125. TreeItem *root = unique_resources_list_tree->create_item();
  126. for (int i = 0; i < resource_propnames.size(); i++) {
  127. String propname = resource_propnames[i].replace("/", " / ");
  128. TreeItem *ti = unique_resources_list_tree->create_item(root);
  129. ti->set_text(0, bool(EDITOR_GET("interface/inspector/capitalize_properties")) ? propname.capitalize() : propname);
  130. }
  131. unique_resources_label->set_text(TTR("The following resources will be duplicated and embedded within this resource/object."));
  132. unique_resources_confirmation->popup_centered();
  133. } else {
  134. current_option = -1;
  135. unique_resources_label->set_text(TTR("This object has no resources."));
  136. unique_resources_confirmation->popup_centered();
  137. }
  138. } else {
  139. editor_data->apply_changes_in_editors();
  140. if (current) {
  141. List<PropertyInfo> props;
  142. current->get_property_list(&props);
  143. HashMap<Ref<Resource>, Ref<Resource>> duplicates;
  144. for (const PropertyInfo &prop_info : props) {
  145. if (!(prop_info.usage & PROPERTY_USAGE_STORAGE)) {
  146. continue;
  147. }
  148. Variant v = current->get(prop_info.name);
  149. if (v.is_ref_counted()) {
  150. Ref<RefCounted> ref = v;
  151. if (ref.is_valid()) {
  152. Ref<Resource> res = ref;
  153. if (res.is_valid()) {
  154. if (!duplicates.has(res)) {
  155. duplicates[res] = res->duplicate();
  156. }
  157. res = duplicates[res];
  158. current->set(prop_info.name, res);
  159. get_inspector_singleton()->update_property(prop_info.name);
  160. }
  161. }
  162. }
  163. }
  164. }
  165. int history_id = EditorUndoRedoManager::get_singleton()->get_history_id_for_object(current);
  166. EditorUndoRedoManager::get_singleton()->clear_history(true, history_id);
  167. EditorNode::get_singleton()->edit_item(current, inspector);
  168. }
  169. } break;
  170. case PROPERTY_NAME_STYLE_RAW:
  171. case PROPERTY_NAME_STYLE_CAPITALIZED:
  172. case PROPERTY_NAME_STYLE_LOCALIZED: {
  173. property_name_style = (EditorPropertyNameProcessor::Style)(p_option - PROPERTY_NAME_STYLE_RAW);
  174. inspector->set_property_name_style(property_name_style);
  175. } break;
  176. default: {
  177. if (p_option >= OBJECT_METHOD_BASE) {
  178. ERR_FAIL_COND(!current);
  179. int idx = p_option - OBJECT_METHOD_BASE;
  180. List<MethodInfo> methods;
  181. current->get_method_list(&methods);
  182. ERR_FAIL_INDEX(idx, methods.size());
  183. String name = methods[idx].name;
  184. current->call(name);
  185. }
  186. }
  187. }
  188. }
  189. void InspectorDock::_new_resource() {
  190. new_resource_dialog->popup_create(true);
  191. }
  192. void InspectorDock::_load_resource(const String &p_type) {
  193. load_resource_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  194. List<String> extensions;
  195. ResourceLoader::get_recognized_extensions_for_type(p_type, &extensions);
  196. load_resource_dialog->clear_filters();
  197. for (int i = 0; i < extensions.size(); i++) {
  198. load_resource_dialog->add_filter("*." + extensions[i], extensions[i].to_upper());
  199. }
  200. const Vector<String> textfile_ext = ((String)(EDITOR_GET("docks/filesystem/textfile_extensions"))).split(",", false);
  201. for (int i = 0; i < textfile_ext.size(); i++) {
  202. load_resource_dialog->add_filter("*." + textfile_ext[i], textfile_ext[i].to_upper());
  203. }
  204. load_resource_dialog->popup_file_dialog();
  205. }
  206. void InspectorDock::_resource_file_selected(String p_file) {
  207. Ref<Resource> res;
  208. if (ResourceLoader::exists(p_file, "")) {
  209. res = ResourceLoader::load(p_file);
  210. } else {
  211. const Vector<String> textfile_ext = ((String)(EDITOR_GET("docks/filesystem/textfile_extensions"))).split(",", false);
  212. if (textfile_ext.has(p_file.get_extension())) {
  213. res = ScriptEditor::get_singleton()->open_file(p_file);
  214. }
  215. }
  216. if (res.is_null()) {
  217. info_dialog->set_text(TTR("Failed to load resource."));
  218. return;
  219. };
  220. EditorNode::get_singleton()->push_item(res.operator->());
  221. }
  222. void InspectorDock::_save_resource(bool save_as) {
  223. Ref<Resource> current_res = _get_current_resource();
  224. ERR_FAIL_COND(current_res.is_null());
  225. if (save_as) {
  226. EditorNode::get_singleton()->save_resource_as(current_res);
  227. } else {
  228. EditorNode::get_singleton()->save_resource(current_res);
  229. }
  230. }
  231. void InspectorDock::_unref_resource() {
  232. Ref<Resource> current_res = _get_current_resource();
  233. ERR_FAIL_COND(current_res.is_null());
  234. current_res->set_path("");
  235. EditorNode::get_singleton()->edit_current();
  236. }
  237. void InspectorDock::_copy_resource() {
  238. Ref<Resource> current_res = _get_current_resource();
  239. ERR_FAIL_COND(current_res.is_null());
  240. EditorSettings::get_singleton()->set_resource_clipboard(current_res);
  241. }
  242. void InspectorDock::_paste_resource() {
  243. Ref<Resource> r = EditorSettings::get_singleton()->get_resource_clipboard();
  244. if (r.is_valid()) {
  245. EditorNode::get_singleton()->push_item(EditorSettings::get_singleton()->get_resource_clipboard().ptr(), String());
  246. }
  247. }
  248. void InspectorDock::_prepare_resource_extra_popup() {
  249. Ref<Resource> r = EditorSettings::get_singleton()->get_resource_clipboard();
  250. PopupMenu *popup = resource_extra_button->get_popup();
  251. popup->set_item_disabled(popup->get_item_index(RESOURCE_EDIT_CLIPBOARD), r.is_null());
  252. Ref<Resource> current_res = _get_current_resource();
  253. popup->set_item_disabled(popup->get_item_index(RESOURCE_SHOW_IN_FILESYSTEM), current_res.is_null() || current_res->is_built_in());
  254. }
  255. Ref<Resource> InspectorDock::_get_current_resource() const {
  256. ObjectID current_id = EditorNode::get_singleton()->get_editor_selection_history()->get_current();
  257. Object *current_obj = current_id.is_valid() ? ObjectDB::get_instance(current_id) : nullptr;
  258. return Ref<Resource>(Object::cast_to<Resource>(current_obj));
  259. }
  260. void InspectorDock::_prepare_history() {
  261. EditorSelectionHistory *editor_history = EditorNode::get_singleton()->get_editor_selection_history();
  262. int history_to = MAX(0, editor_history->get_history_len() - 25);
  263. history_menu->get_popup()->clear();
  264. HashSet<ObjectID> already;
  265. for (int i = editor_history->get_history_len() - 1; i >= history_to; i--) {
  266. ObjectID id = editor_history->get_history_obj(i);
  267. Object *obj = ObjectDB::get_instance(id);
  268. if (!obj || already.has(id)) {
  269. if (history_to > 0) {
  270. history_to--;
  271. }
  272. continue;
  273. }
  274. already.insert(id);
  275. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(obj, "Object");
  276. String text;
  277. if (obj->has_method("_get_editor_name")) {
  278. text = obj->call("_get_editor_name");
  279. } else if (Object::cast_to<Resource>(obj)) {
  280. Resource *r = Object::cast_to<Resource>(obj);
  281. if (r->get_path().is_resource_file()) {
  282. text = r->get_path().get_file();
  283. } else if (!r->get_name().is_empty()) {
  284. text = r->get_name();
  285. } else {
  286. text = r->get_class();
  287. }
  288. } else if (Object::cast_to<Node>(obj)) {
  289. text = Object::cast_to<Node>(obj)->get_name();
  290. } else if (obj->is_class("EditorDebuggerRemoteObject")) {
  291. text = obj->call("get_title");
  292. } else {
  293. text = obj->get_class();
  294. }
  295. if (i == editor_history->get_history_pos() && current) {
  296. text += " " + TTR("(Current)");
  297. }
  298. history_menu->get_popup()->add_icon_item(icon, text, i);
  299. }
  300. }
  301. void InspectorDock::_select_history(int p_idx) {
  302. // Push it to the top, it is not correct, but it's more useful.
  303. ObjectID id = EditorNode::get_singleton()->get_editor_selection_history()->get_history_obj(p_idx);
  304. Object *obj = ObjectDB::get_instance(id);
  305. if (!obj) {
  306. return;
  307. }
  308. EditorNode::get_singleton()->push_item(obj);
  309. }
  310. void InspectorDock::_resource_created() {
  311. Variant c = new_resource_dialog->instantiate_selected();
  312. ERR_FAIL_COND(!c);
  313. Resource *r = Object::cast_to<Resource>(c);
  314. ERR_FAIL_COND(!r);
  315. EditorNode::get_singleton()->push_item(r);
  316. }
  317. void InspectorDock::_resource_selected(const Ref<Resource> &p_res, const String &p_property) {
  318. if (p_res.is_null()) {
  319. return;
  320. }
  321. Ref<Resource> r = p_res;
  322. EditorNode::get_singleton()->push_item(r.operator->(), p_property);
  323. }
  324. void InspectorDock::_edit_forward() {
  325. if (EditorNode::get_singleton()->get_editor_selection_history()->next()) {
  326. EditorNode::get_singleton()->edit_current();
  327. }
  328. }
  329. void InspectorDock::_edit_back() {
  330. EditorSelectionHistory *editor_history = EditorNode::get_singleton()->get_editor_selection_history();
  331. if ((current && editor_history->previous()) || editor_history->get_path_size() == 1) {
  332. EditorNode::get_singleton()->edit_current();
  333. }
  334. }
  335. void InspectorDock::_menu_collapseall() {
  336. inspector->collapse_all_folding();
  337. }
  338. void InspectorDock::_menu_expandall() {
  339. inspector->expand_all_folding();
  340. }
  341. void InspectorDock::_menu_expand_revertable() {
  342. inspector->expand_revertable();
  343. }
  344. void InspectorDock::_info_pressed() {
  345. info_dialog->popup_centered();
  346. }
  347. Container *InspectorDock::get_addon_area() {
  348. return this;
  349. }
  350. void InspectorDock::_notification(int p_what) {
  351. switch (p_what) {
  352. case NOTIFICATION_THEME_CHANGED:
  353. case NOTIFICATION_TRANSLATION_CHANGED:
  354. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  355. resource_new_button->set_icon(get_theme_icon(SNAME("New"), SNAME("EditorIcons")));
  356. resource_load_button->set_icon(get_theme_icon(SNAME("Load"), SNAME("EditorIcons")));
  357. resource_save_button->set_icon(get_theme_icon(SNAME("Save"), SNAME("EditorIcons")));
  358. resource_extra_button->set_icon(get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons")));
  359. open_docs_button->set_icon(get_theme_icon(SNAME("HelpSearch"), SNAME("EditorIcons")));
  360. PopupMenu *resource_extra_popup = resource_extra_button->get_popup();
  361. resource_extra_popup->set_item_icon(resource_extra_popup->get_item_index(RESOURCE_EDIT_CLIPBOARD), get_theme_icon(SNAME("ActionPaste"), SNAME("EditorIcons")));
  362. resource_extra_popup->set_item_icon(resource_extra_popup->get_item_index(RESOURCE_COPY), get_theme_icon(SNAME("ActionCopy"), SNAME("EditorIcons")));
  363. if (is_layout_rtl()) {
  364. backward_button->set_icon(get_theme_icon(SNAME("Forward"), SNAME("EditorIcons")));
  365. forward_button->set_icon(get_theme_icon(SNAME("Back"), SNAME("EditorIcons")));
  366. } else {
  367. backward_button->set_icon(get_theme_icon(SNAME("Back"), SNAME("EditorIcons")));
  368. forward_button->set_icon(get_theme_icon(SNAME("Forward"), SNAME("EditorIcons")));
  369. }
  370. history_menu->set_icon(get_theme_icon(SNAME("History"), SNAME("EditorIcons")));
  371. object_menu->set_icon(get_theme_icon(SNAME("Tools"), SNAME("EditorIcons")));
  372. search->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  373. if (info_is_warning) {
  374. info->set_icon(get_theme_icon(SNAME("NodeWarning"), SNAME("EditorIcons")));
  375. info->add_theme_color_override("font_color", get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  376. } else {
  377. info->set_icon(get_theme_icon(SNAME("NodeInfo"), SNAME("EditorIcons")));
  378. info->add_theme_color_override("font_color", get_theme_color(SNAME("font_color"), SNAME("Editor")));
  379. }
  380. } break;
  381. }
  382. }
  383. void InspectorDock::_bind_methods() {
  384. ClassDB::bind_method("_unref_resource", &InspectorDock::_unref_resource);
  385. ClassDB::bind_method("_paste_resource", &InspectorDock::_paste_resource);
  386. ClassDB::bind_method("_copy_resource", &InspectorDock::_copy_resource);
  387. ClassDB::bind_method("_menu_collapseall", &InspectorDock::_menu_collapseall);
  388. ClassDB::bind_method("_menu_expandall", &InspectorDock::_menu_expandall);
  389. ClassDB::bind_method("edit_resource", &InspectorDock::edit_resource);
  390. ClassDB::bind_method("store_script_properties", &InspectorDock::store_script_properties);
  391. ClassDB::bind_method("apply_script_properties", &InspectorDock::apply_script_properties);
  392. ADD_SIGNAL(MethodInfo("request_help"));
  393. }
  394. void InspectorDock::edit_resource(const Ref<Resource> &p_resource) {
  395. _resource_selected(p_resource, "");
  396. }
  397. void InspectorDock::open_resource(const String &p_type) {
  398. _load_resource(p_type);
  399. }
  400. void InspectorDock::set_info(const String &p_button_text, const String &p_message, bool p_is_warning) {
  401. info->hide();
  402. info_is_warning = p_is_warning;
  403. if (info_is_warning) {
  404. info->set_icon(get_theme_icon(SNAME("NodeWarning"), SNAME("EditorIcons")));
  405. info->add_theme_color_override("font_color", get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  406. } else {
  407. info->set_icon(get_theme_icon(SNAME("NodeInfo"), SNAME("EditorIcons")));
  408. info->add_theme_color_override("font_color", get_theme_color(SNAME("font_color"), SNAME("Editor")));
  409. }
  410. if (!p_button_text.is_empty() && !p_message.is_empty()) {
  411. info->show();
  412. info->set_text(p_button_text);
  413. info_dialog->set_text(p_message);
  414. }
  415. }
  416. void InspectorDock::clear() {
  417. }
  418. void InspectorDock::update(Object *p_object) {
  419. EditorSelectionHistory *editor_history = EditorNode::get_singleton()->get_editor_selection_history();
  420. backward_button->set_disabled(editor_history->is_at_beginning());
  421. forward_button->set_disabled(editor_history->is_at_end());
  422. history_menu->set_disabled(true);
  423. if (editor_history->get_history_len() > 0) {
  424. history_menu->set_disabled(false);
  425. }
  426. object_selector->update_path();
  427. current = p_object;
  428. const bool is_object = p_object != nullptr;
  429. const bool is_resource = is_object && p_object->is_class("Resource");
  430. const bool is_text_file = is_object && p_object->is_class("TextFile");
  431. const bool is_node = is_object && p_object->is_class("Node");
  432. object_menu->set_disabled(!is_object || is_text_file);
  433. search->set_editable(is_object && !is_text_file);
  434. resource_save_button->set_disabled(!is_resource || is_text_file);
  435. open_docs_button->set_disabled(is_text_file || (!is_resource && !is_node));
  436. PopupMenu *resource_extra_popup = resource_extra_button->get_popup();
  437. resource_extra_popup->set_item_disabled(resource_extra_popup->get_item_index(RESOURCE_COPY), !is_resource || is_text_file);
  438. resource_extra_popup->set_item_disabled(resource_extra_popup->get_item_index(RESOURCE_MAKE_BUILT_IN), !is_resource || is_text_file);
  439. if (!is_object || is_text_file) {
  440. info->hide();
  441. object_selector->clear_path();
  442. return;
  443. }
  444. object_selector->enable_path();
  445. PopupMenu *p = object_menu->get_popup();
  446. p->clear();
  447. p->add_icon_shortcut(get_theme_icon(SNAME("GuiTreeArrowDown"), SNAME("EditorIcons")), ED_SHORTCUT("property_editor/expand_all", TTR("Expand All")), EXPAND_ALL);
  448. p->add_icon_shortcut(get_theme_icon(SNAME("GuiTreeArrowRight"), SNAME("EditorIcons")), ED_SHORTCUT("property_editor/collapse_all", TTR("Collapse All")), COLLAPSE_ALL);
  449. // Calling it 'revertable' internally, because that's what the implementation is based on, but labeling it as 'non-default' because that's more user friendly, even if not 100% accurate.
  450. p->add_shortcut(ED_SHORTCUT("property_editor/expand_revertable", TTR("Expand Non-Default")), EXPAND_REVERTABLE);
  451. p->add_separator(TTR("Property Name Style"));
  452. p->add_radio_check_item(TTR("Raw"), PROPERTY_NAME_STYLE_RAW);
  453. p->add_radio_check_item(TTR("Capitalized"), PROPERTY_NAME_STYLE_CAPITALIZED);
  454. p->add_radio_check_item(TTR("Localized"), PROPERTY_NAME_STYLE_LOCALIZED);
  455. if (!EditorPropertyNameProcessor::is_localization_available()) {
  456. const int index = p->get_item_index(PROPERTY_NAME_STYLE_LOCALIZED);
  457. p->set_item_disabled(index, true);
  458. p->set_item_tooltip(index, TTR("Localization not available for current language."));
  459. }
  460. p->add_separator();
  461. p->add_shortcut(ED_SHORTCUT("property_editor/copy_params", TTR("Copy Properties")), OBJECT_COPY_PARAMS);
  462. p->add_shortcut(ED_SHORTCUT("property_editor/paste_params", TTR("Paste Properties")), OBJECT_PASTE_PARAMS);
  463. if (is_resource || is_node) {
  464. p->add_separator();
  465. p->add_shortcut(ED_SHORTCUT("property_editor/make_subresources_unique", TTR("Make Sub-Resources Unique")), OBJECT_UNIQUE_RESOURCES);
  466. }
  467. List<MethodInfo> methods;
  468. p_object->get_method_list(&methods);
  469. if (!methods.is_empty()) {
  470. bool found = false;
  471. List<MethodInfo>::Element *I = methods.front();
  472. int i = 0;
  473. while (I) {
  474. if (I->get().flags & METHOD_FLAG_EDITOR) {
  475. if (!found) {
  476. p->add_separator();
  477. found = true;
  478. }
  479. p->add_item(I->get().name.capitalize(), OBJECT_METHOD_BASE + i);
  480. }
  481. i++;
  482. I = I->next();
  483. }
  484. }
  485. }
  486. void InspectorDock::go_back() {
  487. _edit_back();
  488. }
  489. EditorPropertyNameProcessor::Style InspectorDock::get_property_name_style() const {
  490. return property_name_style;
  491. }
  492. void InspectorDock::store_script_properties(Object *p_object) {
  493. ERR_FAIL_NULL(p_object);
  494. ScriptInstance *si = p_object->get_script_instance();
  495. if (!si) {
  496. return;
  497. }
  498. si->get_property_state(stored_properties);
  499. }
  500. void InspectorDock::apply_script_properties(Object *p_object) {
  501. ERR_FAIL_NULL(p_object);
  502. ScriptInstance *si = p_object->get_script_instance();
  503. if (!si) {
  504. return;
  505. }
  506. for (const Pair<StringName, Variant> &E : stored_properties) {
  507. Variant current_prop;
  508. if (si->get(E.first, current_prop) && current_prop.get_type() == E.second.get_type()) {
  509. si->set(E.first, E.second);
  510. }
  511. }
  512. stored_properties.clear();
  513. }
  514. InspectorDock::InspectorDock(EditorData &p_editor_data) {
  515. singleton = this;
  516. set_name("Inspector");
  517. editor_data = &p_editor_data;
  518. property_name_style = EditorPropertyNameProcessor::get_default_inspector_style();
  519. HBoxContainer *general_options_hb = memnew(HBoxContainer);
  520. add_child(general_options_hb);
  521. resource_new_button = memnew(Button);
  522. resource_new_button->set_flat(true);
  523. resource_new_button->set_tooltip_text(TTR("Create a new resource in memory and edit it."));
  524. general_options_hb->add_child(resource_new_button);
  525. resource_new_button->connect("pressed", callable_mp(this, &InspectorDock::_new_resource));
  526. resource_new_button->set_focus_mode(Control::FOCUS_NONE);
  527. resource_load_button = memnew(Button);
  528. resource_load_button->set_flat(true);
  529. resource_load_button->set_tooltip_text(TTR("Load an existing resource from disk and edit it."));
  530. general_options_hb->add_child(resource_load_button);
  531. resource_load_button->connect("pressed", callable_mp(this, &InspectorDock::_open_resource_selector));
  532. resource_load_button->set_focus_mode(Control::FOCUS_NONE);
  533. resource_save_button = memnew(MenuButton);
  534. resource_save_button->set_tooltip_text(TTR("Save the currently edited resource."));
  535. general_options_hb->add_child(resource_save_button);
  536. resource_save_button->get_popup()->add_item(TTR("Save"), RESOURCE_SAVE);
  537. resource_save_button->get_popup()->add_item(TTR("Save As..."), RESOURCE_SAVE_AS);
  538. resource_save_button->get_popup()->connect("id_pressed", callable_mp(this, &InspectorDock::_menu_option));
  539. resource_save_button->set_focus_mode(Control::FOCUS_NONE);
  540. resource_save_button->set_disabled(true);
  541. resource_extra_button = memnew(MenuButton);
  542. resource_extra_button->set_tooltip_text(TTR("Extra resource options."));
  543. general_options_hb->add_child(resource_extra_button);
  544. resource_extra_button->connect("about_to_popup", callable_mp(this, &InspectorDock::_prepare_resource_extra_popup));
  545. resource_extra_button->get_popup()->add_shortcut(ED_SHORTCUT("property_editor/paste_resource", TTR("Edit Resource from Clipboard")), RESOURCE_EDIT_CLIPBOARD);
  546. resource_extra_button->get_popup()->add_shortcut(ED_SHORTCUT("property_editor/copy_resource", TTR("Copy Resource")), RESOURCE_COPY);
  547. resource_extra_button->get_popup()->set_item_disabled(1, true);
  548. resource_extra_button->get_popup()->add_separator();
  549. resource_extra_button->get_popup()->add_shortcut(ED_SHORTCUT("property_editor/show_in_filesystem", TTR("Show in FileSystem")), RESOURCE_SHOW_IN_FILESYSTEM);
  550. resource_extra_button->get_popup()->add_shortcut(ED_SHORTCUT("property_editor/unref_resource", TTR("Make Resource Built-In")), RESOURCE_MAKE_BUILT_IN);
  551. resource_extra_button->get_popup()->set_item_disabled(3, true);
  552. resource_extra_button->get_popup()->connect("id_pressed", callable_mp(this, &InspectorDock::_menu_option));
  553. general_options_hb->add_spacer();
  554. backward_button = memnew(Button);
  555. backward_button->set_flat(true);
  556. general_options_hb->add_child(backward_button);
  557. backward_button->set_tooltip_text(TTR("Go to previous edited object in history."));
  558. backward_button->set_disabled(true);
  559. backward_button->connect("pressed", callable_mp(this, &InspectorDock::_edit_back));
  560. forward_button = memnew(Button);
  561. forward_button->set_flat(true);
  562. general_options_hb->add_child(forward_button);
  563. forward_button->set_tooltip_text(TTR("Go to next edited object in history."));
  564. forward_button->set_disabled(true);
  565. forward_button->connect("pressed", callable_mp(this, &InspectorDock::_edit_forward));
  566. history_menu = memnew(MenuButton);
  567. history_menu->set_tooltip_text(TTR("History of recently edited objects."));
  568. general_options_hb->add_child(history_menu);
  569. history_menu->connect("about_to_popup", callable_mp(this, &InspectorDock::_prepare_history));
  570. history_menu->get_popup()->connect("id_pressed", callable_mp(this, &InspectorDock::_select_history));
  571. HBoxContainer *subresource_hb = memnew(HBoxContainer);
  572. add_child(subresource_hb);
  573. object_selector = memnew(EditorObjectSelector(EditorNode::get_singleton()->get_editor_selection_history()));
  574. object_selector->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  575. subresource_hb->add_child(object_selector);
  576. open_docs_button = memnew(Button);
  577. open_docs_button->set_flat(true);
  578. open_docs_button->set_disabled(true);
  579. open_docs_button->set_tooltip_text(TTR("Open documentation for this object."));
  580. open_docs_button->set_shortcut(ED_SHORTCUT("property_editor/open_help", TTR("Open Documentation")));
  581. subresource_hb->add_child(open_docs_button);
  582. open_docs_button->connect("pressed", callable_mp(this, &InspectorDock::_menu_option).bind(OBJECT_REQUEST_HELP));
  583. new_resource_dialog = memnew(CreateDialog);
  584. EditorNode::get_singleton()->get_gui_base()->add_child(new_resource_dialog);
  585. new_resource_dialog->set_base_type("Resource");
  586. new_resource_dialog->connect("create", callable_mp(this, &InspectorDock::_resource_created));
  587. HBoxContainer *property_tools_hb = memnew(HBoxContainer);
  588. add_child(property_tools_hb);
  589. search = memnew(LineEdit);
  590. search->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  591. search->set_placeholder(TTR("Filter Properties"));
  592. search->set_clear_button_enabled(true);
  593. property_tools_hb->add_child(search);
  594. object_menu = memnew(MenuButton);
  595. object_menu->set_shortcut_context(this);
  596. property_tools_hb->add_child(object_menu);
  597. object_menu->set_tooltip_text(TTR("Manage object properties."));
  598. object_menu->get_popup()->connect("about_to_popup", callable_mp(this, &InspectorDock::_prepare_menu));
  599. object_menu->get_popup()->connect("id_pressed", callable_mp(this, &InspectorDock::_menu_option));
  600. info = memnew(Button);
  601. add_child(info);
  602. info->set_clip_text(true);
  603. info->hide();
  604. info->connect("pressed", callable_mp(this, &InspectorDock::_info_pressed));
  605. unique_resources_confirmation = memnew(ConfirmationDialog);
  606. add_child(unique_resources_confirmation);
  607. VBoxContainer *container = memnew(VBoxContainer);
  608. unique_resources_confirmation->add_child(container);
  609. unique_resources_label = memnew(Label);
  610. container->add_child(unique_resources_label);
  611. unique_resources_list_tree = memnew(Tree);
  612. unique_resources_list_tree->set_hide_root(true);
  613. unique_resources_list_tree->set_columns(1);
  614. unique_resources_list_tree->set_column_title(0, TTR("Property"));
  615. unique_resources_list_tree->set_custom_minimum_size(Size2(0, 200 * EDSCALE));
  616. container->add_child(unique_resources_list_tree);
  617. Label *bottom_label = memnew(Label);
  618. bottom_label->set_text(TTR("This cannot be undone. Are you sure?"));
  619. container->add_child(bottom_label);
  620. unique_resources_confirmation->connect("confirmed", callable_mp(this, &InspectorDock::_menu_confirm_current));
  621. info_dialog = memnew(AcceptDialog);
  622. EditorNode::get_singleton()->get_gui_base()->add_child(info_dialog);
  623. load_resource_dialog = memnew(EditorFileDialog);
  624. add_child(load_resource_dialog);
  625. load_resource_dialog->set_current_dir("res://");
  626. load_resource_dialog->connect("file_selected", callable_mp(this, &InspectorDock::_resource_file_selected));
  627. inspector = memnew(EditorInspector);
  628. add_child(inspector);
  629. inspector->set_autoclear(true);
  630. inspector->set_show_categories(true);
  631. inspector->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  632. inspector->set_use_doc_hints(true);
  633. inspector->set_hide_script(false);
  634. inspector->set_hide_metadata(false);
  635. inspector->set_use_settings_name_style(false);
  636. inspector->set_property_name_style(property_name_style);
  637. inspector->set_use_folding(!bool(EDITOR_GET("interface/inspector/disable_folding")));
  638. inspector->register_text_enter(search);
  639. inspector->set_use_filter(true); // TODO: check me
  640. inspector->connect("resource_selected", callable_mp(this, &InspectorDock::_resource_selected));
  641. }
  642. InspectorDock::~InspectorDock() {
  643. singleton = nullptr;
  644. }