editor_help.cpp 79 KB

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