asset_library_editor_plugin.cpp 46 KB

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