1 /*************************************************************************/
2 /*  asset_library_editor_plugin.h                                        */
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 #ifndef ASSET_LIBRARY_EDITOR_PLUGIN_H
32 #define ASSET_LIBRARY_EDITOR_PLUGIN_H
33 
34 #include "editor/editor_asset_installer.h"
35 #include "editor/editor_plugin.h"
36 #include "editor/editor_plugin_settings.h"
37 #include "scene/gui/box_container.h"
38 #include "scene/gui/check_box.h"
39 #include "scene/gui/grid_container.h"
40 #include "scene/gui/line_edit.h"
41 #include "scene/gui/link_button.h"
42 #include "scene/gui/option_button.h"
43 #include "scene/gui/panel_container.h"
44 #include "scene/gui/progress_bar.h"
45 #include "scene/gui/rich_text_label.h"
46 #include "scene/gui/scroll_container.h"
47 #include "scene/gui/separator.h"
48 #include "scene/gui/tab_container.h"
49 #include "scene/gui/texture_button.h"
50 #include "scene/main/http_request.h"
51 
52 class EditorAssetLibraryItem : public PanelContainer {
53 
54 	GDCLASS(EditorAssetLibraryItem, PanelContainer);
55 
56 	TextureButton *icon;
57 	LinkButton *title;
58 	LinkButton *category;
59 	LinkButton *author;
60 	TextureRect *stars[5];
61 	Label *price;
62 
63 	int asset_id;
64 	int category_id;
65 	int author_id;
66 
67 	void _asset_clicked();
68 	void _category_clicked();
69 	void _author_clicked();
70 
71 	void set_image(int p_type, int p_index, const Ref<Texture> &p_image);
72 
73 protected:
74 	void _notification(int p_what);
75 	static void _bind_methods();
76 
77 public:
78 	void 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);
79 
80 	EditorAssetLibraryItem();
81 };
82 
83 class EditorAssetLibraryItemDescription : public ConfirmationDialog {
84 
85 	GDCLASS(EditorAssetLibraryItemDescription, ConfirmationDialog);
86 
87 	EditorAssetLibraryItem *item;
88 	RichTextLabel *description;
89 	ScrollContainer *previews;
90 	HBoxContainer *preview_hb;
91 	PanelContainer *previews_bg;
92 
93 	struct Preview {
94 		int id;
95 		bool is_video;
96 		String video_link;
97 		Button *button;
98 		Ref<Texture> image;
99 	};
100 
101 	Vector<Preview> preview_images;
102 	TextureRect *preview;
103 
104 	void set_image(int p_type, int p_index, const Ref<Texture> &p_image);
105 
106 	int asset_id;
107 	String download_url;
108 	String title;
109 	String sha256;
110 	Ref<Texture> icon;
111 
112 	void _link_click(const String &p_url);
113 	void _preview_click(int p_id);
114 
115 protected:
116 	void _notification(int p_what);
117 	static void _bind_methods();
118 
119 public:
120 	void 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);
121 	void add_preview(int p_id, bool p_video, const String &p_url);
122 
get_title()123 	String get_title() { return title; }
get_preview_icon()124 	Ref<Texture> get_preview_icon() { return icon; }
get_download_url()125 	String get_download_url() { return download_url; }
get_asset_id()126 	int get_asset_id() { return asset_id; }
get_sha256()127 	String get_sha256() { return sha256; }
128 	EditorAssetLibraryItemDescription();
129 };
130 
131 class EditorAssetLibraryItemDownload : public PanelContainer {
132 
133 	GDCLASS(EditorAssetLibraryItemDownload, PanelContainer);
134 
135 	TextureRect *icon;
136 	Label *title;
137 	ProgressBar *progress;
138 	Button *install;
139 	Button *retry;
140 	TextureButton *dismiss;
141 
142 	AcceptDialog *download_error;
143 	HTTPRequest *download;
144 	String host;
145 	String sha256;
146 	Label *status;
147 
148 	int prev_status;
149 
150 	int asset_id;
151 
152 	bool external_install;
153 
154 	EditorAssetInstaller *asset_installer;
155 
156 	void _close();
157 	void _install();
158 	void _make_request();
159 	void _http_download_completed(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data);
160 
161 protected:
162 	void _notification(int p_what);
163 	static void _bind_methods();
164 
165 public:
set_external_install(bool p_enable)166 	void set_external_install(bool p_enable) { external_install = p_enable; }
get_asset_id()167 	int get_asset_id() { return asset_id; }
168 	void configure(const String &p_title, int p_asset_id, const Ref<Texture> &p_preview, const String &p_download_url, const String &p_sha256_hash);
169 	EditorAssetLibraryItemDownload();
170 };
171 
172 class EditorAssetLibrary : public PanelContainer {
173 	GDCLASS(EditorAssetLibrary, PanelContainer);
174 
175 	String host;
176 
177 	EditorFileDialog *asset_open;
178 	EditorAssetInstaller *asset_installer;
179 
180 	void _asset_open();
181 	void _asset_file_selected(const String &p_file);
182 
183 	PanelContainer *library_scroll_bg;
184 	ScrollContainer *library_scroll;
185 	VBoxContainer *library_vb;
186 	Label *library_loading;
187 	Label *library_error;
188 	LineEdit *filter;
189 	OptionButton *categories;
190 	OptionButton *repository;
191 	OptionButton *sort;
192 	Button *search;
193 	HBoxContainer *error_hb;
194 	TextureRect *error_tr;
195 	Label *error_label;
196 	MenuButton *support;
197 
198 	HBoxContainer *contents;
199 
200 	HBoxContainer *asset_top_page;
201 	GridContainer *asset_items;
202 	HBoxContainer *asset_bottom_page;
203 
204 	HTTPRequest *request;
205 
206 	bool templates_only;
207 	bool initial_loading;
208 
209 	enum Support {
210 		SUPPORT_OFFICIAL,
211 		SUPPORT_COMMUNITY,
212 		SUPPORT_TESTING,
213 		SUPPORT_MAX
214 	};
215 
216 	enum SortOrder {
217 		SORT_UPDATED,
218 		SORT_UPDATED_REVERSE,
219 		SORT_NAME,
220 		SORT_NAME_REVERSE,
221 		SORT_COST,
222 		SORT_COST_REVERSE,
223 		SORT_MAX
224 	};
225 
226 	static const char *sort_key[SORT_MAX];
227 	static const char *sort_text[SORT_MAX];
228 	static const char *support_key[SUPPORT_MAX];
229 
230 	///MainListing
231 
232 	enum ImageType {
233 		IMAGE_QUEUE_ICON,
234 		IMAGE_QUEUE_THUMBNAIL,
235 		IMAGE_QUEUE_SCREENSHOT,
236 
237 	};
238 
239 	struct ImageQueue {
240 
241 		bool active;
242 		int queue_id;
243 		ImageType image_type;
244 		int image_index;
245 		String image_url;
246 		HTTPRequest *request;
247 		ObjectID target;
248 	};
249 
250 	int last_queue_id;
251 	Map<int, ImageQueue> image_queue;
252 
253 	void _image_update(bool use_cache, bool final, const PoolByteArray &p_data, int p_queue_id);
254 	void _image_request_completed(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data, int p_queue_id);
255 	void _request_image(ObjectID p_for, String p_image_url, ImageType p_type, int p_image_index);
256 	void _update_image_queue();
257 
258 	HBoxContainer *_make_pages(int p_page, int p_page_count, int p_page_len, int p_total_items, int p_current_items);
259 
260 	//
261 	EditorAssetLibraryItemDescription *description;
262 	//
263 
264 	enum RequestType {
265 		REQUESTING_NONE,
266 		REQUESTING_CONFIG,
267 		REQUESTING_SEARCH,
268 		REQUESTING_ASSET,
269 	};
270 
271 	RequestType requesting;
272 	Dictionary category_map;
273 
274 	ScrollContainer *downloads_scroll;
275 	HBoxContainer *downloads_hb;
276 
277 	void _install_asset();
278 
279 	void _select_author(int p_id);
280 	void _select_category(int p_id);
281 	void _select_asset(int p_id);
282 
283 	void _manage_plugins();
284 
285 	void _search(int p_page = 0);
286 	void _rerun_search(int p_ignore);
287 	void _search_text_entered(const String &p_text = "");
288 	void _api_request(const String &p_request, RequestType p_request_type, const String &p_arguments = "");
289 	void _http_request_completed(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data);
290 	void _http_download_completed(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data);
291 
292 	void _repository_changed(int p_repository_id);
293 	void _support_toggled(int p_support);
294 
295 	void _install_external_asset(String p_zip_path, String p_title);
296 
297 	friend class EditorAssetLibraryItemDescription;
298 	friend class EditorAssetLibraryItem;
299 
300 protected:
301 	static void _bind_methods();
302 	void _notification(int p_what);
303 
304 public:
305 	void disable_community_support();
306 
307 	EditorAssetLibrary(bool p_templates_only = false);
308 };
309 
310 class AssetLibraryEditorPlugin : public EditorPlugin {
311 
312 	GDCLASS(AssetLibraryEditorPlugin, EditorPlugin);
313 
314 	EditorAssetLibrary *addon_library;
315 	EditorNode *editor;
316 
317 public:
get_name()318 	virtual String get_name() const { return "AssetLib"; }
has_main_screen()319 	bool has_main_screen() const { return true; }
edit(Object * p_object)320 	virtual void edit(Object *p_object) {}
handles(Object * p_object)321 	virtual bool handles(Object *p_object) const { return false; }
322 	virtual void make_visible(bool p_visible);
323 	//virtual bool get_remove_list(List<Node*> *p_list) { return canvas_item_editor->get_remove_list(p_list); }
324 	//virtual Dictionary get_state() const;
325 	//virtual void set_state(const Dictionary& p_state);
326 
327 	AssetLibraryEditorPlugin(EditorNode *p_node);
328 	~AssetLibraryEditorPlugin();
329 };
330 
331 #endif // EDITORASSETLIBRARY_H
332