asset_library_editor_plugin.cpp 47 KB

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