editor_help.cpp 85 KB

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