editor_resource_picker.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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_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. 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 == "Texture2D" && 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. edit_button->set_icon(get_theme_icon(SNAME("select_arrow"), SNAME("Tree")));
  649. } break;
  650. case NOTIFICATION_DRAW: {
  651. draw_style_box(get_theme_stylebox(SNAME("panel"), SNAME("Tree")), Rect2(Point2(), get_size()));
  652. } break;
  653. case NOTIFICATION_DRAG_BEGIN: {
  654. if (editable && _is_drop_valid(get_viewport()->gui_get_drag_data())) {
  655. dropping = true;
  656. assign_button->queue_redraw();
  657. }
  658. } break;
  659. case NOTIFICATION_DRAG_END: {
  660. if (dropping) {
  661. dropping = false;
  662. assign_button->queue_redraw();
  663. }
  664. } break;
  665. }
  666. }
  667. void EditorResourcePicker::set_base_type(const String &p_base_type) {
  668. base_type = p_base_type;
  669. // There is a possibility that the new base type is conflicting with the existing value.
  670. // Keep the value, but warn the user that there is a potential mistake.
  671. if (!base_type.is_empty() && edited_resource.is_valid()) {
  672. HashSet<StringName> allowed_types;
  673. _get_allowed_types(true, &allowed_types);
  674. StringName custom_class;
  675. bool is_custom = false;
  676. if (edited_resource->get_script()) {
  677. custom_class = EditorNode::get_singleton()->get_object_custom_type_name(edited_resource->get_script());
  678. is_custom = _is_type_valid(custom_class, allowed_types);
  679. }
  680. if (!is_custom && !_is_type_valid(edited_resource->get_class(), allowed_types)) {
  681. String class_str = (custom_class == StringName() ? edited_resource->get_class() : vformat("%s (%s)", custom_class, edited_resource->get_class()));
  682. 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));
  683. }
  684. }
  685. }
  686. String EditorResourcePicker::get_base_type() const {
  687. return base_type;
  688. }
  689. Vector<String> EditorResourcePicker::get_allowed_types() const {
  690. HashSet<StringName> allowed_types;
  691. _get_allowed_types(false, &allowed_types);
  692. Vector<String> types;
  693. types.resize(allowed_types.size());
  694. int i = 0;
  695. String *w = types.ptrw();
  696. for (const StringName &E : allowed_types) {
  697. w[i] = E;
  698. i++;
  699. }
  700. return types;
  701. }
  702. void EditorResourcePicker::set_edited_resource(Ref<Resource> p_resource) {
  703. if (!p_resource.is_valid()) {
  704. edited_resource = Ref<Resource>();
  705. _update_resource();
  706. return;
  707. }
  708. if (!base_type.is_empty()) {
  709. HashSet<StringName> allowed_types;
  710. _get_allowed_types(true, &allowed_types);
  711. StringName custom_class;
  712. bool is_custom = false;
  713. if (p_resource->get_script()) {
  714. custom_class = EditorNode::get_singleton()->get_object_custom_type_name(p_resource->get_script());
  715. is_custom = _is_type_valid(custom_class, allowed_types);
  716. }
  717. if (!is_custom && !_is_type_valid(p_resource->get_class(), allowed_types)) {
  718. String class_str = (custom_class == StringName() ? p_resource->get_class() : vformat("%s (%s)", custom_class, p_resource->get_class()));
  719. 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));
  720. }
  721. }
  722. edited_resource = p_resource;
  723. _update_resource();
  724. }
  725. Ref<Resource> EditorResourcePicker::get_edited_resource() {
  726. return edited_resource;
  727. }
  728. void EditorResourcePicker::set_toggle_mode(bool p_enable) {
  729. assign_button->set_toggle_mode(p_enable);
  730. }
  731. bool EditorResourcePicker::is_toggle_mode() const {
  732. return assign_button->is_toggle_mode();
  733. }
  734. void EditorResourcePicker::set_toggle_pressed(bool p_pressed) {
  735. if (!is_toggle_mode()) {
  736. return;
  737. }
  738. assign_button->set_pressed(p_pressed);
  739. }
  740. void EditorResourcePicker::set_editable(bool p_editable) {
  741. editable = p_editable;
  742. assign_button->set_disabled(!editable && !edited_resource.is_valid());
  743. edit_button->set_visible(editable);
  744. }
  745. bool EditorResourcePicker::is_editable() const {
  746. return editable;
  747. }
  748. void EditorResourcePicker::_ensure_resource_menu() {
  749. if (edit_menu) {
  750. return;
  751. }
  752. edit_menu = memnew(PopupMenu);
  753. add_child(edit_menu);
  754. edit_menu->connect("id_pressed", callable_mp(this, &EditorResourcePicker::_edit_menu_cbk));
  755. edit_menu->connect("popup_hide", callable_mp((BaseButton *)edit_button, &BaseButton::set_pressed).bind(false));
  756. }
  757. EditorResourcePicker::EditorResourcePicker(bool p_hide_assign_button_controls) {
  758. assign_button = memnew(Button);
  759. assign_button->set_flat(true);
  760. assign_button->set_h_size_flags(SIZE_EXPAND_FILL);
  761. assign_button->set_clip_text(true);
  762. assign_button->set_auto_translate(false);
  763. SET_DRAG_FORWARDING_GCD(assign_button, EditorResourcePicker);
  764. add_child(assign_button);
  765. assign_button->connect("pressed", callable_mp(this, &EditorResourcePicker::_resource_selected));
  766. assign_button->connect("draw", callable_mp(this, &EditorResourcePicker::_button_draw));
  767. assign_button->connect("gui_input", callable_mp(this, &EditorResourcePicker::_button_input));
  768. if (!p_hide_assign_button_controls) {
  769. preview_rect = memnew(TextureRect);
  770. preview_rect->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  771. preview_rect->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  772. preview_rect->set_offset(SIDE_TOP, 1);
  773. preview_rect->set_offset(SIDE_BOTTOM, -1);
  774. preview_rect->set_offset(SIDE_RIGHT, -1);
  775. preview_rect->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  776. assign_button->add_child(preview_rect);
  777. }
  778. edit_button = memnew(Button);
  779. edit_button->set_flat(true);
  780. edit_button->set_toggle_mode(true);
  781. edit_button->connect("pressed", callable_mp(this, &EditorResourcePicker::_update_menu));
  782. add_child(edit_button);
  783. edit_button->connect("gui_input", callable_mp(this, &EditorResourcePicker::_button_input));
  784. add_theme_constant_override("separation", 0);
  785. }
  786. // EditorScriptPicker
  787. void EditorScriptPicker::set_create_options(Object *p_menu_node) {
  788. PopupMenu *menu_node = Object::cast_to<PopupMenu>(p_menu_node);
  789. if (!menu_node) {
  790. return;
  791. }
  792. menu_node->add_icon_item(get_theme_icon(SNAME("ScriptCreate"), SNAME("EditorIcons")), TTR("New Script"), OBJ_MENU_NEW_SCRIPT);
  793. if (script_owner) {
  794. Ref<Script> scr = script_owner->get_script();
  795. if (scr.is_valid()) {
  796. menu_node->add_icon_item(get_theme_icon(SNAME("ScriptExtend"), SNAME("EditorIcons")), TTR("Extend Script"), OBJ_MENU_EXTEND_SCRIPT);
  797. }
  798. }
  799. menu_node->add_separator();
  800. }
  801. bool EditorScriptPicker::handle_menu_selected(int p_which) {
  802. switch (p_which) {
  803. case OBJ_MENU_NEW_SCRIPT: {
  804. if (script_owner) {
  805. SceneTreeDock::get_singleton()->open_script_dialog(script_owner, false);
  806. }
  807. return true;
  808. }
  809. case OBJ_MENU_EXTEND_SCRIPT: {
  810. if (script_owner) {
  811. SceneTreeDock::get_singleton()->open_script_dialog(script_owner, true);
  812. }
  813. return true;
  814. }
  815. }
  816. return false;
  817. }
  818. void EditorScriptPicker::set_script_owner(Node *p_owner) {
  819. script_owner = p_owner;
  820. }
  821. Node *EditorScriptPicker::get_script_owner() const {
  822. return script_owner;
  823. }
  824. void EditorScriptPicker::_bind_methods() {
  825. ClassDB::bind_method(D_METHOD("set_script_owner", "owner_node"), &EditorScriptPicker::set_script_owner);
  826. ClassDB::bind_method(D_METHOD("get_script_owner"), &EditorScriptPicker::get_script_owner);
  827. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "script_owner", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_script_owner", "get_script_owner");
  828. }
  829. EditorScriptPicker::EditorScriptPicker() {
  830. }
  831. // EditorShaderPicker
  832. void EditorShaderPicker::set_create_options(Object *p_menu_node) {
  833. PopupMenu *menu_node = Object::cast_to<PopupMenu>(p_menu_node);
  834. if (!menu_node) {
  835. return;
  836. }
  837. menu_node->add_icon_item(get_theme_icon(SNAME("Shader"), SNAME("EditorIcons")), TTR("New Shader"), OBJ_MENU_NEW_SHADER);
  838. menu_node->add_separator();
  839. }
  840. bool EditorShaderPicker::handle_menu_selected(int p_which) {
  841. Ref<ShaderMaterial> ed_material = Ref<ShaderMaterial>(get_edited_material());
  842. switch (p_which) {
  843. case OBJ_MENU_NEW_SHADER: {
  844. if (ed_material.is_valid()) {
  845. SceneTreeDock::get_singleton()->open_shader_dialog(ed_material, preferred_mode);
  846. return true;
  847. }
  848. } break;
  849. default:
  850. break;
  851. }
  852. return false;
  853. }
  854. void EditorShaderPicker::set_edited_material(ShaderMaterial *p_material) {
  855. edited_material = p_material;
  856. }
  857. ShaderMaterial *EditorShaderPicker::get_edited_material() const {
  858. return edited_material;
  859. }
  860. void EditorShaderPicker::set_preferred_mode(int p_mode) {
  861. preferred_mode = p_mode;
  862. }
  863. EditorShaderPicker::EditorShaderPicker() {
  864. }
  865. //////////////
  866. void EditorAudioStreamPicker::_notification(int p_what) {
  867. switch (p_what) {
  868. case NOTIFICATION_READY:
  869. case NOTIFICATION_THEME_CHANGED: {
  870. _update_resource();
  871. } break;
  872. case NOTIFICATION_INTERNAL_PROCESS: {
  873. Ref<AudioStream> audio_stream = get_edited_resource();
  874. if (audio_stream.is_valid()) {
  875. if (audio_stream->get_length() > 0) {
  876. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(audio_stream);
  877. if (preview.is_valid()) {
  878. if (preview->get_version() != last_preview_version) {
  879. stream_preview_rect->queue_redraw();
  880. last_preview_version = preview->get_version();
  881. }
  882. }
  883. }
  884. uint64_t tagged_frame = audio_stream->get_tagged_frame();
  885. uint64_t diff_frames = AudioServer::get_singleton()->get_mixed_frames() - tagged_frame;
  886. uint64_t diff_msec = diff_frames * 1000 / AudioServer::get_singleton()->get_mix_rate();
  887. if (diff_msec < 300) {
  888. uint32_t count = audio_stream->get_tagged_frame_count();
  889. bool differ = false;
  890. if (count != tagged_frame_offset_count) {
  891. differ = true;
  892. }
  893. float offsets[MAX_TAGGED_FRAMES];
  894. for (uint32_t i = 0; i < MIN(count, uint32_t(MAX_TAGGED_FRAMES)); i++) {
  895. offsets[i] = audio_stream->get_tagged_frame_offset(i);
  896. if (offsets[i] != tagged_frame_offsets[i]) {
  897. differ = true;
  898. }
  899. }
  900. if (differ) {
  901. tagged_frame_offset_count = count;
  902. for (uint32_t i = 0; i < count; i++) {
  903. tagged_frame_offsets[i] = offsets[i];
  904. }
  905. }
  906. stream_preview_rect->queue_redraw();
  907. } else {
  908. if (tagged_frame_offset_count != 0) {
  909. stream_preview_rect->queue_redraw();
  910. }
  911. tagged_frame_offset_count = 0;
  912. }
  913. }
  914. } break;
  915. }
  916. }
  917. void EditorAudioStreamPicker::_update_resource() {
  918. EditorResourcePicker::_update_resource();
  919. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  920. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  921. Ref<AudioStream> audio_stream = get_edited_resource();
  922. if (audio_stream.is_valid() && audio_stream->get_length() > 0.0) {
  923. set_assign_button_min_size(Size2(1, font->get_height(font_size) * 3));
  924. } else {
  925. set_assign_button_min_size(Size2(1, font->get_height(font_size) * 1.5));
  926. }
  927. stream_preview_rect->queue_redraw();
  928. }
  929. void EditorAudioStreamPicker::_preview_draw() {
  930. Ref<AudioStream> audio_stream = get_edited_resource();
  931. if (!audio_stream.is_valid()) {
  932. get_assign_button()->set_text(TTR("<empty>"));
  933. return;
  934. }
  935. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  936. get_assign_button()->set_text("");
  937. Size2i size = stream_preview_rect->get_size();
  938. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  939. Rect2 rect(Point2(), size);
  940. if (audio_stream->get_length() > 0) {
  941. rect.size.height *= 0.5;
  942. stream_preview_rect->draw_rect(rect, Color(0, 0, 0, 1));
  943. Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(audio_stream);
  944. float preview_len = preview->get_length();
  945. Vector<Vector2> lines;
  946. lines.resize(size.width * 2);
  947. for (int i = 0; i < size.width; i++) {
  948. float ofs = i * preview_len / size.width;
  949. float ofs_n = (i + 1) * preview_len / size.width;
  950. float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5;
  951. float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5;
  952. int idx = i;
  953. lines.write[idx * 2 + 0] = Vector2(i + 1, rect.position.y + min * rect.size.y);
  954. lines.write[idx * 2 + 1] = Vector2(i + 1, rect.position.y + max * rect.size.y);
  955. }
  956. Vector<Color> color;
  957. color.push_back(get_theme_color(SNAME("contrast_color_2"), SNAME("Editor")));
  958. RS::get_singleton()->canvas_item_add_multiline(stream_preview_rect->get_canvas_item(), lines, color);
  959. if (tagged_frame_offset_count) {
  960. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  961. for (uint32_t i = 0; i < tagged_frame_offset_count; i++) {
  962. int x = CLAMP(tagged_frame_offsets[i] * size.width / preview_len, 0, size.width);
  963. if (x == 0) {
  964. continue; // Because some may always return 0, ignore offset 0.
  965. }
  966. stream_preview_rect->draw_rect(Rect2i(x, 0, 2, rect.size.height), accent);
  967. }
  968. }
  969. rect.position.y += rect.size.height;
  970. }
  971. Ref<Texture2D> icon;
  972. Color icon_modulate(1, 1, 1, 1);
  973. if (tagged_frame_offset_count > 0) {
  974. icon = get_theme_icon(SNAME("Play"), SNAME("EditorIcons"));
  975. if ((OS::get_singleton()->get_ticks_msec() % 500) > 250) {
  976. icon_modulate = Color(1, 0.5, 0.5, 1); // get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  977. }
  978. } else {
  979. icon = EditorNode::get_singleton()->get_object_icon(audio_stream.operator->(), "Object");
  980. }
  981. String text;
  982. if (!audio_stream->get_name().is_empty()) {
  983. text = audio_stream->get_name();
  984. } else if (audio_stream->get_path().is_resource_file()) {
  985. text = audio_stream->get_path().get_file();
  986. } else {
  987. text = audio_stream->get_class().replace_first("AudioStream", "");
  988. }
  989. stream_preview_rect->draw_texture(icon, Point2i(EDSCALE * 2, rect.position.y + (rect.size.height - icon->get_height()) / 2), icon_modulate);
  990. 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());
  991. }
  992. EditorAudioStreamPicker::EditorAudioStreamPicker() :
  993. EditorResourcePicker(true) {
  994. stream_preview_rect = memnew(Control);
  995. stream_preview_rect->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  996. stream_preview_rect->set_offset(SIDE_TOP, 1);
  997. stream_preview_rect->set_offset(SIDE_BOTTOM, -1);
  998. stream_preview_rect->set_offset(SIDE_RIGHT, -1);
  999. stream_preview_rect->set_mouse_filter(MOUSE_FILTER_IGNORE);
  1000. stream_preview_rect->connect("draw", callable_mp(this, &EditorAudioStreamPicker::_preview_draw));
  1001. get_assign_button()->add_child(stream_preview_rect);
  1002. get_assign_button()->move_child(stream_preview_rect, 0);
  1003. set_process_internal(true);
  1004. }