editor_help.h 12 KB

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