2
0

editor_help.cpp 82 KB

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