asset_library_editor_plugin.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  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) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "asset_library_editor_plugin.h"
  31. #include "core/io/json.h"
  32. #include "core/version.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_scale.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/project_settings_editor.h"
  37. 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) {
  38. title->set_text(p_title);
  39. asset_id = p_asset_id;
  40. category->set_text(p_category);
  41. category_id = p_category_id;
  42. author->set_text(p_author);
  43. author_id = p_author_id;
  44. price->set_text(p_cost);
  45. }
  46. void EditorAssetLibraryItem::set_image(int p_type, int p_index, const Ref<Texture> &p_image) {
  47. ERR_FAIL_COND(p_type != EditorAssetLibrary::IMAGE_QUEUE_ICON);
  48. ERR_FAIL_COND(p_index != 0);
  49. icon->set_normal_texture(p_image);
  50. }
  51. void EditorAssetLibraryItem::_notification(int p_what) {
  52. if (p_what == NOTIFICATION_ENTER_TREE) {
  53. icon->set_normal_texture(get_icon("ProjectIconLoading", "EditorIcons"));
  54. category->add_color_override("font_color", Color(0.5, 0.5, 0.5));
  55. author->add_color_override("font_color", Color(0.5, 0.5, 0.5));
  56. price->add_color_override("font_color", Color(0.5, 0.5, 0.5));
  57. }
  58. }
  59. void EditorAssetLibraryItem::_asset_clicked() {
  60. emit_signal("asset_selected", asset_id);
  61. }
  62. void EditorAssetLibraryItem::_category_clicked() {
  63. emit_signal("category_selected", category_id);
  64. }
  65. void EditorAssetLibraryItem::_author_clicked() {
  66. emit_signal("author_selected", author_id);
  67. }
  68. void EditorAssetLibraryItem::_bind_methods() {
  69. ClassDB::bind_method("set_image", &EditorAssetLibraryItem::set_image);
  70. ClassDB::bind_method("_asset_clicked", &EditorAssetLibraryItem::_asset_clicked);
  71. ClassDB::bind_method("_category_clicked", &EditorAssetLibraryItem::_category_clicked);
  72. ClassDB::bind_method("_author_clicked", &EditorAssetLibraryItem::_author_clicked);
  73. ADD_SIGNAL(MethodInfo("asset_selected"));
  74. ADD_SIGNAL(MethodInfo("category_selected"));
  75. ADD_SIGNAL(MethodInfo("author_selected"));
  76. }
  77. EditorAssetLibraryItem::EditorAssetLibraryItem() {
  78. Ref<StyleBoxEmpty> border;
  79. border.instance();
  80. border->set_default_margin(MARGIN_LEFT, 5 * EDSCALE);
  81. border->set_default_margin(MARGIN_RIGHT, 5 * EDSCALE);
  82. border->set_default_margin(MARGIN_BOTTOM, 5 * EDSCALE);
  83. border->set_default_margin(MARGIN_TOP, 5 * EDSCALE);
  84. add_style_override("panel", border);
  85. HBoxContainer *hb = memnew(HBoxContainer);
  86. // Add some spacing to visually separate the icon from the asset details.
  87. hb->add_constant_override("separation", 15 * EDSCALE);
  88. add_child(hb);
  89. icon = memnew(TextureButton);
  90. icon->set_custom_minimum_size(Size2(64, 64) * EDSCALE);
  91. icon->set_default_cursor_shape(CURSOR_POINTING_HAND);
  92. icon->connect("pressed", this, "_asset_clicked");
  93. hb->add_child(icon);
  94. VBoxContainer *vb = memnew(VBoxContainer);
  95. hb->add_child(vb);
  96. vb->set_h_size_flags(SIZE_EXPAND_FILL);
  97. title = memnew(LinkButton);
  98. title->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  99. title->connect("pressed", this, "_asset_clicked");
  100. vb->add_child(title);
  101. category = memnew(LinkButton);
  102. category->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  103. category->connect("pressed", this, "_category_clicked");
  104. vb->add_child(category);
  105. author = memnew(LinkButton);
  106. author->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  107. author->connect("pressed", this, "_author_clicked");
  108. vb->add_child(author);
  109. price = memnew(Label);
  110. vb->add_child(price);
  111. set_custom_minimum_size(Size2(250, 100) * EDSCALE);
  112. set_h_size_flags(SIZE_EXPAND_FILL);
  113. set_mouse_filter(MOUSE_FILTER_PASS);
  114. }
  115. //////////////////////////////////////////////////////////////////////////////
  116. void EditorAssetLibraryItemDescription::set_image(int p_type, int p_index, const Ref<Texture> &p_image) {
  117. switch (p_type) {
  118. case EditorAssetLibrary::IMAGE_QUEUE_ICON: {
  119. item->call("set_image", p_type, p_index, p_image);
  120. icon = p_image;
  121. } break;
  122. case EditorAssetLibrary::IMAGE_QUEUE_THUMBNAIL: {
  123. for (int i = 0; i < preview_images.size(); i++) {
  124. if (preview_images[i].id == p_index) {
  125. if (preview_images[i].is_video) {
  126. Ref<Image> overlay = get_icon("PlayOverlay", "EditorIcons")->get_data();
  127. Ref<Image> thumbnail = p_image->get_data();
  128. thumbnail = thumbnail->duplicate();
  129. Point2 overlay_pos = Point2((thumbnail->get_width() - overlay->get_width()) / 2, (thumbnail->get_height() - overlay->get_height()) / 2);
  130. // Overlay and thumbnail need the same format for `blend_rect` to work.
  131. thumbnail->convert(Image::FORMAT_RGBA8);
  132. thumbnail->lock();
  133. thumbnail->blend_rect(overlay, overlay->get_used_rect(), overlay_pos);
  134. thumbnail->unlock();
  135. Ref<ImageTexture> tex;
  136. tex.instance();
  137. tex->create_from_image(thumbnail);
  138. preview_images[i].button->set_icon(tex);
  139. // Make it clearer that clicking it will open an external link
  140. preview_images[i].button->set_default_cursor_shape(CURSOR_POINTING_HAND);
  141. } else {
  142. preview_images[i].button->set_icon(p_image);
  143. }
  144. break;
  145. }
  146. }
  147. } break;
  148. case EditorAssetLibrary::IMAGE_QUEUE_SCREENSHOT: {
  149. for (int i = 0; i < preview_images.size(); i++) {
  150. if (preview_images[i].id == p_index) {
  151. preview_images.write[i].image = p_image;
  152. if (preview_images[i].button->is_pressed()) {
  153. _preview_click(p_index);
  154. }
  155. break;
  156. }
  157. }
  158. } break;
  159. }
  160. }
  161. void EditorAssetLibraryItemDescription::_notification(int p_what) {
  162. switch (p_what) {
  163. case NOTIFICATION_ENTER_TREE: {
  164. previews_bg->add_style_override("panel", get_stylebox("normal", "TextEdit"));
  165. } break;
  166. }
  167. }
  168. void EditorAssetLibraryItemDescription::_bind_methods() {
  169. ClassDB::bind_method(D_METHOD("set_image"), &EditorAssetLibraryItemDescription::set_image);
  170. ClassDB::bind_method(D_METHOD("_link_click"), &EditorAssetLibraryItemDescription::_link_click);
  171. ClassDB::bind_method(D_METHOD("_preview_click"), &EditorAssetLibraryItemDescription::_preview_click);
  172. }
  173. void EditorAssetLibraryItemDescription::_link_click(const String &p_url) {
  174. ERR_FAIL_COND(!p_url.begins_with("http"));
  175. OS::get_singleton()->shell_open(p_url);
  176. }
  177. void EditorAssetLibraryItemDescription::_preview_click(int p_id) {
  178. for (int i = 0; i < preview_images.size(); i++) {
  179. if (preview_images[i].id == p_id) {
  180. preview_images[i].button->set_pressed(true);
  181. if (!preview_images[i].is_video) {
  182. if (preview_images[i].image.is_valid()) {
  183. preview->set_texture(preview_images[i].image);
  184. minimum_size_changed();
  185. }
  186. } else {
  187. _link_click(preview_images[i].video_link);
  188. }
  189. } else {
  190. preview_images[i].button->set_pressed(false);
  191. }
  192. }
  193. }
  194. 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) {
  195. asset_id = p_asset_id;
  196. title = p_title;
  197. download_url = p_download_url;
  198. sha256 = p_sha256_hash;
  199. item->configure(p_title, p_asset_id, p_category, p_category_id, p_author, p_author_id, p_cost);
  200. description->clear();
  201. description->add_text(TTR("Version:") + " " + p_version_string + "\n");
  202. description->add_text(TTR("Contents:") + " ");
  203. description->push_meta(p_browse_url);
  204. description->add_text(TTR("View Files"));
  205. description->pop();
  206. description->add_text("\n" + TTR("Description:") + "\n\n");
  207. description->append_bbcode(p_description);
  208. set_title(p_title);
  209. }
  210. void EditorAssetLibraryItemDescription::add_preview(int p_id, bool p_video, const String &p_url) {
  211. Preview preview;
  212. preview.id = p_id;
  213. preview.video_link = p_url;
  214. preview.is_video = p_video;
  215. preview.button = memnew(Button);
  216. preview.button->set_flat(true);
  217. preview.button->set_icon(get_icon("ThumbnailWait", "EditorIcons"));
  218. preview.button->set_toggle_mode(true);
  219. preview.button->connect("pressed", this, "_preview_click", varray(p_id));
  220. preview_hb->add_child(preview.button);
  221. if (!p_video) {
  222. preview.image = get_icon("ThumbnailWait", "EditorIcons");
  223. }
  224. preview_images.push_back(preview);
  225. if (preview_images.size() == 1 && !p_video) {
  226. _preview_click(p_id);
  227. }
  228. }
  229. EditorAssetLibraryItemDescription::EditorAssetLibraryItemDescription() {
  230. HBoxContainer *hbox = memnew(HBoxContainer);
  231. add_child(hbox);
  232. VBoxContainer *desc_vbox = memnew(VBoxContainer);
  233. hbox->add_child(desc_vbox);
  234. hbox->add_constant_override("separation", 15 * EDSCALE);
  235. item = memnew(EditorAssetLibraryItem);
  236. desc_vbox->add_child(item);
  237. desc_vbox->set_custom_minimum_size(Size2(440 * EDSCALE, 0));
  238. description = memnew(RichTextLabel);
  239. desc_vbox->add_child(description);
  240. description->set_v_size_flags(SIZE_EXPAND_FILL);
  241. description->connect("meta_clicked", this, "_link_click");
  242. description->add_constant_override("line_separation", Math::round(5 * EDSCALE));
  243. VBoxContainer *previews_vbox = memnew(VBoxContainer);
  244. hbox->add_child(previews_vbox);
  245. previews_vbox->add_constant_override("separation", 15 * EDSCALE);
  246. previews_vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  247. preview = memnew(TextureRect);
  248. previews_vbox->add_child(preview);
  249. preview->set_expand(true);
  250. preview->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  251. preview->set_custom_minimum_size(Size2(640 * EDSCALE, 345 * EDSCALE));
  252. previews_bg = memnew(PanelContainer);
  253. previews_vbox->add_child(previews_bg);
  254. previews_bg->set_custom_minimum_size(Size2(640 * EDSCALE, 101 * EDSCALE));
  255. previews = memnew(ScrollContainer);
  256. previews_bg->add_child(previews);
  257. previews->set_enable_v_scroll(false);
  258. previews->set_enable_h_scroll(true);
  259. preview_hb = memnew(HBoxContainer);
  260. preview_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  261. previews->add_child(preview_hb);
  262. get_ok()->set_text(TTR("Download"));
  263. get_cancel()->set_text(TTR("Close"));
  264. }
  265. ///////////////////////////////////////////////////////////////////////////////////
  266. void EditorAssetLibraryItemDownload::_http_download_completed(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data) {
  267. String error_text;
  268. switch (p_status) {
  269. case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH:
  270. case HTTPRequest::RESULT_CONNECTION_ERROR:
  271. case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED: {
  272. error_text = TTR("Connection error, please try again.");
  273. status->set_text(TTR("Can't connect."));
  274. } break;
  275. case HTTPRequest::RESULT_CANT_CONNECT:
  276. case HTTPRequest::RESULT_SSL_HANDSHAKE_ERROR: {
  277. error_text = TTR("Can't connect to host:") + " " + host;
  278. status->set_text(TTR("Can't connect."));
  279. } break;
  280. case HTTPRequest::RESULT_NO_RESPONSE: {
  281. error_text = TTR("No response from host:") + " " + host;
  282. status->set_text(TTR("No response."));
  283. } break;
  284. case HTTPRequest::RESULT_CANT_RESOLVE: {
  285. error_text = TTR("Can't resolve hostname:") + " " + host;
  286. status->set_text(TTR("Can't resolve."));
  287. } break;
  288. case HTTPRequest::RESULT_REQUEST_FAILED: {
  289. error_text = TTR("Request failed, return code:") + " " + itos(p_code);
  290. status->set_text(TTR("Request failed."));
  291. } break;
  292. case HTTPRequest::RESULT_DOWNLOAD_FILE_CANT_OPEN:
  293. case HTTPRequest::RESULT_DOWNLOAD_FILE_WRITE_ERROR: {
  294. error_text = TTR("Cannot save response to:") + " " + download->get_download_file();
  295. status->set_text(TTR("Write error."));
  296. } break;
  297. case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: {
  298. error_text = TTR("Request failed, too many redirects");
  299. status->set_text(TTR("Redirect loop."));
  300. } break;
  301. case HTTPRequest::RESULT_TIMEOUT: {
  302. error_text = TTR("Request failed, timeout");
  303. status->set_text(TTR("Timeout."));
  304. } break;
  305. default: {
  306. if (p_code != 200) {
  307. error_text = TTR("Request failed, return code:") + " " + itos(p_code);
  308. status->set_text(TTR("Failed:") + " " + itos(p_code));
  309. } else if (sha256 != "") {
  310. String download_sha256 = FileAccess::get_sha256(download->get_download_file());
  311. if (sha256 != download_sha256) {
  312. error_text = TTR("Bad download hash, assuming file has been tampered with.") + "\n";
  313. error_text += TTR("Expected:") + " " + sha256 + "\n" + TTR("Got:") + " " + download_sha256;
  314. status->set_text(TTR("Failed sha256 hash check"));
  315. }
  316. }
  317. } break;
  318. }
  319. if (error_text != String()) {
  320. download_error->set_text(TTR("Asset Download Error:") + "\n" + error_text);
  321. download_error->popup_centered_minsize();
  322. return;
  323. }
  324. install->set_disabled(false);
  325. status->set_text(TTR("Success!"));
  326. // Make the progress bar invisible but don't reflow other Controls around it.
  327. progress->set_modulate(Color(0, 0, 0, 0));
  328. set_process(false);
  329. }
  330. void EditorAssetLibraryItemDownload::configure(const String &p_title, int p_asset_id, const Ref<Texture> &p_preview, const String &p_download_url, const String &p_sha256_hash) {
  331. title->set_text(p_title);
  332. icon->set_texture(p_preview);
  333. asset_id = p_asset_id;
  334. if (!p_preview.is_valid())
  335. icon->set_texture(get_icon("FileBrokenBigThumb", "EditorIcons"));
  336. host = p_download_url;
  337. sha256 = p_sha256_hash;
  338. _make_request();
  339. }
  340. void EditorAssetLibraryItemDownload::_notification(int p_what) {
  341. switch (p_what) {
  342. // FIXME: The editor crashes if 'NOTICATION_THEME_CHANGED' is used.
  343. case NOTIFICATION_ENTER_TREE: {
  344. add_style_override("panel", get_stylebox("panel", "TabContainer"));
  345. dismiss->set_normal_texture(get_icon("Close", "EditorIcons"));
  346. } break;
  347. case NOTIFICATION_PROCESS: {
  348. // Make the progress bar visible again when retrying the download.
  349. progress->set_modulate(Color(1, 1, 1, 1));
  350. if (download->get_downloaded_bytes() > 0) {
  351. progress->set_max(download->get_body_size());
  352. progress->set_value(download->get_downloaded_bytes());
  353. }
  354. int cstatus = download->get_http_client_status();
  355. if (cstatus == HTTPClient::STATUS_BODY) {
  356. if (download->get_body_size() > 0) {
  357. status->set_text(vformat(
  358. TTR("Downloading (%s / %s)..."),
  359. String::humanize_size(download->get_downloaded_bytes()),
  360. String::humanize_size(download->get_body_size())));
  361. } else {
  362. // Total file size is unknown, so it cannot be displayed.
  363. progress->set_modulate(Color(0, 0, 0, 0));
  364. status->set_text(vformat(
  365. TTR("Downloading...") + " (%s)",
  366. String::humanize_size(download->get_downloaded_bytes())));
  367. }
  368. }
  369. if (cstatus != prev_status) {
  370. switch (cstatus) {
  371. case HTTPClient::STATUS_RESOLVING: {
  372. status->set_text(TTR("Resolving..."));
  373. progress->set_max(1);
  374. progress->set_value(0);
  375. } break;
  376. case HTTPClient::STATUS_CONNECTING: {
  377. status->set_text(TTR("Connecting..."));
  378. progress->set_max(1);
  379. progress->set_value(0);
  380. } break;
  381. case HTTPClient::STATUS_REQUESTING: {
  382. status->set_text(TTR("Requesting..."));
  383. progress->set_max(1);
  384. progress->set_value(0);
  385. } break;
  386. default: {
  387. }
  388. }
  389. prev_status = cstatus;
  390. }
  391. } break;
  392. }
  393. }
  394. void EditorAssetLibraryItemDownload::_close() {
  395. // Clean up downloaded file.
  396. DirAccess::remove_file_or_error(download->get_download_file());
  397. queue_delete();
  398. }
  399. void EditorAssetLibraryItemDownload::_install() {
  400. String file = download->get_download_file();
  401. if (external_install) {
  402. emit_signal("install_asset", file, title->get_text());
  403. return;
  404. }
  405. asset_installer->open(file, 1);
  406. }
  407. void EditorAssetLibraryItemDownload::_make_request() {
  408. download->cancel_request();
  409. download->set_download_file(EditorSettings::get_singleton()->get_cache_dir().plus_file("tmp_asset_" + itos(asset_id)) + ".zip");
  410. Error err = download->request(host);
  411. if (err != OK) {
  412. status->set_text(TTR("Error making request"));
  413. } else {
  414. set_process(true);
  415. }
  416. }
  417. void EditorAssetLibraryItemDownload::_bind_methods() {
  418. ClassDB::bind_method("_http_download_completed", &EditorAssetLibraryItemDownload::_http_download_completed);
  419. ClassDB::bind_method("_install", &EditorAssetLibraryItemDownload::_install);
  420. ClassDB::bind_method("_close", &EditorAssetLibraryItemDownload::_close);
  421. ClassDB::bind_method("_make_request", &EditorAssetLibraryItemDownload::_make_request);
  422. ADD_SIGNAL(MethodInfo("install_asset", PropertyInfo(Variant::STRING, "zip_path"), PropertyInfo(Variant::STRING, "name")));
  423. }
  424. EditorAssetLibraryItemDownload::EditorAssetLibraryItemDownload() {
  425. HBoxContainer *hb = memnew(HBoxContainer);
  426. add_child(hb);
  427. icon = memnew(TextureRect);
  428. hb->add_child(icon);
  429. VBoxContainer *vb = memnew(VBoxContainer);
  430. hb->add_child(vb);
  431. vb->set_h_size_flags(SIZE_EXPAND_FILL);
  432. HBoxContainer *title_hb = memnew(HBoxContainer);
  433. vb->add_child(title_hb);
  434. title = memnew(Label);
  435. title_hb->add_child(title);
  436. title->set_h_size_flags(SIZE_EXPAND_FILL);
  437. dismiss = memnew(TextureButton);
  438. dismiss->connect("pressed", this, "_close");
  439. title_hb->add_child(dismiss);
  440. title->set_clip_text(true);
  441. vb->add_spacer();
  442. status = memnew(Label(TTR("Idle")));
  443. vb->add_child(status);
  444. status->add_color_override("font_color", Color(0.5, 0.5, 0.5));
  445. progress = memnew(ProgressBar);
  446. vb->add_child(progress);
  447. HBoxContainer *hb2 = memnew(HBoxContainer);
  448. vb->add_child(hb2);
  449. hb2->add_spacer();
  450. install = memnew(Button);
  451. install->set_text(TTR("Install..."));
  452. install->set_disabled(true);
  453. install->connect("pressed", this, "_install");
  454. retry = memnew(Button);
  455. retry->set_text(TTR("Retry"));
  456. retry->connect("pressed", this, "_make_request");
  457. hb2->add_child(retry);
  458. hb2->add_child(install);
  459. set_custom_minimum_size(Size2(310, 0) * EDSCALE);
  460. download = memnew(HTTPRequest);
  461. add_child(download);
  462. download->connect("request_completed", this, "_http_download_completed");
  463. download->set_use_threads(EDITOR_DEF("asset_library/use_threads", true));
  464. download_error = memnew(AcceptDialog);
  465. add_child(download_error);
  466. download_error->set_title(TTR("Download Error"));
  467. asset_installer = memnew(EditorAssetInstaller);
  468. add_child(asset_installer);
  469. asset_installer->connect("confirmed", this, "_close");
  470. prev_status = -1;
  471. external_install = false;
  472. }
  473. ////////////////////////////////////////////////////////////////////////////////
  474. void EditorAssetLibrary::_notification(int p_what) {
  475. switch (p_what) {
  476. case NOTIFICATION_READY: {
  477. error_tr->set_texture(get_icon("Error", "EditorIcons"));
  478. filter->set_right_icon(get_icon("Search", "EditorIcons"));
  479. filter->set_clear_button_enabled(true);
  480. error_label->raise();
  481. } break;
  482. case NOTIFICATION_VISIBILITY_CHANGED: {
  483. if (is_visible() && initial_loading) {
  484. _repository_changed(0); // Update when shown for the first time.
  485. }
  486. } break;
  487. case NOTIFICATION_PROCESS: {
  488. HTTPClient::Status s = request->get_http_client_status();
  489. const bool loading = s != HTTPClient::STATUS_DISCONNECTED;
  490. if (loading) {
  491. library_scroll->set_modulate(Color(1, 1, 1, 0.5));
  492. } else {
  493. library_scroll->set_modulate(Color(1, 1, 1, 1));
  494. }
  495. const bool no_downloads = downloads_hb->get_child_count() == 0;
  496. if (no_downloads == downloads_scroll->is_visible()) {
  497. downloads_scroll->set_visible(!no_downloads);
  498. }
  499. } break;
  500. case NOTIFICATION_THEME_CHANGED: {
  501. library_scroll_bg->add_style_override("panel", get_stylebox("bg", "Tree"));
  502. downloads_scroll->add_style_override("bg", get_stylebox("bg", "Tree"));
  503. error_tr->set_texture(get_icon("Error", "EditorIcons"));
  504. filter->set_right_icon(get_icon("Search", "EditorIcons"));
  505. filter->set_clear_button_enabled(true);
  506. } break;
  507. }
  508. }
  509. void EditorAssetLibrary::_install_asset() {
  510. ERR_FAIL_COND(!description);
  511. for (int i = 0; i < downloads_hb->get_child_count(); i++) {
  512. EditorAssetLibraryItemDownload *d = Object::cast_to<EditorAssetLibraryItemDownload>(downloads_hb->get_child(i));
  513. if (d && d->get_asset_id() == description->get_asset_id()) {
  514. if (EditorNode::get_singleton() != NULL)
  515. EditorNode::get_singleton()->show_warning(TTR("Download for this asset is already in progress!"));
  516. return;
  517. }
  518. }
  519. EditorAssetLibraryItemDownload *download = memnew(EditorAssetLibraryItemDownload);
  520. downloads_hb->add_child(download);
  521. download->configure(description->get_title(), description->get_asset_id(), description->get_preview_icon(), description->get_download_url(), description->get_sha256());
  522. if (templates_only) {
  523. download->set_external_install(true);
  524. download->connect("install_asset", this, "_install_external_asset");
  525. }
  526. }
  527. const char *EditorAssetLibrary::sort_key[SORT_MAX] = {
  528. "updated",
  529. "updated",
  530. "name",
  531. "name",
  532. "cost",
  533. "cost",
  534. };
  535. const char *EditorAssetLibrary::sort_text[SORT_MAX] = {
  536. TTRC("Recently Updated"),
  537. TTRC("Least Recently Updated"),
  538. TTRC("Name (A-Z)"),
  539. TTRC("Name (Z-A)"),
  540. TTRC("License (A-Z)"), // "cost" stores the SPDX license name in the Godot Asset Library.
  541. TTRC("License (Z-A)"), // "cost" stores the SPDX license name in the Godot Asset Library.
  542. };
  543. const char *EditorAssetLibrary::support_key[SUPPORT_MAX] = {
  544. "official",
  545. "community",
  546. "testing",
  547. };
  548. void EditorAssetLibrary::_select_author(int p_id) {
  549. // Open author window.
  550. }
  551. void EditorAssetLibrary::_select_category(int p_id) {
  552. for (int i = 0; i < categories->get_item_count(); i++) {
  553. if (i == 0)
  554. continue;
  555. int id = categories->get_item_metadata(i);
  556. if (id == p_id) {
  557. categories->select(i);
  558. _search();
  559. break;
  560. }
  561. }
  562. }
  563. void EditorAssetLibrary::_select_asset(int p_id) {
  564. _api_request("asset/" + itos(p_id), REQUESTING_ASSET);
  565. }
  566. void EditorAssetLibrary::_image_update(bool use_cache, bool final, const PoolByteArray &p_data, int p_queue_id) {
  567. Object *obj = ObjectDB::get_instance(image_queue[p_queue_id].target);
  568. if (obj) {
  569. bool image_set = false;
  570. PoolByteArray image_data = p_data;
  571. if (use_cache) {
  572. String cache_filename_base = EditorSettings::get_singleton()->get_cache_dir().plus_file("assetimage_" + image_queue[p_queue_id].image_url.md5_text());
  573. FileAccess *file = FileAccess::open(cache_filename_base + ".data", FileAccess::READ);
  574. if (file) {
  575. PoolByteArray cached_data;
  576. int len = file->get_32();
  577. cached_data.resize(len);
  578. PoolByteArray::Write w = cached_data.write();
  579. file->get_buffer(w.ptr(), len);
  580. image_data = cached_data;
  581. file->close();
  582. memdelete(file);
  583. }
  584. }
  585. int len = image_data.size();
  586. PoolByteArray::Read r = image_data.read();
  587. Ref<Image> image = Ref<Image>(memnew(Image));
  588. uint8_t png_signature[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
  589. uint8_t jpg_signature[3] = { 255, 216, 255 };
  590. if (r.ptr()) {
  591. if ((memcmp(&r[0], &png_signature[0], 8) == 0) && Image::_png_mem_loader_func) {
  592. image->copy_internals_from(Image::_png_mem_loader_func(r.ptr(), len));
  593. } else if ((memcmp(&r[0], &jpg_signature[0], 3) == 0) && Image::_jpg_mem_loader_func) {
  594. image->copy_internals_from(Image::_jpg_mem_loader_func(r.ptr(), len));
  595. }
  596. }
  597. if (!image->empty()) {
  598. switch (image_queue[p_queue_id].image_type) {
  599. case IMAGE_QUEUE_ICON:
  600. image->resize(64 * EDSCALE, 64 * EDSCALE, Image::INTERPOLATE_LANCZOS);
  601. break;
  602. case IMAGE_QUEUE_THUMBNAIL: {
  603. float max_height = 85 * EDSCALE;
  604. float scale_ratio = max_height / (image->get_height() * EDSCALE);
  605. if (scale_ratio < 1) {
  606. image->resize(image->get_width() * EDSCALE * scale_ratio, image->get_height() * EDSCALE * scale_ratio, Image::INTERPOLATE_LANCZOS);
  607. }
  608. } break;
  609. case IMAGE_QUEUE_SCREENSHOT: {
  610. float max_height = 397 * EDSCALE;
  611. float scale_ratio = max_height / (image->get_height() * EDSCALE);
  612. if (scale_ratio < 1) {
  613. image->resize(image->get_width() * EDSCALE * scale_ratio, image->get_height() * EDSCALE * scale_ratio, Image::INTERPOLATE_LANCZOS);
  614. }
  615. } break;
  616. }
  617. Ref<ImageTexture> tex;
  618. tex.instance();
  619. tex->create_from_image(image);
  620. obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, tex);
  621. image_set = true;
  622. }
  623. if (!image_set && final) {
  624. obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, get_icon("FileBrokenBigThumb", "EditorIcons"));
  625. }
  626. }
  627. }
  628. void EditorAssetLibrary::_image_request_completed(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data, int p_queue_id) {
  629. ERR_FAIL_COND(!image_queue.has(p_queue_id));
  630. if (p_status == HTTPRequest::RESULT_SUCCESS && p_code < HTTPClient::RESPONSE_BAD_REQUEST) {
  631. if (p_code != HTTPClient::RESPONSE_NOT_MODIFIED) {
  632. for (int i = 0; i < headers.size(); i++) {
  633. if (headers[i].findn("ETag:") == 0) { // Save etag
  634. String cache_filename_base = EditorSettings::get_singleton()->get_cache_dir().plus_file("assetimage_" + image_queue[p_queue_id].image_url.md5_text());
  635. String new_etag = headers[i].substr(headers[i].find(":") + 1, headers[i].length()).strip_edges();
  636. FileAccess *file;
  637. file = FileAccess::open(cache_filename_base + ".etag", FileAccess::WRITE);
  638. if (file) {
  639. file->store_line(new_etag);
  640. file->close();
  641. memdelete(file);
  642. }
  643. int len = p_data.size();
  644. PoolByteArray::Read r = p_data.read();
  645. file = FileAccess::open(cache_filename_base + ".data", FileAccess::WRITE);
  646. if (file) {
  647. file->store_32(len);
  648. file->store_buffer(r.ptr(), len);
  649. file->close();
  650. memdelete(file);
  651. }
  652. break;
  653. }
  654. }
  655. }
  656. _image_update(p_code == HTTPClient::RESPONSE_NOT_MODIFIED, true, p_data, p_queue_id);
  657. } else {
  658. WARN_PRINTS("Error getting image file from URL: " + image_queue[p_queue_id].image_url);
  659. Object *obj = ObjectDB::get_instance(image_queue[p_queue_id].target);
  660. if (obj) {
  661. obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, get_icon("FileBrokenBigThumb", "EditorIcons"));
  662. }
  663. }
  664. image_queue[p_queue_id].request->queue_delete();
  665. image_queue.erase(p_queue_id);
  666. _update_image_queue();
  667. }
  668. void EditorAssetLibrary::_update_image_queue() {
  669. const int max_images = 6;
  670. int current_images = 0;
  671. List<int> to_delete;
  672. for (Map<int, ImageQueue>::Element *E = image_queue.front(); E; E = E->next()) {
  673. if (!E->get().active && current_images < max_images) {
  674. String cache_filename_base = EditorSettings::get_singleton()->get_cache_dir().plus_file("assetimage_" + E->get().image_url.md5_text());
  675. Vector<String> headers;
  676. if (FileAccess::exists(cache_filename_base + ".etag") && FileAccess::exists(cache_filename_base + ".data")) {
  677. FileAccess *file = FileAccess::open(cache_filename_base + ".etag", FileAccess::READ);
  678. if (file) {
  679. headers.push_back("If-None-Match: " + file->get_line());
  680. file->close();
  681. memdelete(file);
  682. }
  683. }
  684. Error err = E->get().request->request(E->get().image_url, headers);
  685. if (err != OK) {
  686. to_delete.push_back(E->key());
  687. } else {
  688. E->get().active = true;
  689. }
  690. current_images++;
  691. } else if (E->get().active) {
  692. current_images++;
  693. }
  694. }
  695. while (to_delete.size()) {
  696. image_queue[to_delete.front()->get()].request->queue_delete();
  697. image_queue.erase(to_delete.front()->get());
  698. to_delete.pop_front();
  699. }
  700. }
  701. void EditorAssetLibrary::_request_image(ObjectID p_for, String p_image_url, ImageType p_type, int p_image_index) {
  702. ImageQueue iq;
  703. iq.image_url = p_image_url;
  704. iq.image_index = p_image_index;
  705. iq.image_type = p_type;
  706. iq.request = memnew(HTTPRequest);
  707. iq.request->set_use_threads(EDITOR_DEF("asset_library/use_threads", true));
  708. iq.target = p_for;
  709. iq.queue_id = ++last_queue_id;
  710. iq.active = false;
  711. iq.request->connect("request_completed", this, "_image_request_completed", varray(iq.queue_id));
  712. image_queue[iq.queue_id] = iq;
  713. add_child(iq.request);
  714. _image_update(true, false, PoolByteArray(), iq.queue_id);
  715. _update_image_queue();
  716. }
  717. void EditorAssetLibrary::_repository_changed(int p_repository_id) {
  718. host = repository->get_item_metadata(p_repository_id);
  719. if (templates_only) {
  720. _api_request("configure", REQUESTING_CONFIG, "?type=project");
  721. } else {
  722. _api_request("configure", REQUESTING_CONFIG);
  723. }
  724. }
  725. void EditorAssetLibrary::_support_toggled(int p_support) {
  726. support->get_popup()->set_item_checked(p_support, !support->get_popup()->is_item_checked(p_support));
  727. _search();
  728. }
  729. void EditorAssetLibrary::_rerun_search(int p_ignore) {
  730. _search();
  731. }
  732. void EditorAssetLibrary::_search(int p_page) {
  733. String args;
  734. if (templates_only) {
  735. args += "?type=project&";
  736. } else {
  737. args += "?";
  738. }
  739. args += String() + "sort=" + sort_key[sort->get_selected()];
  740. // We use the "branch" version, i.e. major.minor, as patch releases should be compatible
  741. args += "&godot_version=" + String(VERSION_BRANCH);
  742. String support_list;
  743. for (int i = 0; i < SUPPORT_MAX; i++) {
  744. if (support->get_popup()->is_item_checked(i)) {
  745. support_list += String(support_key[i]) + "+";
  746. }
  747. }
  748. if (support_list != String()) {
  749. args += "&support=" + support_list.substr(0, support_list.length() - 1);
  750. }
  751. if (categories->get_selected() > 0) {
  752. args += "&category=" + itos(categories->get_item_metadata(categories->get_selected()));
  753. }
  754. // Sorting options with an odd index are always the reverse of the previous one
  755. if (sort->get_selected() % 2 == 1) {
  756. args += "&reverse=true";
  757. }
  758. if (filter->get_text() != String()) {
  759. args += "&filter=" + filter->get_text().http_escape();
  760. }
  761. if (p_page > 0) {
  762. args += "&page=" + itos(p_page);
  763. }
  764. _api_request("asset", REQUESTING_SEARCH, args);
  765. }
  766. void EditorAssetLibrary::_search_text_entered(const String &p_text) {
  767. _search();
  768. }
  769. HBoxContainer *EditorAssetLibrary::_make_pages(int p_page, int p_page_count, int p_page_len, int p_total_items, int p_current_items) {
  770. HBoxContainer *hbc = memnew(HBoxContainer);
  771. if (p_page_count < 2)
  772. return hbc;
  773. //do the mario
  774. int from = p_page - 5;
  775. if (from < 0)
  776. from = 0;
  777. int to = from + 10;
  778. if (to > p_page_count)
  779. to = p_page_count;
  780. hbc->add_spacer();
  781. hbc->add_constant_override("separation", 5 * EDSCALE);
  782. Button *first = memnew(Button);
  783. first->set_text(TTR("First"));
  784. if (p_page != 0) {
  785. first->connect("pressed", this, "_search", varray(0));
  786. } else {
  787. first->set_disabled(true);
  788. first->set_focus_mode(Control::FOCUS_NONE);
  789. }
  790. hbc->add_child(first);
  791. Button *prev = memnew(Button);
  792. prev->set_text(TTR("Previous"));
  793. if (p_page > 0) {
  794. prev->connect("pressed", this, "_search", varray(p_page - 1));
  795. } else {
  796. prev->set_disabled(true);
  797. prev->set_focus_mode(Control::FOCUS_NONE);
  798. }
  799. hbc->add_child(prev);
  800. hbc->add_child(memnew(VSeparator));
  801. for (int i = from; i < to; i++) {
  802. if (i == p_page) {
  803. Button *current = memnew(Button);
  804. current->set_text(itos(i + 1));
  805. current->set_disabled(true);
  806. current->set_focus_mode(Control::FOCUS_NONE);
  807. hbc->add_child(current);
  808. } else {
  809. Button *current = memnew(Button);
  810. current->set_text(itos(i + 1));
  811. current->connect("pressed", this, "_search", varray(i));
  812. hbc->add_child(current);
  813. }
  814. }
  815. Button *next = memnew(Button);
  816. next->set_text(TTR("Next"));
  817. if (p_page < p_page_count - 1) {
  818. next->connect("pressed", this, "_search", varray(p_page + 1));
  819. } else {
  820. next->set_disabled(true);
  821. next->set_focus_mode(Control::FOCUS_NONE);
  822. }
  823. hbc->add_child(memnew(VSeparator));
  824. hbc->add_child(next);
  825. Button *last = memnew(Button);
  826. last->set_text(TTR("Last"));
  827. if (p_page != p_page_count - 1) {
  828. last->connect("pressed", this, "_search", varray(p_page_count - 1));
  829. } else {
  830. last->set_disabled(true);
  831. last->set_focus_mode(Control::FOCUS_NONE);
  832. }
  833. hbc->add_child(last);
  834. hbc->add_spacer();
  835. return hbc;
  836. }
  837. void EditorAssetLibrary::_api_request(const String &p_request, RequestType p_request_type, const String &p_arguments) {
  838. if (requesting != REQUESTING_NONE) {
  839. request->cancel_request();
  840. }
  841. requesting = p_request_type;
  842. error_hb->hide();
  843. request->request(host + "/" + p_request + p_arguments);
  844. }
  845. void EditorAssetLibrary::_http_request_completed(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data) {
  846. String str;
  847. {
  848. int datalen = p_data.size();
  849. PoolByteArray::Read r = p_data.read();
  850. str.parse_utf8((const char *)r.ptr(), datalen);
  851. }
  852. bool error_abort = true;
  853. switch (p_status) {
  854. case HTTPRequest::RESULT_CANT_RESOLVE: {
  855. error_label->set_text(TTR("Can't resolve hostname:") + " " + host);
  856. } break;
  857. case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED:
  858. case HTTPRequest::RESULT_CONNECTION_ERROR:
  859. case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH: {
  860. error_label->set_text(TTR("Connection error, please try again."));
  861. } break;
  862. case HTTPRequest::RESULT_SSL_HANDSHAKE_ERROR:
  863. case HTTPRequest::RESULT_CANT_CONNECT: {
  864. error_label->set_text(TTR("Can't connect to host:") + " " + host);
  865. } break;
  866. case HTTPRequest::RESULT_NO_RESPONSE: {
  867. error_label->set_text(TTR("No response from host:") + " " + host);
  868. } break;
  869. case HTTPRequest::RESULT_REQUEST_FAILED: {
  870. error_label->set_text(TTR("Request failed, return code:") + " " + itos(p_code));
  871. } break;
  872. case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: {
  873. error_label->set_text(TTR("Request failed, too many redirects"));
  874. } break;
  875. default: {
  876. if (p_code != 200) {
  877. error_label->set_text(TTR("Request failed, return code:") + " " + itos(p_code));
  878. } else {
  879. error_abort = false;
  880. }
  881. } break;
  882. }
  883. if (error_abort) {
  884. error_hb->show();
  885. return;
  886. }
  887. Dictionary d;
  888. {
  889. Variant js;
  890. String errs;
  891. int errl;
  892. JSON::parse(str, js, errs, errl);
  893. d = js;
  894. }
  895. RequestType requested = requesting;
  896. requesting = REQUESTING_NONE;
  897. switch (requested) {
  898. case REQUESTING_CONFIG: {
  899. categories->clear();
  900. categories->add_item(TTR("All"));
  901. categories->set_item_metadata(0, 0);
  902. if (d.has("categories")) {
  903. Array clist = d["categories"];
  904. for (int i = 0; i < clist.size(); i++) {
  905. Dictionary cat = clist[i];
  906. if (!cat.has("name") || !cat.has("id"))
  907. continue;
  908. String name = cat["name"];
  909. int id = cat["id"];
  910. categories->add_item(name);
  911. categories->set_item_metadata(categories->get_item_count() - 1, id);
  912. category_map[cat["id"]] = name;
  913. }
  914. }
  915. _search();
  916. } break;
  917. case REQUESTING_SEARCH: {
  918. initial_loading = false;
  919. // The loading text only needs to be displayed before the first page is loaded.
  920. // Therefore, we don't need to show it again.
  921. library_loading->hide();
  922. library_error->hide();
  923. if (asset_items) {
  924. memdelete(asset_items);
  925. }
  926. if (asset_top_page) {
  927. memdelete(asset_top_page);
  928. }
  929. if (asset_bottom_page) {
  930. memdelete(asset_bottom_page);
  931. }
  932. int page = 0;
  933. int pages = 1;
  934. int page_len = 10;
  935. int total_items = 1;
  936. Array result;
  937. if (d.has("page")) {
  938. page = d["page"];
  939. }
  940. if (d.has("pages")) {
  941. pages = d["pages"];
  942. }
  943. if (d.has("page_length")) {
  944. page_len = d["page_length"];
  945. }
  946. if (d.has("total")) {
  947. total_items = d["total"];
  948. }
  949. if (d.has("result")) {
  950. result = d["result"];
  951. }
  952. asset_top_page = _make_pages(page, pages, page_len, total_items, result.size());
  953. library_vb->add_child(asset_top_page);
  954. asset_items = memnew(GridContainer);
  955. asset_items->set_columns(2);
  956. asset_items->add_constant_override("hseparation", 10 * EDSCALE);
  957. asset_items->add_constant_override("vseparation", 10 * EDSCALE);
  958. library_vb->add_child(asset_items);
  959. asset_bottom_page = _make_pages(page, pages, page_len, total_items, result.size());
  960. library_vb->add_child(asset_bottom_page);
  961. if (result.empty()) {
  962. library_error->set_text(vformat(TTR("No results for \"%s\"."), filter->get_text()));
  963. library_error->show();
  964. }
  965. for (int i = 0; i < result.size(); i++) {
  966. Dictionary r = result[i];
  967. ERR_CONTINUE(!r.has("title"));
  968. ERR_CONTINUE(!r.has("asset_id"));
  969. ERR_CONTINUE(!r.has("author"));
  970. ERR_CONTINUE(!r.has("author_id"));
  971. ERR_CONTINUE(!r.has("category_id"));
  972. ERR_FAIL_COND(!category_map.has(r["category_id"]));
  973. ERR_CONTINUE(!r.has("cost"));
  974. EditorAssetLibraryItem *item = memnew(EditorAssetLibraryItem);
  975. asset_items->add_child(item);
  976. item->configure(r["title"], r["asset_id"], category_map[r["category_id"]], r["category_id"], r["author"], r["author_id"], r["cost"]);
  977. item->connect("asset_selected", this, "_select_asset");
  978. item->connect("author_selected", this, "_select_author");
  979. item->connect("category_selected", this, "_select_category");
  980. if (r.has("icon_url") && r["icon_url"] != "") {
  981. _request_image(item->get_instance_id(), r["icon_url"], IMAGE_QUEUE_ICON, 0);
  982. }
  983. }
  984. } break;
  985. case REQUESTING_ASSET: {
  986. Dictionary r = d;
  987. ERR_FAIL_COND(!r.has("title"));
  988. ERR_FAIL_COND(!r.has("asset_id"));
  989. ERR_FAIL_COND(!r.has("author"));
  990. ERR_FAIL_COND(!r.has("author_id"));
  991. ERR_FAIL_COND(!r.has("version"));
  992. ERR_FAIL_COND(!r.has("version_string"));
  993. ERR_FAIL_COND(!r.has("category_id"));
  994. ERR_FAIL_COND(!category_map.has(r["category_id"]));
  995. ERR_FAIL_COND(!r.has("cost"));
  996. ERR_FAIL_COND(!r.has("description"));
  997. ERR_FAIL_COND(!r.has("download_url"));
  998. ERR_FAIL_COND(!r.has("download_hash"));
  999. ERR_FAIL_COND(!r.has("browse_url"));
  1000. if (description) {
  1001. memdelete(description);
  1002. }
  1003. description = memnew(EditorAssetLibraryItemDescription);
  1004. add_child(description);
  1005. description->popup_centered_minsize();
  1006. description->connect("confirmed", this, "_install_asset");
  1007. 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"]);
  1008. if (r.has("icon_url") && r["icon_url"] != "") {
  1009. _request_image(description->get_instance_id(), r["icon_url"], IMAGE_QUEUE_ICON, 0);
  1010. }
  1011. if (d.has("previews")) {
  1012. Array previews = d["previews"];
  1013. for (int i = 0; i < previews.size(); i++) {
  1014. Dictionary p = previews[i];
  1015. ERR_CONTINUE(!p.has("type"));
  1016. ERR_CONTINUE(!p.has("link"));
  1017. bool is_video = p.has("type") && String(p["type"]) == "video";
  1018. String video_url;
  1019. if (is_video && p.has("link")) {
  1020. video_url = p["link"];
  1021. }
  1022. description->add_preview(i, is_video, video_url);
  1023. if (p.has("thumbnail")) {
  1024. _request_image(description->get_instance_id(), p["thumbnail"], IMAGE_QUEUE_THUMBNAIL, i);
  1025. }
  1026. if (!is_video) {
  1027. _request_image(description->get_instance_id(), p["link"], IMAGE_QUEUE_SCREENSHOT, i);
  1028. }
  1029. }
  1030. }
  1031. } break;
  1032. default: break;
  1033. }
  1034. }
  1035. void EditorAssetLibrary::_asset_file_selected(const String &p_file) {
  1036. if (asset_installer) {
  1037. memdelete(asset_installer);
  1038. asset_installer = NULL;
  1039. }
  1040. asset_installer = memnew(EditorAssetInstaller);
  1041. add_child(asset_installer);
  1042. asset_installer->open(p_file);
  1043. }
  1044. void EditorAssetLibrary::_asset_open() {
  1045. asset_open->popup_centered_ratio();
  1046. }
  1047. void EditorAssetLibrary::_manage_plugins() {
  1048. ProjectSettingsEditor::get_singleton()->popup_project_settings();
  1049. ProjectSettingsEditor::get_singleton()->set_plugins_page();
  1050. }
  1051. void EditorAssetLibrary::_install_external_asset(String p_zip_path, String p_title) {
  1052. emit_signal("install_asset", p_zip_path, p_title);
  1053. }
  1054. void EditorAssetLibrary::disable_community_support() {
  1055. support->get_popup()->set_item_checked(SUPPORT_COMMUNITY, false);
  1056. }
  1057. void EditorAssetLibrary::_bind_methods() {
  1058. ClassDB::bind_method("_http_request_completed", &EditorAssetLibrary::_http_request_completed);
  1059. ClassDB::bind_method("_select_asset", &EditorAssetLibrary::_select_asset);
  1060. ClassDB::bind_method("_select_author", &EditorAssetLibrary::_select_author);
  1061. ClassDB::bind_method("_select_category", &EditorAssetLibrary::_select_category);
  1062. ClassDB::bind_method("_image_request_completed", &EditorAssetLibrary::_image_request_completed);
  1063. ClassDB::bind_method("_search", &EditorAssetLibrary::_search, DEFVAL(0));
  1064. ClassDB::bind_method("_search_text_entered", &EditorAssetLibrary::_search_text_entered);
  1065. ClassDB::bind_method("_install_asset", &EditorAssetLibrary::_install_asset);
  1066. ClassDB::bind_method("_manage_plugins", &EditorAssetLibrary::_manage_plugins);
  1067. ClassDB::bind_method("_asset_open", &EditorAssetLibrary::_asset_open);
  1068. ClassDB::bind_method("_asset_file_selected", &EditorAssetLibrary::_asset_file_selected);
  1069. ClassDB::bind_method("_repository_changed", &EditorAssetLibrary::_repository_changed);
  1070. ClassDB::bind_method("_support_toggled", &EditorAssetLibrary::_support_toggled);
  1071. ClassDB::bind_method("_rerun_search", &EditorAssetLibrary::_rerun_search);
  1072. ClassDB::bind_method("_install_external_asset", &EditorAssetLibrary::_install_external_asset);
  1073. ADD_SIGNAL(MethodInfo("install_asset", PropertyInfo(Variant::STRING, "zip_path"), PropertyInfo(Variant::STRING, "name")));
  1074. }
  1075. EditorAssetLibrary::EditorAssetLibrary(bool p_templates_only) {
  1076. requesting = REQUESTING_NONE;
  1077. templates_only = p_templates_only;
  1078. initial_loading = true;
  1079. VBoxContainer *library_main = memnew(VBoxContainer);
  1080. add_child(library_main);
  1081. HBoxContainer *search_hb = memnew(HBoxContainer);
  1082. library_main->add_child(search_hb);
  1083. library_main->add_constant_override("separation", 10 * EDSCALE);
  1084. filter = memnew(LineEdit);
  1085. search_hb->add_child(filter);
  1086. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  1087. filter->connect("text_entered", this, "_search_text_entered");
  1088. search = memnew(Button(TTR("Search")));
  1089. search->connect("pressed", this, "_search");
  1090. search_hb->add_child(search);
  1091. if (!p_templates_only)
  1092. search_hb->add_child(memnew(VSeparator));
  1093. Button *open_asset = memnew(Button);
  1094. open_asset->set_text(TTR("Import..."));
  1095. search_hb->add_child(open_asset);
  1096. open_asset->connect("pressed", this, "_asset_open");
  1097. Button *plugins = memnew(Button);
  1098. plugins->set_text(TTR("Plugins..."));
  1099. search_hb->add_child(plugins);
  1100. plugins->connect("pressed", this, "_manage_plugins");
  1101. if (p_templates_only) {
  1102. open_asset->hide();
  1103. plugins->hide();
  1104. }
  1105. HBoxContainer *search_hb2 = memnew(HBoxContainer);
  1106. library_main->add_child(search_hb2);
  1107. search_hb2->add_child(memnew(Label(TTR("Sort:") + " ")));
  1108. sort = memnew(OptionButton);
  1109. for (int i = 0; i < SORT_MAX; i++) {
  1110. sort->add_item(TTRGET(sort_text[i]));
  1111. }
  1112. search_hb2->add_child(sort);
  1113. sort->set_h_size_flags(SIZE_EXPAND_FILL);
  1114. sort->connect("item_selected", this, "_rerun_search");
  1115. search_hb2->add_child(memnew(VSeparator));
  1116. search_hb2->add_child(memnew(Label(TTR("Category:") + " ")));
  1117. categories = memnew(OptionButton);
  1118. categories->add_item(TTR("All"));
  1119. search_hb2->add_child(categories);
  1120. categories->set_h_size_flags(SIZE_EXPAND_FILL);
  1121. categories->connect("item_selected", this, "_rerun_search");
  1122. search_hb2->add_child(memnew(VSeparator));
  1123. search_hb2->add_child(memnew(Label(TTR("Site:") + " ")));
  1124. repository = memnew(OptionButton);
  1125. repository->add_item("godotengine.org");
  1126. repository->set_item_metadata(0, "https://godotengine.org/asset-library/api");
  1127. repository->add_item("localhost");
  1128. repository->set_item_metadata(1, "http://127.0.0.1/asset-library/api");
  1129. repository->connect("item_selected", this, "_repository_changed");
  1130. search_hb2->add_child(repository);
  1131. repository->set_h_size_flags(SIZE_EXPAND_FILL);
  1132. search_hb2->add_child(memnew(VSeparator));
  1133. support = memnew(MenuButton);
  1134. search_hb2->add_child(support);
  1135. support->set_text(TTR("Support"));
  1136. support->get_popup()->add_check_item(TTR("Official"), SUPPORT_OFFICIAL);
  1137. support->get_popup()->add_check_item(TTR("Community"), SUPPORT_COMMUNITY);
  1138. support->get_popup()->add_check_item(TTR("Testing"), SUPPORT_TESTING);
  1139. support->get_popup()->set_item_checked(SUPPORT_OFFICIAL, true);
  1140. support->get_popup()->set_item_checked(SUPPORT_COMMUNITY, true);
  1141. support->get_popup()->connect("id_pressed", this, "_support_toggled");
  1142. /////////
  1143. library_scroll_bg = memnew(PanelContainer);
  1144. library_main->add_child(library_scroll_bg);
  1145. library_scroll_bg->set_v_size_flags(SIZE_EXPAND_FILL);
  1146. library_scroll = memnew(ScrollContainer);
  1147. library_scroll->set_enable_v_scroll(true);
  1148. library_scroll->set_enable_h_scroll(false);
  1149. library_scroll_bg->add_child(library_scroll);
  1150. Ref<StyleBoxEmpty> border2;
  1151. border2.instance();
  1152. border2->set_default_margin(MARGIN_LEFT, 15 * EDSCALE);
  1153. border2->set_default_margin(MARGIN_RIGHT, 35 * EDSCALE);
  1154. border2->set_default_margin(MARGIN_BOTTOM, 15 * EDSCALE);
  1155. border2->set_default_margin(MARGIN_TOP, 15 * EDSCALE);
  1156. PanelContainer *library_vb_border = memnew(PanelContainer);
  1157. library_scroll->add_child(library_vb_border);
  1158. library_vb_border->add_style_override("panel", border2);
  1159. library_vb_border->set_h_size_flags(SIZE_EXPAND_FILL);
  1160. library_vb_border->set_mouse_filter(MOUSE_FILTER_PASS);
  1161. library_vb = memnew(VBoxContainer);
  1162. library_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1163. library_vb_border->add_child(library_vb);
  1164. library_loading = memnew(Label(TTR("Loading...")));
  1165. library_loading->set_align(Label::ALIGN_CENTER);
  1166. library_vb->add_child(library_loading);
  1167. library_error = memnew(Label);
  1168. library_error->set_align(Label::ALIGN_CENTER);
  1169. library_error->hide();
  1170. library_vb->add_child(library_error);
  1171. asset_top_page = memnew(HBoxContainer);
  1172. library_vb->add_child(asset_top_page);
  1173. asset_items = memnew(GridContainer);
  1174. asset_items->set_columns(2);
  1175. asset_items->add_constant_override("hseparation", 10 * EDSCALE);
  1176. asset_items->add_constant_override("vseparation", 10 * EDSCALE);
  1177. library_vb->add_child(asset_items);
  1178. asset_bottom_page = memnew(HBoxContainer);
  1179. library_vb->add_child(asset_bottom_page);
  1180. request = memnew(HTTPRequest);
  1181. add_child(request);
  1182. request->set_use_threads(EDITOR_DEF("asset_library/use_threads", true));
  1183. request->connect("request_completed", this, "_http_request_completed");
  1184. last_queue_id = 0;
  1185. library_vb->add_constant_override("separation", 20 * EDSCALE);
  1186. error_hb = memnew(HBoxContainer);
  1187. library_main->add_child(error_hb);
  1188. error_label = memnew(Label);
  1189. error_label->add_color_override("color", get_color("error_color", "Editor"));
  1190. error_hb->add_child(error_label);
  1191. error_tr = memnew(TextureRect);
  1192. error_tr->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  1193. error_hb->add_child(error_tr);
  1194. description = NULL;
  1195. set_process(true);
  1196. downloads_scroll = memnew(ScrollContainer);
  1197. downloads_scroll->set_enable_h_scroll(true);
  1198. downloads_scroll->set_enable_v_scroll(false);
  1199. library_main->add_child(downloads_scroll);
  1200. downloads_hb = memnew(HBoxContainer);
  1201. downloads_scroll->add_child(downloads_hb);
  1202. asset_open = memnew(EditorFileDialog);
  1203. asset_open->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  1204. asset_open->add_filter("*.zip ; " + TTR("Assets ZIP File"));
  1205. asset_open->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  1206. add_child(asset_open);
  1207. asset_open->connect("file_selected", this, "_asset_file_selected");
  1208. asset_installer = NULL;
  1209. }
  1210. ///////
  1211. void AssetLibraryEditorPlugin::make_visible(bool p_visible) {
  1212. if (p_visible) {
  1213. addon_library->show();
  1214. } else {
  1215. addon_library->hide();
  1216. }
  1217. }
  1218. AssetLibraryEditorPlugin::AssetLibraryEditorPlugin(EditorNode *p_node) {
  1219. editor = p_node;
  1220. addon_library = memnew(EditorAssetLibrary);
  1221. addon_library->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1222. editor->get_viewport()->add_child(addon_library);
  1223. addon_library->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1224. addon_library->hide();
  1225. }
  1226. AssetLibraryEditorPlugin::~AssetLibraryEditorPlugin() {
  1227. }