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