asset_library_editor_plugin.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. }
  342. prev_status = cstatus;
  343. }
  344. }
  345. }
  346. void EditorAssetLibraryItemDownload::_close() {
  347. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  348. da->remove(download->get_download_file()); //clean up removed file
  349. memdelete(da);
  350. queue_delete();
  351. }
  352. void EditorAssetLibraryItemDownload::_install() {
  353. String file = download->get_download_file();
  354. if (external_install) {
  355. emit_signal("install_asset", file, title->get_text());
  356. return;
  357. }
  358. asset_installer->open(file, 1);
  359. }
  360. void EditorAssetLibraryItemDownload::_make_request() {
  361. download->cancel_request();
  362. download->set_download_file(EditorSettings::get_singleton()->get_settings_path().plus_file("tmp").plus_file("tmp_asset_" + itos(asset_id)) + ".zip");
  363. Error err = download->request(host);
  364. if (err != OK) {
  365. status->set_text(TTR("Error making request"));
  366. } else {
  367. set_process(true);
  368. }
  369. }
  370. void EditorAssetLibraryItemDownload::_bind_methods() {
  371. ObjectTypeDB::bind_method("_http_download_completed", &EditorAssetLibraryItemDownload::_http_download_completed);
  372. ObjectTypeDB::bind_method("_install", &EditorAssetLibraryItemDownload::_install);
  373. ObjectTypeDB::bind_method("_close", &EditorAssetLibraryItemDownload::_close);
  374. ObjectTypeDB::bind_method("_make_request", &EditorAssetLibraryItemDownload::_make_request);
  375. ADD_SIGNAL(MethodInfo("install_asset", PropertyInfo(Variant::STRING, "zip_path"), PropertyInfo(Variant::STRING, "name")));
  376. }
  377. EditorAssetLibraryItemDownload::EditorAssetLibraryItemDownload() {
  378. HBoxContainer *hb = memnew(HBoxContainer);
  379. add_child(hb);
  380. icon = memnew(TextureFrame);
  381. hb->add_child(icon);
  382. VBoxContainer *vb = memnew(VBoxContainer);
  383. hb->add_child(vb);
  384. vb->set_h_size_flags(SIZE_EXPAND_FILL);
  385. HBoxContainer *title_hb = memnew(HBoxContainer);
  386. vb->add_child(title_hb);
  387. title = memnew(Label);
  388. title_hb->add_child(title);
  389. title->set_h_size_flags(SIZE_EXPAND_FILL);
  390. dismiss = memnew(TextureButton);
  391. dismiss->connect("pressed", this, "_close");
  392. title_hb->add_child(dismiss);
  393. title->set_clip_text(true);
  394. vb->add_spacer();
  395. status = memnew(Label(TTR("Idle")));
  396. vb->add_child(status);
  397. status->add_color_override("font_color", Color(0.5, 0.5, 0.5));
  398. progress = memnew(ProgressBar);
  399. vb->add_child(progress);
  400. HBoxContainer *hb2 = memnew(HBoxContainer);
  401. vb->add_child(hb2);
  402. hb2->add_spacer();
  403. install = memnew(Button);
  404. install->set_text(TTR("Install"));
  405. install->set_disabled(true);
  406. install->connect("pressed", this, "_install");
  407. retry = memnew(Button);
  408. retry->set_text(TTR("Retry"));
  409. retry->connect("pressed", this, "_make_request");
  410. hb2->add_child(retry);
  411. hb2->add_child(install);
  412. set_custom_minimum_size(Size2(250, 0));
  413. download = memnew(HTTPRequest);
  414. add_child(download);
  415. download->connect("request_completed", this, "_http_download_completed");
  416. download_error = memnew(AcceptDialog);
  417. add_child(download_error);
  418. download_error->set_title(TTR("Download Error"));
  419. asset_installer = memnew(EditorAssetInstaller);
  420. add_child(asset_installer);
  421. prev_status = -1;
  422. external_install = false;
  423. }
  424. ////////////////////////////////////////////////////////////////////////////////
  425. void EditorAssetLibrary::_notification(int p_what) {
  426. if (p_what == NOTIFICATION_READY) {
  427. TextureFrame *tf = memnew(TextureFrame);
  428. tf->set_texture(get_icon("Error", "EditorIcons"));
  429. reverse->set_icon(get_icon("Updown", "EditorIcons"));
  430. error_hb->add_child(tf);
  431. error_label->raise();
  432. }
  433. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  434. if (!is_hidden()) {
  435. _repository_changed(0); // Update when shown for the first time
  436. }
  437. }
  438. if (p_what == NOTIFICATION_PROCESS) {
  439. HTTPClient::Status s = request->get_http_client_status();
  440. bool visible = s != HTTPClient::STATUS_DISCONNECTED;
  441. if (visible != !load_status->is_hidden()) {
  442. load_status->set_hidden(!visible);
  443. }
  444. if (visible) {
  445. switch (s) {
  446. case HTTPClient::STATUS_RESOLVING: {
  447. load_status->set_val(0.1);
  448. } break;
  449. case HTTPClient::STATUS_CONNECTING: {
  450. load_status->set_val(0.2);
  451. } break;
  452. case HTTPClient::STATUS_REQUESTING: {
  453. load_status->set_val(0.3);
  454. } break;
  455. case HTTPClient::STATUS_BODY: {
  456. load_status->set_val(0.4);
  457. } break;
  458. default: {
  459. }
  460. }
  461. }
  462. bool no_downloads = downloads_hb->get_child_count() == 0;
  463. if (no_downloads != downloads_scroll->is_hidden()) {
  464. downloads_scroll->set_hidden(no_downloads);
  465. }
  466. }
  467. }
  468. void EditorAssetLibrary::_install_asset() {
  469. ERR_FAIL_COND(!description);
  470. for (int i = 0; i < downloads_hb->get_child_count(); i++) {
  471. EditorAssetLibraryItemDownload *d = downloads_hb->get_child(i)->cast_to<EditorAssetLibraryItemDownload>();
  472. if (d && d->get_asset_id() == description->get_asset_id()) {
  473. if (EditorNode::get_singleton() != NULL)
  474. EditorNode::get_singleton()->show_warning(TTR("Download for this asset is already in progress!"));
  475. return;
  476. }
  477. }
  478. EditorAssetLibraryItemDownload *download = memnew(EditorAssetLibraryItemDownload);
  479. downloads_hb->add_child(download);
  480. download->configure(description->get_title(), description->get_asset_id(), description->get_preview_icon(), description->get_download_url(), description->get_sha256());
  481. if (templates_only) {
  482. download->set_external_install(true);
  483. download->connect("install_asset", this, "_install_external_asset");
  484. }
  485. }
  486. const char *EditorAssetLibrary::sort_key[SORT_MAX] = {
  487. "rating",
  488. "downloads",
  489. "name",
  490. "cost",
  491. "updated"
  492. };
  493. const char *EditorAssetLibrary::sort_text[SORT_MAX] = {
  494. "Rating",
  495. "Downloads",
  496. "Name",
  497. "Cost",
  498. "Updated"
  499. };
  500. const char *EditorAssetLibrary::support_key[SUPPORT_MAX] = {
  501. "official",
  502. "community",
  503. "testing"
  504. };
  505. void EditorAssetLibrary::_select_author(int p_id) {
  506. //opemn author window
  507. }
  508. void EditorAssetLibrary::_select_category(int p_id) {
  509. for (int i = 0; i < categories->get_item_count(); i++) {
  510. if (i == 0)
  511. continue;
  512. int id = categories->get_item_metadata(i);
  513. if (id == p_id) {
  514. categories->select(i);
  515. _search();
  516. break;
  517. }
  518. }
  519. }
  520. void EditorAssetLibrary::_select_asset(int p_id) {
  521. _api_request("asset/" + itos(p_id), REQUESTING_ASSET);
  522. /*
  523. if (description) {
  524. memdelete(description);
  525. }
  526. description = memnew( EditorAssetLibraryItemDescription );
  527. add_child(description);
  528. description->popup_centered_minsize();*/
  529. }
  530. void EditorAssetLibrary::_image_update(bool use_cache, bool final, const ByteArray &p_data, int p_queue_id) {
  531. Object *obj = ObjectDB::get_instance(image_queue[p_queue_id].target);
  532. if (obj) {
  533. bool image_set = false;
  534. ByteArray image_data = p_data;
  535. if (use_cache) {
  536. 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());
  537. FileAccess *file = FileAccess::open(cache_filename_base + ".data", FileAccess::READ);
  538. if (file) {
  539. ByteArray cached_data;
  540. int len = file->get_32();
  541. cached_data.resize(len);
  542. ByteArray::Write w = cached_data.write();
  543. file->get_buffer(w.ptr(), len);
  544. image_data = cached_data;
  545. file->close();
  546. }
  547. }
  548. int len = image_data.size();
  549. ByteArray::Read r = image_data.read();
  550. Image image(r.ptr(), len);
  551. if (!image.empty()) {
  552. float max_height = 10000;
  553. switch (image_queue[p_queue_id].image_type) {
  554. case IMAGE_QUEUE_ICON: max_height = 80; break;
  555. case IMAGE_QUEUE_THUMBNAIL: max_height = 80; break;
  556. case IMAGE_QUEUE_SCREENSHOT: max_height = 345; break;
  557. }
  558. float scale_ratio = max_height / image.get_height();
  559. if (scale_ratio < 1) {
  560. image.resize(image.get_width() * scale_ratio, image.get_height() * scale_ratio, Image::INTERPOLATE_CUBIC);
  561. }
  562. Ref<ImageTexture> tex;
  563. tex.instance();
  564. tex->create_from_image(image);
  565. obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, tex);
  566. image_set = true;
  567. }
  568. if (!image_set && final) {
  569. obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, get_icon("ErrorSign", "EditorIcons"));
  570. }
  571. }
  572. }
  573. void EditorAssetLibrary::_image_request_completed(int p_status, int p_code, const StringArray &headers, const ByteArray &p_data, int p_queue_id) {
  574. ERR_FAIL_COND(!image_queue.has(p_queue_id));
  575. if (p_status == HTTPRequest::RESULT_SUCCESS) {
  576. if (p_code != HTTPClient::RESPONSE_NOT_MODIFIED) {
  577. for (int i = 0; i < headers.size(); i++) {
  578. if (headers[i].findn("ETag:") == 0) { // Save etag
  579. 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());
  580. String new_etag = headers[i].substr(headers[i].find(":") + 1, headers[i].length()).strip_edges();
  581. FileAccess *file;
  582. file = FileAccess::open(cache_filename_base + ".etag", FileAccess::WRITE);
  583. if (file) {
  584. file->store_line(new_etag);
  585. file->close();
  586. }
  587. int len = p_data.size();
  588. ByteArray::Read r = p_data.read();
  589. file = FileAccess::open(cache_filename_base + ".data", FileAccess::WRITE);
  590. if (file) {
  591. file->store_32(len);
  592. file->store_buffer(r.ptr(), len);
  593. file->close();
  594. }
  595. break;
  596. }
  597. }
  598. }
  599. _image_update(p_code == HTTPClient::RESPONSE_NOT_MODIFIED, true, p_data, p_queue_id);
  600. } else {
  601. WARN_PRINTS("Error getting PNG file for asset id " + itos(image_queue[p_queue_id].asset_id));
  602. Object *obj = ObjectDB::get_instance(image_queue[p_queue_id].target);
  603. if (obj) {
  604. obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, get_icon("ErrorSign", "EditorIcons"));
  605. }
  606. }
  607. image_queue[p_queue_id].request->queue_delete();
  608. image_queue.erase(p_queue_id);
  609. _update_image_queue();
  610. }
  611. void EditorAssetLibrary::_update_image_queue() {
  612. int max_images = 2;
  613. int current_images = 0;
  614. List<int> to_delete;
  615. for (Map<int, ImageQueue>::Element *E = image_queue.front(); E; E = E->next()) {
  616. if (!E->get().active && current_images < max_images) {
  617. String cache_filename_base = EditorSettings::get_singleton()->get_settings_path().plus_file("tmp").plus_file("assetimage_" + E->get().image_url.md5_text());
  618. Vector<String> headers;
  619. if (FileAccess::exists(cache_filename_base + ".etag") && FileAccess::exists(cache_filename_base + ".data")) {
  620. FileAccess *file = FileAccess::open(cache_filename_base + ".etag", FileAccess::READ);
  621. if (file) {
  622. headers.push_back("If-None-Match: " + file->get_line());
  623. file->close();
  624. }
  625. }
  626. print_line("REQUEST ICON FOR: " + itos(E->get().asset_id));
  627. Error err = E->get().request->request(E->get().image_url, headers);
  628. if (err != OK) {
  629. to_delete.push_back(E->key());
  630. } else {
  631. E->get().active = true;
  632. }
  633. current_images++;
  634. } else if (E->get().active) {
  635. current_images++;
  636. }
  637. }
  638. while (to_delete.size()) {
  639. image_queue[to_delete.front()->get()].request->queue_delete();
  640. image_queue.erase(to_delete.front()->get());
  641. to_delete.pop_front();
  642. }
  643. }
  644. void EditorAssetLibrary::_request_image(ObjectID p_for, String p_image_url, ImageType p_type, int p_image_index) {
  645. ImageQueue iq;
  646. iq.image_url = p_image_url;
  647. iq.image_index = p_image_index;
  648. iq.image_type = p_type;
  649. iq.request = memnew(HTTPRequest);
  650. iq.target = p_for;
  651. iq.queue_id = ++last_queue_id;
  652. iq.active = false;
  653. iq.request->connect("request_completed", this, "_image_request_completed", varray(iq.queue_id));
  654. image_queue[iq.queue_id] = iq;
  655. add_child(iq.request);
  656. _image_update(true, false, ByteArray(), iq.queue_id);
  657. _update_image_queue();
  658. }
  659. void EditorAssetLibrary::_repository_changed(int p_repository_id) {
  660. host = repository->get_item_metadata(p_repository_id);
  661. print_line(".." + host);
  662. if (templates_only) {
  663. _api_request("configure", REQUESTING_CONFIG, "?type=project");
  664. } else {
  665. _api_request("configure", REQUESTING_CONFIG);
  666. }
  667. }
  668. void EditorAssetLibrary::_support_toggled(int p_support) {
  669. support->get_popup()->set_item_checked(p_support, !support->get_popup()->is_item_checked(p_support));
  670. _search();
  671. }
  672. void EditorAssetLibrary::_rerun_search(int p_ignore) {
  673. _search();
  674. }
  675. void EditorAssetLibrary::_search(int p_page) {
  676. String args;
  677. if (templates_only) {
  678. args += "?type=project&";
  679. } else {
  680. args += "?";
  681. }
  682. args += String() + "sort=" + sort_key[sort->get_selected()];
  683. String support_list;
  684. for (int i = 0; i < SUPPORT_MAX; i++) {
  685. if (support->get_popup()->is_item_checked(i)) {
  686. support_list += String(support_key[i]) + "+";
  687. }
  688. }
  689. if (support_list != String()) {
  690. args += "&support=" + support_list.substr(0, support_list.length() - 1);
  691. }
  692. if (categories->get_selected() > 0) {
  693. args += "&category=" + itos(categories->get_item_metadata(categories->get_selected()));
  694. }
  695. if (reverse->is_pressed()) {
  696. args += "&reverse=true";
  697. }
  698. if (filter->get_text() != String()) {
  699. args += "&filter=" + filter->get_text().http_escape();
  700. }
  701. if (p_page > 0) {
  702. args += "&page=" + itos(p_page);
  703. }
  704. _api_request("asset", REQUESTING_SEARCH, args);
  705. }
  706. HBoxContainer *EditorAssetLibrary::_make_pages(int p_page, int p_page_count, int p_page_len, int p_total_items, int p_current_items) {
  707. HBoxContainer *hbc = memnew(HBoxContainer);
  708. //do the mario
  709. int from = p_page - 5;
  710. if (from < 0)
  711. from = 0;
  712. int to = from + 10;
  713. if (to > p_page_count)
  714. to = p_page_count;
  715. Color gray = Color(0.65, 0.65, 0.65);
  716. hbc->add_spacer();
  717. hbc->add_constant_override("separation", 10);
  718. if (p_page != 0) {
  719. LinkButton *first = memnew(LinkButton);
  720. first->set_text(TTR("first"));
  721. first->add_color_override("font_color", gray);
  722. first->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  723. first->connect("pressed", this, "_search", varray(0));
  724. hbc->add_child(first);
  725. }
  726. if (p_page > 0) {
  727. LinkButton *prev = memnew(LinkButton);
  728. prev->set_text(TTR("prev"));
  729. prev->add_color_override("font_color", gray);
  730. prev->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  731. prev->connect("pressed", this, "_search", varray(p_page - 1));
  732. hbc->add_child(prev);
  733. }
  734. for (int i = from; i < to; i++) {
  735. if (i == p_page) {
  736. Label *current = memnew(Label);
  737. current->set_text(itos(i + 1));
  738. hbc->add_child(current);
  739. } else {
  740. LinkButton *current = memnew(LinkButton);
  741. current->add_color_override("font_color", gray);
  742. current->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  743. current->set_text(itos(i + 1));
  744. current->connect("pressed", this, "_search", varray(i));
  745. hbc->add_child(current);
  746. }
  747. }
  748. if (p_page < p_page_count - 1) {
  749. LinkButton *next = memnew(LinkButton);
  750. next->set_text(TTR("next"));
  751. next->add_color_override("font_color", gray);
  752. next->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  753. next->connect("pressed", this, "_search", varray(p_page + 1));
  754. hbc->add_child(next);
  755. }
  756. if (p_page != p_page_count - 1) {
  757. LinkButton *last = memnew(LinkButton);
  758. last->set_text(TTR("last"));
  759. last->add_color_override("font_color", gray);
  760. last->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  761. hbc->add_child(last);
  762. last->connect("pressed", this, "_search", varray(p_page_count - 1));
  763. }
  764. Label *totals = memnew(Label);
  765. totals->set_text("( " + itos(from * p_page_len) + " - " + itos(from * p_page_len + p_current_items - 1) + " / " + itos(p_total_items) + " )");
  766. hbc->add_child(totals);
  767. hbc->add_spacer();
  768. return hbc;
  769. }
  770. void EditorAssetLibrary::_api_request(const String &p_request, RequestType p_request_type, const String &p_arguments) {
  771. if (requesting != REQUESTING_NONE) {
  772. request->cancel_request();
  773. }
  774. requesting = p_request_type;
  775. error_hb->hide();
  776. request->request(host + "/" + p_request + p_arguments);
  777. }
  778. void EditorAssetLibrary::_http_request_completed(int p_status, int p_code, const StringArray &headers, const ByteArray &p_data) {
  779. String str;
  780. {
  781. int datalen = p_data.size();
  782. ByteArray::Read r = p_data.read();
  783. str.parse_utf8((const char *)r.ptr(), datalen);
  784. }
  785. bool error_abort = true;
  786. switch (p_status) {
  787. case HTTPRequest::RESULT_CANT_RESOLVE: {
  788. error_label->set_text(TTR("Can't resolve hostname:") + " " + host);
  789. } break;
  790. case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED:
  791. case HTTPRequest::RESULT_CONNECTION_ERROR:
  792. case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH: {
  793. error_label->set_text(TTR("Connection error, please try again."));
  794. } break;
  795. case HTTPRequest::RESULT_SSL_HANDSHAKE_ERROR:
  796. case HTTPRequest::RESULT_CANT_CONNECT: {
  797. error_label->set_text(TTR("Can't connect to host:") + " " + host);
  798. } break;
  799. case HTTPRequest::RESULT_NO_RESPONSE: {
  800. error_label->set_text(TTR("No response from host:") + " " + host);
  801. } break;
  802. case HTTPRequest::RESULT_REQUEST_FAILED: {
  803. error_label->set_text(TTR("Request failed, return code:") + " " + itos(p_code));
  804. } break;
  805. case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: {
  806. error_label->set_text(TTR("Request failed, too many redirects"));
  807. } break;
  808. default: {
  809. if (p_code != 200) {
  810. error_label->set_text(TTR("Request failed, return code:") + " " + itos(p_code));
  811. } else {
  812. error_abort = false;
  813. }
  814. } break;
  815. }
  816. if (error_abort) {
  817. error_hb->show();
  818. return;
  819. }
  820. print_line("response: " + itos(p_status) + " code: " + itos(p_code));
  821. Dictionary d;
  822. d.parse_json(str);
  823. print_line(Variant(d).get_construct_string());
  824. RequestType requested = requesting;
  825. requesting = REQUESTING_NONE;
  826. switch (requested) {
  827. case REQUESTING_CONFIG: {
  828. categories->clear();
  829. categories->add_item(TTR("All"));
  830. categories->set_item_metadata(0, 0);
  831. if (d.has("categories")) {
  832. Array clist = d["categories"];
  833. for (int i = 0; i < clist.size(); i++) {
  834. Dictionary cat = clist[i];
  835. if (!cat.has("name") || !cat.has("id"))
  836. continue;
  837. String name = cat["name"];
  838. int id = cat["id"];
  839. categories->add_item(name);
  840. categories->set_item_metadata(categories->get_item_count() - 1, id);
  841. category_map[cat["id"]] = name;
  842. }
  843. }
  844. _search();
  845. } break;
  846. case REQUESTING_SEARCH: {
  847. if (asset_items) {
  848. memdelete(asset_items);
  849. }
  850. if (asset_top_page) {
  851. memdelete(asset_top_page);
  852. }
  853. if (asset_bottom_page) {
  854. memdelete(asset_bottom_page);
  855. }
  856. int page = 0;
  857. int pages = 1;
  858. int page_len = 10;
  859. int total_items = 1;
  860. Array result;
  861. if (d.has("page")) {
  862. page = d["page"];
  863. }
  864. if (d.has("pages")) {
  865. pages = d["pages"];
  866. }
  867. if (d.has("page_length")) {
  868. page_len = d["page_length"];
  869. }
  870. if (d.has("total")) {
  871. total_items = d["total"];
  872. }
  873. if (d.has("result")) {
  874. result = d["result"];
  875. }
  876. asset_top_page = _make_pages(page, pages, page_len, total_items, result.size());
  877. library_vb->add_child(asset_top_page);
  878. asset_items = memnew(GridContainer);
  879. asset_items->set_columns(2);
  880. asset_items->add_constant_override("hseparation", 10);
  881. asset_items->add_constant_override("vseparation", 10);
  882. library_vb->add_child(asset_items);
  883. asset_bottom_page = _make_pages(page, pages, page_len, total_items, result.size());
  884. library_vb->add_child(asset_bottom_page);
  885. for (int i = 0; i < result.size(); i++) {
  886. Dictionary r = result[i];
  887. ERR_CONTINUE(!r.has("title"));
  888. ERR_CONTINUE(!r.has("asset_id"));
  889. ERR_CONTINUE(!r.has("author"));
  890. ERR_CONTINUE(!r.has("author_id"));
  891. ERR_CONTINUE(!r.has("category_id"));
  892. ERR_FAIL_COND(!category_map.has(r["category_id"]));
  893. ERR_CONTINUE(!r.has("rating"));
  894. ERR_CONTINUE(!r.has("cost"));
  895. EditorAssetLibraryItem *item = memnew(EditorAssetLibraryItem);
  896. asset_items->add_child(item);
  897. item->configure(r["title"], r["asset_id"], category_map[r["category_id"]], r["category_id"], r["author"], r["author_id"], r["rating"], r["cost"]);
  898. item->connect("asset_selected", this, "_select_asset");
  899. item->connect("author_selected", this, "_select_author");
  900. item->connect("category_selected", this, "_select_category");
  901. if (r.has("icon_url") && r["icon_url"] != "") {
  902. _request_image(item->get_instance_ID(), r["icon_url"], IMAGE_QUEUE_ICON, 0);
  903. }
  904. }
  905. } break;
  906. case REQUESTING_ASSET: {
  907. Dictionary r = d;
  908. ERR_FAIL_COND(!r.has("title"));
  909. ERR_FAIL_COND(!r.has("asset_id"));
  910. ERR_FAIL_COND(!r.has("author"));
  911. ERR_FAIL_COND(!r.has("author_id"));
  912. ERR_FAIL_COND(!r.has("version"));
  913. ERR_FAIL_COND(!r.has("version_string"));
  914. ERR_FAIL_COND(!r.has("category_id"));
  915. ERR_FAIL_COND(!category_map.has(r["category_id"]));
  916. ERR_FAIL_COND(!r.has("rating"));
  917. ERR_FAIL_COND(!r.has("cost"));
  918. ERR_FAIL_COND(!r.has("description"));
  919. ERR_FAIL_COND(!r.has("download_url"));
  920. ERR_FAIL_COND(!r.has("download_hash"));
  921. ERR_FAIL_COND(!r.has("browse_url"));
  922. if (description) {
  923. memdelete(description);
  924. }
  925. description = memnew(EditorAssetLibraryItemDescription);
  926. add_child(description);
  927. description->popup_centered_minsize();
  928. description->connect("confirmed", this, "_install_asset");
  929. 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"]);
  930. /*item->connect("asset_selected",this,"_select_asset");
  931. item->connect("author_selected",this,"_select_author");
  932. item->connect("category_selected",this,"_category_selected");*/
  933. if (r.has("icon_url") && r["icon_url"] != "") {
  934. _request_image(description->get_instance_ID(), r["icon_url"], IMAGE_QUEUE_ICON, 0);
  935. }
  936. if (d.has("previews")) {
  937. Array previews = d["previews"];
  938. for (int i = 0; i < previews.size(); i++) {
  939. Dictionary p = previews[i];
  940. ERR_CONTINUE(!p.has("type"));
  941. ERR_CONTINUE(!p.has("link"));
  942. bool is_video = p.has("type") && String(p["type"]) == "video";
  943. String video_url;
  944. if (is_video && p.has("link")) {
  945. video_url = p["link"];
  946. }
  947. description->add_preview(i, is_video, video_url);
  948. if (p.has("thumbnail")) {
  949. _request_image(description->get_instance_ID(), p["thumbnail"], IMAGE_QUEUE_THUMBNAIL, i);
  950. }
  951. if (is_video) {
  952. //_request_image(description->get_instance_ID(),p["link"],IMAGE_QUEUE_SCREENSHOT,i);
  953. } else {
  954. _request_image(description->get_instance_ID(), p["link"], IMAGE_QUEUE_SCREENSHOT, i);
  955. }
  956. }
  957. }
  958. } break;
  959. default: break;
  960. }
  961. }
  962. void EditorAssetLibrary::_asset_file_selected(const String &p_file) {
  963. if (asset_installer) {
  964. memdelete(asset_installer);
  965. asset_installer = NULL;
  966. }
  967. asset_installer = memnew(EditorAssetInstaller);
  968. add_child(asset_installer);
  969. asset_installer->open(p_file);
  970. }
  971. void EditorAssetLibrary::_asset_open() {
  972. asset_open->popup_centered_ratio();
  973. }
  974. void EditorAssetLibrary::_manage_plugins() {
  975. ProjectSettings::get_singleton()->popup_project_settings();
  976. ProjectSettings::get_singleton()->set_plugins_page();
  977. }
  978. void EditorAssetLibrary::_install_external_asset(String p_zip_path, String p_title) {
  979. emit_signal("install_asset", p_zip_path, p_title);
  980. }
  981. void EditorAssetLibrary::_bind_methods() {
  982. ObjectTypeDB::bind_method("_http_request_completed", &EditorAssetLibrary::_http_request_completed);
  983. ObjectTypeDB::bind_method("_select_asset", &EditorAssetLibrary::_select_asset);
  984. ObjectTypeDB::bind_method("_select_author", &EditorAssetLibrary::_select_author);
  985. ObjectTypeDB::bind_method("_select_category", &EditorAssetLibrary::_select_category);
  986. ObjectTypeDB::bind_method("_image_request_completed", &EditorAssetLibrary::_image_request_completed);
  987. ObjectTypeDB::bind_method("_search", &EditorAssetLibrary::_search, DEFVAL(0));
  988. ObjectTypeDB::bind_method("_install_asset", &EditorAssetLibrary::_install_asset);
  989. ObjectTypeDB::bind_method("_manage_plugins", &EditorAssetLibrary::_manage_plugins);
  990. ObjectTypeDB::bind_method("_asset_open", &EditorAssetLibrary::_asset_open);
  991. ObjectTypeDB::bind_method("_asset_file_selected", &EditorAssetLibrary::_asset_file_selected);
  992. ObjectTypeDB::bind_method("_repository_changed", &EditorAssetLibrary::_repository_changed);
  993. ObjectTypeDB::bind_method("_support_toggled", &EditorAssetLibrary::_support_toggled);
  994. ObjectTypeDB::bind_method("_rerun_search", &EditorAssetLibrary::_rerun_search);
  995. ObjectTypeDB::bind_method("_install_external_asset", &EditorAssetLibrary::_install_external_asset);
  996. ADD_SIGNAL(MethodInfo("install_asset", PropertyInfo(Variant::STRING, "zip_path"), PropertyInfo(Variant::STRING, "name")));
  997. }
  998. EditorAssetLibrary::EditorAssetLibrary(bool p_templates_only) {
  999. templates_only = p_templates_only;
  1000. Ref<StyleBoxEmpty> border;
  1001. border.instance();
  1002. border->set_default_margin(MARGIN_LEFT, 15);
  1003. border->set_default_margin(MARGIN_RIGHT, 15);
  1004. border->set_default_margin(MARGIN_BOTTOM, 5);
  1005. border->set_default_margin(MARGIN_TOP, 5);
  1006. add_style_override("panel", border);
  1007. VBoxContainer *library_main = memnew(VBoxContainer);
  1008. add_child(library_main);
  1009. HBoxContainer *search_hb = memnew(HBoxContainer);
  1010. library_main->add_child(search_hb);
  1011. library_main->add_constant_override("separation", 10);
  1012. search_hb->add_child(memnew(Label(TTR("Search:") + " ")));
  1013. filter = memnew(LineEdit);
  1014. search_hb->add_child(filter);
  1015. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  1016. filter->connect("text_entered", this, "_search");
  1017. search = memnew(Button(TTR("Search")));
  1018. search->connect("pressed", this, "_search");
  1019. search_hb->add_child(search);
  1020. if (!p_templates_only)
  1021. search_hb->add_child(memnew(VSeparator));
  1022. Button *open_asset = memnew(Button);
  1023. open_asset->set_text(TTR("Import"));
  1024. search_hb->add_child(open_asset);
  1025. open_asset->connect("pressed", this, "_asset_open");
  1026. Button *plugins = memnew(Button);
  1027. plugins->set_text(TTR("Plugins"));
  1028. search_hb->add_child(plugins);
  1029. plugins->connect("pressed", this, "_manage_plugins");
  1030. if (p_templates_only) {
  1031. open_asset->hide();
  1032. plugins->hide();
  1033. }
  1034. HBoxContainer *search_hb2 = memnew(HBoxContainer);
  1035. library_main->add_child(search_hb2);
  1036. search_hb2->add_child(memnew(Label(TTR("Sort:") + " ")));
  1037. sort = memnew(OptionButton);
  1038. for (int i = 0; i < SORT_MAX; i++) {
  1039. sort->add_item(sort_text[i]);
  1040. }
  1041. search_hb2->add_child(sort);
  1042. sort->set_h_size_flags(SIZE_EXPAND_FILL);
  1043. sort->connect("item_selected", this, "_rerun_search");
  1044. reverse = memnew(ToolButton);
  1045. reverse->set_toggle_mode(true);
  1046. reverse->connect("toggled", this, "_rerun_search");
  1047. //reverse->set_text(TTR("Reverse"));
  1048. search_hb2->add_child(reverse);
  1049. search_hb2->add_child(memnew(VSeparator));
  1050. //search_hb2->add_spacer();
  1051. search_hb2->add_child(memnew(Label(TTR("Category:") + " ")));
  1052. categories = memnew(OptionButton);
  1053. categories->add_item(TTR("All"));
  1054. search_hb2->add_child(categories);
  1055. categories->set_h_size_flags(SIZE_EXPAND_FILL);
  1056. //search_hb2->add_spacer();
  1057. categories->connect("item_selected", this, "_rerun_search");
  1058. search_hb2->add_child(memnew(VSeparator));
  1059. search_hb2->add_child(memnew(Label(TTR("Site:") + " ")));
  1060. repository = memnew(OptionButton);
  1061. repository->add_item("godotengine.org");
  1062. repository->set_item_metadata(0, "https://godotengine.org/asset-library/api");
  1063. repository->add_item("localhost"); // TODO: Maybe remove?
  1064. repository->set_item_metadata(1, "http://127.0.0.1/asset-library/api");
  1065. repository->connect("item_selected", this, "_repository_changed");
  1066. search_hb2->add_child(repository);
  1067. repository->set_h_size_flags(SIZE_EXPAND_FILL);
  1068. search_hb2->add_child(memnew(VSeparator));
  1069. support = memnew(MenuButton);
  1070. search_hb2->add_child(support);
  1071. support->set_text(TTR("Support.."));
  1072. support->get_popup()->add_check_item(TTR("Official"), SUPPORT_OFFICIAL);
  1073. support->get_popup()->add_check_item(TTR("Community"), SUPPORT_COMMUNITY);
  1074. support->get_popup()->add_check_item(TTR("Testing"), SUPPORT_TESTING);
  1075. support->get_popup()->set_item_checked(SUPPORT_OFFICIAL, true);
  1076. support->get_popup()->set_item_checked(SUPPORT_COMMUNITY, true);
  1077. support->get_popup()->connect("item_pressed", this, "_support_toggled");
  1078. /////////
  1079. PanelContainer *library_scroll_bg = memnew(PanelContainer);
  1080. library_main->add_child(library_scroll_bg);
  1081. library_scroll_bg->add_style_override("panel", get_stylebox("normal", "TextEdit"));
  1082. library_scroll_bg->set_v_size_flags(SIZE_EXPAND_FILL);
  1083. library_scroll = memnew(ScrollContainer);
  1084. library_scroll->set_enable_v_scroll(true);
  1085. library_scroll->set_enable_h_scroll(false);
  1086. library_scroll_bg->add_child(library_scroll);
  1087. Ref<StyleBoxEmpty> border2;
  1088. border2.instance();
  1089. border2->set_default_margin(MARGIN_LEFT, 15);
  1090. border2->set_default_margin(MARGIN_RIGHT, 35);
  1091. border2->set_default_margin(MARGIN_BOTTOM, 15);
  1092. border2->set_default_margin(MARGIN_TOP, 15);
  1093. PanelContainer *library_vb_border = memnew(PanelContainer);
  1094. library_scroll->add_child(library_vb_border);
  1095. library_vb_border->add_style_override("panel", border2);
  1096. library_vb_border->set_h_size_flags(SIZE_EXPAND_FILL);
  1097. library_vb_border->set_stop_mouse(false);
  1098. library_vb = memnew(VBoxContainer);
  1099. library_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  1100. library_vb_border->add_child(library_vb);
  1101. // margin_panel->set_stop_mouse(false);
  1102. asset_top_page = memnew(HBoxContainer);
  1103. library_vb->add_child(asset_top_page);
  1104. asset_items = memnew(GridContainer);
  1105. asset_items->set_columns(2);
  1106. asset_items->add_constant_override("hseparation", 10);
  1107. asset_items->add_constant_override("vseparation", 10);
  1108. library_vb->add_child(asset_items);
  1109. asset_bottom_page = memnew(HBoxContainer);
  1110. library_vb->add_child(asset_bottom_page);
  1111. request = memnew(HTTPRequest);
  1112. add_child(request);
  1113. request->set_use_threads(EDITOR_DEF("asset_library/use_threads", true));
  1114. request->connect("request_completed", this, "_http_request_completed");
  1115. last_queue_id = 0;
  1116. library_vb->add_constant_override("separation", 20);
  1117. load_status = memnew(ProgressBar);
  1118. load_status->set_min(0);
  1119. load_status->set_max(1);
  1120. load_status->set_step(0.001);
  1121. library_main->add_child(load_status);
  1122. error_hb = memnew(HBoxContainer);
  1123. library_main->add_child(error_hb);
  1124. error_label = memnew(Label);
  1125. error_label->add_color_override("color", Color(1, 0.4, 0.3));
  1126. error_hb->add_child(error_label);
  1127. description = NULL;
  1128. set_process(true);
  1129. downloads_scroll = memnew(ScrollContainer);
  1130. downloads_scroll->set_enable_h_scroll(true);
  1131. downloads_scroll->set_enable_v_scroll(false);
  1132. library_main->add_child(downloads_scroll);
  1133. downloads_hb = memnew(HBoxContainer);
  1134. downloads_scroll->add_child(downloads_hb);
  1135. asset_open = memnew(EditorFileDialog);
  1136. asset_open->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  1137. asset_open->add_filter("*.zip ; " + TTR("Assets ZIP File"));
  1138. asset_open->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  1139. add_child(asset_open);
  1140. asset_open->connect("file_selected", this, "_asset_file_selected");
  1141. asset_installer = NULL;
  1142. }
  1143. ///////
  1144. void AssetLibraryEditorPlugin::make_visible(bool p_visible) {
  1145. if (p_visible) {
  1146. addon_library->show();
  1147. } else {
  1148. addon_library->hide();
  1149. }
  1150. }
  1151. AssetLibraryEditorPlugin::AssetLibraryEditorPlugin(EditorNode *p_node) {
  1152. editor = p_node;
  1153. addon_library = memnew(EditorAssetLibrary);
  1154. addon_library->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1155. editor->get_viewport()->add_child(addon_library);
  1156. addon_library->set_area_as_parent_rect();
  1157. addon_library->hide();
  1158. }
  1159. AssetLibraryEditorPlugin::~AssetLibraryEditorPlugin() {
  1160. }