editor_resource_picker.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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/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. #include "scene/main/viewport.h"
  37. #include "scene/resources/dynamic_font.h"
  38. HashMap<StringName, List<StringName>> EditorResourcePicker::allowed_types_cache;
  39. void EditorResourcePicker::clear_caches() {
  40. allowed_types_cache.clear();
  41. }
  42. void EditorResourcePicker::_update_resource() {
  43. preview_rect->set_texture(Ref<Texture>());
  44. assign_button->set_custom_minimum_size(Size2(1, 1));
  45. if (edited_resource == RES()) {
  46. assign_button->set_icon(Ref<Texture>());
  47. assign_button->set_text(TTR("[empty]"));
  48. } else {
  49. assign_button->set_icon(EditorNode::get_singleton()->get_object_icon(edited_resource.operator->(), "Object"));
  50. if (edited_resource->get_name() != String()) {
  51. assign_button->set_text(edited_resource->get_name());
  52. } else if (edited_resource->get_path().is_resource_file()) {
  53. assign_button->set_text(edited_resource->get_path().get_file());
  54. assign_button->set_tooltip(edited_resource->get_path());
  55. } else {
  56. assign_button->set_text(edited_resource->get_class());
  57. }
  58. if (edited_resource->get_path().is_resource_file()) {
  59. assign_button->set_tooltip(edited_resource->get_path());
  60. }
  61. // Preview will override the above, so called at the end.
  62. EditorResourcePreview::get_singleton()->queue_edited_resource_preview(edited_resource, this, "_update_resource_preview", edited_resource->get_instance_id());
  63. }
  64. }
  65. void EditorResourcePicker::_update_resource_preview(const String &p_path, const Ref<Texture> &p_preview, const Ref<Texture> &p_small_preview, ObjectID p_obj) {
  66. if (!edited_resource.is_valid() || edited_resource->get_instance_id() != p_obj) {
  67. return;
  68. }
  69. String type = edited_resource->get_class_name();
  70. if (ClassDB::class_exists(type) && ClassDB::is_parent_class(type, "Script")) {
  71. assign_button->set_text(edited_resource->get_path().get_file());
  72. return;
  73. }
  74. if (p_preview.is_valid()) {
  75. preview_rect->set_margin(MARGIN_LEFT, assign_button->get_icon()->get_width() + assign_button->get_stylebox("normal")->get_default_margin(MARGIN_LEFT) + get_constant("hseparation", "Button"));
  76. if (type == "GradientTexture") {
  77. preview_rect->set_stretch_mode(TextureRect::STRETCH_SCALE);
  78. assign_button->set_custom_minimum_size(Size2(1, 1));
  79. } else {
  80. preview_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  81. int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size");
  82. thumbnail_size *= EDSCALE;
  83. assign_button->set_custom_minimum_size(Size2(1, thumbnail_size));
  84. }
  85. preview_rect->set_texture(p_preview);
  86. assign_button->set_text("");
  87. }
  88. }
  89. void EditorResourcePicker::_resource_selected() {
  90. if (edited_resource.is_null()) {
  91. edit_button->set_pressed(true);
  92. _update_menu();
  93. return;
  94. }
  95. emit_signal("resource_selected", edited_resource, false);
  96. }
  97. void EditorResourcePicker::_file_selected(const String &p_path) {
  98. RES loaded_resource = ResourceLoader::load(p_path);
  99. ERR_FAIL_COND_MSG(loaded_resource.is_null(), "Cannot load resource from path '" + p_path + "'.");
  100. if (base_type != "") {
  101. bool any_type_matches = false;
  102. for (int i = 0; i < base_type.get_slice_count(","); i++) {
  103. String base = base_type.get_slice(",", i);
  104. if (loaded_resource->is_class(base)) {
  105. any_type_matches = true;
  106. break;
  107. }
  108. }
  109. if (!any_type_matches) {
  110. 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));
  111. return;
  112. }
  113. }
  114. edited_resource = loaded_resource;
  115. emit_signal("resource_changed", edited_resource);
  116. _update_resource();
  117. }
  118. void EditorResourcePicker::_file_quick_selected() {
  119. _file_selected(quick_open->get_selected());
  120. }
  121. void EditorResourcePicker::_update_menu() {
  122. _update_menu_items();
  123. Rect2 gt = edit_button->get_global_rect();
  124. edit_menu->set_as_minsize();
  125. int ms = edit_menu->get_combined_minimum_size().width;
  126. Vector2 popup_pos = gt.position + gt.size - Vector2(ms, 0);
  127. edit_menu->set_global_position(popup_pos);
  128. edit_menu->popup();
  129. }
  130. void EditorResourcePicker::_update_menu_items() {
  131. edit_menu->clear();
  132. // Add options for creating specific subtypes of the base resource type.
  133. set_create_options(edit_menu);
  134. // Add an option to load a resource from a file using the QuickOpen dialog.
  135. edit_menu->add_icon_item(get_icon("Load", "EditorIcons"), TTR("Quick Load"), OBJ_MENU_QUICKLOAD);
  136. // Add an option to load a resource from a file using the regular file dialog.
  137. edit_menu->add_icon_item(get_icon("Load", "EditorIcons"), TTR("Load"), OBJ_MENU_LOAD);
  138. // Add options for changing existing value of the resource.
  139. if (edited_resource.is_valid()) {
  140. edit_menu->add_icon_item(get_icon("Edit", "EditorIcons"), TTR("Edit"), OBJ_MENU_EDIT);
  141. edit_menu->add_icon_item(get_icon("Clear", "EditorIcons"), TTR("Clear"), OBJ_MENU_CLEAR);
  142. edit_menu->add_icon_item(get_icon("Duplicate", "EditorIcons"), TTR("Make Unique"), OBJ_MENU_MAKE_UNIQUE);
  143. edit_menu->add_icon_item(get_icon("Save", "EditorIcons"), TTR("Save"), OBJ_MENU_SAVE);
  144. if (edited_resource->get_path().is_resource_file()) {
  145. edit_menu->add_separator();
  146. edit_menu->add_item(TTR("Show in FileSystem"), OBJ_MENU_SHOW_IN_FILE_SYSTEM);
  147. }
  148. }
  149. // Add options to copy/paste resource.
  150. RES cb = EditorSettings::get_singleton()->get_resource_clipboard();
  151. bool paste_valid = false;
  152. if (cb.is_valid()) {
  153. if (base_type == "") {
  154. paste_valid = true;
  155. } else {
  156. for (int i = 0; i < base_type.get_slice_count(","); i++) {
  157. if (ClassDB::class_exists(cb->get_class()) && ClassDB::is_parent_class(cb->get_class(), base_type.get_slice(",", i))) {
  158. paste_valid = true;
  159. break;
  160. }
  161. }
  162. }
  163. }
  164. if (edited_resource.is_valid() || paste_valid) {
  165. edit_menu->add_separator();
  166. if (edited_resource.is_valid()) {
  167. edit_menu->add_item(TTR("Copy"), OBJ_MENU_COPY);
  168. }
  169. if (paste_valid) {
  170. edit_menu->add_item(TTR("Paste"), OBJ_MENU_PASTE);
  171. }
  172. }
  173. // Add options to convert existing resource to another type of resource.
  174. if (edited_resource.is_valid()) {
  175. Vector<Ref<EditorResourceConversionPlugin>> conversions = EditorNode::get_singleton()->find_resource_conversion_plugin(edited_resource);
  176. if (conversions.size()) {
  177. edit_menu->add_separator();
  178. }
  179. for (int i = 0; i < conversions.size(); i++) {
  180. String what = conversions[i]->converts_to();
  181. Ref<Texture> icon;
  182. if (has_icon(what, "EditorIcons")) {
  183. icon = get_icon(what, "EditorIcons");
  184. } else {
  185. icon = get_icon(what, "Resource");
  186. }
  187. edit_menu->add_icon_item(icon, vformat(TTR("Convert to %s"), what), CONVERT_BASE_ID + i);
  188. }
  189. }
  190. }
  191. void EditorResourcePicker::_edit_menu_cbk(int p_which) {
  192. switch (p_which) {
  193. case OBJ_MENU_LOAD: {
  194. List<String> extensions;
  195. for (int i = 0; i < base_type.get_slice_count(","); i++) {
  196. String base = base_type.get_slice(",", i);
  197. ResourceLoader::get_recognized_extensions_for_type(base, &extensions);
  198. }
  199. Set<String> valid_extensions;
  200. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  201. valid_extensions.insert(E->get());
  202. }
  203. if (!file_dialog) {
  204. file_dialog = memnew(EditorFileDialog);
  205. file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  206. add_child(file_dialog);
  207. file_dialog->connect("file_selected", this, "_file_selected");
  208. }
  209. file_dialog->clear_filters();
  210. for (Set<String>::Element *E = valid_extensions.front(); E; E = E->next()) {
  211. file_dialog->add_filter("*." + E->get() + " ; " + E->get().to_upper());
  212. }
  213. file_dialog->popup_centered_ratio();
  214. } break;
  215. case OBJ_MENU_QUICKLOAD: {
  216. if (!quick_open) {
  217. quick_open = memnew(EditorQuickOpen);
  218. add_child(quick_open);
  219. quick_open->connect("quick_open", this, "_file_quick_selected");
  220. }
  221. quick_open->popup_dialog(base_type);
  222. quick_open->set_title(TTR("Resource"));
  223. } break;
  224. case OBJ_MENU_EDIT: {
  225. if (edited_resource.is_valid()) {
  226. emit_signal("resource_selected", edited_resource, true);
  227. }
  228. } break;
  229. case OBJ_MENU_CLEAR: {
  230. edited_resource = RES();
  231. emit_signal("resource_changed", edited_resource);
  232. _update_resource();
  233. } break;
  234. case OBJ_MENU_MAKE_UNIQUE: {
  235. if (edited_resource.is_null()) {
  236. return;
  237. }
  238. List<PropertyInfo> property_list;
  239. edited_resource->get_property_list(&property_list);
  240. List<Pair<String, Variant>> propvalues;
  241. for (List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) {
  242. Pair<String, Variant> p;
  243. PropertyInfo &pi = E->get();
  244. if (pi.usage & PROPERTY_USAGE_STORAGE) {
  245. p.first = pi.name;
  246. p.second = edited_resource->get(pi.name);
  247. }
  248. propvalues.push_back(p);
  249. }
  250. String orig_type = edited_resource->get_class();
  251. Object *inst = ClassDB::instance(orig_type);
  252. Ref<Resource> unique_resource = Ref<Resource>(Object::cast_to<Resource>(inst));
  253. ERR_FAIL_COND(unique_resource.is_null());
  254. for (List<Pair<String, Variant>>::Element *E = propvalues.front(); E; E = E->next()) {
  255. Pair<String, Variant> &p = E->get();
  256. unique_resource->set(p.first, p.second);
  257. }
  258. edited_resource = unique_resource;
  259. emit_signal("resource_changed", edited_resource);
  260. _update_resource();
  261. } break;
  262. case OBJ_MENU_SAVE: {
  263. if (edited_resource.is_null()) {
  264. return;
  265. }
  266. EditorNode::get_singleton()->save_resource(edited_resource);
  267. } break;
  268. case OBJ_MENU_COPY: {
  269. EditorSettings::get_singleton()->set_resource_clipboard(edited_resource);
  270. } break;
  271. case OBJ_MENU_PASTE: {
  272. edited_resource = EditorSettings::get_singleton()->get_resource_clipboard();
  273. emit_signal("resource_changed", edited_resource);
  274. _update_resource();
  275. } break;
  276. case OBJ_MENU_SHOW_IN_FILE_SYSTEM: {
  277. FileSystemDock *file_system_dock = EditorNode::get_singleton()->get_filesystem_dock();
  278. file_system_dock->navigate_to_path(edited_resource->get_path());
  279. // Ensure that the FileSystem dock is visible.
  280. TabContainer *tab_container = (TabContainer *)file_system_dock->get_parent_control();
  281. tab_container->set_current_tab(file_system_dock->get_index());
  282. } break;
  283. default: {
  284. // Allow subclasses to handle their own options first, only then fallback on the default branch logic.
  285. if (handle_menu_selected(p_which)) {
  286. break;
  287. }
  288. if (p_which >= CONVERT_BASE_ID) {
  289. int to_type = p_which - CONVERT_BASE_ID;
  290. Vector<Ref<EditorResourceConversionPlugin>> conversions = EditorNode::get_singleton()->find_resource_conversion_plugin(edited_resource);
  291. ERR_FAIL_INDEX(to_type, conversions.size());
  292. edited_resource = conversions[to_type]->convert(edited_resource);
  293. emit_signal("resource_changed", edited_resource);
  294. _update_resource();
  295. break;
  296. }
  297. ERR_FAIL_COND(inheritors_array.empty());
  298. String intype = inheritors_array[p_which - TYPE_BASE_ID];
  299. Variant obj;
  300. if (ScriptServer::is_global_class(intype)) {
  301. obj = ClassDB::instance(ScriptServer::get_global_class_native_base(intype));
  302. if (obj) {
  303. Ref<Script> script = ResourceLoader::load(ScriptServer::get_global_class_path(intype));
  304. if (script.is_valid()) {
  305. ((Object *)obj)->set_script(script.get_ref_ptr());
  306. }
  307. }
  308. } else {
  309. obj = ClassDB::instance(intype);
  310. }
  311. if (!obj) {
  312. obj = EditorNode::get_editor_data().instance_custom_type(intype, "Resource");
  313. }
  314. Resource *resp = Object::cast_to<Resource>(obj);
  315. ERR_BREAK(!resp);
  316. edited_resource = RES(resp);
  317. emit_signal("resource_changed", edited_resource);
  318. _update_resource();
  319. } break;
  320. }
  321. }
  322. void EditorResourcePicker::set_create_options(Object *p_menu_node) {
  323. // If a subclass implements this method, use it to replace all create items.
  324. if (get_script_instance() && get_script_instance()->has_method("set_create_options")) {
  325. get_script_instance()->call("set_create_options", p_menu_node);
  326. return;
  327. }
  328. // By default provide generic "New ..." options.
  329. if (base_type != "") {
  330. int idx = 0;
  331. Set<String> allowed_types;
  332. _get_allowed_types(false, &allowed_types);
  333. Vector<EditorData::CustomType> custom_resources;
  334. if (EditorNode::get_editor_data().get_custom_types().has("Resource")) {
  335. custom_resources = EditorNode::get_editor_data().get_custom_types()["Resource"];
  336. }
  337. for (Set<String>::Element *E = allowed_types.front(); E; E = E->next()) {
  338. const String &t = E->get();
  339. bool is_custom_resource = false;
  340. Ref<Texture> icon;
  341. if (!custom_resources.empty()) {
  342. for (int j = 0; j < custom_resources.size(); j++) {
  343. if (custom_resources[j].name == t) {
  344. is_custom_resource = true;
  345. if (custom_resources[j].icon.is_valid()) {
  346. icon = custom_resources[j].icon;
  347. }
  348. break;
  349. }
  350. }
  351. }
  352. if (!is_custom_resource && !(ScriptServer::is_global_class(t) || ClassDB::can_instance(t))) {
  353. continue;
  354. }
  355. inheritors_array.push_back(t);
  356. if (!icon.is_valid()) {
  357. icon = get_icon(has_icon(t, "EditorIcons") ? t : "Object", "EditorIcons");
  358. }
  359. int id = TYPE_BASE_ID + idx;
  360. edit_menu->add_icon_item(icon, vformat(TTR("New %s"), t), id);
  361. idx++;
  362. }
  363. if (edit_menu->get_item_count()) {
  364. edit_menu->add_separator();
  365. }
  366. }
  367. }
  368. bool EditorResourcePicker::handle_menu_selected(int p_which) {
  369. if (get_script_instance() && get_script_instance()->has_method("handle_menu_selected")) {
  370. return get_script_instance()->call("handle_menu_selected", p_which);
  371. }
  372. return false;
  373. }
  374. void EditorResourcePicker::_button_draw() {
  375. if (dropping) {
  376. Color color = get_color("accent_color", "Editor");
  377. assign_button->draw_rect(Rect2(Point2(), assign_button->get_size()), color, false);
  378. }
  379. }
  380. void EditorResourcePicker::_button_input(const Ref<InputEvent> &p_event) {
  381. if (!editable) {
  382. return;
  383. }
  384. Ref<InputEventMouseButton> mb = p_event;
  385. if (mb.is_valid()) {
  386. if (mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  387. _update_menu_items();
  388. Vector2 pos = get_global_position() + mb->get_position();
  389. edit_menu->set_as_minsize();
  390. edit_menu->set_global_position(pos);
  391. edit_menu->popup();
  392. }
  393. }
  394. }
  395. void EditorResourcePicker::_get_allowed_types(bool p_with_convert, Set<String> *p_vector) const {
  396. Vector<String> allowed_types = base_type.split(",");
  397. int size = allowed_types.size();
  398. List<StringName> global_classes;
  399. ScriptServer::get_global_class_list(&global_classes);
  400. for (int i = 0; i < size; i++) {
  401. String base = allowed_types[i].strip_edges();
  402. p_vector->insert(base);
  403. // If we hit a familiar base type, take all the data from cache.
  404. if (allowed_types_cache.has(base)) {
  405. List<StringName> allowed_subtypes = allowed_types_cache[base];
  406. for (List<StringName>::Element *E = allowed_subtypes.front(); E; E = E->next()) {
  407. p_vector->insert(E->get());
  408. }
  409. } else {
  410. List<StringName> allowed_subtypes;
  411. List<StringName> inheriters;
  412. ClassDB::get_inheriters_from_class(base, &inheriters);
  413. for (List<StringName>::Element *E = inheriters.front(); E; E = E->next()) {
  414. p_vector->insert(E->get());
  415. allowed_subtypes.push_back(E->get());
  416. }
  417. for (List<StringName>::Element *E = global_classes.front(); E; E = E->next()) {
  418. if (EditorNode::get_editor_data().script_class_is_parent(E->get(), base)) {
  419. p_vector->insert(E->get());
  420. allowed_subtypes.push_back(E->get());
  421. }
  422. }
  423. // Store the subtypes of the base type in the cache for future use.
  424. allowed_types_cache[base] = allowed_subtypes;
  425. }
  426. if (p_with_convert) {
  427. if (base == "SpatialMaterial") {
  428. p_vector->insert("Texture");
  429. } else if (base == "ShaderMaterial") {
  430. p_vector->insert("Shader");
  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.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().is_null()) {
  461. Ref<Script> res_script = res->get_script();
  462. StringName custom_class = EditorNode::get_singleton()->get_object_custom_type_name(res_script.ptr());
  463. if (_is_type_valid(custom_class, allowed_types)) {
  464. return true;
  465. }
  466. }
  467. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  468. Vector<String> files = drag_data["files"];
  469. if (files.size() == 1) {
  470. String file = files[0];
  471. String file_type = EditorFileSystem::get_singleton()->get_file_type(file);
  472. if (file_type != "" && _is_type_valid(file_type, allowed_types)) {
  473. return true;
  474. }
  475. }
  476. }
  477. return false;
  478. }
  479. bool EditorResourcePicker::_is_type_valid(const String p_type_name, Set<String> p_allowed_types) const {
  480. for (Set<String>::Element *E = p_allowed_types.front(); E; E = E->next()) {
  481. String at = E->get().strip_edges();
  482. if (p_type_name == at || (ClassDB::class_exists(p_type_name) && ClassDB::is_parent_class(p_type_name, at)) || EditorNode::get_editor_data().script_class_is_parent(p_type_name, at)) {
  483. return true;
  484. }
  485. }
  486. return false;
  487. }
  488. Variant EditorResourcePicker::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  489. if (edited_resource.is_valid()) {
  490. return EditorNode::get_singleton()->drag_resource(edited_resource, p_from);
  491. }
  492. return Variant();
  493. }
  494. bool EditorResourcePicker::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  495. return editable && _is_drop_valid(p_data);
  496. }
  497. void EditorResourcePicker::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  498. ERR_FAIL_COND(!_is_drop_valid(p_data));
  499. Dictionary drag_data = p_data;
  500. Ref<Resource> dropped_resource;
  501. if (drag_data.has("type") && String(drag_data["type"]) == "script_list_element") {
  502. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(drag_data["script_list_element"]);
  503. if (se) {
  504. dropped_resource = se->get_edited_resource();
  505. }
  506. } else if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
  507. dropped_resource = drag_data["resource"];
  508. }
  509. if (!dropped_resource.is_valid() && drag_data.has("type") && String(drag_data["type"]) == "files") {
  510. Vector<String> files = drag_data["files"];
  511. if (files.size() == 1) {
  512. String file = files[0];
  513. dropped_resource = ResourceLoader::load(file);
  514. }
  515. }
  516. if (dropped_resource.is_valid()) {
  517. Set<String> allowed_types;
  518. _get_allowed_types(false, &allowed_types);
  519. // If the accepted dropped resource is from the extended list, it requires conversion.
  520. if (!_is_type_valid(dropped_resource->get_class(), allowed_types)) {
  521. for (Set<String>::Element *E = allowed_types.front(); E; E = E->next()) {
  522. String at = E->get().strip_edges();
  523. if (at == "SpatialMaterial" && ClassDB::is_parent_class(dropped_resource->get_class(), "Texture")) {
  524. Ref<SpatialMaterial> mat = memnew(SpatialMaterial);
  525. mat->set_texture(SpatialMaterial::TextureParam::TEXTURE_ALBEDO, dropped_resource);
  526. dropped_resource = mat;
  527. break;
  528. }
  529. if (at == "ShaderMaterial" && ClassDB::is_parent_class(dropped_resource->get_class(), "Shader")) {
  530. Ref<ShaderMaterial> mat = memnew(ShaderMaterial);
  531. mat->set_shader(dropped_resource);
  532. dropped_resource = mat;
  533. break;
  534. }
  535. }
  536. }
  537. edited_resource = dropped_resource;
  538. emit_signal("resource_changed", edited_resource);
  539. _update_resource();
  540. }
  541. }
  542. void EditorResourcePicker::_bind_methods() {
  543. // Internal binds.
  544. ClassDB::bind_method(D_METHOD("_file_selected"), &EditorResourcePicker::_file_selected);
  545. ClassDB::bind_method(D_METHOD("_file_quick_selected"), &EditorResourcePicker::_file_quick_selected);
  546. ClassDB::bind_method(D_METHOD("_resource_selected"), &EditorResourcePicker::_resource_selected);
  547. ClassDB::bind_method(D_METHOD("_button_draw"), &EditorResourcePicker::_button_draw);
  548. ClassDB::bind_method(D_METHOD("_button_input"), &EditorResourcePicker::_button_input);
  549. ClassDB::bind_method(D_METHOD("_update_menu"), &EditorResourcePicker::_update_menu);
  550. ClassDB::bind_method(D_METHOD("_edit_menu_cbk"), &EditorResourcePicker::_edit_menu_cbk);
  551. // Public binds.
  552. ClassDB::bind_method(D_METHOD("_update_resource_preview"), &EditorResourcePicker::_update_resource_preview);
  553. ClassDB::bind_method(D_METHOD("get_drag_data_fw", "position", "from"), &EditorResourcePicker::get_drag_data_fw);
  554. ClassDB::bind_method(D_METHOD("can_drop_data_fw", "position", "data", "from"), &EditorResourcePicker::can_drop_data_fw);
  555. ClassDB::bind_method(D_METHOD("drop_data_fw", "position", "data", "from"), &EditorResourcePicker::drop_data_fw);
  556. ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &EditorResourcePicker::set_base_type);
  557. ClassDB::bind_method(D_METHOD("get_base_type"), &EditorResourcePicker::get_base_type);
  558. ClassDB::bind_method(D_METHOD("get_allowed_types"), &EditorResourcePicker::get_allowed_types);
  559. ClassDB::bind_method(D_METHOD("set_edited_resource", "resource"), &EditorResourcePicker::set_edited_resource);
  560. ClassDB::bind_method(D_METHOD("get_edited_resource"), &EditorResourcePicker::get_edited_resource);
  561. ClassDB::bind_method(D_METHOD("set_toggle_mode", "enable"), &EditorResourcePicker::set_toggle_mode);
  562. ClassDB::bind_method(D_METHOD("is_toggle_mode"), &EditorResourcePicker::is_toggle_mode);
  563. ClassDB::bind_method(D_METHOD("set_toggle_pressed", "pressed"), &EditorResourcePicker::set_toggle_pressed);
  564. ClassDB::bind_method(D_METHOD("set_editable", "enable"), &EditorResourcePicker::set_editable);
  565. ClassDB::bind_method(D_METHOD("is_editable"), &EditorResourcePicker::is_editable);
  566. ClassDB::add_virtual_method(get_class_static(), MethodInfo("set_create_options", PropertyInfo(Variant::OBJECT, "menu_node")));
  567. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "handle_menu_selected", PropertyInfo(Variant::INT, "id")));
  568. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type"), "set_base_type", "get_base_type");
  569. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "edited_resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource", 0), "set_edited_resource", "get_edited_resource");
  570. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  571. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode");
  572. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), PropertyInfo(Variant::BOOL, "edit")));
  573. ADD_SIGNAL(MethodInfo("resource_changed", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
  574. }
  575. void EditorResourcePicker::_notification(int p_what) {
  576. switch (p_what) {
  577. case NOTIFICATION_ENTER_TREE: {
  578. _update_resource();
  579. FALLTHROUGH;
  580. }
  581. case NOTIFICATION_THEME_CHANGED: {
  582. edit_button->set_icon(get_icon("select_arrow", "Tree"));
  583. } break;
  584. case NOTIFICATION_DRAW: {
  585. draw_style_box(get_stylebox("bg", "Tree"), Rect2(Point2(), get_size()));
  586. } break;
  587. case NOTIFICATION_DRAG_BEGIN: {
  588. if (editable && _is_drop_valid(get_viewport()->gui_get_drag_data())) {
  589. dropping = true;
  590. assign_button->update();
  591. }
  592. } break;
  593. case NOTIFICATION_DRAG_END: {
  594. if (dropping) {
  595. dropping = false;
  596. assign_button->update();
  597. }
  598. } break;
  599. }
  600. }
  601. void EditorResourcePicker::set_base_type(const String &p_base_type) {
  602. base_type = p_base_type;
  603. // There is a possibility that the new base type is conflicting with the existing value.
  604. // Keep the value, but warn the user that there is a potential mistake.
  605. if (!base_type.empty() && edited_resource.is_valid()) {
  606. Set<String> allowed_types;
  607. _get_allowed_types(true, &allowed_types);
  608. StringName custom_class;
  609. bool is_custom = false;
  610. if (!edited_resource->get_script().is_null()) {
  611. Ref<Script> res_script = edited_resource->get_script();
  612. custom_class = EditorNode::get_singleton()->get_object_custom_type_name(res_script.ptr());
  613. is_custom = _is_type_valid(custom_class, allowed_types);
  614. }
  615. if (!is_custom && !_is_type_valid(edited_resource->get_class(), allowed_types)) {
  616. String class_str = (custom_class == StringName() ? edited_resource->get_class() : vformat("%s (%s)", custom_class, edited_resource->get_class()));
  617. 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));
  618. }
  619. } else {
  620. // Call the method to build the cache immediately.
  621. Set<String> allowed_types;
  622. _get_allowed_types(false, &allowed_types);
  623. }
  624. }
  625. String EditorResourcePicker::get_base_type() const {
  626. return base_type;
  627. }
  628. Vector<String> EditorResourcePicker::get_allowed_types() const {
  629. Set<String> allowed_types;
  630. _get_allowed_types(false, &allowed_types);
  631. Vector<String> types;
  632. types.resize(allowed_types.size());
  633. int i = 0;
  634. String *w = types.ptrw();
  635. for (Set<String>::Element *E = allowed_types.front(); E; E = E->next(), i++) {
  636. w[i] = E->get();
  637. }
  638. return types;
  639. }
  640. void EditorResourcePicker::set_edited_resource(RES p_resource) {
  641. if (!p_resource.is_valid()) {
  642. edited_resource = RES();
  643. _update_resource();
  644. return;
  645. }
  646. if (!base_type.empty()) {
  647. Set<String> allowed_types;
  648. _get_allowed_types(true, &allowed_types);
  649. StringName custom_class;
  650. bool is_custom = false;
  651. if (!p_resource->get_script().is_null()) {
  652. Ref<Script> res_script = p_resource->get_script();
  653. custom_class = EditorNode::get_singleton()->get_object_custom_type_name(res_script.ptr());
  654. is_custom = _is_type_valid(custom_class, allowed_types);
  655. }
  656. if (!is_custom && !_is_type_valid(p_resource->get_class(), allowed_types)) {
  657. String class_str = (custom_class == StringName() ? p_resource->get_class() : vformat("%s (%s)", custom_class, p_resource->get_class()));
  658. 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));
  659. }
  660. }
  661. edited_resource = p_resource;
  662. _update_resource();
  663. }
  664. RES EditorResourcePicker::get_edited_resource() {
  665. return edited_resource;
  666. }
  667. void EditorResourcePicker::set_toggle_mode(bool p_enable) {
  668. assign_button->set_toggle_mode(p_enable);
  669. }
  670. bool EditorResourcePicker::is_toggle_mode() const {
  671. return assign_button->is_toggle_mode();
  672. }
  673. void EditorResourcePicker::set_toggle_pressed(bool p_pressed) {
  674. if (!is_toggle_mode()) {
  675. return;
  676. }
  677. assign_button->set_pressed(p_pressed);
  678. }
  679. void EditorResourcePicker::set_editable(bool p_editable) {
  680. editable = p_editable;
  681. assign_button->set_disabled(!editable);
  682. edit_button->set_visible(editable);
  683. }
  684. bool EditorResourcePicker::is_editable() const {
  685. return editable;
  686. }
  687. EditorResourcePicker::EditorResourcePicker() {
  688. assign_button = memnew(Button);
  689. assign_button->set_flat(true);
  690. assign_button->set_h_size_flags(SIZE_EXPAND_FILL);
  691. assign_button->set_clip_text(true);
  692. assign_button->set_drag_forwarding(this);
  693. add_child(assign_button);
  694. assign_button->connect("pressed", this, "_resource_selected");
  695. assign_button->connect("draw", this, "_button_draw");
  696. assign_button->connect("gui_input", this, "_button_input");
  697. preview_rect = memnew(TextureRect);
  698. preview_rect->set_expand(true);
  699. preview_rect->set_anchors_and_margins_preset(PRESET_WIDE);
  700. preview_rect->set_margin(MARGIN_TOP, 1);
  701. preview_rect->set_margin(MARGIN_BOTTOM, -1);
  702. preview_rect->set_margin(MARGIN_RIGHT, -1);
  703. assign_button->add_child(preview_rect);
  704. edit_button = memnew(Button);
  705. edit_button->set_flat(true);
  706. edit_button->set_toggle_mode(true);
  707. edit_button->connect("pressed", this, "_update_menu");
  708. add_child(edit_button);
  709. edit_button->connect("gui_input", this, "_button_input");
  710. edit_menu = memnew(PopupMenu);
  711. add_child(edit_menu);
  712. edit_menu->connect("id_pressed", this, "_edit_menu_cbk");
  713. edit_menu->connect("popup_hide", edit_button, "set_pressed", varray(false));
  714. add_constant_override("separation", 0);
  715. }
  716. void EditorScriptPicker::set_create_options(Object *p_menu_node) {
  717. PopupMenu *menu_node = Object::cast_to<PopupMenu>(p_menu_node);
  718. if (!menu_node) {
  719. return;
  720. }
  721. menu_node->add_icon_item(get_icon("ScriptCreate", "EditorIcons"), TTR("New Script"), OBJ_MENU_NEW_SCRIPT);
  722. if (script_owner) {
  723. Ref<Script> script = script_owner->get_script();
  724. if (script.is_valid()) {
  725. menu_node->add_icon_item(get_icon("ScriptExtend", "EditorIcons"), TTR("Extend Script"), OBJ_MENU_EXTEND_SCRIPT);
  726. }
  727. }
  728. menu_node->add_separator();
  729. }
  730. bool EditorScriptPicker::handle_menu_selected(int p_which) {
  731. switch (p_which) {
  732. case OBJ_MENU_NEW_SCRIPT: {
  733. if (script_owner) {
  734. EditorNode::get_singleton()->get_scene_tree_dock()->open_script_dialog(script_owner, false);
  735. }
  736. return true;
  737. }
  738. case OBJ_MENU_EXTEND_SCRIPT: {
  739. if (script_owner) {
  740. EditorNode::get_singleton()->get_scene_tree_dock()->open_script_dialog(script_owner, true);
  741. }
  742. return true;
  743. }
  744. }
  745. return false;
  746. }
  747. void EditorScriptPicker::set_script_owner(Node *p_owner) {
  748. script_owner = p_owner;
  749. }
  750. Node *EditorScriptPicker::get_script_owner() const {
  751. return script_owner;
  752. }
  753. void EditorScriptPicker::_bind_methods() {
  754. ClassDB::bind_method(D_METHOD("set_script_owner", "owner_node"), &EditorScriptPicker::set_script_owner);
  755. ClassDB::bind_method(D_METHOD("get_script_owner"), &EditorScriptPicker::get_script_owner);
  756. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "script_owner", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_script_owner", "get_script_owner");
  757. }
  758. EditorScriptPicker::EditorScriptPicker() {
  759. }