editor_resource_picker.cpp 33 KB

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