inspector_dock.cpp 30 KB

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