Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/doc/editor_help.h
9903 views
1
/**************************************************************************/
2
/* editor_help.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/os/thread.h"
34
#include "editor/doc/doc_tools.h"
35
#include "editor/plugins/editor_plugin.h"
36
#include "scene/gui/dialogs.h"
37
#include "scene/gui/popup.h"
38
#include "scene/gui/rich_text_label.h"
39
#include "scene/gui/split_container.h"
40
#include "scene/gui/text_edit.h"
41
#include "scene/main/timer.h"
42
43
class FindBar : public HBoxContainer {
44
GDCLASS(FindBar, HBoxContainer);
45
46
LineEdit *search_text = nullptr;
47
Button *find_prev = nullptr;
48
Button *find_next = nullptr;
49
Label *matches_label = nullptr;
50
Button *hide_button = nullptr;
51
52
RichTextLabel *rich_text_label = nullptr;
53
54
String prev_search;
55
int results_count = 0;
56
int results_count_to_current = 0;
57
58
virtual void input(const Ref<InputEvent> &p_event) override;
59
60
void _hide_bar();
61
62
void _search_text_changed(const String &p_text);
63
void _search_text_submitted(const String &p_text);
64
65
void _update_results_count(bool p_search_previous);
66
void _update_matches_label();
67
68
protected:
69
void _notification(int p_what);
70
71
bool _search(bool p_search_previous = false);
72
73
public:
74
void set_rich_text_label(RichTextLabel *p_rich_text_label);
75
76
void popup_search();
77
78
bool search_prev();
79
bool search_next();
80
81
FindBar();
82
};
83
84
class EditorFileSystemDirectory;
85
86
class EditorHelp : public VBoxContainer {
87
GDCLASS(EditorHelp, VBoxContainer);
88
89
enum MethodType {
90
METHOD_TYPE_METHOD,
91
METHOD_TYPE_CONSTRUCTOR,
92
METHOD_TYPE_OPERATOR,
93
METHOD_TYPE_MAX
94
};
95
96
bool select_locked = false;
97
bool update_pending = false;
98
99
String prev_search;
100
101
String edited_class;
102
103
Vector<Pair<String, int>> section_line;
104
HashMap<String, int> method_line;
105
HashMap<String, int> signal_line;
106
HashMap<String, int> property_line;
107
HashMap<String, int> theme_property_line;
108
HashMap<String, int> constant_line;
109
HashMap<String, int> annotation_line;
110
HashMap<String, int> enum_line;
111
HashMap<String, HashMap<String, int>> enum_values_line;
112
int description_line = 0;
113
114
RichTextLabel *class_desc = nullptr;
115
HSplitContainer *h_split = nullptr;
116
inline static DocTools *doc = nullptr;
117
inline static DocTools *ext_doc = nullptr;
118
119
ConfirmationDialog *search_dialog = nullptr;
120
LineEdit *search = nullptr;
121
FindBar *find_bar = nullptr;
122
HBoxContainer *status_bar = nullptr;
123
Button *toggle_files_button = nullptr;
124
125
String base_path;
126
127
struct ThemeCache {
128
Ref<StyleBox> background_style;
129
130
Color text_color;
131
Color title_color;
132
Color headline_color;
133
Color comment_color;
134
Color symbol_color;
135
Color value_color;
136
Color qualifier_color;
137
Color type_color;
138
Color override_color;
139
140
Ref<Font> doc_font;
141
Ref<Font> doc_bold_font;
142
Ref<Font> doc_italic_font;
143
Ref<Font> doc_title_font;
144
Ref<Font> doc_code_font;
145
Ref<Font> doc_kbd_font;
146
147
int doc_font_size = 0;
148
int doc_title_font_size = 0;
149
int doc_code_font_size = 0;
150
int doc_kbd_font_size = 0;
151
} theme_cache;
152
153
int scroll_to = -1;
154
155
void _help_callback(const String &p_topic);
156
157
void _add_text(const String &p_bbcode);
158
bool scroll_locked = false;
159
160
//void _button_pressed(int p_idx);
161
void _add_type(const String &p_type, const String &p_enum = String(), bool p_is_bitfield = false);
162
void _add_type_icon(const String &p_type, int p_size = 0, const String &p_fallback = "");
163
void _add_method(const DocData::MethodDoc &p_method, bool p_overview, bool p_override = true);
164
165
void _add_bulletpoint();
166
167
void _push_normal_font();
168
void _pop_normal_font();
169
void _push_title_font();
170
void _pop_title_font();
171
void _push_code_font();
172
void _pop_code_font();
173
174
void _class_desc_finished();
175
void _class_list_select(const String &p_select);
176
void _class_desc_select(const String &p_select);
177
void _class_desc_input(const Ref<InputEvent> &p_input);
178
void _class_desc_resized(bool p_force_update_theme);
179
int display_margin = 0;
180
181
Error _goto_desc(const String &p_class);
182
//void _update_history_buttons();
183
void _update_method_list(MethodType p_method_type, const Vector<DocData::MethodDoc> &p_methods);
184
void _update_method_descriptions(const DocData::ClassDoc &p_classdoc, MethodType p_method_type, const Vector<DocData::MethodDoc> &p_methods);
185
void _update_doc();
186
187
void _request_help(const String &p_string);
188
void _search(bool p_search_previous = false);
189
190
void _toggle_files_pressed();
191
192
inline static int doc_generation_count = 0;
193
inline static String doc_version_hash;
194
inline static Thread worker_thread;
195
inline static Thread loader_thread; // Only load scripts here to avoid deadlocking with main thread.
196
197
inline static SafeFlag _script_docs_loaded = SafeFlag(false);
198
inline static LocalVector<DocData::ClassDoc> _docs_to_add;
199
inline static LocalVector<String> _docs_to_remove;
200
inline static LocalVector<String> _docs_to_remove_by_path;
201
202
static void _wait_for_thread(Thread &p_thread = worker_thread);
203
static void _load_doc_thread(void *p_udata);
204
static void _gen_doc_thread(void *p_udata);
205
static void _gen_extensions_docs();
206
static void _process_postponed_docs();
207
static void _load_script_doc_cache_thread(void *p_udata);
208
static void _regen_script_doc_thread(void *p_udata);
209
static void _finish_regen_script_doc_thread(void *p_udata);
210
static void _reload_scripts_documentation(EditorFileSystemDirectory *p_dir);
211
static void _delete_script_doc_cache();
212
static void _compute_doc_version_hash();
213
214
struct PropertyCompare {
215
_FORCE_INLINE_ bool operator()(const DocData::PropertyDoc &p_l, const DocData::PropertyDoc &p_r) const {
216
// Sort overridden properties above all else.
217
if (p_l.overridden == p_r.overridden) {
218
return p_l.name.naturalcasecmp_to(p_r.name) < 0;
219
}
220
return p_l.overridden;
221
}
222
};
223
224
protected:
225
virtual void _update_theme_item_cache() override;
226
227
void _notification(int p_what);
228
static void _bind_methods();
229
230
public:
231
static void generate_doc(bool p_use_cache = true, bool p_use_script_cache = true);
232
static void cleanup_doc();
233
static void load_script_doc_cache();
234
static void regenerate_script_doc_cache();
235
static void save_script_doc_cache();
236
static String get_cache_full_path();
237
static String get_script_doc_cache_full_path();
238
239
// Adding scripts to DocData directly may make script doc cache inconsistent. Use methods below when adding script docs.
240
// Usage during startup can also cause deadlocks.
241
static DocTools *get_doc_data();
242
// Method forwarding to underlying DocTools to keep script doc cache consistent.
243
static DocData::ClassDoc *get_doc(const String &p_class_name);
244
static void add_doc(const DocData::ClassDoc &p_class_doc);
245
static void remove_doc(const String &p_class_name);
246
static void remove_script_doc_by_path(const String &p_path);
247
static bool has_doc(const String &p_class_name);
248
249
static void load_xml_buffer(const uint8_t *p_buffer, int p_size);
250
static void remove_class(const String &p_class);
251
252
void go_to_help(const String &p_help);
253
void go_to_class(const String &p_class);
254
void update_doc();
255
256
Vector<Pair<String, int>> get_sections();
257
void scroll_to_section(int p_section_index);
258
259
void popup_search();
260
void search_again(bool p_search_previous = false);
261
262
String get_class();
263
264
void set_focused() { class_desc->grab_focus(); }
265
266
int get_scroll() const;
267
void set_scroll(int p_scroll);
268
269
void update_toggle_files_button();
270
271
static void init_gdext_pointers();
272
273
EditorHelp();
274
};
275
276
class EditorHelpBit : public VBoxContainer {
277
GDCLASS(EditorHelpBit, VBoxContainer);
278
279
enum SymbolHint {
280
SYMBOL_HINT_NONE,
281
SYMBOL_HINT_INHERITANCE, // [ < ParentClass[ < ...]]
282
SYMBOL_HINT_ASSIGNABLE, // [: Type][ = value]
283
SYMBOL_HINT_SIGNATURE, // (arguments)[ -> Type][ qualifiers]
284
};
285
286
struct DocType {
287
String type;
288
String enumeration;
289
bool is_bitfield = false;
290
};
291
292
struct ArgumentData {
293
String name;
294
DocType doc_type;
295
String default_value;
296
};
297
298
struct HelpData {
299
String description;
300
String deprecated_message;
301
String experimental_message;
302
DocType doc_type;
303
String value;
304
Vector<ArgumentData> arguments;
305
ArgumentData rest_argument;
306
String qualifiers;
307
String resource_path;
308
};
309
310
inline static HashMap<StringName, HelpData> doc_class_cache;
311
inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_enum_cache;
312
inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_constant_cache;
313
inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_property_cache;
314
inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_theme_item_cache;
315
inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_method_cache;
316
inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_signal_cache;
317
inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_annotation_cache;
318
319
RichTextLabel *title = nullptr;
320
RichTextLabel *content = nullptr;
321
322
bool use_class_prefix = false;
323
324
String symbol_doc_link;
325
String symbol_class_name;
326
String symbol_type;
327
String symbol_name;
328
SymbolHint symbol_hint = SYMBOL_HINT_NONE;
329
330
HelpData help_data;
331
332
float content_min_height = 0.0;
333
float content_max_height = 0.0;
334
335
static HelpData _get_class_help_data(const StringName &p_class_name);
336
static HelpData _get_enum_help_data(const StringName &p_class_name, const StringName &p_enum_name);
337
static HelpData _get_constant_help_data(const StringName &p_class_name, const StringName &p_constant_name);
338
static HelpData _get_property_help_data(const StringName &p_class_name, const StringName &p_property_name);
339
static HelpData _get_theme_item_help_data(const StringName &p_class_name, const StringName &p_theme_item_name);
340
static HelpData _get_method_help_data(const StringName &p_class_name, const StringName &p_method_name);
341
static HelpData _get_signal_help_data(const StringName &p_class_name, const StringName &p_signal_name);
342
static HelpData _get_annotation_help_data(const StringName &p_class_name, const StringName &p_annotation_name);
343
344
void _add_type_to_title(const DocType &p_doc_type);
345
void _update_labels();
346
void _go_to_help(const String &p_what);
347
void _meta_clicked(const String &p_select);
348
349
protected:
350
static void _bind_methods();
351
void _notification(int p_what);
352
353
public:
354
void parse_symbol(const String &p_symbol, const String &p_prologue = String());
355
void set_custom_text(const String &p_type, const String &p_name, const String &p_description);
356
357
void set_content_height_limits(float p_min, float p_max);
358
void update_content_height();
359
360
EditorHelpBit(const String &p_symbol = String(), const String &p_prologue = String(), bool p_use_class_prefix = false, bool p_allow_selection = true);
361
};
362
363
// Standard tooltips do not allow you to hover over them.
364
// This class is intended as a temporary workaround.
365
class EditorHelpBitTooltip : public PopupPanel {
366
GDCLASS(EditorHelpBitTooltip, PopupPanel);
367
368
static bool _is_tooltip_visible;
369
370
Timer *timer = nullptr;
371
uint64_t _enter_tree_time = 0;
372
bool _is_mouse_inside_tooltip = false;
373
374
static Control *_make_invisible_control();
375
376
void _start_timer();
377
void _target_gui_input(const Ref<InputEvent> &p_event);
378
379
protected:
380
void _notification(int p_what);
381
382
public:
383
static Control *show_tooltip(Control *p_target, const String &p_symbol, const String &p_prologue = String(), bool p_use_class_prefix = false);
384
385
void popup_under_cursor();
386
387
EditorHelpBitTooltip(Control *p_target);
388
};
389
390
class EditorSyntaxHighlighter;
391
392
class EditorHelpHighlighter {
393
public:
394
enum Language {
395
LANGUAGE_GDSCRIPT,
396
LANGUAGE_CSHARP,
397
LANGUAGE_MAX,
398
};
399
400
private:
401
using HighlightData = Vector<Pair<int, Color>>;
402
403
static EditorHelpHighlighter *singleton;
404
405
HashMap<String, HighlightData> highlight_data_caches[LANGUAGE_MAX];
406
407
TextEdit *text_edits[LANGUAGE_MAX];
408
Ref<Script> scripts[LANGUAGE_MAX];
409
Ref<EditorSyntaxHighlighter> highlighters[LANGUAGE_MAX];
410
411
HighlightData _get_highlight_data(Language p_language, const String &p_source, bool p_use_cache);
412
413
public:
414
static void create_singleton();
415
static void free_singleton();
416
static EditorHelpHighlighter *get_singleton();
417
418
void highlight(RichTextLabel *p_rich_text_label, Language p_language, const String &p_source, bool p_use_cache);
419
void reset_cache();
420
421
EditorHelpHighlighter();
422
virtual ~EditorHelpHighlighter();
423
};
424
425