editor_help.cpp 97 KB

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