editor_resource_picker.cpp 42 KB

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