editor_help.cpp 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589
  1. /**************************************************************************/
  2. /* editor_help.cpp */
  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. #include "editor_help.h"
  31. #include "core/core_constants.h"
  32. #include "core/input/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/version_generated.gen.h"
  35. #include "doc_data_compressed.gen.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_scale.h"
  38. #include "editor/editor_settings.h"
  39. #include "editor/plugins/script_editor_plugin.h"
  40. #include "scene/gui/line_edit.h"
  41. #define CONTRIBUTE_URL vformat("%s/contributing/documentation/updating_the_class_reference.html", VERSION_DOCS_URL)
  42. DocTools *EditorHelp::doc = nullptr;
  43. void EditorHelp::_update_theme_item_cache() {
  44. VBoxContainer::_update_theme_item_cache();
  45. theme_cache.text_color = get_theme_color(SNAME("text_color"), SNAME("EditorHelp"));
  46. theme_cache.title_color = get_theme_color(SNAME("title_color"), SNAME("EditorHelp"));
  47. theme_cache.headline_color = get_theme_color(SNAME("headline_color"), SNAME("EditorHelp"));
  48. theme_cache.comment_color = get_theme_color(SNAME("comment_color"), SNAME("EditorHelp"));
  49. theme_cache.symbol_color = get_theme_color(SNAME("symbol_color"), SNAME("EditorHelp"));
  50. theme_cache.value_color = get_theme_color(SNAME("value_color"), SNAME("EditorHelp"));
  51. theme_cache.qualifier_color = get_theme_color(SNAME("qualifier_color"), SNAME("EditorHelp"));
  52. theme_cache.type_color = get_theme_color(SNAME("type_color"), SNAME("EditorHelp"));
  53. theme_cache.doc_font = get_theme_font(SNAME("doc"), SNAME("EditorFonts"));
  54. theme_cache.doc_bold_font = get_theme_font(SNAME("doc_bold"), SNAME("EditorFonts"));
  55. theme_cache.doc_italic_font = get_theme_font(SNAME("doc_italic"), SNAME("EditorFonts"));
  56. theme_cache.doc_title_font = get_theme_font(SNAME("doc_title"), SNAME("EditorFonts"));
  57. theme_cache.doc_code_font = get_theme_font(SNAME("doc_source"), SNAME("EditorFonts"));
  58. theme_cache.doc_kbd_font = get_theme_font(SNAME("doc_keyboard"), SNAME("EditorFonts"));
  59. theme_cache.doc_font_size = get_theme_font_size(SNAME("doc_size"), SNAME("EditorFonts"));
  60. theme_cache.doc_title_font_size = get_theme_font_size(SNAME("doc_title_size"), SNAME("EditorFonts"));
  61. theme_cache.doc_code_font_size = get_theme_font_size(SNAME("doc_source_size"), SNAME("EditorFonts"));
  62. theme_cache.doc_kbd_font_size = get_theme_font_size(SNAME("doc_keyboard_size"), SNAME("EditorFonts"));
  63. theme_cache.background_style = get_theme_stylebox(SNAME("background"), SNAME("EditorHelp"));
  64. class_desc->add_theme_font_override("normal_font", theme_cache.doc_font);
  65. class_desc->add_theme_font_size_override("normal_font_size", theme_cache.doc_font_size);
  66. class_desc->add_theme_color_override("selection_color", get_theme_color(SNAME("selection_color"), SNAME("EditorHelp")));
  67. class_desc->add_theme_constant_override("line_separation", get_theme_constant(SNAME("line_separation"), SNAME("EditorHelp")));
  68. class_desc->add_theme_constant_override("table_h_separation", get_theme_constant(SNAME("table_h_separation"), SNAME("EditorHelp")));
  69. class_desc->add_theme_constant_override("table_v_separation", get_theme_constant(SNAME("table_v_separation"), SNAME("EditorHelp")));
  70. class_desc->add_theme_constant_override("text_highlight_h_padding", get_theme_constant(SNAME("text_highlight_h_padding"), SNAME("EditorHelp")));
  71. class_desc->add_theme_constant_override("text_highlight_v_padding", get_theme_constant(SNAME("text_highlight_v_padding"), SNAME("EditorHelp")));
  72. }
  73. void EditorHelp::_search(bool p_search_previous) {
  74. if (p_search_previous) {
  75. find_bar->search_prev();
  76. } else {
  77. find_bar->search_next();
  78. }
  79. }
  80. void EditorHelp::_class_desc_finished() {
  81. if (scroll_to >= 0) {
  82. class_desc->scroll_to_paragraph(scroll_to);
  83. }
  84. scroll_to = -1;
  85. }
  86. void EditorHelp::_class_list_select(const String &p_select) {
  87. _goto_desc(p_select);
  88. }
  89. void EditorHelp::_class_desc_select(const String &p_select) {
  90. if (p_select.begins_with("$")) { //enum
  91. String select = p_select.substr(1, p_select.length());
  92. String class_name;
  93. if (select.contains(".")) {
  94. class_name = select.get_slice(".", 0);
  95. select = select.get_slice(".", 1);
  96. } else {
  97. class_name = "@GlobalScope";
  98. }
  99. emit_signal(SNAME("go_to_help"), "class_enum:" + class_name + ":" + select);
  100. return;
  101. } else if (p_select.begins_with("#")) {
  102. emit_signal(SNAME("go_to_help"), "class_name:" + p_select.substr(1, p_select.length()));
  103. return;
  104. } else if (p_select.begins_with("@")) {
  105. int tag_end = p_select.find(" ");
  106. String tag = p_select.substr(1, tag_end - 1);
  107. String link = p_select.substr(tag_end + 1, p_select.length()).lstrip(" ");
  108. String topic;
  109. HashMap<String, int> *table = nullptr;
  110. if (tag == "method") {
  111. topic = "class_method";
  112. table = &this->method_line;
  113. } else if (tag == "member") {
  114. topic = "class_property";
  115. table = &this->property_line;
  116. } else if (tag == "enum") {
  117. topic = "class_enum";
  118. table = &this->enum_line;
  119. } else if (tag == "signal") {
  120. topic = "class_signal";
  121. table = &this->signal_line;
  122. } else if (tag == "constant") {
  123. topic = "class_constant";
  124. table = &this->constant_line;
  125. } else if (tag == "annotation") {
  126. topic = "class_annotation";
  127. table = &this->annotation_line;
  128. } else if (tag == "theme_item") {
  129. topic = "theme_item";
  130. table = &this->theme_property_line;
  131. } else {
  132. return;
  133. }
  134. // Case order is important here to correctly handle edge cases like Variant.Type in @GlobalScope.
  135. if (table->has(link)) {
  136. // Found in the current page.
  137. if (class_desc->is_ready()) {
  138. class_desc->scroll_to_paragraph((*table)[link]);
  139. } else {
  140. scroll_to = (*table)[link];
  141. }
  142. } else {
  143. // Look for link in @GlobalScope.
  144. // Note that a link like @GlobalScope.enum_name will not be found in this section, only enum_name will be.
  145. if (topic == "class_enum") {
  146. const DocData::ClassDoc &cd = doc->class_list["@GlobalScope"];
  147. for (int i = 0; i < cd.constants.size(); i++) {
  148. if (cd.constants[i].enumeration == link) {
  149. // Found in @GlobalScope.
  150. emit_signal(SNAME("go_to_help"), topic + ":@GlobalScope:" + link);
  151. return;
  152. }
  153. }
  154. } else if (topic == "class_constant") {
  155. const DocData::ClassDoc &cd = doc->class_list["@GlobalScope"];
  156. for (int i = 0; i < cd.constants.size(); i++) {
  157. if (cd.constants[i].name == link) {
  158. // Found in @GlobalScope.
  159. emit_signal(SNAME("go_to_help"), topic + ":@GlobalScope:" + link);
  160. return;
  161. }
  162. }
  163. }
  164. if (link.contains(".")) {
  165. int class_end = link.find(".");
  166. emit_signal(SNAME("go_to_help"), topic + ":" + link.substr(0, class_end) + ":" + link.substr(class_end + 1, link.length()));
  167. }
  168. }
  169. } else if (p_select.begins_with("http")) {
  170. OS::get_singleton()->shell_open(p_select);
  171. }
  172. }
  173. void EditorHelp::_class_desc_input(const Ref<InputEvent> &p_input) {
  174. }
  175. void EditorHelp::_class_desc_resized(bool p_force_update_theme) {
  176. // Add extra horizontal margins for better readability.
  177. // The margins increase as the width of the editor help container increases.
  178. real_t char_width = theme_cache.doc_code_font->get_char_size('x', theme_cache.doc_code_font_size).width;
  179. const int new_display_margin = MAX(30 * EDSCALE, get_parent_anchorable_rect().size.width - char_width * 120 * EDSCALE) * 0.5;
  180. if (display_margin != new_display_margin || p_force_update_theme) {
  181. display_margin = new_display_margin;
  182. Ref<StyleBox> class_desc_stylebox = theme_cache.background_style->duplicate();
  183. class_desc_stylebox->set_content_margin(SIDE_LEFT, display_margin);
  184. class_desc_stylebox->set_content_margin(SIDE_RIGHT, display_margin);
  185. class_desc->add_theme_style_override("normal", class_desc_stylebox);
  186. class_desc->add_theme_style_override("focused", class_desc_stylebox);
  187. }
  188. }
  189. void EditorHelp::_add_type(const String &p_type, const String &p_enum) {
  190. if (p_type.is_empty() || p_type == "void") {
  191. class_desc->push_color(Color(theme_cache.type_color, 0.5));
  192. class_desc->push_hint(TTR("No return value."));
  193. class_desc->add_text("void");
  194. class_desc->pop();
  195. class_desc->pop();
  196. return;
  197. }
  198. bool is_enum_type = !p_enum.is_empty();
  199. bool can_ref = !p_type.contains("*") || is_enum_type;
  200. String t = p_type;
  201. if (is_enum_type) {
  202. if (p_enum.get_slice_count(".") > 1) {
  203. t = p_enum.get_slice(".", 1);
  204. } else {
  205. t = p_enum.get_slice(".", 0);
  206. }
  207. }
  208. class_desc->push_color(theme_cache.type_color);
  209. bool add_array = false;
  210. if (can_ref) {
  211. if (t.ends_with("[]")) {
  212. add_array = true;
  213. t = t.replace("[]", "");
  214. class_desc->push_meta("#Array"); //class
  215. class_desc->add_text("Array");
  216. class_desc->pop();
  217. class_desc->add_text("[");
  218. }
  219. if (is_enum_type) {
  220. class_desc->push_meta("$" + p_enum); //class
  221. } else {
  222. class_desc->push_meta("#" + t); //class
  223. }
  224. }
  225. class_desc->add_text(t);
  226. if (can_ref) {
  227. class_desc->pop(); // Pushed meta above.
  228. if (add_array) {
  229. class_desc->add_text("]");
  230. }
  231. }
  232. class_desc->pop();
  233. }
  234. void EditorHelp::_add_type_icon(const String &p_type, int p_size) {
  235. Ref<Texture2D> icon;
  236. if (has_theme_icon(p_type, SNAME("EditorIcons"))) {
  237. icon = get_theme_icon(p_type, SNAME("EditorIcons"));
  238. } else if (ClassDB::class_exists(p_type) && ClassDB::is_parent_class(p_type, "Object")) {
  239. icon = get_theme_icon(SNAME("Object"), SNAME("EditorIcons"));
  240. } else {
  241. icon = get_theme_icon(SNAME("ArrowRight"), SNAME("EditorIcons"));
  242. }
  243. Vector2i size = Vector2i(icon->get_width(), icon->get_height());
  244. if (p_size > 0) {
  245. // Ensures icon scales proportionally on both axis, based on icon height.
  246. float ratio = p_size / float(size.height);
  247. size.width *= ratio;
  248. size.height *= ratio;
  249. }
  250. class_desc->add_image(icon, size.width, size.height);
  251. }
  252. String EditorHelp::_fix_constant(const String &p_constant) const {
  253. if (p_constant.strip_edges() == "4294967295") {
  254. return "0xFFFFFFFF";
  255. }
  256. if (p_constant.strip_edges() == "2147483647") {
  257. return "0x7FFFFFFF";
  258. }
  259. if (p_constant.strip_edges() == "1048575") {
  260. return "0xFFFFF";
  261. }
  262. return p_constant;
  263. }
  264. // Macros for assigning the deprecation/experimental information to class members
  265. #define DEPRECATED_DOC_TAG \
  266. class_desc->push_color(get_theme_color(SNAME("error_color"), SNAME("Editor"))); \
  267. Ref<Texture2D> error_icon = get_theme_icon(SNAME("StatusError"), SNAME("EditorIcons")); \
  268. class_desc->add_text(" "); \
  269. class_desc->add_image(error_icon, error_icon->get_width(), error_icon->get_height()); \
  270. class_desc->add_text(" (" + TTR("Deprecated") + ")"); \
  271. class_desc->pop();
  272. #define EXPERIMENTAL_DOC_TAG \
  273. class_desc->push_color(get_theme_color(SNAME("warning_color"), SNAME("Editor"))); \
  274. Ref<Texture2D> warning_icon = get_theme_icon(SNAME("NodeWarning"), SNAME("EditorIcons")); \
  275. class_desc->add_text(" "); \
  276. class_desc->add_image(warning_icon, warning_icon->get_width(), warning_icon->get_height()); \
  277. class_desc->add_text(" (" + TTR("Experimental") + ")"); \
  278. class_desc->pop();
  279. void EditorHelp::_add_method(const DocData::MethodDoc &p_method, bool p_overview) {
  280. method_line[p_method.name] = class_desc->get_paragraph_count() - 2; //gets overridden if description
  281. const bool is_vararg = p_method.qualifiers.contains("vararg");
  282. if (p_overview) {
  283. class_desc->push_cell();
  284. class_desc->push_paragraph(HORIZONTAL_ALIGNMENT_RIGHT, Control::TEXT_DIRECTION_AUTO, "");
  285. } else {
  286. _add_bulletpoint();
  287. }
  288. _add_type(p_method.return_type, p_method.return_enum);
  289. if (p_overview) {
  290. class_desc->pop(); //align
  291. class_desc->pop(); //cell
  292. class_desc->push_cell();
  293. } else {
  294. class_desc->add_text(" ");
  295. }
  296. if (p_overview && !p_method.description.strip_edges().is_empty()) {
  297. class_desc->push_meta("@method " + p_method.name);
  298. }
  299. class_desc->push_color(theme_cache.headline_color);
  300. _add_text(p_method.name);
  301. class_desc->pop();
  302. if (p_overview && !p_method.description.strip_edges().is_empty()) {
  303. class_desc->pop(); //meta
  304. }
  305. class_desc->push_color(theme_cache.symbol_color);
  306. class_desc->add_text("(");
  307. class_desc->pop();
  308. for (int j = 0; j < p_method.arguments.size(); j++) {
  309. class_desc->push_color(theme_cache.text_color);
  310. if (j > 0) {
  311. class_desc->add_text(", ");
  312. }
  313. _add_text(p_method.arguments[j].name);
  314. class_desc->add_text(": ");
  315. _add_type(p_method.arguments[j].type, p_method.arguments[j].enumeration);
  316. if (!p_method.arguments[j].default_value.is_empty()) {
  317. class_desc->push_color(theme_cache.symbol_color);
  318. class_desc->add_text(" = ");
  319. class_desc->pop();
  320. class_desc->push_color(theme_cache.value_color);
  321. _add_text(_fix_constant(p_method.arguments[j].default_value));
  322. class_desc->pop();
  323. }
  324. class_desc->pop();
  325. }
  326. if (is_vararg) {
  327. class_desc->push_color(theme_cache.text_color);
  328. if (p_method.arguments.size()) {
  329. class_desc->add_text(", ");
  330. }
  331. class_desc->push_color(theme_cache.symbol_color);
  332. class_desc->add_text("...");
  333. class_desc->pop();
  334. class_desc->pop();
  335. }
  336. class_desc->push_color(theme_cache.symbol_color);
  337. class_desc->add_text(")");
  338. class_desc->pop();
  339. if (!p_method.qualifiers.is_empty()) {
  340. class_desc->push_color(theme_cache.qualifier_color);
  341. PackedStringArray qualifiers = p_method.qualifiers.split_spaces();
  342. for (const String &qualifier : qualifiers) {
  343. String hint;
  344. if (qualifier == "vararg") {
  345. hint = TTR("This method supports a variable number of arguments.");
  346. } else if (qualifier == "virtual") {
  347. hint = TTR("This method is called by the engine.\nIt can be overridden to customize built-in behavior.");
  348. } else if (qualifier == "const") {
  349. hint = TTR("This method has no side effects.\nIt does not modify the object in any way.");
  350. } else if (qualifier == "static") {
  351. hint = TTR("This method does not need an instance to be called.\nIt can be called directly using the class name.");
  352. }
  353. class_desc->add_text(" ");
  354. if (!hint.is_empty()) {
  355. class_desc->push_hint(hint);
  356. class_desc->add_text(qualifier);
  357. class_desc->pop();
  358. } else {
  359. class_desc->add_text(qualifier);
  360. }
  361. }
  362. class_desc->pop();
  363. }
  364. if (p_method.is_deprecated) {
  365. DEPRECATED_DOC_TAG;
  366. }
  367. if (p_method.is_experimental) {
  368. EXPERIMENTAL_DOC_TAG;
  369. }
  370. if (p_overview) {
  371. class_desc->pop(); //cell
  372. }
  373. }
  374. void EditorHelp::_add_bulletpoint() {
  375. static const char32_t prefix[3] = { 0x25CF /* filled circle */, ' ', 0 };
  376. class_desc->add_text(String(prefix));
  377. }
  378. void EditorHelp::_push_normal_font() {
  379. class_desc->push_font(theme_cache.doc_font);
  380. class_desc->push_font_size(theme_cache.doc_font_size);
  381. }
  382. void EditorHelp::_pop_normal_font() {
  383. class_desc->pop();
  384. class_desc->pop();
  385. }
  386. void EditorHelp::_push_title_font() {
  387. class_desc->push_color(theme_cache.title_color);
  388. class_desc->push_font(theme_cache.doc_title_font);
  389. class_desc->push_font_size(theme_cache.doc_title_font_size);
  390. }
  391. void EditorHelp::_pop_title_font() {
  392. class_desc->pop();
  393. class_desc->pop();
  394. class_desc->pop();
  395. }
  396. void EditorHelp::_push_code_font() {
  397. class_desc->push_font(theme_cache.doc_code_font);
  398. class_desc->push_font_size(theme_cache.doc_code_font_size);
  399. }
  400. void EditorHelp::_pop_code_font() {
  401. class_desc->pop();
  402. class_desc->pop();
  403. }
  404. Error EditorHelp::_goto_desc(const String &p_class, int p_vscr) {
  405. if (!doc->class_list.has(p_class)) {
  406. return ERR_DOES_NOT_EXIST;
  407. }
  408. select_locked = true;
  409. class_desc->show();
  410. description_line = 0;
  411. if (p_class == edited_class) {
  412. return OK; // Already there.
  413. }
  414. edited_class = p_class;
  415. _update_doc();
  416. return OK;
  417. }
  418. void EditorHelp::_update_method_list(const Vector<DocData::MethodDoc> p_methods) {
  419. class_desc->add_newline();
  420. _push_code_font();
  421. class_desc->push_indent(1);
  422. class_desc->push_table(2);
  423. class_desc->set_table_column_expand(1, true);
  424. bool any_previous = false;
  425. for (int pass = 0; pass < 2; pass++) {
  426. Vector<DocData::MethodDoc> m;
  427. for (int i = 0; i < p_methods.size(); i++) {
  428. const String &q = p_methods[i].qualifiers;
  429. if ((pass == 0 && q.contains("virtual")) || (pass == 1 && !q.contains("virtual"))) {
  430. m.push_back(p_methods[i]);
  431. }
  432. }
  433. if (any_previous && !m.is_empty()) {
  434. class_desc->push_cell();
  435. class_desc->pop(); //cell
  436. class_desc->push_cell();
  437. class_desc->pop(); //cell
  438. }
  439. String group_prefix;
  440. for (int i = 0; i < m.size(); i++) {
  441. const String new_prefix = m[i].name.substr(0, 3);
  442. bool is_new_group = false;
  443. if (i < m.size() - 1 && new_prefix == m[i + 1].name.substr(0, 3) && new_prefix != group_prefix) {
  444. is_new_group = i > 0;
  445. group_prefix = new_prefix;
  446. } else if (!group_prefix.is_empty() && new_prefix != group_prefix) {
  447. is_new_group = true;
  448. group_prefix = "";
  449. }
  450. if (is_new_group && pass == 1) {
  451. class_desc->push_cell();
  452. class_desc->pop(); //cell
  453. class_desc->push_cell();
  454. class_desc->pop(); //cell
  455. }
  456. _add_method(m[i], true);
  457. }
  458. any_previous = !m.is_empty();
  459. }
  460. class_desc->pop(); //table
  461. class_desc->pop();
  462. _pop_code_font();
  463. class_desc->add_newline();
  464. class_desc->add_newline();
  465. }
  466. void EditorHelp::_update_method_descriptions(const DocData::ClassDoc p_classdoc, const Vector<DocData::MethodDoc> p_methods, const String &p_method_type) {
  467. String link_color_text = theme_cache.title_color.to_html(false);
  468. class_desc->add_newline();
  469. class_desc->add_newline();
  470. for (int pass = 0; pass < 2; pass++) {
  471. Vector<DocData::MethodDoc> methods_filtered;
  472. for (int i = 0; i < p_methods.size(); i++) {
  473. const String &q = p_methods[i].qualifiers;
  474. if ((pass == 0 && q.contains("virtual")) || (pass == 1 && !q.contains("virtual"))) {
  475. methods_filtered.push_back(p_methods[i]);
  476. }
  477. }
  478. for (int i = 0; i < methods_filtered.size(); i++) {
  479. _push_code_font();
  480. _add_method(methods_filtered[i], false);
  481. _pop_code_font();
  482. class_desc->add_newline();
  483. class_desc->add_newline();
  484. class_desc->push_color(theme_cache.text_color);
  485. _push_normal_font();
  486. class_desc->push_indent(1);
  487. if (methods_filtered[i].errors_returned.size()) {
  488. class_desc->append_text(TTR("Error codes returned:"));
  489. class_desc->add_newline();
  490. class_desc->push_list(0, RichTextLabel::LIST_DOTS, false);
  491. for (int j = 0; j < methods_filtered[i].errors_returned.size(); j++) {
  492. if (j > 0) {
  493. class_desc->add_newline();
  494. }
  495. int val = methods_filtered[i].errors_returned[j];
  496. String text = itos(val);
  497. for (int k = 0; k < CoreConstants::get_global_constant_count(); k++) {
  498. if (CoreConstants::get_global_constant_value(k) == val && CoreConstants::get_global_constant_enum(k) == SNAME("Error")) {
  499. text = CoreConstants::get_global_constant_name(k);
  500. break;
  501. }
  502. }
  503. class_desc->push_bold();
  504. class_desc->append_text(text);
  505. class_desc->pop();
  506. }
  507. class_desc->pop();
  508. class_desc->add_newline();
  509. class_desc->add_newline();
  510. }
  511. if (!methods_filtered[i].description.strip_edges().is_empty()) {
  512. _add_text(DTR(methods_filtered[i].description));
  513. } else {
  514. class_desc->add_image(get_theme_icon(SNAME("Error"), SNAME("EditorIcons")));
  515. class_desc->add_text(" ");
  516. class_desc->push_color(theme_cache.comment_color);
  517. if (p_classdoc.is_script_doc) {
  518. class_desc->append_text(vformat(TTR("There is currently no description for this %s."), p_method_type));
  519. } else {
  520. class_desc->append_text(vformat(TTR("There is currently no description for this %s. Please help us by [color=$color][url=$url]contributing one[/url][/color]!"), p_method_type).replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
  521. }
  522. class_desc->pop();
  523. }
  524. class_desc->pop();
  525. _pop_normal_font();
  526. class_desc->pop();
  527. class_desc->add_newline();
  528. class_desc->add_newline();
  529. class_desc->add_newline();
  530. }
  531. }
  532. }
  533. void EditorHelp::_update_doc() {
  534. if (!doc->class_list.has(edited_class)) {
  535. return;
  536. }
  537. scroll_locked = true;
  538. class_desc->clear();
  539. method_line.clear();
  540. section_line.clear();
  541. String link_color_text = theme_cache.title_color.to_html(false);
  542. DocData::ClassDoc cd = doc->class_list[edited_class]; // Make a copy, so we can sort without worrying.
  543. // Class name
  544. section_line.push_back(Pair<String, int>(TTR("Top"), 0));
  545. _push_title_font();
  546. class_desc->add_text(TTR("Class:") + " ");
  547. _add_type_icon(edited_class, theme_cache.doc_title_font_size);
  548. class_desc->add_text(" ");
  549. class_desc->push_color(theme_cache.headline_color);
  550. _add_text(edited_class);
  551. class_desc->pop(); // color
  552. _pop_title_font();
  553. if (cd.is_deprecated) {
  554. class_desc->add_text(" ");
  555. Ref<Texture2D> error_icon = get_theme_icon(SNAME("StatusError"), SNAME("EditorIcons"));
  556. class_desc->add_image(error_icon, error_icon->get_width(), error_icon->get_height());
  557. }
  558. if (cd.is_experimental) {
  559. class_desc->add_text(" ");
  560. Ref<Texture2D> warning_icon = get_theme_icon(SNAME("NodeWarning"), SNAME("EditorIcons"));
  561. class_desc->add_image(warning_icon, warning_icon->get_width(), warning_icon->get_height());
  562. }
  563. class_desc->add_newline();
  564. const String non_breaking_space = String::chr(160);
  565. // Inheritance tree
  566. // Ascendents
  567. if (!cd.inherits.is_empty()) {
  568. class_desc->push_color(theme_cache.title_color);
  569. _push_normal_font();
  570. class_desc->add_text(TTR("Inherits:") + " ");
  571. String inherits = cd.inherits;
  572. while (!inherits.is_empty()) {
  573. _add_type_icon(inherits);
  574. class_desc->add_text(non_breaking_space); // Otherwise icon borrows hyperlink from _add_type().
  575. _add_type(inherits);
  576. inherits = doc->class_list[inherits].inherits;
  577. if (!inherits.is_empty()) {
  578. class_desc->add_text(" < ");
  579. }
  580. }
  581. _pop_normal_font();
  582. class_desc->pop();
  583. class_desc->add_newline();
  584. }
  585. // Descendents
  586. if (cd.is_script_doc || ClassDB::class_exists(cd.name)) {
  587. bool found = false;
  588. bool prev = false;
  589. _push_normal_font();
  590. for (const KeyValue<String, DocData::ClassDoc> &E : doc->class_list) {
  591. if (E.value.inherits == cd.name) {
  592. if (!found) {
  593. class_desc->push_color(theme_cache.title_color);
  594. class_desc->add_text(TTR("Inherited by:") + " ");
  595. found = true;
  596. }
  597. if (prev) {
  598. class_desc->add_text(" , ");
  599. }
  600. _add_type_icon(E.value.name);
  601. class_desc->add_text(non_breaking_space); // Otherwise icon borrows hyperlink from _add_type().
  602. _add_type(E.value.name);
  603. prev = true;
  604. }
  605. }
  606. _pop_normal_font();
  607. if (found) {
  608. class_desc->pop();
  609. class_desc->add_newline();
  610. }
  611. }
  612. // Note if deprecated.
  613. if (cd.is_deprecated) {
  614. Ref<Texture2D> error_icon = get_theme_icon(SNAME("StatusError"), SNAME("EditorIcons"));
  615. class_desc->push_color(get_theme_color(SNAME("error_color"), SNAME("Editor")));
  616. class_desc->add_image(error_icon, error_icon->get_width(), error_icon->get_height());
  617. class_desc->add_text(String(" ") + TTR("This class is marked as deprecated. It will be removed in future versions."));
  618. class_desc->pop();
  619. class_desc->add_newline();
  620. }
  621. // Note if experimental.
  622. if (cd.is_experimental) {
  623. Ref<Texture2D> warning_icon = get_theme_icon(SNAME("NodeWarning"), SNAME("EditorIcons"));
  624. class_desc->push_color(get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  625. class_desc->add_image(warning_icon, warning_icon->get_width(), warning_icon->get_height());
  626. class_desc->add_text(String(" ") + TTR("This class is marked as experimental. It is subject to likely change or possible removal in future versions. Use at your own discretion."));
  627. class_desc->pop();
  628. class_desc->add_newline();
  629. }
  630. bool has_description = false;
  631. class_desc->add_newline();
  632. class_desc->add_newline();
  633. // Brief description
  634. if (!cd.brief_description.strip_edges().is_empty()) {
  635. has_description = true;
  636. class_desc->push_color(theme_cache.text_color);
  637. class_desc->push_font(theme_cache.doc_bold_font);
  638. class_desc->push_indent(1);
  639. _add_text(DTR(cd.brief_description));
  640. class_desc->pop();
  641. class_desc->pop();
  642. class_desc->pop();
  643. class_desc->add_newline();
  644. class_desc->add_newline();
  645. class_desc->add_newline();
  646. }
  647. // Class description
  648. if (!cd.description.strip_edges().is_empty()) {
  649. has_description = true;
  650. section_line.push_back(Pair<String, int>(TTR("Description"), class_desc->get_paragraph_count() - 2));
  651. description_line = class_desc->get_paragraph_count() - 2;
  652. _push_title_font();
  653. class_desc->add_text(TTR("Description"));
  654. _pop_title_font();
  655. class_desc->add_newline();
  656. class_desc->add_newline();
  657. class_desc->push_color(theme_cache.text_color);
  658. _push_normal_font();
  659. class_desc->push_indent(1);
  660. _add_text(DTR(cd.description));
  661. class_desc->pop();
  662. _pop_normal_font();
  663. class_desc->pop();
  664. class_desc->add_newline();
  665. class_desc->add_newline();
  666. class_desc->add_newline();
  667. }
  668. if (!has_description) {
  669. class_desc->add_image(get_theme_icon(SNAME("Error"), SNAME("EditorIcons")));
  670. class_desc->add_text(" ");
  671. class_desc->push_color(theme_cache.comment_color);
  672. if (cd.is_script_doc) {
  673. class_desc->append_text(TTR("There is currently no description for this class."));
  674. } else {
  675. class_desc->append_text(TTR("There is currently no description for this class. Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
  676. }
  677. class_desc->pop();
  678. class_desc->add_newline();
  679. class_desc->add_newline();
  680. }
  681. // Online tutorials
  682. if (cd.tutorials.size()) {
  683. _push_title_font();
  684. class_desc->add_text(TTR("Online Tutorials"));
  685. _pop_title_font();
  686. class_desc->add_newline();
  687. class_desc->push_indent(1);
  688. _push_code_font();
  689. for (int i = 0; i < cd.tutorials.size(); i++) {
  690. const String link = DTR(cd.tutorials[i].link);
  691. String linktxt = (cd.tutorials[i].title.is_empty()) ? link : DTR(cd.tutorials[i].title);
  692. const int seppos = linktxt.find("//");
  693. if (seppos != -1) {
  694. linktxt = link.substr(seppos + 2);
  695. }
  696. class_desc->push_color(theme_cache.symbol_color);
  697. class_desc->append_text("[url=" + link + "]" + linktxt + "[/url]");
  698. class_desc->pop();
  699. class_desc->add_newline();
  700. }
  701. _pop_code_font();
  702. class_desc->pop();
  703. class_desc->add_newline();
  704. class_desc->add_newline();
  705. }
  706. // Properties overview
  707. HashSet<String> skip_methods;
  708. bool has_properties = false;
  709. bool has_property_descriptions = false;
  710. for (const DocData::PropertyDoc &prop : cd.properties) {
  711. if (cd.is_script_doc && prop.name.begins_with("_") && prop.description.strip_edges().is_empty()) {
  712. continue;
  713. }
  714. has_properties = true;
  715. if (!prop.overridden) {
  716. has_property_descriptions = true;
  717. break;
  718. }
  719. }
  720. if (has_properties) {
  721. section_line.push_back(Pair<String, int>(TTR("Properties"), class_desc->get_paragraph_count() - 2));
  722. _push_title_font();
  723. class_desc->add_text(TTR("Properties"));
  724. _pop_title_font();
  725. class_desc->add_newline();
  726. _push_code_font();
  727. class_desc->push_indent(1);
  728. class_desc->push_table(4);
  729. class_desc->set_table_column_expand(1, true);
  730. for (int i = 0; i < cd.properties.size(); i++) {
  731. // Ignore undocumented private.
  732. if (cd.properties[i].name.begins_with("_") && cd.properties[i].description.strip_edges().is_empty()) {
  733. continue;
  734. }
  735. property_line[cd.properties[i].name] = class_desc->get_paragraph_count() - 2; //gets overridden if description
  736. // Property type.
  737. class_desc->push_cell();
  738. class_desc->push_paragraph(HORIZONTAL_ALIGNMENT_RIGHT, Control::TEXT_DIRECTION_AUTO, "");
  739. _push_code_font();
  740. _add_type(cd.properties[i].type, cd.properties[i].enumeration);
  741. _pop_code_font();
  742. class_desc->pop();
  743. class_desc->pop(); // cell
  744. bool describe = false;
  745. if (!cd.properties[i].setter.is_empty()) {
  746. skip_methods.insert(cd.properties[i].setter);
  747. describe = true;
  748. }
  749. if (!cd.properties[i].getter.is_empty()) {
  750. skip_methods.insert(cd.properties[i].getter);
  751. describe = true;
  752. }
  753. if (!cd.properties[i].description.strip_edges().is_empty()) {
  754. describe = true;
  755. }
  756. if (cd.properties[i].overridden) {
  757. describe = false;
  758. }
  759. // Property name.
  760. class_desc->push_cell();
  761. _push_code_font();
  762. class_desc->push_color(theme_cache.headline_color);
  763. if (describe) {
  764. class_desc->push_meta("@member " + cd.properties[i].name);
  765. }
  766. _add_text(cd.properties[i].name);
  767. if (describe) {
  768. class_desc->pop();
  769. }
  770. class_desc->pop();
  771. _pop_code_font();
  772. class_desc->pop(); // cell
  773. // Property value.
  774. class_desc->push_cell();
  775. _push_code_font();
  776. if (!cd.properties[i].default_value.is_empty()) {
  777. class_desc->push_color(theme_cache.symbol_color);
  778. if (cd.properties[i].overridden) {
  779. class_desc->add_text(" [");
  780. class_desc->push_meta("@member " + cd.properties[i].overrides + "." + cd.properties[i].name);
  781. _add_text(vformat(TTR("overrides %s:"), cd.properties[i].overrides));
  782. class_desc->pop();
  783. class_desc->add_text(" ");
  784. } else {
  785. class_desc->add_text(" [" + TTR("default:") + " ");
  786. }
  787. class_desc->pop();
  788. class_desc->push_color(theme_cache.value_color);
  789. _add_text(_fix_constant(cd.properties[i].default_value));
  790. class_desc->pop();
  791. class_desc->push_color(theme_cache.symbol_color);
  792. class_desc->add_text("]");
  793. class_desc->pop();
  794. }
  795. if (cd.properties[i].is_deprecated) {
  796. DEPRECATED_DOC_TAG;
  797. }
  798. if (cd.properties[i].is_experimental) {
  799. EXPERIMENTAL_DOC_TAG;
  800. }
  801. _pop_code_font();
  802. class_desc->pop(); // cell
  803. // Property setters and getters.
  804. class_desc->push_cell();
  805. _push_code_font();
  806. if (cd.is_script_doc && (!cd.properties[i].setter.is_empty() || !cd.properties[i].getter.is_empty())) {
  807. class_desc->push_color(theme_cache.symbol_color);
  808. class_desc->add_text(" [" + TTR("property:") + " ");
  809. class_desc->pop(); // color
  810. if (!cd.properties[i].setter.is_empty()) {
  811. class_desc->push_color(theme_cache.value_color);
  812. class_desc->add_text("setter");
  813. class_desc->pop(); // color
  814. }
  815. if (!cd.properties[i].getter.is_empty()) {
  816. if (!cd.properties[i].setter.is_empty()) {
  817. class_desc->push_color(theme_cache.symbol_color);
  818. class_desc->add_text(", ");
  819. class_desc->pop(); // color
  820. }
  821. class_desc->push_color(theme_cache.value_color);
  822. class_desc->add_text("getter");
  823. class_desc->pop(); // color
  824. }
  825. class_desc->push_color(theme_cache.symbol_color);
  826. class_desc->add_text("]");
  827. class_desc->pop(); // color
  828. }
  829. _pop_code_font();
  830. class_desc->pop(); // cell
  831. }
  832. class_desc->pop(); // table
  833. class_desc->pop();
  834. _pop_code_font();
  835. class_desc->add_newline();
  836. class_desc->add_newline();
  837. }
  838. // Methods overview
  839. bool sort_methods = EDITOR_GET("text_editor/help/sort_functions_alphabetically");
  840. Vector<DocData::MethodDoc> methods;
  841. for (int i = 0; i < cd.methods.size(); i++) {
  842. if (skip_methods.has(cd.methods[i].name)) {
  843. if (cd.methods[i].arguments.size() == 0 /* getter */ || (cd.methods[i].arguments.size() == 1 && cd.methods[i].return_type == "void" /* setter */)) {
  844. continue;
  845. }
  846. }
  847. // Ignore undocumented non virtual private.
  848. if (cd.methods[i].name.begins_with("_") && cd.methods[i].description.strip_edges().is_empty() && !cd.methods[i].qualifiers.contains("virtual")) {
  849. continue;
  850. }
  851. methods.push_back(cd.methods[i]);
  852. }
  853. if (!cd.constructors.is_empty()) {
  854. if (sort_methods) {
  855. cd.constructors.sort();
  856. }
  857. section_line.push_back(Pair<String, int>(TTR("Constructors"), class_desc->get_paragraph_count() - 2));
  858. _push_title_font();
  859. class_desc->add_text(TTR("Constructors"));
  860. _pop_title_font();
  861. _update_method_list(cd.constructors);
  862. }
  863. if (!methods.is_empty()) {
  864. if (sort_methods) {
  865. methods.sort();
  866. }
  867. section_line.push_back(Pair<String, int>(TTR("Methods"), class_desc->get_paragraph_count() - 2));
  868. _push_title_font();
  869. class_desc->add_text(TTR("Methods"));
  870. _pop_title_font();
  871. _update_method_list(methods);
  872. }
  873. if (!cd.operators.is_empty()) {
  874. if (sort_methods) {
  875. cd.operators.sort();
  876. }
  877. section_line.push_back(Pair<String, int>(TTR("Operators"), class_desc->get_paragraph_count() - 2));
  878. _push_title_font();
  879. class_desc->add_text(TTR("Operators"));
  880. _pop_title_font();
  881. _update_method_list(cd.operators);
  882. }
  883. // Theme properties
  884. if (!cd.theme_properties.is_empty()) {
  885. section_line.push_back(Pair<String, int>(TTR("Theme Properties"), class_desc->get_paragraph_count() - 2));
  886. _push_title_font();
  887. class_desc->add_text(TTR("Theme Properties"));
  888. _pop_title_font();
  889. class_desc->add_newline();
  890. class_desc->add_newline();
  891. class_desc->push_indent(1);
  892. String theme_data_type;
  893. HashMap<String, String> data_type_names;
  894. data_type_names["color"] = TTR("Colors");
  895. data_type_names["constant"] = TTR("Constants");
  896. data_type_names["font"] = TTR("Fonts");
  897. data_type_names["font_size"] = TTR("Font Sizes");
  898. data_type_names["icon"] = TTR("Icons");
  899. data_type_names["style"] = TTR("Styles");
  900. for (int i = 0; i < cd.theme_properties.size(); i++) {
  901. theme_property_line[cd.theme_properties[i].name] = class_desc->get_paragraph_count() - 2; // Gets overridden if description.
  902. if (theme_data_type != cd.theme_properties[i].data_type) {
  903. theme_data_type = cd.theme_properties[i].data_type;
  904. _push_title_font();
  905. if (data_type_names.has(theme_data_type)) {
  906. class_desc->add_text(data_type_names[theme_data_type]);
  907. } else {
  908. class_desc->add_text("");
  909. }
  910. _pop_title_font();
  911. class_desc->add_newline();
  912. class_desc->add_newline();
  913. }
  914. // Theme item header.
  915. _push_code_font();
  916. _add_bulletpoint();
  917. // Theme item object type.
  918. _add_type(cd.theme_properties[i].type);
  919. // Theme item name.
  920. class_desc->push_color(theme_cache.headline_color);
  921. class_desc->add_text(" ");
  922. _add_text(cd.theme_properties[i].name);
  923. class_desc->pop();
  924. // Theme item default value.
  925. if (!cd.theme_properties[i].default_value.is_empty()) {
  926. class_desc->push_color(theme_cache.symbol_color);
  927. class_desc->add_text(" [" + TTR("default:") + " ");
  928. class_desc->pop();
  929. class_desc->push_color(theme_cache.value_color);
  930. _add_text(_fix_constant(cd.theme_properties[i].default_value));
  931. class_desc->pop();
  932. class_desc->push_color(theme_cache.symbol_color);
  933. class_desc->add_text("]");
  934. class_desc->pop();
  935. }
  936. _pop_code_font();
  937. // Theme item description.
  938. if (!cd.theme_properties[i].description.strip_edges().is_empty()) {
  939. class_desc->push_color(theme_cache.comment_color);
  940. _push_normal_font();
  941. class_desc->push_indent(1);
  942. _add_text(DTR(cd.theme_properties[i].description));
  943. class_desc->pop(); // indent
  944. _pop_normal_font();
  945. class_desc->pop(); // color
  946. }
  947. class_desc->add_newline();
  948. class_desc->add_newline();
  949. }
  950. class_desc->pop();
  951. class_desc->add_newline();
  952. }
  953. // Signals
  954. if (!cd.signals.is_empty()) {
  955. if (sort_methods) {
  956. cd.signals.sort();
  957. }
  958. section_line.push_back(Pair<String, int>(TTR("Signals"), class_desc->get_paragraph_count() - 2));
  959. _push_title_font();
  960. class_desc->add_text(TTR("Signals"));
  961. _pop_title_font();
  962. class_desc->add_newline();
  963. class_desc->add_newline();
  964. class_desc->push_indent(1);
  965. for (int i = 0; i < cd.signals.size(); i++) {
  966. signal_line[cd.signals[i].name] = class_desc->get_paragraph_count() - 2; // Gets overridden if description.
  967. _push_code_font();
  968. _add_bulletpoint();
  969. class_desc->push_color(theme_cache.headline_color);
  970. _add_text(cd.signals[i].name);
  971. class_desc->pop();
  972. class_desc->push_color(theme_cache.symbol_color);
  973. class_desc->add_text("(");
  974. class_desc->pop();
  975. for (int j = 0; j < cd.signals[i].arguments.size(); j++) {
  976. class_desc->push_color(theme_cache.text_color);
  977. if (j > 0) {
  978. class_desc->add_text(", ");
  979. }
  980. _add_text(cd.signals[i].arguments[j].name);
  981. class_desc->add_text(": ");
  982. _add_type(cd.signals[i].arguments[j].type);
  983. if (!cd.signals[i].arguments[j].default_value.is_empty()) {
  984. class_desc->push_color(theme_cache.symbol_color);
  985. class_desc->add_text(" = ");
  986. class_desc->pop();
  987. _add_text(cd.signals[i].arguments[j].default_value);
  988. }
  989. class_desc->pop();
  990. }
  991. class_desc->push_color(theme_cache.symbol_color);
  992. class_desc->add_text(")");
  993. if (cd.signals[i].is_deprecated) {
  994. DEPRECATED_DOC_TAG;
  995. }
  996. if (cd.signals[i].is_experimental) {
  997. EXPERIMENTAL_DOC_TAG;
  998. }
  999. class_desc->pop();
  1000. _pop_code_font();
  1001. if (!cd.signals[i].description.strip_edges().is_empty()) {
  1002. class_desc->push_color(theme_cache.comment_color);
  1003. _push_normal_font();
  1004. class_desc->push_indent(1);
  1005. _add_text(DTR(cd.signals[i].description));
  1006. class_desc->pop(); // indent
  1007. _pop_normal_font();
  1008. class_desc->pop(); // color
  1009. }
  1010. class_desc->add_newline();
  1011. class_desc->add_newline();
  1012. }
  1013. class_desc->pop();
  1014. class_desc->add_newline();
  1015. }
  1016. // Constants and enums
  1017. if (!cd.constants.is_empty()) {
  1018. HashMap<String, Vector<DocData::ConstantDoc>> enums;
  1019. Vector<DocData::ConstantDoc> constants;
  1020. for (int i = 0; i < cd.constants.size(); i++) {
  1021. if (!cd.constants[i].enumeration.is_empty()) {
  1022. if (!enums.has(cd.constants[i].enumeration)) {
  1023. enums[cd.constants[i].enumeration] = Vector<DocData::ConstantDoc>();
  1024. }
  1025. enums[cd.constants[i].enumeration].push_back(cd.constants[i]);
  1026. } else {
  1027. // Ignore undocumented private.
  1028. if (cd.constants[i].name.begins_with("_") && cd.constants[i].description.strip_edges().is_empty()) {
  1029. continue;
  1030. }
  1031. constants.push_back(cd.constants[i]);
  1032. }
  1033. }
  1034. // Enums
  1035. if (enums.size()) {
  1036. section_line.push_back(Pair<String, int>(TTR("Enumerations"), class_desc->get_paragraph_count() - 2));
  1037. _push_title_font();
  1038. class_desc->add_text(TTR("Enumerations"));
  1039. _pop_title_font();
  1040. class_desc->push_indent(1);
  1041. class_desc->add_newline();
  1042. for (KeyValue<String, Vector<DocData::ConstantDoc>> &E : enums) {
  1043. enum_line[E.key] = class_desc->get_paragraph_count() - 2;
  1044. _push_code_font();
  1045. class_desc->push_color(theme_cache.title_color);
  1046. if (E.value.size() && E.value[0].is_bitfield) {
  1047. class_desc->add_text("flags ");
  1048. } else {
  1049. class_desc->add_text("enum ");
  1050. }
  1051. class_desc->pop();
  1052. String e = E.key;
  1053. if ((e.get_slice_count(".") > 1) && (e.get_slice(".", 0) == edited_class)) {
  1054. e = e.get_slice(".", 1);
  1055. }
  1056. class_desc->push_color(theme_cache.headline_color);
  1057. class_desc->add_text(e);
  1058. class_desc->pop();
  1059. _pop_code_font();
  1060. class_desc->push_color(theme_cache.symbol_color);
  1061. class_desc->add_text(":");
  1062. class_desc->pop();
  1063. class_desc->add_newline();
  1064. class_desc->add_newline();
  1065. // Enum description.
  1066. if (e != "@unnamed_enums" && cd.enums.has(e)) {
  1067. class_desc->push_color(theme_cache.text_color);
  1068. _push_normal_font();
  1069. class_desc->push_indent(1);
  1070. _add_text(cd.enums[e]);
  1071. class_desc->pop();
  1072. _pop_normal_font();
  1073. class_desc->pop();
  1074. class_desc->add_newline();
  1075. class_desc->add_newline();
  1076. }
  1077. class_desc->push_indent(1);
  1078. Vector<DocData::ConstantDoc> enum_list = E.value;
  1079. HashMap<String, int> enumValuesContainer;
  1080. int enumStartingLine = enum_line[E.key];
  1081. for (int i = 0; i < enum_list.size(); i++) {
  1082. if (cd.name == "@GlobalScope") {
  1083. enumValuesContainer[enum_list[i].name] = enumStartingLine;
  1084. }
  1085. // Add the enum constant line to the constant_line map so we can locate it as a constant.
  1086. constant_line[enum_list[i].name] = class_desc->get_paragraph_count() - 2;
  1087. _push_code_font();
  1088. _add_bulletpoint();
  1089. class_desc->push_color(theme_cache.headline_color);
  1090. _add_text(enum_list[i].name);
  1091. class_desc->pop();
  1092. class_desc->push_color(theme_cache.symbol_color);
  1093. class_desc->add_text(" = ");
  1094. class_desc->pop();
  1095. class_desc->push_color(theme_cache.value_color);
  1096. _add_text(_fix_constant(enum_list[i].value));
  1097. class_desc->pop();
  1098. _pop_code_font();
  1099. if (enum_list[i].is_deprecated) {
  1100. DEPRECATED_DOC_TAG;
  1101. }
  1102. if (enum_list[i].is_experimental) {
  1103. EXPERIMENTAL_DOC_TAG;
  1104. }
  1105. class_desc->add_newline();
  1106. if (!enum_list[i].description.strip_edges().is_empty()) {
  1107. class_desc->push_color(theme_cache.comment_color);
  1108. _push_normal_font();
  1109. _add_text(DTR(enum_list[i].description));
  1110. _pop_normal_font();
  1111. class_desc->pop();
  1112. if (DTR(enum_list[i].description).find("\n") > 0) {
  1113. class_desc->add_newline();
  1114. }
  1115. }
  1116. class_desc->add_newline();
  1117. }
  1118. if (cd.name == "@GlobalScope") {
  1119. enum_values_line[E.key] = enumValuesContainer;
  1120. }
  1121. class_desc->pop();
  1122. class_desc->add_newline();
  1123. }
  1124. class_desc->pop();
  1125. class_desc->add_newline();
  1126. }
  1127. // Constants
  1128. if (constants.size()) {
  1129. section_line.push_back(Pair<String, int>(TTR("Constants"), class_desc->get_paragraph_count() - 2));
  1130. _push_title_font();
  1131. class_desc->add_text(TTR("Constants"));
  1132. _pop_title_font();
  1133. class_desc->push_indent(1);
  1134. class_desc->add_newline();
  1135. for (int i = 0; i < constants.size(); i++) {
  1136. constant_line[constants[i].name] = class_desc->get_paragraph_count() - 2;
  1137. _push_code_font();
  1138. if (constants[i].value.begins_with("Color(") && constants[i].value.ends_with(")")) {
  1139. String stripped = constants[i].value.replace(" ", "").replace("Color(", "").replace(")", "");
  1140. PackedFloat64Array color = stripped.split_floats(",");
  1141. if (color.size() >= 3) {
  1142. class_desc->push_color(Color(color[0], color[1], color[2]));
  1143. _add_bulletpoint();
  1144. class_desc->pop();
  1145. }
  1146. } else {
  1147. _add_bulletpoint();
  1148. }
  1149. class_desc->push_color(theme_cache.headline_color);
  1150. _add_text(constants[i].name);
  1151. class_desc->pop();
  1152. class_desc->push_color(theme_cache.symbol_color);
  1153. class_desc->add_text(" = ");
  1154. class_desc->pop();
  1155. class_desc->push_color(theme_cache.value_color);
  1156. _add_text(_fix_constant(constants[i].value));
  1157. class_desc->pop();
  1158. _pop_code_font();
  1159. if (constants[i].is_deprecated) {
  1160. DEPRECATED_DOC_TAG;
  1161. }
  1162. if (constants[i].is_experimental) {
  1163. EXPERIMENTAL_DOC_TAG;
  1164. }
  1165. class_desc->add_newline();
  1166. if (!constants[i].description.strip_edges().is_empty()) {
  1167. class_desc->push_color(theme_cache.comment_color);
  1168. _push_normal_font();
  1169. _add_text(DTR(constants[i].description));
  1170. _pop_normal_font();
  1171. class_desc->pop();
  1172. if (DTR(constants[i].description).find("\n") > 0) {
  1173. class_desc->add_newline();
  1174. }
  1175. }
  1176. class_desc->add_newline();
  1177. }
  1178. class_desc->pop();
  1179. class_desc->add_newline();
  1180. }
  1181. }
  1182. // Annotations
  1183. if (!cd.annotations.is_empty()) {
  1184. if (sort_methods) {
  1185. cd.annotations.sort();
  1186. }
  1187. section_line.push_back(Pair<String, int>(TTR("Annotations"), class_desc->get_paragraph_count() - 2));
  1188. _push_title_font();
  1189. class_desc->add_text(TTR("Annotations"));
  1190. _pop_title_font();
  1191. class_desc->add_newline();
  1192. class_desc->add_newline();
  1193. class_desc->push_indent(1);
  1194. for (int i = 0; i < cd.annotations.size(); i++) {
  1195. annotation_line[cd.annotations[i].name] = class_desc->get_paragraph_count() - 2; // Gets overridden if description.
  1196. _push_code_font();
  1197. _add_bulletpoint();
  1198. class_desc->push_color(theme_cache.headline_color);
  1199. _add_text(cd.annotations[i].name);
  1200. class_desc->pop();
  1201. if (cd.annotations[i].arguments.size() > 0) {
  1202. class_desc->push_color(theme_cache.symbol_color);
  1203. class_desc->add_text("(");
  1204. class_desc->pop();
  1205. for (int j = 0; j < cd.annotations[i].arguments.size(); j++) {
  1206. class_desc->push_color(theme_cache.text_color);
  1207. if (j > 0) {
  1208. class_desc->add_text(", ");
  1209. }
  1210. _add_text(cd.annotations[i].arguments[j].name);
  1211. class_desc->add_text(": ");
  1212. _add_type(cd.annotations[i].arguments[j].type);
  1213. if (!cd.annotations[i].arguments[j].default_value.is_empty()) {
  1214. class_desc->push_color(theme_cache.symbol_color);
  1215. class_desc->add_text(" = ");
  1216. class_desc->pop();
  1217. _add_text(cd.annotations[i].arguments[j].default_value);
  1218. }
  1219. class_desc->pop();
  1220. }
  1221. if (cd.annotations[i].qualifiers.contains("vararg")) {
  1222. class_desc->push_color(theme_cache.text_color);
  1223. if (cd.annotations[i].arguments.size()) {
  1224. class_desc->add_text(", ");
  1225. }
  1226. class_desc->push_color(theme_cache.symbol_color);
  1227. class_desc->add_text("...");
  1228. class_desc->pop();
  1229. class_desc->pop();
  1230. }
  1231. class_desc->push_color(theme_cache.symbol_color);
  1232. class_desc->add_text(")");
  1233. class_desc->pop();
  1234. }
  1235. if (!cd.annotations[i].qualifiers.is_empty()) {
  1236. class_desc->push_color(theme_cache.qualifier_color);
  1237. class_desc->add_text(" ");
  1238. _add_text(cd.annotations[i].qualifiers);
  1239. class_desc->pop();
  1240. }
  1241. _pop_code_font();
  1242. if (!cd.annotations[i].description.strip_edges().is_empty()) {
  1243. class_desc->push_color(theme_cache.comment_color);
  1244. _push_normal_font();
  1245. class_desc->push_indent(1);
  1246. _add_text(DTR(cd.annotations[i].description));
  1247. class_desc->pop(); // indent
  1248. _pop_normal_font();
  1249. class_desc->pop(); // color
  1250. } else {
  1251. class_desc->push_indent(1);
  1252. class_desc->add_image(get_theme_icon(SNAME("Error"), SNAME("EditorIcons")));
  1253. class_desc->add_text(" ");
  1254. class_desc->push_color(theme_cache.comment_color);
  1255. if (cd.is_script_doc) {
  1256. class_desc->append_text(TTR("There is currently no description for this annotation."));
  1257. } else {
  1258. class_desc->append_text(TTR("There is currently no description for this annotation. Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
  1259. }
  1260. class_desc->pop();
  1261. class_desc->pop(); // indent
  1262. }
  1263. class_desc->add_newline();
  1264. class_desc->add_newline();
  1265. }
  1266. class_desc->pop();
  1267. class_desc->add_newline();
  1268. }
  1269. // Property descriptions
  1270. if (has_property_descriptions) {
  1271. section_line.push_back(Pair<String, int>(TTR("Property Descriptions"), class_desc->get_paragraph_count() - 2));
  1272. _push_title_font();
  1273. class_desc->add_text(TTR("Property Descriptions"));
  1274. _pop_title_font();
  1275. class_desc->add_newline();
  1276. class_desc->add_newline();
  1277. for (int i = 0; i < cd.properties.size(); i++) {
  1278. if (cd.properties[i].overridden) {
  1279. continue;
  1280. }
  1281. // Ignore undocumented private.
  1282. if (cd.properties[i].name.begins_with("_") && cd.properties[i].description.strip_edges().is_empty()) {
  1283. continue;
  1284. }
  1285. property_line[cd.properties[i].name] = class_desc->get_paragraph_count() - 2;
  1286. class_desc->push_table(2);
  1287. class_desc->set_table_column_expand(1, true);
  1288. class_desc->push_cell();
  1289. _push_code_font();
  1290. _add_bulletpoint();
  1291. _add_type(cd.properties[i].type, cd.properties[i].enumeration);
  1292. class_desc->add_text(" ");
  1293. _pop_code_font();
  1294. class_desc->pop(); // cell
  1295. class_desc->push_cell();
  1296. _push_code_font();
  1297. class_desc->push_color(theme_cache.headline_color);
  1298. _add_text(cd.properties[i].name);
  1299. class_desc->pop(); // color
  1300. if (!cd.properties[i].default_value.is_empty()) {
  1301. class_desc->push_color(theme_cache.symbol_color);
  1302. class_desc->add_text(" [" + TTR("default:") + " ");
  1303. class_desc->pop(); // color
  1304. class_desc->push_color(theme_cache.value_color);
  1305. _add_text(_fix_constant(cd.properties[i].default_value));
  1306. class_desc->pop(); // color
  1307. class_desc->push_color(theme_cache.symbol_color);
  1308. class_desc->add_text("]");
  1309. class_desc->pop(); // color
  1310. }
  1311. if (cd.properties[i].is_deprecated) {
  1312. DEPRECATED_DOC_TAG;
  1313. }
  1314. if (cd.properties[i].is_experimental) {
  1315. EXPERIMENTAL_DOC_TAG;
  1316. }
  1317. if (cd.is_script_doc && (!cd.properties[i].setter.is_empty() || !cd.properties[i].getter.is_empty())) {
  1318. class_desc->push_color(theme_cache.symbol_color);
  1319. class_desc->add_text(" [" + TTR("property:") + " ");
  1320. class_desc->pop(); // color
  1321. if (!cd.properties[i].setter.is_empty()) {
  1322. class_desc->push_color(theme_cache.value_color);
  1323. class_desc->add_text("setter");
  1324. class_desc->pop(); // color
  1325. }
  1326. if (!cd.properties[i].getter.is_empty()) {
  1327. if (!cd.properties[i].setter.is_empty()) {
  1328. class_desc->push_color(theme_cache.symbol_color);
  1329. class_desc->add_text(", ");
  1330. class_desc->pop(); // color
  1331. }
  1332. class_desc->push_color(theme_cache.value_color);
  1333. class_desc->add_text("getter");
  1334. class_desc->pop(); // color
  1335. }
  1336. class_desc->push_color(theme_cache.symbol_color);
  1337. class_desc->add_text("]");
  1338. class_desc->pop(); // color
  1339. }
  1340. _pop_code_font();
  1341. class_desc->pop(); // cell
  1342. // Script doc doesn't have setter, getter.
  1343. if (!cd.is_script_doc) {
  1344. HashMap<String, DocData::MethodDoc> method_map;
  1345. for (int j = 0; j < methods.size(); j++) {
  1346. method_map[methods[j].name] = methods[j];
  1347. }
  1348. if (!cd.properties[i].setter.is_empty()) {
  1349. class_desc->push_cell();
  1350. class_desc->pop(); // cell
  1351. class_desc->push_cell();
  1352. _push_code_font();
  1353. class_desc->push_color(theme_cache.text_color);
  1354. if (method_map[cd.properties[i].setter].arguments.size() > 1) {
  1355. // Setters with additional arguments are exposed in the method list, so we link them here for quick access.
  1356. class_desc->push_meta("@method " + cd.properties[i].setter);
  1357. class_desc->add_text(cd.properties[i].setter + TTR("(value)"));
  1358. class_desc->pop();
  1359. } else {
  1360. class_desc->add_text(cd.properties[i].setter + TTR("(value)"));
  1361. }
  1362. class_desc->pop(); // color
  1363. class_desc->push_color(theme_cache.comment_color);
  1364. class_desc->add_text(" setter");
  1365. class_desc->pop(); // color
  1366. _pop_code_font();
  1367. class_desc->pop(); // cell
  1368. method_line[cd.properties[i].setter] = property_line[cd.properties[i].name];
  1369. }
  1370. if (!cd.properties[i].getter.is_empty()) {
  1371. class_desc->push_cell();
  1372. class_desc->pop(); // cell
  1373. class_desc->push_cell();
  1374. _push_code_font();
  1375. class_desc->push_color(theme_cache.text_color);
  1376. if (method_map[cd.properties[i].getter].arguments.size() > 0) {
  1377. // Getters with additional arguments are exposed in the method list, so we link them here for quick access.
  1378. class_desc->push_meta("@method " + cd.properties[i].getter);
  1379. class_desc->add_text(cd.properties[i].getter + "()");
  1380. class_desc->pop();
  1381. } else {
  1382. class_desc->add_text(cd.properties[i].getter + "()");
  1383. }
  1384. class_desc->pop(); // color
  1385. class_desc->push_color(theme_cache.comment_color);
  1386. class_desc->add_text(" getter");
  1387. class_desc->pop(); // color
  1388. _pop_code_font();
  1389. class_desc->pop(); // cell
  1390. method_line[cd.properties[i].getter] = property_line[cd.properties[i].name];
  1391. }
  1392. }
  1393. class_desc->pop(); // table
  1394. class_desc->add_newline();
  1395. class_desc->add_newline();
  1396. class_desc->push_color(theme_cache.text_color);
  1397. _push_normal_font();
  1398. class_desc->push_indent(1);
  1399. if (!cd.properties[i].description.strip_edges().is_empty()) {
  1400. _add_text(DTR(cd.properties[i].description));
  1401. } else {
  1402. class_desc->add_image(get_theme_icon(SNAME("Error"), SNAME("EditorIcons")));
  1403. class_desc->add_text(" ");
  1404. class_desc->push_color(theme_cache.comment_color);
  1405. if (cd.is_script_doc) {
  1406. class_desc->append_text(TTR("There is currently no description for this property."));
  1407. } else {
  1408. class_desc->append_text(TTR("There is currently no description for this property. Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
  1409. }
  1410. class_desc->pop();
  1411. }
  1412. class_desc->pop();
  1413. _pop_normal_font();
  1414. class_desc->pop();
  1415. class_desc->add_newline();
  1416. class_desc->add_newline();
  1417. class_desc->add_newline();
  1418. }
  1419. }
  1420. // Constructor descriptions
  1421. if (!cd.constructors.is_empty()) {
  1422. section_line.push_back(Pair<String, int>(TTR("Constructor Descriptions"), class_desc->get_paragraph_count() - 2));
  1423. _push_title_font();
  1424. class_desc->add_text(TTR("Constructor Descriptions"));
  1425. _pop_title_font();
  1426. _update_method_descriptions(cd, cd.constructors, "constructor");
  1427. }
  1428. // Method descriptions
  1429. if (!methods.is_empty()) {
  1430. section_line.push_back(Pair<String, int>(TTR("Method Descriptions"), class_desc->get_paragraph_count() - 2));
  1431. _push_title_font();
  1432. class_desc->add_text(TTR("Method Descriptions"));
  1433. _pop_title_font();
  1434. _update_method_descriptions(cd, methods, "method");
  1435. }
  1436. // Operator descriptions
  1437. if (!cd.operators.is_empty()) {
  1438. section_line.push_back(Pair<String, int>(TTR("Operator Descriptions"), class_desc->get_paragraph_count() - 2));
  1439. _push_title_font();
  1440. class_desc->add_text(TTR("Operator Descriptions"));
  1441. _pop_title_font();
  1442. _update_method_descriptions(cd, cd.operators, "operator");
  1443. }
  1444. // Free the scroll.
  1445. scroll_locked = false;
  1446. }
  1447. void EditorHelp::_request_help(const String &p_string) {
  1448. Error err = _goto_desc(p_string);
  1449. if (err == OK) {
  1450. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  1451. }
  1452. //100 palabras
  1453. }
  1454. void EditorHelp::_help_callback(const String &p_topic) {
  1455. String what = p_topic.get_slice(":", 0);
  1456. String clss = p_topic.get_slice(":", 1);
  1457. String name;
  1458. if (p_topic.get_slice_count(":") == 3) {
  1459. name = p_topic.get_slice(":", 2);
  1460. }
  1461. _request_help(clss); // First go to class.
  1462. int line = 0;
  1463. if (what == "class_desc") {
  1464. line = description_line;
  1465. } else if (what == "class_signal") {
  1466. if (signal_line.has(name)) {
  1467. line = signal_line[name];
  1468. }
  1469. } else if (what == "class_method" || what == "class_method_desc") {
  1470. if (method_line.has(name)) {
  1471. line = method_line[name];
  1472. }
  1473. } else if (what == "class_property") {
  1474. if (property_line.has(name)) {
  1475. line = property_line[name];
  1476. }
  1477. } else if (what == "class_enum") {
  1478. if (enum_line.has(name)) {
  1479. line = enum_line[name];
  1480. }
  1481. } else if (what == "class_theme_item") {
  1482. if (theme_property_line.has(name)) {
  1483. line = theme_property_line[name];
  1484. }
  1485. } else if (what == "class_constant") {
  1486. if (constant_line.has(name)) {
  1487. line = constant_line[name];
  1488. }
  1489. } else if (what == "class_annotation") {
  1490. if (annotation_line.has(name)) {
  1491. line = annotation_line[name];
  1492. }
  1493. } else if (what == "class_global") {
  1494. if (constant_line.has(name)) {
  1495. line = constant_line[name];
  1496. } else if (method_line.has(name)) {
  1497. line = method_line[name];
  1498. } else {
  1499. HashMap<String, HashMap<String, int>>::Iterator iter = enum_values_line.begin();
  1500. while (true) {
  1501. if (iter->value.has(name)) {
  1502. line = iter->value[name];
  1503. break;
  1504. } else if (iter == enum_values_line.last()) {
  1505. break;
  1506. } else {
  1507. ++iter;
  1508. }
  1509. }
  1510. }
  1511. }
  1512. if (class_desc->is_ready()) {
  1513. class_desc->call_deferred(SNAME("scroll_to_paragraph"), line);
  1514. } else {
  1515. scroll_to = line;
  1516. }
  1517. }
  1518. static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt, Control *p_owner_node) {
  1519. DocTools *doc = EditorHelp::get_doc_data();
  1520. String base_path;
  1521. Ref<Font> doc_font = p_owner_node->get_theme_font(SNAME("doc"), SNAME("EditorFonts"));
  1522. Ref<Font> doc_bold_font = p_owner_node->get_theme_font(SNAME("doc_bold"), SNAME("EditorFonts"));
  1523. Ref<Font> doc_italic_font = p_owner_node->get_theme_font(SNAME("doc_italic"), SNAME("EditorFonts"));
  1524. Ref<Font> doc_code_font = p_owner_node->get_theme_font(SNAME("doc_source"), SNAME("EditorFonts"));
  1525. Ref<Font> doc_kbd_font = p_owner_node->get_theme_font(SNAME("doc_keyboard"), SNAME("EditorFonts"));
  1526. int doc_code_font_size = p_owner_node->get_theme_font_size(SNAME("doc_source_size"), SNAME("EditorFonts"));
  1527. int doc_kbd_font_size = p_owner_node->get_theme_font_size(SNAME("doc_keyboard_size"), SNAME("EditorFonts"));
  1528. const Color type_color = p_owner_node->get_theme_color(SNAME("type_color"), SNAME("EditorHelp"));
  1529. const Color code_color = p_owner_node->get_theme_color(SNAME("code_color"), SNAME("EditorHelp"));
  1530. const Color kbd_color = p_owner_node->get_theme_color(SNAME("kbd_color"), SNAME("EditorHelp"));
  1531. const Color code_dark_color = Color(code_color, 0.8);
  1532. const Color link_color = p_owner_node->get_theme_color(SNAME("link_color"), SNAME("EditorHelp"));
  1533. const Color link_method_color = p_owner_node->get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  1534. const Color link_property_color = link_color.lerp(p_owner_node->get_theme_color(SNAME("accent_color"), SNAME("Editor")), 0.25);
  1535. const Color link_annotation_color = link_color.lerp(p_owner_node->get_theme_color(SNAME("accent_color"), SNAME("Editor")), 0.5);
  1536. const Color code_bg_color = p_owner_node->get_theme_color(SNAME("code_bg_color"), SNAME("EditorHelp"));
  1537. const Color kbd_bg_color = p_owner_node->get_theme_color(SNAME("kbd_bg_color"), SNAME("EditorHelp"));
  1538. const Color param_bg_color = p_owner_node->get_theme_color(SNAME("param_bg_color"), SNAME("EditorHelp"));
  1539. String bbcode = p_bbcode.dedent().replace("\t", "").replace("\r", "").strip_edges();
  1540. // Select the correct code examples.
  1541. switch ((int)EDITOR_GET("text_editor/help/class_reference_examples")) {
  1542. case 0: // GDScript
  1543. bbcode = bbcode.replace("[gdscript]", "[codeblock]");
  1544. bbcode = bbcode.replace("[/gdscript]", "[/codeblock]");
  1545. for (int pos = bbcode.find("[csharp]"); pos != -1; pos = bbcode.find("[csharp]")) {
  1546. int end_pos = bbcode.find("[/csharp]");
  1547. if (end_pos == -1) {
  1548. WARN_PRINT("Unclosed [csharp] block or parse fail in code (search for tag errors)");
  1549. break;
  1550. }
  1551. bbcode = bbcode.left(pos) + bbcode.substr(end_pos + 9); // 9 is length of "[/csharp]".
  1552. while (bbcode[pos] == '\n') {
  1553. bbcode = bbcode.left(pos) + bbcode.substr(pos + 1);
  1554. }
  1555. }
  1556. break;
  1557. case 1: // C#
  1558. bbcode = bbcode.replace("[csharp]", "[codeblock]");
  1559. bbcode = bbcode.replace("[/csharp]", "[/codeblock]");
  1560. for (int pos = bbcode.find("[gdscript]"); pos != -1; pos = bbcode.find("[gdscript]")) {
  1561. int end_pos = bbcode.find("[/gdscript]");
  1562. if (end_pos == -1) {
  1563. WARN_PRINT("Unclosed [gdscript] block or parse fail in code (search for tag errors)");
  1564. break;
  1565. }
  1566. bbcode = bbcode.left(pos) + bbcode.substr(end_pos + 11); // 11 is length of "[/gdscript]".
  1567. while (bbcode[pos] == '\n') {
  1568. bbcode = bbcode.left(pos) + bbcode.substr(pos + 1);
  1569. }
  1570. }
  1571. break;
  1572. case 2: // GDScript and C#
  1573. bbcode = bbcode.replace("[csharp]", "[b]C#:[/b]\n[codeblock]");
  1574. bbcode = bbcode.replace("[gdscript]", "[b]GDScript:[/b]\n[codeblock]");
  1575. bbcode = bbcode.replace("[/csharp]", "[/codeblock]");
  1576. bbcode = bbcode.replace("[/gdscript]", "[/codeblock]");
  1577. break;
  1578. }
  1579. // Remove codeblocks (they would be printed otherwise).
  1580. bbcode = bbcode.replace("[codeblocks]\n", "");
  1581. bbcode = bbcode.replace("\n[/codeblocks]", "");
  1582. bbcode = bbcode.replace("[codeblocks]", "");
  1583. bbcode = bbcode.replace("[/codeblocks]", "");
  1584. // Remove extra new lines around code blocks.
  1585. bbcode = bbcode.replace("[codeblock]\n", "[codeblock]");
  1586. bbcode = bbcode.replace("\n[/codeblock]", "[/codeblock]");
  1587. List<String> tag_stack;
  1588. bool code_tag = false;
  1589. bool codeblock_tag = false;
  1590. int pos = 0;
  1591. while (pos < bbcode.length()) {
  1592. int brk_pos = bbcode.find("[", pos);
  1593. if (brk_pos < 0) {
  1594. brk_pos = bbcode.length();
  1595. }
  1596. if (brk_pos > pos) {
  1597. String text = bbcode.substr(pos, brk_pos - pos);
  1598. if (!code_tag && !codeblock_tag) {
  1599. text = text.replace("\n", "\n\n");
  1600. }
  1601. p_rt->add_text(text);
  1602. }
  1603. if (brk_pos == bbcode.length()) {
  1604. break; // Nothing else to add.
  1605. }
  1606. int brk_end = bbcode.find("]", brk_pos + 1);
  1607. if (brk_end == -1) {
  1608. String text = bbcode.substr(brk_pos, bbcode.length() - brk_pos);
  1609. if (!code_tag && !codeblock_tag) {
  1610. text = text.replace("\n", "\n\n");
  1611. }
  1612. p_rt->add_text(text);
  1613. break;
  1614. }
  1615. String tag = bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
  1616. if (tag.begins_with("/")) {
  1617. bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length());
  1618. if (!tag_ok) {
  1619. p_rt->add_text("[");
  1620. pos = brk_pos + 1;
  1621. continue;
  1622. }
  1623. tag_stack.pop_front();
  1624. pos = brk_end + 1;
  1625. if (tag != "/img") {
  1626. p_rt->pop();
  1627. if (code_tag) {
  1628. p_rt->pop(); // font size
  1629. // Pop both color and background color.
  1630. p_rt->pop();
  1631. p_rt->pop();
  1632. } else if (codeblock_tag) {
  1633. p_rt->pop(); // font size
  1634. // Pop color, cell and table.
  1635. p_rt->pop();
  1636. p_rt->pop();
  1637. p_rt->pop();
  1638. }
  1639. }
  1640. code_tag = false;
  1641. codeblock_tag = false;
  1642. } else if (code_tag || codeblock_tag) {
  1643. p_rt->add_text("[");
  1644. pos = brk_pos + 1;
  1645. } else if (tag.begins_with("method ") || tag.begins_with("member ") || tag.begins_with("signal ") || tag.begins_with("enum ") || tag.begins_with("constant ") || tag.begins_with("annotation ") || tag.begins_with("theme_item ")) {
  1646. const int tag_end = tag.find(" ");
  1647. const String link_tag = tag.substr(0, tag_end);
  1648. const String link_target = tag.substr(tag_end + 1, tag.length()).lstrip(" ");
  1649. // Use monospace font to make clickable references
  1650. // easier to distinguish from inline code and other text.
  1651. p_rt->push_font(doc_code_font);
  1652. p_rt->push_font_size(doc_code_font_size);
  1653. Color target_color = link_color;
  1654. if (link_tag == "method") {
  1655. target_color = link_method_color;
  1656. } else if (link_tag == "member" || link_tag == "signal" || link_tag == "theme property") {
  1657. target_color = link_property_color;
  1658. } else if (link_tag == "annotation") {
  1659. target_color = link_annotation_color;
  1660. }
  1661. p_rt->push_color(target_color);
  1662. p_rt->push_meta("@" + link_tag + " " + link_target);
  1663. p_rt->add_text(link_target + (link_tag == "method" ? "()" : ""));
  1664. p_rt->pop();
  1665. p_rt->pop();
  1666. p_rt->pop(); // font size
  1667. p_rt->pop(); // font
  1668. pos = brk_end + 1;
  1669. } else if (tag.begins_with("param ")) {
  1670. const int tag_end = tag.find(" ");
  1671. const String param_name = tag.substr(tag_end + 1, tag.length()).lstrip(" ");
  1672. // Use monospace font with translucent background color to make code easier to distinguish from other text.
  1673. p_rt->push_font(doc_code_font);
  1674. p_rt->push_font_size(doc_code_font_size);
  1675. p_rt->push_bgcolor(param_bg_color);
  1676. p_rt->push_color(code_color);
  1677. p_rt->add_text(param_name);
  1678. p_rt->pop();
  1679. p_rt->pop();
  1680. p_rt->pop(); // font size
  1681. p_rt->pop(); // font
  1682. pos = brk_end + 1;
  1683. } else if (doc->class_list.has(tag)) {
  1684. // Class reference tag such as [Node2D] or [SceneTree].
  1685. // Use monospace font to make clickable references
  1686. // easier to distinguish from inline code and other text.
  1687. p_rt->push_font(doc_code_font);
  1688. p_rt->push_font_size(doc_code_font_size);
  1689. p_rt->push_color(type_color);
  1690. p_rt->push_meta("#" + tag);
  1691. p_rt->add_text(tag);
  1692. p_rt->pop();
  1693. p_rt->pop();
  1694. p_rt->pop(); // font size
  1695. p_rt->pop(); // font
  1696. pos = brk_end + 1;
  1697. } else if (tag == "b") {
  1698. // Use bold font.
  1699. p_rt->push_font(doc_bold_font);
  1700. pos = brk_end + 1;
  1701. tag_stack.push_front(tag);
  1702. } else if (tag == "i") {
  1703. // Use italics font.
  1704. p_rt->push_font(doc_italic_font);
  1705. pos = brk_end + 1;
  1706. tag_stack.push_front(tag);
  1707. } else if (tag == "code") {
  1708. // Use monospace font with darkened background color to make code easier to distinguish from other text.
  1709. p_rt->push_font(doc_code_font);
  1710. p_rt->push_font_size(doc_code_font_size);
  1711. p_rt->push_bgcolor(code_bg_color);
  1712. p_rt->push_color(code_color.lerp(p_owner_node->get_theme_color(SNAME("error_color"), SNAME("Editor")), 0.6));
  1713. code_tag = true;
  1714. pos = brk_end + 1;
  1715. tag_stack.push_front(tag);
  1716. } else if (tag == "codeblock") {
  1717. // Use monospace font with darkened background color to make code easier to distinguish from other text.
  1718. // Use a single-column table with cell row background color instead of `[bgcolor]`.
  1719. // This makes the background color highlight cover the entire block, rather than individual lines.
  1720. p_rt->push_font(doc_code_font);
  1721. p_rt->push_font_size(doc_code_font_size);
  1722. p_rt->push_table(1);
  1723. p_rt->push_cell();
  1724. p_rt->set_cell_row_background_color(code_bg_color, Color(code_bg_color, 0.99));
  1725. p_rt->set_cell_padding(Rect2(10 * EDSCALE, 10 * EDSCALE, 10 * EDSCALE, 10 * EDSCALE));
  1726. p_rt->push_color(code_dark_color);
  1727. codeblock_tag = true;
  1728. pos = brk_end + 1;
  1729. tag_stack.push_front(tag);
  1730. } else if (tag == "kbd") {
  1731. // Use keyboard font with custom color and background color.
  1732. p_rt->push_font(doc_kbd_font);
  1733. p_rt->push_font_size(doc_kbd_font_size);
  1734. p_rt->push_bgcolor(kbd_bg_color);
  1735. p_rt->push_color(kbd_color);
  1736. code_tag = true; // Though not strictly a code tag, logic is similar.
  1737. pos = brk_end + 1;
  1738. tag_stack.push_front(tag);
  1739. } else if (tag == "center") {
  1740. // Align to center.
  1741. p_rt->push_paragraph(HORIZONTAL_ALIGNMENT_CENTER, Control::TEXT_DIRECTION_AUTO, "");
  1742. pos = brk_end + 1;
  1743. tag_stack.push_front(tag);
  1744. } else if (tag == "br") {
  1745. // Force a line break.
  1746. p_rt->add_newline();
  1747. pos = brk_end + 1;
  1748. } else if (tag == "u") {
  1749. // Use underline.
  1750. p_rt->push_underline();
  1751. pos = brk_end + 1;
  1752. tag_stack.push_front(tag);
  1753. } else if (tag == "s") {
  1754. // Use strikethrough.
  1755. p_rt->push_strikethrough();
  1756. pos = brk_end + 1;
  1757. tag_stack.push_front(tag);
  1758. } else if (tag == "url") {
  1759. int end = bbcode.find("[", brk_end);
  1760. if (end == -1) {
  1761. end = bbcode.length();
  1762. }
  1763. String url = bbcode.substr(brk_end + 1, end - brk_end - 1);
  1764. p_rt->push_meta(url);
  1765. pos = brk_end + 1;
  1766. tag_stack.push_front(tag);
  1767. } else if (tag.begins_with("url=")) {
  1768. String url = tag.substr(4, tag.length());
  1769. p_rt->push_meta(url);
  1770. pos = brk_end + 1;
  1771. tag_stack.push_front("url");
  1772. } else if (tag == "img") {
  1773. int end = bbcode.find("[", brk_end);
  1774. if (end == -1) {
  1775. end = bbcode.length();
  1776. }
  1777. String image = bbcode.substr(brk_end + 1, end - brk_end - 1);
  1778. Ref<Texture2D> texture = ResourceLoader::load(base_path.path_join(image), "Texture2D");
  1779. if (texture.is_valid()) {
  1780. p_rt->add_image(texture);
  1781. }
  1782. pos = end;
  1783. tag_stack.push_front(tag);
  1784. } else if (tag.begins_with("color=")) {
  1785. String col = tag.substr(6, tag.length());
  1786. Color color = Color::from_string(col, Color());
  1787. p_rt->push_color(color);
  1788. pos = brk_end + 1;
  1789. tag_stack.push_front("color");
  1790. } else if (tag.begins_with("font=")) {
  1791. String fnt = tag.substr(5, tag.length());
  1792. Ref<Font> font = ResourceLoader::load(base_path.path_join(fnt), "Font");
  1793. if (font.is_valid()) {
  1794. p_rt->push_font(font);
  1795. } else {
  1796. p_rt->push_font(doc_font);
  1797. }
  1798. pos = brk_end + 1;
  1799. tag_stack.push_front("font");
  1800. } else {
  1801. p_rt->add_text("["); //ignore
  1802. pos = brk_pos + 1;
  1803. }
  1804. }
  1805. }
  1806. void EditorHelp::_add_text(const String &p_bbcode) {
  1807. _add_text_to_rt(p_bbcode, class_desc, this);
  1808. }
  1809. Thread EditorHelp::thread;
  1810. void EditorHelp::_wait_for_thread() {
  1811. if (thread.is_started()) {
  1812. thread.wait_to_finish();
  1813. }
  1814. }
  1815. void EditorHelp::_gen_doc_thread(void *p_udata) {
  1816. DocTools compdoc;
  1817. compdoc.load_compressed(_doc_data_compressed, _doc_data_compressed_size, _doc_data_uncompressed_size);
  1818. doc->merge_from(compdoc); // Ensure all is up to date.
  1819. }
  1820. static bool doc_gen_use_threads = true;
  1821. void EditorHelp::generate_doc() {
  1822. doc = memnew(DocTools);
  1823. // Not doable on threads unfortunately, since it instantiates all sorts of classes to get default values.
  1824. doc->generate(true);
  1825. if (doc_gen_use_threads) {
  1826. thread.start(_gen_doc_thread, nullptr);
  1827. } else {
  1828. _gen_doc_thread(nullptr);
  1829. }
  1830. }
  1831. void EditorHelp::_toggle_scripts_pressed() {
  1832. ScriptEditor::get_singleton()->toggle_scripts_panel();
  1833. update_toggle_scripts_button();
  1834. }
  1835. void EditorHelp::_notification(int p_what) {
  1836. switch (p_what) {
  1837. case NOTIFICATION_POSTINITIALIZE: {
  1838. // Requires theme to be up to date.
  1839. _class_desc_resized(false);
  1840. } break;
  1841. case NOTIFICATION_READY:
  1842. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1843. _wait_for_thread();
  1844. _update_doc();
  1845. } break;
  1846. case NOTIFICATION_THEME_CHANGED: {
  1847. if (is_inside_tree()) {
  1848. _class_desc_resized(true);
  1849. }
  1850. update_toggle_scripts_button();
  1851. } break;
  1852. case NOTIFICATION_VISIBILITY_CHANGED: {
  1853. update_toggle_scripts_button();
  1854. } break;
  1855. }
  1856. }
  1857. void EditorHelp::go_to_help(const String &p_help) {
  1858. _wait_for_thread();
  1859. _help_callback(p_help);
  1860. }
  1861. void EditorHelp::go_to_class(const String &p_class, int p_scroll) {
  1862. _wait_for_thread();
  1863. _goto_desc(p_class, p_scroll);
  1864. }
  1865. void EditorHelp::update_doc() {
  1866. _wait_for_thread();
  1867. ERR_FAIL_COND(!doc->class_list.has(edited_class));
  1868. ERR_FAIL_COND(!doc->class_list[edited_class].is_script_doc);
  1869. _update_doc();
  1870. }
  1871. void EditorHelp::cleanup_doc() {
  1872. _wait_for_thread();
  1873. if (doc_gen_use_threads) {
  1874. thread.wait_to_finish();
  1875. }
  1876. memdelete(doc);
  1877. }
  1878. Vector<Pair<String, int>> EditorHelp::get_sections() {
  1879. _wait_for_thread();
  1880. Vector<Pair<String, int>> sections;
  1881. for (int i = 0; i < section_line.size(); i++) {
  1882. sections.push_back(Pair<String, int>(section_line[i].first, i));
  1883. }
  1884. return sections;
  1885. }
  1886. void EditorHelp::scroll_to_section(int p_section_index) {
  1887. _wait_for_thread();
  1888. int line = section_line[p_section_index].second;
  1889. if (class_desc->is_ready()) {
  1890. class_desc->scroll_to_paragraph(line);
  1891. } else {
  1892. scroll_to = line;
  1893. }
  1894. }
  1895. void EditorHelp::popup_search() {
  1896. _wait_for_thread();
  1897. find_bar->popup_search();
  1898. }
  1899. String EditorHelp::get_class() {
  1900. return edited_class;
  1901. }
  1902. void EditorHelp::search_again(bool p_search_previous) {
  1903. _search(p_search_previous);
  1904. }
  1905. int EditorHelp::get_scroll() const {
  1906. return class_desc->get_v_scroll_bar()->get_value();
  1907. }
  1908. void EditorHelp::set_scroll(int p_scroll) {
  1909. class_desc->get_v_scroll_bar()->set_value(p_scroll);
  1910. }
  1911. void EditorHelp::update_toggle_scripts_button() {
  1912. if (is_layout_rtl()) {
  1913. toggle_scripts_button->set_icon(get_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Forward") : SNAME("Back"), SNAME("EditorIcons")));
  1914. } else {
  1915. toggle_scripts_button->set_icon(get_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Back") : SNAME("Forward"), SNAME("EditorIcons")));
  1916. }
  1917. toggle_scripts_button->set_tooltip_text(vformat("%s (%s)", TTR("Toggle Scripts Panel"), ED_GET_SHORTCUT("script_editor/toggle_scripts_panel")->get_as_text()));
  1918. }
  1919. void EditorHelp::_bind_methods() {
  1920. ClassDB::bind_method("_class_list_select", &EditorHelp::_class_list_select);
  1921. ClassDB::bind_method("_request_help", &EditorHelp::_request_help);
  1922. ClassDB::bind_method("_search", &EditorHelp::_search);
  1923. ClassDB::bind_method("_help_callback", &EditorHelp::_help_callback);
  1924. ADD_SIGNAL(MethodInfo("go_to_help"));
  1925. }
  1926. EditorHelp::EditorHelp() {
  1927. set_custom_minimum_size(Size2(150 * EDSCALE, 0));
  1928. EDITOR_DEF("text_editor/help/sort_functions_alphabetically", true);
  1929. class_desc = memnew(RichTextLabel);
  1930. add_child(class_desc);
  1931. class_desc->set_threaded(true);
  1932. class_desc->set_v_size_flags(SIZE_EXPAND_FILL);
  1933. class_desc->connect("finished", callable_mp(this, &EditorHelp::_class_desc_finished));
  1934. class_desc->connect("meta_clicked", callable_mp(this, &EditorHelp::_class_desc_select));
  1935. class_desc->connect("gui_input", callable_mp(this, &EditorHelp::_class_desc_input));
  1936. class_desc->connect("resized", callable_mp(this, &EditorHelp::_class_desc_resized).bind(false));
  1937. // Added second so it opens at the bottom so it won't offset the entire widget.
  1938. find_bar = memnew(FindBar);
  1939. add_child(find_bar);
  1940. find_bar->hide();
  1941. find_bar->set_rich_text_label(class_desc);
  1942. status_bar = memnew(HBoxContainer);
  1943. add_child(status_bar);
  1944. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  1945. status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE));
  1946. toggle_scripts_button = memnew(Button);
  1947. toggle_scripts_button->set_flat(true);
  1948. toggle_scripts_button->connect("pressed", callable_mp(this, &EditorHelp::_toggle_scripts_pressed));
  1949. status_bar->add_child(toggle_scripts_button);
  1950. class_desc->set_selection_enabled(true);
  1951. class_desc->hide();
  1952. }
  1953. EditorHelp::~EditorHelp() {
  1954. }
  1955. DocTools *EditorHelp::get_doc_data() {
  1956. _wait_for_thread();
  1957. return doc;
  1958. }
  1959. //// EditorHelpBit ///
  1960. void EditorHelpBit::_go_to_help(String p_what) {
  1961. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  1962. ScriptEditor::get_singleton()->goto_help(p_what);
  1963. emit_signal(SNAME("request_hide"));
  1964. }
  1965. void EditorHelpBit::_meta_clicked(String p_select) {
  1966. if (p_select.begins_with("$")) { //enum
  1967. String select = p_select.substr(1, p_select.length());
  1968. String class_name;
  1969. if (select.contains(".")) {
  1970. class_name = select.get_slice(".", 0);
  1971. } else {
  1972. class_name = "@Global";
  1973. }
  1974. _go_to_help("class_enum:" + class_name + ":" + select);
  1975. return;
  1976. } else if (p_select.begins_with("#")) {
  1977. _go_to_help("class_name:" + p_select.substr(1, p_select.length()));
  1978. return;
  1979. } else if (p_select.begins_with("@")) {
  1980. String m = p_select.substr(1, p_select.length());
  1981. if (m.contains(".")) {
  1982. _go_to_help("class_method:" + m.get_slice(".", 0) + ":" + m.get_slice(".", 0)); // Must go somewhere else.
  1983. }
  1984. }
  1985. }
  1986. void EditorHelpBit::_bind_methods() {
  1987. ClassDB::bind_method(D_METHOD("set_text", "text"), &EditorHelpBit::set_text);
  1988. ADD_SIGNAL(MethodInfo("request_hide"));
  1989. }
  1990. void EditorHelpBit::_notification(int p_what) {
  1991. switch (p_what) {
  1992. case NOTIFICATION_THEME_CHANGED: {
  1993. rich_text->add_theme_color_override("selection_color", get_theme_color(SNAME("selection_color"), SNAME("EditorHelp")));
  1994. rich_text->clear();
  1995. _add_text_to_rt(text, rich_text, this);
  1996. rich_text->reset_size(); // Force recalculating size after parsing bbcode.
  1997. } break;
  1998. }
  1999. }
  2000. void EditorHelpBit::set_text(const String &p_text) {
  2001. text = p_text;
  2002. rich_text->clear();
  2003. _add_text_to_rt(text, rich_text, this);
  2004. }
  2005. EditorHelpBit::EditorHelpBit() {
  2006. rich_text = memnew(RichTextLabel);
  2007. add_child(rich_text);
  2008. rich_text->connect("meta_clicked", callable_mp(this, &EditorHelpBit::_meta_clicked));
  2009. rich_text->set_fit_content(true);
  2010. set_custom_minimum_size(Size2(0, 50 * EDSCALE));
  2011. }
  2012. //// FindBar ///
  2013. FindBar::FindBar() {
  2014. search_text = memnew(LineEdit);
  2015. add_child(search_text);
  2016. search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  2017. search_text->set_h_size_flags(SIZE_EXPAND_FILL);
  2018. search_text->connect("text_changed", callable_mp(this, &FindBar::_search_text_changed));
  2019. search_text->connect("text_submitted", callable_mp(this, &FindBar::_search_text_submitted));
  2020. matches_label = memnew(Label);
  2021. add_child(matches_label);
  2022. matches_label->hide();
  2023. find_prev = memnew(Button);
  2024. find_prev->set_flat(true);
  2025. add_child(find_prev);
  2026. find_prev->set_focus_mode(FOCUS_NONE);
  2027. find_prev->connect("pressed", callable_mp(this, &FindBar::search_prev));
  2028. find_next = memnew(Button);
  2029. find_next->set_flat(true);
  2030. add_child(find_next);
  2031. find_next->set_focus_mode(FOCUS_NONE);
  2032. find_next->connect("pressed", callable_mp(this, &FindBar::search_next));
  2033. Control *space = memnew(Control);
  2034. add_child(space);
  2035. space->set_custom_minimum_size(Size2(4, 0) * EDSCALE);
  2036. hide_button = memnew(TextureButton);
  2037. add_child(hide_button);
  2038. hide_button->set_focus_mode(FOCUS_NONE);
  2039. hide_button->set_ignore_texture_size(true);
  2040. hide_button->set_stretch_mode(TextureButton::STRETCH_KEEP_CENTERED);
  2041. hide_button->connect("pressed", callable_mp(this, &FindBar::_hide_bar));
  2042. }
  2043. void FindBar::popup_search() {
  2044. show();
  2045. bool grabbed_focus = false;
  2046. if (!search_text->has_focus()) {
  2047. search_text->grab_focus();
  2048. grabbed_focus = true;
  2049. }
  2050. if (!search_text->get_text().is_empty()) {
  2051. search_text->select_all();
  2052. search_text->set_caret_column(search_text->get_text().length());
  2053. if (grabbed_focus) {
  2054. _search();
  2055. }
  2056. }
  2057. }
  2058. void FindBar::_notification(int p_what) {
  2059. switch (p_what) {
  2060. case NOTIFICATION_THEME_CHANGED: {
  2061. find_prev->set_icon(get_theme_icon(SNAME("MoveUp"), SNAME("EditorIcons")));
  2062. find_next->set_icon(get_theme_icon(SNAME("MoveDown"), SNAME("EditorIcons")));
  2063. hide_button->set_texture_normal(get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  2064. hide_button->set_texture_hover(get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  2065. hide_button->set_texture_pressed(get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  2066. hide_button->set_custom_minimum_size(hide_button->get_texture_normal()->get_size());
  2067. matches_label->add_theme_color_override("font_color", results_count > 0 ? get_theme_color(SNAME("font_color"), SNAME("Label")) : get_theme_color(SNAME("error_color"), SNAME("Editor")));
  2068. } break;
  2069. case NOTIFICATION_VISIBILITY_CHANGED: {
  2070. set_process_unhandled_input(is_visible_in_tree());
  2071. } break;
  2072. }
  2073. }
  2074. void FindBar::set_rich_text_label(RichTextLabel *p_rich_text_label) {
  2075. rich_text_label = p_rich_text_label;
  2076. }
  2077. bool FindBar::search_next() {
  2078. return _search();
  2079. }
  2080. bool FindBar::search_prev() {
  2081. return _search(true);
  2082. }
  2083. bool FindBar::_search(bool p_search_previous) {
  2084. String stext = search_text->get_text();
  2085. bool keep = prev_search == stext;
  2086. bool ret = rich_text_label->search(stext, keep, p_search_previous);
  2087. prev_search = stext;
  2088. if (ret) {
  2089. _update_results_count();
  2090. } else {
  2091. results_count = 0;
  2092. }
  2093. _update_matches_label();
  2094. return ret;
  2095. }
  2096. void FindBar::_update_results_count() {
  2097. results_count = 0;
  2098. String searched = search_text->get_text();
  2099. if (searched.is_empty()) {
  2100. return;
  2101. }
  2102. String full_text = rich_text_label->get_parsed_text();
  2103. int from_pos = 0;
  2104. while (true) {
  2105. int pos = full_text.findn(searched, from_pos);
  2106. if (pos == -1) {
  2107. break;
  2108. }
  2109. results_count++;
  2110. from_pos = pos + searched.length();
  2111. }
  2112. }
  2113. void FindBar::_update_matches_label() {
  2114. if (search_text->get_text().is_empty() || results_count == -1) {
  2115. matches_label->hide();
  2116. } else {
  2117. matches_label->show();
  2118. matches_label->add_theme_color_override("font_color", results_count > 0 ? get_theme_color(SNAME("font_color"), SNAME("Label")) : get_theme_color(SNAME("error_color"), SNAME("Editor")));
  2119. matches_label->set_text(vformat(results_count == 1 ? TTR("%d match.") : TTR("%d matches."), results_count));
  2120. }
  2121. }
  2122. void FindBar::_hide_bar() {
  2123. if (search_text->has_focus()) {
  2124. rich_text_label->grab_focus();
  2125. }
  2126. hide();
  2127. }
  2128. void FindBar::unhandled_input(const Ref<InputEvent> &p_event) {
  2129. ERR_FAIL_COND(p_event.is_null());
  2130. Ref<InputEventKey> k = p_event;
  2131. if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel"), false, true)) {
  2132. if (rich_text_label->has_focus() || is_ancestor_of(get_viewport()->gui_get_focus_owner())) {
  2133. _hide_bar();
  2134. accept_event();
  2135. }
  2136. }
  2137. }
  2138. void FindBar::_search_text_changed(const String &p_text) {
  2139. search_next();
  2140. }
  2141. void FindBar::_search_text_submitted(const String &p_text) {
  2142. if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  2143. search_prev();
  2144. } else {
  2145. search_next();
  2146. }
  2147. }