editor_resource_picker.cpp 47 KB

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