Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/gui/editor_quick_open_dialog.h
9902 views
1
/**************************************************************************/
2
/* editor_quick_open_dialog.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
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
void save_selected_item();
101
void cleanup();
102
103
QuickOpenResultContainer();
104
105
protected:
106
void _notification(int p_what);
107
108
private:
109
static constexpr int MAX_HISTORY_SIZE = 20;
110
111
Vector<FuzzySearchResult> search_results;
112
Vector<StringName> base_types;
113
Vector<String> filepaths;
114
AHashMap<String, StringName> filetypes;
115
Vector<QuickOpenResultCandidate> candidates;
116
117
AHashMap<StringName, Vector<QuickOpenResultCandidate>> selected_history;
118
HashSet<String> history_set;
119
120
String query;
121
int selection_index = -1;
122
int num_visible_results = 0;
123
int max_total_results = 0;
124
125
bool never_opened = true;
126
Ref<ConfigFile> history_file;
127
128
QuickOpenDisplayMode content_display_mode = QuickOpenDisplayMode::LIST;
129
Vector<QuickOpenResultItem *> result_items;
130
131
ScrollContainer *scroll_container = nullptr;
132
VBoxContainer *list = nullptr;
133
HFlowContainer *grid = nullptr;
134
PopupMenu *file_context_menu = nullptr;
135
136
PanelContainer *panel_container = nullptr;
137
CenterContainer *no_results_container = nullptr;
138
Label *no_results_label = nullptr;
139
140
Label *file_details_path = nullptr;
141
Button *display_mode_toggle = nullptr;
142
CheckButton *include_addons_toggle = nullptr;
143
CheckButton *fuzzy_search_toggle = nullptr;
144
145
AHashMap<StringName, Ref<Texture2D>> file_type_icons;
146
147
static QuickOpenDisplayMode get_adaptive_display_mode(const Vector<StringName> &p_base_types);
148
149
void _ensure_result_vector_capacity();
150
void _sort_filepaths(int p_max_results);
151
void _create_initial_results();
152
void _find_filepaths_in_folder(EditorFileSystemDirectory *p_directory, bool p_include_addons);
153
154
Vector<QuickOpenResultCandidate> *_get_history();
155
void _setup_candidate(QuickOpenResultCandidate &p_candidate, const String &p_filepath);
156
void _setup_candidate(QuickOpenResultCandidate &p_candidate, const FuzzySearchResult &p_result);
157
void _update_fuzzy_search_results();
158
void _use_default_candidates();
159
void _score_and_sort_candidates();
160
void _update_result_items(int p_new_visible_results_count, int p_new_selection_index);
161
162
void _move_selection_index(Key p_key);
163
void _select_item(int p_index);
164
165
void _item_input(const Ref<InputEvent> &p_ev, int p_index);
166
167
CanvasItem *_get_result_root();
168
void _layout_result_item(QuickOpenResultItem *p_item);
169
void _set_display_mode(QuickOpenDisplayMode p_display_mode);
170
void _toggle_display_mode();
171
void _toggle_include_addons(bool p_pressed);
172
void _toggle_fuzzy_search(bool p_pressed);
173
void _menu_option(int p_option);
174
175
String _get_cache_file_path() const;
176
177
static void _bind_methods();
178
};
179
180
class QuickOpenResultGridItem : public MarginContainer {
181
GDCLASS(QuickOpenResultGridItem, MarginContainer)
182
183
public:
184
QuickOpenResultGridItem();
185
186
void reset();
187
void set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight);
188
void highlight_item(const Color &p_color);
189
void remove_highlight();
190
191
private:
192
VBoxContainer *vbc = nullptr;
193
TextureRect *thumbnail = nullptr;
194
HighlightedLabel *name = nullptr;
195
};
196
197
class QuickOpenResultListItem : public MarginContainer {
198
GDCLASS(QuickOpenResultListItem, MarginContainer)
199
200
public:
201
QuickOpenResultListItem();
202
203
void reset();
204
void set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight);
205
void highlight_item(const Color &p_color);
206
void remove_highlight();
207
208
protected:
209
void _notification(int p_what);
210
211
private:
212
HBoxContainer *hbc = nullptr;
213
VBoxContainer *text_container = nullptr;
214
215
TextureRect *thumbnail = nullptr;
216
HighlightedLabel *name = nullptr;
217
HighlightedLabel *path = nullptr;
218
};
219
220
class QuickOpenResultItem : public HBoxContainer {
221
GDCLASS(QuickOpenResultItem, HBoxContainer)
222
223
public:
224
QuickOpenResultItem();
225
226
bool enable_highlights = true;
227
228
void reset();
229
void set_content(const QuickOpenResultCandidate &p_candidate);
230
void set_display_mode(QuickOpenDisplayMode p_display_mode);
231
void highlight_item(bool p_enabled);
232
233
protected:
234
void _notification(int p_what);
235
236
private:
237
QuickOpenResultListItem *list_item = nullptr;
238
QuickOpenResultGridItem *grid_item = nullptr;
239
240
Ref<StyleBox> selected_stylebox;
241
Ref<StyleBox> hovering_stylebox;
242
Color highlighted_font_color;
243
244
bool is_hovering = false;
245
bool is_selected = false;
246
247
void _set_enabled(bool p_enabled);
248
};
249
250
class EditorQuickOpenDialog : public AcceptDialog {
251
GDCLASS(EditorQuickOpenDialog, AcceptDialog);
252
253
public:
254
void popup_dialog(const Vector<StringName> &p_base_types, const Callable &p_item_selected_callback);
255
EditorQuickOpenDialog();
256
257
protected:
258
virtual void cancel_pressed() override;
259
virtual void ok_pressed() override;
260
261
private:
262
static String get_dialog_title(const Vector<StringName> &p_base_types);
263
264
LineEdit *search_box = nullptr;
265
QuickOpenResultContainer *container = nullptr;
266
267
Callable item_selected_callback;
268
269
void _search_box_text_changed(const String &p_query);
270
};
271
272