editor_resource_picker.cpp 62 KB

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