asset_library_editor_plugin.cpp 61 KB

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