asset_library_editor_plugin.h 11 KB

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