editor_resource_picker.cpp 50 KB

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