Path: blob/master/editor/asset_library/asset_library_editor_plugin.h
22120 views
/**************************************************************************/1/* asset_library_editor_plugin.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "editor/asset_library/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"4748class EditorFileDialog;49class HSeparator;50class MenuButton;5152class EditorAssetLibraryItem : public PanelContainer {53GDCLASS(EditorAssetLibraryItem, PanelContainer);5455TextureButton *icon = nullptr;56LinkButton *title = nullptr;57LinkButton *category = nullptr;58LinkButton *author = nullptr;59Label *price = nullptr;60HSeparator *separator = nullptr;61Control *spacer = nullptr;62HBoxContainer *author_price_hbox = nullptr;6364String title_text;65int asset_id = 0;66int category_id = 0;67int author_id = 0;6869int author_width = 0;70int price_width = 0;7172void _asset_clicked();73void _category_clicked();74void _author_clicked();7576void _calculate_misc_links_size();7778void set_image(int p_type, int p_index, const Ref<Texture2D> &p_image);7980protected:81void _notification(int p_what);82static void _bind_methods();8384public:85void 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);8687void calculate_misc_links_ratio();8889EditorAssetLibraryItem(bool p_clickable = false);90};9192class EditorAssetLibraryItemDescription : public ConfirmationDialog {93GDCLASS(EditorAssetLibraryItemDescription, ConfirmationDialog);9495EditorAssetLibraryItem *item = nullptr;96RichTextLabel *description = nullptr;97VBoxContainer *previews_vbox = nullptr;98ScrollContainer *previews = nullptr;99HBoxContainer *preview_hb = nullptr;100PanelContainer *previews_bg = nullptr;101102struct Preview {103int id = 0;104bool is_video = false;105String video_link;106Button *button = nullptr;107Ref<Texture2D> image;108};109110Vector<Preview> preview_images;111TextureRect *preview = nullptr;112113void set_image(int p_type, int p_index, const Ref<Texture2D> &p_image);114115int asset_id = 0;116String download_url;117String title;118String sha256;119Ref<Texture2D> icon;120121void _link_click(const String &p_url);122void _preview_click(int p_id);123124protected:125void _notification(int p_what);126static void _bind_methods();127128public:129void 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);130void add_preview(int p_id, bool p_video, const String &p_url);131132String get_title() { return title; }133Ref<Texture2D> get_preview_icon() { return icon; }134String get_download_url() { return download_url; }135int get_asset_id() { return asset_id; }136String get_sha256() { return sha256; }137EditorAssetLibraryItemDescription();138};139140class EditorAssetLibraryItemDownload : public MarginContainer {141GDCLASS(EditorAssetLibraryItemDownload, MarginContainer);142143PanelContainer *panel = nullptr;144TextureRect *icon = nullptr;145Label *title = nullptr;146ProgressBar *progress = nullptr;147Button *install_button = nullptr;148Button *retry_button = nullptr;149TextureButton *dismiss_button = nullptr;150151AcceptDialog *download_error = nullptr;152HTTPRequest *download = nullptr;153String host;154String sha256;155Label *status = nullptr;156157int prev_status;158159int asset_id = 0;160161bool external_install;162163EditorAssetInstaller *asset_installer = nullptr;164165void _close();166void _make_request();167void _http_download_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data);168169protected:170void _notification(int p_what);171static void _bind_methods();172173public:174void set_external_install(bool p_enable) { external_install = p_enable; }175int get_asset_id() { return asset_id; }176void configure(const String &p_title, int p_asset_id, const Ref<Texture2D> &p_preview, const String &p_download_url, const String &p_sha256_hash);177178bool can_install() const;179void install();180181EditorAssetLibraryItemDownload();182};183184class EditorAssetLibrary : public PanelContainer {185GDCLASS(EditorAssetLibrary, PanelContainer);186187String host;188189EditorFileDialog *asset_open = nullptr;190EditorAssetInstaller *asset_installer = nullptr;191192void _asset_open();193void _asset_file_selected(const String &p_file);194void _update_repository_options();195196MarginContainer *library_mc = nullptr;197ScrollContainer *library_scroll = nullptr;198VBoxContainer *library_vb = nullptr;199VBoxContainer *library_message_box = nullptr;200Label *library_message = nullptr;201Button *library_message_button = nullptr;202Callable library_message_action;203204void _set_library_message(const String &p_message);205void _set_library_message_with_action(const String &p_message, const String &p_action_text, const Callable &p_action);206207LineEdit *filter = nullptr;208Timer *filter_debounce_timer = nullptr;209OptionButton *categories = nullptr;210OptionButton *repository = nullptr;211OptionButton *sort = nullptr;212HBoxContainer *error_hb = nullptr;213TextureRect *error_tr = nullptr;214Label *error_label = nullptr;215MenuButton *support = nullptr;216217HBoxContainer *contents = nullptr;218219HBoxContainer *asset_top_page = nullptr;220GridContainer *asset_items = nullptr;221HBoxContainer *asset_bottom_page = nullptr;222223HTTPRequest *request = nullptr;224225bool templates_only = false;226bool initial_loading = true;227bool loading_blocked = false;228229void _force_online_mode();230231enum Support {232SUPPORT_FEATURED,233SUPPORT_COMMUNITY,234SUPPORT_TESTING,235SUPPORT_MAX236};237238enum SortOrder {239SORT_UPDATED,240SORT_UPDATED_REVERSE,241SORT_NAME,242SORT_NAME_REVERSE,243SORT_COST,244SORT_COST_REVERSE,245SORT_MAX246};247248static const char *sort_key[SORT_MAX];249static const char *sort_text[SORT_MAX];250static const char *support_key[SUPPORT_MAX];251static const char *support_text[SUPPORT_MAX];252253///MainListing254255enum ImageType {256IMAGE_QUEUE_ICON,257IMAGE_QUEUE_THUMBNAIL,258IMAGE_QUEUE_SCREENSHOT,259260};261262struct ImageQueue {263bool active = false;264int queue_id = 0;265ImageType image_type = ImageType::IMAGE_QUEUE_ICON;266int image_index = 0;267String image_url;268HTTPRequest *request = nullptr;269ObjectID target;270int asset_id = -1;271};272273int last_queue_id;274HashMap<int, ImageQueue> image_queue;275276void _image_update(bool p_use_cache, bool p_final, const PackedByteArray &p_data, int p_queue_id);277void _image_request_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data, int p_queue_id);278void _request_image(ObjectID p_for, int p_asset_id, String p_image_url, ImageType p_type, int p_image_index);279void _update_image_queue();280281HBoxContainer *_make_pages(int p_page, int p_page_count, int p_page_len, int p_total_items, int p_current_items);282283//284EditorAssetLibraryItemDescription *description = nullptr;285//286287enum RequestType {288REQUESTING_NONE,289REQUESTING_CONFIG,290REQUESTING_SEARCH,291REQUESTING_ASSET,292};293294RequestType requesting;295Dictionary category_map;296297ScrollContainer *downloads_scroll = nullptr;298HBoxContainer *downloads_hb = nullptr;299300void _install_asset();301302void _select_author(const String &p_author);303void _select_category(int p_id);304void _select_asset(int p_id);305306void _manage_plugins();307308void _search(int p_page = 0);309void _rerun_search(int p_ignore);310void _search_text_changed(const String &p_text = "");311void _search_text_submitted(const String &p_text = "");312void _api_request(const String &p_request, RequestType p_request_type, const String &p_arguments = "");313void _http_request_completed(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data);314void _filter_debounce_timer_timeout();315void _request_current_config();316EditorAssetLibraryItemDownload *_get_asset_in_progress(int p_asset_id) const;317318void _repository_changed(int p_repository_id);319void _support_toggled(int p_support);320321void _install_external_asset(String p_zip_path, String p_title);322323void _update_asset_items_columns();324325friend class EditorAssetLibraryItemDescription;326friend class EditorAssetLibraryItem;327328protected:329static void _bind_methods();330void _notification(int p_what);331virtual void shortcut_input(const Ref<InputEvent> &p_event) override;332333public:334void disable_community_support();335336EditorAssetLibrary(bool p_templates_only = false);337};338339class AssetLibraryEditorPlugin : public EditorPlugin {340GDCLASS(AssetLibraryEditorPlugin, EditorPlugin);341342EditorAssetLibrary *addon_library = nullptr;343344public:345static bool is_available();346347virtual String get_plugin_name() const override { return TTRC("AssetLib"); }348bool has_main_screen() const override { return true; }349virtual void edit(Object *p_object) override {}350virtual bool handles(Object *p_object) const override { return false; }351virtual void make_visible(bool p_visible) override;352//virtual bool get_remove_list(List<Node*> *p_list) { return canvas_item_editor->get_remove_list(p_list); }353//virtual Dictionary get_state() const;354//virtual void set_state(const Dictionary& p_state);355356AssetLibraryEditorPlugin();357};358359360