Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/gui/editor_quick_open_dialog.h
21102 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
ResourceUID::ID uid;
64
Ref<Texture2D> thumbnail;
65
const FuzzySearchResult *result = nullptr;
66
67
static QuickOpenResultCandidate from_uid(const ResourceUID::ID &p_uid, bool &r_success);
68
static QuickOpenResultCandidate from_result(const FuzzySearchResult &p_result, bool &r_success);
69
};
70
71
class HighlightedLabel : public Label {
72
GDCLASS(HighlightedLabel, Label)
73
74
Vector<Vector2i> highlights;
75
76
void draw_substr_rects(const Vector2i &p_substr, Vector2 p_offset, int p_line_limit, int line_spacing);
77
78
public:
79
void add_highlight(const Vector2i &p_interval);
80
void reset_highlights();
81
82
protected:
83
void _notification(int p_notification);
84
};
85
86
class QuickOpenResultContainer : public VBoxContainer {
87
GDCLASS(QuickOpenResultContainer, VBoxContainer)
88
89
enum {
90
FILE_SHOW_IN_FILESYSTEM,
91
FILE_SHOW_IN_FILE_MANAGER
92
};
93
94
public:
95
void init(const Vector<StringName> &p_base_types);
96
void handle_search_box_input(const Ref<InputEvent> &p_ie);
97
void set_query_and_update(const String &p_query);
98
void update_results();
99
100
bool has_nothing_selected() const;
101
ResourceUID::ID get_selected() const;
102
String get_selected_path() const;
103
104
bool is_instant_preview_enabled() const;
105
void set_instant_preview_toggle_visible(bool p_visible);
106
107
void save_selected_item();
108
void cleanup();
109
110
QuickOpenResultContainer();
111
112
protected:
113
void _notification(int p_what);
114
115
private:
116
static constexpr int MAX_HISTORY_SIZE = 20;
117
118
Vector<FuzzySearchResult> search_results;
119
Vector<StringName> base_types;
120
LocalVector<ResourceUID::ID> uids;
121
AHashMap<ResourceUID::ID, StringName> filetypes;
122
Vector<QuickOpenResultCandidate> candidates;
123
HashSet<ResourceUID::ID> candidates_uids;
124
125
AHashMap<StringName, Vector<ResourceUID::ID>> selected_history;
126
HashSet<ResourceUID::ID> history_set;
127
128
String query;
129
int selection_index = -1;
130
int num_visible_results = 0;
131
int max_total_results = 0;
132
133
bool never_opened = true;
134
Ref<ConfigFile> history_file;
135
136
QuickOpenDisplayMode content_display_mode = QuickOpenDisplayMode::LIST;
137
Vector<QuickOpenResultItem *> result_items;
138
139
ScrollContainer *scroll_container = nullptr;
140
VBoxContainer *list = nullptr;
141
HFlowContainer *grid = nullptr;
142
PopupMenu *file_context_menu = nullptr;
143
144
PanelContainer *panel_container = nullptr;
145
CenterContainer *no_results_container = nullptr;
146
Label *no_results_label = nullptr;
147
148
Label *file_details_path = nullptr;
149
Button *display_mode_toggle = nullptr;
150
CheckButton *instant_preview_toggle = nullptr;
151
CheckButton *include_addons_toggle = nullptr;
152
CheckButton *fuzzy_search_toggle = nullptr;
153
154
AHashMap<StringName, Ref<Texture2D>> file_type_icons;
155
156
static QuickOpenDisplayMode get_adaptive_display_mode(const Vector<StringName> &p_base_types);
157
158
void _ensure_result_vector_capacity();
159
void _sort_uids(int p_max_results);
160
void _create_initial_results();
161
void _find_uids_in_folder(EditorFileSystemDirectory *p_directory, bool p_include_addons);
162
163
Vector<ResourceUID::ID> *_get_history();
164
void _add_candidate(QuickOpenResultCandidate &p_candidate);
165
void _update_fuzzy_search_results();
166
void _use_default_candidates();
167
void _score_and_sort_candidates();
168
void _update_result_items(int p_new_visible_results_count, int p_new_selection_index);
169
170
void _move_selection_index(Key p_key);
171
void _select_item(int p_index);
172
173
void _item_input(const Ref<InputEvent> &p_ev, int p_index);
174
175
CanvasItem *_get_result_root();
176
void _layout_result_item(QuickOpenResultItem *p_item);
177
void _set_display_mode(QuickOpenDisplayMode p_display_mode);
178
void _toggle_display_mode();
179
void _toggle_instant_preview(bool p_pressed);
180
void _toggle_include_addons(bool p_pressed);
181
void _toggle_fuzzy_search(bool p_pressed);
182
void _menu_option(int p_option);
183
184
String _get_cache_file_path() const;
185
186
static void _bind_methods();
187
};
188
189
class QuickOpenResultGridItem : public MarginContainer {
190
GDCLASS(QuickOpenResultGridItem, MarginContainer)
191
192
public:
193
QuickOpenResultGridItem();
194
195
void reset();
196
void set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight);
197
void highlight_item(const Color &p_color);
198
void remove_highlight();
199
200
private:
201
VBoxContainer *vbc = nullptr;
202
TextureRect *thumbnail = nullptr;
203
HighlightedLabel *name = nullptr;
204
};
205
206
class QuickOpenResultListItem : public MarginContainer {
207
GDCLASS(QuickOpenResultListItem, MarginContainer)
208
209
public:
210
QuickOpenResultListItem();
211
212
void reset();
213
void set_content(const QuickOpenResultCandidate &p_candidate, bool p_highlight);
214
void highlight_item(const Color &p_color);
215
void remove_highlight();
216
217
protected:
218
void _notification(int p_what);
219
220
private:
221
HBoxContainer *hbc = nullptr;
222
VBoxContainer *text_container = nullptr;
223
224
TextureRect *thumbnail = nullptr;
225
HighlightedLabel *name = nullptr;
226
HighlightedLabel *path = nullptr;
227
};
228
229
class QuickOpenResultItem : public HBoxContainer {
230
GDCLASS(QuickOpenResultItem, HBoxContainer)
231
232
public:
233
QuickOpenResultItem();
234
235
bool enable_highlights = true;
236
237
void reset();
238
void set_content(const QuickOpenResultCandidate &p_candidate);
239
void set_display_mode(QuickOpenDisplayMode p_display_mode);
240
void highlight_item(bool p_enabled);
241
242
protected:
243
void _notification(int p_what);
244
245
private:
246
QuickOpenResultListItem *list_item = nullptr;
247
QuickOpenResultGridItem *grid_item = nullptr;
248
249
Ref<StyleBox> selected_stylebox;
250
Ref<StyleBox> hovering_stylebox;
251
Color highlighted_font_color;
252
253
bool is_hovering = false;
254
bool is_selected = false;
255
256
void _set_enabled(bool p_enabled);
257
};
258
259
class EditorQuickOpenDialog : public AcceptDialog {
260
GDCLASS(EditorQuickOpenDialog, AcceptDialog);
261
262
public:
263
void popup_dialog(const Vector<StringName> &p_base_types, const Callable &p_item_selected_callback);
264
void popup_dialog_for_property(const Vector<StringName> &p_base_types, Object *p_obj, const StringName &p_path, const Callable &p_item_selected_callback);
265
EditorQuickOpenDialog();
266
267
protected:
268
virtual void cancel_pressed() override;
269
virtual void ok_pressed() override;
270
void item_pressed(bool p_double_click);
271
void selection_changed();
272
273
private:
274
static String get_dialog_title(const Vector<StringName> &p_base_types);
275
276
LineEdit *search_box = nullptr;
277
QuickOpenResultContainer *container = nullptr;
278
279
Callable item_selected_callback;
280
281
Object *property_object = nullptr;
282
StringName property_path;
283
Variant initial_property_value;
284
bool initial_selection_performed = false;
285
bool _is_instant_preview_active() const;
286
void _search_box_text_changed(const String &p_query);
287
void _finish_dialog_setup(const Vector<StringName> &p_base_types);
288
289
void preview_property();
290
void update_property();
291
};
292
293