editor_help.cpp 77 KB

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