editor_resource_picker.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. /**************************************************************************/
  2. /* editor_resource_picker.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 "editor_resource_picker.h"
  31. #include "editor/audio_stream_preview.h"
  32. #include "editor/editor_help.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_resource_preview.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/editor_string_names.h"
  37. #include "editor/filesystem_dock.h"
  38. #include "editor/gui/editor_file_dialog.h"
  39. #include "editor/gui/editor_quick_open_dialog.h"
  40. #include "editor/plugins/editor_resource_conversion_plugin.h"
  41. #include "editor/plugins/script_editor_plugin.h"
  42. #include "editor/scene_tree_dock.h"
  43. #include "editor/themes/editor_scale.h"
  44. #include "scene/gui/button.h"
  45. #include "scene/gui/texture_rect.h"
  46. #include "scene/resources/gradient_texture.h"
  47. #include "scene/resources/image_texture.h"
  48. void EditorResourcePicker::_update_resource() {
  49. String resource_path;
  50. if (edited_resource.is_valid() && edited_resource->get_path().is_resource_file()) {
  51. resource_path = edited_resource->get_path() + "\n";
  52. }
  53. String class_name = _get_resource_type(edited_resource);
  54. if (preview_rect) {
  55. preview_rect->set_texture(Ref<Texture2D>());
  56. assign_button->set_custom_minimum_size(assign_button_min_size);
  57. if (edited_resource == Ref<Resource>()) {
  58. assign_button->set_button_icon(Ref<Texture2D>());
  59. assign_button->set_text(TTR("<empty>"));
  60. assign_button->set_tooltip_text("");
  61. } else {
  62. assign_button->set_button_icon(EditorNode::get_singleton()->get_object_icon(edited_resource.operator->(), SNAME("Object")));
  63. if (!edited_resource->get_name().is_empty()) {
  64. assign_button->set_text(edited_resource->get_name());
  65. } else if (edited_resource->get_path().is_resource_file()) {
  66. assign_button->set_text(edited_resource->get_path().get_file());
  67. } else {
  68. assign_button->set_text(class_name);
  69. }
  70. if (edited_resource->get_path().is_resource_file()) {
  71. resource_path = edited_resource->get_path() + "\n";
  72. }
  73. assign_button->set_tooltip_text(resource_path + TTR("Type:") + " " + class_name);
  74. // Preview will override the above, so called at the end.
  75. EditorResourcePreview::get_singleton()->queue_edited_resource_preview(edited_resource, this, "_update_resource_preview", edited_resource->get_instance_id());
  76. }
  77. } else if (edited_resource.is_valid()) {
  78. assign_button->set_tooltip_text(resource_path + TTR("Type:") + " " + edited_resource->get_class());
  79. }
  80. assign_button->set_disabled(!editable && edited_resource.is_null());
  81. quick_load_button->set_visible(editable && edited_resource.is_null());
  82. }
  83. void EditorResourcePicker::_update_resource_preview(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, ObjectID p_obj) {
  84. if (edited_resource.is_null() || edited_resource->get_instance_id() != p_obj) {
  85. return;
  86. }
  87. if (preview_rect) {
  88. Ref<Script> scr = edited_resource;
  89. if (scr.is_valid()) {
  90. assign_button->set_text(scr->get_path().get_file());
  91. return;
  92. }
  93. if (p_preview.is_valid()) {
  94. preview_rect->set_offset(SIDE_LEFT, assign_button->get_button_icon()->get_width() + assign_button->get_theme_stylebox(CoreStringName(normal))->get_content_margin(SIDE_LEFT) + get_theme_constant(SNAME("h_separation"), SNAME("Button")));
  95. // Resource-specific stretching.
  96. if (Ref<GradientTexture1D>(edited_resource).is_valid() || Ref<Gradient>(edited_resource).is_valid()) {
  97. preview_rect->set_stretch_mode(TextureRect::STRETCH_SCALE);
  98. assign_button->set_custom_minimum_size(assign_button_min_size);
  99. } else {
  100. preview_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  101. int thumbnail_size = EDITOR_GET("filesystem/file_dialog/thumbnail_size");
  102. thumbnail_size *= EDSCALE;
  103. assign_button->set_custom_minimum_size(assign_button_min_size.max(Size2(1, thumbnail_size)));
  104. }
  105. preview_rect->set_texture(p_preview);
  106. assign_button->set_text("");
  107. }
  108. }
  109. }
  110. void EditorResourcePicker::_resource_selected() {
  111. if (edited_resource.is_null()) {
  112. edit_button->set_pressed(true);
  113. _update_menu();
  114. return;
  115. }
  116. emit_signal(SNAME("resource_selected"), edited_resource, false);
  117. }
  118. void EditorResourcePicker::_resource_changed() {
  119. emit_signal(SNAME("resource_changed"), edited_resource);
  120. _update_resource();
  121. }
  122. void EditorResourcePicker::_file_selected(const String &p_path) {
  123. Ref<Resource> loaded_resource = ResourceLoader::load(p_path);
  124. ERR_FAIL_COND_MSG(loaded_resource.is_null(), "Cannot load resource from path '" + p_path + "'.");
  125. if (!base_type.is_empty()) {
  126. bool any_type_matches = false;
  127. String res_type = loaded_resource->get_class();
  128. Ref<Script> res_script = loaded_resource->get_script();
  129. bool is_global_class = false;
  130. if (res_script.is_valid()) {
  131. String script_type = EditorNode::get_editor_data().script_class_get_name(res_script->get_path());
  132. if (!script_type.is_empty()) {
  133. is_global_class = true;
  134. res_type = script_type;
  135. }
  136. }
  137. for (int i = 0; i < base_type.get_slice_count(","); i++) {
  138. String base = base_type.get_slicec(',', i);
  139. any_type_matches = is_global_class ? EditorNode::get_editor_data().script_class_is_parent(res_type, base) : loaded_resource->is_class(base);
  140. if (any_type_matches) {
  141. break;
  142. }
  143. }
  144. if (!any_type_matches) {
  145. EditorNode::get_singleton()->show_warning(vformat(TTR("The selected resource (%s) does not match any type expected for this property (%s)."), res_type, base_type));
  146. return;
  147. }
  148. }
  149. edited_resource = loaded_resource;
  150. _resource_changed();
  151. }
  152. void EditorResourcePicker::_resource_saved(Object *p_resource) {
  153. if (edited_resource.is_valid() && p_resource == edited_resource.ptr()) {
  154. emit_signal(SNAME("resource_changed"), edited_resource);
  155. _update_resource();
  156. }
  157. }
  158. void EditorResourcePicker::_update_menu() {
  159. if (edit_menu && edit_menu->is_visible()) {
  160. edit_button->set_pressed(false);
  161. edit_menu->hide();
  162. return;
  163. }
  164. _update_menu_items();
  165. Rect2 gt = edit_button->get_screen_rect();
  166. edit_menu->reset_size();
  167. int ms = edit_menu->get_contents_minimum_size().width;
  168. Vector2 popup_pos = gt.get_end() - Vector2(ms, 0);
  169. edit_menu->set_position(popup_pos);
  170. edit_menu->popup();
  171. }
  172. void EditorResourcePicker::_update_menu_items() {
  173. _ensure_resource_menu();
  174. edit_menu->clear();
  175. // Add options for creating specific subtypes of the base resource type.
  176. if (is_editable()) {
  177. set_create_options(edit_menu);
  178. // Add an option to load a resource from a file using the QuickOpen dialog.
  179. edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Load")), TTR("Quick Load..."), OBJ_MENU_QUICKLOAD);
  180. edit_menu->set_item_tooltip(-1, TTR("Opens a quick menu to select from a list of allowed Resource files."));
  181. // Add an option to load a resource from a file using the regular file dialog.
  182. edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Load")), TTR("Load..."), OBJ_MENU_LOAD);
  183. }
  184. // Add options for changing existing value of the resource.
  185. if (edited_resource.is_valid()) {
  186. // Determine if the edited resource is part of another scene (foreign) which was imported
  187. bool is_edited_resource_foreign_import = EditorNode::get_singleton()->is_resource_read_only(edited_resource, true);
  188. // If the resource is determined to be foreign and imported, change the menu entry's description to 'inspect' rather than 'edit'
  189. // since will only be able to view its properties in read-only mode.
  190. if (is_edited_resource_foreign_import) {
  191. // The 'Search' icon is a magnifying glass, which seems appropriate, but maybe a bespoke icon is preferred here.
  192. edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Search")), TTR("Inspect"), OBJ_MENU_INSPECT);
  193. } else {
  194. edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Edit")), TTR("Edit"), OBJ_MENU_INSPECT);
  195. }
  196. if (is_editable()) {
  197. if (!_is_custom_type_script()) {
  198. edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Clear")), TTR("Clear"), OBJ_MENU_CLEAR);
  199. }
  200. edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Duplicate")), TTR("Make Unique"), OBJ_MENU_MAKE_UNIQUE);
  201. // Check whether the resource has subresources.
  202. List<PropertyInfo> property_list;
  203. edited_resource->get_property_list(&property_list);
  204. bool has_subresources = false;
  205. for (PropertyInfo &p : property_list) {
  206. if ((p.type == Variant::OBJECT) && (p.hint == PROPERTY_HINT_RESOURCE_TYPE) && (p.name != "script") && ((Object *)edited_resource->get(p.name) != nullptr)) {
  207. has_subresources = true;
  208. break;
  209. }
  210. }
  211. if (has_subresources) {
  212. edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Duplicate")), TTR("Make Unique (Recursive)"), OBJ_MENU_MAKE_UNIQUE_RECURSIVE);
  213. }
  214. edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Save")), TTR("Save"), OBJ_MENU_SAVE);
  215. edit_menu->add_icon_item(get_editor_theme_icon(SNAME("Save")), TTR("Save As..."), OBJ_MENU_SAVE_AS);
  216. }
  217. if (edited_resource->get_path().is_resource_file()) {
  218. edit_menu->add_separator();
  219. edit_menu->add_icon_item(get_editor_theme_icon(SNAME("ShowInFileSystem")), TTR("Show in FileSystem"), OBJ_MENU_SHOW_IN_FILE_SYSTEM);
  220. }
  221. }
  222. // Add options to copy/paste resource.
  223. Ref<Resource> cb = EditorSettings::get_singleton()->get_resource_clipboard();
  224. bool paste_valid = false;
  225. if (is_editable() && cb.is_valid()) {
  226. if (base_type.is_empty()) {
  227. paste_valid = true;
  228. } else {
  229. String res_type = _get_resource_type(cb);
  230. for (int i = 0; i < base_type.get_slice_count(","); i++) {
  231. String base = base_type.get_slicec(',', i);
  232. paste_valid = ClassDB::is_parent_class(res_type, base) || EditorNode::get_editor_data().script_class_is_parent(res_type, base);
  233. if (paste_valid) {
  234. break;
  235. }
  236. }
  237. }
  238. }
  239. if (edited_resource.is_valid() || paste_valid) {
  240. edit_menu->add_separator();
  241. if (edited_resource.is_valid()) {
  242. edit_menu->add_item(TTR("Copy"), OBJ_MENU_COPY);
  243. }
  244. if (paste_valid) {
  245. edit_menu->add_item(TTR("Paste"), OBJ_MENU_PASTE);
  246. }
  247. }
  248. // Add options to convert existing resource to another type of resource.
  249. if (is_editable() && edited_resource.is_valid()) {
  250. Vector<Ref<EditorResourceConversionPlugin>> conversions = EditorNode::get_singleton()->find_resource_conversion_plugin_for_resource(edited_resource);
  251. if (!conversions.is_empty()) {
  252. edit_menu->add_separator();
  253. }
  254. int relative_id = 0;
  255. for (const Ref<EditorResourceConversionPlugin> &conversion : conversions) {
  256. String what = conversion->converts_to();
  257. Ref<Texture2D> icon;
  258. if (has_theme_icon(what, EditorStringName(EditorIcons))) {
  259. icon = get_editor_theme_icon(what);
  260. } else {
  261. icon = get_editor_theme_icon(SNAME("Object"));
  262. }
  263. edit_menu->add_icon_item(icon, vformat(TTR("Convert to %s"), what), CONVERT_BASE_ID + relative_id);
  264. relative_id++;
  265. }
  266. }
  267. }
  268. void EditorResourcePicker::_edit_menu_cbk(int p_which) {
  269. switch (p_which) {
  270. case OBJ_MENU_LOAD: {
  271. List<String> extensions;
  272. for (int i = 0; i < base_type.get_slice_count(","); i++) {
  273. String base = base_type.get_slicec(',', i);
  274. if (base == "Resource") {
  275. base = "";
  276. }
  277. ResourceLoader::get_recognized_extensions_for_type(base, &extensions);
  278. if (ScriptServer::is_global_class(base)) {
  279. ResourceLoader::get_recognized_extensions_for_type(ScriptServer::get_global_class_native_base(base), &extensions);
  280. }
  281. }
  282. HashSet<String> valid_extensions;
  283. for (const String &E : extensions) {
  284. valid_extensions.insert(E);
  285. }
  286. if (!file_dialog) {
  287. file_dialog = memnew(EditorFileDialog);
  288. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  289. add_child(file_dialog);
  290. file_dialog->connect("file_selected", callable_mp(this, &EditorResourcePicker::_file_selected));
  291. }
  292. file_dialog->clear_filters();
  293. for (const String &E : valid_extensions) {
  294. file_dialog->add_filter("*." + E, E.to_upper());
  295. }
  296. file_dialog->popup_file_dialog();
  297. } break;
  298. case OBJ_MENU_QUICKLOAD: {
  299. const Vector<String> &base_types_string = base_type.split(",");
  300. Vector<StringName> base_types;
  301. for (const String &type : base_types_string) {
  302. base_types.push_back(type);
  303. }
  304. EditorNode::get_singleton()->get_quick_open_dialog()->popup_dialog(base_types, callable_mp(this, &EditorResourcePicker::_file_selected));
  305. } break;
  306. case OBJ_MENU_INSPECT: {
  307. if (edited_resource.is_valid()) {
  308. emit_signal(SNAME("resource_selected"), edited_resource, true);
  309. }
  310. } break;
  311. case OBJ_MENU_CLEAR: {
  312. edited_resource = Ref<Resource>();
  313. _resource_changed();
  314. } break;
  315. case OBJ_MENU_MAKE_UNIQUE: {
  316. if (edited_resource.is_null()) {
  317. return;
  318. }
  319. Ref<Resource> unique_resource = edited_resource->duplicate();
  320. ERR_FAIL_COND(unique_resource.is_null()); // duplicate() may fail.
  321. edited_resource = unique_resource;
  322. _resource_changed();
  323. } break;
  324. case OBJ_MENU_MAKE_UNIQUE_RECURSIVE: {
  325. if (edited_resource.is_null()) {
  326. return;
  327. }
  328. if (!duplicate_resources_dialog) {
  329. duplicate_resources_dialog = memnew(ConfirmationDialog);
  330. add_child(duplicate_resources_dialog);
  331. duplicate_resources_dialog->set_title(TTR("Make Unique (Recursive)"));
  332. duplicate_resources_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorResourcePicker::_duplicate_selected_resources));
  333. VBoxContainer *vb = memnew(VBoxContainer);
  334. duplicate_resources_dialog->add_child(vb);
  335. Label *label = memnew(Label(TTR("Select resources to make unique:")));
  336. vb->add_child(label);
  337. duplicate_resources_tree = memnew(Tree);
  338. duplicate_resources_tree->set_accessibility_name(TTRC("Duplicate resources"));
  339. duplicate_resources_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  340. vb->add_child(duplicate_resources_tree);
  341. duplicate_resources_tree->set_columns(2);
  342. duplicate_resources_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  343. }
  344. duplicate_resources_tree->clear();
  345. TreeItem *root = duplicate_resources_tree->create_item();
  346. _gather_resources_to_duplicate(edited_resource, root);
  347. duplicate_resources_dialog->reset_size();
  348. duplicate_resources_dialog->popup_centered(Vector2(500, 400) * EDSCALE);
  349. } break;
  350. case OBJ_MENU_SAVE: {
  351. if (edited_resource.is_null()) {
  352. return;
  353. }
  354. EditorNode::get_singleton()->save_resource(edited_resource);
  355. } break;
  356. case OBJ_MENU_SAVE_AS: {
  357. if (edited_resource.is_null()) {
  358. return;
  359. }
  360. Callable resource_saved = callable_mp(this, &EditorResourcePicker::_resource_saved);
  361. if (!EditorNode::get_singleton()->is_connected("resource_saved", resource_saved)) {
  362. EditorNode::get_singleton()->connect("resource_saved", resource_saved);
  363. }
  364. EditorNode::get_singleton()->save_resource_as(edited_resource);
  365. } break;
  366. case OBJ_MENU_COPY: {
  367. EditorSettings::get_singleton()->set_resource_clipboard(edited_resource);
  368. } break;
  369. case OBJ_MENU_PASTE: {
  370. edited_resource = EditorSettings::get_singleton()->get_resource_clipboard();
  371. if (edited_resource->is_built_in() && EditorNode::get_singleton()->get_edited_scene() &&
  372. edited_resource->get_path().get_slice("::", 0) != EditorNode::get_singleton()->get_edited_scene()->get_scene_file_path()) {
  373. // Automatically make resource unique if it belongs to another scene.
  374. _edit_menu_cbk(OBJ_MENU_MAKE_UNIQUE);
  375. return;
  376. }
  377. _resource_changed();
  378. } break;
  379. case OBJ_MENU_SHOW_IN_FILE_SYSTEM: {
  380. FileSystemDock::get_singleton()->navigate_to_path(edited_resource->get_path());
  381. } break;
  382. default: {
  383. // Allow subclasses to handle their own options first, only then fallback on the default branch logic.
  384. if (handle_menu_selected(p_which)) {
  385. break;
  386. }
  387. if (p_which >= CONVERT_BASE_ID) {
  388. int to_type = p_which - CONVERT_BASE_ID;
  389. Vector<Ref<EditorResourceConversionPlugin>> conversions = EditorNode::get_singleton()->find_resource_conversion_plugin_for_resource(edited_resource);
  390. ERR_FAIL_INDEX(to_type, conversions.size());
  391. edited_resource = conversions[to_type]->convert(edited_resource);
  392. _resource_changed();
  393. break;
  394. }
  395. ERR_FAIL_COND(inheritors_array.is_empty());
  396. String intype = inheritors_array[p_which - TYPE_BASE_ID];
  397. Variant obj;
  398. if (ScriptServer::is_global_class(intype)) {
  399. obj = EditorNode::get_editor_data().script_class_instance(intype);
  400. } else {
  401. obj = ClassDB::instantiate(intype);
  402. }
  403. if (!obj) {
  404. obj = EditorNode::get_editor_data().instantiate_custom_type(intype, "Resource");
  405. }
  406. Resource *resp = Object::cast_to<Resource>(obj);
  407. ERR_BREAK(!resp);
  408. EditorNode::get_editor_data().instantiate_object_properties(obj);
  409. // Prevent freeing of the object until the end of the update of the resource (GH-88286).
  410. Ref<Resource> old_edited_resource = edited_resource;
  411. edited_resource = Ref<Resource>(resp);
  412. _resource_changed();
  413. } break;
  414. }
  415. }
  416. void EditorResourcePicker::set_create_options(Object *p_menu_node) {
  417. _ensure_resource_menu();
  418. // If a subclass implements this method, use it to replace all create items.
  419. if (GDVIRTUAL_CALL(_set_create_options, p_menu_node)) {
  420. return;
  421. }
  422. // By default provide generic "New ..." options.
  423. if (!base_type.is_empty()) {
  424. int idx = 0;
  425. _ensure_allowed_types();
  426. HashSet<StringName> allowed_types = allowed_types_without_convert;
  427. if (!allowed_types.is_empty()) {
  428. edit_menu->add_separator(TTRC("New"));
  429. }
  430. for (const StringName &E : allowed_types) {
  431. const String &t = E;
  432. if (!ClassDB::can_instantiate(t)) {
  433. continue;
  434. }
  435. inheritors_array.push_back(t);
  436. Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(t, "Object");
  437. int id = TYPE_BASE_ID + idx;
  438. edit_menu->add_icon_item(icon, t, id);
  439. edit_menu->set_item_auto_translate_mode(-1, AUTO_TRANSLATE_MODE_DISABLED);
  440. HashMap<String, DocData::ClassDoc>::Iterator class_doc = EditorHelp::get_doc_data()->class_list.find(t);
  441. if (class_doc) {
  442. edit_menu->set_item_tooltip(-1, DTR(class_doc->value.brief_description));
  443. }
  444. idx++;
  445. }
  446. if (edit_menu->get_item_count()) {
  447. edit_menu->add_separator();
  448. }
  449. }
  450. }
  451. bool EditorResourcePicker::handle_menu_selected(int p_which) {
  452. bool success = false;
  453. GDVIRTUAL_CALL(_handle_menu_selected, p_which, success);
  454. return success;
  455. }
  456. void EditorResourcePicker::_button_draw() {
  457. if (dropping) {
  458. Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  459. assign_button->draw_rect(Rect2(Point2(), assign_button->get_size()), color, false);
  460. }
  461. }
  462. void EditorResourcePicker::_button_input(const Ref<InputEvent> &p_event) {
  463. Ref<InputEventMouseButton> mb = p_event;
  464. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
  465. // Only attempt to update and show the menu if we have
  466. // a valid resource or the Picker is editable, as
  467. // there will otherwise be nothing to display.
  468. if (edited_resource.is_valid() || is_editable()) {
  469. if (edit_menu && edit_menu->is_visible()) {
  470. edit_button->set_pressed(false);
  471. edit_menu->hide();
  472. return;
  473. }
  474. _update_menu_items();
  475. Vector2 pos = get_screen_position() + mb->get_position();
  476. edit_menu->reset_size();
  477. edit_menu->set_position(pos);
  478. edit_menu->popup();
  479. }
  480. }
  481. }
  482. String EditorResourcePicker::_get_resource_type(const Ref<Resource> &p_resource) const {
  483. if (p_resource.is_null()) {
  484. return String();
  485. }
  486. String res_type = p_resource->get_class();
  487. Ref<Script> res_script = p_resource->get_script();
  488. if (res_script.is_null()) {
  489. return res_type;
  490. }
  491. // TODO: Replace with EditorFileSystem when PR #60606 is merged to use cached resource type.
  492. String script_type = EditorNode::get_editor_data().script_class_get_name(res_script->get_path());
  493. if (!script_type.is_empty()) {
  494. res_type = script_type;
  495. }
  496. return res_type;
  497. }
  498. static void _add_allowed_type(const StringName &p_type, List<StringName> *p_vector) {
  499. if (p_vector->find(p_type)) {
  500. // Already added.
  501. return;
  502. }
  503. if (ClassDB::class_exists(p_type)) {
  504. // Engine class,
  505. if (!ClassDB::is_virtual(p_type)) {
  506. p_vector->push_back(p_type);
  507. }
  508. LocalVector<StringName> inheriters;
  509. ClassDB::get_inheriters_from_class(p_type, inheriters);
  510. for (const StringName &S : inheriters) {
  511. _add_allowed_type(S, p_vector);
  512. }
  513. } else {
  514. // Script class.
  515. p_vector->push_back(p_type);
  516. }
  517. List<StringName> inheriters;
  518. ScriptServer::get_inheriters_list(p_type, &inheriters);
  519. for (const StringName &S : inheriters) {
  520. _add_allowed_type(S, p_vector);
  521. }
  522. }
  523. void EditorResourcePicker::_ensure_allowed_types() const {
  524. if (!allowed_types_without_convert.is_empty()) {
  525. return;
  526. }
  527. List<StringName> final_allowed;
  528. Vector<String> allowed_types = base_type.split(",");
  529. int size = allowed_types.size();
  530. for (const String &S : allowed_types) {
  531. const String base = S.strip_edges();
  532. if (base.begins_with("-")) {
  533. final_allowed.erase(base.right(-1));
  534. continue;
  535. }
  536. _add_allowed_type(base, &final_allowed);
  537. }
  538. for (const StringName &SN : final_allowed) {
  539. allowed_types_without_convert.insert(SN);
  540. }
  541. allowed_types_with_convert = HashSet<StringName>(allowed_types_without_convert);
  542. for (int i = 0; i < size; i++) {
  543. const String base = allowed_types[i].strip_edges();
  544. if (base == "BaseMaterial3D") {
  545. allowed_types_with_convert.insert("Texture2D");
  546. } else if (ClassDB::is_parent_class("ShaderMaterial", base)) {
  547. allowed_types_with_convert.insert("Shader");
  548. } else if (ClassDB::is_parent_class("ImageTexture", base)) {
  549. allowed_types_with_convert.insert("Image");
  550. }
  551. }
  552. }
  553. bool EditorResourcePicker::_is_drop_valid(const Dictionary &p_drag_data) const {
  554. {
  555. const ObjectID source_picker = p_drag_data.get("source_picker", ObjectID());
  556. if (source_picker == get_instance_id()) {
  557. return false;
  558. }
  559. }
  560. if (base_type.is_empty()) {
  561. return true;
  562. }
  563. Dictionary drag_data = p_drag_data;
  564. Ref<Resource> res;
  565. if (drag_data.has("type") && String(drag_data["type"]) == "script_list_element") {
  566. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(drag_data["script_list_element"]);
  567. if (se) {
  568. res = se->get_edited_resource();
  569. }
  570. } else if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  571. res = drag_data["resource"];
  572. } else if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  573. Vector<String> files = drag_data["files"];
  574. // TODO: Extract the typename of the dropped filepath's resource in a more performant way, without fully loading it.
  575. if (files.size() == 1) {
  576. res = ResourceLoader::load(files[0]);
  577. }
  578. }
  579. _ensure_allowed_types();
  580. HashSet<StringName> allowed_types = allowed_types_with_convert;
  581. if (res.is_valid()) {
  582. String res_type = _get_resource_type(res);
  583. if (_is_type_valid(res_type, allowed_types)) {
  584. return true;
  585. }
  586. if (res->get_script()) {
  587. StringName custom_class = EditorNode::get_singleton()->get_object_custom_type_name(res->get_script());
  588. if (_is_type_valid(custom_class, allowed_types)) {
  589. return true;
  590. }
  591. }
  592. }
  593. return false;
  594. }
  595. bool EditorResourcePicker::_is_type_valid(const String &p_type_name, const HashSet<StringName> &p_allowed_types) const {
  596. for (const StringName &E : p_allowed_types) {
  597. String at = E;
  598. if (p_type_name == at || ClassDB::is_parent_class(p_type_name, at) || EditorNode::get_editor_data().script_class_is_parent(p_type_name, at)) {
  599. return true;
  600. }
  601. }
  602. return false;
  603. }
  604. bool EditorResourcePicker::_is_custom_type_script() const {
  605. Ref<Script> resource_as_script = edited_resource;
  606. if (resource_as_script.is_valid() && resource_owner && resource_owner->has_meta(SceneStringName(_custom_type_script))) {
  607. return true;
  608. }
  609. return false;
  610. }
  611. Variant EditorResourcePicker::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  612. if (edited_resource.is_valid()) {
  613. Dictionary drag_data = EditorNode::get_singleton()->drag_resource(edited_resource, p_from);
  614. drag_data["source_picker"] = get_instance_id();
  615. return drag_data;
  616. }
  617. return Variant();
  618. }
  619. bool EditorResourcePicker::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  620. return editable && _is_drop_valid(p_data);
  621. }
  622. void EditorResourcePicker::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  623. ERR_FAIL_COND(!_is_drop_valid(p_data));
  624. Dictionary drag_data = p_data;
  625. Ref<Resource> dropped_resource;
  626. if (drag_data.has("type") && String(drag_data["type"]) == "script_list_element") {
  627. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(drag_data["script_list_element"]);
  628. if (se) {
  629. dropped_resource = se->get_edited_resource();
  630. }
  631. } else if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  632. dropped_resource = drag_data["resource"];
  633. }
  634. if (dropped_resource.is_null() && drag_data.has("type") && String(drag_data["type"]) == "files") {
  635. Vector<String> files = drag_data["files"];
  636. if (files.size() == 1) {
  637. dropped_resource = ResourceLoader::load(files[0]);
  638. }
  639. }
  640. if (dropped_resource.is_valid()) {
  641. _ensure_allowed_types();
  642. HashSet<StringName> allowed_types = allowed_types_without_convert;
  643. String res_type = _get_resource_type(dropped_resource);
  644. // If the accepted dropped resource is from the extended list, it requires conversion.
  645. if (!_is_type_valid(res_type, allowed_types)) {
  646. for (const StringName &E : allowed_types) {
  647. String at = E;
  648. if (at == "BaseMaterial3D" && Ref<Texture2D>(dropped_resource).is_valid()) {
  649. // Use existing resource if possible and only replace its data.
  650. Ref<StandardMaterial3D> mat = edited_resource;
  651. if (mat.is_null()) {
  652. mat.instantiate();
  653. }
  654. mat->set_texture(StandardMaterial3D::TextureParam::TEXTURE_ALBEDO, dropped_resource);
  655. dropped_resource = mat;
  656. break;
  657. }
  658. if (at == "ShaderMaterial" && Ref<Shader>(dropped_resource).is_valid()) {
  659. Ref<ShaderMaterial> mat = edited_resource;
  660. if (mat.is_null()) {
  661. mat.instantiate();
  662. }
  663. mat->set_shader(dropped_resource);
  664. dropped_resource = mat;
  665. break;
  666. }
  667. if (at == "ImageTexture" && Ref<Image>(dropped_resource).is_valid()) {
  668. Ref<ImageTexture> texture = edited_resource;
  669. if (texture.is_null()) {
  670. texture.instantiate();
  671. }
  672. texture->set_image(dropped_resource);
  673. dropped_resource = texture;
  674. break;
  675. }
  676. }
  677. }
  678. edited_resource = dropped_resource;
  679. _resource_changed();
  680. }
  681. }
  682. void EditorResourcePicker::_bind_methods() {
  683. ClassDB::bind_method(D_METHOD("_update_resource_preview"), &EditorResourcePicker::_update_resource_preview);
  684. ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &EditorResourcePicker::set_base_type);
  685. ClassDB::bind_method(D_METHOD("get_base_type"), &EditorResourcePicker::get_base_type);
  686. ClassDB::bind_method(D_METHOD("get_allowed_types"), &EditorResourcePicker::get_allowed_types);
  687. ClassDB::bind_method(D_METHOD("set_edited_resource", "resource"), &EditorResourcePicker::set_edited_resource);
  688. ClassDB::bind_method(D_METHOD("get_edited_resource"), &EditorResourcePicker::get_edited_resource);
  689. ClassDB::bind_method(D_METHOD("set_toggle_mode", "enable"), &EditorResourcePicker::set_toggle_mode);
  690. ClassDB::bind_method(D_METHOD("is_toggle_mode"), &EditorResourcePicker::is_toggle_mode);
  691. ClassDB::bind_method(D_METHOD("set_toggle_pressed", "pressed"), &EditorResourcePicker::set_toggle_pressed);
  692. ClassDB::bind_method(D_METHOD("set_editable", "enable"), &EditorResourcePicker::set_editable);
  693. ClassDB::bind_method(D_METHOD("is_editable"), &EditorResourcePicker::is_editable);
  694. GDVIRTUAL_BIND(_set_create_options, "menu_node");
  695. GDVIRTUAL_BIND(_handle_menu_selected, "id");
  696. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type"), "set_base_type", "get_base_type");
  697. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "edited_resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource", PROPERTY_USAGE_NONE), "set_edited_resource", "get_edited_resource");
  698. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  699. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode");
  700. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), PropertyInfo(Variant::BOOL, "inspect")));
  701. ADD_SIGNAL(MethodInfo("resource_changed", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
  702. }
  703. void EditorResourcePicker::_notification(int p_what) {
  704. switch (p_what) {
  705. case NOTIFICATION_ENTER_TREE: {
  706. _update_resource();
  707. [[fallthrough]];
  708. }
  709. case NOTIFICATION_THEME_CHANGED: {
  710. const int icon_width = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
  711. assign_button->add_theme_constant_override("icon_max_width", icon_width);
  712. if (edit_menu) {
  713. edit_menu->add_theme_constant_override("icon_max_width", icon_width);
  714. }
  715. quick_load_button->set_button_icon(get_editor_theme_icon(SNAME("Load")));
  716. edit_button->set_button_icon(get_theme_icon(SNAME("select_arrow"), SNAME("Tree")));
  717. } break;
  718. case NOTIFICATION_DRAW: {
  719. draw_style_box(get_theme_stylebox(SceneStringName(panel), SNAME("Tree")), Rect2(Point2(), get_size()));
  720. } break;
  721. case NOTIFICATION_DRAG_BEGIN: {
  722. if (editable && _is_drop_valid(get_viewport()->gui_get_drag_data())) {
  723. dropping = true;
  724. assign_button->queue_redraw();
  725. }
  726. } break;
  727. case NOTIFICATION_DRAG_END: {
  728. if (dropping) {
  729. dropping = false;
  730. assign_button->queue_redraw();
  731. }
  732. } break;
  733. case NOTIFICATION_EXIT_TREE: {
  734. Callable resource_saved = callable_mp(this, &EditorResourcePicker::_resource_saved);
  735. if (EditorNode::get_singleton()->is_connected("resource_saved", resource_saved)) {
  736. EditorNode::get_singleton()->disconnect("resource_saved", resource_saved);
  737. }
  738. } break;
  739. }
  740. }
  741. void EditorResourcePicker::set_assign_button_min_size(const Size2i &p_size) {
  742. assign_button_min_size = p_size;
  743. assign_button->set_custom_minimum_size(assign_button_min_size);
  744. }
  745. void EditorResourcePicker::set_base_type(const String &p_base_type) {
  746. base_type = p_base_type;
  747. // There is a possibility that the new base type is conflicting with the existing value.
  748. // Keep the value, but warn the user that there is a potential mistake.
  749. if (!base_type.is_empty() && edited_resource.is_valid()) {
  750. _ensure_allowed_types();
  751. HashSet<StringName> allowed_types = allowed_types_with_convert;
  752. StringName custom_class;
  753. bool is_custom = false;
  754. if (edited_resource->get_script()) {
  755. custom_class = EditorNode::get_singleton()->get_object_custom_type_name(edited_resource->get_script());
  756. is_custom = _is_type_valid(custom_class, allowed_types);
  757. }
  758. if (!is_custom && !_is_type_valid(edited_resource->get_class(), allowed_types)) {
  759. String class_str = (custom_class == StringName() ? edited_resource->get_class() : vformat("%s (%s)", custom_class, edited_resource->get_class()));
  760. WARN_PRINT(vformat("Value mismatch between the new base type of this EditorResourcePicker, '%s', and the type of the value it already has, '%s'.", base_type, class_str));
  761. }
  762. }
  763. }
  764. String EditorResourcePicker::get_base_type() const {
  765. return base_type;
  766. }
  767. Vector<String> EditorResourcePicker::get_allowed_types() const {
  768. _ensure_allowed_types();
  769. HashSet<StringName> allowed_types = allowed_types_without_convert;
  770. Vector<String> types;
  771. types.resize(allowed_types.size());
  772. int i = 0;
  773. String *w = types.ptrw();
  774. for (const StringName &E : allowed_types) {
  775. w[i] = E;
  776. i++;
  777. }
  778. return types;
  779. }
  780. void EditorResourcePicker::set_edited_resource(Ref<Resource> p_resource) {
  781. if (p_resource.is_null()) {
  782. edited_resource = Ref<Resource>();
  783. _update_resource();
  784. return;
  785. }
  786. if (!base_type.is_empty()) {
  787. _ensure_allowed_types();
  788. HashSet<StringName> allowed_types = allowed_types_with_convert;
  789. StringName custom_class;
  790. bool is_custom = false;
  791. if (p_resource->get_script()) {
  792. custom_class = EditorNode::get_singleton()->get_object_custom_type_name(p_resource->get_script());
  793. is_custom = _is_type_valid(custom_class, allowed_types);
  794. }
  795. if (!is_custom && !_is_type_valid(p_resource->get_class(), allowed_types)) {
  796. String class_str = (custom_class == StringName() ? p_resource->get_class() : vformat("%s (%s)", custom_class, p_resource->get_class()));
  797. ERR_FAIL_MSG(vformat("Failed to set a resource of the type '%s' because this EditorResourcePicker only accepts '%s' and its derivatives.", class_str, base_type));
  798. }
  799. }
  800. set_edited_resource_no_check(p_resource);
  801. }
  802. void EditorResourcePicker::set_edited_resource_no_check(Ref<Resource> p_resource) {
  803. edited_resource = p_resource;
  804. _update_resource();
  805. }
  806. Ref<Resource> EditorResourcePicker::get_edited_resource() {
  807. return edited_resource;
  808. }
  809. void EditorResourcePicker::set_toggle_mode(bool p_enable) {
  810. assign_button->set_toggle_mode(p_enable);
  811. }
  812. bool EditorResourcePicker::is_toggle_mode() const {
  813. return assign_button->is_toggle_mode();
  814. }
  815. void EditorResourcePicker::set_toggle_pressed(bool p_pressed) {
  816. if (!is_toggle_mode()) {
  817. return;
  818. }
  819. assign_button->set_pressed(p_pressed);
  820. }
  821. bool EditorResourcePicker::is_toggle_pressed() const {
  822. return assign_button->is_pressed();
  823. }
  824. void EditorResourcePicker::set_resource_owner(Object *p_object) {
  825. resource_owner = p_object;
  826. }
  827. void EditorResourcePicker::set_editable(bool p_editable) {
  828. editable = p_editable;
  829. assign_button->set_disabled(!editable && edited_resource.is_null());
  830. quick_load_button->set_visible(editable && edited_resource.is_null());
  831. edit_button->set_visible(editable);
  832. }
  833. bool EditorResourcePicker::is_editable() const {
  834. return editable;
  835. }
  836. void EditorResourcePicker::_ensure_resource_menu() {
  837. if (edit_menu) {
  838. return;
  839. }
  840. edit_menu = memnew(PopupMenu);
  841. edit_menu->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)));
  842. add_child(edit_menu);
  843. edit_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorResourcePicker::_edit_menu_cbk));
  844. edit_menu->connect("popup_hide", callable_mp((BaseButton *)edit_button, &BaseButton::set_pressed).bind(false));
  845. }
  846. void EditorResourcePicker::_gather_resources_to_duplicate(const Ref<Resource> p_resource, TreeItem *p_item, const String &p_property_name) const {
  847. p_item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  848. String res_name = p_resource->get_name();
  849. if (res_name.is_empty() && !p_resource->is_built_in()) {
  850. res_name = p_resource->get_path().get_file();
  851. }
  852. if (res_name.is_empty()) {
  853. p_item->set_text(0, p_resource->get_class());
  854. } else {
  855. p_item->set_text(0, vformat("%s (%s)", p_resource->get_class(), res_name));
  856. }
  857. p_item->set_icon(0, EditorNode::get_singleton()->get_object_icon(p_resource.ptr()));
  858. p_item->set_editable(0, true);
  859. Array meta = { p_resource };
  860. p_item->set_metadata(0, meta);
  861. if (!p_property_name.is_empty()) {
  862. p_item->set_text(1, p_property_name);
  863. }
  864. static Vector<String> unique_exceptions = { "Image", "Shader", "Mesh", "FontFile" };
  865. if (!unique_exceptions.has(p_resource->get_class())) {
  866. // Automatically select resource, unless it's something that shouldn't be duplicated.
  867. p_item->set_checked(0, true);
  868. }
  869. List<PropertyInfo> plist;
  870. p_resource->get_property_list(&plist);
  871. for (const PropertyInfo &E : plist) {
  872. if (!(E.usage & PROPERTY_USAGE_STORAGE) || E.type != Variant::OBJECT || E.hint != PROPERTY_HINT_RESOURCE_TYPE) {
  873. continue;
  874. }
  875. Ref<Resource> res = p_resource->get(E.name);
  876. if (res.is_null()) {
  877. continue;
  878. }
  879. TreeItem *child = p_item->create_child();
  880. _gather_resources_to_duplicate(res, child, E.name);
  881. meta = child->get_metadata(0);
  882. // Remember property name.
  883. meta.append(E.name);
  884. if ((E.usage & PROPERTY_USAGE_NEVER_DUPLICATE)) {
  885. // The resource can't be duplicated, but make it appear on the list anyway.
  886. child->set_checked(0, false);
  887. child->set_editable(0, false);
  888. }
  889. }
  890. }
  891. void EditorResourcePicker::_duplicate_selected_resources() {
  892. for (TreeItem *item = duplicate_resources_tree->get_root(); item; item = item->get_next_in_tree()) {
  893. if (!item->is_checked(0)) {
  894. continue;
  895. }
  896. Array meta = item->get_metadata(0);
  897. Ref<Resource> res = meta[0];
  898. Ref<Resource> unique_resource = res->duplicate();
  899. ERR_FAIL_COND(unique_resource.is_null()); // duplicate() may fail.
  900. meta[0] = unique_resource;
  901. if (meta.size() == 1) { // Root.
  902. edited_resource = unique_resource;
  903. _resource_changed();
  904. } else {
  905. Array parent_meta = item->get_parent()->get_metadata(0);
  906. Ref<Resource> parent = parent_meta[0];
  907. parent->set(meta[1], unique_resource);
  908. }
  909. }
  910. }
  911. EditorResourcePicker::EditorResourcePicker(bool p_hide_assign_button_controls) {
  912. assign_button = memnew(Button);
  913. assign_button->set_flat(true);
  914. assign_button->set_h_size_flags(SIZE_EXPAND_FILL);
  915. assign_button->set_accessibility_name(TTRC("Assign Resource"));
  916. assign_button->set_expand_icon(true);
  917. assign_button->set_clip_text(true);
  918. assign_button->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  919. SET_DRAG_FORWARDING_GCD(assign_button, EditorResourcePicker);
  920. add_child(assign_button);
  921. assign_button->connect(SceneStringName(pressed), callable_mp(this, &EditorResourcePicker::_resource_selected));
  922. assign_button->connect(SceneStringName(draw), callable_mp(this, &EditorResourcePicker::_button_draw));
  923. assign_button->connect(SceneStringName(gui_input), callable_mp(this, &EditorResourcePicker::_button_input));
  924. if (!p_hide_assign_button_controls) {
  925. preview_rect = memnew(TextureRect);
  926. preview_rect->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  927. preview_rect->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  928. preview_rect->set_offset(SIDE_TOP, 1);
  929. preview_rect->set_offset(SIDE_BOTTOM, -1);
  930. preview_rect->set_offset(SIDE_RIGHT, -1);
  931. preview_rect->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  932. assign_button->add_child(preview_rect);
  933. }
  934. quick_load_button = memnew(Button);
  935. quick_load_button->set_tooltip_text(TTRC("Quick Load"));
  936. add_child(quick_load_button);
  937. quick_load_button->connect(SceneStringName(pressed), callable_mp(this, &EditorResourcePicker::_edit_menu_cbk).bind(OBJ_MENU_QUICKLOAD));
  938. edit_button = memnew(Button);
  939. edit_button->set_flat(false);
  940. edit_button->set_toggle_mode(true);
  941. edit_button->set_action_mode(BaseButton::ACTION_MODE_BUTTON_PRESS);
  942. edit_button->set_accessibility_name(TTRC("Edit"));
  943. add_child(edit_button);
  944. edit_button->connect(SceneStringName(pressed), callable_mp(this, &EditorResourcePicker::_update_menu));
  945. edit_button->connect(SceneStringName(gui_input), callable_mp(this, &EditorResourcePicker::_button_input));
  946. add_theme_constant_override("separation", 0);
  947. }
  948. // EditorScriptPicker
  949. void EditorScriptPicker::set_create_options(Object *p_menu_node) {
  950. PopupMenu *menu_node = Object::cast_to<PopupMenu>(p_menu_node);
  951. if (!menu_node) {
  952. return;
  953. }
  954. if (!(script_owner && script_owner->has_meta(SceneStringName(_custom_type_script)))) {
  955. menu_node->add_icon_item(get_editor_theme_icon(SNAME("ScriptCreate")), TTR("New Script..."), OBJ_MENU_NEW_SCRIPT);
  956. }
  957. if (script_owner) {
  958. Ref<Script> scr = script_owner->get_script();
  959. if (scr.is_valid()) {
  960. menu_node->add_icon_item(get_editor_theme_icon(SNAME("ScriptExtend")), TTR("Extend Script..."), OBJ_MENU_EXTEND_SCRIPT);
  961. }
  962. }
  963. menu_node->add_separator();
  964. }
  965. bool EditorScriptPicker::handle_menu_selected(int p_which) {
  966. switch (p_which) {
  967. case OBJ_MENU_NEW_SCRIPT: {
  968. if (script_owner) {
  969. SceneTreeDock::get_singleton()->open_script_dialog(script_owner, false);
  970. }
  971. return true;
  972. }
  973. case OBJ_MENU_EXTEND_SCRIPT: {
  974. if (script_owner) {
  975. SceneTreeDock::get_singleton()->open_script_dialog(script_owner, true);
  976. }
  977. return true;
  978. }
  979. }
  980. return false;
  981. }
  982. void EditorScriptPicker::set_script_owner(Node *p_owner) {
  983. script_owner = p_owner;
  984. }
  985. Node *EditorScriptPicker::get_script_owner() const {
  986. return script_owner;
  987. }
  988. void EditorScriptPicker::_bind_methods() {
  989. ClassDB::bind_method(D_METHOD("set_script_owner", "owner_node"), &EditorScriptPicker::set_script_owner);
  990. ClassDB::bind_method(D_METHOD("get_script_owner"), &EditorScriptPicker::get_script_owner);
  991. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "script_owner", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_script_owner", "get_script_owner");
  992. }
  993. // EditorShaderPicker
  994. void EditorShaderPicker::set_create_options(Object *p_menu_node) {
  995. PopupMenu *menu_node = Object::cast_to<PopupMenu>(p_menu_node);
  996. if (!menu_node) {
  997. return;
  998. }
  999. menu_node->add_icon_item(get_editor_theme_icon(SNAME("Shader")), TTR("New Shader..."), OBJ_MENU_NEW_SHADER);
  1000. menu_node->add_separator();
  1001. }
  1002. bool EditorShaderPicker::handle_menu_selected(int p_which) {
  1003. Ref<ShaderMaterial> ed_material = Ref<ShaderMaterial>(get_edited_material());
  1004. switch (p_which) {
  1005. case OBJ_MENU_NEW_SHADER: {
  1006. if (ed_material.is_valid()) {
  1007. SceneTreeDock::get_singleton()->open_shader_dialog(ed_material, preferred_mode);
  1008. return true;
  1009. }
  1010. } break;
  1011. default:
  1012. break;
  1013. }
  1014. return false;
  1015. }
  1016. void EditorShaderPicker::set_edited_material(ShaderMaterial *p_material) {
  1017. edited_material = p_material;
  1018. }
  1019. ShaderMaterial *EditorShaderPicker::get_edited_material() const {
  1020. return edited_material;
  1021. }
  1022. void EditorShaderPicker::set_preferred_mode(int p_mode) {
  1023. preferred_mode = p_mode;
  1024. }
  1025. //////////////
  1026. void EditorAudioStreamPicker::_notification(int p_what) {
  1027. switch (p_what) {
  1028. case NOTIFICATION_READY:
  1029. case NOTIFICATION_THEME_CHANGED: {
  1030. _update_resource();
  1031. } break;
  1032. case NOTIFICATION_INTERNAL_PROCESS: {
  1033. Ref<AudioStream> audio_stream = get_edited_resource();
  1034. if (audio_stream.is_valid()) {
  1035. if (audio_stream->get_length() > 0) {
  1036. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(audio_stream);
  1037. if (preview.is_valid()) {
  1038. if (preview->get_version() != last_preview_version) {
  1039. stream_preview_rect->queue_redraw();
  1040. last_preview_version = preview->get_version();
  1041. }
  1042. }
  1043. }
  1044. uint64_t tagged_frame = audio_stream->get_tagged_frame();
  1045. uint64_t diff_frames = AudioServer::get_singleton()->get_mixed_frames() - tagged_frame;
  1046. uint64_t diff_msec = diff_frames * 1000 / AudioServer::get_singleton()->get_mix_rate();
  1047. if (diff_msec < 300) {
  1048. uint32_t count = audio_stream->get_tagged_frame_count();
  1049. bool differ = false;
  1050. if (count != tagged_frame_offset_count) {
  1051. differ = true;
  1052. }
  1053. float offsets[MAX_TAGGED_FRAMES];
  1054. for (uint32_t i = 0; i < MIN(count, uint32_t(MAX_TAGGED_FRAMES)); i++) {
  1055. offsets[i] = audio_stream->get_tagged_frame_offset(i);
  1056. if (offsets[i] != tagged_frame_offsets[i]) {
  1057. differ = true;
  1058. }
  1059. }
  1060. if (differ) {
  1061. tagged_frame_offset_count = count;
  1062. for (uint32_t i = 0; i < count; i++) {
  1063. tagged_frame_offsets[i] = offsets[i];
  1064. }
  1065. }
  1066. stream_preview_rect->queue_redraw();
  1067. } else {
  1068. if (tagged_frame_offset_count != 0) {
  1069. stream_preview_rect->queue_redraw();
  1070. }
  1071. tagged_frame_offset_count = 0;
  1072. }
  1073. }
  1074. } break;
  1075. }
  1076. }
  1077. void EditorAudioStreamPicker::_update_resource() {
  1078. EditorResourcePicker::_update_resource();
  1079. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  1080. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  1081. Ref<AudioStream> audio_stream = get_edited_resource();
  1082. if (audio_stream.is_valid() && audio_stream->get_length() > 0.0) {
  1083. set_assign_button_min_size(Size2(1, font->get_height(font_size) * 3));
  1084. } else {
  1085. set_assign_button_min_size(Size2(1, font->get_height(font_size) * 1.5));
  1086. }
  1087. stream_preview_rect->queue_redraw();
  1088. }
  1089. void EditorAudioStreamPicker::_preview_draw() {
  1090. Ref<AudioStream> audio_stream = get_edited_resource();
  1091. if (audio_stream.is_null()) {
  1092. get_assign_button()->set_text(TTR("<empty>"));
  1093. return;
  1094. }
  1095. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  1096. get_assign_button()->set_text("");
  1097. Size2i size = stream_preview_rect->get_size();
  1098. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  1099. Rect2 rect(Point2(), size);
  1100. if (audio_stream->get_length() > 0 && size.width > 0) {
  1101. rect.size.height *= 0.5;
  1102. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(audio_stream);
  1103. float preview_len = preview->get_length();
  1104. Vector<Vector2> points;
  1105. points.resize(size.width * 2);
  1106. for (int i = 0; i < size.width; i++) {
  1107. float ofs = i * preview_len / size.width;
  1108. float ofs_n = (i + 1) * preview_len / size.width;
  1109. float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5;
  1110. float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5;
  1111. int idx = i;
  1112. points.write[idx * 2 + 0] = Vector2(i + 1, rect.position.y + min * rect.size.y);
  1113. points.write[idx * 2 + 1] = Vector2(i + 1, rect.position.y + max * rect.size.y);
  1114. }
  1115. Vector<Color> colors = { get_theme_color(SNAME("contrast_color_2"), EditorStringName(Editor)) };
  1116. RS::get_singleton()->canvas_item_add_multiline(stream_preview_rect->get_canvas_item(), points, colors);
  1117. if (tagged_frame_offset_count) {
  1118. Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  1119. for (uint32_t i = 0; i < tagged_frame_offset_count; i++) {
  1120. int x = CLAMP(tagged_frame_offsets[i] * size.width / preview_len, 0, size.width);
  1121. if (x == 0) {
  1122. continue; // Because some may always return 0, ignore offset 0.
  1123. }
  1124. stream_preview_rect->draw_rect(Rect2i(x, 0, 2, rect.size.height), accent);
  1125. }
  1126. }
  1127. rect.position.y += rect.size.height;
  1128. }
  1129. Ref<Texture2D> icon;
  1130. Color icon_modulate(1, 1, 1, 1);
  1131. if (tagged_frame_offset_count > 0) {
  1132. icon = get_editor_theme_icon(SNAME("Play"));
  1133. if ((OS::get_singleton()->get_ticks_msec() % 500) > 250) {
  1134. icon_modulate = Color(1, 0.5, 0.5, 1); // get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  1135. }
  1136. } else {
  1137. icon = EditorNode::get_singleton()->get_object_icon(audio_stream.operator->(), "Object");
  1138. }
  1139. String text;
  1140. if (!audio_stream->get_name().is_empty()) {
  1141. text = audio_stream->get_name();
  1142. } else if (audio_stream->get_path().is_resource_file()) {
  1143. text = audio_stream->get_path().get_file();
  1144. } else {
  1145. text = audio_stream->get_class().replace_first("AudioStream", "");
  1146. }
  1147. stream_preview_rect->draw_texture(icon, Point2i(EDSCALE * 4, rect.position.y + (rect.size.height - icon->get_height()) / 2), icon_modulate);
  1148. stream_preview_rect->draw_string(font, Point2i(EDSCALE * 4 + icon->get_width(), rect.position.y + font->get_ascent(font_size) + (rect.size.height - font->get_height(font_size)) / 2), text, HORIZONTAL_ALIGNMENT_CENTER, size.width - 4 * EDSCALE - icon->get_width(), font_size, get_theme_color(SceneStringName(font_color), EditorStringName(Editor)));
  1149. }
  1150. EditorAudioStreamPicker::EditorAudioStreamPicker() :
  1151. EditorResourcePicker(true) {
  1152. stream_preview_rect = memnew(Control);
  1153. stream_preview_rect->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  1154. stream_preview_rect->set_offset(SIDE_TOP, 1);
  1155. stream_preview_rect->set_offset(SIDE_BOTTOM, -1);
  1156. stream_preview_rect->set_offset(SIDE_RIGHT, -1);
  1157. stream_preview_rect->set_mouse_filter(MOUSE_FILTER_IGNORE);
  1158. stream_preview_rect->connect(SceneStringName(draw), callable_mp(this, &EditorAudioStreamPicker::_preview_draw));
  1159. get_assign_button()->add_child(stream_preview_rect);
  1160. get_assign_button()->move_child(stream_preview_rect, 0);
  1161. set_process_internal(true);
  1162. }