editor_resource_picker.cpp 41 KB

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