asset_library_editor_plugin.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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) 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. #pragma once
  31. #include "editor/editor_asset_installer.h"
  32. #include "editor/plugins/editor_plugin.h"
  33. #include "scene/gui/box_container.h"
  34. #include "scene/gui/grid_container.h"
  35. #include "scene/gui/line_edit.h"
  36. #include "scene/gui/link_button.h"
  37. #include "scene/gui/margin_container.h"
  38. #include "scene/gui/option_button.h"
  39. #include "scene/gui/panel_container.h"
  40. #include "scene/gui/progress_bar.h"
  41. #include "scene/gui/rich_text_label.h"
  42. #include "scene/gui/scroll_container.h"
  43. #include "scene/gui/texture_button.h"
  44. #include "scene/gui/texture_rect.h"
  45. #include "scene/main/http_request.h"
  46. class EditorFileDialog;
  47. class MenuButton;
  48. class EditorAssetLibraryItem : public PanelContainer {
  49. GDCLASS(EditorAssetLibraryItem, PanelContainer);
  50. TextureButton *icon = nullptr;
  51. LinkButton *title = nullptr;
  52. LinkButton *category = nullptr;
  53. LinkButton *author = nullptr;
  54. Label *price = nullptr;
  55. String title_text;
  56. int asset_id = 0;
  57. int category_id = 0;
  58. int author_id = 0;
  59. void _asset_clicked();
  60. void _category_clicked();
  61. void _author_clicked();
  62. void set_image(int p_type, int p_index, const Ref<Texture2D> &p_image);
  63. protected:
  64. void _notification(int p_what);
  65. static void _bind_methods();
  66. public:
  67. 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);
  68. void clamp_width(int p_max_width);
  69. EditorAssetLibraryItem(bool p_clickable = false);
  70. };
  71. class EditorAssetLibraryItemDescription : public ConfirmationDialog {
  72. GDCLASS(EditorAssetLibraryItemDescription, ConfirmationDialog);
  73. EditorAssetLibraryItem *item = nullptr;
  74. RichTextLabel *description = nullptr;
  75. VBoxContainer *previews_vbox = nullptr;
  76. ScrollContainer *previews = nullptr;
  77. HBoxContainer *preview_hb = nullptr;
  78. PanelContainer *previews_bg = nullptr;
  79. struct Preview {
  80. int id = 0;
  81. bool is_video = false;
  82. String video_link;
  83. Button *button = nullptr;
  84. Ref<Texture2D> image;
  85. };
  86. Vector<Preview> preview_images;
  87. TextureRect *preview = nullptr;
  88. void set_image(int p_type, int p_index, const Ref<Texture2D> &p_image);
  89. int asset_id = 0;
  90. String download_url;
  91. String title;
  92. String sha256;
  93. Ref<Texture2D> icon;
  94. void _link_click(const String &p_url);
  95. void _preview_click(int p_id);
  96. protected:
  97. void _notification(int p_what);
  98. static void _bind_methods();
  99. public:
  100. 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);
  101. void add_preview(int p_id, bool p_video, const String &p_url);
  102. String get_title() { return title; }
  103. Ref<Texture2D> get_preview_icon() { return icon; }
  104. String get_download_url() { return download_url; }
  105. int get_asset_id() { return asset_id; }
  106. String get_sha256() { return sha256; }
  107. EditorAssetLibraryItemDescription();
  108. };
  109. class EditorAssetLibraryItemDownload : public MarginContainer {
  110. GDCLASS(EditorAssetLibraryItemDownload, MarginContainer);
  111. PanelContainer *panel = nullptr;
  112. TextureRect *icon = nullptr;
  113. Label *title = nullptr;
  114. ProgressBar *progress = nullptr;
  115. Button *install_button = nullptr;
  116. Button *retry_button = nullptr;
  117. TextureButton *dismiss_button = nullptr;
  118. AcceptDialog *download_error = nullptr;
  119. HTTPRequest *download = nullptr;
  120. String host;
  121. String sha256;
  122. Label *status = nullptr;
  123. int prev_status;
  124. int asset_id = 0;
  125. bool external_install;
  126. EditorAssetInstaller *asset_installer = nullptr;
  127. void _close();
  128. void _make_request();
  129. void _http_download_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data);
  130. protected:
  131. void _notification(int p_what);
  132. static void _bind_methods();
  133. public:
  134. void set_external_install(bool p_enable) { external_install = p_enable; }
  135. int get_asset_id() { return asset_id; }
  136. void configure(const String &p_title, int p_asset_id, const Ref<Texture2D> &p_preview, const String &p_download_url, const String &p_sha256_hash);
  137. bool can_install() const;
  138. void install();
  139. EditorAssetLibraryItemDownload();
  140. };
  141. class EditorAssetLibrary : public PanelContainer {
  142. GDCLASS(EditorAssetLibrary, PanelContainer);
  143. String host;
  144. EditorFileDialog *asset_open = nullptr;
  145. EditorAssetInstaller *asset_installer = nullptr;
  146. void _asset_open();
  147. void _asset_file_selected(const String &p_file);
  148. void _update_repository_options();
  149. PanelContainer *library_scroll_bg = nullptr;
  150. ScrollContainer *library_scroll = nullptr;
  151. VBoxContainer *library_vb = nullptr;
  152. VBoxContainer *library_message_box = nullptr;
  153. Label *library_message = nullptr;
  154. Button *library_message_button = nullptr;
  155. Callable library_message_action;
  156. void _set_library_message(const String &p_message);
  157. void _set_library_message_with_action(const String &p_message, const String &p_action_text, const Callable &p_action);
  158. LineEdit *filter = nullptr;
  159. Timer *filter_debounce_timer = nullptr;
  160. OptionButton *categories = nullptr;
  161. OptionButton *repository = nullptr;
  162. OptionButton *sort = nullptr;
  163. HBoxContainer *error_hb = nullptr;
  164. TextureRect *error_tr = nullptr;
  165. Label *error_label = nullptr;
  166. MenuButton *support = nullptr;
  167. HBoxContainer *contents = nullptr;
  168. HBoxContainer *asset_top_page = nullptr;
  169. GridContainer *asset_items = nullptr;
  170. HBoxContainer *asset_bottom_page = nullptr;
  171. HTTPRequest *request = nullptr;
  172. bool templates_only = false;
  173. bool initial_loading = true;
  174. bool loading_blocked = false;
  175. void _force_online_mode();
  176. enum Support {
  177. SUPPORT_FEATURED,
  178. SUPPORT_COMMUNITY,
  179. SUPPORT_TESTING,
  180. SUPPORT_MAX
  181. };
  182. enum SortOrder {
  183. SORT_UPDATED,
  184. SORT_UPDATED_REVERSE,
  185. SORT_NAME,
  186. SORT_NAME_REVERSE,
  187. SORT_COST,
  188. SORT_COST_REVERSE,
  189. SORT_MAX
  190. };
  191. static const char *sort_key[SORT_MAX];
  192. static const char *sort_text[SORT_MAX];
  193. static const char *support_key[SUPPORT_MAX];
  194. static const char *support_text[SUPPORT_MAX];
  195. ///MainListing
  196. enum ImageType {
  197. IMAGE_QUEUE_ICON,
  198. IMAGE_QUEUE_THUMBNAIL,
  199. IMAGE_QUEUE_SCREENSHOT,
  200. };
  201. struct ImageQueue {
  202. bool active = false;
  203. int queue_id = 0;
  204. ImageType image_type = ImageType::IMAGE_QUEUE_ICON;
  205. int image_index = 0;
  206. String image_url;
  207. HTTPRequest *request = nullptr;
  208. ObjectID target;
  209. int asset_id = -1;
  210. };
  211. int last_queue_id;
  212. HashMap<int, ImageQueue> image_queue;
  213. void _image_update(bool p_use_cache, bool p_final, const PackedByteArray &p_data, int p_queue_id);
  214. void _image_request_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data, int p_queue_id);
  215. void _request_image(ObjectID p_for, int p_asset_id, String p_image_url, ImageType p_type, int p_image_index);
  216. void _update_image_queue();
  217. HBoxContainer *_make_pages(int p_page, int p_page_count, int p_page_len, int p_total_items, int p_current_items);
  218. //
  219. EditorAssetLibraryItemDescription *description = nullptr;
  220. //
  221. enum RequestType {
  222. REQUESTING_NONE,
  223. REQUESTING_CONFIG,
  224. REQUESTING_SEARCH,
  225. REQUESTING_ASSET,
  226. };
  227. RequestType requesting;
  228. Dictionary category_map;
  229. ScrollContainer *downloads_scroll = nullptr;
  230. HBoxContainer *downloads_hb = nullptr;
  231. void _install_asset();
  232. void _select_author(const String &p_author);
  233. void _select_category(int p_id);
  234. void _select_asset(int p_id);
  235. void _manage_plugins();
  236. void _search(int p_page = 0);
  237. void _rerun_search(int p_ignore);
  238. void _search_text_changed(const String &p_text = "");
  239. void _search_text_submitted(const String &p_text = "");
  240. void _api_request(const String &p_request, RequestType p_request_type, const String &p_arguments = "");
  241. void _http_request_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data);
  242. void _filter_debounce_timer_timeout();
  243. void _request_current_config();
  244. EditorAssetLibraryItemDownload *_get_asset_in_progress(int p_asset_id) const;
  245. void _repository_changed(int p_repository_id);
  246. void _support_toggled(int p_support);
  247. void _install_external_asset(String p_zip_path, String p_title);
  248. int asset_items_column_width = 0;
  249. void _update_asset_items_columns();
  250. friend class EditorAssetLibraryItemDescription;
  251. friend class EditorAssetLibraryItem;
  252. protected:
  253. static void _bind_methods();
  254. void _notification(int p_what);
  255. virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
  256. public:
  257. void disable_community_support();
  258. EditorAssetLibrary(bool p_templates_only = false);
  259. };
  260. class AssetLibraryEditorPlugin : public EditorPlugin {
  261. GDCLASS(AssetLibraryEditorPlugin, EditorPlugin);
  262. EditorAssetLibrary *addon_library = nullptr;
  263. public:
  264. static bool is_available();
  265. virtual String get_plugin_name() const override { return TTRC("AssetLib"); }
  266. bool has_main_screen() const override { return true; }
  267. virtual void edit(Object *p_object) override {}
  268. virtual bool handles(Object *p_object) const override { return false; }
  269. virtual void make_visible(bool p_visible) override;
  270. //virtual bool get_remove_list(List<Node*> *p_list) { return canvas_item_editor->get_remove_list(p_list); }
  271. //virtual Dictionary get_state() const;
  272. //virtual void set_state(const Dictionary& p_state);
  273. AssetLibraryEditorPlugin();
  274. };