inspector_dock.cpp 30 KB

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