Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/asset_library/asset_library_editor_plugin.cpp
9898 views
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) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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/io/stream_peer_tls.h"
35
#include "core/os/keyboard.h"
36
#include "core/version.h"
37
#include "editor/editor_main_screen.h"
38
#include "editor/editor_node.h"
39
#include "editor/editor_string_names.h"
40
#include "editor/file_system/editor_paths.h"
41
#include "editor/gui/editor_file_dialog.h"
42
#include "editor/settings/editor_settings.h"
43
#include "editor/settings/project_settings_editor.h"
44
#include "editor/themes/editor_scale.h"
45
#include "scene/gui/menu_button.h"
46
#include "scene/gui/separator.h"
47
#include "scene/resources/image_texture.h"
48
49
static inline void setup_http_request(HTTPRequest *request) {
50
request->set_use_threads(EDITOR_GET("asset_library/use_threads"));
51
52
const String proxy_host = EDITOR_GET("network/http_proxy/host");
53
const int proxy_port = EDITOR_GET("network/http_proxy/port");
54
request->set_http_proxy(proxy_host, proxy_port);
55
request->set_https_proxy(proxy_host, proxy_port);
56
}
57
58
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) {
59
title_text = p_title;
60
title->set_text(title_text);
61
title->set_tooltip_text(title_text);
62
asset_id = p_asset_id;
63
category->set_text(p_category);
64
category_id = p_category_id;
65
author->set_text(p_author);
66
author_id = p_author_id;
67
price->set_text(p_cost);
68
}
69
70
// TODO: Refactor this method to use the TextServer.
71
void EditorAssetLibraryItem::clamp_width(int p_max_width) {
72
int text_pixel_width = title->get_button_font()->get_string_size(title_text).x * EDSCALE;
73
74
if (text_pixel_width > p_max_width) {
75
// Truncate title text to within the current column width.
76
int max_length = p_max_width / (text_pixel_width / title_text.length());
77
String truncated_text = title_text.left(max_length - 3) + "...";
78
title->set_text(truncated_text);
79
} else {
80
title->set_text(title_text);
81
}
82
}
83
84
void EditorAssetLibraryItem::set_image(int p_type, int p_index, const Ref<Texture2D> &p_image) {
85
ERR_FAIL_COND(p_type != EditorAssetLibrary::IMAGE_QUEUE_ICON);
86
ERR_FAIL_COND(p_index != 0);
87
88
icon->set_texture_normal(p_image);
89
}
90
91
void EditorAssetLibraryItem::_notification(int p_what) {
92
switch (p_what) {
93
case NOTIFICATION_ENTER_TREE: {
94
icon->set_texture_normal(get_editor_theme_icon(SNAME("ProjectIconLoading")));
95
category->add_theme_color_override(SceneStringName(font_color), Color(0.5, 0.5, 0.5));
96
author->add_theme_color_override(SceneStringName(font_color), Color(0.5, 0.5, 0.5));
97
price->add_theme_color_override(SceneStringName(font_color), Color(0.5, 0.5, 0.5));
98
99
if (author->get_default_cursor_shape() == CURSOR_ARROW) {
100
// Disable visible feedback if author link isn't clickable.
101
author->add_theme_color_override("font_pressed_color", Color(0.5, 0.5, 0.5));
102
author->add_theme_color_override("font_hover_color", Color(0.5, 0.5, 0.5));
103
}
104
} break;
105
}
106
}
107
108
void EditorAssetLibraryItem::_asset_clicked() {
109
emit_signal(SNAME("asset_selected"), asset_id);
110
}
111
112
void EditorAssetLibraryItem::_category_clicked() {
113
emit_signal(SNAME("category_selected"), category_id);
114
}
115
116
void EditorAssetLibraryItem::_author_clicked() {
117
emit_signal(SNAME("author_selected"), author->get_text());
118
}
119
120
void EditorAssetLibraryItem::_bind_methods() {
121
ClassDB::bind_method("set_image", &EditorAssetLibraryItem::set_image);
122
ADD_SIGNAL(MethodInfo("asset_selected"));
123
ADD_SIGNAL(MethodInfo("category_selected"));
124
ADD_SIGNAL(MethodInfo("author_selected"));
125
}
126
127
EditorAssetLibraryItem::EditorAssetLibraryItem(bool p_clickable) {
128
Ref<StyleBoxEmpty> border;
129
border.instantiate();
130
border->set_content_margin_all(5 * EDSCALE);
131
add_theme_style_override(SceneStringName(panel), border);
132
133
HBoxContainer *hb = memnew(HBoxContainer);
134
// Add some spacing to visually separate the icon from the asset details.
135
hb->add_theme_constant_override("separation", 15 * EDSCALE);
136
add_child(hb);
137
138
icon = memnew(TextureButton);
139
icon->set_accessibility_name(TTRC("Open asset details"));
140
icon->set_custom_minimum_size(Size2(64, 64) * EDSCALE);
141
hb->add_child(icon);
142
143
VBoxContainer *vb = memnew(VBoxContainer);
144
145
hb->add_child(vb);
146
vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
147
148
title = memnew(LinkButton);
149
title->set_accessibility_name(TTRC("Title"));
150
title->set_auto_translate_mode(AutoTranslateMode::AUTO_TRANSLATE_MODE_DISABLED);
151
title->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
152
vb->add_child(title);
153
154
category = memnew(LinkButton);
155
category->set_accessibility_name(TTRC("Category"));
156
category->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
157
vb->add_child(category);
158
159
HBoxContainer *author_price_hbox = memnew(HBoxContainer);
160
author_price_hbox->add_theme_constant_override("separation", 5 * EDSCALE);
161
vb->add_child(author_price_hbox);
162
163
author = memnew(LinkButton);
164
author->set_tooltip_text(TTRC("Author"));
165
author->set_accessibility_name(TTRC("Author"));
166
author_price_hbox->add_child(author);
167
168
author_price_hbox->add_child(memnew(HSeparator));
169
170
if (p_clickable) {
171
author->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
172
icon->set_default_cursor_shape(CURSOR_POINTING_HAND);
173
icon->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibraryItem::_asset_clicked));
174
title->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibraryItem::_asset_clicked));
175
category->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibraryItem::_category_clicked));
176
author->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibraryItem::_author_clicked));
177
} else {
178
title->set_mouse_filter(MOUSE_FILTER_IGNORE);
179
category->set_mouse_filter(MOUSE_FILTER_IGNORE);
180
author->set_underline_mode(LinkButton::UNDERLINE_MODE_NEVER);
181
author->set_default_cursor_shape(CURSOR_ARROW);
182
}
183
184
Ref<StyleBoxEmpty> label_margin;
185
label_margin.instantiate();
186
label_margin->set_content_margin_all(0);
187
188
price = memnew(Label);
189
price->set_focus_mode(FOCUS_ACCESSIBILITY);
190
price->add_theme_style_override(CoreStringName(normal), label_margin);
191
price->set_tooltip_text(TTRC("License"));
192
price->set_accessibility_name(TTRC("License"));
193
price->set_mouse_filter(MOUSE_FILTER_PASS);
194
195
author_price_hbox->add_child(price);
196
197
set_custom_minimum_size(Size2(250, 80) * EDSCALE);
198
set_h_size_flags(Control::SIZE_EXPAND_FILL);
199
}
200
201
//////////////////////////////////////////////////////////////////////////////
202
203
void EditorAssetLibraryItemDescription::set_image(int p_type, int p_index, const Ref<Texture2D> &p_image) {
204
switch (p_type) {
205
case EditorAssetLibrary::IMAGE_QUEUE_ICON: {
206
item->call("set_image", p_type, p_index, p_image);
207
icon = p_image;
208
} break;
209
case EditorAssetLibrary::IMAGE_QUEUE_THUMBNAIL: {
210
for (int i = 0; i < preview_images.size(); i++) {
211
if (preview_images[i].id == p_index) {
212
if (preview_images[i].is_video) {
213
Ref<Image> overlay = previews->get_editor_theme_icon(SNAME("PlayOverlay"))->get_image();
214
Ref<Image> thumbnail = p_image->get_image();
215
thumbnail = thumbnail->duplicate();
216
Point2i overlay_pos = Point2i((thumbnail->get_width() - overlay->get_width()) / 2, (thumbnail->get_height() - overlay->get_height()) / 2);
217
218
// Overlay and thumbnail need the same format for `blend_rect` to work.
219
thumbnail->convert(Image::FORMAT_RGBA8);
220
thumbnail->blend_rect(overlay, overlay->get_used_rect(), overlay_pos);
221
preview_images[i].button->set_button_icon(ImageTexture::create_from_image(thumbnail));
222
223
// Make it clearer that clicking it will open an external link
224
preview_images[i].button->set_default_cursor_shape(Control::CURSOR_POINTING_HAND);
225
} else {
226
preview_images[i].button->set_button_icon(p_image);
227
}
228
break;
229
}
230
}
231
} break;
232
case EditorAssetLibrary::IMAGE_QUEUE_SCREENSHOT: {
233
for (int i = 0; i < preview_images.size(); i++) {
234
if (preview_images[i].id == p_index) {
235
preview_images.write[i].image = p_image;
236
if (preview_images[i].button->is_pressed()) {
237
_preview_click(p_index);
238
}
239
break;
240
}
241
}
242
} break;
243
}
244
}
245
246
void EditorAssetLibraryItemDescription::_notification(int p_what) {
247
switch (p_what) {
248
case NOTIFICATION_THEME_CHANGED: {
249
previews_bg->add_theme_style_override(SceneStringName(panel), previews->get_theme_stylebox(CoreStringName(normal), SNAME("TextEdit")));
250
} break;
251
}
252
}
253
254
void EditorAssetLibraryItemDescription::_bind_methods() {
255
ClassDB::bind_method(D_METHOD("set_image"), &EditorAssetLibraryItemDescription::set_image);
256
}
257
258
void EditorAssetLibraryItemDescription::_link_click(const String &p_url) {
259
ERR_FAIL_COND(!p_url.begins_with("http"));
260
OS::get_singleton()->shell_open(p_url);
261
}
262
263
void EditorAssetLibraryItemDescription::_preview_click(int p_id) {
264
for (int i = 0; i < preview_images.size(); i++) {
265
if (preview_images[i].id == p_id) {
266
preview_images[i].button->set_pressed(true);
267
if (!preview_images[i].is_video) {
268
if (preview_images[i].image.is_valid()) {
269
preview->set_texture(preview_images[i].image);
270
child_controls_changed();
271
}
272
} else {
273
_link_click(preview_images[i].video_link);
274
}
275
} else {
276
preview_images[i].button->set_pressed(false);
277
}
278
}
279
}
280
281
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) {
282
asset_id = p_asset_id;
283
title = p_title;
284
download_url = p_download_url;
285
sha256 = p_sha256_hash;
286
item->configure(p_title, p_asset_id, p_category, p_category_id, p_author, p_author_id, p_cost);
287
description->clear();
288
description->add_text(TTR("Version:") + " " + p_version_string + "\n");
289
description->add_text(TTR("Contents:") + " ");
290
description->push_meta(p_browse_url);
291
description->add_text(TTR("View Files"));
292
description->pop();
293
description->add_text("\n" + TTR("Description:") + "\n\n");
294
description->append_text(p_description);
295
description->set_selection_enabled(true);
296
description->set_context_menu_enabled(true);
297
set_title(p_title);
298
}
299
300
void EditorAssetLibraryItemDescription::add_preview(int p_id, bool p_video, const String &p_url) {
301
if (preview_images.is_empty()) {
302
previews_vbox->show();
303
}
304
305
Preview new_preview;
306
new_preview.id = p_id;
307
new_preview.video_link = p_url;
308
new_preview.is_video = p_video;
309
new_preview.button = memnew(Button);
310
new_preview.button->set_button_icon(previews->get_editor_theme_icon(SNAME("ThumbnailWait")));
311
new_preview.button->set_toggle_mode(true);
312
new_preview.button->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibraryItemDescription::_preview_click).bind(p_id));
313
preview_hb->add_child(new_preview.button);
314
if (!p_video) {
315
new_preview.image = previews->get_editor_theme_icon(SNAME("ThumbnailWait"));
316
}
317
preview_images.push_back(new_preview);
318
if (preview_images.size() == 1 && !p_video) {
319
_preview_click(p_id);
320
}
321
}
322
323
EditorAssetLibraryItemDescription::EditorAssetLibraryItemDescription() {
324
HBoxContainer *hbox = memnew(HBoxContainer);
325
add_child(hbox);
326
VBoxContainer *desc_vbox = memnew(VBoxContainer);
327
hbox->add_child(desc_vbox);
328
hbox->add_theme_constant_override("separation", 15 * EDSCALE);
329
330
item = memnew(EditorAssetLibraryItem);
331
332
desc_vbox->add_child(item);
333
desc_vbox->set_custom_minimum_size(Size2(440 * EDSCALE, 440 * EDSCALE));
334
335
description = memnew(RichTextLabel);
336
desc_vbox->add_child(description);
337
description->set_v_size_flags(Control::SIZE_EXPAND_FILL);
338
description->connect("meta_clicked", callable_mp(this, &EditorAssetLibraryItemDescription::_link_click));
339
description->add_theme_constant_override(SceneStringName(line_separation), Math::round(5 * EDSCALE));
340
341
previews_vbox = memnew(VBoxContainer);
342
previews_vbox->hide(); // Will be shown if we add any previews later.
343
344
hbox->add_child(previews_vbox);
345
previews_vbox->add_theme_constant_override("separation", 15 * EDSCALE);
346
previews_vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
347
previews_vbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
348
349
preview = memnew(TextureRect);
350
previews_vbox->add_child(preview);
351
preview->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
352
preview->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
353
preview->set_custom_minimum_size(Size2(640 * EDSCALE, 345 * EDSCALE));
354
preview->set_v_size_flags(Control::SIZE_EXPAND_FILL);
355
preview->set_h_size_flags(Control::SIZE_EXPAND_FILL);
356
357
previews_bg = memnew(PanelContainer);
358
previews_vbox->add_child(previews_bg);
359
previews_bg->set_custom_minimum_size(Size2(640 * EDSCALE, 101 * EDSCALE));
360
361
previews = memnew(ScrollContainer);
362
previews_bg->add_child(previews);
363
previews->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
364
preview_hb = memnew(HBoxContainer);
365
preview_hb->set_v_size_flags(Control::SIZE_EXPAND_FILL);
366
367
previews->add_child(preview_hb);
368
set_ok_button_text(TTRC("Download"));
369
set_cancel_button_text(TTRC("Close"));
370
}
371
372
///////////////////////////////////////////////////////////////////////////////////
373
374
void EditorAssetLibraryItemDownload::_http_download_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data) {
375
String error_text;
376
377
switch (p_status) {
378
case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH:
379
case HTTPRequest::RESULT_CONNECTION_ERROR:
380
case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED: {
381
error_text = TTR("Connection error, please try again.");
382
status->set_text(TTRC("Can't connect."));
383
} break;
384
case HTTPRequest::RESULT_CANT_CONNECT:
385
case HTTPRequest::RESULT_TLS_HANDSHAKE_ERROR: {
386
error_text = TTR("Can't connect to host:") + " " + host;
387
status->set_text(TTRC("Can't connect."));
388
} break;
389
case HTTPRequest::RESULT_NO_RESPONSE: {
390
error_text = TTR("No response from host:") + " " + host;
391
status->set_text(TTRC("No response."));
392
} break;
393
case HTTPRequest::RESULT_CANT_RESOLVE: {
394
error_text = TTR("Can't resolve hostname:") + " " + host;
395
status->set_text(TTRC("Can't resolve."));
396
} break;
397
case HTTPRequest::RESULT_REQUEST_FAILED: {
398
error_text = TTR("Request failed, return code:") + " " + itos(p_code);
399
status->set_text(TTRC("Request failed."));
400
} break;
401
case HTTPRequest::RESULT_DOWNLOAD_FILE_CANT_OPEN:
402
case HTTPRequest::RESULT_DOWNLOAD_FILE_WRITE_ERROR: {
403
error_text = TTR("Cannot save response to:") + " " + download->get_download_file();
404
status->set_text(TTRC("Write error."));
405
} break;
406
case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: {
407
error_text = TTR("Request failed, too many redirects");
408
status->set_text(TTRC("Redirect loop."));
409
} break;
410
case HTTPRequest::RESULT_TIMEOUT: {
411
error_text = TTR("Request failed, timeout");
412
status->set_text(TTRC("Timeout."));
413
} break;
414
default: {
415
if (p_code != 200) {
416
error_text = TTR("Request failed, return code:") + " " + itos(p_code);
417
status->set_text(TTR("Failed:") + " " + itos(p_code));
418
} else if (!sha256.is_empty()) {
419
String download_sha256 = FileAccess::get_sha256(download->get_download_file());
420
if (sha256 != download_sha256) {
421
error_text = TTR("Bad download hash, assuming file has been tampered with.") + "\n";
422
error_text += TTR("Expected:") + " " + sha256 + "\n" + TTR("Got:") + " " + download_sha256;
423
status->set_text(TTRC("Failed SHA-256 hash check"));
424
}
425
}
426
} break;
427
}
428
429
// Make the progress bar invisible but don't reflow other Controls around it.
430
progress->set_modulate(Color(0, 0, 0, 0));
431
progress->set_indeterminate(false);
432
433
if (!error_text.is_empty()) {
434
download_error->set_text(TTR("Asset Download Error:") + "\n" + error_text);
435
download_error->popup_centered();
436
// Let the user retry the download.
437
retry_button->show();
438
return;
439
}
440
441
install_button->set_disabled(false);
442
status->set_text(TTRC("Ready to install!"));
443
444
set_process(false);
445
446
// Automatically prompt for installation once the download is completed.
447
install();
448
}
449
450
void EditorAssetLibraryItemDownload::configure(const String &p_title, int p_asset_id, const Ref<Texture2D> &p_preview, const String &p_download_url, const String &p_sha256_hash) {
451
title->set_text(p_title);
452
icon->set_texture(p_preview);
453
asset_id = p_asset_id;
454
if (p_preview.is_null()) {
455
icon->set_texture(get_editor_theme_icon(SNAME("FileBrokenBigThumb")));
456
}
457
host = p_download_url;
458
sha256 = p_sha256_hash;
459
_make_request();
460
}
461
462
void EditorAssetLibraryItemDownload::_notification(int p_what) {
463
switch (p_what) {
464
case NOTIFICATION_THEME_CHANGED: {
465
panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("AssetLib")));
466
status->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("status_color"), SNAME("AssetLib")));
467
dismiss_button->set_texture_normal(get_theme_icon(SNAME("dismiss"), SNAME("AssetLib")));
468
} break;
469
470
case NOTIFICATION_PROCESS: {
471
// Make the progress bar visible again when retrying the download.
472
progress->set_modulate(Color(1, 1, 1, 1));
473
474
if (download->get_downloaded_bytes() > 0) {
475
progress->set_max(download->get_body_size());
476
progress->set_value(download->get_downloaded_bytes());
477
}
478
479
int cstatus = download->get_http_client_status();
480
481
if (cstatus == HTTPClient::STATUS_BODY) {
482
if (download->get_body_size() > 0) {
483
progress->set_indeterminate(false);
484
status->set_text(vformat(
485
TTR("Downloading (%s / %s)..."),
486
String::humanize_size(download->get_downloaded_bytes()),
487
String::humanize_size(download->get_body_size())));
488
} else {
489
progress->set_indeterminate(true);
490
status->set_text(vformat(
491
TTR("Downloading...") + " (%s)",
492
String::humanize_size(download->get_downloaded_bytes())));
493
}
494
}
495
496
if (cstatus != prev_status) {
497
switch (cstatus) {
498
case HTTPClient::STATUS_RESOLVING: {
499
status->set_text(TTRC("Resolving..."));
500
progress->set_max(1);
501
progress->set_value(0);
502
} break;
503
case HTTPClient::STATUS_CONNECTING: {
504
status->set_text(TTRC("Connecting..."));
505
progress->set_max(1);
506
progress->set_value(0);
507
} break;
508
case HTTPClient::STATUS_REQUESTING: {
509
status->set_text(TTRC("Requesting..."));
510
progress->set_max(1);
511
progress->set_value(0);
512
} break;
513
default: {
514
}
515
}
516
prev_status = cstatus;
517
}
518
} break;
519
}
520
}
521
522
void EditorAssetLibraryItemDownload::_close() {
523
// Clean up downloaded file.
524
DirAccess::remove_file_or_error(download->get_download_file());
525
queue_free();
526
}
527
528
bool EditorAssetLibraryItemDownload::can_install() const {
529
return !install_button->is_disabled();
530
}
531
532
void EditorAssetLibraryItemDownload::install() {
533
String file = download->get_download_file();
534
535
if (external_install) {
536
emit_signal(SNAME("install_asset"), file, title->get_text());
537
return;
538
}
539
540
asset_installer->set_asset_name(title->get_text());
541
asset_installer->open_asset(file, true);
542
}
543
544
void EditorAssetLibraryItemDownload::_make_request() {
545
// Hide the Retry button if we've just pressed it.
546
retry_button->hide();
547
548
download->cancel_request();
549
download->set_download_file(EditorPaths::get_singleton()->get_cache_dir().path_join("tmp_asset_" + itos(asset_id)) + ".zip");
550
551
Error err = download->request(host);
552
if (err != OK) {
553
status->set_text(TTRC("Error making request"));
554
} else {
555
progress->set_indeterminate(true);
556
set_process(true);
557
}
558
}
559
560
void EditorAssetLibraryItemDownload::_bind_methods() {
561
ADD_SIGNAL(MethodInfo("install_asset", PropertyInfo(Variant::STRING, "zip_path"), PropertyInfo(Variant::STRING, "name")));
562
}
563
564
EditorAssetLibraryItemDownload::EditorAssetLibraryItemDownload() {
565
panel = memnew(PanelContainer);
566
add_child(panel);
567
568
HBoxContainer *hb = memnew(HBoxContainer);
569
panel->add_child(hb);
570
icon = memnew(TextureRect);
571
icon->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
572
icon->set_v_size_flags(0);
573
hb->add_child(icon);
574
575
VBoxContainer *vb = memnew(VBoxContainer);
576
hb->add_child(vb);
577
vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
578
579
HBoxContainer *title_hb = memnew(HBoxContainer);
580
vb->add_child(title_hb);
581
title = memnew(Label);
582
title->set_focus_mode(FOCUS_ACCESSIBILITY);
583
title_hb->add_child(title);
584
title->set_h_size_flags(Control::SIZE_EXPAND_FILL);
585
586
dismiss_button = memnew(TextureButton);
587
dismiss_button->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibraryItemDownload::_close));
588
dismiss_button->set_accessibility_name(TTRC("Close"));
589
title_hb->add_child(dismiss_button);
590
591
title->set_clip_text(true);
592
593
vb->add_spacer();
594
595
status = memnew(Label(TTRC("Idle")));
596
vb->add_child(status);
597
progress = memnew(ProgressBar);
598
progress->set_editor_preview_indeterminate(true);
599
vb->add_child(progress);
600
601
HBoxContainer *hb2 = memnew(HBoxContainer);
602
vb->add_child(hb2);
603
hb2->add_spacer();
604
605
install_button = memnew(Button);
606
install_button->set_text(TTRC("Install..."));
607
install_button->set_disabled(true);
608
install_button->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibraryItemDownload::install));
609
610
retry_button = memnew(Button);
611
retry_button->set_text(TTRC("Retry"));
612
retry_button->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibraryItemDownload::_make_request));
613
// Only show the Retry button in case of a failure.
614
retry_button->hide();
615
616
hb2->add_child(retry_button);
617
hb2->add_child(install_button);
618
set_custom_minimum_size(Size2(310, 0) * EDSCALE);
619
620
download = memnew(HTTPRequest);
621
panel->add_child(download);
622
download->connect("request_completed", callable_mp(this, &EditorAssetLibraryItemDownload::_http_download_completed));
623
setup_http_request(download);
624
625
download_error = memnew(AcceptDialog);
626
panel->add_child(download_error);
627
download_error->set_title(TTRC("Download Error"));
628
629
asset_installer = memnew(EditorAssetInstaller);
630
panel->add_child(asset_installer);
631
asset_installer->connect(SceneStringName(confirmed), callable_mp(this, &EditorAssetLibraryItemDownload::_close));
632
633
prev_status = -1;
634
635
external_install = false;
636
}
637
638
////////////////////////////////////////////////////////////////////////////////
639
void EditorAssetLibrary::_notification(int p_what) {
640
switch (p_what) {
641
case NOTIFICATION_READY: {
642
add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("bg"), SNAME("AssetLib")));
643
error_label->move_to_front();
644
} break;
645
646
case NOTIFICATION_TRANSLATION_CHANGED: {
647
if (!initial_loading) {
648
_rerun_search(-1);
649
}
650
} break;
651
652
case NOTIFICATION_THEME_CHANGED: {
653
error_tr->set_texture(get_editor_theme_icon(SNAME("Error")));
654
filter->set_right_icon(get_editor_theme_icon(SNAME("Search")));
655
library_scroll_bg->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
656
downloads_scroll->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
657
error_label->add_theme_color_override("color", get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
658
} break;
659
660
case NOTIFICATION_VISIBILITY_CHANGED: {
661
if (is_visible()) {
662
#ifndef ANDROID_ENABLED
663
// Focus the search box automatically when switching to the Templates tab (in the Project Manager)
664
// or switching to the AssetLib tab (in the editor).
665
// The Project Manager's project filter box is automatically focused in the project manager code.
666
filter->grab_focus();
667
#endif
668
669
if (initial_loading) {
670
_repository_changed(0); // Update when shown for the first time.
671
}
672
}
673
} break;
674
675
case NOTIFICATION_PROCESS: {
676
HTTPClient::Status s = request->get_http_client_status();
677
const bool loading = s != HTTPClient::STATUS_DISCONNECTED;
678
679
if (loading) {
680
library_scroll->set_modulate(Color(1, 1, 1, 0.5));
681
} else {
682
library_scroll->set_modulate(Color(1, 1, 1, 1));
683
}
684
685
const bool no_downloads = downloads_hb->get_child_count() == 0;
686
if (no_downloads == downloads_scroll->is_visible()) {
687
downloads_scroll->set_visible(!no_downloads);
688
}
689
690
} break;
691
692
case NOTIFICATION_RESIZED: {
693
_update_asset_items_columns();
694
} break;
695
696
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
697
if (!EditorSettings::get_singleton()->check_changed_settings_in_group("asset_library") &&
698
!EditorSettings::get_singleton()->check_changed_settings_in_group("network")) {
699
break;
700
}
701
702
_update_repository_options();
703
setup_http_request(request);
704
705
const bool loading_blocked_new = ((int)EDITOR_GET("network/connection/network_mode") == EditorSettings::NETWORK_OFFLINE);
706
if (loading_blocked_new != loading_blocked) {
707
loading_blocked = loading_blocked_new;
708
709
if (!loading_blocked && is_visible()) {
710
_request_current_config(); // Reload config now that the network is available.
711
}
712
}
713
} break;
714
}
715
}
716
717
void EditorAssetLibrary::_update_repository_options() {
718
// TODO: Move to editor_settings.cpp
719
Dictionary default_urls;
720
default_urls["godotengine.org (Official)"] = "https://godotengine.org/asset-library/api";
721
Dictionary available_urls = _EDITOR_DEF("asset_library/available_urls", default_urls, true);
722
repository->clear();
723
int i = 0;
724
for (const KeyValue<Variant, Variant> &kv : available_urls) {
725
repository->add_item(kv.key);
726
repository->set_item_metadata(i, kv.value);
727
i++;
728
}
729
}
730
731
void EditorAssetLibrary::shortcut_input(const Ref<InputEvent> &p_event) {
732
ERR_FAIL_COND(p_event.is_null());
733
734
const Ref<InputEventKey> key = p_event;
735
736
if (key.is_valid() && key->is_pressed()) {
737
if (key->is_match(InputEventKey::create_reference(KeyModifierMask::CMD_OR_CTRL | Key::F)) && is_visible_in_tree()) {
738
filter->grab_focus();
739
filter->select_all();
740
accept_event();
741
}
742
}
743
}
744
745
void EditorAssetLibrary::_install_asset() {
746
ERR_FAIL_NULL(description);
747
748
EditorAssetLibraryItemDownload *d = _get_asset_in_progress(description->get_asset_id());
749
if (d) {
750
d->install();
751
return;
752
}
753
754
EditorAssetLibraryItemDownload *download = memnew(EditorAssetLibraryItemDownload);
755
downloads_hb->add_child(download);
756
download->configure(description->get_title(), description->get_asset_id(), description->get_preview_icon(), description->get_download_url(), description->get_sha256());
757
758
if (templates_only) {
759
download->set_external_install(true);
760
download->connect("install_asset", callable_mp(this, &EditorAssetLibrary::_install_external_asset));
761
}
762
}
763
764
const char *EditorAssetLibrary::sort_key[SORT_MAX] = {
765
"updated",
766
"updated",
767
"name",
768
"name",
769
"cost",
770
"cost",
771
};
772
773
const char *EditorAssetLibrary::sort_text[SORT_MAX] = {
774
TTRC("Recently Updated"),
775
TTRC("Least Recently Updated"),
776
TTRC("Name (A-Z)"),
777
TTRC("Name (Z-A)"),
778
TTRC("License (A-Z)"), // "cost" stores the SPDX license name in the Godot Asset Library.
779
TTRC("License (Z-A)"), // "cost" stores the SPDX license name in the Godot Asset Library.
780
};
781
782
const char *EditorAssetLibrary::support_key[SUPPORT_MAX] = {
783
"official", // Former name for the Featured support level (still used on the API backend).
784
"community",
785
"testing",
786
};
787
788
const char *EditorAssetLibrary::support_text[SUPPORT_MAX] = {
789
TTRC("Featured"),
790
TTRC("Community"),
791
TTRC("Testing"),
792
};
793
794
void EditorAssetLibrary::_select_author(const String &p_author) {
795
if (!host.contains("godotengine.org")) {
796
// Don't open the link for alternative repositories.
797
return;
798
}
799
OS::get_singleton()->shell_open("https://godotengine.org/asset-library/asset?user=" + p_author.uri_encode());
800
}
801
802
void EditorAssetLibrary::_select_category(int p_id) {
803
for (int i = 0; i < categories->get_item_count(); i++) {
804
if (i == 0) {
805
continue;
806
}
807
int id = categories->get_item_metadata(i);
808
if (id == p_id) {
809
categories->select(i);
810
_search();
811
break;
812
}
813
}
814
}
815
816
void EditorAssetLibrary::_select_asset(int p_id) {
817
_api_request("asset/" + itos(p_id), REQUESTING_ASSET);
818
}
819
820
void EditorAssetLibrary::_image_update(bool p_use_cache, bool p_final, const PackedByteArray &p_data, int p_queue_id) {
821
Object *obj = ObjectDB::get_instance(image_queue[p_queue_id].target);
822
if (!obj) {
823
return;
824
}
825
826
bool image_set = false;
827
PackedByteArray image_data = p_data;
828
829
if (p_use_cache) {
830
String cache_filename_base = EditorPaths::get_singleton()->get_cache_dir().path_join("assetimage_" + image_queue[p_queue_id].image_url.md5_text());
831
832
Ref<FileAccess> file = FileAccess::open(cache_filename_base + ".data", FileAccess::READ);
833
if (file.is_valid()) {
834
PackedByteArray cached_data;
835
int len = file->get_32();
836
cached_data.resize(len);
837
838
uint8_t *w = cached_data.ptrw();
839
file->get_buffer(w, len);
840
841
image_data = cached_data;
842
}
843
}
844
845
int len = image_data.size();
846
const uint8_t *r = image_data.ptr();
847
Ref<Image> image = memnew(Image);
848
849
uint8_t png_signature[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
850
uint8_t jpg_signature[3] = { 255, 216, 255 };
851
uint8_t webp_signature[4] = { 82, 73, 70, 70 };
852
uint8_t bmp_signature[2] = { 66, 77 };
853
854
if (r) {
855
Ref<Image> parsed_image;
856
857
if ((memcmp(&r[0], &png_signature[0], 8) == 0) && Image::_png_mem_loader_func) {
858
parsed_image = Image::_png_mem_loader_func(r, len);
859
} else if ((memcmp(&r[0], &jpg_signature[0], 3) == 0) && Image::_jpg_mem_loader_func) {
860
parsed_image = Image::_jpg_mem_loader_func(r, len);
861
} else if ((memcmp(&r[0], &webp_signature[0], 4) == 0) && Image::_webp_mem_loader_func) {
862
parsed_image = Image::_webp_mem_loader_func(r, len);
863
} else if ((memcmp(&r[0], &bmp_signature[0], 2) == 0) && Image::_bmp_mem_loader_func) {
864
parsed_image = Image::_bmp_mem_loader_func(r, len);
865
} else if (Image::_svg_scalable_mem_loader_func) {
866
parsed_image = Image::_svg_scalable_mem_loader_func(r, len, 1.0);
867
}
868
869
if (parsed_image.is_null()) {
870
if (is_print_verbose_enabled()) {
871
ERR_PRINT(vformat("Asset Library: Invalid image downloaded from '%s' for asset # %d", image_queue[p_queue_id].image_url, image_queue[p_queue_id].asset_id));
872
}
873
} else {
874
image->copy_internals_from(parsed_image);
875
}
876
}
877
878
if (!image->is_empty()) {
879
switch (image_queue[p_queue_id].image_type) {
880
case IMAGE_QUEUE_ICON:
881
image->resize(64 * EDSCALE, 64 * EDSCALE, Image::INTERPOLATE_LANCZOS);
882
break;
883
884
case IMAGE_QUEUE_THUMBNAIL: {
885
float max_height = 85 * EDSCALE;
886
887
float scale_ratio = max_height / (image->get_height() * EDSCALE);
888
if (scale_ratio < 1) {
889
image->resize(image->get_width() * EDSCALE * scale_ratio, image->get_height() * EDSCALE * scale_ratio, Image::INTERPOLATE_LANCZOS);
890
}
891
} break;
892
893
case IMAGE_QUEUE_SCREENSHOT: {
894
float max_height = 397 * EDSCALE;
895
896
float scale_ratio = max_height / (image->get_height() * EDSCALE);
897
if (scale_ratio < 1) {
898
image->resize(image->get_width() * EDSCALE * scale_ratio, image->get_height() * EDSCALE * scale_ratio, Image::INTERPOLATE_LANCZOS);
899
}
900
} break;
901
}
902
903
Ref<ImageTexture> tex = ImageTexture::create_from_image(image);
904
905
obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, tex);
906
image_set = true;
907
}
908
909
if (!image_set && p_final) {
910
obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, get_editor_theme_icon(SNAME("FileBrokenBigThumb")));
911
}
912
}
913
914
void EditorAssetLibrary::_image_request_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data, int p_queue_id) {
915
ERR_FAIL_COND(!image_queue.has(p_queue_id));
916
917
if (p_status == HTTPRequest::RESULT_SUCCESS && p_code < HTTPClient::RESPONSE_BAD_REQUEST) {
918
if (p_code != HTTPClient::RESPONSE_NOT_MODIFIED) {
919
for (int i = 0; i < headers.size(); i++) {
920
if (headers[i].findn("ETag:") == 0) { // Save etag
921
String cache_filename_base = EditorPaths::get_singleton()->get_cache_dir().path_join("assetimage_" + image_queue[p_queue_id].image_url.md5_text());
922
String new_etag = headers[i].substr(headers[i].find_char(':') + 1).strip_edges();
923
Ref<FileAccess> file = FileAccess::open(cache_filename_base + ".etag", FileAccess::WRITE);
924
if (file.is_valid()) {
925
file->store_line(new_etag);
926
}
927
928
int len = p_data.size();
929
const uint8_t *r = p_data.ptr();
930
file = FileAccess::open(cache_filename_base + ".data", FileAccess::WRITE);
931
if (file.is_valid()) {
932
file->store_32(len);
933
file->store_buffer(r, len);
934
}
935
936
break;
937
}
938
}
939
}
940
_image_update(p_code == HTTPClient::RESPONSE_NOT_MODIFIED, true, p_data, p_queue_id);
941
942
} else {
943
if (is_print_verbose_enabled()) {
944
WARN_PRINT(vformat("Asset Library: Error getting image from '%s' for asset # %d.", image_queue[p_queue_id].image_url, image_queue[p_queue_id].asset_id));
945
}
946
947
Object *obj = ObjectDB::get_instance(image_queue[p_queue_id].target);
948
if (obj) {
949
obj->call("set_image", image_queue[p_queue_id].image_type, image_queue[p_queue_id].image_index, get_editor_theme_icon(SNAME("FileBrokenBigThumb")));
950
}
951
}
952
953
image_queue[p_queue_id].request->queue_free();
954
image_queue.erase(p_queue_id);
955
956
_update_image_queue();
957
}
958
959
void EditorAssetLibrary::_update_image_queue() {
960
const int max_images = 6;
961
int current_images = 0;
962
963
List<int> to_delete;
964
for (KeyValue<int, ImageQueue> &E : image_queue) {
965
if (!E.value.active && current_images < max_images) {
966
String cache_filename_base = EditorPaths::get_singleton()->get_cache_dir().path_join("assetimage_" + E.value.image_url.md5_text());
967
Vector<String> headers;
968
969
if (FileAccess::exists(cache_filename_base + ".etag") && FileAccess::exists(cache_filename_base + ".data")) {
970
Ref<FileAccess> file = FileAccess::open(cache_filename_base + ".etag", FileAccess::READ);
971
if (file.is_valid()) {
972
headers.push_back("If-None-Match: " + file->get_line());
973
}
974
}
975
976
Error err = E.value.request->request(E.value.image_url, headers);
977
if (err != OK) {
978
to_delete.push_back(E.key);
979
} else {
980
E.value.active = true;
981
}
982
current_images++;
983
} else if (E.value.active) {
984
current_images++;
985
}
986
}
987
988
while (to_delete.size()) {
989
image_queue[to_delete.front()->get()].request->queue_free();
990
image_queue.erase(to_delete.front()->get());
991
to_delete.pop_front();
992
}
993
}
994
995
void EditorAssetLibrary::_request_image(ObjectID p_for, int p_asset_id, String p_image_url, ImageType p_type, int p_image_index) {
996
// Remove extra spaces around the URL. This isn't strictly valid, but recoverable.
997
String trimmed_url = p_image_url.strip_edges();
998
if (trimmed_url != p_image_url && is_print_verbose_enabled()) {
999
WARN_PRINT(vformat("Asset Library: Badly formatted image URL '%s' for asset # %d.", p_image_url, p_asset_id));
1000
}
1001
1002
// Validate the image URL first.
1003
{
1004
String url_scheme;
1005
String url_host;
1006
int url_port;
1007
String url_path;
1008
String url_fragment;
1009
Error err = trimmed_url.parse_url(url_scheme, url_host, url_port, url_path, url_fragment);
1010
if (err != OK) {
1011
if (is_print_verbose_enabled()) {
1012
ERR_PRINT(vformat("Asset Library: Invalid image URL '%s' for asset # %d.", trimmed_url, p_asset_id));
1013
}
1014
1015
Object *obj = ObjectDB::get_instance(p_for);
1016
if (obj) {
1017
obj->call("set_image", p_type, p_image_index, get_editor_theme_icon(SNAME("FileBrokenBigThumb")));
1018
}
1019
return;
1020
}
1021
}
1022
1023
ImageQueue iq;
1024
iq.image_url = trimmed_url;
1025
iq.image_index = p_image_index;
1026
iq.image_type = p_type;
1027
iq.request = memnew(HTTPRequest);
1028
setup_http_request(iq.request);
1029
1030
iq.target = p_for;
1031
iq.asset_id = p_asset_id;
1032
iq.queue_id = ++last_queue_id;
1033
iq.active = false;
1034
1035
iq.request->connect("request_completed", callable_mp(this, &EditorAssetLibrary::_image_request_completed).bind(iq.queue_id));
1036
1037
image_queue[iq.queue_id] = iq;
1038
add_child(iq.request);
1039
1040
_image_update(true, false, PackedByteArray(), iq.queue_id);
1041
_update_image_queue();
1042
}
1043
1044
void EditorAssetLibrary::_repository_changed(int p_repository_id) {
1045
_set_library_message(TTRC("Loading..."));
1046
1047
asset_top_page->hide();
1048
asset_bottom_page->hide();
1049
asset_items->hide();
1050
1051
filter->set_editable(false);
1052
sort->set_disabled(true);
1053
categories->set_disabled(true);
1054
support->set_disabled(true);
1055
1056
host = repository->get_item_metadata(p_repository_id);
1057
if (templates_only) {
1058
_api_request("configure", REQUESTING_CONFIG, "?type=project");
1059
} else {
1060
_api_request("configure", REQUESTING_CONFIG);
1061
}
1062
}
1063
1064
void EditorAssetLibrary::_support_toggled(int p_support) {
1065
support->get_popup()->set_item_checked(p_support, !support->get_popup()->is_item_checked(p_support));
1066
_search();
1067
}
1068
1069
void EditorAssetLibrary::_rerun_search(int p_ignore) {
1070
_search();
1071
}
1072
1073
void EditorAssetLibrary::_search(int p_page) {
1074
String args;
1075
1076
if (templates_only) {
1077
args += "?type=project&";
1078
} else {
1079
args += "?";
1080
}
1081
args += String() + "sort=" + sort_key[sort->get_selected()];
1082
1083
// We use the "branch" version, i.e. major.minor, as patch releases should be compatible
1084
args += "&godot_version=" + String(GODOT_VERSION_BRANCH);
1085
1086
String support_list;
1087
for (int i = 0; i < SUPPORT_MAX; i++) {
1088
if (support->get_popup()->is_item_checked(i)) {
1089
support_list += String(support_key[i]) + "+";
1090
}
1091
}
1092
if (!support_list.is_empty()) {
1093
args += "&support=" + support_list.substr(0, support_list.length() - 1);
1094
}
1095
1096
if (categories->get_selected() > 0) {
1097
args += "&category=" + itos(categories->get_item_metadata(categories->get_selected()));
1098
}
1099
1100
// Sorting options with an odd index are always the reverse of the previous one
1101
if (sort->get_selected() % 2 == 1) {
1102
args += "&reverse=true";
1103
}
1104
1105
if (!filter->get_text().is_empty()) {
1106
args += "&filter=" + filter->get_text().uri_encode();
1107
}
1108
1109
if (p_page > 0) {
1110
args += "&page=" + itos(p_page);
1111
}
1112
1113
_api_request("asset", REQUESTING_SEARCH, args);
1114
}
1115
1116
void EditorAssetLibrary::_search_text_changed(const String &p_text) {
1117
filter_debounce_timer->start();
1118
}
1119
1120
void EditorAssetLibrary::_filter_debounce_timer_timeout() {
1121
_search();
1122
}
1123
1124
void EditorAssetLibrary::_request_current_config() {
1125
_repository_changed(repository->get_selected());
1126
}
1127
1128
HBoxContainer *EditorAssetLibrary::_make_pages(int p_page, int p_page_count, int p_page_len, int p_total_items, int p_current_items) {
1129
HBoxContainer *hbc = memnew(HBoxContainer);
1130
1131
if (p_page_count < 2) {
1132
return hbc;
1133
}
1134
1135
//do the mario
1136
int from = p_page - (5 / EDSCALE);
1137
if (from < 0) {
1138
from = 0;
1139
}
1140
int to = from + (10 / EDSCALE);
1141
if (to > p_page_count) {
1142
to = p_page_count;
1143
}
1144
1145
hbc->add_spacer();
1146
hbc->add_theme_constant_override("separation", 5 * EDSCALE);
1147
1148
Button *first = memnew(Button);
1149
first->set_text(TTR("First", "Pagination"));
1150
first->set_theme_type_variation("PanelBackgroundButton");
1151
if (p_page != 0) {
1152
first->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibrary::_search).bind(0));
1153
} else {
1154
first->set_disabled(true);
1155
first->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
1156
}
1157
hbc->add_child(first);
1158
1159
Button *prev = memnew(Button);
1160
prev->set_text(TTR("Previous", "Pagination"));
1161
prev->set_theme_type_variation("PanelBackgroundButton");
1162
if (p_page > 0) {
1163
prev->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibrary::_search).bind(p_page - 1));
1164
} else {
1165
prev->set_disabled(true);
1166
prev->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
1167
}
1168
hbc->add_child(prev);
1169
hbc->add_child(memnew(VSeparator));
1170
1171
for (int i = from; i < to; i++) {
1172
Button *current = memnew(Button);
1173
// Add padding to make page number buttons easier to click.
1174
current->set_text(vformat(" %d ", i + 1));
1175
current->set_theme_type_variation("PanelBackgroundButton");
1176
if (i == p_page) {
1177
current->set_disabled(true);
1178
current->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
1179
} else {
1180
current->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibrary::_search).bind(i));
1181
}
1182
hbc->add_child(current);
1183
}
1184
1185
Button *next = memnew(Button);
1186
next->set_text(TTR("Next", "Pagination"));
1187
next->set_theme_type_variation("PanelBackgroundButton");
1188
if (p_page < p_page_count - 1) {
1189
next->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibrary::_search).bind(p_page + 1));
1190
} else {
1191
next->set_disabled(true);
1192
next->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
1193
}
1194
hbc->add_child(memnew(VSeparator));
1195
hbc->add_child(next);
1196
1197
Button *last = memnew(Button);
1198
last->set_text(TTR("Last", "Pagination"));
1199
last->set_theme_type_variation("PanelBackgroundButton");
1200
if (p_page != p_page_count - 1) {
1201
last->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibrary::_search).bind(p_page_count - 1));
1202
} else {
1203
last->set_disabled(true);
1204
last->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
1205
}
1206
hbc->add_child(last);
1207
1208
hbc->add_spacer();
1209
1210
return hbc;
1211
}
1212
1213
void EditorAssetLibrary::_api_request(const String &p_request, RequestType p_request_type, const String &p_arguments) {
1214
if (requesting != REQUESTING_NONE) {
1215
request->cancel_request();
1216
}
1217
error_hb->hide();
1218
1219
if (loading_blocked) {
1220
_set_library_message_with_action(TTRC("The Asset Library requires an online connection and involves sending data over the internet."), TTRC("Go Online"), callable_mp(this, &EditorAssetLibrary::_force_online_mode));
1221
return;
1222
}
1223
1224
requesting = p_request_type;
1225
request->request(host + "/" + p_request + p_arguments);
1226
}
1227
1228
void EditorAssetLibrary::_http_request_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data) {
1229
String str = String::utf8((const char *)p_data.ptr(), (int)p_data.size());
1230
bool error_abort = true;
1231
1232
switch (p_status) {
1233
case HTTPRequest::RESULT_CANT_RESOLVE: {
1234
error_label->set_text(TTR("Can't resolve hostname:") + " " + host);
1235
} break;
1236
case HTTPRequest::RESULT_BODY_SIZE_LIMIT_EXCEEDED:
1237
case HTTPRequest::RESULT_CONNECTION_ERROR:
1238
case HTTPRequest::RESULT_CHUNKED_BODY_SIZE_MISMATCH: {
1239
error_label->set_text(TTR("Connection error, please try again."));
1240
} break;
1241
case HTTPRequest::RESULT_TLS_HANDSHAKE_ERROR:
1242
case HTTPRequest::RESULT_CANT_CONNECT: {
1243
error_label->set_text(TTR("Can't connect to host:") + " " + host);
1244
} break;
1245
case HTTPRequest::RESULT_NO_RESPONSE: {
1246
error_label->set_text(TTR("No response from host:") + " " + host);
1247
} break;
1248
case HTTPRequest::RESULT_REQUEST_FAILED: {
1249
error_label->set_text(TTR("Request failed, return code:") + " " + itos(p_code));
1250
} break;
1251
case HTTPRequest::RESULT_REDIRECT_LIMIT_REACHED: {
1252
error_label->set_text(TTRC("Request failed, too many redirects"));
1253
1254
} break;
1255
default: {
1256
if (p_code != 200) {
1257
error_label->set_text(TTR("Request failed, return code:") + " " + itos(p_code));
1258
} else {
1259
error_abort = false;
1260
}
1261
} break;
1262
}
1263
1264
if (error_abort) {
1265
if (requesting == REQUESTING_CONFIG) {
1266
_set_library_message_with_action(TTRC("Failed to get repository configuration."), TTRC("Retry"), callable_mp(this, &EditorAssetLibrary::_request_current_config));
1267
}
1268
error_hb->show();
1269
return;
1270
}
1271
1272
Dictionary d;
1273
{
1274
JSON json;
1275
json.parse(str);
1276
d = json.get_data();
1277
}
1278
1279
RequestType requested = requesting;
1280
requesting = REQUESTING_NONE;
1281
1282
switch (requested) {
1283
case REQUESTING_CONFIG: {
1284
categories->clear();
1285
categories->add_item(TTRC("All"));
1286
categories->set_item_metadata(0, 0);
1287
if (d.has("categories")) {
1288
Array clist = d["categories"];
1289
for (int i = 0; i < clist.size(); i++) {
1290
Dictionary cat = clist[i];
1291
if (!cat.has("name") || !cat.has("id")) {
1292
continue;
1293
}
1294
String name = cat["name"];
1295
int id = cat["id"];
1296
categories->add_item(name);
1297
categories->set_item_metadata(-1, id);
1298
category_map[cat["id"]] = name;
1299
}
1300
}
1301
1302
filter->set_editable(true);
1303
sort->set_disabled(false);
1304
categories->set_disabled(false);
1305
support->set_disabled(false);
1306
1307
_search();
1308
} break;
1309
case REQUESTING_SEARCH: {
1310
initial_loading = false;
1311
1312
if (asset_items) {
1313
memdelete(asset_items);
1314
}
1315
1316
if (asset_top_page) {
1317
memdelete(asset_top_page);
1318
}
1319
1320
if (asset_bottom_page) {
1321
memdelete(asset_bottom_page);
1322
}
1323
1324
int page = 0;
1325
int pages = 1;
1326
int page_len = 10;
1327
int total_items = 1;
1328
Array result;
1329
1330
if (d.has("page")) {
1331
page = d["page"];
1332
}
1333
if (d.has("pages")) {
1334
pages = d["pages"];
1335
}
1336
if (d.has("page_length")) {
1337
page_len = d["page_length"];
1338
}
1339
if (d.has("total")) {
1340
total_items = d["total"];
1341
}
1342
if (d.has("result")) {
1343
result = d["result"];
1344
}
1345
1346
asset_top_page = _make_pages(page, pages, page_len, total_items, result.size());
1347
library_vb->add_child(asset_top_page);
1348
1349
asset_items = memnew(GridContainer);
1350
_update_asset_items_columns();
1351
asset_items->add_theme_constant_override("h_separation", 10 * EDSCALE);
1352
asset_items->add_theme_constant_override("v_separation", 10 * EDSCALE);
1353
1354
library_vb->add_child(asset_items);
1355
1356
asset_bottom_page = _make_pages(page, pages, page_len, total_items, result.size());
1357
library_vb->add_child(asset_bottom_page);
1358
1359
if (result.is_empty()) {
1360
String support_list;
1361
for (int i = 0; i < SUPPORT_MAX; i++) {
1362
if (support->get_popup()->is_item_checked(i)) {
1363
if (!support_list.is_empty()) {
1364
support_list += ", ";
1365
}
1366
support_list += TTRGET(support_text[i]);
1367
}
1368
}
1369
if (support_list.is_empty()) {
1370
support_list = "-";
1371
}
1372
1373
if (!filter->get_text().is_empty()) {
1374
_set_library_message(
1375
vformat(TTR("No results for \"%s\" for support level(s): %s."), filter->get_text(), support_list));
1376
} else {
1377
// No results, even though the user didn't search for anything specific.
1378
// This is typically because the version number changed recently
1379
// and no assets compatible with the new version have been published yet.
1380
_set_library_message(
1381
vformat(TTR("No results compatible with %s %s for support level(s): %s.\nCheck the enabled support levels using the 'Support' button in the top-right corner."), String(GODOT_VERSION_SHORT_NAME).capitalize(), String(GODOT_VERSION_BRANCH), support_list));
1382
}
1383
} else {
1384
library_message_box->hide();
1385
}
1386
1387
for (int i = 0; i < result.size(); i++) {
1388
Dictionary r = result[i];
1389
1390
ERR_CONTINUE(!r.has("title"));
1391
ERR_CONTINUE(!r.has("asset_id"));
1392
ERR_CONTINUE(!r.has("author"));
1393
ERR_CONTINUE(!r.has("author_id"));
1394
ERR_CONTINUE(!r.has("category_id"));
1395
ERR_FAIL_COND(!category_map.has(r["category_id"]));
1396
ERR_CONTINUE(!r.has("cost"));
1397
1398
EditorAssetLibraryItem *item = memnew(EditorAssetLibraryItem(true));
1399
asset_items->add_child(item);
1400
item->configure(r["title"], r["asset_id"], category_map[r["category_id"]], r["category_id"], r["author"], r["author_id"], r["cost"]);
1401
item->clamp_width(asset_items_column_width);
1402
item->connect("asset_selected", callable_mp(this, &EditorAssetLibrary::_select_asset));
1403
item->connect("author_selected", callable_mp(this, &EditorAssetLibrary::_select_author));
1404
item->connect("category_selected", callable_mp(this, &EditorAssetLibrary::_select_category));
1405
1406
if (r.has("icon_url") && !r["icon_url"].operator String().is_empty()) {
1407
_request_image(item->get_instance_id(), r["asset_id"], r["icon_url"], IMAGE_QUEUE_ICON, 0);
1408
}
1409
}
1410
1411
if (!result.is_empty()) {
1412
library_scroll->set_v_scroll(0);
1413
}
1414
} break;
1415
case REQUESTING_ASSET: {
1416
Dictionary r = d;
1417
1418
ERR_FAIL_COND(!r.has("title"));
1419
ERR_FAIL_COND(!r.has("asset_id"));
1420
ERR_FAIL_COND(!r.has("author"));
1421
ERR_FAIL_COND(!r.has("author_id"));
1422
ERR_FAIL_COND(!r.has("version"));
1423
ERR_FAIL_COND(!r.has("version_string"));
1424
ERR_FAIL_COND(!r.has("category_id"));
1425
ERR_FAIL_COND(!category_map.has(r["category_id"]));
1426
ERR_FAIL_COND(!r.has("cost"));
1427
ERR_FAIL_COND(!r.has("description"));
1428
ERR_FAIL_COND(!r.has("download_url"));
1429
ERR_FAIL_COND(!r.has("download_hash"));
1430
ERR_FAIL_COND(!r.has("browse_url"));
1431
1432
if (description) {
1433
memdelete(description);
1434
}
1435
1436
description = memnew(EditorAssetLibraryItemDescription);
1437
add_child(description);
1438
description->connect(SceneStringName(confirmed), callable_mp(this, &EditorAssetLibrary::_install_asset));
1439
1440
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"]);
1441
1442
EditorAssetLibraryItemDownload *download_item = _get_asset_in_progress(description->get_asset_id());
1443
if (download_item) {
1444
if (download_item->can_install()) {
1445
description->set_ok_button_text(TTRC("Install"));
1446
description->get_ok_button()->set_disabled(false);
1447
} else {
1448
description->set_ok_button_text(TTRC("Downloading..."));
1449
description->get_ok_button()->set_disabled(true);
1450
}
1451
} else {
1452
description->set_ok_button_text(TTRC("Download"));
1453
description->get_ok_button()->set_disabled(false);
1454
}
1455
1456
if (r.has("icon_url") && !r["icon_url"].operator String().is_empty()) {
1457
_request_image(description->get_instance_id(), r["asset_id"], r["icon_url"], IMAGE_QUEUE_ICON, 0);
1458
}
1459
1460
if (d.has("previews")) {
1461
Array previews = d["previews"];
1462
1463
for (int i = 0; i < previews.size(); i++) {
1464
Dictionary p = previews[i];
1465
1466
ERR_CONTINUE(!p.has("type"));
1467
ERR_CONTINUE(!p.has("link"));
1468
1469
bool is_video = p.has("type") && String(p["type"]) == "video";
1470
String video_url;
1471
if (is_video && p.has("link")) {
1472
video_url = p["link"];
1473
}
1474
1475
description->add_preview(i, is_video, video_url);
1476
1477
if (p.has("thumbnail")) {
1478
_request_image(description->get_instance_id(), r["asset_id"], p["thumbnail"], IMAGE_QUEUE_THUMBNAIL, i);
1479
}
1480
1481
if (!is_video) {
1482
_request_image(description->get_instance_id(), r["asset_id"], p["link"], IMAGE_QUEUE_SCREENSHOT, i);
1483
}
1484
}
1485
}
1486
1487
description->popup_centered();
1488
} break;
1489
default:
1490
break;
1491
}
1492
}
1493
1494
void EditorAssetLibrary::_asset_file_selected(const String &p_file) {
1495
if (asset_installer) {
1496
memdelete(asset_installer);
1497
asset_installer = nullptr;
1498
}
1499
1500
asset_installer = memnew(EditorAssetInstaller);
1501
asset_installer->set_asset_name(p_file);
1502
add_child(asset_installer);
1503
asset_installer->open_asset(p_file);
1504
}
1505
1506
void EditorAssetLibrary::_asset_open() {
1507
asset_open->popup_file_dialog();
1508
}
1509
1510
void EditorAssetLibrary::_manage_plugins() {
1511
ProjectSettingsEditor::get_singleton()->popup_project_settings(true);
1512
ProjectSettingsEditor::get_singleton()->set_plugins_page();
1513
}
1514
1515
EditorAssetLibraryItemDownload *EditorAssetLibrary::_get_asset_in_progress(int p_asset_id) const {
1516
for (int i = 0; i < downloads_hb->get_child_count(); i++) {
1517
EditorAssetLibraryItemDownload *d = Object::cast_to<EditorAssetLibraryItemDownload>(downloads_hb->get_child(i));
1518
if (d && d->get_asset_id() == p_asset_id) {
1519
return d;
1520
}
1521
}
1522
1523
return nullptr;
1524
}
1525
1526
void EditorAssetLibrary::_install_external_asset(String p_zip_path, String p_title) {
1527
emit_signal(SNAME("install_asset"), p_zip_path, p_title);
1528
}
1529
1530
void EditorAssetLibrary::_update_asset_items_columns() {
1531
int new_columns = get_size().x / (450.0 * EDSCALE);
1532
new_columns = MAX(1, new_columns);
1533
1534
if (new_columns != asset_items->get_columns()) {
1535
asset_items->set_columns(new_columns);
1536
}
1537
1538
asset_items_column_width = (get_size().x / new_columns) - (120 * EDSCALE);
1539
1540
for (int i = 0; i < asset_items->get_child_count(); i++) {
1541
EditorAssetLibraryItem *item = Object::cast_to<EditorAssetLibraryItem>(asset_items->get_child(i));
1542
if (!item || !item->is_visible()) {
1543
continue;
1544
}
1545
item->clamp_width(asset_items_column_width);
1546
}
1547
}
1548
1549
void EditorAssetLibrary::_set_library_message(const String &p_message) {
1550
library_message->set_text(p_message);
1551
1552
if (library_message_action.is_valid()) {
1553
library_message_button->disconnect(SceneStringName(pressed), library_message_action);
1554
library_message_action = Callable();
1555
}
1556
library_message_button->hide();
1557
1558
library_message_box->show();
1559
}
1560
1561
void EditorAssetLibrary::_set_library_message_with_action(const String &p_message, const String &p_action_text, const Callable &p_action) {
1562
library_message->set_text(p_message);
1563
1564
library_message_button->set_text(p_action_text);
1565
if (library_message_action.is_valid()) {
1566
library_message_button->disconnect(SceneStringName(pressed), library_message_action);
1567
library_message_action = Callable();
1568
}
1569
library_message_action = p_action;
1570
library_message_button->connect(SceneStringName(pressed), library_message_action);
1571
library_message_button->show();
1572
1573
library_message_box->show();
1574
}
1575
1576
void EditorAssetLibrary::_force_online_mode() {
1577
EditorSettings::get_singleton()->set_setting("network/connection/network_mode", EditorSettings::NETWORK_ONLINE);
1578
EditorSettings::get_singleton()->notify_changes();
1579
EditorSettings::get_singleton()->save();
1580
}
1581
1582
void EditorAssetLibrary::disable_community_support() {
1583
support->get_popup()->set_item_checked(SUPPORT_COMMUNITY, false);
1584
}
1585
1586
void EditorAssetLibrary::_bind_methods() {
1587
ADD_SIGNAL(MethodInfo("install_asset", PropertyInfo(Variant::STRING, "zip_path"), PropertyInfo(Variant::STRING, "name")));
1588
}
1589
1590
EditorAssetLibrary::EditorAssetLibrary(bool p_templates_only) {
1591
requesting = REQUESTING_NONE;
1592
templates_only = p_templates_only;
1593
loading_blocked = ((int)EDITOR_GET("network/connection/network_mode") == EditorSettings::NETWORK_OFFLINE);
1594
1595
VBoxContainer *library_main = memnew(VBoxContainer);
1596
add_child(library_main);
1597
1598
HBoxContainer *search_hb = memnew(HBoxContainer);
1599
1600
library_main->add_child(search_hb);
1601
library_main->add_theme_constant_override("separation", 10 * EDSCALE);
1602
1603
filter = memnew(LineEdit);
1604
if (templates_only) {
1605
filter->set_placeholder(TTRC("Search Templates, Projects, and Demos"));
1606
} else {
1607
filter->set_placeholder(TTRC("Search Assets (Excluding Templates, Projects, and Demos)"));
1608
}
1609
filter->set_clear_button_enabled(true);
1610
search_hb->add_child(filter);
1611
filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1612
filter->connect(SceneStringName(text_changed), callable_mp(this, &EditorAssetLibrary::_search_text_changed));
1613
1614
// Perform a search automatically if the user hasn't entered any text for a certain duration.
1615
// This way, the user doesn't need to press Enter to initiate their search.
1616
filter_debounce_timer = memnew(Timer);
1617
filter_debounce_timer->set_one_shot(true);
1618
filter_debounce_timer->set_wait_time(0.25);
1619
filter_debounce_timer->connect("timeout", callable_mp(this, &EditorAssetLibrary::_filter_debounce_timer_timeout));
1620
search_hb->add_child(filter_debounce_timer);
1621
1622
if (!p_templates_only) {
1623
search_hb->add_child(memnew(VSeparator));
1624
}
1625
1626
Button *open_asset = memnew(Button);
1627
open_asset->set_text(TTRC("Import..."));
1628
search_hb->add_child(open_asset);
1629
open_asset->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibrary::_asset_open));
1630
1631
Button *plugins = memnew(Button);
1632
plugins->set_text(TTRC("Plugins..."));
1633
search_hb->add_child(plugins);
1634
plugins->connect(SceneStringName(pressed), callable_mp(this, &EditorAssetLibrary::_manage_plugins));
1635
1636
if (p_templates_only) {
1637
open_asset->hide();
1638
plugins->hide();
1639
}
1640
1641
HBoxContainer *search_hb2 = memnew(HBoxContainer);
1642
library_main->add_child(search_hb2);
1643
1644
search_hb2->add_child(memnew(Label(TTRC("Sort:"))));
1645
sort = memnew(OptionButton);
1646
for (int i = 0; i < SORT_MAX; i++) {
1647
sort->add_item(sort_text[i]);
1648
}
1649
1650
search_hb2->add_child(sort);
1651
1652
sort->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1653
sort->set_clip_text(true);
1654
sort->connect(SceneStringName(item_selected), callable_mp(this, &EditorAssetLibrary::_rerun_search));
1655
1656
search_hb2->add_child(memnew(VSeparator));
1657
1658
search_hb2->add_child(memnew(Label(TTRC("Category:"))));
1659
categories = memnew(OptionButton);
1660
categories->add_item(TTRC("All"));
1661
search_hb2->add_child(categories);
1662
categories->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1663
categories->set_clip_text(true);
1664
categories->connect(SceneStringName(item_selected), callable_mp(this, &EditorAssetLibrary::_rerun_search));
1665
1666
search_hb2->add_child(memnew(VSeparator));
1667
1668
search_hb2->add_child(memnew(Label(TTRC("Site:"))));
1669
repository = memnew(OptionButton);
1670
1671
_update_repository_options();
1672
1673
repository->connect(SceneStringName(item_selected), callable_mp(this, &EditorAssetLibrary::_repository_changed));
1674
1675
search_hb2->add_child(repository);
1676
repository->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1677
repository->set_clip_text(true);
1678
1679
search_hb2->add_child(memnew(VSeparator));
1680
1681
support = memnew(MenuButton);
1682
search_hb2->add_child(support);
1683
support->set_text(TTRC("Support"));
1684
support->get_popup()->set_hide_on_checkable_item_selection(false);
1685
support->get_popup()->add_check_item(support_text[SUPPORT_FEATURED], SUPPORT_FEATURED);
1686
support->get_popup()->add_check_item(support_text[SUPPORT_COMMUNITY], SUPPORT_COMMUNITY);
1687
support->get_popup()->add_check_item(support_text[SUPPORT_TESTING], SUPPORT_TESTING);
1688
support->get_popup()->set_item_checked(SUPPORT_FEATURED, true);
1689
support->get_popup()->set_item_checked(SUPPORT_COMMUNITY, true);
1690
support->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditorAssetLibrary::_support_toggled));
1691
1692
/////////
1693
1694
library_scroll_bg = memnew(PanelContainer);
1695
library_main->add_child(library_scroll_bg);
1696
library_scroll_bg->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1697
1698
library_scroll = memnew(ScrollContainer);
1699
library_scroll->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
1700
1701
library_scroll_bg->add_child(library_scroll);
1702
1703
Ref<StyleBoxEmpty> border2;
1704
border2.instantiate();
1705
border2->set_content_margin_individual(15 * EDSCALE, 15 * EDSCALE, 35 * EDSCALE, 15 * EDSCALE);
1706
1707
PanelContainer *library_vb_border = memnew(PanelContainer);
1708
library_scroll->add_child(library_vb_border);
1709
library_vb_border->add_theme_style_override(SceneStringName(panel), border2);
1710
library_vb_border->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1711
1712
library_vb = memnew(VBoxContainer);
1713
library_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1714
1715
library_vb_border->add_child(library_vb);
1716
1717
library_message_box = memnew(VBoxContainer);
1718
library_message_box->hide();
1719
library_vb->add_child(library_message_box);
1720
1721
library_message = memnew(Label);
1722
library_message->set_focus_mode(FOCUS_ACCESSIBILITY);
1723
library_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
1724
library_message_box->add_child(library_message);
1725
1726
library_message_button = memnew(Button);
1727
library_message_button->set_h_size_flags(SIZE_SHRINK_CENTER);
1728
library_message_button->set_theme_type_variation("PanelBackgroundButton");
1729
library_message_box->add_child(library_message_button);
1730
1731
asset_top_page = memnew(HBoxContainer);
1732
library_vb->add_child(asset_top_page);
1733
1734
asset_items = memnew(GridContainer);
1735
_update_asset_items_columns();
1736
asset_items->add_theme_constant_override("h_separation", 10 * EDSCALE);
1737
asset_items->add_theme_constant_override("v_separation", 10 * EDSCALE);
1738
1739
library_vb->add_child(asset_items);
1740
1741
asset_bottom_page = memnew(HBoxContainer);
1742
library_vb->add_child(asset_bottom_page);
1743
1744
request = memnew(HTTPRequest);
1745
add_child(request);
1746
setup_http_request(request);
1747
request->connect("request_completed", callable_mp(this, &EditorAssetLibrary::_http_request_completed));
1748
1749
last_queue_id = 0;
1750
1751
library_vb->add_theme_constant_override("separation", 20 * EDSCALE);
1752
1753
error_hb = memnew(HBoxContainer);
1754
library_main->add_child(error_hb);
1755
error_label = memnew(Label);
1756
error_label->set_focus_mode(FOCUS_ACCESSIBILITY);
1757
error_hb->add_child(error_label);
1758
error_tr = memnew(TextureRect);
1759
error_tr->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
1760
error_hb->add_child(error_tr);
1761
1762
description = nullptr;
1763
1764
set_process(true);
1765
set_process_shortcut_input(true); // Global shortcuts since there is no main element to be focused.
1766
1767
downloads_scroll = memnew(ScrollContainer);
1768
downloads_scroll->set_vertical_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
1769
library_main->add_child(downloads_scroll);
1770
downloads_hb = memnew(HBoxContainer);
1771
downloads_scroll->add_child(downloads_hb);
1772
1773
asset_open = memnew(EditorFileDialog);
1774
1775
asset_open->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
1776
asset_open->add_filter("*.zip", TTRC("Assets ZIP File"));
1777
asset_open->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
1778
add_child(asset_open);
1779
asset_open->connect("file_selected", callable_mp(this, &EditorAssetLibrary::_asset_file_selected));
1780
1781
asset_installer = nullptr;
1782
}
1783
1784
///////
1785
1786
bool AssetLibraryEditorPlugin::is_available() {
1787
#ifdef WEB_ENABLED
1788
// Asset Library can't work on Web editor for now as most assets are sourced
1789
// directly from GitHub which does not set CORS.
1790
return false;
1791
#else
1792
return StreamPeerTLS::is_available() && !Engine::get_singleton()->is_recovery_mode_hint();
1793
#endif
1794
}
1795
1796
void AssetLibraryEditorPlugin::make_visible(bool p_visible) {
1797
if (p_visible) {
1798
addon_library->show();
1799
} else {
1800
addon_library->hide();
1801
}
1802
}
1803
1804
AssetLibraryEditorPlugin::AssetLibraryEditorPlugin() {
1805
addon_library = memnew(EditorAssetLibrary);
1806
addon_library->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1807
EditorNode::get_singleton()->get_editor_main_screen()->get_control()->add_child(addon_library);
1808
addon_library->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
1809
addon_library->hide();
1810
}
1811
1812