asset_library_editor_plugin.cpp 60 KB

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