Path: blob/master/editor/gui/editor_quick_open_dialog.h
9902 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 {62String file_path;63Ref<Texture2D> thumbnail;64const FuzzySearchResult *result = nullptr;65};6667class HighlightedLabel : public Label {68GDCLASS(HighlightedLabel, Label)6970Vector<Vector2i> highlights;7172void draw_substr_rects(const Vector2i &p_substr, Vector2 p_offset, int p_line_limit, int line_spacing);7374public:75void add_highlight(const Vector2i &p_interval);76void reset_highlights();7778protected:79void _notification(int p_notification);80};8182class QuickOpenResultContainer : public VBoxContainer {83GDCLASS(QuickOpenResultContainer, VBoxContainer)8485enum {86FILE_SHOW_IN_FILESYSTEM,87FILE_SHOW_IN_FILE_MANAGER88};8990public:91void init(const Vector<StringName> &p_base_types);92void handle_search_box_input(const Ref<InputEvent> &p_ie);93void set_query_and_update(const String &p_query);94void update_results();9596bool has_nothing_selected() const;97String get_selected() const;9899void save_selected_item();100void cleanup();101102QuickOpenResultContainer();103104protected:105void _notification(int p_what);106107private:108static constexpr int MAX_HISTORY_SIZE = 20;109110Vector<FuzzySearchResult> search_results;111Vector<StringName> base_types;112Vector<String> filepaths;113AHashMap<String, StringName> filetypes;114Vector<QuickOpenResultCandidate> candidates;115116AHashMap<StringName, Vector<QuickOpenResultCandidate>> selected_history;117HashSet<String> history_set;118119String query;120int selection_index = -1;121int num_visible_results = 0;122int max_total_results = 0;123124bool never_opened = true;125Ref<ConfigFile> history_file;126127QuickOpenDisplayMode content_display_mode = QuickOpenDisplayMode::LIST;128Vector<QuickOpenResultItem *> result_items;129130ScrollContainer *scroll_container = nullptr;131VBoxContainer *list = nullptr;132HFlowContainer *grid = nullptr;133PopupMenu *file_context_menu = nullptr;134135PanelContainer *panel_container = nullptr;136CenterContainer *no_results_container = nullptr;137Label *no_results_label = nullptr;138139Label *file_details_path = nullptr;140Button *display_mode_toggle = nullptr;141CheckButton *include_addons_toggle = nullptr;142CheckButton *fuzzy_search_toggle = nullptr;143144AHashMap<StringName, Ref<Texture2D>> file_type_icons;145146static QuickOpenDisplayMode get_adaptive_display_mode(const Vector<StringName> &p_base_types);147148void _ensure_result_vector_capacity();149void _sort_filepaths(int p_max_results);150void _create_initial_results();151void _find_filepaths_in_folder(EditorFileSystemDirectory *p_directory, bool p_include_addons);152153Vector<QuickOpenResultCandidate> *_get_history();154void _setup_candidate(QuickOpenResultCandidate &p_candidate, const String &p_filepath);155void _setup_candidate(QuickOpenResultCandidate &p_candidate, const FuzzySearchResult &p_result);156void _update_fuzzy_search_results();157void _use_default_candidates();158void _score_and_sort_candidates();159void _update_result_items(int p_new_visible_results_count, int p_new_selection_index);160161void _move_selection_index(Key p_key);162void _select_item(int p_index);163164void _item_input(const Ref<InputEvent> &p_ev, int p_index);165166CanvasItem *_get_result_root();167void _layout_result_item(QuickOpenResultItem *p_item);168void _set_display_mode(QuickOpenDisplayMode p_display_mode);169void _toggle_display_mode();170void _toggle_include_addons(bool p_pressed);171void _toggle_fuzzy_search(bool p_pressed);172void _menu_option(int p_option);173174String _get_cache_file_path() const;175176static void _bind_methods();177};178179class QuickOpenResultGridItem : public MarginContainer {180GDCLASS(QuickOpenResultGridItem, MarginContainer)181182public:183QuickOpenResultGridItem();184185void reset();186void set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight);187void highlight_item(const Color &p_color);188void remove_highlight();189190private:191VBoxContainer *vbc = nullptr;192TextureRect *thumbnail = nullptr;193HighlightedLabel *name = nullptr;194};195196class QuickOpenResultListItem : public MarginContainer {197GDCLASS(QuickOpenResultListItem, MarginContainer)198199public:200QuickOpenResultListItem();201202void reset();203void set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight);204void highlight_item(const Color &p_color);205void remove_highlight();206207protected:208void _notification(int p_what);209210private:211HBoxContainer *hbc = nullptr;212VBoxContainer *text_container = nullptr;213214TextureRect *thumbnail = nullptr;215HighlightedLabel *name = nullptr;216HighlightedLabel *path = nullptr;217};218219class QuickOpenResultItem : public HBoxContainer {220GDCLASS(QuickOpenResultItem, HBoxContainer)221222public:223QuickOpenResultItem();224225bool enable_highlights = true;226227void reset();228void set_content(const QuickOpenResultCandidate &p_candidate);229void set_display_mode(QuickOpenDisplayMode p_display_mode);230void highlight_item(bool p_enabled);231232protected:233void _notification(int p_what);234235private:236QuickOpenResultListItem *list_item = nullptr;237QuickOpenResultGridItem *grid_item = nullptr;238239Ref<StyleBox> selected_stylebox;240Ref<StyleBox> hovering_stylebox;241Color highlighted_font_color;242243bool is_hovering = false;244bool is_selected = false;245246void _set_enabled(bool p_enabled);247};248249class EditorQuickOpenDialog : public AcceptDialog {250GDCLASS(EditorQuickOpenDialog, AcceptDialog);251252public:253void popup_dialog(const Vector<StringName> &p_base_types, const Callable &p_item_selected_callback);254EditorQuickOpenDialog();255256protected:257virtual void cancel_pressed() override;258virtual void ok_pressed() override;259260private:261static String get_dialog_title(const Vector<StringName> &p_base_types);262263LineEdit *search_box = nullptr;264QuickOpenResultContainer *container = nullptr;265266Callable item_selected_callback;267268void _search_box_text_changed(const String &p_query);269};270271272