asset_library_editor_plugin.cpp 46 KB

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