Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/doc/editor_help.h
20956 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
inline static DocTools *doc = nullptr;
116
inline static DocTools *ext_doc = nullptr;
117
118
LineEdit *search = nullptr;
119
FindBar *find_bar = nullptr;
120
HBoxContainer *status_bar = nullptr;
121
Button *toggle_files_button = nullptr;
122
123
struct ThemeCache {
124
Ref<StyleBox> background_style;
125
126
Color text_color;
127
Color title_color;
128
Color headline_color;
129
Color comment_color;
130
Color symbol_color;
131
Color value_color;
132
Color qualifier_color;
133
Color type_color;
134
Color override_color;
135
Color primary_hr_color;
136
Color secondary_hr_color;
137
138
Ref<Font> doc_font;
139
Ref<Font> doc_bold_font;
140
Ref<Font> doc_italic_font;
141
Ref<Font> doc_title_font;
142
Ref<Font> doc_code_font;
143
Ref<Font> doc_kbd_font;
144
145
int doc_font_size = 0;
146
int doc_title_font_size = 0;
147
int doc_code_font_size = 0;
148
int doc_kbd_font_size = 0;
149
} theme_cache;
150
151
int scroll_to = -1;
152
153
void _help_callback(const String &p_topic);
154
155
void _add_text(const String &p_bbcode);
156
bool scroll_locked = false;
157
158
//void _button_pressed(int p_idx);
159
void _add_type(const String &p_type, const String &p_enum = String(), bool p_is_bitfield = false);
160
void _add_type_icon(const String &p_type, int p_size = 0, const String &p_fallback = "");
161
void _add_method(const DocData::MethodDoc &p_method, bool p_overview, bool p_override = true);
162
163
void _add_bulletpoint();
164
165
void _push_normal_font();
166
void _pop_normal_font();
167
void _push_title_font();
168
void _pop_title_font();
169
void _push_code_font();
170
void _pop_code_font();
171
172
void _class_desc_finished();
173
void _class_list_select(const String &p_select);
174
void _class_desc_select(const String &p_select);
175
void _class_desc_input(const Ref<InputEvent> &p_input);
176
void _class_desc_resized(bool p_force_update_theme);
177
int display_margin = 0;
178
179
Error _goto_desc(const String &p_class);
180
//void _update_history_buttons();
181
void _update_method_list(MethodType p_method_type, const Vector<DocData::MethodDoc> &p_methods);
182
void _update_method_descriptions(const DocData::ClassDoc &p_classdoc, MethodType p_method_type, const Vector<DocData::MethodDoc> &p_methods);
183
void _update_doc();
184
185
void _request_help(const String &p_string);
186
void _search(bool p_search_previous = false);
187
188
void _toggle_files_pressed();
189
190
inline static int doc_generation_count = 0;
191
inline static String doc_version_hash;
192
inline static Thread worker_thread;
193
inline static Thread loader_thread; // Only load scripts here to avoid deadlocking with main thread.
194
195
inline static SafeFlag _script_docs_loaded = SafeFlag(false);
196
inline static LocalVector<DocData::ClassDoc> _docs_to_add;
197
inline static LocalVector<String> _docs_to_remove;
198
inline static LocalVector<String> _docs_to_remove_by_path;
199
200
static void _wait_for_thread(Thread &p_thread = worker_thread);
201
static void _load_doc_thread(void *p_udata);
202
static void _gen_doc_thread(void *p_udata);
203
static void _gen_extensions_docs();
204
static void _process_postponed_docs();
205
static void _load_script_doc_cache_thread(void *p_udata);
206
static void _regen_script_doc_thread(void *p_udata);
207
static void _finish_regen_script_doc_thread(void *p_udata);
208
static void _reload_scripts_documentation(EditorFileSystemDirectory *p_dir);
209
static void _delete_script_doc_cache();
210
static void _compute_doc_version_hash();
211
212
struct PropertyCompare {
213
_FORCE_INLINE_ bool operator()(const DocData::PropertyDoc &p_l, const DocData::PropertyDoc &p_r) const {
214
// Sort overridden properties above all else.
215
if (p_l.overridden == p_r.overridden) {
216
return p_l.name.naturalcasecmp_to(p_r.name) < 0;
217
}
218
return p_l.overridden;
219
}
220
};
221
222
protected:
223
virtual void _update_theme_item_cache() override;
224
225
void _notification(int p_what);
226
static void _bind_methods();
227
228
public:
229
static void generate_doc(bool p_use_cache = true, bool p_use_script_cache = true);
230
static void cleanup_doc();
231
static void load_script_doc_cache();
232
static void regenerate_script_doc_cache();
233
static void save_script_doc_cache();
234
static String get_cache_full_path();
235
static String get_script_doc_cache_full_path();
236
237
// Adding scripts to DocData directly may make script doc cache inconsistent. Use methods below when adding script docs.
238
// Usage during startup can also cause deadlocks.
239
static DocTools *get_doc_data();
240
// Method forwarding to underlying DocTools to keep script doc cache consistent.
241
static DocData::ClassDoc *get_doc(const String &p_class_name);
242
static void add_doc(const DocData::ClassDoc &p_class_doc);
243
static void remove_doc(const String &p_class_name);
244
static void remove_script_doc_by_path(const String &p_path);
245
static bool has_doc(const String &p_class_name);
246
247
static void load_xml_buffer(const uint8_t *p_buffer, int p_size);
248
static void remove_class(const String &p_class);
249
250
void go_to_help(const String &p_help);
251
void go_to_class(const String &p_class);
252
void update_doc();
253
254
Vector<Pair<String, int>> get_sections();
255
void scroll_to_section(int p_section_index);
256
257
void popup_search();
258
void search_again(bool p_search_previous = false);
259
260
String get_class();
261
262
void set_focused() { class_desc->grab_focus(); }
263
264
int get_scroll() const;
265
void set_scroll(int p_scroll);
266
267
void update_toggle_files_button();
268
269
static void init_gdext_pointers();
270
271
EditorHelp();
272
};
273
274
class EditorHelpBit : public VBoxContainer {
275
GDCLASS(EditorHelpBit, VBoxContainer);
276
277
enum SymbolHint {
278
SYMBOL_HINT_NONE,
279
SYMBOL_HINT_INHERITANCE, // [ < ParentClass[ < ...]]
280
SYMBOL_HINT_ASSIGNABLE, // [: Type][ = value]
281
SYMBOL_HINT_SIGNATURE, // (arguments)[ -> Type][ qualifiers]
282
};
283
284
struct DocType {
285
String type;
286
String enumeration;
287
bool is_bitfield = false;
288
};
289
290
struct ArgumentData {
291
String name;
292
DocType doc_type;
293
String default_value;
294
};
295
296
struct HelpData {
297
String description;
298
String deprecated_message;
299
String experimental_message;
300
DocType doc_type;
301
String value;
302
Vector<ArgumentData> arguments;
303
ArgumentData rest_argument;
304
String qualifiers;
305
String resource_path;
306
};
307
308
inline static HashMap<StringName, HelpData> doc_class_cache;
309
inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_enum_cache;
310
inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_constant_cache;
311
inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_property_cache;
312
inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_theme_item_cache;
313
inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_method_cache;
314
inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_signal_cache;
315
inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_annotation_cache;
316
317
RichTextLabel *title = nullptr;
318
RichTextLabel *content = nullptr;
319
320
bool use_class_prefix = false;
321
322
String symbol_doc_link;
323
String symbol_class_name;
324
String symbol_type;
325
String symbol_name;
326
SymbolHint symbol_hint = SYMBOL_HINT_NONE;
327
328
HelpData help_data;
329
330
float content_min_height = 0.0;
331
float content_max_height = 0.0;
332
333
static HelpData _get_class_help_data(const StringName &p_class_name);
334
static HelpData _get_enum_help_data(const StringName &p_class_name, const StringName &p_enum_name);
335
static HelpData _get_constant_help_data(const StringName &p_class_name, const StringName &p_constant_name);
336
static HelpData _get_property_help_data(const StringName &p_class_name, const StringName &p_property_name);
337
static HelpData _get_theme_item_help_data(const StringName &p_class_name, const StringName &p_theme_item_name);
338
static HelpData _get_method_help_data(const StringName &p_class_name, const StringName &p_method_name);
339
static HelpData _get_signal_help_data(const StringName &p_class_name, const StringName &p_signal_name);
340
static HelpData _get_annotation_help_data(const StringName &p_class_name, const StringName &p_annotation_name);
341
342
void _add_type_to_title(const DocType &p_doc_type);
343
void _update_labels();
344
void _go_to_help(const String &p_what);
345
void _go_to_url(const String &p_what);
346
void _meta_clicked(const String &p_select);
347
348
protected:
349
static void _bind_methods();
350
void _notification(int p_what);
351
352
public:
353
void parse_symbol(const String &p_symbol, const String &p_prologue = String());
354
void set_custom_text(const String &p_type, const String &p_name, const String &p_description);
355
356
void set_content_height_limits(float p_min, float p_max);
357
void update_content_height();
358
359
EditorHelpBit(const String &p_symbol = String(), const String &p_prologue = String(), bool p_use_class_prefix = false, bool p_allow_selection = true, bool p_in_tooltip = false);
360
};
361
362
// Standard tooltips do not allow you to hover over them.
363
// This class is intended as a temporary workaround.
364
class EditorHelpBitTooltip : public PopupPanel {
365
GDCLASS(EditorHelpBitTooltip, PopupPanel);
366
367
static bool _is_tooltip_visible;
368
369
Timer *timer = nullptr;
370
uint64_t _enter_tree_time = 0;
371
bool _is_mouse_inside_tooltip = false;
372
373
static Control *_make_invisible_control();
374
375
void _start_timer();
376
void _target_gui_input(const Ref<InputEvent> &p_event);
377
378
protected:
379
void _notification(int p_what);
380
381
public:
382
// The returned control is an orphan node, which is to make the standard tooltip invisible.
383
[[nodiscard]] static Control *make_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