inspector_dock.cpp 34 KB

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