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