inspector_dock.cpp 30 KB

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