editor_resource_picker.cpp 46 KB

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