Path: blob/master/editor/gui/editor_quick_open_dialog.h
21102 views
/**************************************************************************/1/* editor_quick_open_dialog.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 "core/templates/a_hash_map.h"33#include "scene/gui/dialogs.h"34#include "scene/gui/margin_container.h"3536class Button;37class CenterContainer;38class CheckButton;39class ConfigFile;40class EditorFileSystemDirectory;41class LineEdit;42class HFlowContainer;43class MarginContainer;44class PanelContainer;45class PopupMenu;46class ScrollContainer;47class StringName;48class Texture2D;49class TextureRect;50class VBoxContainer;5152class FuzzySearchResult;5354class QuickOpenResultItem;5556enum class QuickOpenDisplayMode {57GRID,58LIST,59};6061struct QuickOpenResultCandidate {62ResourceUID::ID uid;63Ref<Texture2D> thumbnail;64const FuzzySearchResult *result = nullptr;6566static QuickOpenResultCandidate from_uid(const ResourceUID::ID &p_uid, bool &r_success);67static QuickOpenResultCandidate from_result(const FuzzySearchResult &p_result, bool &r_success);68};6970class HighlightedLabel : public Label {71GDCLASS(HighlightedLabel, Label)7273Vector<Vector2i> highlights;7475void draw_substr_rects(const Vector2i &p_substr, Vector2 p_offset, int p_line_limit, int line_spacing);7677public:78void add_highlight(const Vector2i &p_interval);79void reset_highlights();8081protected:82void _notification(int p_notification);83};8485class QuickOpenResultContainer : public VBoxContainer {86GDCLASS(QuickOpenResultContainer, VBoxContainer)8788enum {89FILE_SHOW_IN_FILESYSTEM,90FILE_SHOW_IN_FILE_MANAGER91};9293public:94void init(const Vector<StringName> &p_base_types);95void handle_search_box_input(const Ref<InputEvent> &p_ie);96void set_query_and_update(const String &p_query);97void update_results();9899bool has_nothing_selected() const;100ResourceUID::ID get_selected() const;101String get_selected_path() const;102103bool is_instant_preview_enabled() const;104void set_instant_preview_toggle_visible(bool p_visible);105106void save_selected_item();107void cleanup();108109QuickOpenResultContainer();110111protected:112void _notification(int p_what);113114private:115static constexpr int MAX_HISTORY_SIZE = 20;116117Vector<FuzzySearchResult> search_results;118Vector<StringName> base_types;119LocalVector<ResourceUID::ID> uids;120AHashMap<ResourceUID::ID, StringName> filetypes;121Vector<QuickOpenResultCandidate> candidates;122HashSet<ResourceUID::ID> candidates_uids;123124AHashMap<StringName, Vector<ResourceUID::ID>> selected_history;125HashSet<ResourceUID::ID> history_set;126127String query;128int selection_index = -1;129int num_visible_results = 0;130int max_total_results = 0;131132bool never_opened = true;133Ref<ConfigFile> history_file;134135QuickOpenDisplayMode content_display_mode = QuickOpenDisplayMode::LIST;136Vector<QuickOpenResultItem *> result_items;137138ScrollContainer *scroll_container = nullptr;139VBoxContainer *list = nullptr;140HFlowContainer *grid = nullptr;141PopupMenu *file_context_menu = nullptr;142143PanelContainer *panel_container = nullptr;144CenterContainer *no_results_container = nullptr;145Label *no_results_label = nullptr;146147Label *file_details_path = nullptr;148Button *display_mode_toggle = nullptr;149CheckButton *instant_preview_toggle = nullptr;150CheckButton *include_addons_toggle = nullptr;151CheckButton *fuzzy_search_toggle = nullptr;152153AHashMap<StringName, Ref<Texture2D>> file_type_icons;154155static QuickOpenDisplayMode get_adaptive_display_mode(const Vector<StringName> &p_base_types);156157void _ensure_result_vector_capacity();158void _sort_uids(int p_max_results);159void _create_initial_results();160void _find_uids_in_folder(EditorFileSystemDirectory *p_directory, bool p_include_addons);161162Vector<ResourceUID::ID> *_get_history();163void _add_candidate(QuickOpenResultCandidate &p_candidate);164void _update_fuzzy_search_results();165void _use_default_candidates();166void _score_and_sort_candidates();167void _update_result_items(int p_new_visible_results_count, int p_new_selection_index);168169void _move_selection_index(Key p_key);170void _select_item(int p_index);171172void _item_input(const Ref<InputEvent> &p_ev, int p_index);173174CanvasItem *_get_result_root();175void _layout_result_item(QuickOpenResultItem *p_item);176void _set_display_mode(QuickOpenDisplayMode p_display_mode);177void _toggle_display_mode();178void _toggle_instant_preview(bool p_pressed);179void _toggle_include_addons(bool p_pressed);180void _toggle_fuzzy_search(bool p_pressed);181void _menu_option(int p_option);182183String _get_cache_file_path() const;184185static void _bind_methods();186};187188class QuickOpenResultGridItem : public MarginContainer {189GDCLASS(QuickOpenResultGridItem, MarginContainer)190191public:192QuickOpenResultGridItem();193194void reset();195void set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight);196void highlight_item(const Color &p_color);197void remove_highlight();198199private:200VBoxContainer *vbc = nullptr;201TextureRect *thumbnail = nullptr;202HighlightedLabel *name = nullptr;203};204205class QuickOpenResultListItem : public MarginContainer {206GDCLASS(QuickOpenResultListItem, MarginContainer)207208public:209QuickOpenResultListItem();210211void reset();212void set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight);213void highlight_item(const Color &p_color);214void remove_highlight();215216protected:217void _notification(int p_what);218219private:220HBoxContainer *hbc = nullptr;221VBoxContainer *text_container = nullptr;222223TextureRect *thumbnail = nullptr;224HighlightedLabel *name = nullptr;225HighlightedLabel *path = nullptr;226};227228class QuickOpenResultItem : public HBoxContainer {229GDCLASS(QuickOpenResultItem, HBoxContainer)230231public:232QuickOpenResultItem();233234bool enable_highlights = true;235236void reset();237void set_content(const QuickOpenResultCandidate &p_candidate);238void set_display_mode(QuickOpenDisplayMode p_display_mode);239void highlight_item(bool p_enabled);240241protected:242void _notification(int p_what);243244private:245QuickOpenResultListItem *list_item = nullptr;246QuickOpenResultGridItem *grid_item = nullptr;247248Ref<StyleBox> selected_stylebox;249Ref<StyleBox> hovering_stylebox;250Color highlighted_font_color;251252bool is_hovering = false;253bool is_selected = false;254255void _set_enabled(bool p_enabled);256};257258class EditorQuickOpenDialog : public AcceptDialog {259GDCLASS(EditorQuickOpenDialog, AcceptDialog);260261public:262void popup_dialog(const Vector<StringName> &p_base_types, const Callable &p_item_selected_callback);263void popup_dialog_for_property(const Vector<StringName> &p_base_types, Object *p_obj, const StringName &p_path, const Callable &p_item_selected_callback);264EditorQuickOpenDialog();265266protected:267virtual void cancel_pressed() override;268virtual void ok_pressed() override;269void item_pressed(bool p_double_click);270void selection_changed();271272private:273static String get_dialog_title(const Vector<StringName> &p_base_types);274275LineEdit *search_box = nullptr;276QuickOpenResultContainer *container = nullptr;277278Callable item_selected_callback;279280Object *property_object = nullptr;281StringName property_path;282Variant initial_property_value;283bool initial_selection_performed = false;284bool _is_instant_preview_active() const;285void _search_box_text_changed(const String &p_query);286void _finish_dialog_setup(const Vector<StringName> &p_base_types);287288void preview_property();289void update_property();290};291292293