asset_library_editor_plugin.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 SHA-256 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. // Let the user retry the download.
  323. retry->show();
  324. return;
  325. }
  326. install->set_disabled(false);
  327. status->set_text(TTR("Success!"));
  328. // Make the progress bar invisible but don't reflow other Controls around it.
  329. progress->set_modulate(Color(0, 0, 0, 0));
  330. set_process(false);
  331. }
  332. 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) {
  333. title->set_text(p_title);
  334. icon->set_texture(p_preview);
  335. asset_id = p_asset_id;
  336. if (!p_preview.is_valid())
  337. icon->set_texture(get_icon("FileBrokenBigThumb", "EditorIcons"));
  338. host = p_download_url;
  339. sha256 = p_sha256_hash;
  340. _make_request();
  341. }
  342. void EditorAssetLibraryItemDownload::_notification(int p_what) {
  343. switch (p_what) {
  344. // FIXME: The editor crashes if 'NOTICATION_THEME_CHANGED' is used.
  345. case NOTIFICATION_ENTER_TREE: {
  346. add_style_override("panel", get_stylebox("panel", "TabContainer"));
  347. dismiss->set_normal_texture(get_icon("Close", "EditorIcons"));
  348. } break;
  349. case NOTIFICATION_PROCESS: {
  350. // Make the progress bar visible again when retrying the download.
  351. progress->set_modulate(Color(1, 1, 1, 1));
  352. if (download->get_downloaded_bytes() > 0) {
  353. progress->set_max(download->get_body_size());
  354. progress->set_value(download->get_downloaded_bytes());
  355. }
  356. int cstatus = download->get_http_client_status();
  357. if (cstatus == HTTPClient::STATUS_BODY) {
  358. if (download->get_body_size() > 0) {
  359. status->set_text(vformat(
  360. TTR("Downloading (%s / %s)..."),
  361. String::humanize_size(download->get_downloaded_bytes()),
  362. String::humanize_size(download->get_body_size())));
  363. } else {
  364. // Total file size is unknown, so it cannot be displayed.
  365. progress->set_modulate(Color(0, 0, 0, 0));
  366. status->set_text(vformat(
  367. TTR("Downloading...") + " (%s)",
  368. String::humanize_size(download->get_downloaded_bytes())));
  369. }
  370. }
  371. if (cstatus != prev_status) {
  372. switch (cstatus) {
  373. case HTTPClient::STATUS_RESOLVING: {
  374. status->set_text(TTR("Resolving..."));
  375. progress->set_max(1);
  376. progress->set_value(0);
  377. } break;
  378. case HTTPClient::STATUS_CONNECTING: {
  379. status->set_text(TTR("Connecting..."));
  380. progress->set_max(1);
  381. progress->set_value(0);
  382. } break;
  383. case HTTPClient::STATUS_REQUESTING: {
  384. status->set_text(TTR("Requesting..."));
  385. progress->set_max(1);
  386. progress->set_value(0);
  387. } break;
  388. default: {
  389. }
  390. }
  391. prev_status = cstatus;
  392. }
  393. } break;
  394. }
  395. }
  396. void EditorAssetLibraryItemDownload::_close() {
  397. // Clean up downloaded file.
  398. DirAccess::remove_file_or_error(download->get_download_file());
  399. queue_delete();
  400. }
  401. void EditorAssetLibraryItemDownload::_install() {
  402. String file = download->get_download_file();
  403. if (external_install) {
  404. emit_signal("install_asset", file, title->get_text());
  405. return;
  406. }
  407. asset_installer->open(file, 1);
  408. }
  409. void EditorAssetLibraryItemDownload::_make_request() {
  410. // Hide the Retry button if we've just pressed it.
  411. retry->hide();
  412. download->cancel_request();
  413. download->set_download_file(EditorSettings::get_singleton()->get_cache_dir().plus_file("tmp_asset_" + itos(asset_id)) + ".zip");
  414. Error err = download->request(host);
  415. if (err != OK) {
  416. status->set_text(TTR("Error making request"));
  417. } else {
  418. set_process(true);
  419. }
  420. }
  421. void EditorAssetLibraryItemDownload::_bind_methods() {
  422. ClassDB::bind_method("_http_download_completed", &EditorAssetLibraryItemDownload::_http_download_completed);
  423. ClassDB::bind_method("_install", &EditorAssetLibraryItemDownload::_install);
  424. ClassDB::bind_method("_close", &EditorAssetLibraryItemDownload::_close);
  425. ClassDB::bind_method("_make_request", &EditorAssetLibraryItemDownload::_make_request);
  426. ADD_SIGNAL(MethodInfo("install_asset", PropertyInfo(Variant::STRING, "zip_path"), PropertyInfo(Variant::STRING, "name")));
  427. }
  428. EditorAssetLibraryItemDownload::EditorAssetLibraryItemDownload() {
  429. HBoxContainer *hb = memnew(HBoxContainer);
  430. add_child(hb);
  431. icon = memnew(TextureRect);
  432. hb->add_child(icon);
  433. VBoxContainer *vb = memnew(VBoxContainer);
  434. hb->add_child(vb);
  435. vb->set_h_size_flags(SIZE_EXPAND_FILL);
  436. HBoxContainer *title_hb = memnew(HBoxContainer);
  437. vb->add_child(title_hb);
  438. title = memnew(Label);
  439. title_hb->add_child(title);
  440. title->set_h_size_flags(SIZE_EXPAND_FILL);
  441. dismiss = memnew(TextureButton);
  442. dismiss->connect("pressed", this, "_close");
  443. title_hb->add_child(dismiss);
  444. title->set_clip_text(true);
  445. vb->add_spacer();
  446. status = memnew(Label(TTR("Idle")));
  447. vb->add_child(status);
  448. status->add_color_override("font_color", Color(0.5, 0.5, 0.5));
  449. progress = memnew(ProgressBar);
  450. vb->add_child(progress);
  451. HBoxContainer *hb2 = memnew(HBoxContainer);
  452. vb->add_child(hb2);
  453. hb2->add_spacer();
  454. install = memnew(Button);
  455. install->set_text(TTR("Install..."));
  456. install->set_disabled(true);
  457. install->connect("pressed", this, "_install");
  458. retry = memnew(Button);
  459. retry->set_text(TTR("Retry"));
  460. retry->connect("pressed", this, "_make_request");
  461. // Only show the Retry button in case of a failure.
  462. retry->hide();
  463. hb2->add_child(retry);
  464. hb2->add_child(install);
  465. set_custom_minimum_size(Size2(310, 0) * EDSCALE);
  466. download = memnew(HTTPRequest);
  467. add_child(download);
  468. download->connect("request_completed", this, "_http_download_completed");
  469. download->set_use_threads(EDITOR_DEF("asset_library/use_threads", true));
  470. download_error = memnew(AcceptDialog);
  471. add_child(download_error);
  472. download_error->set_title(TTR("Download Error"));
  473. asset_installer = memnew(EditorAssetInstaller);
  474. add_child(asset_installer);
  475. asset_installer->connect("confirmed", this, "_close");
  476. prev_status = -1;
  477. external_install = false;
  478. }
  479. ////////////////////////////////////////////////////////////////////////////////
  480. void EditorAssetLibrary::_notification(int p_what) {
  481. switch (p_what) {
  482. case NOTIFICATION_READY: {
  483. error_tr->set_texture(get_icon("Error", "EditorIcons"));
  484. filter->set_right_icon(get_icon("Search", "EditorIcons"));
  485. filter->set_clear_button_enabled(true);
  486. error_label->raise();
  487. } break;
  488. case NOTIFICATION_VISIBILITY_CHANGED: {
  489. if (is_visible() && initial_loading) {
  490. _repository_changed(0); // Update when shown for the first time.
  491. }
  492. } break;
  493. case NOTIFICATION_PROCESS: {
  494. HTTPClient::Status s = request->get_http_client_status();
  495. const bool loading = s != HTTPClient::STATUS_DISCONNECTED;
  496. if (loading) {
  497. library_scroll->set_modulate(Color(1, 1, 1, 0.5));
  498. } else {
  499. library_scroll->set_modulate(Color(1, 1, 1, 1));
  500. }
  501. const bool no_downloads = downloads_hb->get_child_count() == 0;
  502. if (no_downloads == downloads_scroll->is_visible()) {
  503. downloads_scroll->set_visible(!no_downloads);
  504. }
  505. } break;
  506. case NOTIFICATION_THEME_CHANGED: {
  507. library_scroll_bg->add_style_override("panel", get_stylebox("bg", "Tree"));
  508. downloads_scroll->add_style_override("bg", get_stylebox("bg", "Tree"));
  509. error_tr->set_texture(get_icon("Error", "EditorIcons"));
  510. filter->set_right_icon(get_icon("Search", "EditorIcons"));
  511. filter->set_clear_button_enabled(true);
  512. } break;
  513. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  514. _update_repository_options();
  515. } break;
  516. }
  517. }
  518. void EditorAssetLibrary::_update_repository_options() {
  519. Dictionary default_urls;
  520. default_urls["godotengine.org (Official)"] = "https://godotengine.org/asset-library/api";
  521. Dictionary available_urls = _EDITOR_DEF("asset_library/available_urls", default_urls, true);
  522. repository->clear();
  523. Array keys = available_urls.keys();
  524. for (int i = 0; i < available_urls.size(); i++) {
  525. String key = keys[i];
  526. repository->add_item(key);
  527. repository->set_item_metadata(i, available_urls[key]);
  528. }
  529. }
  530. void EditorAssetLibrary::_install_asset() {
  531. ERR_FAIL_COND(!description);
  532. for (int i = 0; i < downloads_hb->get_child_count(); i++) {
  533. EditorAssetLibraryItemDownload *d = Object::cast_to<EditorAssetLibraryItemDownload>(downloads_hb->get_child(i));
  534. if (d && d->get_asset_id() == description->get_asset_id()) {
  535. if (EditorNode::get_singleton() != NULL)
  536. EditorNode::get_singleton()->show_warning(TTR("Download for this asset is already in progress!"));
  537. return;
  538. }
  539. }
  540. EditorAssetLibraryItemDownload *download = memnew(EditorAssetLibraryItemDownload);
  541. downloads_hb->add_child(download);
  542. download->configure(description->get_title(), description->get_asset_id(), description->get_preview_icon(), description->get_download_url(), description->get_sha256());
  543. if (templates_only) {
  544. download->set_external_install(true);
  545. download->connect("install_asset", this, "_install_external_asset");
  546. }
  547. }
  548. const char *EditorAssetLibrary::sort_key[SORT_MAX] = {
  549. "updated",
  550. "updated",
  551. "name",
  552. "name",
  553. "cost",
  554. "cost",
  555. };
  556. const char *EditorAssetLibrary::sort_text[SORT_MAX] = {
  557. TTRC("Recently Updated"),
  558. TTRC("Least Recently Updated"),
  559. TTRC("Name (A-Z)"),
  560. TTRC("Name (Z-A)"),
  561. TTRC("License (A-Z)"), // "cost" stores the SPDX license name in the Godot Asset Library.
  562. TTRC("License (Z-A)"), // "cost" stores the SPDX license name in the Godot Asset Library.
  563. };
  564. const char *EditorAssetLibrary::support_key[SUPPORT_MAX] = {
  565. "official",
  566. "community",
  567. "testing",
  568. };
  569. void EditorAssetLibrary::_select_author(int p_id) {
  570. // Open author window.
  571. }
  572. void EditorAssetLibrary::_select_category(int p_id) {
  573. for (int i = 0; i < categories->get_item_count(); i++) {
  574. if (i == 0)
  575. continue;
  576. int id = categories->get_item_metadata(i);
  577. if (id == p_id) {
  578. categories->select(i);
  579. _search();
  580. break;
  581. }
  582. }
  583. }
  584. void EditorAssetLibrary::_select_asset(int p_id) {
  585. _api_request("asset/" + itos(p_id), REQUESTING_ASSET);
  586. }
  587. void EditorAssetLibrary::_image_update(bool use_cache, bool final, const PoolByteArray &p_data, int p_queue_id) {
  588. Object *obj = ObjectDB::get_instance(image_queue[p_queue_id].target);
  589. if (obj) {
  590. bool image_set = false;
  591. PoolByteArray image_data = p_data;
  592. if (use_cache) {
  593. String cache_filename_base = EditorSettings::get_singleton()->get_cache_dir().plus_file("assetimage_" + image_queue[p_queue_id].image_url.md5_text());
  594. FileAccess *file = FileAccess::open(cache_filename_base + ".data", FileAccess::READ);
  595. if (file) {
  596. PoolByteArray cached_data;
  597. int len = file->get_32();
  598. cached_data.resize(len);
  599. PoolByteArray::Write w = cached_data.write();
  600. file->get_buffer(w.ptr(), len);
  601. image_data = cached_data;
  602. file->close();
  603. memdelete(file);
  604. }
  605. }
  606. int len = image_data.size();
  607. PoolByteArray::Read r = image_data.read();
  608. Ref<Image> image = Ref<Image>(memnew(Image));
  609. uint8_t png_signature[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
  610. uint8_t jpg_signature[3] = { 255, 216, 255 };
  611. if (r.ptr()) {
  612. if ((memcmp(&r[0], &png_signature[0], 8) == 0) && Image::_png_mem_loader_func) {
  613. image->copy_internals_from(Image::_png_mem_loader_func(r.ptr(), len));
  614. } else if ((memcmp(&r[0], &jpg_signature[0], 3) == 0) && Image::_jpg_mem_loader_func) {
  615. image->copy_internals_from(Image::_jpg_mem_loader_func(r.ptr(), len));
  616. }
  617. }
  618. if (!image->empty()) {
  619. switch (image_queue[p_queue_id].image_type) {
  620. case IMAGE_QUEUE_ICON:
  621. image->resize(64 * EDSCALE, 64 * EDSCALE, Image::INTERPOLATE_LANCZOS);
  622. break;
  623. case IMAGE_QUEUE_THUMBNAIL: {
  624. float max_height = 85 * EDSCALE;
  625. float scale_ratio = max_height / (image->get_height() * EDSCALE);
  626. if (scale_ratio < 1) {
  627. image->resize(image->get_width() * EDSCALE * scale_ratio, image->get_height() * EDSCALE * scale_ratio, Image::INTERPOLATE_LANCZOS);
  628. }
  629. } break;
  630. case IMAGE_QUEUE_SCREENSHOT: {
  631. float max_height = 397 * EDSCALE;
  632. float scale_ratio = max_height / (image->get_height() * EDSCALE);
  633. if (scale_ratio < 1) {
  634. image->resize(image->get_width() * EDSCALE * scale_ratio, image->get_height() * EDSCALE * scale_ratio, Image::INTERPOLATE_LANCZOS);
  635. }
  636. } break;
  637. }
  638. Ref<ImageTexture> tex;
  639. tex.instance();
  640. tex->create_from_image(image);
  641. obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, tex);
  642. image_set = true;
  643. }
  644. if (!image_set && final) {
  645. obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, get_icon("FileBrokenBigThumb", "EditorIcons"));
  646. }
  647. }
  648. }
  649. void EditorAssetLibrary::_image_request_completed(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data, int p_queue_id) {
  650. ERR_FAIL_COND(!image_queue.has(p_queue_id));
  651. if (p_status == HTTPRequest::RESULT_SUCCESS && p_code < HTTPClient::RESPONSE_BAD_REQUEST) {
  652. if (p_code != HTTPClient::RESPONSE_NOT_MODIFIED) {
  653. for (int i = 0; i < headers.size(); i++) {
  654. if (headers[i].findn("ETag:") == 0) { // Save etag
  655. String cache_filename_base = EditorSettings::get_singleton()->get_cache_dir().plus_file("assetimage_" + image_queue[p_queue_id].image_url.md5_text());
  656. String new_etag = headers[i].substr(headers[i].find(":") + 1, headers[i].length()).strip_edges();
  657. FileAccess *file;
  658. file = FileAccess::open(cache_filename_base + ".etag", FileAccess::WRITE);
  659. if (file) {
  660. file->store_line(new_etag);
  661. file->close();
  662. memdelete(file);
  663. }
  664. int len = p_data.size();
  665. PoolByteArray::Read r = p_data.read();
  666. file = FileAccess::open(cache_filename_base + ".data", FileAccess::WRITE);
  667. if (file) {
  668. file->store_32(len);
  669. file->store_buffer(r.ptr(), len);
  670. file->close();
  671. memdelete(file);
  672. }
  673. break;
  674. }
  675. }
  676. }
  677. _image_update(p_code == HTTPClient::RESPONSE_NOT_MODIFIED, true, p_data, p_queue_id);
  678. } else {
  679. WARN_PRINTS("Error getting image file from URL: " + image_queue[p_queue_id].image_url);
  680. Object *obj = ObjectDB::get_instance(image_queue[p_queue_id].target);
  681. if (obj) {
  682. obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, get_icon("FileBrokenBigThumb", "EditorIcons"));
  683. }
  684. }
  685. image_queue[p_queue_id].request->queue_delete();
  686. image_queue.erase(p_queue_id);
  687. _update_image_queue();
  688. }
  689. void EditorAssetLibrary::_update_image_queue() {
  690. const int max_images = 6;
  691. int current_images = 0;
  692. List<int> to_delete;
  693. for (Map<int, ImageQueue>::Element *E = image_queue.front(); E; E = E->next()) {
  694. if (!E->get().active && current_images < max_images) {
  695. String cache_filename_base = EditorSettings::get_singleton()->get_cache_dir().plus_file("assetimage_" + E->get().image_url.md5_text());
  696. Vector<String> headers;
  697. if (FileAccess::exists(cache_filename_base + ".etag") && FileAccess::exists(cache_filename_base + ".data")) {
  698. FileAccess *file = FileAccess::open(cache_filename_base + ".etag", FileAccess::READ);
  699. if (file) {
  700. headers.push_back("If-None-Match: " + file->get_line());
  701. file->close();
  702. memdelete(file);
  703. }
  704. }
  705. Error err = E->get().request->request(E->get().image_url, headers);
  706. if (err != OK) {
  707. to_delete.push_back(E->key());
  708. } else {
  709. E->get().active = true;
  710. }
  711. current_images++;
  712. } else if (E->get().active) {
  713. current_images++;
  714. }
  715. }
  716. while (to_delete.size()) {
  717. image_queue[to_delete.front()->get()].request->queue_delete();
  718. image_queue.erase(to_delete.front()->get());
  719. to_delete.pop_front();
  720. }
  721. }
  722. void EditorAssetLibrary::_request_image(ObjectID p_for, String p_image_url, ImageType p_type, int p_image_index) {
  723. ImageQueue iq;
  724. iq.image_url = p_image_url;
  725. iq.image_index = p_image_index;
  726. iq.image_type = p_type;
  727. iq.request = memnew(HTTPRequest);
  728. iq.request->set_use_threads(EDITOR_DEF("asset_library/use_threads", true));
  729. iq.target = p_for;
  730. iq.queue_id = ++last_queue_id;
  731. iq.active = false;
  732. iq.request->connect("request_completed", this, "_image_request_completed", varray(iq.queue_id));
  733. image_queue[iq.queue_id] = iq;
  734. add_child(iq.request);
  735. _image_update(true, false, PoolByteArray(), iq.queue_id);
  736. _update_image_queue();
  737. }
  738. void EditorAssetLibrary::_repository_changed(int p_repository_id) {
  739. host = repository->get_item_metadata(p_repository_id);
  740. if (templates_only) {
  741. _api_request("configure", REQUESTING_CONFIG, "?type=project");
  742. } else {
  743. _api_request("configure", REQUESTING_CONFIG);
  744. }
  745. }
  746. void EditorAssetLibrary::_support_toggled(int p_support) {
  747. support->get_popup()->set_item_checked(p_support, !support->get_popup()->is_item_checked(p_support));
  748. _search();
  749. }
  750. void EditorAssetLibrary::_rerun_search(int p_ignore) {
  751. _search();
  752. }
  753. void EditorAssetLibrary::_search(int p_page) {
  754. String args;
  755. if (templates_only) {
  756. args += "?type=project&";
  757. } else {
  758. args += "?";
  759. }
  760. args += String() + "sort=" + sort_key[sort->get_selected()];
  761. // We use the "branch" version, i.e. major.minor, as patch releases should be compatible
  762. args += "&godot_version=" + String(VERSION_BRANCH);
  763. String support_list;
  764. for (int i = 0; i < SUPPORT_MAX; i++) {
  765. if (support->get_popup()->is_item_checked(i)) {
  766. support_list += String(support_key[i]) + "+";
  767. }
  768. }
  769. if (support_list != String()) {
  770. args += "&support=" + support_list.substr(0, support_list.length() - 1);
  771. }
  772. if (categories->get_selected() > 0) {
  773. args += "&category=" + itos(categories->get_item_metadata(categories->get_selected()));
  774. }
  775. // Sorting options with an odd index are always the reverse of the previous one
  776. if (sort->get_selected() % 2 == 1) {
  777. args += "&reverse=true";
  778. }
  779. if (filter->get_text() != String()) {
  780. args += "&filter=" + filter->get_text().http_escape();
  781. }
  782. if (p_page > 0) {
  783. args += "&page=" + itos(p_page);
  784. }
  785. _api_request("asset", REQUESTING_SEARCH, args);
  786. }
  787. void EditorAssetLibrary::_search_text_changed(const String &p_text) {
  788. filter_debounce_timer->start();
  789. }
  790. void EditorAssetLibrary::_filter_debounce_timer_timeout() {
  791. _search();
  792. }
  793. HBoxContainer *EditorAssetLibrary::_make_pages(int p_page, int p_page_count, int p_page_len, int p_total_items, int p_current_items) {
  794. HBoxContainer *hbc = memnew(HBoxContainer);
  795. if (p_page_count < 2)
  796. return hbc;
  797. //do the mario
  798. int from = p_page - 5;
  799. if (from < 0)
  800. from = 0;
  801. int to = from + 10;
  802. if (to > p_page_count)
  803. to = p_page_count;
  804. hbc->add_spacer();
  805. hbc->add_constant_override("separation", 5 * EDSCALE);
  806. Button *first = memnew(Button);
  807. first->set_text(TTR("First"));
  808. if (p_page != 0) {
  809. first->connect("pressed", this, "_search", varray(0));
  810. } else {
  811. first->set_disabled(true);
  812. first->set_focus_mode(Control::FOCUS_NONE);
  813. }
  814. hbc->add_child(first);
  815. Button *prev = memnew(Button);
  816. prev->set_text(TTR("Previous"));
  817. if (p_page > 0) {
  818. prev->connect("pressed", this, "_search", varray(p_page - 1));
  819. } else {
  820. prev->set_disabled(true);
  821. prev->set_focus_mode(Control::FOCUS_NONE);
  822. }
  823. hbc->add_child(prev);
  824. hbc->add_child(memnew(VSeparator));
  825. for (int i = from; i < to; i++) {
  826. if (i == p_page) {
  827. Button *current = memnew(Button);
  828. // Keep the extended padding for the currently active page (see below).
  829. current->set_text(vformat(" %d ", i + 1));
  830. current->set_disabled(true);
  831. current->set_focus_mode(Control::FOCUS_NONE);
  832. hbc->add_child(current);
  833. } else {
  834. Button *current = memnew(Button);
  835. // Add padding to make page number buttons easier to click.
  836. current->set_text(vformat(" %d ", i + 1));
  837. current->connect("pressed", this, "_search", varray(i));
  838. hbc->add_child(current);
  839. }
  840. }
  841. Button *next = memnew(Button);
  842. next->set_text(TTR("Next"));
  843. if (p_page < p_page_count - 1) {
  844. next->connect("pressed", this, "_search", varray(p_page + 1));
  845. } else {
  846. next->set_disabled(true);
  847. next->set_focus_mode(Control::FOCUS_NONE);
  848. }
  849. hbc->add_child(memnew(VSeparator));
  850. hbc->add_child(next);
  851. Button *last = memnew(Button);
  852. last->set_text(TTR("Last"));
  853. if (p_page != p_page_count - 1) {
  854. last->connect("pressed", this, "_search", varray(p_page_count - 1));
  855. } else {
  856. last->set_disabled(true);
  857. last->set_focus_mode(Control::FOCUS_NONE);
  858. }
  859. hbc->add_child(last);
  860. hbc->add_spacer();
  861. return hbc;
  862. }
  863. void EditorAssetLibrary::_api_request(const String &p_request, RequestType p_request_type, const String &p_arguments) {
  864. if (requesting != REQUESTING_NONE) {
  865. request->cancel_request();
  866. }
  867. requesting = p_request_type;
  868. error_hb->hide();
  869. request->request(host + "/" + p_request + p_arguments);
  870. }
  871. void EditorAssetLibrary::_http_request_completed(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data) {
  872. String str;
  873. {
  874. int datalen = p_data.size();
  875. PoolByteArray::Read r = p_data.read();
  876. str.parse_utf8((const char *)r.ptr(), datalen);
  877. }
  878. bool error_abort = true;
  879. switch (p_status) {
  880. case HTTPRequest::RESULT_CANT_RESOLVE: {
  881. error_label->set_text(TTR("Can't resolve hostname:") + " " + host);
  882. } break;
  883. case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED:
  884. case HTTPRequest::RESULT_CONNECTION_ERROR:
  885. case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH: {
  886. error_label->set_text(TTR("Connection error, please try again."));
  887. } break;
  888. case HTTPRequest::RESULT_SSL_HANDSHAKE_ERROR:
  889. case HTTPRequest::RESULT_CANT_CONNECT: {
  890. error_label->set_text(TTR("Can't connect to host:") + " " + host);
  891. } break;
  892. case HTTPRequest::RESULT_NO_RESPONSE: {
  893. error_label->set_text(TTR("No response from host:") + " " + host);
  894. } break;
  895. case HTTPRequest::RESULT_REQUEST_FAILED: {
  896. error_label->set_text(TTR("Request failed, return code:") + " " + itos(p_code));
  897. } break;
  898. case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: {
  899. error_label->set_text(TTR("Request failed, too many redirects"));
  900. } break;
  901. default: {
  902. if (p_code != 200) {
  903. error_label->set_text(TTR("Request failed, return code:") + " " + itos(p_code));
  904. } else {
  905. error_abort = false;
  906. }
  907. } break;
  908. }
  909. if (error_abort) {
  910. error_hb->show();
  911. return;
  912. }
  913. Dictionary d;
  914. {
  915. Variant js;
  916. String errs;
  917. int errl;
  918. JSON::parse(str, js, errs, errl);
  919. d = js;
  920. }
  921. RequestType requested = requesting;
  922. requesting = REQUESTING_NONE;
  923. switch (requested) {
  924. case REQUESTING_CONFIG: {
  925. categories->clear();
  926. categories->add_item(TTR("All"));
  927. categories->set_item_metadata(0, 0);
  928. if (d.has("categories")) {
  929. Array clist = d["categories"];
  930. for (int i = 0; i < clist.size(); i++) {
  931. Dictionary cat = clist[i];
  932. if (!cat.has("name") || !cat.has("id"))
  933. continue;
  934. String name = cat["name"];
  935. int id = cat["id"];
  936. categories->add_item(name);
  937. categories->set_item_metadata(categories->get_item_count() - 1, id);
  938. category_map[cat["id"]] = name;
  939. }
  940. }
  941. _search();
  942. } break;
  943. case REQUESTING_SEARCH: {
  944. initial_loading = false;
  945. // The loading text only needs to be displayed before the first page is loaded.
  946. // Therefore, we don't need to show it again.
  947. library_loading->hide();
  948. library_error->hide();
  949. if (asset_items) {
  950. memdelete(asset_items);
  951. }
  952. if (asset_top_page) {
  953. memdelete(asset_top_page);
  954. }
  955. if (asset_bottom_page) {
  956. memdelete(asset_bottom_page);
  957. }
  958. int page = 0;
  959. int pages = 1;
  960. int page_len = 10;
  961. int total_items = 1;
  962. Array result;
  963. if (d.has("page")) {
  964. page = d["page"];
  965. }
  966. if (d.has("pages")) {
  967. pages = d["pages"];
  968. }
  969. if (d.has("page_length")) {
  970. page_len = d["page_length"];
  971. }
  972. if (d.has("total")) {
  973. total_items = d["total"];
  974. }
  975. if (d.has("result")) {
  976. result = d["result"];
  977. }
  978. asset_top_page = _make_pages(page, pages, page_len, total_items, result.size());
  979. library_vb->add_child(asset_top_page);
  980. asset_items = memnew(GridContainer);
  981. asset_items->set_columns(2);
  982. asset_items->add_constant_override("hseparation", 10 * EDSCALE);
  983. asset_items->add_constant_override("vseparation", 10 * EDSCALE);
  984. library_vb->add_child(asset_items);
  985. asset_bottom_page = _make_pages(page, pages, page_len, total_items, result.size());
  986. library_vb->add_child(asset_bottom_page);
  987. if (result.empty()) {
  988. library_error->set_text(vformat(TTR("No results for \"%s\"."), filter->get_text()));
  989. library_error->show();
  990. }
  991. for (int i = 0; i < result.size(); i++) {
  992. Dictionary r = result[i];
  993. ERR_CONTINUE(!r.has("title"));
  994. ERR_CONTINUE(!r.has("asset_id"));
  995. ERR_CONTINUE(!r.has("author"));
  996. ERR_CONTINUE(!r.has("author_id"));
  997. ERR_CONTINUE(!r.has("category_id"));
  998. ERR_FAIL_COND(!category_map.has(r["category_id"]));
  999. ERR_CONTINUE(!r.has("cost"));
  1000. EditorAssetLibraryItem *item = memnew(EditorAssetLibraryItem);
  1001. asset_items->add_child(item);
  1002. item->configure(r["title"], r["asset_id"], category_map[r["category_id"]], r["category_id"], r["author"], r["author_id"], r["cost"]);
  1003. item->connect("asset_selected", this, "_select_asset");
  1004. item->connect("author_selected", this, "_select_author");
  1005. item->connect("category_selected", this, "_select_category");
  1006. if (r.has("icon_url") && r["icon_url"] != "") {
  1007. _request_image(item->get_instance_id(), r["icon_url"], IMAGE_QUEUE_ICON, 0);
  1008. }
  1009. }
  1010. if (!result.empty()) {
  1011. library_scroll->set_v_scroll(0);
  1012. }
  1013. } break;
  1014. case REQUESTING_ASSET: {
  1015. Dictionary r = d;
  1016. ERR_FAIL_COND(!r.has("title"));
  1017. ERR_FAIL_COND(!r.has("asset_id"));
  1018. ERR_FAIL_COND(!r.has("author"));
  1019. ERR_FAIL_COND(!r.has("author_id"));
  1020. ERR_FAIL_COND(!r.has("version"));
  1021. ERR_FAIL_COND(!r.has("version_string"));
  1022. ERR_FAIL_COND(!r.has("category_id"));
  1023. ERR_FAIL_COND(!category_map.has(r["category_id"]));
  1024. ERR_FAIL_COND(!r.has("cost"));
  1025. ERR_FAIL_COND(!r.has("description"));
  1026. ERR_FAIL_COND(!r.has("download_url"));
  1027. ERR_FAIL_COND(!r.has("download_hash"));
  1028. ERR_FAIL_COND(!r.has("browse_url"));
  1029. if (description) {
  1030. memdelete(description);
  1031. }
  1032. description = memnew(EditorAssetLibraryItemDescription);
  1033. add_child(description);
  1034. description->popup_centered_minsize();
  1035. description->connect("confirmed", this, "_install_asset");
  1036. 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"]);
  1037. if (r.has("icon_url") && r["icon_url"] != "") {
  1038. _request_image(description->get_instance_id(), r["icon_url"], IMAGE_QUEUE_ICON, 0);
  1039. }
  1040. if (d.has("previews")) {
  1041. Array previews = d["previews"];
  1042. for (int i = 0; i < previews.size(); i++) {
  1043. Dictionary p = previews[i];
  1044. ERR_CONTINUE(!p.has("type"));
  1045. ERR_CONTINUE(!p.has("link"));
  1046. bool is_video = p.has("type") && String(p["type"]) == "video";
  1047. String video_url;
  1048. if (is_video && p.has("link")) {
  1049. video_url = p["link"];
  1050. }
  1051. description->add_preview(i, is_video, video_url);
  1052. if (p.has("thumbnail")) {
  1053. _request_image(description->get_instance_id(), p["thumbnail"], IMAGE_QUEUE_THUMBNAIL, i);
  1054. }
  1055. if (!is_video) {
  1056. _request_image(description->get_instance_id(), p["link"], IMAGE_QUEUE_SCREENSHOT, i);
  1057. }
  1058. }
  1059. }
  1060. } break;
  1061. default: break;
  1062. }
  1063. }
  1064. void EditorAssetLibrary::_asset_file_selected(const String &p_file) {
  1065. if (asset_installer) {
  1066. memdelete(asset_installer);
  1067. asset_installer = NULL;
  1068. }
  1069. asset_installer = memnew(EditorAssetInstaller);
  1070. add_child(asset_installer);
  1071. asset_installer->open(p_file);
  1072. }
  1073. void EditorAssetLibrary::_asset_open() {
  1074. asset_open->popup_centered_ratio();
  1075. }
  1076. void EditorAssetLibrary::_manage_plugins() {
  1077. ProjectSettingsEditor::get_singleton()->popup_project_settings();
  1078. ProjectSettingsEditor::get_singleton()->set_plugins_page();
  1079. }
  1080. void EditorAssetLibrary::_install_external_asset(String p_zip_path, String p_title) {
  1081. emit_signal("install_asset", p_zip_path, p_title);
  1082. }
  1083. void EditorAssetLibrary::disable_community_support() {
  1084. support->get_popup()->set_item_checked(SUPPORT_COMMUNITY, false);
  1085. }
  1086. void EditorAssetLibrary::_bind_methods() {
  1087. ClassDB::bind_method("_http_request_completed", &EditorAssetLibrary::_http_request_completed);
  1088. ClassDB::bind_method("_select_asset", &EditorAssetLibrary::_select_asset);
  1089. ClassDB::bind_method("_select_author", &EditorAssetLibrary::_select_author);
  1090. ClassDB::bind_method("_select_category", &EditorAssetLibrary::_select_category);
  1091. ClassDB::bind_method("_image_request_completed", &EditorAssetLibrary::_image_request_completed);
  1092. ClassDB::bind_method("_search", &EditorAssetLibrary::_search, DEFVAL(0));
  1093. ClassDB::bind_method("_search_text_changed", &EditorAssetLibrary::_search_text_changed);
  1094. ClassDB::bind_method("_filter_debounce_timer_timeout", &EditorAssetLibrary::_filter_debounce_timer_timeout);
  1095. ClassDB::bind_method("_install_asset", &EditorAssetLibrary::_install_asset);
  1096. ClassDB::bind_method("_manage_plugins", &EditorAssetLibrary::_manage_plugins);
  1097. ClassDB::bind_method("_asset_open", &EditorAssetLibrary::_asset_open);
  1098. ClassDB::bind_method("_asset_file_selected", &EditorAssetLibrary::_asset_file_selected);
  1099. ClassDB::bind_method("_repository_changed", &EditorAssetLibrary::_repository_changed);
  1100. ClassDB::bind_method("_support_toggled", &EditorAssetLibrary::_support_toggled);
  1101. ClassDB::bind_method("_rerun_search", &EditorAssetLibrary::_rerun_search);
  1102. ClassDB::bind_method("_install_external_asset", &EditorAssetLibrary::_install_external_asset);
  1103. ADD_SIGNAL(MethodInfo("install_asset", PropertyInfo(Variant::STRING, "zip_path"), PropertyInfo(Variant::STRING, "name")));
  1104. }
  1105. EditorAssetLibrary::EditorAssetLibrary(bool p_templates_only) {
  1106. requesting = REQUESTING_NONE;
  1107. templates_only = p_templates_only;
  1108. initial_loading = true;
  1109. VBoxContainer *library_main = memnew(VBoxContainer);
  1110. add_child(library_main);
  1111. HBoxContainer *search_hb = memnew(HBoxContainer);
  1112. library_main->add_child(search_hb);
  1113. library_main->add_constant_override("separation", 10 * EDSCALE);
  1114. filter = memnew(LineEdit);
  1115. search_hb->add_child(filter);
  1116. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  1117. filter->connect("text_changed", this, "_search_text_changed");
  1118. // Perform a search automatically if the user hasn't entered any text for a certain duration.
  1119. // This way, the user doesn't need to press Enter to initiate their search.
  1120. filter_debounce_timer = memnew(Timer);
  1121. filter_debounce_timer->set_one_shot(true);
  1122. filter_debounce_timer->set_wait_time(0.25);
  1123. filter_debounce_timer->connect("timeout", this, "_filter_debounce_timer_timeout");
  1124. search_hb->add_child(filter_debounce_timer);
  1125. if (!p_templates_only)
  1126. search_hb->add_child(memnew(VSeparator));
  1127. Button *open_asset = memnew(Button);
  1128. open_asset->set_text(TTR("Import..."));
  1129. search_hb->add_child(open_asset);
  1130. open_asset->connect("pressed", this, "_asset_open");
  1131. Button *plugins = memnew(Button);
  1132. plugins->set_text(TTR("Plugins..."));
  1133. search_hb->add_child(plugins);
  1134. plugins->connect("pressed", this, "_manage_plugins");
  1135. if (p_templates_only) {
  1136. open_asset->hide();
  1137. plugins->hide();
  1138. }
  1139. HBoxContainer *search_hb2 = memnew(HBoxContainer);
  1140. library_main->add_child(search_hb2);
  1141. search_hb2->add_child(memnew(Label(TTR("Sort:") + " ")));
  1142. sort = memnew(OptionButton);
  1143. for (int i = 0; i < SORT_MAX; i++) {
  1144. sort->add_item(TTRGET(sort_text[i]));
  1145. }
  1146. search_hb2->add_child(sort);
  1147. sort->set_h_size_flags(SIZE_EXPAND_FILL);
  1148. sort->connect("item_selected", this, "_rerun_search");
  1149. search_hb2->add_child(memnew(VSeparator));
  1150. search_hb2->add_child(memnew(Label(TTR("Category:") + " ")));
  1151. categories = memnew(OptionButton);
  1152. categories->add_item(TTR("All"));
  1153. search_hb2->add_child(categories);
  1154. categories->set_h_size_flags(SIZE_EXPAND_FILL);
  1155. categories->connect("item_selected", this, "_rerun_search");
  1156. search_hb2->add_child(memnew(VSeparator));
  1157. search_hb2->add_child(memnew(Label(TTR("Site:") + " ")));
  1158. repository = memnew(OptionButton);
  1159. _update_repository_options();
  1160. repository->connect("item_selected", this, "_repository_changed");
  1161. search_hb2->add_child(repository);
  1162. repository->set_h_size_flags(SIZE_EXPAND_FILL);
  1163. search_hb2->add_child(memnew(VSeparator));
  1164. support = memnew(MenuButton);
  1165. search_hb2->add_child(support);
  1166. support->set_text(TTR("Support"));
  1167. support->get_popup()->set_hide_on_checkable_item_selection(false);
  1168. support->get_popup()->add_check_item(TTR("Official"), SUPPORT_OFFICIAL);
  1169. support->get_popup()->add_check_item(TTR("Community"), SUPPORT_COMMUNITY);
  1170. support->get_popup()->add_check_item(TTR("Testing"), SUPPORT_TESTING);
  1171. support->get_popup()->set_item_checked(SUPPORT_OFFICIAL, true);
  1172. support->get_popup()->set_item_checked(SUPPORT_COMMUNITY, true);
  1173. support->get_popup()->connect("id_pressed", this, "_support_toggled");
  1174. /////////
  1175. library_scroll_bg = memnew(PanelContainer);
  1176. library_main->add_child(library_scroll_bg);
  1177. library_scroll_bg->set_v_size_flags(SIZE_EXPAND_FILL);
  1178. library_scroll = memnew(ScrollContainer);
  1179. library_scroll->set_enable_v_scroll(true);
  1180. library_scroll->set_enable_h_scroll(false);
  1181. library_scroll_bg->add_child(library_scroll);
  1182. Ref<StyleBoxEmpty> border2;
  1183. border2.instance();
  1184. border2->set_default_margin(MARGIN_LEFT, 15 * EDSCALE);
  1185. border2->set_default_margin(MARGIN_RIGHT, 35 * EDSCALE);
  1186. border2->set_default_margin(MARGIN_BOTTOM, 15 * EDSCALE);
  1187. border2->set_default_margin(MARGIN_TOP, 15 * EDSCALE);
  1188. PanelContainer *library_vb_border = memnew(PanelContainer);
  1189. library_scroll->add_child(library_vb_border);
  1190. library_vb_border->add_style_override("panel", border2);
  1191. library_vb_border->set_h_size_flags(SIZE_EXPAND_FILL);
  1192. library_vb_border->set_mouse_filter(MOUSE_FILTER_PASS);
  1193. library_vb = memnew(VBoxContainer);
  1194. library_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1195. library_vb_border->add_child(library_vb);
  1196. library_loading = memnew(Label(TTR("Loading...")));
  1197. library_loading->set_align(Label::ALIGN_CENTER);
  1198. library_vb->add_child(library_loading);
  1199. library_error = memnew(Label);
  1200. library_error->set_align(Label::ALIGN_CENTER);
  1201. library_error->hide();
  1202. library_vb->add_child(library_error);
  1203. asset_top_page = memnew(HBoxContainer);
  1204. library_vb->add_child(asset_top_page);
  1205. asset_items = memnew(GridContainer);
  1206. asset_items->set_columns(2);
  1207. asset_items->add_constant_override("hseparation", 10 * EDSCALE);
  1208. asset_items->add_constant_override("vseparation", 10 * EDSCALE);
  1209. library_vb->add_child(asset_items);
  1210. asset_bottom_page = memnew(HBoxContainer);
  1211. library_vb->add_child(asset_bottom_page);
  1212. request = memnew(HTTPRequest);
  1213. add_child(request);
  1214. request->set_use_threads(EDITOR_DEF("asset_library/use_threads", true));
  1215. request->connect("request_completed", this, "_http_request_completed");
  1216. last_queue_id = 0;
  1217. library_vb->add_constant_override("separation", 20 * EDSCALE);
  1218. error_hb = memnew(HBoxContainer);
  1219. library_main->add_child(error_hb);
  1220. error_label = memnew(Label);
  1221. error_label->add_color_override("color", get_color("error_color", "Editor"));
  1222. error_hb->add_child(error_label);
  1223. error_tr = memnew(TextureRect);
  1224. error_tr->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  1225. error_hb->add_child(error_tr);
  1226. description = NULL;
  1227. set_process(true);
  1228. downloads_scroll = memnew(ScrollContainer);
  1229. downloads_scroll->set_enable_h_scroll(true);
  1230. downloads_scroll->set_enable_v_scroll(false);
  1231. library_main->add_child(downloads_scroll);
  1232. downloads_hb = memnew(HBoxContainer);
  1233. downloads_scroll->add_child(downloads_hb);
  1234. asset_open = memnew(EditorFileDialog);
  1235. asset_open->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  1236. asset_open->add_filter("*.zip ; " + TTR("Assets ZIP File"));
  1237. asset_open->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  1238. add_child(asset_open);
  1239. asset_open->connect("file_selected", this, "_asset_file_selected");
  1240. asset_installer = NULL;
  1241. }
  1242. ///////
  1243. void AssetLibraryEditorPlugin::make_visible(bool p_visible) {
  1244. if (p_visible) {
  1245. addon_library->show();
  1246. } else {
  1247. addon_library->hide();
  1248. }
  1249. }
  1250. AssetLibraryEditorPlugin::AssetLibraryEditorPlugin(EditorNode *p_node) {
  1251. editor = p_node;
  1252. addon_library = memnew(EditorAssetLibrary);
  1253. addon_library->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1254. editor->get_viewport()->add_child(addon_library);
  1255. addon_library->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1256. addon_library->hide();
  1257. }
  1258. AssetLibraryEditorPlugin::~AssetLibraryEditorPlugin() {
  1259. }