inspector_dock.cpp 27 KB

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