| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/templates/a_hash_map.h" |
| 34 |
#include "scene/gui/dialogs.h" |
| 35 |
#include "scene/gui/margin_container.h" |
| 36 |
|
| 37 |
class Button; |
| 38 |
class CenterContainer; |
| 39 |
class CheckButton; |
| 40 |
class ConfigFile; |
| 41 |
class EditorFileSystemDirectory; |
| 42 |
class LineEdit; |
| 43 |
class HFlowContainer; |
| 44 |
class MarginContainer; |
| 45 |
class PanelContainer; |
| 46 |
class PopupMenu; |
| 47 |
class ScrollContainer; |
| 48 |
class StringName; |
| 49 |
class Texture2D; |
| 50 |
class TextureRect; |
| 51 |
class VBoxContainer; |
| 52 |
|
| 53 |
class FuzzySearchResult; |
| 54 |
|
| 55 |
class QuickOpenResultItem; |
| 56 |
|
| 57 |
enum class QuickOpenDisplayMode { |
| 58 |
GRID, |
| 59 |
LIST, |
| 60 |
}; |
| 61 |
|
| 62 |
struct QuickOpenResultCandidate { |
| 63 |
String file_path; |
| 64 |
Ref<Texture2D> thumbnail; |
| 65 |
const FuzzySearchResult *result = nullptr; |
| 66 |
}; |
| 67 |
|
| 68 |
class HighlightedLabel : public Label { |
| 69 |
GDCLASS(HighlightedLabel, Label) |
| 70 |
|
| 71 |
Vector<Vector2i> highlights; |
| 72 |
|
| 73 |
void draw_substr_rects(const Vector2i &p_substr, Vector2 p_offset, int p_line_limit, int line_spacing); |
| 74 |
|
| 75 |
public: |
| 76 |
void add_highlight(const Vector2i &p_interval); |
| 77 |
void reset_highlights(); |
| 78 |
|
| 79 |
protected: |
| 80 |
void _notification(int p_notification); |
| 81 |
}; |
| 82 |
|
| 83 |
class QuickOpenResultContainer : public VBoxContainer { |
| 84 |
GDCLASS(QuickOpenResultContainer, VBoxContainer) |
| 85 |
|
| 86 |
enum { |
| 87 |
FILE_SHOW_IN_FILESYSTEM, |
| 88 |
FILE_SHOW_IN_FILE_MANAGER |
| 89 |
}; |
| 90 |
|
| 91 |
public: |
| 92 |
void init(const Vector<StringName> &p_base_types); |
| 93 |
void handle_search_box_input(const Ref<InputEvent> &p_ie); |
| 94 |
void set_query_and_update(const String &p_query); |
| 95 |
void update_results(); |
| 96 |
|
| 97 |
bool has_nothing_selected() const; |
| 98 |
String get_selected() const; |
| 99 |
|
| 100 |
bool is_instant_preview_enabled() const; |
| 101 |
void set_instant_preview_toggle_visible(bool p_visible); |
| 102 |
|
| 103 |
void save_selected_item(); |
| 104 |
void cleanup(); |
| 105 |
|
| 106 |
QuickOpenResultContainer(); |
| 107 |
|
| 108 |
protected: |
| 109 |
void _notification(int p_what); |
| 110 |
|
| 111 |
private: |
| 112 |
static constexpr int MAX_HISTORY_SIZE = 20; |
| 113 |
|
| 114 |
Vector<FuzzySearchResult> search_results; |
| 115 |
Vector<StringName> base_types; |
| 116 |
Vector<String> filepaths; |
| 117 |
AHashMap<String, StringName> filetypes; |
| 118 |
Vector<QuickOpenResultCandidate> candidates; |
| 119 |
|
| 120 |
AHashMap<StringName, Vector<QuickOpenResultCandidate>> selected_history; |
| 121 |
HashSet<String> history_set; |
| 122 |
|
| 123 |
String query; |
| 124 |
int selection_index = -1; |
| 125 |
int num_visible_results = 0; |
| 126 |
int max_total_results = 0; |
| 127 |
|
| 128 |
bool never_opened = true; |
| 129 |
Ref<ConfigFile> history_file; |
| 130 |
|
| 131 |
QuickOpenDisplayMode content_display_mode = QuickOpenDisplayMode::LIST; |
| 132 |
Vector<QuickOpenResultItem *> result_items; |
| 133 |
|
| 134 |
ScrollContainer *scroll_container = nullptr; |
| 135 |
VBoxContainer *list = nullptr; |
| 136 |
HFlowContainer *grid = nullptr; |
| 137 |
PopupMenu *file_context_menu = nullptr; |
| 138 |
|
| 139 |
PanelContainer *panel_container = nullptr; |
| 140 |
CenterContainer *no_results_container = nullptr; |
| 141 |
Label *no_results_label = nullptr; |
| 142 |
|
| 143 |
Label *file_details_path = nullptr; |
| 144 |
Button *display_mode_toggle = nullptr; |
| 145 |
CheckButton *instant_preview_toggle = nullptr; |
| 146 |
CheckButton *include_addons_toggle = nullptr; |
| 147 |
CheckButton *fuzzy_search_toggle = nullptr; |
| 148 |
|
| 149 |
AHashMap<StringName, Ref<Texture2D>> file_type_icons; |
| 150 |
|
| 151 |
static QuickOpenDisplayMode get_adaptive_display_mode(const Vector<StringName> &p_base_types); |
| 152 |
|
| 153 |
void _ensure_result_vector_capacity(); |
| 154 |
void _sort_filepaths(int p_max_results); |
| 155 |
void _create_initial_results(); |
| 156 |
void _find_filepaths_in_folder(EditorFileSystemDirectory *p_directory, bool p_include_addons); |
| 157 |
|
| 158 |
Vector<QuickOpenResultCandidate> *_get_history(); |
| 159 |
void _setup_candidate(QuickOpenResultCandidate &p_candidate, const String &p_filepath); |
| 160 |
void _setup_candidate(QuickOpenResultCandidate &p_candidate, const FuzzySearchResult &p_result); |
| 161 |
void _update_fuzzy_search_results(); |
| 162 |
void _use_default_candidates(); |
| 163 |
void _score_and_sort_candidates(); |
| 164 |
void _update_result_items(int p_new_visible_results_count, int p_new_selection_index); |
| 165 |
|
| 166 |
void _move_selection_index(Key p_key); |
| 167 |
void _select_item(int p_index); |
| 168 |
|
| 169 |
void _item_input(const Ref<InputEvent> &p_ev, int p_index); |
| 170 |
|
| 171 |
CanvasItem *_get_result_root(); |
| 172 |
void _layout_result_item(QuickOpenResultItem *p_item); |
| 173 |
void _set_display_mode(QuickOpenDisplayMode p_display_mode); |
| 174 |
void _toggle_display_mode(); |
| 175 |
void _toggle_instant_preview(bool p_pressed); |
| 176 |
void _toggle_include_addons(bool p_pressed); |
| 177 |
void _toggle_fuzzy_search(bool p_pressed); |
| 178 |
void _menu_option(int p_option); |
| 179 |
|
| 180 |
String _get_cache_file_path() const; |
| 181 |
|
| 182 |
static void _bind_methods(); |
| 183 |
}; |
| 184 |
|
| 185 |
class QuickOpenResultGridItem : public MarginContainer { |
| 186 |
GDCLASS(QuickOpenResultGridItem, MarginContainer) |
| 187 |
|
| 188 |
public: |
| 189 |
QuickOpenResultGridItem(); |
| 190 |
|
| 191 |
void reset(); |
| 192 |
void set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight); |
| 193 |
void highlight_item(const Color &p_color); |
| 194 |
void remove_highlight(); |
| 195 |
|
| 196 |
private: |
| 197 |
VBoxContainer *vbc = nullptr; |
| 198 |
TextureRect *thumbnail = nullptr; |
| 199 |
HighlightedLabel *name = nullptr; |
| 200 |
}; |
| 201 |
|
| 202 |
class QuickOpenResultListItem : public MarginContainer { |
| 203 |
GDCLASS(QuickOpenResultListItem, MarginContainer) |
| 204 |
|
| 205 |
public: |
| 206 |
QuickOpenResultListItem(); |
| 207 |
|
| 208 |
void reset(); |
| 209 |
void set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight); |
| 210 |
void highlight_item(const Color &p_color); |
| 211 |
void remove_highlight(); |
| 212 |
|
| 213 |
protected: |
| 214 |
void _notification(int p_what); |
| 215 |
|
| 216 |
private: |
| 217 |
HBoxContainer *hbc = nullptr; |
| 218 |
VBoxContainer *text_container = nullptr; |
| 219 |
|
| 220 |
TextureRect *thumbnail = nullptr; |
| 221 |
HighlightedLabel *name = nullptr; |
| 222 |
HighlightedLabel *path = nullptr; |
| 223 |
}; |
| 224 |
|
| 225 |
class QuickOpenResultItem : public HBoxContainer { |
| 226 |
GDCLASS(QuickOpenResultItem, HBoxContainer) |
| 227 |
|
| 228 |
public: |
| 229 |
QuickOpenResultItem(); |
| 230 |
|
| 231 |
bool enable_highlights = true; |
| 232 |
|
| 233 |
void reset(); |
| 234 |
void set_content(const QuickOpenResultCandidate &p_candidate); |
| 235 |
void set_display_mode(QuickOpenDisplayMode p_display_mode); |
| 236 |
void highlight_item(bool p_enabled); |
| 237 |
|
| 238 |
protected: |
| 239 |
void _notification(int p_what); |
| 240 |
|
| 241 |
private: |
| 242 |
QuickOpenResultListItem *list_item = nullptr; |
| 243 |
QuickOpenResultGridItem *grid_item = nullptr; |
| 244 |
|
| 245 |
Ref<StyleBox> selected_stylebox; |
| 246 |
Ref<StyleBox> hovering_stylebox; |
| 247 |
Color highlighted_font_color; |
| 248 |
|
| 249 |
bool is_hovering = false; |
| 250 |
bool is_selected = false; |
| 251 |
|
| 252 |
void _set_enabled(bool p_enabled); |
| 253 |
}; |
| 254 |
|
| 255 |
class EditorQuickOpenDialog : public AcceptDialog { |
| 256 |
GDCLASS(EditorQuickOpenDialog, AcceptDialog); |
| 257 |
|
| 258 |
public: |
| 259 |
void popup_dialog(const Vector<StringName> &p_base_types, const Callable &p_item_selected_callback); |
| 260 |
void popup_dialog_for_property(const Vector<StringName> &p_base_types, Object *p_obj, const StringName &p_path, const Callable &p_item_selected_callback); |
| 261 |
EditorQuickOpenDialog(); |
| 262 |
|
| 263 |
protected: |
| 264 |
virtual void cancel_pressed() override; |
| 265 |
virtual void ok_pressed() override; |
| 266 |
void item_pressed(bool p_double_click); |
| 267 |
void selection_changed(); |
| 268 |
|
| 269 |
private: |
| 270 |
static String get_dialog_title(const Vector<StringName> &p_base_types); |
| 271 |
|
| 272 |
LineEdit *search_box = nullptr; |
| 273 |
QuickOpenResultContainer *container = nullptr; |
| 274 |
|
| 275 |
Callable item_selected_callback; |
| 276 |
|
| 277 |
Object *property_object = nullptr; |
| 278 |
StringName property_path; |
| 279 |
Variant initial_property_value; |
| 280 |
bool initial_selection_performed = false; |
| 281 |
bool _is_instant_preview_active() const; |
| 282 |
void _search_box_text_changed(const String &p_query); |
| 283 |
void _finish_dialog_setup(const Vector<StringName> &p_base_types); |
| 284 |
|
| 285 |
void preview_property(); |
| 286 |
void update_property(); |
| 287 |
}; |