asset_library_editor_plugin.cpp 47 KB

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