asset_library_editor_plugin.cpp 45 KB

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