editor_help.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. #ifndef EDITOR_HELP_H
  31. #define EDITOR_HELP_H
  32. #include "core/os/thread.h"
  33. #include "editor/doc_tools.h"
  34. #include "editor/plugins/editor_plugin.h"
  35. #include "scene/gui/dialogs.h"
  36. #include "scene/gui/popup.h"
  37. #include "scene/gui/rich_text_label.h"
  38. #include "scene/gui/split_container.h"
  39. #include "scene/gui/text_edit.h"
  40. #include "scene/main/timer.h"
  41. #include "modules/modules_enabled.gen.h" // For gdscript, mono.
  42. class FindBar : public HBoxContainer {
  43. GDCLASS(FindBar, HBoxContainer);
  44. LineEdit *search_text = nullptr;
  45. Button *find_prev = nullptr;
  46. Button *find_next = nullptr;
  47. Label *matches_label = nullptr;
  48. TextureButton *hide_button = nullptr;
  49. String prev_search;
  50. RichTextLabel *rich_text_label = nullptr;
  51. int results_count = 0;
  52. virtual void input(const Ref<InputEvent> &p_event) override;
  53. void _hide_bar();
  54. void _search_text_changed(const String &p_text);
  55. void _search_text_submitted(const String &p_text);
  56. void _update_results_count();
  57. void _update_matches_label();
  58. protected:
  59. void _notification(int p_what);
  60. bool _search(bool p_search_previous = false);
  61. public:
  62. void set_rich_text_label(RichTextLabel *p_rich_text_label);
  63. void popup_search();
  64. bool search_prev();
  65. bool search_next();
  66. FindBar();
  67. };
  68. class EditorHelp : public VBoxContainer {
  69. GDCLASS(EditorHelp, VBoxContainer);
  70. enum MethodType {
  71. METHOD_TYPE_METHOD,
  72. METHOD_TYPE_CONSTRUCTOR,
  73. METHOD_TYPE_OPERATOR,
  74. METHOD_TYPE_MAX
  75. };
  76. bool select_locked = false;
  77. String prev_search;
  78. String edited_class;
  79. Vector<Pair<String, int>> section_line;
  80. HashMap<String, int> method_line;
  81. HashMap<String, int> signal_line;
  82. HashMap<String, int> property_line;
  83. HashMap<String, int> theme_property_line;
  84. HashMap<String, int> constant_line;
  85. HashMap<String, int> annotation_line;
  86. HashMap<String, int> enum_line;
  87. HashMap<String, HashMap<String, int>> enum_values_line;
  88. int description_line = 0;
  89. RichTextLabel *class_desc = nullptr;
  90. HSplitContainer *h_split = nullptr;
  91. static DocTools *doc;
  92. static DocTools *ext_doc;
  93. ConfirmationDialog *search_dialog = nullptr;
  94. LineEdit *search = nullptr;
  95. FindBar *find_bar = nullptr;
  96. HBoxContainer *status_bar = nullptr;
  97. Button *toggle_scripts_button = nullptr;
  98. String base_path;
  99. struct ThemeCache {
  100. Ref<StyleBox> background_style;
  101. Color text_color;
  102. Color title_color;
  103. Color headline_color;
  104. Color comment_color;
  105. Color symbol_color;
  106. Color value_color;
  107. Color qualifier_color;
  108. Color type_color;
  109. Color override_color;
  110. Ref<Font> doc_font;
  111. Ref<Font> doc_bold_font;
  112. Ref<Font> doc_italic_font;
  113. Ref<Font> doc_title_font;
  114. Ref<Font> doc_code_font;
  115. Ref<Font> doc_kbd_font;
  116. int doc_font_size = 0;
  117. int doc_title_font_size = 0;
  118. int doc_code_font_size = 0;
  119. int doc_kbd_font_size = 0;
  120. } theme_cache;
  121. int scroll_to = -1;
  122. void _help_callback(const String &p_topic);
  123. void _add_text(const String &p_bbcode);
  124. bool scroll_locked = false;
  125. //void _button_pressed(int p_idx);
  126. void _add_type(const String &p_type, const String &p_enum = String(), bool p_is_bitfield = false);
  127. void _add_type_icon(const String &p_type, int p_size = 0, const String &p_fallback = "");
  128. void _add_method(const DocData::MethodDoc &p_method, bool p_overview, bool p_override = true);
  129. void _add_bulletpoint();
  130. void _push_normal_font();
  131. void _pop_normal_font();
  132. void _push_title_font();
  133. void _pop_title_font();
  134. void _push_code_font();
  135. void _pop_code_font();
  136. void _class_desc_finished();
  137. void _class_list_select(const String &p_select);
  138. void _class_desc_select(const String &p_select);
  139. void _class_desc_input(const Ref<InputEvent> &p_input);
  140. void _class_desc_resized(bool p_force_update_theme);
  141. int display_margin = 0;
  142. Error _goto_desc(const String &p_class);
  143. //void _update_history_buttons();
  144. void _update_method_list(MethodType p_method_type, const Vector<DocData::MethodDoc> &p_methods);
  145. void _update_method_descriptions(const DocData::ClassDoc &p_classdoc, MethodType p_method_type, const Vector<DocData::MethodDoc> &p_methods);
  146. void _update_doc();
  147. void _request_help(const String &p_string);
  148. void _search(bool p_search_previous = false);
  149. void _toggle_scripts_pressed();
  150. static int doc_generation_count;
  151. static String doc_version_hash;
  152. static Thread worker_thread;
  153. static void _wait_for_thread();
  154. static void _load_doc_thread(void *p_udata);
  155. static void _gen_doc_thread(void *p_udata);
  156. static void _gen_extensions_docs();
  157. static void _compute_doc_version_hash();
  158. struct PropertyCompare {
  159. _FORCE_INLINE_ bool operator()(const DocData::PropertyDoc &p_l, const DocData::PropertyDoc &p_r) const {
  160. // Sort overridden properties above all else.
  161. if (p_l.overridden == p_r.overridden) {
  162. return p_l.name.naturalcasecmp_to(p_r.name) < 0;
  163. }
  164. return p_l.overridden;
  165. }
  166. };
  167. protected:
  168. virtual void _update_theme_item_cache() override;
  169. void _notification(int p_what);
  170. static void _bind_methods();
  171. public:
  172. static void generate_doc(bool p_use_cache = true);
  173. static DocTools *get_doc_data();
  174. static void cleanup_doc();
  175. static String get_cache_full_path();
  176. static void load_xml_buffer(const uint8_t *p_buffer, int p_size);
  177. static void remove_class(const String &p_class);
  178. void go_to_help(const String &p_help);
  179. void go_to_class(const String &p_class);
  180. void update_doc();
  181. Vector<Pair<String, int>> get_sections();
  182. void scroll_to_section(int p_section_index);
  183. void popup_search();
  184. void search_again(bool p_search_previous = false);
  185. String get_class();
  186. void set_focused() { class_desc->grab_focus(); }
  187. int get_scroll() const;
  188. void set_scroll(int p_scroll);
  189. void update_toggle_scripts_button();
  190. static void init_gdext_pointers();
  191. EditorHelp();
  192. ~EditorHelp();
  193. };
  194. class EditorHelpBit : public VBoxContainer {
  195. GDCLASS(EditorHelpBit, VBoxContainer);
  196. enum SymbolHint {
  197. SYMBOL_HINT_NONE,
  198. SYMBOL_HINT_INHERITANCE, // [ < ParentClass[ < ...]]
  199. SYMBOL_HINT_ASSIGNABLE, // [: Type][ = value]
  200. SYMBOL_HINT_SIGNATURE, // (arguments)[ -> Type][ qualifiers]
  201. };
  202. struct DocType {
  203. String type;
  204. String enumeration;
  205. bool is_bitfield = false;
  206. };
  207. struct ArgumentData {
  208. String name;
  209. DocType doc_type;
  210. String default_value;
  211. };
  212. struct HelpData {
  213. String description;
  214. String deprecated_message;
  215. String experimental_message;
  216. DocType doc_type;
  217. String value;
  218. Vector<ArgumentData> arguments;
  219. String qualifiers;
  220. String resource_path;
  221. };
  222. inline static HashMap<StringName, HelpData> doc_class_cache;
  223. inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_enum_cache;
  224. inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_constant_cache;
  225. inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_property_cache;
  226. inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_theme_item_cache;
  227. inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_method_cache;
  228. inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_signal_cache;
  229. inline static HashMap<StringName, HashMap<StringName, HelpData>> doc_annotation_cache;
  230. RichTextLabel *title = nullptr;
  231. RichTextLabel *content = nullptr;
  232. bool use_class_prefix = false;
  233. String symbol_doc_link;
  234. String symbol_class_name;
  235. String symbol_type;
  236. String symbol_name;
  237. SymbolHint symbol_hint = SYMBOL_HINT_NONE;
  238. HelpData help_data;
  239. float content_min_height = 0.0;
  240. float content_max_height = 0.0;
  241. static HelpData _get_class_help_data(const StringName &p_class_name);
  242. static HelpData _get_enum_help_data(const StringName &p_class_name, const StringName &p_enum_name);
  243. static HelpData _get_constant_help_data(const StringName &p_class_name, const StringName &p_constant_name);
  244. static HelpData _get_property_help_data(const StringName &p_class_name, const StringName &p_property_name);
  245. static HelpData _get_theme_item_help_data(const StringName &p_class_name, const StringName &p_theme_item_name);
  246. static HelpData _get_method_help_data(const StringName &p_class_name, const StringName &p_method_name);
  247. static HelpData _get_signal_help_data(const StringName &p_class_name, const StringName &p_signal_name);
  248. static HelpData _get_annotation_help_data(const StringName &p_class_name, const StringName &p_annotation_name);
  249. void _add_type_to_title(const DocType &p_doc_type);
  250. void _update_labels();
  251. void _go_to_help(const String &p_what);
  252. void _meta_clicked(const String &p_select);
  253. protected:
  254. static void _bind_methods();
  255. void _notification(int p_what);
  256. public:
  257. void parse_symbol(const String &p_symbol, const String &p_prologue = String());
  258. void set_custom_text(const String &p_type, const String &p_name, const String &p_description);
  259. void set_content_height_limits(float p_min, float p_max);
  260. void update_content_height();
  261. EditorHelpBit(const String &p_symbol = String(), const String &p_prologue = String(), bool p_use_class_prefix = false, bool p_allow_selection = true);
  262. };
  263. // Standard tooltips do not allow you to hover over them.
  264. // This class is intended as a temporary workaround.
  265. class EditorHelpBitTooltip : public PopupPanel {
  266. GDCLASS(EditorHelpBitTooltip, PopupPanel);
  267. static bool _is_tooltip_visible;
  268. Timer *timer = nullptr;
  269. uint64_t _enter_tree_time = 0;
  270. bool _is_mouse_inside_tooltip = false;
  271. static Control *_make_invisible_control();
  272. void _start_timer();
  273. void _target_gui_input(const Ref<InputEvent> &p_event);
  274. protected:
  275. void _notification(int p_what);
  276. public:
  277. static Control *show_tooltip(Control *p_target, const String &p_symbol, const String &p_prologue = String(), bool p_use_class_prefix = false);
  278. void popup_under_cursor();
  279. EditorHelpBitTooltip(Control *p_target);
  280. };
  281. #if defined(MODULE_GDSCRIPT_ENABLED) || defined(MODULE_MONO_ENABLED)
  282. class EditorSyntaxHighlighter;
  283. class EditorHelpHighlighter {
  284. public:
  285. enum Language {
  286. LANGUAGE_GDSCRIPT,
  287. LANGUAGE_CSHARP,
  288. LANGUAGE_MAX,
  289. };
  290. private:
  291. using HighlightData = Vector<Pair<int, Color>>;
  292. static EditorHelpHighlighter *singleton;
  293. HashMap<String, HighlightData> highlight_data_caches[LANGUAGE_MAX];
  294. TextEdit *text_edits[LANGUAGE_MAX];
  295. Ref<Script> scripts[LANGUAGE_MAX];
  296. Ref<EditorSyntaxHighlighter> highlighters[LANGUAGE_MAX];
  297. HighlightData _get_highlight_data(Language p_language, const String &p_source, bool p_use_cache);
  298. public:
  299. static void create_singleton();
  300. static void free_singleton();
  301. static EditorHelpHighlighter *get_singleton();
  302. void highlight(RichTextLabel *p_rich_text_label, Language p_language, const String &p_source, bool p_use_cache);
  303. void reset_cache();
  304. EditorHelpHighlighter();
  305. virtual ~EditorHelpHighlighter();
  306. };
  307. #endif // defined(MODULE_GDSCRIPT_ENABLED) || defined(MODULE_MONO_ENABLED)
  308. #endif // EDITOR_HELP_H