editor_help.cpp 73 KB

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