editor_resource_picker.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. /*************************************************************************/
  2. /* editor_resource_picker.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "editor_resource_picker.h"
  31. #include "editor/editor_resource_preview.h"
  32. #include "editor_node.h"
  33. #include "editor_scale.h"
  34. #include "editor_settings.h"
  35. #include "filesystem_dock.h"
  36. HashMap<StringName, List<StringName>> EditorResourcePicker::allowed_types_cache;
  37. void EditorResourcePicker::clear_caches() {
  38. allowed_types_cache.clear();
  39. }
  40. void EditorResourcePicker::_update_resource() {
  41. preview_rect->set_texture(Ref<Texture2D>());
  42. assign_button->set_custom_minimum_size(Size2(1, 1));
  43. if (edited_resource == RES()) {
  44. assign_button->set_icon(Ref<Texture2D>());
  45. assign_button->set_text(TTR("[empty]"));
  46. } else {
  47. assign_button->set_icon(EditorNode::get_singleton()->get_object_icon(edited_resource.operator->(), "Object"));
  48. if (edited_resource->get_name() != String()) {
  49. assign_button->set_text(edited_resource->get_name());
  50. } else if (edited_resource->get_path().is_resource_file()) {
  51. assign_button->set_text(edited_resource->get_path().get_file());
  52. assign_button->set_tooltip(edited_resource->get_path());
  53. } else {
  54. assign_button->set_text(edited_resource->get_class());
  55. }
  56. if (edited_resource->get_path().is_resource_file()) {
  57. assign_button->set_tooltip(edited_resource->get_path());
  58. }
  59. // Preview will override the above, so called at the end.
  60. EditorResourcePreview::get_singleton()->queue_edited_resource_preview(edited_resource, this, "_update_resource_preview", edited_resource->get_instance_id());
  61. }
  62. }
  63. void EditorResourcePicker::_update_resource_preview(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, ObjectID p_obj) {
  64. if (!edited_resource.is_valid() || edited_resource->get_instance_id() != p_obj) {
  65. return;
  66. }
  67. String type = edited_resource->get_class_name();
  68. if (ClassDB::is_parent_class(type, "Script")) {
  69. assign_button->set_text(edited_resource->get_path().get_file());
  70. return;
  71. }
  72. if (p_preview.is_valid()) {
  73. preview_rect->set_offset(SIDE_LEFT, assign_button->get_icon()->get_width() + assign_button->get_theme_stylebox(SNAME("normal"))->get_default_margin(SIDE_LEFT) + get_theme_constant(SNAME("hseparation"), SNAME("Button")));
  74. if (type == "GradientTexture1D") {
  75. preview_rect->set_stretch_mode(TextureRect::STRETCH_SCALE);
  76. assign_button->set_custom_minimum_size(Size2(1, 1));
  77. } else {
  78. preview_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  79. int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size");
  80. thumbnail_size *= EDSCALE;
  81. assign_button->set_custom_minimum_size(Size2(1, thumbnail_size));
  82. }
  83. preview_rect->set_texture(p_preview);
  84. assign_button->set_text("");
  85. }
  86. }
  87. void EditorResourcePicker::_resource_selected() {
  88. if (edited_resource.is_null()) {
  89. edit_button->set_pressed(true);
  90. _update_menu();
  91. return;
  92. }
  93. emit_signal(SNAME("resource_selected"), edited_resource, false);
  94. }
  95. void EditorResourcePicker::_file_selected(const String &p_path) {
  96. RES loaded_resource = ResourceLoader::load(p_path);
  97. ERR_FAIL_COND_MSG(loaded_resource.is_null(), "Cannot load resource from path '" + p_path + "'.");
  98. if (base_type != "") {
  99. bool any_type_matches = false;
  100. for (int i = 0; i < base_type.get_slice_count(","); i++) {
  101. String base = base_type.get_slice(",", i);
  102. if (loaded_resource->is_class(base)) {
  103. any_type_matches = true;
  104. break;
  105. }
  106. }
  107. if (!any_type_matches) {
  108. EditorNode::get_singleton()->show_warning(vformat(TTR("The selected resource (%s) does not match any type expected for this property (%s)."), loaded_resource->get_class(), base_type));
  109. return;
  110. }
  111. }
  112. edited_resource = loaded_resource;
  113. emit_signal(SNAME("resource_changed"), edited_resource);
  114. _update_resource();
  115. }
  116. void EditorResourcePicker::_file_quick_selected() {
  117. _file_selected(quick_open->get_selected());
  118. }
  119. void EditorResourcePicker::_update_menu() {
  120. _update_menu_items();
  121. Rect2 gt = edit_button->get_screen_rect();
  122. edit_menu->set_as_minsize();
  123. int ms = edit_menu->get_contents_minimum_size().width;
  124. Vector2 popup_pos = gt.get_end() - Vector2(ms, 0);
  125. edit_menu->set_position(popup_pos);
  126. edit_menu->popup();
  127. }
  128. void EditorResourcePicker::_update_menu_items() {
  129. _ensure_resource_menu();
  130. edit_menu->clear();
  131. // Add options for creating specific subtypes of the base resource type.
  132. set_create_options(edit_menu);
  133. // Add an option to load a resource from a file using the QuickOpen dialog.
  134. edit_menu->add_icon_item(get_theme_icon(SNAME("Load"), SNAME("EditorIcons")), TTR("Quick Load"), OBJ_MENU_QUICKLOAD);
  135. // Add an option to load a resource from a file using the regular file dialog.
  136. edit_menu->add_icon_item(get_theme_icon(SNAME("Load"), SNAME("EditorIcons")), TTR("Load"), OBJ_MENU_LOAD);
  137. // Add options for changing existing value of the resource.
  138. if (edited_resource.is_valid()) {
  139. edit_menu->add_icon_item(get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")), TTR("Edit"), OBJ_MENU_EDIT);
  140. edit_menu->add_icon_item(get_theme_icon(SNAME("Clear"), SNAME("EditorIcons")), TTR("Clear"), OBJ_MENU_CLEAR);
  141. edit_menu->add_icon_item(get_theme_icon(SNAME("Duplicate"), SNAME("EditorIcons")), TTR("Make Unique"), OBJ_MENU_MAKE_UNIQUE);
  142. edit_menu->add_icon_item(get_theme_icon(SNAME("Save"), SNAME("EditorIcons")), TTR("Save"), OBJ_MENU_SAVE);
  143. if (edited_resource->get_path().is_resource_file()) {
  144. edit_menu->add_separator();
  145. edit_menu->add_item(TTR("Show in FileSystem"), OBJ_MENU_SHOW_IN_FILE_SYSTEM);
  146. }
  147. }
  148. // Add options to copy/paste resource.
  149. RES cb = EditorSettings::get_singleton()->get_resource_clipboard();
  150. bool paste_valid = false;
  151. if (cb.is_valid()) {
  152. if (base_type == "") {
  153. paste_valid = true;
  154. } else {
  155. for (int i = 0; i < base_type.get_slice_count(","); i++) {
  156. if (ClassDB::is_parent_class(cb->get_class(), base_type.get_slice(",", i))) {
  157. paste_valid = true;
  158. break;
  159. }
  160. }
  161. }
  162. }
  163. if (edited_resource.is_valid() || paste_valid) {
  164. edit_menu->add_separator();
  165. if (edited_resource.is_valid()) {
  166. edit_menu->add_item(TTR("Copy"), OBJ_MENU_COPY);
  167. }
  168. if (paste_valid) {
  169. edit_menu->add_item(TTR("Paste"), OBJ_MENU_PASTE);
  170. }
  171. }
  172. // Add options to convert existing resource to another type of resource.
  173. if (edited_resource.is_valid()) {
  174. Vector<Ref<EditorResourceConversionPlugin>> conversions = EditorNode::get_singleton()->find_resource_conversion_plugin(edited_resource);
  175. if (conversions.size()) {
  176. edit_menu->add_separator();
  177. }
  178. for (int i = 0; i < conversions.size(); i++) {
  179. String what = conversions[i]->converts_to();
  180. Ref<Texture2D> icon;
  181. if (has_theme_icon(what, SNAME("EditorIcons"))) {
  182. icon = get_theme_icon(what, SNAME("EditorIcons"));
  183. } else {
  184. icon = get_theme_icon(what, SNAME("Resource"));
  185. }
  186. edit_menu->add_icon_item(icon, vformat(TTR("Convert to %s"), what), CONVERT_BASE_ID + i);
  187. }
  188. }
  189. }
  190. void EditorResourcePicker::_edit_menu_cbk(int p_which) {
  191. switch (p_which) {
  192. case OBJ_MENU_LOAD: {
  193. List<String> extensions;
  194. for (int i = 0; i < base_type.get_slice_count(","); i++) {
  195. String base = base_type.get_slice(",", i);
  196. ResourceLoader::get_recognized_extensions_for_type(base, &extensions);
  197. }
  198. Set<String> valid_extensions;
  199. for (const String &E : extensions) {
  200. valid_extensions.insert(E);
  201. }
  202. if (!file_dialog) {
  203. file_dialog = memnew(EditorFileDialog);
  204. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  205. add_child(file_dialog);
  206. file_dialog->connect("file_selected", callable_mp(this, &EditorResourcePicker::_file_selected));
  207. }
  208. file_dialog->clear_filters();
  209. for (Set<String>::Element *E = valid_extensions.front(); E; E = E->next()) {
  210. file_dialog->add_filter("*." + E->get() + " ; " + E->get().to_upper());
  211. }
  212. file_dialog->popup_file_dialog();
  213. } break;
  214. case OBJ_MENU_QUICKLOAD: {
  215. if (!quick_open) {
  216. quick_open = memnew(EditorQuickOpen);
  217. add_child(quick_open);
  218. quick_open->connect("quick_open", callable_mp(this, &EditorResourcePicker::_file_quick_selected));
  219. }
  220. quick_open->popup_dialog(base_type);
  221. quick_open->set_title(TTR("Resource"));
  222. } break;
  223. case OBJ_MENU_EDIT: {
  224. if (edited_resource.is_valid()) {
  225. emit_signal(SNAME("resource_selected"), edited_resource, true);
  226. }
  227. } break;
  228. case OBJ_MENU_CLEAR: {
  229. edited_resource = RES();
  230. emit_signal(SNAME("resource_changed"), edited_resource);
  231. _update_resource();
  232. } break;
  233. case OBJ_MENU_MAKE_UNIQUE: {
  234. if (edited_resource.is_null()) {
  235. return;
  236. }
  237. List<PropertyInfo> property_list;
  238. edited_resource->get_property_list(&property_list);
  239. List<Pair<String, Variant>> propvalues;
  240. for (const PropertyInfo &pi : property_list) {
  241. Pair<String, Variant> p;
  242. if (pi.usage & PROPERTY_USAGE_STORAGE) {
  243. p.first = pi.name;
  244. p.second = edited_resource->get(pi.name);
  245. }
  246. propvalues.push_back(p);
  247. }
  248. String orig_type = edited_resource->get_class();
  249. Object *inst = ClassDB::instantiate(orig_type);
  250. Ref<Resource> unique_resource = Ref<Resource>(Object::cast_to<Resource>(inst));
  251. ERR_FAIL_COND(unique_resource.is_null());
  252. for (const Pair<String, Variant> &p : propvalues) {
  253. unique_resource->set(p.first, p.second);
  254. }
  255. edited_resource = unique_resource;
  256. emit_signal(SNAME("resource_changed"), edited_resource);
  257. _update_resource();
  258. } break;
  259. case OBJ_MENU_SAVE: {
  260. if (edited_resource.is_null()) {
  261. return;
  262. }
  263. EditorNode::get_singleton()->save_resource(edited_resource);
  264. } break;
  265. case OBJ_MENU_COPY: {
  266. EditorSettings::get_singleton()->set_resource_clipboard(edited_resource);
  267. } break;
  268. case OBJ_MENU_PASTE: {
  269. edited_resource = EditorSettings::get_singleton()->get_resource_clipboard();
  270. emit_signal(SNAME("resource_changed"), edited_resource);
  271. _update_resource();
  272. } break;
  273. case OBJ_MENU_SHOW_IN_FILE_SYSTEM: {
  274. FileSystemDock *file_system_dock = EditorNode::get_singleton()->get_filesystem_dock();
  275. file_system_dock->navigate_to_path(edited_resource->get_path());
  276. // Ensure that the FileSystem dock is visible.
  277. TabContainer *tab_container = (TabContainer *)file_system_dock->get_parent_control();
  278. tab_container->set_current_tab(file_system_dock->get_index());
  279. } break;
  280. default: {
  281. // Allow subclasses to handle their own options first, only then fallback on the default branch logic.
  282. if (handle_menu_selected(p_which)) {
  283. break;
  284. }
  285. if (p_which >= CONVERT_BASE_ID) {
  286. int to_type = p_which - CONVERT_BASE_ID;
  287. Vector<Ref<EditorResourceConversionPlugin>> conversions = EditorNode::get_singleton()->find_resource_conversion_plugin(edited_resource);
  288. ERR_FAIL_INDEX(to_type, conversions.size());
  289. edited_resource = conversions[to_type]->convert(edited_resource);
  290. emit_signal(SNAME("resource_changed"), edited_resource);
  291. _update_resource();
  292. break;
  293. }
  294. ERR_FAIL_COND(inheritors_array.is_empty());
  295. String intype = inheritors_array[p_which - TYPE_BASE_ID];
  296. Variant obj;
  297. if (ScriptServer::is_global_class(intype)) {
  298. obj = ClassDB::instantiate(ScriptServer::get_global_class_native_base(intype));
  299. if (obj) {
  300. Ref<Script> script = ResourceLoader::load(ScriptServer::get_global_class_path(intype));
  301. if (script.is_valid()) {
  302. ((Object *)obj)->set_script(script);
  303. }
  304. }
  305. } else {
  306. obj = ClassDB::instantiate(intype);
  307. }
  308. if (!obj) {
  309. obj = EditorNode::get_editor_data().instance_custom_type(intype, "Resource");
  310. }
  311. Resource *resp = Object::cast_to<Resource>(obj);
  312. ERR_BREAK(!resp);
  313. edited_resource = RES(resp);
  314. emit_signal(SNAME("resource_changed"), edited_resource);
  315. _update_resource();
  316. } break;
  317. }
  318. }
  319. void EditorResourcePicker::set_create_options(Object *p_menu_node) {
  320. _ensure_resource_menu();
  321. // If a subclass implements this method, use it to replace all create items.
  322. if (get_script_instance() && get_script_instance()->has_method("_set_create_options")) {
  323. get_script_instance()->call("_set_create_options", p_menu_node);
  324. return;
  325. }
  326. // By default provide generic "New ..." options.
  327. if (base_type != "") {
  328. int idx = 0;
  329. Set<String> allowed_types;
  330. _get_allowed_types(false, &allowed_types);
  331. Vector<EditorData::CustomType> custom_resources;
  332. if (EditorNode::get_editor_data().get_custom_types().has("Resource")) {
  333. custom_resources = EditorNode::get_editor_data().get_custom_types()["Resource"];
  334. }
  335. for (Set<String>::Element *E = allowed_types.front(); E; E = E->next()) {
  336. const String &t = E->get();
  337. bool is_custom_resource = false;
  338. Ref<Texture2D> icon;
  339. if (!custom_resources.is_empty()) {
  340. for (int j = 0; j < custom_resources.size(); j++) {
  341. if (custom_resources[j].name == t) {
  342. is_custom_resource = true;
  343. if (custom_resources[j].icon.is_valid()) {
  344. icon = custom_resources[j].icon;
  345. }
  346. break;
  347. }
  348. }
  349. }
  350. if (!is_custom_resource && !(ScriptServer::is_global_class(t) || ClassDB::can_instantiate(t))) {
  351. continue;
  352. }
  353. inheritors_array.push_back(t);
  354. if (!icon.is_valid()) {
  355. icon = get_theme_icon(has_theme_icon(t, SNAME("EditorIcons")) ? t : String("Object"), SNAME("EditorIcons"));
  356. }
  357. int id = TYPE_BASE_ID + idx;
  358. edit_menu->add_icon_item(icon, vformat(TTR("New %s"), t), id);
  359. idx++;
  360. }
  361. if (edit_menu->get_item_count()) {
  362. edit_menu->add_separator();
  363. }
  364. }
  365. }
  366. bool EditorResourcePicker::handle_menu_selected(int p_which) {
  367. if (get_script_instance() && get_script_instance()->has_method("_handle_menu_selected")) {
  368. return get_script_instance()->call("_handle_menu_selected", p_which);
  369. }
  370. return false;
  371. }
  372. void EditorResourcePicker::_button_draw() {
  373. if (dropping) {
  374. Color color = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  375. assign_button->draw_rect(Rect2(Point2(), assign_button->get_size()), color, false);
  376. }
  377. }
  378. void EditorResourcePicker::_button_input(const Ref<InputEvent> &p_event) {
  379. if (!editable) {
  380. return;
  381. }
  382. Ref<InputEventMouseButton> mb = p_event;
  383. if (mb.is_valid()) {
  384. if (mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_RIGHT) {
  385. _update_menu_items();
  386. Vector2 pos = get_screen_position() + mb->get_position();
  387. edit_menu->set_as_minsize();
  388. edit_menu->set_position(pos);
  389. edit_menu->popup();
  390. }
  391. }
  392. }
  393. void EditorResourcePicker::_get_allowed_types(bool p_with_convert, Set<String> *p_vector) const {
  394. Vector<String> allowed_types = base_type.split(",");
  395. int size = allowed_types.size();
  396. List<StringName> global_classes;
  397. ScriptServer::get_global_class_list(&global_classes);
  398. for (int i = 0; i < size; i++) {
  399. String base = allowed_types[i].strip_edges();
  400. p_vector->insert(base);
  401. // If we hit a familiar base type, take all the data from cache.
  402. if (allowed_types_cache.has(base)) {
  403. List<StringName> allowed_subtypes = allowed_types_cache[base];
  404. for (const StringName &subtype_name : allowed_subtypes) {
  405. p_vector->insert(subtype_name);
  406. }
  407. } else {
  408. List<StringName> allowed_subtypes;
  409. List<StringName> inheriters;
  410. ClassDB::get_inheriters_from_class(base, &inheriters);
  411. for (const StringName &subtype_name : inheriters) {
  412. p_vector->insert(subtype_name);
  413. allowed_subtypes.push_back(subtype_name);
  414. }
  415. for (const StringName &subtype_name : global_classes) {
  416. if (EditorNode::get_editor_data().script_class_is_parent(subtype_name, base)) {
  417. p_vector->insert(subtype_name);
  418. allowed_subtypes.push_back(subtype_name);
  419. }
  420. }
  421. // Store the subtypes of the base type in the cache for future use.
  422. allowed_types_cache[base] = allowed_subtypes;
  423. }
  424. if (p_with_convert) {
  425. if (base == "StandardMaterial3D") {
  426. p_vector->insert("Texture2D");
  427. } else if (base == "ShaderMaterial") {
  428. p_vector->insert("Shader");
  429. } else if (base == "Font") {
  430. p_vector->insert("FontData");
  431. }
  432. }
  433. }
  434. if (EditorNode::get_editor_data().get_custom_types().has("Resource")) {
  435. Vector<EditorData::CustomType> custom_resources = EditorNode::get_editor_data().get_custom_types()["Resource"];
  436. for (int i = 0; i < custom_resources.size(); i++) {
  437. p_vector->insert(custom_resources[i].name);
  438. }
  439. }
  440. }
  441. bool EditorResourcePicker::_is_drop_valid(const Dictionary &p_drag_data) const {
  442. if (base_type.is_empty()) {
  443. return true;
  444. }
  445. Dictionary drag_data = p_drag_data;
  446. Ref<Resource> res;
  447. if (drag_data.has("type") && String(drag_data["type"]) == "script_list_element") {
  448. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(drag_data["script_list_element"]);
  449. if (se) {
  450. res = se->get_edited_resource();
  451. }
  452. } else if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  453. res = drag_data["resource"];
  454. }
  455. Set<String> allowed_types;
  456. _get_allowed_types(true, &allowed_types);
  457. if (res.is_valid() && _is_type_valid(res->get_class(), allowed_types)) {
  458. return true;
  459. }
  460. if (res.is_valid() && res->get_script()) {
  461. StringName custom_class = EditorNode::get_singleton()->get_object_custom_type_name(res->get_script());
  462. if (_is_type_valid(custom_class, allowed_types)) {
  463. return true;
  464. }
  465. }
  466. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  467. Vector<String> files = drag_data["files"];
  468. if (files.size() == 1) {
  469. String file = files[0];
  470. String file_type = EditorFileSystem::get_singleton()->get_file_type(file);
  471. if (file_type != "" && _is_type_valid(file_type, allowed_types)) {
  472. return true;
  473. }
  474. }
  475. }
  476. return false;
  477. }
  478. bool EditorResourcePicker::_is_type_valid(const String p_type_name, Set<String> p_allowed_types) const {
  479. for (Set<String>::Element *E = p_allowed_types.front(); E; E = E->next()) {
  480. String at = E->get().strip_edges();
  481. 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)) {
  482. return true;
  483. }
  484. }
  485. return false;
  486. }
  487. Variant EditorResourcePicker::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  488. if (edited_resource.is_valid()) {
  489. return EditorNode::get_singleton()->drag_resource(edited_resource, p_from);
  490. }
  491. return Variant();
  492. }
  493. bool EditorResourcePicker::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  494. return editable && _is_drop_valid(p_data);
  495. }
  496. void EditorResourcePicker::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  497. ERR_FAIL_COND(!_is_drop_valid(p_data));
  498. Dictionary drag_data = p_data;
  499. Ref<Resource> dropped_resource;
  500. if (drag_data.has("type") && String(drag_data["type"]) == "script_list_element") {
  501. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(drag_data["script_list_element"]);
  502. if (se) {
  503. dropped_resource = se->get_edited_resource();
  504. }
  505. } else if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  506. dropped_resource = drag_data["resource"];
  507. }
  508. if (!dropped_resource.is_valid() && drag_data.has("type") && String(drag_data["type"]) == "files") {
  509. Vector<String> files = drag_data["files"];
  510. if (files.size() == 1) {
  511. String file = files[0];
  512. dropped_resource = ResourceLoader::load(file);
  513. }
  514. }
  515. if (dropped_resource.is_valid()) {
  516. Set<String> allowed_types;
  517. _get_allowed_types(false, &allowed_types);
  518. // If the accepted dropped resource is from the extended list, it requires conversion.
  519. if (!_is_type_valid(dropped_resource->get_class(), allowed_types)) {
  520. for (Set<String>::Element *E = allowed_types.front(); E; E = E->next()) {
  521. String at = E->get().strip_edges();
  522. if (at == "StandardMaterial3D" && ClassDB::is_parent_class(dropped_resource->get_class(), "Texture2D")) {
  523. Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);
  524. mat->set_texture(StandardMaterial3D::TextureParam::TEXTURE_ALBEDO, dropped_resource);
  525. dropped_resource = mat;
  526. break;
  527. }
  528. if (at == "ShaderMaterial" && ClassDB::is_parent_class(dropped_resource->get_class(), "Shader")) {
  529. Ref<ShaderMaterial> mat = memnew(ShaderMaterial);
  530. mat->set_shader(dropped_resource);
  531. dropped_resource = mat;
  532. break;
  533. }
  534. if (at == "Font" && ClassDB::is_parent_class(dropped_resource->get_class(), "FontData")) {
  535. Ref<Font> font = memnew(Font);
  536. font->add_data(dropped_resource);
  537. dropped_resource = font;
  538. break;
  539. }
  540. }
  541. }
  542. edited_resource = dropped_resource;
  543. emit_signal(SNAME("resource_changed"), edited_resource);
  544. _update_resource();
  545. }
  546. }
  547. void EditorResourcePicker::_bind_methods() {
  548. ClassDB::bind_method(D_METHOD("_update_resource_preview"), &EditorResourcePicker::_update_resource_preview);
  549. ClassDB::bind_method(D_METHOD("_get_drag_data_fw", "position", "from"), &EditorResourcePicker::get_drag_data_fw);
  550. ClassDB::bind_method(D_METHOD("_can_drop_data_fw", "position", "data", "from"), &EditorResourcePicker::can_drop_data_fw);
  551. ClassDB::bind_method(D_METHOD("_drop_data_fw", "position", "data", "from"), &EditorResourcePicker::drop_data_fw);
  552. ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &EditorResourcePicker::set_base_type);
  553. ClassDB::bind_method(D_METHOD("get_base_type"), &EditorResourcePicker::get_base_type);
  554. ClassDB::bind_method(D_METHOD("get_allowed_types"), &EditorResourcePicker::get_allowed_types);
  555. ClassDB::bind_method(D_METHOD("set_edited_resource", "resource"), &EditorResourcePicker::set_edited_resource);
  556. ClassDB::bind_method(D_METHOD("get_edited_resource"), &EditorResourcePicker::get_edited_resource);
  557. ClassDB::bind_method(D_METHOD("set_toggle_mode", "enable"), &EditorResourcePicker::set_toggle_mode);
  558. ClassDB::bind_method(D_METHOD("is_toggle_mode"), &EditorResourcePicker::is_toggle_mode);
  559. ClassDB::bind_method(D_METHOD("set_toggle_pressed", "pressed"), &EditorResourcePicker::set_toggle_pressed);
  560. ClassDB::bind_method(D_METHOD("set_editable", "enable"), &EditorResourcePicker::set_editable);
  561. ClassDB::bind_method(D_METHOD("is_editable"), &EditorResourcePicker::is_editable);
  562. ClassDB::add_virtual_method(get_class_static(), MethodInfo("_set_create_options", PropertyInfo(Variant::OBJECT, "menu_node")));
  563. ClassDB::add_virtual_method(get_class_static(), MethodInfo("_handle_menu_selected", PropertyInfo(Variant::INT, "id")));
  564. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type"), "set_base_type", "get_base_type");
  565. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "edited_resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource", PROPERTY_USAGE_NONE), "set_edited_resource", "get_edited_resource");
  566. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  567. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode");
  568. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), PropertyInfo(Variant::BOOL, "edit")));
  569. ADD_SIGNAL(MethodInfo("resource_changed", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
  570. }
  571. void EditorResourcePicker::_notification(int p_what) {
  572. switch (p_what) {
  573. case NOTIFICATION_ENTER_TREE: {
  574. _update_resource();
  575. [[fallthrough]];
  576. }
  577. case NOTIFICATION_THEME_CHANGED: {
  578. edit_button->set_icon(get_theme_icon(SNAME("select_arrow"), SNAME("Tree")));
  579. } break;
  580. case NOTIFICATION_DRAW: {
  581. draw_style_box(get_theme_stylebox(SNAME("bg"), SNAME("Tree")), Rect2(Point2(), get_size()));
  582. } break;
  583. case NOTIFICATION_DRAG_BEGIN: {
  584. if (editable && _is_drop_valid(get_viewport()->gui_get_drag_data())) {
  585. dropping = true;
  586. assign_button->update();
  587. }
  588. } break;
  589. case NOTIFICATION_DRAG_END: {
  590. if (dropping) {
  591. dropping = false;
  592. assign_button->update();
  593. }
  594. } break;
  595. }
  596. }
  597. void EditorResourcePicker::set_base_type(const String &p_base_type) {
  598. base_type = p_base_type;
  599. // There is a possibility that the new base type is conflicting with the existing value.
  600. // Keep the value, but warn the user that there is a potential mistake.
  601. if (!base_type.is_empty() && edited_resource.is_valid()) {
  602. Set<String> allowed_types;
  603. _get_allowed_types(true, &allowed_types);
  604. StringName custom_class;
  605. bool is_custom = false;
  606. if (edited_resource->get_script()) {
  607. custom_class = EditorNode::get_singleton()->get_object_custom_type_name(edited_resource->get_script());
  608. is_custom = _is_type_valid(custom_class, allowed_types);
  609. }
  610. if (!is_custom && !_is_type_valid(edited_resource->get_class(), allowed_types)) {
  611. String class_str = (custom_class == StringName() ? edited_resource->get_class() : vformat("%s (%s)", custom_class, edited_resource->get_class()));
  612. 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));
  613. }
  614. } else {
  615. // Call the method to build the cache immediately.
  616. Set<String> allowed_types;
  617. _get_allowed_types(false, &allowed_types);
  618. }
  619. }
  620. String EditorResourcePicker::get_base_type() const {
  621. return base_type;
  622. }
  623. Vector<String> EditorResourcePicker::get_allowed_types() const {
  624. Set<String> allowed_types;
  625. _get_allowed_types(false, &allowed_types);
  626. Vector<String> types;
  627. types.resize(allowed_types.size());
  628. int i = 0;
  629. String *w = types.ptrw();
  630. for (Set<String>::Element *E = allowed_types.front(); E; E = E->next(), i++) {
  631. w[i] = E->get();
  632. }
  633. return types;
  634. }
  635. void EditorResourcePicker::set_edited_resource(RES p_resource) {
  636. if (!p_resource.is_valid()) {
  637. edited_resource = RES();
  638. _update_resource();
  639. return;
  640. }
  641. if (!base_type.is_empty()) {
  642. Set<String> allowed_types;
  643. _get_allowed_types(true, &allowed_types);
  644. StringName custom_class;
  645. bool is_custom = false;
  646. if (p_resource->get_script()) {
  647. custom_class = EditorNode::get_singleton()->get_object_custom_type_name(p_resource->get_script());
  648. is_custom = _is_type_valid(custom_class, allowed_types);
  649. }
  650. if (!is_custom && !_is_type_valid(p_resource->get_class(), allowed_types)) {
  651. String class_str = (custom_class == StringName() ? p_resource->get_class() : vformat("%s (%s)", custom_class, p_resource->get_class()));
  652. 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));
  653. }
  654. }
  655. edited_resource = p_resource;
  656. _update_resource();
  657. }
  658. RES EditorResourcePicker::get_edited_resource() {
  659. return edited_resource;
  660. }
  661. void EditorResourcePicker::set_toggle_mode(bool p_enable) {
  662. assign_button->set_toggle_mode(p_enable);
  663. }
  664. bool EditorResourcePicker::is_toggle_mode() const {
  665. return assign_button->is_toggle_mode();
  666. }
  667. void EditorResourcePicker::set_toggle_pressed(bool p_pressed) {
  668. if (!is_toggle_mode()) {
  669. return;
  670. }
  671. assign_button->set_pressed(p_pressed);
  672. }
  673. void EditorResourcePicker::set_editable(bool p_editable) {
  674. editable = p_editable;
  675. assign_button->set_disabled(!editable);
  676. edit_button->set_visible(editable);
  677. }
  678. bool EditorResourcePicker::is_editable() const {
  679. return editable;
  680. }
  681. void EditorResourcePicker::_ensure_resource_menu() {
  682. if (edit_menu) {
  683. return;
  684. }
  685. edit_menu = memnew(PopupMenu);
  686. add_child(edit_menu);
  687. edit_menu->connect("id_pressed", callable_mp(this, &EditorResourcePicker::_edit_menu_cbk));
  688. edit_menu->connect("popup_hide", callable_mp((BaseButton *)edit_button, &BaseButton::set_pressed), varray(false));
  689. }
  690. EditorResourcePicker::EditorResourcePicker() {
  691. assign_button = memnew(Button);
  692. assign_button->set_flat(true);
  693. assign_button->set_h_size_flags(SIZE_EXPAND_FILL);
  694. assign_button->set_clip_text(true);
  695. assign_button->set_drag_forwarding(this);
  696. add_child(assign_button);
  697. assign_button->connect("pressed", callable_mp(this, &EditorResourcePicker::_resource_selected));
  698. assign_button->connect("draw", callable_mp(this, &EditorResourcePicker::_button_draw));
  699. assign_button->connect("gui_input", callable_mp(this, &EditorResourcePicker::_button_input));
  700. preview_rect = memnew(TextureRect);
  701. preview_rect->set_expand(true);
  702. preview_rect->set_anchors_and_offsets_preset(PRESET_WIDE);
  703. preview_rect->set_offset(SIDE_TOP, 1);
  704. preview_rect->set_offset(SIDE_BOTTOM, -1);
  705. preview_rect->set_offset(SIDE_RIGHT, -1);
  706. assign_button->add_child(preview_rect);
  707. edit_button = memnew(Button);
  708. edit_button->set_flat(true);
  709. edit_button->set_toggle_mode(true);
  710. edit_button->connect("pressed", callable_mp(this, &EditorResourcePicker::_update_menu));
  711. add_child(edit_button);
  712. edit_button->connect("gui_input", callable_mp(this, &EditorResourcePicker::_button_input));
  713. }
  714. // EditorScriptPicker
  715. void EditorScriptPicker::set_create_options(Object *p_menu_node) {
  716. PopupMenu *menu_node = Object::cast_to<PopupMenu>(p_menu_node);
  717. if (!menu_node) {
  718. return;
  719. }
  720. menu_node->add_icon_item(get_theme_icon(SNAME("ScriptCreate"), SNAME("EditorIcons")), TTR("New Script"), OBJ_MENU_NEW_SCRIPT);
  721. menu_node->add_icon_item(get_theme_icon(SNAME("ScriptExtend"), SNAME("EditorIcons")), TTR("Extend Script"), OBJ_MENU_EXTEND_SCRIPT);
  722. menu_node->add_separator();
  723. }
  724. bool EditorScriptPicker::handle_menu_selected(int p_which) {
  725. switch (p_which) {
  726. case OBJ_MENU_NEW_SCRIPT: {
  727. if (script_owner) {
  728. EditorNode::get_singleton()->get_scene_tree_dock()->open_script_dialog(script_owner, false);
  729. }
  730. return true;
  731. }
  732. case OBJ_MENU_EXTEND_SCRIPT: {
  733. if (script_owner) {
  734. EditorNode::get_singleton()->get_scene_tree_dock()->open_script_dialog(script_owner, true);
  735. }
  736. return true;
  737. }
  738. }
  739. return false;
  740. }
  741. void EditorScriptPicker::set_script_owner(Node *p_owner) {
  742. script_owner = p_owner;
  743. }
  744. Node *EditorScriptPicker::get_script_owner() const {
  745. return script_owner;
  746. }
  747. void EditorScriptPicker::_bind_methods() {
  748. ClassDB::bind_method(D_METHOD("set_script_owner", "owner_node"), &EditorScriptPicker::set_script_owner);
  749. ClassDB::bind_method(D_METHOD("get_script_owner"), &EditorScriptPicker::get_script_owner);
  750. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "script_owner", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_script_owner", "get_script_owner");
  751. }
  752. EditorScriptPicker::EditorScriptPicker() {
  753. }
  754. // EditorShaderPicker
  755. void EditorShaderPicker::set_create_options(Object *p_menu_node) {
  756. PopupMenu *menu_node = Object::cast_to<PopupMenu>(p_menu_node);
  757. if (!menu_node) {
  758. return;
  759. }
  760. menu_node->add_icon_item(get_theme_icon(SNAME("Shader"), SNAME("EditorIcons")), TTR("New Shader"), OBJ_MENU_NEW_SHADER);
  761. menu_node->add_separator();
  762. }
  763. bool EditorShaderPicker::handle_menu_selected(int p_which) {
  764. Ref<ShaderMaterial> material = Ref<ShaderMaterial>(get_edited_material());
  765. switch (p_which) {
  766. case OBJ_MENU_NEW_SHADER: {
  767. if (material.is_valid()) {
  768. EditorNode::get_singleton()->get_scene_tree_dock()->open_shader_dialog(material, preferred_mode);
  769. return true;
  770. }
  771. } break;
  772. default:
  773. break;
  774. }
  775. return false;
  776. }
  777. void EditorShaderPicker::set_edited_material(ShaderMaterial *p_material) {
  778. edited_material = p_material;
  779. }
  780. ShaderMaterial *EditorShaderPicker::get_edited_material() const {
  781. return edited_material;
  782. }
  783. void EditorShaderPicker::set_preferred_mode(int p_mode) {
  784. preferred_mode = p_mode;
  785. }
  786. EditorShaderPicker::EditorShaderPicker() {
  787. }