inspector_dock.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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/editor_main_screen.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/editor_string_names.h"
  37. #include "editor/editor_undo_redo_manager.h"
  38. #include "editor/filesystem_dock.h"
  39. #include "editor/gui/editor_file_dialog.h"
  40. #include "editor/gui/editor_object_selector.h"
  41. #include "editor/plugins/script_editor_plugin.h"
  42. #include "editor/themes/editor_scale.h"
  43. InspectorDock *InspectorDock::singleton = nullptr;
  44. void InspectorDock::_prepare_menu() {
  45. PopupMenu *menu = object_menu->get_popup();
  46. for (int i = EditorPropertyNameProcessor::STYLE_RAW; i <= EditorPropertyNameProcessor::STYLE_LOCALIZED; i++) {
  47. menu->set_item_checked(menu->get_item_index(PROPERTY_NAME_STYLE_RAW + i), i == property_name_style);
  48. }
  49. }
  50. void InspectorDock::_menu_option(int p_option) {
  51. _menu_option_confirm(p_option, false);
  52. }
  53. void InspectorDock::_menu_confirm_current() {
  54. _menu_option_confirm(current_option, true);
  55. }
  56. void InspectorDock::_menu_option_confirm(int p_option, bool p_confirmed) {
  57. if (!p_confirmed) {
  58. current_option = p_option;
  59. }
  60. switch (p_option) {
  61. case EXPAND_ALL: {
  62. _menu_expandall();
  63. } break;
  64. case COLLAPSE_ALL: {
  65. _menu_collapseall();
  66. } break;
  67. case EXPAND_REVERTABLE: {
  68. _menu_expand_revertable();
  69. } break;
  70. case RESOURCE_SAVE: {
  71. _save_resource(false);
  72. } break;
  73. case RESOURCE_SAVE_AS: {
  74. _save_resource(true);
  75. } break;
  76. case RESOURCE_MAKE_BUILT_IN: {
  77. _unref_resource();
  78. } break;
  79. case RESOURCE_COPY: {
  80. _copy_resource();
  81. } break;
  82. case RESOURCE_EDIT_CLIPBOARD: {
  83. _paste_resource();
  84. } break;
  85. case RESOURCE_SHOW_IN_FILESYSTEM: {
  86. Ref<Resource> current_res = _get_current_resource();
  87. ERR_FAIL_COND(current_res.is_null());
  88. FileSystemDock::get_singleton()->navigate_to_path(current_res->get_path());
  89. } break;
  90. case OBJECT_REQUEST_HELP: {
  91. if (current) {
  92. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
  93. emit_signal(SNAME("request_help"), current->get_class());
  94. }
  95. } break;
  96. case OBJECT_COPY_PARAMS: {
  97. editor_data->apply_changes_in_editors();
  98. if (current) {
  99. editor_data->copy_object_params(current);
  100. }
  101. } break;
  102. case OBJECT_PASTE_PARAMS: {
  103. editor_data->apply_changes_in_editors();
  104. if (current) {
  105. editor_data->paste_object_params(current);
  106. }
  107. } break;
  108. case OBJECT_UNIQUE_RESOURCES: {
  109. if (!p_confirmed) {
  110. Vector<String> resource_propnames;
  111. if (current) {
  112. List<PropertyInfo> props;
  113. current->get_property_list(&props);
  114. for (const PropertyInfo &property : props) {
  115. if (!(property.usage & PROPERTY_USAGE_STORAGE)) {
  116. continue;
  117. }
  118. if (property.usage & PROPERTY_USAGE_NEVER_DUPLICATE) {
  119. continue;
  120. }
  121. Variant v = current->get(property.name);
  122. Ref<RefCounted> ref = v;
  123. Ref<Resource> res = ref;
  124. if (v.is_ref_counted() && ref.is_valid() && res.is_valid()) {
  125. // Valid resource which would be duplicated if action is confirmed.
  126. resource_propnames.append(property.name);
  127. }
  128. }
  129. }
  130. unique_resources_list_tree->clear();
  131. if (resource_propnames.size()) {
  132. const EditorPropertyNameProcessor::Style name_style = inspector->get_property_name_style();
  133. TreeItem *root = unique_resources_list_tree->create_item();
  134. for (const String &E : resource_propnames) {
  135. const String propname = EditorPropertyNameProcessor::get_singleton()->process_name(E, name_style);
  136. TreeItem *ti = unique_resources_list_tree->create_item(root);
  137. ti->set_text(0, propname);
  138. }
  139. unique_resources_label->set_text(TTRC("The following resources will be duplicated and embedded within this resource/object."));
  140. unique_resources_confirmation->popup_centered();
  141. } else {
  142. current_option = -1;
  143. unique_resources_label->set_text(TTRC("This object has no resources."));
  144. unique_resources_confirmation->popup_centered();
  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, "Object");
  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. }
  419. }
  420. void InspectorDock::_bind_methods() {
  421. ClassDB::bind_method("store_script_properties", &InspectorDock::store_script_properties);
  422. ClassDB::bind_method("apply_script_properties", &InspectorDock::apply_script_properties);
  423. ADD_SIGNAL(MethodInfo("request_help"));
  424. }
  425. void InspectorDock::edit_resource(const Ref<Resource> &p_resource) {
  426. _resource_selected(p_resource, "");
  427. }
  428. void InspectorDock::open_resource(const String &p_type) {
  429. _load_resource(p_type);
  430. }
  431. void InspectorDock::set_info(const String &p_button_text, const String &p_message, bool p_is_warning) {
  432. info->hide();
  433. info_is_warning = p_is_warning;
  434. if (info_is_warning) {
  435. info->set_button_icon(get_editor_theme_icon(SNAME("NodeWarning")));
  436. info->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  437. } else {
  438. info->set_button_icon(get_editor_theme_icon(SNAME("NodeInfo")));
  439. info->add_theme_color_override(SceneStringName(font_color), get_theme_color(SceneStringName(font_color), EditorStringName(Editor)));
  440. }
  441. if (!p_button_text.is_empty() && !p_message.is_empty()) {
  442. info->show();
  443. info->set_text(p_button_text);
  444. info_dialog->set_text(p_message);
  445. }
  446. }
  447. void InspectorDock::clear() {
  448. }
  449. void InspectorDock::update(Object *p_object) {
  450. EditorSelectionHistory *editor_history = EditorNode::get_singleton()->get_editor_selection_history();
  451. backward_button->set_disabled(editor_history->is_at_beginning());
  452. forward_button->set_disabled(editor_history->is_at_end());
  453. history_menu->set_disabled(true);
  454. if (editor_history->get_history_len() > 0) {
  455. history_menu->set_disabled(false);
  456. }
  457. object_selector->update_path();
  458. current = p_object;
  459. const bool is_object = p_object != nullptr;
  460. const bool is_resource = is_object && p_object->is_class("Resource");
  461. const bool is_text_file = is_object && p_object->is_class("TextFile");
  462. const bool is_node = is_object && p_object->is_class("Node");
  463. object_menu->set_disabled(!is_object || is_text_file);
  464. search->set_editable(is_object && !is_text_file);
  465. resource_save_button->set_disabled(!is_resource || is_text_file);
  466. open_docs_button->set_disabled(is_text_file || (!is_resource && !is_node));
  467. PopupMenu *resource_extra_popup = resource_extra_button->get_popup();
  468. resource_extra_popup->set_item_disabled(resource_extra_popup->get_item_index(RESOURCE_COPY), !is_resource || is_text_file);
  469. resource_extra_popup->set_item_disabled(resource_extra_popup->get_item_index(RESOURCE_MAKE_BUILT_IN), !is_resource || is_text_file);
  470. if (!is_object || is_text_file) {
  471. info->hide();
  472. object_selector->clear_path();
  473. return;
  474. }
  475. object_selector->enable_path();
  476. PopupMenu *p = object_menu->get_popup();
  477. p->clear();
  478. p->add_icon_shortcut(get_editor_theme_icon(SNAME("GuiTreeArrowDown")), ED_SHORTCUT("property_editor/expand_all", TTRC("Expand All")), EXPAND_ALL);
  479. p->add_icon_shortcut(get_editor_theme_icon(SNAME("GuiTreeArrowRight")), ED_SHORTCUT("property_editor/collapse_all", TTRC("Collapse All")), COLLAPSE_ALL);
  480. // 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.
  481. p->add_shortcut(ED_SHORTCUT("property_editor/expand_revertable", TTRC("Expand Non-Default")), EXPAND_REVERTABLE);
  482. p->add_separator(TTRC("Property Name Style"));
  483. p->add_radio_check_item(vformat(TTR("Raw (e.g. \"%s\")"), "z_index"), PROPERTY_NAME_STYLE_RAW);
  484. p->add_radio_check_item(vformat(TTR("Capitalized (e.g. \"%s\")"), "Z Index"), PROPERTY_NAME_STYLE_CAPITALIZED);
  485. // TRANSLATORS: "Z Index" should match the existing translated CanvasItem property name in the current language you're working on.
  486. p->add_radio_check_item(TTR("Localized (e.g. \"Z Index\")"), PROPERTY_NAME_STYLE_LOCALIZED);
  487. if (!EditorPropertyNameProcessor::is_localization_available()) {
  488. const int index = p->get_item_index(PROPERTY_NAME_STYLE_LOCALIZED);
  489. p->set_item_disabled(index, true);
  490. p->set_item_tooltip(index, TTRC("Localization not available for current language."));
  491. }
  492. p->add_separator();
  493. p->add_shortcut(ED_SHORTCUT("property_editor/copy_params", TTRC("Copy Properties")), OBJECT_COPY_PARAMS);
  494. p->add_shortcut(ED_SHORTCUT("property_editor/paste_params", TTRC("Paste Properties")), OBJECT_PASTE_PARAMS);
  495. if (is_resource || is_node) {
  496. p->add_separator();
  497. p->add_shortcut(ED_SHORTCUT("property_editor/make_subresources_unique", TTRC("Make Sub-Resources Unique")), OBJECT_UNIQUE_RESOURCES);
  498. }
  499. List<MethodInfo> methods;
  500. p_object->get_method_list(&methods);
  501. if (!methods.is_empty()) {
  502. bool found = false;
  503. List<MethodInfo>::Element *I = methods.front();
  504. int i = 0;
  505. while (I) {
  506. if (I->get().flags & METHOD_FLAG_EDITOR) {
  507. if (!found) {
  508. p->add_separator();
  509. found = true;
  510. }
  511. p->add_item(I->get().name.capitalize(), OBJECT_METHOD_BASE + i);
  512. }
  513. i++;
  514. I = I->next();
  515. }
  516. }
  517. }
  518. void InspectorDock::go_back() {
  519. _edit_back();
  520. }
  521. EditorPropertyNameProcessor::Style InspectorDock::get_property_name_style() const {
  522. return property_name_style;
  523. }
  524. void InspectorDock::store_script_properties(Object *p_object) {
  525. ERR_FAIL_NULL(p_object);
  526. ScriptInstance *si = p_object->get_script_instance();
  527. if (!si) {
  528. return;
  529. }
  530. si->get_property_state(stored_properties);
  531. }
  532. void InspectorDock::apply_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. List<PropertyInfo> properties;
  539. si->get_property_list(&properties);
  540. for (const Pair<StringName, Variant> &E : stored_properties) {
  541. Variant current_prop;
  542. if (si->get(E.first, current_prop) && current_prop.get_type() == E.second.get_type()) {
  543. si->set(E.first, E.second);
  544. } else if (E.second.get_type() == Variant::OBJECT) {
  545. for (const PropertyInfo &pi : properties) {
  546. if (E.first != pi.name) {
  547. continue;
  548. }
  549. if (pi.type != Variant::OBJECT) {
  550. break;
  551. }
  552. Object *p_property_object = E.second;
  553. if (p_property_object->is_class(pi.hint_string)) {
  554. si->set(E.first, E.second);
  555. break;
  556. }
  557. Ref<Script> base_script = p_property_object->get_script();
  558. while (base_script.is_valid()) {
  559. if (base_script->get_global_name() == pi.hint_string) {
  560. si->set(E.first, E.second);
  561. break;
  562. }
  563. base_script = base_script->get_base_script();
  564. }
  565. break;
  566. }
  567. }
  568. }
  569. stored_properties.clear();
  570. }
  571. void InspectorDock::shortcut_input(const Ref<InputEvent> &p_event) {
  572. ERR_FAIL_COND(p_event.is_null());
  573. Ref<InputEventKey> key = p_event;
  574. if (key.is_null() || !key->is_pressed() || key->is_echo()) {
  575. return;
  576. }
  577. if (!is_visible() || !inspector->get_rect().has_point(inspector->get_local_mouse_position())) {
  578. return;
  579. }
  580. if (ED_IS_SHORTCUT("editor/open_search", p_event)) {
  581. search->grab_focus();
  582. search->select_all();
  583. accept_event();
  584. }
  585. }
  586. InspectorDock::InspectorDock(EditorData &p_editor_data) {
  587. singleton = this;
  588. set_name("Inspector");
  589. editor_data = &p_editor_data;
  590. property_name_style = EditorPropertyNameProcessor::get_default_inspector_style();
  591. HBoxContainer *general_options_hb = memnew(HBoxContainer);
  592. add_child(general_options_hb);
  593. resource_new_button = memnew(Button);
  594. resource_new_button->set_theme_type_variation("FlatMenuButton");
  595. resource_new_button->set_tooltip_text(TTRC("Create a new resource in memory and edit it."));
  596. general_options_hb->add_child(resource_new_button);
  597. resource_new_button->connect(SceneStringName(pressed), callable_mp(this, &InspectorDock::_new_resource));
  598. resource_new_button->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  599. resource_load_button = memnew(Button);
  600. resource_load_button->set_theme_type_variation("FlatMenuButton");
  601. resource_load_button->set_tooltip_text(TTRC("Load an existing resource from disk and edit it."));
  602. general_options_hb->add_child(resource_load_button);
  603. resource_load_button->connect(SceneStringName(pressed), callable_mp(this, &InspectorDock::_open_resource_selector));
  604. resource_load_button->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  605. resource_save_button = memnew(MenuButton);
  606. resource_save_button->set_flat(false);
  607. resource_save_button->set_theme_type_variation("FlatMenuButton");
  608. resource_save_button->set_tooltip_text(TTRC("Save the currently edited resource."));
  609. general_options_hb->add_child(resource_save_button);
  610. resource_save_button->get_popup()->add_item(TTRC("Save"), RESOURCE_SAVE);
  611. resource_save_button->get_popup()->add_item(TTRC("Save As..."), RESOURCE_SAVE_AS);
  612. resource_save_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &InspectorDock::_menu_option));
  613. resource_save_button->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  614. resource_save_button->set_disabled(true);
  615. resource_extra_button = memnew(MenuButton);
  616. resource_extra_button->set_flat(false);
  617. resource_extra_button->set_theme_type_variation("FlatMenuButton");
  618. resource_extra_button->set_tooltip_text(TTRC("Extra resource options."));
  619. general_options_hb->add_child(resource_extra_button);
  620. resource_extra_button->connect("about_to_popup", callable_mp(this, &InspectorDock::_prepare_resource_extra_popup));
  621. resource_extra_button->get_popup()->add_shortcut(ED_SHORTCUT("property_editor/paste_resource", TTRC("Edit Resource from Clipboard")), RESOURCE_EDIT_CLIPBOARD);
  622. resource_extra_button->get_popup()->add_shortcut(ED_SHORTCUT("property_editor/copy_resource", TTRC("Copy Resource")), RESOURCE_COPY);
  623. resource_extra_button->get_popup()->set_item_disabled(1, true);
  624. resource_extra_button->get_popup()->add_separator();
  625. resource_extra_button->get_popup()->add_shortcut(ED_SHORTCUT("property_editor/show_in_filesystem", TTRC("Show in FileSystem")), RESOURCE_SHOW_IN_FILESYSTEM);
  626. resource_extra_button->get_popup()->add_shortcut(ED_SHORTCUT("property_editor/unref_resource", TTRC("Make Resource Built-In")), RESOURCE_MAKE_BUILT_IN);
  627. resource_extra_button->get_popup()->set_item_disabled(3, true);
  628. resource_extra_button->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &InspectorDock::_menu_option));
  629. general_options_hb->add_spacer();
  630. backward_button = memnew(Button);
  631. backward_button->set_flat(true);
  632. general_options_hb->add_child(backward_button);
  633. backward_button->set_tooltip_text(TTRC("Go to previous edited object in history."));
  634. backward_button->set_disabled(true);
  635. backward_button->connect(SceneStringName(pressed), callable_mp(this, &InspectorDock::_edit_back));
  636. forward_button = memnew(Button);
  637. forward_button->set_flat(true);
  638. general_options_hb->add_child(forward_button);
  639. forward_button->set_tooltip_text(TTRC("Go to next edited object in history."));
  640. forward_button->set_disabled(true);
  641. forward_button->connect(SceneStringName(pressed), callable_mp(this, &InspectorDock::_edit_forward));
  642. history_menu = memnew(MenuButton);
  643. history_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  644. history_menu->set_flat(false);
  645. history_menu->set_theme_type_variation("FlatMenuButton");
  646. history_menu->set_tooltip_text(TTRC("History of recently edited objects."));
  647. general_options_hb->add_child(history_menu);
  648. history_menu->connect("about_to_popup", callable_mp(this, &InspectorDock::_prepare_history));
  649. history_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &InspectorDock::_select_history));
  650. HBoxContainer *subresource_hb = memnew(HBoxContainer);
  651. add_child(subresource_hb);
  652. object_selector = memnew(EditorObjectSelector(EditorNode::get_singleton()->get_editor_selection_history()));
  653. object_selector->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  654. subresource_hb->add_child(object_selector);
  655. open_docs_button = memnew(Button);
  656. open_docs_button->set_theme_type_variation("FlatMenuButton");
  657. open_docs_button->set_disabled(true);
  658. open_docs_button->set_tooltip_text(TTRC("Open documentation for this object."));
  659. open_docs_button->set_shortcut(ED_SHORTCUT("property_editor/open_help", TTRC("Open Documentation")));
  660. subresource_hb->add_child(open_docs_button);
  661. open_docs_button->connect(SceneStringName(pressed), callable_mp(this, &InspectorDock::_menu_option).bind(OBJECT_REQUEST_HELP));
  662. new_resource_dialog = memnew(CreateDialog);
  663. EditorNode::get_singleton()->get_gui_base()->add_child(new_resource_dialog);
  664. new_resource_dialog->set_base_type("Resource");
  665. new_resource_dialog->connect("create", callable_mp(this, &InspectorDock::_resource_created));
  666. HBoxContainer *property_tools_hb = memnew(HBoxContainer);
  667. add_child(property_tools_hb);
  668. search = memnew(LineEdit);
  669. search->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  670. search->set_placeholder(TTRC("Filter Properties"));
  671. search->set_clear_button_enabled(true);
  672. property_tools_hb->add_child(search);
  673. object_menu = memnew(MenuButton);
  674. object_menu->set_flat(false);
  675. object_menu->set_theme_type_variation("FlatMenuButton");
  676. property_tools_hb->add_child(object_menu);
  677. object_menu->set_tooltip_text(TTRC("Manage object properties."));
  678. object_menu->get_popup()->connect("about_to_popup", callable_mp(this, &InspectorDock::_prepare_menu));
  679. object_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &InspectorDock::_menu_option));
  680. info = memnew(Button);
  681. add_child(info);
  682. info->set_clip_text(true);
  683. info->set_accessibility_name(TTRC("Information"));
  684. info->hide();
  685. info->connect(SceneStringName(pressed), callable_mp(this, &InspectorDock::_info_pressed));
  686. unique_resources_confirmation = memnew(ConfirmationDialog);
  687. add_child(unique_resources_confirmation);
  688. VBoxContainer *container = memnew(VBoxContainer);
  689. unique_resources_confirmation->add_child(container);
  690. unique_resources_label = memnew(Label);
  691. unique_resources_label->set_focus_mode(FOCUS_ACCESSIBILITY);
  692. container->add_child(unique_resources_label);
  693. unique_resources_list_tree = memnew(Tree);
  694. unique_resources_list_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  695. unique_resources_list_tree->set_hide_root(true);
  696. unique_resources_list_tree->set_columns(1);
  697. unique_resources_list_tree->set_custom_minimum_size(Size2(0, 200 * EDSCALE));
  698. container->add_child(unique_resources_list_tree);
  699. Label *bottom_label = memnew(Label);
  700. bottom_label->set_focus_mode(FOCUS_ACCESSIBILITY);
  701. bottom_label->set_text(TTRC("This cannot be undone. Are you sure?"));
  702. container->add_child(bottom_label);
  703. unique_resources_confirmation->connect(SceneStringName(confirmed), callable_mp(this, &InspectorDock::_menu_confirm_current));
  704. info_dialog = memnew(AcceptDialog);
  705. EditorNode::get_singleton()->get_gui_base()->add_child(info_dialog);
  706. load_resource_dialog = memnew(EditorFileDialog);
  707. add_child(load_resource_dialog);
  708. load_resource_dialog->set_current_dir("res://");
  709. load_resource_dialog->connect("file_selected", callable_mp(this, &InspectorDock::_resource_file_selected));
  710. inspector = memnew(EditorInspector);
  711. add_child(inspector);
  712. inspector->set_autoclear(true);
  713. inspector->set_show_categories(true, true);
  714. inspector->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  715. inspector->set_use_doc_hints(true);
  716. inspector->set_hide_script(false);
  717. inspector->set_hide_metadata(false);
  718. inspector->set_use_settings_name_style(false);
  719. inspector->set_property_name_style(property_name_style);
  720. inspector->set_use_folding(!bool(EDITOR_GET("interface/inspector/disable_folding")));
  721. inspector->register_text_enter(search);
  722. inspector->set_use_filter(true);
  723. inspector->connect("resource_selected", callable_mp(this, &InspectorDock::_resource_selected));
  724. FileSystemDock::get_singleton()->connect("files_moved", callable_mp(this, &InspectorDock::_files_moved));
  725. set_process_shortcut_input(true);
  726. }
  727. InspectorDock::~InspectorDock() {
  728. singleton = nullptr;
  729. }