asset_library_editor_plugin.h 11 KB

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