asset_library_editor_plugin.cpp 62 KB

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