asset_library_editor_plugin.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  1. /**************************************************************************/
  2. /* asset_library_editor_plugin.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 "asset_library_editor_plugin.h"
  31. #include "core/input/input.h"
  32. #include "core/io/json.h"
  33. #include "core/io/stream_peer_tls.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/version.h"
  36. #include "editor/editor_main_screen.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_paths.h"
  39. #include "editor/editor_settings.h"
  40. #include "editor/editor_string_names.h"
  41. #include "editor/gui/editor_file_dialog.h"
  42. #include "editor/project_settings_editor.h"
  43. #include "editor/themes/editor_scale.h"
  44. #include "scene/gui/menu_button.h"
  45. #include "scene/resources/image_texture.h"
  46. static inline void setup_http_request(HTTPRequest *request) {
  47. request->set_use_threads(EDITOR_GET("asset_library/use_threads"));
  48. const String proxy_host = EDITOR_GET("network/http_proxy/host");
  49. const int proxy_port = EDITOR_GET("network/http_proxy/port");
  50. request->set_http_proxy(proxy_host, proxy_port);
  51. request->set_https_proxy(proxy_host, proxy_port);
  52. }
  53. void EditorAssetLibraryItem::configure(const String &p_title, int p_asset_id, const String &p_category, int p_category_id, const String &p_author, int p_author_id, const String &p_cost) {
  54. title_text = p_title;
  55. title->set_text(title_text);
  56. title->set_tooltip_text(title_text);
  57. asset_id = p_asset_id;
  58. category->set_text(p_category);
  59. category_id = p_category_id;
  60. author->set_text(p_author);
  61. author_id = p_author_id;
  62. price->set_text(p_cost);
  63. }
  64. // TODO: Refactor this method to use the TextServer.
  65. void EditorAssetLibraryItem::clamp_width(int p_max_width) {
  66. int text_pixel_width = title->get_button_font()->get_string_size(title_text).x * EDSCALE;
  67. if (text_pixel_width > p_max_width) {
  68. // Truncate title text to within the current column width.
  69. int max_length = p_max_width / (text_pixel_width / title_text.length());
  70. String truncated_text = title_text.left(max_length - 3) + "...";
  71. title->set_text(truncated_text);
  72. } else {
  73. title->set_text(title_text);
  74. }
  75. }
  76. void EditorAssetLibraryItem::set_image(int p_type, int p_index, const Ref<Texture2D> &p_image) {
  77. ERR_FAIL_COND(p_type != EditorAssetLibrary::IMAGE_QUEUE_ICON);
  78. ERR_FAIL_COND(p_index != 0);
  79. icon->set_texture_normal(p_image);
  80. }
  81. void EditorAssetLibraryItem::_notification(int p_what) {
  82. switch (p_what) {
  83. case NOTIFICATION_ENTER_TREE: {
  84. icon->set_texture_normal(get_editor_theme_icon(SNAME("ProjectIconLoading")));
  85. category->add_theme_color_override(SceneStringName(font_color), Color(0.5, 0.5, 0.5));
  86. author->add_theme_color_override(SceneStringName(font_color), Color(0.5, 0.5, 0.5));
  87. price->add_theme_color_override(SceneStringName(font_color), Color(0.5, 0.5, 0.5));
  88. if (author->get_default_cursor_shape() == CURSOR_ARROW) {
  89. // Disable visible feedback if author link isn't clickable.
  90. author->add_theme_color_override("font_pressed_color", Color(0.5, 0.5, 0.5));
  91. author->add_theme_color_override("font_hover_color", Color(0.5, 0.5, 0.5));
  92. }
  93. } break;
  94. }
  95. }
  96. void EditorAssetLibraryItem::_asset_clicked() {
  97. emit_signal(SNAME("asset_selected"), asset_id);
  98. }
  99. void EditorAssetLibraryItem::_category_clicked() {
  100. emit_signal(SNAME("category_selected"), category_id);
  101. }
  102. void EditorAssetLibraryItem::_author_clicked() {
  103. emit_signal(SNAME("author_selected"), author->get_text());
  104. }
  105. void EditorAssetLibraryItem::_bind_methods() {
  106. ClassDB::bind_method("set_image", &EditorAssetLibraryItem::set_image);
  107. ADD_SIGNAL(MethodInfo("asset_selected"));
  108. ADD_SIGNAL(MethodInfo("category_selected"));
  109. ADD_SIGNAL(MethodInfo("author_selected"));
  110. }
  111. EditorAssetLibraryItem::EditorAssetLibraryItem(bool p_clickable) {
  112. Ref<StyleBoxEmpty> border;
  113. border.instantiate();
  114. border->set_content_margin_all(5 * EDSCALE);
  115. add_theme_style_override(SceneStringName(panel), border);
  116. HBoxContainer *hb = memnew(HBoxContainer);
  117. // Add some spacing to visually separate the icon from the asset details.
  118. hb->add_theme_constant_override("separation", 15 * EDSCALE);
  119. add_child(hb);
  120. icon = memnew(TextureButton);
  121. icon->set_custom_minimum_size(Size2(64, 64) * EDSCALE);
  122. hb->add_child(icon);
  123. VBoxContainer *vb = memnew(VBoxContainer);
  124. hb->add_child(vb);
  125. vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  126. title = memnew(LinkButton);
  127. title->set_auto_translate_mode(AutoTranslateMode::AUTO_TRANSLATE_MODE_DISABLED);
  128. title->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  129. vb->add_child(title);
  130. category = memnew(LinkButton);
  131. category->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  132. vb->add_child(category);
  133. HBoxContainer *author_price_hbox = memnew(HBoxContainer);
  134. author_price_hbox->add_theme_constant_override("separation", 5 * EDSCALE);
  135. vb->add_child(author_price_hbox);
  136. author = memnew(LinkButton);
  137. author->set_tooltip_text(TTR("Author"));
  138. author_price_hbox->add_child(author);
  139. author_price_hbox->add_child(memnew(HSeparator));
  140. if (p_clickable) {
  141. author->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  142. icon->set_default_cursor_shape(CURSOR_POINTING_HAND);
  143. icon->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibraryItem::_asset_clicked));
  144. title->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibraryItem::_asset_clicked));
  145. category->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibraryItem::_category_clicked));
  146. author->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibraryItem::_author_clicked));
  147. } else {
  148. title->set_mouse_filter(MOUSE_FILTER_IGNORE);
  149. category->set_mouse_filter(MOUSE_FILTER_IGNORE);
  150. author->set_underline_mode(LinkButton::UNDERLINE_MODE_NEVER);
  151. author->set_default_cursor_shape(CURSOR_ARROW);
  152. }
  153. Ref<StyleBoxEmpty> label_margin;
  154. label_margin.instantiate();
  155. label_margin->set_content_margin_all(0);
  156. price = memnew(Label);
  157. price->add_theme_style_override(CoreStringName(normal), label_margin);
  158. price->set_tooltip_text(TTR("License"));
  159. price->set_mouse_filter(MOUSE_FILTER_PASS);
  160. author_price_hbox->add_child(price);
  161. set_custom_minimum_size(Size2(250, 80) * EDSCALE);
  162. set_h_size_flags(Control::SIZE_EXPAND_FILL);
  163. }
  164. //////////////////////////////////////////////////////////////////////////////
  165. void EditorAssetLibraryItemDescription::set_image(int p_type, int p_index, const Ref<Texture2D> &p_image) {
  166. switch (p_type) {
  167. case EditorAssetLibrary::IMAGE_QUEUE_ICON: {
  168. item->call("set_image", p_type, p_index, p_image);
  169. icon = p_image;
  170. } break;
  171. case EditorAssetLibrary::IMAGE_QUEUE_THUMBNAIL: {
  172. for (int i = 0; i < preview_images.size(); i++) {
  173. if (preview_images[i].id == p_index) {
  174. if (preview_images[i].is_video) {
  175. Ref<Image> overlay = previews->get_editor_theme_icon(SNAME("PlayOverlay"))->get_image();
  176. Ref<Image> thumbnail = p_image->get_image();
  177. thumbnail = thumbnail->duplicate();
  178. Point2i overlay_pos = Point2i((thumbnail->get_width() - overlay->get_width()) / 2, (thumbnail->get_height() - overlay->get_height()) / 2);
  179. // Overlay and thumbnail need the same format for `blend_rect` to work.
  180. thumbnail->convert(Image::FORMAT_RGBA8);
  181. thumbnail->blend_rect(overlay, overlay->get_used_rect(), overlay_pos);
  182. preview_images[i].button->set_button_icon(ImageTexture::create_from_image(thumbnail));
  183. // Make it clearer that clicking it will open an external link
  184. preview_images[i].button->set_default_cursor_shape(Control::CURSOR_POINTING_HAND);
  185. } else {
  186. preview_images[i].button->set_button_icon(p_image);
  187. }
  188. break;
  189. }
  190. }
  191. } break;
  192. case EditorAssetLibrary::IMAGE_QUEUE_SCREENSHOT: {
  193. for (int i = 0; i < preview_images.size(); i++) {
  194. if (preview_images[i].id == p_index) {
  195. preview_images.write[i].image = p_image;
  196. if (preview_images[i].button->is_pressed()) {
  197. _preview_click(p_index);
  198. }
  199. break;
  200. }
  201. }
  202. } break;
  203. }
  204. }
  205. void EditorAssetLibraryItemDescription::_notification(int p_what) {
  206. switch (p_what) {
  207. case NOTIFICATION_ENTER_TREE:
  208. case NOTIFICATION_THEME_CHANGED: {
  209. previews_bg->add_theme_style_override(SceneStringName(panel), previews->get_theme_stylebox(CoreStringName(normal), SNAME("TextEdit")));
  210. } break;
  211. }
  212. }
  213. void EditorAssetLibraryItemDescription::_bind_methods() {
  214. ClassDB::bind_method(D_METHOD("set_image"), &EditorAssetLibraryItemDescription::set_image);
  215. }
  216. void EditorAssetLibraryItemDescription::_link_click(const String &p_url) {
  217. ERR_FAIL_COND(!p_url.begins_with("http"));
  218. OS::get_singleton()->shell_open(p_url);
  219. }
  220. void EditorAssetLibraryItemDescription::_preview_click(int p_id) {
  221. for (int i = 0; i < preview_images.size(); i++) {
  222. if (preview_images[i].id == p_id) {
  223. preview_images[i].button->set_pressed(true);
  224. if (!preview_images[i].is_video) {
  225. if (preview_images[i].image.is_valid()) {
  226. preview->set_texture(preview_images[i].image);
  227. child_controls_changed();
  228. }
  229. } else {
  230. _link_click(preview_images[i].video_link);
  231. }
  232. } else {
  233. preview_images[i].button->set_pressed(false);
  234. }
  235. }
  236. }
  237. void EditorAssetLibraryItemDescription::configure(const String &p_title, int p_asset_id, const String &p_category, int p_category_id, const String &p_author, int p_author_id, const String &p_cost, int p_version, const String &p_version_string, const String &p_description, const String &p_download_url, const String &p_browse_url, const String &p_sha256_hash) {
  238. asset_id = p_asset_id;
  239. title = p_title;
  240. download_url = p_download_url;
  241. sha256 = p_sha256_hash;
  242. item->configure(p_title, p_asset_id, p_category, p_category_id, p_author, p_author_id, p_cost);
  243. description->clear();
  244. description->add_text(TTR("Version:") + " " + p_version_string + "\n");
  245. description->add_text(TTR("Contents:") + " ");
  246. description->push_meta(p_browse_url);
  247. description->add_text(TTR("View Files"));
  248. description->pop();
  249. description->add_text("\n" + TTR("Description:") + "\n\n");
  250. description->append_text(p_description);
  251. description->set_selection_enabled(true);
  252. description->set_context_menu_enabled(true);
  253. set_title(p_title);
  254. }
  255. void EditorAssetLibraryItemDescription::add_preview(int p_id, bool p_video, const String &p_url) {
  256. if (preview_images.is_empty()) {
  257. previews_vbox->show();
  258. }
  259. Preview new_preview;
  260. new_preview.id = p_id;
  261. new_preview.video_link = p_url;
  262. new_preview.is_video = p_video;
  263. new_preview.button = memnew(Button);
  264. new_preview.button->set_button_icon(previews->get_editor_theme_icon(SNAME("ThumbnailWait")));
  265. new_preview.button->set_toggle_mode(true);
  266. new_preview.button->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibraryItemDescription::_preview_click).bind(p_id));
  267. preview_hb->add_child(new_preview.button);
  268. if (!p_video) {
  269. new_preview.image = previews->get_editor_theme_icon(SNAME("ThumbnailWait"));
  270. }
  271. preview_images.push_back(new_preview);
  272. if (preview_images.size() == 1 && !p_video) {
  273. _preview_click(p_id);
  274. }
  275. }
  276. EditorAssetLibraryItemDescription::EditorAssetLibraryItemDescription() {
  277. HBoxContainer *hbox = memnew(HBoxContainer);
  278. add_child(hbox);
  279. VBoxContainer *desc_vbox = memnew(VBoxContainer);
  280. hbox->add_child(desc_vbox);
  281. hbox->add_theme_constant_override("separation", 15 * EDSCALE);
  282. item = memnew(EditorAssetLibraryItem);
  283. desc_vbox->add_child(item);
  284. desc_vbox->set_custom_minimum_size(Size2(440 * EDSCALE, 440 * EDSCALE));
  285. description = memnew(RichTextLabel);
  286. desc_vbox->add_child(description);
  287. description->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  288. description->connect("meta_clicked", callable_mp(this, &EditorAssetLibraryItemDescription::_link_click));
  289. description->add_theme_constant_override(SceneStringName(line_separation), Math::round(5 * EDSCALE));
  290. previews_vbox = memnew(VBoxContainer);
  291. previews_vbox->hide(); // Will be shown if we add any previews later.
  292. hbox->add_child(previews_vbox);
  293. previews_vbox->add_theme_constant_override("separation", 15 * EDSCALE);
  294. previews_vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  295. previews_vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  296. preview = memnew(TextureRect);
  297. previews_vbox->add_child(preview);
  298. preview->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  299. preview->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  300. preview->set_custom_minimum_size(Size2(640 * EDSCALE, 345 * EDSCALE));
  301. preview->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  302. preview->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  303. previews_bg = memnew(PanelContainer);
  304. previews_vbox->add_child(previews_bg);
  305. previews_bg->set_custom_minimum_size(Size2(640 * EDSCALE, 101 * EDSCALE));
  306. previews = memnew(ScrollContainer);
  307. previews_bg->add_child(previews);
  308. previews->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  309. preview_hb = memnew(HBoxContainer);
  310. preview_hb->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  311. previews->add_child(preview_hb);
  312. set_ok_button_text(TTR("Download"));
  313. set_cancel_button_text(TTR("Close"));
  314. }
  315. ///////////////////////////////////////////////////////////////////////////////////
  316. void EditorAssetLibraryItemDownload::_http_download_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data) {
  317. String error_text;
  318. switch (p_status) {
  319. case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH:
  320. case HTTPRequest::RESULT_CONNECTION_ERROR:
  321. case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED: {
  322. error_text = TTR("Connection error, please try again.");
  323. status->set_text(TTR("Can't connect."));
  324. } break;
  325. case HTTPRequest::RESULT_CANT_CONNECT:
  326. case HTTPRequest::RESULT_TLS_HANDSHAKE_ERROR: {
  327. error_text = TTR("Can't connect to host:") + " " + host;
  328. status->set_text(TTR("Can't connect."));
  329. } break;
  330. case HTTPRequest::RESULT_NO_RESPONSE: {
  331. error_text = TTR("No response from host:") + " " + host;
  332. status->set_text(TTR("No response."));
  333. } break;
  334. case HTTPRequest::RESULT_CANT_RESOLVE: {
  335. error_text = TTR("Can't resolve hostname:") + " " + host;
  336. status->set_text(TTR("Can't resolve."));
  337. } break;
  338. case HTTPRequest::RESULT_REQUEST_FAILED: {
  339. error_text = TTR("Request failed, return code:") + " " + itos(p_code);
  340. status->set_text(TTR("Request failed."));
  341. } break;
  342. case HTTPRequest::RESULT_DOWNLOAD_FILE_CANT_OPEN:
  343. case HTTPRequest::RESULT_DOWNLOAD_FILE_WRITE_ERROR: {
  344. error_text = TTR("Cannot save response to:") + " " + download->get_download_file();
  345. status->set_text(TTR("Write error."));
  346. } break;
  347. case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: {
  348. error_text = TTR("Request failed, too many redirects");
  349. status->set_text(TTR("Redirect loop."));
  350. } break;
  351. case HTTPRequest::RESULT_TIMEOUT: {
  352. error_text = TTR("Request failed, timeout");
  353. status->set_text(TTR("Timeout."));
  354. } break;
  355. default: {
  356. if (p_code != 200) {
  357. error_text = TTR("Request failed, return code:") + " " + itos(p_code);
  358. status->set_text(TTR("Failed:") + " " + itos(p_code));
  359. } else if (!sha256.is_empty()) {
  360. String download_sha256 = FileAccess::get_sha256(download->get_download_file());
  361. if (sha256 != download_sha256) {
  362. error_text = TTR("Bad download hash, assuming file has been tampered with.") + "\n";
  363. error_text += TTR("Expected:") + " " + sha256 + "\n" + TTR("Got:") + " " + download_sha256;
  364. status->set_text(TTR("Failed SHA-256 hash check"));
  365. }
  366. }
  367. } break;
  368. }
  369. // Make the progress bar invisible but don't reflow other Controls around it.
  370. progress->set_modulate(Color(0, 0, 0, 0));
  371. progress->set_indeterminate(false);
  372. if (!error_text.is_empty()) {
  373. download_error->set_text(TTR("Asset Download Error:") + "\n" + error_text);
  374. download_error->popup_centered();
  375. // Let the user retry the download.
  376. retry_button->show();
  377. return;
  378. }
  379. install_button->set_disabled(false);
  380. status->set_text(TTR("Ready to install!"));
  381. set_process(false);
  382. // Automatically prompt for installation once the download is completed.
  383. install();
  384. }
  385. void EditorAssetLibraryItemDownload::configure(const String &p_title, int p_asset_id, const Ref<Texture2D> &p_preview, const String &p_download_url, const String &p_sha256_hash) {
  386. title->set_text(p_title);
  387. icon->set_texture(p_preview);
  388. asset_id = p_asset_id;
  389. if (!p_preview.is_valid()) {
  390. icon->set_texture(get_editor_theme_icon(SNAME("FileBrokenBigThumb")));
  391. }
  392. host = p_download_url;
  393. sha256 = p_sha256_hash;
  394. _make_request();
  395. }
  396. void EditorAssetLibraryItemDownload::_notification(int p_what) {
  397. switch (p_what) {
  398. case NOTIFICATION_THEME_CHANGED: {
  399. panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("AssetLib")));
  400. status->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("status_color"), SNAME("AssetLib")));
  401. dismiss_button->set_texture_normal(get_theme_icon(SNAME("dismiss"), SNAME("AssetLib")));
  402. } break;
  403. case NOTIFICATION_PROCESS: {
  404. // Make the progress bar visible again when retrying the download.
  405. progress->set_modulate(Color(1, 1, 1, 1));
  406. if (download->get_downloaded_bytes() > 0) {
  407. progress->set_max(download->get_body_size());
  408. progress->set_value(download->get_downloaded_bytes());
  409. }
  410. int cstatus = download->get_http_client_status();
  411. if (cstatus == HTTPClient::STATUS_BODY) {
  412. if (download->get_body_size() > 0) {
  413. progress->set_indeterminate(false);
  414. status->set_text(vformat(
  415. TTR("Downloading (%s / %s)..."),
  416. String::humanize_size(download->get_downloaded_bytes()),
  417. String::humanize_size(download->get_body_size())));
  418. } else {
  419. progress->set_indeterminate(true);
  420. status->set_text(vformat(
  421. TTR("Downloading...") + " (%s)",
  422. String::humanize_size(download->get_downloaded_bytes())));
  423. }
  424. }
  425. if (cstatus != prev_status) {
  426. switch (cstatus) {
  427. case HTTPClient::STATUS_RESOLVING: {
  428. status->set_text(TTR("Resolving..."));
  429. progress->set_max(1);
  430. progress->set_value(0);
  431. } break;
  432. case HTTPClient::STATUS_CONNECTING: {
  433. status->set_text(TTR("Connecting..."));
  434. progress->set_max(1);
  435. progress->set_value(0);
  436. } break;
  437. case HTTPClient::STATUS_REQUESTING: {
  438. status->set_text(TTR("Requesting..."));
  439. progress->set_max(1);
  440. progress->set_value(0);
  441. } break;
  442. default: {
  443. }
  444. }
  445. prev_status = cstatus;
  446. }
  447. } break;
  448. }
  449. }
  450. void EditorAssetLibraryItemDownload::_close() {
  451. // Clean up downloaded file.
  452. DirAccess::remove_file_or_error(download->get_download_file());
  453. queue_free();
  454. }
  455. bool EditorAssetLibraryItemDownload::can_install() const {
  456. return !install_button->is_disabled();
  457. }
  458. void EditorAssetLibraryItemDownload::install() {
  459. String file = download->get_download_file();
  460. if (external_install) {
  461. emit_signal(SNAME("install_asset"), file, title->get_text());
  462. return;
  463. }
  464. asset_installer->set_asset_name(title->get_text());
  465. asset_installer->open_asset(file, true);
  466. }
  467. void EditorAssetLibraryItemDownload::_make_request() {
  468. // Hide the Retry button if we've just pressed it.
  469. retry_button->hide();
  470. download->cancel_request();
  471. download->set_download_file(EditorPaths::get_singleton()->get_cache_dir().path_join("tmp_asset_" + itos(asset_id)) + ".zip");
  472. Error err = download->request(host);
  473. if (err != OK) {
  474. status->set_text(TTR("Error making request"));
  475. } else {
  476. progress->set_indeterminate(true);
  477. set_process(true);
  478. }
  479. }
  480. void EditorAssetLibraryItemDownload::_bind_methods() {
  481. ADD_SIGNAL(MethodInfo("install_asset", PropertyInfo(Variant::STRING, "zip_path"), PropertyInfo(Variant::STRING, "name")));
  482. }
  483. EditorAssetLibraryItemDownload::EditorAssetLibraryItemDownload() {
  484. panel = memnew(PanelContainer);
  485. add_child(panel);
  486. HBoxContainer *hb = memnew(HBoxContainer);
  487. panel->add_child(hb);
  488. icon = memnew(TextureRect);
  489. icon->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  490. icon->set_v_size_flags(0);
  491. hb->add_child(icon);
  492. VBoxContainer *vb = memnew(VBoxContainer);
  493. hb->add_child(vb);
  494. vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  495. HBoxContainer *title_hb = memnew(HBoxContainer);
  496. vb->add_child(title_hb);
  497. title = memnew(Label);
  498. title_hb->add_child(title);
  499. title->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  500. dismiss_button = memnew(TextureButton);
  501. dismiss_button->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibraryItemDownload::_close));
  502. title_hb->add_child(dismiss_button);
  503. title->set_clip_text(true);
  504. vb->add_spacer();
  505. status = memnew(Label(TTR("Idle")));
  506. vb->add_child(status);
  507. progress = memnew(ProgressBar);
  508. progress->set_editor_preview_indeterminate(true);
  509. vb->add_child(progress);
  510. HBoxContainer *hb2 = memnew(HBoxContainer);
  511. vb->add_child(hb2);
  512. hb2->add_spacer();
  513. install_button = memnew(Button);
  514. install_button->set_text(TTR("Install..."));
  515. install_button->set_disabled(true);
  516. install_button->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibraryItemDownload::install));
  517. retry_button = memnew(Button);
  518. retry_button->set_text(TTR("Retry"));
  519. retry_button->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibraryItemDownload::_make_request));
  520. // Only show the Retry button in case of a failure.
  521. retry_button->hide();
  522. hb2->add_child(retry_button);
  523. hb2->add_child(install_button);
  524. set_custom_minimum_size(Size2(310, 0) * EDSCALE);
  525. download = memnew(HTTPRequest);
  526. panel->add_child(download);
  527. download->connect("request_completed", callable_mp(this, &EditorAssetLibraryItemDownload::_http_download_completed));
  528. setup_http_request(download);
  529. download_error = memnew(AcceptDialog);
  530. panel->add_child(download_error);
  531. download_error->set_title(TTR("Download Error"));
  532. asset_installer = memnew(EditorAssetInstaller);
  533. panel->add_child(asset_installer);
  534. asset_installer->connect(SceneStringName(confirmed), callable_mp(this, &EditorAssetLibraryItemDownload::_close));
  535. prev_status = -1;
  536. external_install = false;
  537. }
  538. ////////////////////////////////////////////////////////////////////////////////
  539. void EditorAssetLibrary::_notification(int p_what) {
  540. switch (p_what) {
  541. case NOTIFICATION_READY: {
  542. add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("bg"), SNAME("AssetLib")));
  543. error_label->move_to_front();
  544. } break;
  545. case NOTIFICATION_ENTER_TREE:
  546. case NOTIFICATION_THEME_CHANGED: {
  547. error_tr->set_texture(get_editor_theme_icon(SNAME("Error")));
  548. filter->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  549. library_scroll_bg->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  550. downloads_scroll->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  551. error_label->add_theme_color_override("color", get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  552. } break;
  553. case NOTIFICATION_VISIBILITY_CHANGED: {
  554. if (is_visible()) {
  555. #ifndef ANDROID_ENABLED
  556. // Focus the search box automatically when switching to the Templates tab (in the Project Manager)
  557. // or switching to the AssetLib tab (in the editor).
  558. // The Project Manager's project filter box is automatically focused in the project manager code.
  559. filter->grab_focus();
  560. #endif
  561. if (initial_loading) {
  562. _repository_changed(0); // Update when shown for the first time.
  563. }
  564. }
  565. } break;
  566. case NOTIFICATION_PROCESS: {
  567. HTTPClient::Status s = request->get_http_client_status();
  568. const bool loading = s != HTTPClient::STATUS_DISCONNECTED;
  569. if (loading) {
  570. library_scroll->set_modulate(Color(1, 1, 1, 0.5));
  571. } else {
  572. library_scroll->set_modulate(Color(1, 1, 1, 1));
  573. }
  574. const bool no_downloads = downloads_hb->get_child_count() == 0;
  575. if (no_downloads == downloads_scroll->is_visible()) {
  576. downloads_scroll->set_visible(!no_downloads);
  577. }
  578. } break;
  579. case NOTIFICATION_RESIZED: {
  580. _update_asset_items_columns();
  581. } break;
  582. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  583. if (!EditorSettings::get_singleton()->check_changed_settings_in_group("asset_library") &&
  584. !EditorSettings::get_singleton()->check_changed_settings_in_group("network")) {
  585. break;
  586. }
  587. _update_repository_options();
  588. setup_http_request(request);
  589. const bool loading_blocked_new = ((int)EDITOR_GET("network/connection/network_mode") == EditorSettings::NETWORK_OFFLINE);
  590. if (loading_blocked_new != loading_blocked) {
  591. loading_blocked = loading_blocked_new;
  592. if (!loading_blocked && is_visible()) {
  593. _request_current_config(); // Reload config now that the network is available.
  594. }
  595. }
  596. } break;
  597. }
  598. }
  599. void EditorAssetLibrary::_update_repository_options() {
  600. // TODO: Move to editor_settings.cpp
  601. Dictionary default_urls;
  602. default_urls["godotengine.org (Official)"] = "https://godotengine.org/asset-library/api";
  603. Dictionary available_urls = _EDITOR_DEF("asset_library/available_urls", default_urls, true);
  604. repository->clear();
  605. Array keys = available_urls.keys();
  606. for (int i = 0; i < keys.size(); i++) {
  607. String key = keys[i];
  608. repository->add_item(key);
  609. repository->set_item_metadata(i, available_urls[key]);
  610. }
  611. }
  612. void EditorAssetLibrary::shortcut_input(const Ref<InputEvent> &p_event) {
  613. ERR_FAIL_COND(p_event.is_null());
  614. const Ref<InputEventKey> key = p_event;
  615. if (key.is_valid() && key->is_pressed()) {
  616. if (key->is_match(InputEventKey::create_reference(KeyModifierMask::CMD_OR_CTRL | Key::F)) && is_visible_in_tree()) {
  617. filter->grab_focus();
  618. filter->select_all();
  619. accept_event();
  620. }
  621. }
  622. }
  623. void EditorAssetLibrary::_install_asset() {
  624. ERR_FAIL_NULL(description);
  625. EditorAssetLibraryItemDownload *d = _get_asset_in_progress(description->get_asset_id());
  626. if (d) {
  627. d->install();
  628. return;
  629. }
  630. EditorAssetLibraryItemDownload *download = memnew(EditorAssetLibraryItemDownload);
  631. downloads_hb->add_child(download);
  632. download->configure(description->get_title(), description->get_asset_id(), description->get_preview_icon(), description->get_download_url(), description->get_sha256());
  633. if (templates_only) {
  634. download->set_external_install(true);
  635. download->connect("install_asset", callable_mp(this, &EditorAssetLibrary::_install_external_asset));
  636. }
  637. }
  638. const char *EditorAssetLibrary::sort_key[SORT_MAX] = {
  639. "updated",
  640. "updated",
  641. "name",
  642. "name",
  643. "cost",
  644. "cost",
  645. };
  646. const char *EditorAssetLibrary::sort_text[SORT_MAX] = {
  647. TTRC("Recently Updated"),
  648. TTRC("Least Recently Updated"),
  649. TTRC("Name (A-Z)"),
  650. TTRC("Name (Z-A)"),
  651. TTRC("License (A-Z)"), // "cost" stores the SPDX license name in the Godot Asset Library.
  652. TTRC("License (Z-A)"), // "cost" stores the SPDX license name in the Godot Asset Library.
  653. };
  654. const char *EditorAssetLibrary::support_key[SUPPORT_MAX] = {
  655. "official", // Former name for the Featured support level (still used on the API backend).
  656. "community",
  657. "testing",
  658. };
  659. const char *EditorAssetLibrary::support_text[SUPPORT_MAX] = {
  660. TTRC("Featured"),
  661. TTRC("Community"),
  662. TTRC("Testing"),
  663. };
  664. void EditorAssetLibrary::_select_author(const String &p_author) {
  665. if (!host.contains("godotengine.org")) {
  666. // Don't open the link for alternative repositories.
  667. return;
  668. }
  669. OS::get_singleton()->shell_open("https://godotengine.org/asset-library/asset?user=" + p_author.uri_encode());
  670. }
  671. void EditorAssetLibrary::_select_category(int p_id) {
  672. for (int i = 0; i < categories->get_item_count(); i++) {
  673. if (i == 0) {
  674. continue;
  675. }
  676. int id = categories->get_item_metadata(i);
  677. if (id == p_id) {
  678. categories->select(i);
  679. _search();
  680. break;
  681. }
  682. }
  683. }
  684. void EditorAssetLibrary::_select_asset(int p_id) {
  685. _api_request("asset/" + itos(p_id), REQUESTING_ASSET);
  686. }
  687. void EditorAssetLibrary::_image_update(bool p_use_cache, bool p_final, const PackedByteArray &p_data, int p_queue_id) {
  688. Object *obj = ObjectDB::get_instance(image_queue[p_queue_id].target);
  689. if (!obj) {
  690. return;
  691. }
  692. bool image_set = false;
  693. PackedByteArray image_data = p_data;
  694. if (p_use_cache) {
  695. String cache_filename_base = EditorPaths::get_singleton()->get_cache_dir().path_join("assetimage_" + image_queue[p_queue_id].image_url.md5_text());
  696. Ref<FileAccess> file = FileAccess::open(cache_filename_base + ".data", FileAccess::READ);
  697. if (file.is_valid()) {
  698. PackedByteArray cached_data;
  699. int len = file->get_32();
  700. cached_data.resize(len);
  701. uint8_t *w = cached_data.ptrw();
  702. file->get_buffer(w, len);
  703. image_data = cached_data;
  704. }
  705. }
  706. int len = image_data.size();
  707. const uint8_t *r = image_data.ptr();
  708. Ref<Image> image = memnew(Image);
  709. uint8_t png_signature[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
  710. uint8_t jpg_signature[3] = { 255, 216, 255 };
  711. uint8_t webp_signature[4] = { 82, 73, 70, 70 };
  712. uint8_t bmp_signature[2] = { 66, 77 };
  713. if (r) {
  714. Ref<Image> parsed_image;
  715. if ((memcmp(&r[0], &png_signature[0], 8) == 0) && Image::_png_mem_loader_func) {
  716. parsed_image = Image::_png_mem_loader_func(r, len);
  717. } else if ((memcmp(&r[0], &jpg_signature[0], 3) == 0) && Image::_jpg_mem_loader_func) {
  718. parsed_image = Image::_jpg_mem_loader_func(r, len);
  719. } else if ((memcmp(&r[0], &webp_signature[0], 4) == 0) && Image::_webp_mem_loader_func) {
  720. parsed_image = Image::_webp_mem_loader_func(r, len);
  721. } else if ((memcmp(&r[0], &bmp_signature[0], 2) == 0) && Image::_bmp_mem_loader_func) {
  722. parsed_image = Image::_bmp_mem_loader_func(r, len);
  723. } else if (Image::_svg_scalable_mem_loader_func) {
  724. parsed_image = Image::_svg_scalable_mem_loader_func(r, len, 1.0);
  725. }
  726. if (parsed_image.is_null()) {
  727. if (is_print_verbose_enabled()) {
  728. ERR_PRINT(vformat("Asset Library: Invalid image downloaded from '%s' for asset # %d", image_queue[p_queue_id].image_url, image_queue[p_queue_id].asset_id));
  729. }
  730. } else {
  731. image->copy_internals_from(parsed_image);
  732. }
  733. }
  734. if (!image->is_empty()) {
  735. switch (image_queue[p_queue_id].image_type) {
  736. case IMAGE_QUEUE_ICON:
  737. image->resize(64 * EDSCALE, 64 * EDSCALE, Image::INTERPOLATE_LANCZOS);
  738. break;
  739. case IMAGE_QUEUE_THUMBNAIL: {
  740. float max_height = 85 * EDSCALE;
  741. float scale_ratio = max_height / (image->get_height() * EDSCALE);
  742. if (scale_ratio < 1) {
  743. image->resize(image->get_width() * EDSCALE * scale_ratio, image->get_height() * EDSCALE * scale_ratio, Image::INTERPOLATE_LANCZOS);
  744. }
  745. } break;
  746. case IMAGE_QUEUE_SCREENSHOT: {
  747. float max_height = 397 * EDSCALE;
  748. float scale_ratio = max_height / (image->get_height() * EDSCALE);
  749. if (scale_ratio < 1) {
  750. image->resize(image->get_width() * EDSCALE * scale_ratio, image->get_height() * EDSCALE * scale_ratio, Image::INTERPOLATE_LANCZOS);
  751. }
  752. } break;
  753. }
  754. Ref<ImageTexture> tex = ImageTexture::create_from_image(image);
  755. obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, tex);
  756. image_set = true;
  757. }
  758. if (!image_set && p_final) {
  759. obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, get_editor_theme_icon(SNAME("FileBrokenBigThumb")));
  760. }
  761. }
  762. void EditorAssetLibrary::_image_request_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data, int p_queue_id) {
  763. ERR_FAIL_COND(!image_queue.has(p_queue_id));
  764. if (p_status == HTTPRequest::RESULT_SUCCESS && p_code < HTTPClient::RESPONSE_BAD_REQUEST) {
  765. if (p_code != HTTPClient::RESPONSE_NOT_MODIFIED) {
  766. for (int i = 0; i < headers.size(); i++) {
  767. if (headers[i].findn("ETag:") == 0) { // Save etag
  768. String cache_filename_base = EditorPaths::get_singleton()->get_cache_dir().path_join("assetimage_" + image_queue[p_queue_id].image_url.md5_text());
  769. String new_etag = headers[i].substr(headers[i].find_char(':') + 1, headers[i].length()).strip_edges();
  770. Ref<FileAccess> file = FileAccess::open(cache_filename_base + ".etag", FileAccess::WRITE);
  771. if (file.is_valid()) {
  772. file->store_line(new_etag);
  773. }
  774. int len = p_data.size();
  775. const uint8_t *r = p_data.ptr();
  776. file = FileAccess::open(cache_filename_base + ".data", FileAccess::WRITE);
  777. if (file.is_valid()) {
  778. file->store_32(len);
  779. file->store_buffer(r, len);
  780. }
  781. break;
  782. }
  783. }
  784. }
  785. _image_update(p_code == HTTPClient::RESPONSE_NOT_MODIFIED, true, p_data, p_queue_id);
  786. } else {
  787. if (is_print_verbose_enabled()) {
  788. WARN_PRINT(vformat("Asset Library: Error getting image from '%s' for asset # %d.", image_queue[p_queue_id].image_url, image_queue[p_queue_id].asset_id));
  789. }
  790. Object *obj = ObjectDB::get_instance(image_queue[p_queue_id].target);
  791. if (obj) {
  792. obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, get_editor_theme_icon(SNAME("FileBrokenBigThumb")));
  793. }
  794. }
  795. image_queue[p_queue_id].request->queue_free();
  796. image_queue.erase(p_queue_id);
  797. _update_image_queue();
  798. }
  799. void EditorAssetLibrary::_update_image_queue() {
  800. const int max_images = 6;
  801. int current_images = 0;
  802. List<int> to_delete;
  803. for (KeyValue<int, ImageQueue> &E : image_queue) {
  804. if (!E.value.active && current_images < max_images) {
  805. String cache_filename_base = EditorPaths::get_singleton()->get_cache_dir().path_join("assetimage_" + E.value.image_url.md5_text());
  806. Vector<String> headers;
  807. if (FileAccess::exists(cache_filename_base + ".etag") && FileAccess::exists(cache_filename_base + ".data")) {
  808. Ref<FileAccess> file = FileAccess::open(cache_filename_base + ".etag", FileAccess::READ);
  809. if (file.is_valid()) {
  810. headers.push_back("If-None-Match: " + file->get_line());
  811. }
  812. }
  813. Error err = E.value.request->request(E.value.image_url, headers);
  814. if (err != OK) {
  815. to_delete.push_back(E.key);
  816. } else {
  817. E.value.active = true;
  818. }
  819. current_images++;
  820. } else if (E.value.active) {
  821. current_images++;
  822. }
  823. }
  824. while (to_delete.size()) {
  825. image_queue[to_delete.front()->get()].request->queue_free();
  826. image_queue.erase(to_delete.front()->get());
  827. to_delete.pop_front();
  828. }
  829. }
  830. void EditorAssetLibrary::_request_image(ObjectID p_for, int p_asset_id, String p_image_url, ImageType p_type, int p_image_index) {
  831. // Remove extra spaces around the URL. This isn't strictly valid, but recoverable.
  832. String trimmed_url = p_image_url.strip_edges();
  833. if (trimmed_url != p_image_url && is_print_verbose_enabled()) {
  834. WARN_PRINT(vformat("Asset Library: Badly formatted image URL '%s' for asset # %d.", p_image_url, p_asset_id));
  835. }
  836. // Validate the image URL first.
  837. {
  838. String url_scheme;
  839. String url_host;
  840. int url_port;
  841. String url_path;
  842. String url_fragment;
  843. Error err = trimmed_url.parse_url(url_scheme, url_host, url_port, url_path, url_fragment);
  844. if (err != OK) {
  845. if (is_print_verbose_enabled()) {
  846. ERR_PRINT(vformat("Asset Library: Invalid image URL '%s' for asset # %d.", trimmed_url, p_asset_id));
  847. }
  848. Object *obj = ObjectDB::get_instance(p_for);
  849. if (obj) {
  850. obj->call("set_image", p_type, p_image_index, get_editor_theme_icon(SNAME("FileBrokenBigThumb")));
  851. }
  852. return;
  853. }
  854. }
  855. ImageQueue iq;
  856. iq.image_url = trimmed_url;
  857. iq.image_index = p_image_index;
  858. iq.image_type = p_type;
  859. iq.request = memnew(HTTPRequest);
  860. setup_http_request(iq.request);
  861. iq.target = p_for;
  862. iq.asset_id = p_asset_id;
  863. iq.queue_id = ++last_queue_id;
  864. iq.active = false;
  865. iq.request->connect("request_completed", callable_mp(this, &EditorAssetLibrary::_image_request_completed).bind(iq.queue_id));
  866. image_queue[iq.queue_id] = iq;
  867. add_child(iq.request);
  868. _image_update(true, false, PackedByteArray(), iq.queue_id);
  869. _update_image_queue();
  870. }
  871. void EditorAssetLibrary::_repository_changed(int p_repository_id) {
  872. _set_library_message(TTR("Loading..."));
  873. asset_top_page->hide();
  874. asset_bottom_page->hide();
  875. asset_items->hide();
  876. filter->set_editable(false);
  877. sort->set_disabled(true);
  878. categories->set_disabled(true);
  879. support->set_disabled(true);
  880. host = repository->get_item_metadata(p_repository_id);
  881. if (templates_only) {
  882. _api_request("configure", REQUESTING_CONFIG, "?type=project");
  883. } else {
  884. _api_request("configure", REQUESTING_CONFIG);
  885. }
  886. }
  887. void EditorAssetLibrary::_support_toggled(int p_support) {
  888. support->get_popup()->set_item_checked(p_support, !support->get_popup()->is_item_checked(p_support));
  889. _search();
  890. }
  891. void EditorAssetLibrary::_rerun_search(int p_ignore) {
  892. _search();
  893. }
  894. void EditorAssetLibrary::_search(int p_page) {
  895. String args;
  896. if (templates_only) {
  897. args += "?type=project&";
  898. } else {
  899. args += "?";
  900. }
  901. args += String() + "sort=" + sort_key[sort->get_selected()];
  902. // We use the "branch" version, i.e. major.minor, as patch releases should be compatible
  903. args += "&godot_version=" + String(VERSION_BRANCH);
  904. String support_list;
  905. for (int i = 0; i < SUPPORT_MAX; i++) {
  906. if (support->get_popup()->is_item_checked(i)) {
  907. support_list += String(support_key[i]) + "+";
  908. }
  909. }
  910. if (!support_list.is_empty()) {
  911. args += "&support=" + support_list.substr(0, support_list.length() - 1);
  912. }
  913. if (categories->get_selected() > 0) {
  914. args += "&category=" + itos(categories->get_item_metadata(categories->get_selected()));
  915. }
  916. // Sorting options with an odd index are always the reverse of the previous one
  917. if (sort->get_selected() % 2 == 1) {
  918. args += "&reverse=true";
  919. }
  920. if (!filter->get_text().is_empty()) {
  921. args += "&filter=" + filter->get_text().uri_encode();
  922. }
  923. if (p_page > 0) {
  924. args += "&page=" + itos(p_page);
  925. }
  926. _api_request("asset", REQUESTING_SEARCH, args);
  927. }
  928. void EditorAssetLibrary::_search_text_changed(const String &p_text) {
  929. filter_debounce_timer->start();
  930. }
  931. void EditorAssetLibrary::_filter_debounce_timer_timeout() {
  932. _search();
  933. }
  934. void EditorAssetLibrary::_request_current_config() {
  935. _repository_changed(repository->get_selected());
  936. }
  937. HBoxContainer *EditorAssetLibrary::_make_pages(int p_page, int p_page_count, int p_page_len, int p_total_items, int p_current_items) {
  938. HBoxContainer *hbc = memnew(HBoxContainer);
  939. if (p_page_count < 2) {
  940. return hbc;
  941. }
  942. //do the mario
  943. int from = p_page - (5 / EDSCALE);
  944. if (from < 0) {
  945. from = 0;
  946. }
  947. int to = from + (10 / EDSCALE);
  948. if (to > p_page_count) {
  949. to = p_page_count;
  950. }
  951. hbc->add_spacer();
  952. hbc->add_theme_constant_override("separation", 5 * EDSCALE);
  953. Button *first = memnew(Button);
  954. first->set_text(TTR("First", "Pagination"));
  955. first->set_theme_type_variation("PanelBackgroundButton");
  956. if (p_page != 0) {
  957. first->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibrary::_search).bind(0));
  958. } else {
  959. first->set_disabled(true);
  960. first->set_focus_mode(Control::FOCUS_NONE);
  961. }
  962. hbc->add_child(first);
  963. Button *prev = memnew(Button);
  964. prev->set_text(TTR("Previous", "Pagination"));
  965. prev->set_theme_type_variation("PanelBackgroundButton");
  966. if (p_page > 0) {
  967. prev->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibrary::_search).bind(p_page - 1));
  968. } else {
  969. prev->set_disabled(true);
  970. prev->set_focus_mode(Control::FOCUS_NONE);
  971. }
  972. hbc->add_child(prev);
  973. hbc->add_child(memnew(VSeparator));
  974. for (int i = from; i < to; i++) {
  975. Button *current = memnew(Button);
  976. // Add padding to make page number buttons easier to click.
  977. current->set_text(vformat(" %d ", i + 1));
  978. current->set_theme_type_variation("PanelBackgroundButton");
  979. if (i == p_page) {
  980. current->set_disabled(true);
  981. current->set_focus_mode(Control::FOCUS_NONE);
  982. } else {
  983. current->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibrary::_search).bind(i));
  984. }
  985. hbc->add_child(current);
  986. }
  987. Button *next = memnew(Button);
  988. next->set_text(TTR("Next", "Pagination"));
  989. next->set_theme_type_variation("PanelBackgroundButton");
  990. if (p_page < p_page_count - 1) {
  991. next->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibrary::_search).bind(p_page + 1));
  992. } else {
  993. next->set_disabled(true);
  994. next->set_focus_mode(Control::FOCUS_NONE);
  995. }
  996. hbc->add_child(memnew(VSeparator));
  997. hbc->add_child(next);
  998. Button *last = memnew(Button);
  999. last->set_text(TTR("Last", "Pagination"));
  1000. last->set_theme_type_variation("PanelBackgroundButton");
  1001. if (p_page != p_page_count - 1) {
  1002. last->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibrary::_search).bind(p_page_count - 1));
  1003. } else {
  1004. last->set_disabled(true);
  1005. last->set_focus_mode(Control::FOCUS_NONE);
  1006. }
  1007. hbc->add_child(last);
  1008. hbc->add_spacer();
  1009. return hbc;
  1010. }
  1011. void EditorAssetLibrary::_api_request(const String &p_request, RequestType p_request_type, const String &p_arguments) {
  1012. if (requesting != REQUESTING_NONE) {
  1013. request->cancel_request();
  1014. }
  1015. error_hb->hide();
  1016. if (loading_blocked) {
  1017. _set_library_message_with_action(TTR("The Asset Library requires an online connection and involves sending data over the internet."), TTR("Go Online"), callable_mp(this, &EditorAssetLibrary::_force_online_mode));
  1018. return;
  1019. }
  1020. requesting = p_request_type;
  1021. request->request(host + "/" + p_request + p_arguments);
  1022. }
  1023. void EditorAssetLibrary::_http_request_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data) {
  1024. String str;
  1025. {
  1026. int datalen = p_data.size();
  1027. const uint8_t *r = p_data.ptr();
  1028. str.parse_utf8((const char *)r, datalen);
  1029. }
  1030. bool error_abort = true;
  1031. switch (p_status) {
  1032. case HTTPRequest::RESULT_CANT_RESOLVE: {
  1033. error_label->set_text(TTR("Can't resolve hostname:") + " " + host);
  1034. } break;
  1035. case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED:
  1036. case HTTPRequest::RESULT_CONNECTION_ERROR:
  1037. case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH: {
  1038. error_label->set_text(TTR("Connection error, please try again."));
  1039. } break;
  1040. case HTTPRequest::RESULT_TLS_HANDSHAKE_ERROR:
  1041. case HTTPRequest::RESULT_CANT_CONNECT: {
  1042. error_label->set_text(TTR("Can't connect to host:") + " " + host);
  1043. } break;
  1044. case HTTPRequest::RESULT_NO_RESPONSE: {
  1045. error_label->set_text(TTR("No response from host:") + " " + host);
  1046. } break;
  1047. case HTTPRequest::RESULT_REQUEST_FAILED: {
  1048. error_label->set_text(TTR("Request failed, return code:") + " " + itos(p_code));
  1049. } break;
  1050. case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: {
  1051. error_label->set_text(TTR("Request failed, too many redirects"));
  1052. } break;
  1053. default: {
  1054. if (p_code != 200) {
  1055. error_label->set_text(TTR("Request failed, return code:") + " " + itos(p_code));
  1056. } else {
  1057. error_abort = false;
  1058. }
  1059. } break;
  1060. }
  1061. if (error_abort) {
  1062. if (requesting == REQUESTING_CONFIG) {
  1063. _set_library_message_with_action(TTR("Failed to get repository configuration."), TTR("Retry"), callable_mp(this, &EditorAssetLibrary::_request_current_config));
  1064. }
  1065. error_hb->show();
  1066. return;
  1067. }
  1068. Dictionary d;
  1069. {
  1070. JSON json;
  1071. json.parse(str);
  1072. d = json.get_data();
  1073. }
  1074. RequestType requested = requesting;
  1075. requesting = REQUESTING_NONE;
  1076. switch (requested) {
  1077. case REQUESTING_CONFIG: {
  1078. categories->clear();
  1079. categories->add_item(TTR("All"));
  1080. categories->set_item_metadata(0, 0);
  1081. if (d.has("categories")) {
  1082. Array clist = d["categories"];
  1083. for (int i = 0; i < clist.size(); i++) {
  1084. Dictionary cat = clist[i];
  1085. if (!cat.has("name") || !cat.has("id")) {
  1086. continue;
  1087. }
  1088. String name = cat["name"];
  1089. int id = cat["id"];
  1090. categories->add_item(name);
  1091. categories->set_item_metadata(-1, id);
  1092. category_map[cat["id"]] = name;
  1093. }
  1094. }
  1095. filter->set_editable(true);
  1096. sort->set_disabled(false);
  1097. categories->set_disabled(false);
  1098. support->set_disabled(false);
  1099. _search();
  1100. } break;
  1101. case REQUESTING_SEARCH: {
  1102. initial_loading = false;
  1103. if (asset_items) {
  1104. memdelete(asset_items);
  1105. }
  1106. if (asset_top_page) {
  1107. memdelete(asset_top_page);
  1108. }
  1109. if (asset_bottom_page) {
  1110. memdelete(asset_bottom_page);
  1111. }
  1112. int page = 0;
  1113. int pages = 1;
  1114. int page_len = 10;
  1115. int total_items = 1;
  1116. Array result;
  1117. if (d.has("page")) {
  1118. page = d["page"];
  1119. }
  1120. if (d.has("pages")) {
  1121. pages = d["pages"];
  1122. }
  1123. if (d.has("page_length")) {
  1124. page_len = d["page_length"];
  1125. }
  1126. if (d.has("total")) {
  1127. total_items = d["total"];
  1128. }
  1129. if (d.has("result")) {
  1130. result = d["result"];
  1131. }
  1132. asset_top_page = _make_pages(page, pages, page_len, total_items, result.size());
  1133. library_vb->add_child(asset_top_page);
  1134. asset_items = memnew(GridContainer);
  1135. _update_asset_items_columns();
  1136. asset_items->add_theme_constant_override("h_separation", 10 * EDSCALE);
  1137. asset_items->add_theme_constant_override("v_separation", 10 * EDSCALE);
  1138. library_vb->add_child(asset_items);
  1139. asset_bottom_page = _make_pages(page, pages, page_len, total_items, result.size());
  1140. library_vb->add_child(asset_bottom_page);
  1141. if (result.is_empty()) {
  1142. String support_list;
  1143. for (int i = 0; i < SUPPORT_MAX; i++) {
  1144. if (support->get_popup()->is_item_checked(i)) {
  1145. if (!support_list.is_empty()) {
  1146. support_list += ", ";
  1147. }
  1148. support_list += TTRGET(support_text[i]);
  1149. }
  1150. }
  1151. if (support_list.is_empty()) {
  1152. support_list = "-";
  1153. }
  1154. if (!filter->get_text().is_empty()) {
  1155. _set_library_message(
  1156. vformat(TTR("No results for \"%s\" for support level(s): %s."), filter->get_text(), support_list));
  1157. } else {
  1158. // No results, even though the user didn't search for anything specific.
  1159. // This is typically because the version number changed recently
  1160. // and no assets compatible with the new version have been published yet.
  1161. _set_library_message(
  1162. vformat(TTR("No results compatible with %s %s for support level(s): %s.\nCheck the enabled support levels using the 'Support' button in the top-right corner."), String(VERSION_SHORT_NAME).capitalize(), String(VERSION_BRANCH), support_list));
  1163. }
  1164. } else {
  1165. library_message_box->hide();
  1166. }
  1167. for (int i = 0; i < result.size(); i++) {
  1168. Dictionary r = result[i];
  1169. ERR_CONTINUE(!r.has("title"));
  1170. ERR_CONTINUE(!r.has("asset_id"));
  1171. ERR_CONTINUE(!r.has("author"));
  1172. ERR_CONTINUE(!r.has("author_id"));
  1173. ERR_CONTINUE(!r.has("category_id"));
  1174. ERR_FAIL_COND(!category_map.has(r["category_id"]));
  1175. ERR_CONTINUE(!r.has("cost"));
  1176. EditorAssetLibraryItem *item = memnew(EditorAssetLibraryItem(true));
  1177. asset_items->add_child(item);
  1178. item->configure(r["title"], r["asset_id"], category_map[r["category_id"]], r["category_id"], r["author"], r["author_id"], r["cost"]);
  1179. item->clamp_width(asset_items_column_width);
  1180. item->connect("asset_selected", callable_mp(this, &EditorAssetLibrary::_select_asset));
  1181. item->connect("author_selected", callable_mp(this, &EditorAssetLibrary::_select_author));
  1182. item->connect("category_selected", callable_mp(this, &EditorAssetLibrary::_select_category));
  1183. if (r.has("icon_url") && !r["icon_url"].operator String().is_empty()) {
  1184. _request_image(item->get_instance_id(), r["asset_id"], r["icon_url"], IMAGE_QUEUE_ICON, 0);
  1185. }
  1186. }
  1187. if (!result.is_empty()) {
  1188. library_scroll->set_v_scroll(0);
  1189. }
  1190. } break;
  1191. case REQUESTING_ASSET: {
  1192. Dictionary r = d;
  1193. ERR_FAIL_COND(!r.has("title"));
  1194. ERR_FAIL_COND(!r.has("asset_id"));
  1195. ERR_FAIL_COND(!r.has("author"));
  1196. ERR_FAIL_COND(!r.has("author_id"));
  1197. ERR_FAIL_COND(!r.has("version"));
  1198. ERR_FAIL_COND(!r.has("version_string"));
  1199. ERR_FAIL_COND(!r.has("category_id"));
  1200. ERR_FAIL_COND(!category_map.has(r["category_id"]));
  1201. ERR_FAIL_COND(!r.has("cost"));
  1202. ERR_FAIL_COND(!r.has("description"));
  1203. ERR_FAIL_COND(!r.has("download_url"));
  1204. ERR_FAIL_COND(!r.has("download_hash"));
  1205. ERR_FAIL_COND(!r.has("browse_url"));
  1206. if (description) {
  1207. memdelete(description);
  1208. }
  1209. description = memnew(EditorAssetLibraryItemDescription);
  1210. add_child(description);
  1211. description->connect(SceneStringName(confirmed), callable_mp(this, &EditorAssetLibrary::_install_asset));
  1212. description->configure(r["title"], r["asset_id"], category_map[r["category_id"]], r["category_id"], r["author"], r["author_id"], r["cost"], r["version"], r["version_string"], r["description"], r["download_url"], r["browse_url"], r["download_hash"]);
  1213. EditorAssetLibraryItemDownload *download_item = _get_asset_in_progress(description->get_asset_id());
  1214. if (download_item) {
  1215. if (download_item->can_install()) {
  1216. description->set_ok_button_text(TTR("Install"));
  1217. description->get_ok_button()->set_disabled(false);
  1218. } else {
  1219. description->set_ok_button_text(TTR("Downloading..."));
  1220. description->get_ok_button()->set_disabled(true);
  1221. }
  1222. } else {
  1223. description->set_ok_button_text(TTR("Download"));
  1224. description->get_ok_button()->set_disabled(false);
  1225. }
  1226. if (r.has("icon_url") && !r["icon_url"].operator String().is_empty()) {
  1227. _request_image(description->get_instance_id(), r["asset_id"], r["icon_url"], IMAGE_QUEUE_ICON, 0);
  1228. }
  1229. if (d.has("previews")) {
  1230. Array previews = d["previews"];
  1231. for (int i = 0; i < previews.size(); i++) {
  1232. Dictionary p = previews[i];
  1233. ERR_CONTINUE(!p.has("type"));
  1234. ERR_CONTINUE(!p.has("link"));
  1235. bool is_video = p.has("type") && String(p["type"]) == "video";
  1236. String video_url;
  1237. if (is_video && p.has("link")) {
  1238. video_url = p["link"];
  1239. }
  1240. description->add_preview(i, is_video, video_url);
  1241. if (p.has("thumbnail")) {
  1242. _request_image(description->get_instance_id(), r["asset_id"], p["thumbnail"], IMAGE_QUEUE_THUMBNAIL, i);
  1243. }
  1244. if (!is_video) {
  1245. _request_image(description->get_instance_id(), r["asset_id"], p["link"], IMAGE_QUEUE_SCREENSHOT, i);
  1246. }
  1247. }
  1248. }
  1249. description->popup_centered();
  1250. } break;
  1251. default:
  1252. break;
  1253. }
  1254. }
  1255. void EditorAssetLibrary::_asset_file_selected(const String &p_file) {
  1256. if (asset_installer) {
  1257. memdelete(asset_installer);
  1258. asset_installer = nullptr;
  1259. }
  1260. asset_installer = memnew(EditorAssetInstaller);
  1261. asset_installer->set_asset_name(p_file);
  1262. add_child(asset_installer);
  1263. asset_installer->open_asset(p_file);
  1264. }
  1265. void EditorAssetLibrary::_asset_open() {
  1266. asset_open->popup_file_dialog();
  1267. }
  1268. void EditorAssetLibrary::_manage_plugins() {
  1269. ProjectSettingsEditor::get_singleton()->popup_project_settings(true);
  1270. ProjectSettingsEditor::get_singleton()->set_plugins_page();
  1271. }
  1272. EditorAssetLibraryItemDownload *EditorAssetLibrary::_get_asset_in_progress(int p_asset_id) const {
  1273. for (int i = 0; i < downloads_hb->get_child_count(); i++) {
  1274. EditorAssetLibraryItemDownload *d = Object::cast_to<EditorAssetLibraryItemDownload>(downloads_hb->get_child(i));
  1275. if (d && d->get_asset_id() == p_asset_id) {
  1276. return d;
  1277. }
  1278. }
  1279. return nullptr;
  1280. }
  1281. void EditorAssetLibrary::_install_external_asset(String p_zip_path, String p_title) {
  1282. emit_signal(SNAME("install_asset"), p_zip_path, p_title);
  1283. }
  1284. void EditorAssetLibrary::_update_asset_items_columns() {
  1285. int new_columns = get_size().x / (450.0 * EDSCALE);
  1286. new_columns = MAX(1, new_columns);
  1287. if (new_columns != asset_items->get_columns()) {
  1288. asset_items->set_columns(new_columns);
  1289. }
  1290. asset_items_column_width = (get_size().x / new_columns) - (120 * EDSCALE);
  1291. for (int i = 0; i < asset_items->get_child_count(); i++) {
  1292. EditorAssetLibraryItem *item = Object::cast_to<EditorAssetLibraryItem>(asset_items->get_child(i));
  1293. if (!item || !item->is_visible()) {
  1294. continue;
  1295. }
  1296. item->clamp_width(asset_items_column_width);
  1297. }
  1298. }
  1299. void EditorAssetLibrary::_set_library_message(const String &p_message) {
  1300. library_message->set_text(p_message);
  1301. if (library_message_action.is_valid()) {
  1302. library_message_button->disconnect(SceneStringName(pressed), library_message_action);
  1303. library_message_action = Callable();
  1304. }
  1305. library_message_button->hide();
  1306. library_message_box->show();
  1307. }
  1308. void EditorAssetLibrary::_set_library_message_with_action(const String &p_message, const String &p_action_text, const Callable &p_action) {
  1309. library_message->set_text(p_message);
  1310. library_message_button->set_text(p_action_text);
  1311. if (library_message_action.is_valid()) {
  1312. library_message_button->disconnect(SceneStringName(pressed), library_message_action);
  1313. library_message_action = Callable();
  1314. }
  1315. library_message_action = p_action;
  1316. library_message_button->connect(SceneStringName(pressed), library_message_action);
  1317. library_message_button->show();
  1318. library_message_box->show();
  1319. }
  1320. void EditorAssetLibrary::_force_online_mode() {
  1321. EditorSettings::get_singleton()->set_setting("network/connection/network_mode", EditorSettings::NETWORK_ONLINE);
  1322. EditorSettings::get_singleton()->notify_changes();
  1323. EditorSettings::get_singleton()->save();
  1324. }
  1325. void EditorAssetLibrary::disable_community_support() {
  1326. support->get_popup()->set_item_checked(SUPPORT_COMMUNITY, false);
  1327. }
  1328. void EditorAssetLibrary::_bind_methods() {
  1329. ADD_SIGNAL(MethodInfo("install_asset", PropertyInfo(Variant::STRING, "zip_path"), PropertyInfo(Variant::STRING, "name")));
  1330. }
  1331. EditorAssetLibrary::EditorAssetLibrary(bool p_templates_only) {
  1332. requesting = REQUESTING_NONE;
  1333. templates_only = p_templates_only;
  1334. loading_blocked = ((int)EDITOR_GET("network/connection/network_mode") == EditorSettings::NETWORK_OFFLINE);
  1335. VBoxContainer *library_main = memnew(VBoxContainer);
  1336. add_child(library_main);
  1337. HBoxContainer *search_hb = memnew(HBoxContainer);
  1338. library_main->add_child(search_hb);
  1339. library_main->add_theme_constant_override("separation", 10 * EDSCALE);
  1340. filter = memnew(LineEdit);
  1341. if (templates_only) {
  1342. filter->set_placeholder(TTR("Search Templates, Projects, and Demos"));
  1343. } else {
  1344. filter->set_placeholder(TTR("Search Assets (Excluding Templates, Projects, and Demos)"));
  1345. }
  1346. filter->set_clear_button_enabled(true);
  1347. search_hb->add_child(filter);
  1348. filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1349. filter->connect(SceneStringName(text_changed), callable_mp(this, &EditorAssetLibrary::_search_text_changed));
  1350. // Perform a search automatically if the user hasn't entered any text for a certain duration.
  1351. // This way, the user doesn't need to press Enter to initiate their search.
  1352. filter_debounce_timer = memnew(Timer);
  1353. filter_debounce_timer->set_one_shot(true);
  1354. filter_debounce_timer->set_wait_time(0.25);
  1355. filter_debounce_timer->connect("timeout", callable_mp(this, &EditorAssetLibrary::_filter_debounce_timer_timeout));
  1356. search_hb->add_child(filter_debounce_timer);
  1357. if (!p_templates_only) {
  1358. search_hb->add_child(memnew(VSeparator));
  1359. }
  1360. Button *open_asset = memnew(Button);
  1361. open_asset->set_text(TTR("Import..."));
  1362. search_hb->add_child(open_asset);
  1363. open_asset->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibrary::_asset_open));
  1364. Button *plugins = memnew(Button);
  1365. plugins->set_text(TTR("Plugins..."));
  1366. search_hb->add_child(plugins);
  1367. plugins->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibrary::_manage_plugins));
  1368. if (p_templates_only) {
  1369. open_asset->hide();
  1370. plugins->hide();
  1371. }
  1372. HBoxContainer *search_hb2 = memnew(HBoxContainer);
  1373. library_main->add_child(search_hb2);
  1374. search_hb2->add_child(memnew(Label(TTR("Sort:") + " ")));
  1375. sort = memnew(OptionButton);
  1376. for (int i = 0; i < SORT_MAX; i++) {
  1377. sort->add_item(TTRGET(sort_text[i]));
  1378. }
  1379. search_hb2->add_child(sort);
  1380. sort->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1381. sort->set_clip_text(true);
  1382. sort->connect(SceneStringName(item_selected), callable_mp(this, &EditorAssetLibrary::_rerun_search));
  1383. search_hb2->add_child(memnew(VSeparator));
  1384. search_hb2->add_child(memnew(Label(TTR("Category:") + " ")));
  1385. categories = memnew(OptionButton);
  1386. categories->add_item(TTR("All"));
  1387. search_hb2->add_child(categories);
  1388. categories->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1389. categories->set_clip_text(true);
  1390. categories->connect(SceneStringName(item_selected), callable_mp(this, &EditorAssetLibrary::_rerun_search));
  1391. search_hb2->add_child(memnew(VSeparator));
  1392. search_hb2->add_child(memnew(Label(TTR("Site:") + " ")));
  1393. repository = memnew(OptionButton);
  1394. _update_repository_options();
  1395. repository->connect(SceneStringName(item_selected), callable_mp(this, &EditorAssetLibrary::_repository_changed));
  1396. search_hb2->add_child(repository);
  1397. repository->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1398. repository->set_clip_text(true);
  1399. search_hb2->add_child(memnew(VSeparator));
  1400. support = memnew(MenuButton);
  1401. search_hb2->add_child(support);
  1402. support->set_text(TTR("Support"));
  1403. support->get_popup()->set_hide_on_checkable_item_selection(false);
  1404. support->get_popup()->add_check_item(TTRGET(support_text[SUPPORT_FEATURED]), SUPPORT_FEATURED);
  1405. support->get_popup()->add_check_item(TTRGET(support_text[SUPPORT_COMMUNITY]), SUPPORT_COMMUNITY);
  1406. support->get_popup()->add_check_item(TTRGET(support_text[SUPPORT_TESTING]), SUPPORT_TESTING);
  1407. support->get_popup()->set_item_checked(SUPPORT_FEATURED, true);
  1408. support->get_popup()->set_item_checked(SUPPORT_COMMUNITY, true);
  1409. support->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditorAssetLibrary::_support_toggled));
  1410. /////////
  1411. library_scroll_bg = memnew(PanelContainer);
  1412. library_main->add_child(library_scroll_bg);
  1413. library_scroll_bg->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1414. library_scroll = memnew(ScrollContainer);
  1415. library_scroll->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  1416. library_scroll_bg->add_child(library_scroll);
  1417. Ref<StyleBoxEmpty> border2;
  1418. border2.instantiate();
  1419. border2->set_content_margin_individual(15 * EDSCALE, 15 * EDSCALE, 35 * EDSCALE, 15 * EDSCALE);
  1420. PanelContainer *library_vb_border = memnew(PanelContainer);
  1421. library_scroll->add_child(library_vb_border);
  1422. library_vb_border->add_theme_style_override(SceneStringName(panel), border2);
  1423. library_vb_border->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1424. library_vb = memnew(VBoxContainer);
  1425. library_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1426. library_vb_border->add_child(library_vb);
  1427. library_message_box = memnew(VBoxContainer);
  1428. library_message_box->hide();
  1429. library_vb->add_child(library_message_box);
  1430. library_message = memnew(Label);
  1431. library_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1432. library_message_box->add_child(library_message);
  1433. library_message_button = memnew(Button);
  1434. library_message_button->set_h_size_flags(SIZE_SHRINK_CENTER);
  1435. library_message_button->set_theme_type_variation("PanelBackgroundButton");
  1436. library_message_box->add_child(library_message_button);
  1437. asset_top_page = memnew(HBoxContainer);
  1438. library_vb->add_child(asset_top_page);
  1439. asset_items = memnew(GridContainer);
  1440. _update_asset_items_columns();
  1441. asset_items->add_theme_constant_override("h_separation", 10 * EDSCALE);
  1442. asset_items->add_theme_constant_override("v_separation", 10 * EDSCALE);
  1443. library_vb->add_child(asset_items);
  1444. asset_bottom_page = memnew(HBoxContainer);
  1445. library_vb->add_child(asset_bottom_page);
  1446. request = memnew(HTTPRequest);
  1447. add_child(request);
  1448. setup_http_request(request);
  1449. request->connect("request_completed", callable_mp(this, &EditorAssetLibrary::_http_request_completed));
  1450. last_queue_id = 0;
  1451. library_vb->add_theme_constant_override("separation", 20 * EDSCALE);
  1452. error_hb = memnew(HBoxContainer);
  1453. library_main->add_child(error_hb);
  1454. error_label = memnew(Label);
  1455. error_hb->add_child(error_label);
  1456. error_tr = memnew(TextureRect);
  1457. error_tr->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  1458. error_hb->add_child(error_tr);
  1459. description = nullptr;
  1460. set_process(true);
  1461. set_process_shortcut_input(true); // Global shortcuts since there is no main element to be focused.
  1462. downloads_scroll = memnew(ScrollContainer);
  1463. downloads_scroll->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  1464. library_main->add_child(downloads_scroll);
  1465. downloads_hb = memnew(HBoxContainer);
  1466. downloads_scroll->add_child(downloads_hb);
  1467. asset_open = memnew(EditorFileDialog);
  1468. asset_open->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  1469. asset_open->add_filter("*.zip", TTR("Assets ZIP File"));
  1470. asset_open->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  1471. add_child(asset_open);
  1472. asset_open->connect("file_selected", callable_mp(this, &EditorAssetLibrary::_asset_file_selected));
  1473. asset_installer = nullptr;
  1474. }
  1475. ///////
  1476. bool AssetLibraryEditorPlugin::is_available() {
  1477. #ifdef WEB_ENABLED
  1478. // Asset Library can't work on Web editor for now as most assets are sourced
  1479. // directly from GitHub which does not set CORS.
  1480. return false;
  1481. #else
  1482. return StreamPeerTLS::is_available();
  1483. #endif
  1484. }
  1485. void AssetLibraryEditorPlugin::make_visible(bool p_visible) {
  1486. if (p_visible) {
  1487. addon_library->show();
  1488. } else {
  1489. addon_library->hide();
  1490. }
  1491. }
  1492. AssetLibraryEditorPlugin::AssetLibraryEditorPlugin() {
  1493. addon_library = memnew(EditorAssetLibrary);
  1494. addon_library->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1495. EditorNode::get_singleton()->get_editor_main_screen()->get_control()->add_child(addon_library);
  1496. addon_library->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  1497. addon_library->hide();
  1498. }
  1499. AssetLibraryEditorPlugin::~AssetLibraryEditorPlugin() {
  1500. }