asset_library_editor_plugin.cpp 45 KB

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