editor_resource_picker.cpp 30 KB

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