asset_library_editor_plugin.cpp 50 KB

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