editor_help.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049
  1. /*************************************************************************/
  2. /* editor_help.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "doc_data_compressed.gen.h"
  35. #include "editor/plugins/script_editor_plugin.h"
  36. #include "editor_node.h"
  37. #include "editor_scale.h"
  38. #include "editor_settings.h"
  39. #define CONTRIBUTE_URL "https://docs.godotengine.org/en/latest/community/contributing/updating_the_class_reference.html"
  40. DocTools *EditorHelp::doc = nullptr;
  41. void EditorHelp::_init_colors() {
  42. title_color = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  43. text_color = get_theme_color(SNAME("default_color"), SNAME("RichTextLabel"));
  44. headline_color = get_theme_color(SNAME("headline_color"), SNAME("EditorHelp"));
  45. base_type_color = title_color.lerp(text_color, 0.5);
  46. comment_color = text_color * Color(1, 1, 1, 0.6);
  47. symbol_color = comment_color;
  48. value_color = text_color * Color(1, 1, 1, 0.6);
  49. qualifier_color = text_color * Color(1, 1, 1, 0.8);
  50. type_color = get_theme_color(SNAME("accent_color"), SNAME("Editor")).lerp(text_color, 0.5);
  51. class_desc->add_theme_color_override("selection_color", get_theme_color(SNAME("accent_color"), SNAME("Editor")) * Color(1, 1, 1, 0.4));
  52. class_desc->add_theme_constant_override("line_separation", Math::round(5 * EDSCALE));
  53. }
  54. void EditorHelp::_search(bool p_search_previous) {
  55. if (p_search_previous) {
  56. find_bar->search_prev();
  57. } else {
  58. find_bar->search_next();
  59. }
  60. }
  61. void EditorHelp::_class_list_select(const String &p_select) {
  62. _goto_desc(p_select);
  63. }
  64. void EditorHelp::_class_desc_select(const String &p_select) {
  65. if (p_select.begins_with("$")) { //enum
  66. String select = p_select.substr(1, p_select.length());
  67. String class_name;
  68. if (select.find(".") != -1) {
  69. class_name = select.get_slice(".", 0);
  70. select = select.get_slice(".", 1);
  71. } else {
  72. class_name = "@GlobalScope";
  73. }
  74. emit_signal(SNAME("go_to_help"), "class_enum:" + class_name + ":" + select);
  75. return;
  76. } else if (p_select.begins_with("#")) {
  77. emit_signal(SNAME("go_to_help"), "class_name:" + p_select.substr(1, p_select.length()));
  78. return;
  79. } else if (p_select.begins_with("@")) {
  80. int tag_end = p_select.find(" ");
  81. String tag = p_select.substr(1, tag_end - 1);
  82. String link = p_select.substr(tag_end + 1, p_select.length()).lstrip(" ");
  83. String topic;
  84. Map<String, int> *table = nullptr;
  85. if (tag == "method") {
  86. topic = "class_method";
  87. table = &this->method_line;
  88. } else if (tag == "member") {
  89. topic = "class_property";
  90. table = &this->property_line;
  91. } else if (tag == "enum") {
  92. topic = "class_enum";
  93. table = &this->enum_line;
  94. } else if (tag == "signal") {
  95. topic = "class_signal";
  96. table = &this->signal_line;
  97. } else if (tag == "constant") {
  98. topic = "class_constant";
  99. table = &this->constant_line;
  100. } else {
  101. return;
  102. }
  103. if (link.find(".") != -1) {
  104. emit_signal(SNAME("go_to_help"), topic + ":" + link.get_slice(".", 0) + ":" + link.get_slice(".", 1));
  105. } else {
  106. if (table->has(link)) {
  107. // Found in the current page
  108. class_desc->scroll_to_paragraph((*table)[link]);
  109. } else {
  110. if (topic == "class_enum") {
  111. // Try to find the enum in @GlobalScope
  112. const DocData::ClassDoc &cd = doc->class_list["@GlobalScope"];
  113. for (int i = 0; i < cd.constants.size(); i++) {
  114. if (cd.constants[i].enumeration == link) {
  115. // Found in @GlobalScope
  116. emit_signal(SNAME("go_to_help"), topic + ":@GlobalScope:" + link);
  117. break;
  118. }
  119. }
  120. } else if (topic == "class_constant") {
  121. // Try to find the constant in @GlobalScope
  122. const DocData::ClassDoc &cd = doc->class_list["@GlobalScope"];
  123. for (int i = 0; i < cd.constants.size(); i++) {
  124. if (cd.constants[i].name == link) {
  125. // Found in @GlobalScope
  126. emit_signal(SNAME("go_to_help"), topic + ":@GlobalScope:" + link);
  127. break;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. } else if (p_select.begins_with("http")) {
  134. OS::get_singleton()->shell_open(p_select);
  135. }
  136. }
  137. void EditorHelp::_class_desc_input(const Ref<InputEvent> &p_input) {
  138. }
  139. void EditorHelp::_class_desc_resized() {
  140. // Add extra horizontal margins for better readability.
  141. // The margins increase as the width of the editor help container increases.
  142. Ref<Font> doc_code_font = get_theme_font(SNAME("doc_source"), SNAME("EditorFonts"));
  143. int font_size = get_theme_font_size(SNAME("doc_source_size"), SNAME("EditorFonts"));
  144. real_t char_width = doc_code_font->get_char_size('x', 0, font_size).width;
  145. const int display_margin = MAX(30 * EDSCALE, get_parent_anchorable_rect().size.width - char_width * 120 * EDSCALE) * 0.5;
  146. Ref<StyleBox> class_desc_stylebox = EditorNode::get_singleton()->get_theme_base()->get_theme_stylebox(SNAME("normal"), SNAME("RichTextLabel"))->duplicate();
  147. class_desc_stylebox->set_default_margin(SIDE_LEFT, display_margin);
  148. class_desc_stylebox->set_default_margin(SIDE_RIGHT, display_margin);
  149. class_desc->add_theme_style_override("normal", class_desc_stylebox);
  150. }
  151. void EditorHelp::_add_type(const String &p_type, const String &p_enum) {
  152. String t = p_type;
  153. if (t.is_empty()) {
  154. t = "void";
  155. }
  156. bool can_ref = (t != "void" && t.find("*") == -1) || !p_enum.is_empty();
  157. if (!p_enum.is_empty()) {
  158. if (p_enum.get_slice_count(".") > 1) {
  159. t = p_enum.get_slice(".", 1);
  160. } else {
  161. t = p_enum.get_slice(".", 0);
  162. }
  163. }
  164. const Color text_color = get_theme_color(SNAME("default_color"), SNAME("RichTextLabel"));
  165. const Color type_color = get_theme_color(SNAME("accent_color"), SNAME("Editor")).lerp(text_color, 0.5);
  166. class_desc->push_color(type_color);
  167. bool add_array = false;
  168. if (can_ref) {
  169. if (t.ends_with("[]")) {
  170. add_array = true;
  171. t = t.replace("[]", "");
  172. }
  173. if (p_enum.is_empty()) {
  174. class_desc->push_meta("#" + t); //class
  175. } else {
  176. class_desc->push_meta("$" + p_enum); //class
  177. }
  178. }
  179. class_desc->add_text(t);
  180. if (can_ref) {
  181. class_desc->pop();
  182. if (add_array) {
  183. class_desc->add_text(" ");
  184. class_desc->push_meta("#Array"); //class
  185. class_desc->add_text("[]");
  186. class_desc->pop();
  187. }
  188. }
  189. class_desc->pop();
  190. }
  191. String EditorHelp::_fix_constant(const String &p_constant) const {
  192. if (p_constant.strip_edges() == "4294967295") {
  193. return "0xFFFFFFFF";
  194. }
  195. if (p_constant.strip_edges() == "2147483647") {
  196. return "0x7FFFFFFF";
  197. }
  198. if (p_constant.strip_edges() == "1048575") {
  199. return "0xFFFFF";
  200. }
  201. return p_constant;
  202. }
  203. void EditorHelp::_add_method(const DocData::MethodDoc &p_method, bool p_overview) {
  204. method_line[p_method.name] = class_desc->get_line_count() - 2; //gets overridden if description
  205. const bool is_vararg = p_method.qualifiers.find("vararg") != -1;
  206. if (p_overview) {
  207. class_desc->push_cell();
  208. class_desc->push_paragraph(RichTextLabel::ALIGN_RIGHT, Control::TEXT_DIRECTION_AUTO, "");
  209. } else {
  210. static const char32_t prefix[3] = { 0x25CF /* filled circle */, ' ', 0 };
  211. class_desc->add_text(String(prefix));
  212. }
  213. _add_type(p_method.return_type, p_method.return_enum);
  214. if (p_overview) {
  215. class_desc->pop(); //align
  216. class_desc->pop(); //cell
  217. class_desc->push_cell();
  218. } else {
  219. class_desc->add_text(" ");
  220. }
  221. if (p_overview && p_method.description != "") {
  222. class_desc->push_meta("@method " + p_method.name);
  223. }
  224. class_desc->push_color(headline_color);
  225. _add_text(p_method.name);
  226. class_desc->pop();
  227. if (p_overview && p_method.description != "") {
  228. class_desc->pop(); //meta
  229. }
  230. class_desc->push_color(symbol_color);
  231. class_desc->add_text("(");
  232. class_desc->pop();
  233. for (int j = 0; j < p_method.arguments.size(); j++) {
  234. class_desc->push_color(text_color);
  235. if (j > 0) {
  236. class_desc->add_text(", ");
  237. }
  238. _add_text(p_method.arguments[j].name);
  239. class_desc->add_text(": ");
  240. _add_type(p_method.arguments[j].type, p_method.arguments[j].enumeration);
  241. if (p_method.arguments[j].default_value != "") {
  242. class_desc->push_color(symbol_color);
  243. class_desc->add_text(" = ");
  244. class_desc->pop();
  245. class_desc->push_color(value_color);
  246. _add_text(_fix_constant(p_method.arguments[j].default_value));
  247. class_desc->pop();
  248. }
  249. class_desc->pop();
  250. }
  251. if (is_vararg) {
  252. class_desc->push_color(text_color);
  253. if (p_method.arguments.size()) {
  254. class_desc->add_text(", ");
  255. }
  256. class_desc->push_color(symbol_color);
  257. class_desc->add_text("...");
  258. class_desc->pop();
  259. class_desc->pop();
  260. }
  261. class_desc->push_color(symbol_color);
  262. class_desc->add_text(")");
  263. class_desc->pop();
  264. if (p_method.qualifiers != "") {
  265. class_desc->push_color(qualifier_color);
  266. class_desc->add_text(" ");
  267. _add_text(p_method.qualifiers);
  268. class_desc->pop();
  269. }
  270. if (p_overview) {
  271. class_desc->pop(); //cell
  272. }
  273. }
  274. Error EditorHelp::_goto_desc(const String &p_class, int p_vscr) {
  275. if (!doc->class_list.has(p_class)) {
  276. return ERR_DOES_NOT_EXIST;
  277. }
  278. select_locked = true;
  279. class_desc->show();
  280. description_line = 0;
  281. if (p_class == edited_class) {
  282. return OK; //already there
  283. }
  284. edited_class = p_class;
  285. _update_doc();
  286. return OK;
  287. }
  288. void EditorHelp::_update_method_list(const Vector<DocData::MethodDoc> p_methods, bool &r_method_descrpitons) {
  289. Ref<Font> doc_code_font = get_theme_font(SNAME("doc_source"), SNAME("EditorFonts"));
  290. class_desc->pop();
  291. class_desc->pop();
  292. class_desc->add_newline();
  293. class_desc->push_font(doc_code_font);
  294. class_desc->push_indent(1);
  295. class_desc->push_table(2);
  296. class_desc->set_table_column_expand(1, true);
  297. bool any_previous = false;
  298. for (int pass = 0; pass < 2; pass++) {
  299. Vector<DocData::MethodDoc> m;
  300. for (int i = 0; i < p_methods.size(); i++) {
  301. const String &q = p_methods[i].qualifiers;
  302. if ((pass == 0 && q.find("virtual") != -1) || (pass == 1 && q.find("virtual") == -1)) {
  303. m.push_back(p_methods[i]);
  304. }
  305. }
  306. if (any_previous && !m.is_empty()) {
  307. class_desc->push_cell();
  308. class_desc->pop(); //cell
  309. class_desc->push_cell();
  310. class_desc->pop(); //cell
  311. }
  312. String group_prefix;
  313. for (int i = 0; i < m.size(); i++) {
  314. const String new_prefix = m[i].name.substr(0, 3);
  315. bool is_new_group = false;
  316. if (i < m.size() - 1 && new_prefix == m[i + 1].name.substr(0, 3) && new_prefix != group_prefix) {
  317. is_new_group = i > 0;
  318. group_prefix = new_prefix;
  319. } else if (group_prefix != "" && new_prefix != group_prefix) {
  320. is_new_group = true;
  321. group_prefix = "";
  322. }
  323. if (is_new_group && pass == 1) {
  324. class_desc->push_cell();
  325. class_desc->pop(); //cell
  326. class_desc->push_cell();
  327. class_desc->pop(); //cell
  328. }
  329. if (m[i].description != "" || m[i].errors_returned.size() > 0) {
  330. r_method_descrpitons = true;
  331. }
  332. _add_method(m[i], true);
  333. }
  334. any_previous = !m.is_empty();
  335. }
  336. class_desc->pop(); //table
  337. class_desc->pop();
  338. class_desc->pop(); // font
  339. class_desc->add_newline();
  340. class_desc->add_newline();
  341. }
  342. void EditorHelp::_update_method_descriptions(const DocData::ClassDoc p_classdoc, const Vector<DocData::MethodDoc> p_methods, const String &p_method_type) {
  343. Ref<Font> doc_font = get_theme_font(SNAME("doc"), SNAME("EditorFonts"));
  344. Ref<Font> doc_bold_font = get_theme_font(SNAME("doc_bold"), SNAME("EditorFonts"));
  345. Ref<Font> doc_code_font = get_theme_font(SNAME("doc_source"), SNAME("EditorFonts"));
  346. String link_color_text = title_color.to_html(false);
  347. class_desc->pop();
  348. class_desc->pop();
  349. class_desc->add_newline();
  350. class_desc->add_newline();
  351. for (int pass = 0; pass < 2; pass++) {
  352. Vector<DocData::MethodDoc> methods_filtered;
  353. for (int i = 0; i < p_methods.size(); i++) {
  354. const String &q = p_methods[i].qualifiers;
  355. if ((pass == 0 && q.find("virtual") != -1) || (pass == 1 && q.find("virtual") == -1)) {
  356. methods_filtered.push_back(p_methods[i]);
  357. }
  358. }
  359. for (int i = 0; i < methods_filtered.size(); i++) {
  360. class_desc->push_font(doc_code_font);
  361. _add_method(methods_filtered[i], false);
  362. class_desc->pop();
  363. class_desc->add_newline();
  364. class_desc->add_newline();
  365. class_desc->push_color(text_color);
  366. class_desc->push_font(doc_font);
  367. class_desc->push_indent(1);
  368. if (methods_filtered[i].errors_returned.size()) {
  369. class_desc->append_text(TTR("Error codes returned:"));
  370. class_desc->add_newline();
  371. class_desc->push_list(0, RichTextLabel::LIST_DOTS, false);
  372. for (int j = 0; j < methods_filtered[i].errors_returned.size(); j++) {
  373. if (j > 0) {
  374. class_desc->add_newline();
  375. }
  376. int val = methods_filtered[i].errors_returned[j];
  377. String text = itos(val);
  378. for (int k = 0; k < CoreConstants::get_global_constant_count(); k++) {
  379. if (CoreConstants::get_global_constant_value(k) == val && CoreConstants::get_global_constant_enum(k) == SNAME("Error")) {
  380. text = CoreConstants::get_global_constant_name(k);
  381. break;
  382. }
  383. }
  384. class_desc->push_bold();
  385. class_desc->append_text(text);
  386. class_desc->pop();
  387. }
  388. class_desc->pop();
  389. class_desc->add_newline();
  390. class_desc->add_newline();
  391. }
  392. if (!methods_filtered[i].description.strip_edges().is_empty()) {
  393. _add_text(DTR(methods_filtered[i].description));
  394. } else {
  395. class_desc->add_image(get_theme_icon(SNAME("Error"), SNAME("EditorIcons")));
  396. class_desc->add_text(" ");
  397. class_desc->push_color(comment_color);
  398. if (p_classdoc.is_script_doc) {
  399. class_desc->append_text(TTR("There is currently no description for this " + p_method_type + "."));
  400. } else {
  401. class_desc->append_text(TTR("There is currently no description for this " + p_method_type + ". Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
  402. }
  403. class_desc->pop();
  404. }
  405. class_desc->pop();
  406. class_desc->pop();
  407. class_desc->pop();
  408. class_desc->add_newline();
  409. class_desc->add_newline();
  410. class_desc->add_newline();
  411. }
  412. }
  413. }
  414. void EditorHelp::_update_doc() {
  415. if (!doc->class_list.has(edited_class)) {
  416. return;
  417. }
  418. scroll_locked = true;
  419. class_desc->clear();
  420. method_line.clear();
  421. section_line.clear();
  422. _init_colors();
  423. DocData::ClassDoc cd = doc->class_list[edited_class]; //make a copy, so we can sort without worrying
  424. Ref<Font> doc_font = get_theme_font(SNAME("doc"), SNAME("EditorFonts"));
  425. Ref<Font> doc_bold_font = get_theme_font(SNAME("doc_bold"), SNAME("EditorFonts"));
  426. Ref<Font> doc_title_font = get_theme_font(SNAME("doc_title"), SNAME("EditorFonts"));
  427. Ref<Font> doc_code_font = get_theme_font(SNAME("doc_source"), SNAME("EditorFonts"));
  428. String link_color_text = title_color.to_html(false);
  429. // Class name
  430. section_line.push_back(Pair<String, int>(TTR("Top"), 0));
  431. class_desc->push_font(doc_title_font);
  432. class_desc->push_color(title_color);
  433. class_desc->add_text(TTR("Class:") + " ");
  434. class_desc->push_color(headline_color);
  435. _add_text(edited_class);
  436. class_desc->pop();
  437. class_desc->pop();
  438. class_desc->pop();
  439. class_desc->add_newline();
  440. // Inheritance tree
  441. // Ascendents
  442. if (cd.inherits != "") {
  443. class_desc->push_color(title_color);
  444. class_desc->push_font(doc_font);
  445. class_desc->add_text(TTR("Inherits:") + " ");
  446. String inherits = cd.inherits;
  447. while (inherits != "") {
  448. _add_type(inherits);
  449. inherits = doc->class_list[inherits].inherits;
  450. if (inherits != "") {
  451. class_desc->add_text(" < ");
  452. }
  453. }
  454. class_desc->pop();
  455. class_desc->pop();
  456. class_desc->add_newline();
  457. }
  458. // Descendents
  459. if (cd.is_script_doc || ClassDB::class_exists(cd.name)) {
  460. bool found = false;
  461. bool prev = false;
  462. class_desc->push_font(doc_font);
  463. for (const KeyValue<String, DocData::ClassDoc> &E : doc->class_list) {
  464. if (E.value.inherits == cd.name) {
  465. if (!found) {
  466. class_desc->push_color(title_color);
  467. class_desc->add_text(TTR("Inherited by:") + " ");
  468. found = true;
  469. }
  470. if (prev) {
  471. class_desc->add_text(" , ");
  472. }
  473. _add_type(E.value.name);
  474. prev = true;
  475. }
  476. }
  477. class_desc->pop();
  478. if (found) {
  479. class_desc->pop();
  480. class_desc->add_newline();
  481. }
  482. }
  483. class_desc->add_newline();
  484. class_desc->add_newline();
  485. // Brief description
  486. if (cd.brief_description != "") {
  487. class_desc->push_color(text_color);
  488. class_desc->push_font(doc_bold_font);
  489. class_desc->push_indent(1);
  490. _add_text(DTR(cd.brief_description));
  491. class_desc->pop();
  492. class_desc->pop();
  493. class_desc->pop();
  494. class_desc->add_newline();
  495. class_desc->add_newline();
  496. class_desc->add_newline();
  497. }
  498. // Class description
  499. if (cd.description != "") {
  500. section_line.push_back(Pair<String, int>(TTR("Description"), class_desc->get_line_count() - 2));
  501. description_line = class_desc->get_line_count() - 2;
  502. class_desc->push_color(title_color);
  503. class_desc->push_font(doc_title_font);
  504. class_desc->add_text(TTR("Description"));
  505. class_desc->pop();
  506. class_desc->pop();
  507. class_desc->add_newline();
  508. class_desc->add_newline();
  509. class_desc->push_color(text_color);
  510. class_desc->push_font(doc_font);
  511. class_desc->push_indent(1);
  512. _add_text(DTR(cd.description));
  513. class_desc->pop();
  514. class_desc->pop();
  515. class_desc->pop();
  516. class_desc->add_newline();
  517. class_desc->add_newline();
  518. class_desc->add_newline();
  519. }
  520. // Online tutorials
  521. if (cd.tutorials.size()) {
  522. class_desc->push_color(title_color);
  523. class_desc->push_font(doc_title_font);
  524. class_desc->add_text(TTR("Online Tutorials"));
  525. class_desc->pop();
  526. class_desc->pop();
  527. class_desc->push_indent(1);
  528. class_desc->push_font(doc_code_font);
  529. class_desc->add_newline();
  530. for (int i = 0; i < cd.tutorials.size(); i++) {
  531. const String link = DTR(cd.tutorials[i].link);
  532. String linktxt = (cd.tutorials[i].title.is_empty()) ? link : DTR(cd.tutorials[i].title);
  533. const int seppos = linktxt.find("//");
  534. if (seppos != -1) {
  535. linktxt = link.substr(seppos + 2);
  536. }
  537. class_desc->push_color(symbol_color);
  538. class_desc->append_text("[url=" + link + "]" + linktxt + "[/url]");
  539. class_desc->pop();
  540. class_desc->add_newline();
  541. }
  542. class_desc->pop();
  543. class_desc->pop();
  544. class_desc->add_newline();
  545. class_desc->add_newline();
  546. }
  547. // Properties overview
  548. Set<String> skip_methods;
  549. bool property_descr = false;
  550. bool has_properties = cd.properties.size() != 0;
  551. if (cd.is_script_doc) {
  552. has_properties = false;
  553. for (int i = 0; i < cd.properties.size(); i++) {
  554. if (cd.properties[i].name.begins_with("_") && cd.properties[i].description.is_empty()) {
  555. continue;
  556. }
  557. has_properties = true;
  558. break;
  559. }
  560. }
  561. if (has_properties) {
  562. section_line.push_back(Pair<String, int>(TTR("Properties"), class_desc->get_line_count() - 2));
  563. class_desc->push_color(title_color);
  564. class_desc->push_font(doc_title_font);
  565. class_desc->add_text(TTR("Properties"));
  566. class_desc->pop();
  567. class_desc->pop();
  568. class_desc->add_newline();
  569. class_desc->push_font(doc_code_font);
  570. class_desc->push_indent(1);
  571. class_desc->push_table(2);
  572. class_desc->set_table_column_expand(1, true);
  573. for (int i = 0; i < cd.properties.size(); i++) {
  574. // Ignore undocumented private.
  575. if (cd.properties[i].name.begins_with("_") && cd.properties[i].description.is_empty()) {
  576. continue;
  577. }
  578. property_line[cd.properties[i].name] = class_desc->get_line_count() - 2; //gets overridden if description
  579. class_desc->push_cell();
  580. class_desc->push_paragraph(RichTextLabel::ALIGN_RIGHT, Control::TEXT_DIRECTION_AUTO, "");
  581. class_desc->push_font(doc_code_font);
  582. _add_type(cd.properties[i].type, cd.properties[i].enumeration);
  583. class_desc->pop();
  584. class_desc->pop();
  585. class_desc->pop();
  586. bool describe = false;
  587. if (cd.properties[i].setter != "") {
  588. skip_methods.insert(cd.properties[i].setter);
  589. describe = true;
  590. }
  591. if (cd.properties[i].getter != "") {
  592. skip_methods.insert(cd.properties[i].getter);
  593. describe = true;
  594. }
  595. if (cd.properties[i].description != "") {
  596. describe = true;
  597. }
  598. if (cd.properties[i].overridden) {
  599. describe = false;
  600. }
  601. class_desc->push_cell();
  602. class_desc->push_font(doc_code_font);
  603. class_desc->push_color(headline_color);
  604. if (describe) {
  605. class_desc->push_meta("@member " + cd.properties[i].name);
  606. }
  607. _add_text(cd.properties[i].name);
  608. if (describe) {
  609. class_desc->pop();
  610. property_descr = true;
  611. }
  612. if (cd.properties[i].default_value != "") {
  613. class_desc->push_color(symbol_color);
  614. class_desc->add_text(cd.properties[i].overridden ? " [" + TTR("override:") + " " : " [" + TTR("default:") + " ");
  615. class_desc->pop();
  616. class_desc->push_color(value_color);
  617. _add_text(_fix_constant(cd.properties[i].default_value));
  618. class_desc->pop();
  619. class_desc->push_color(symbol_color);
  620. class_desc->add_text("]");
  621. class_desc->pop();
  622. }
  623. if (cd.is_script_doc && (cd.properties[i].setter != "" || cd.properties[i].getter != "")) {
  624. class_desc->push_color(symbol_color);
  625. class_desc->add_text(" [" + TTR("property:") + " ");
  626. class_desc->pop(); // color
  627. if (cd.properties[i].setter != "") {
  628. class_desc->push_color(value_color);
  629. class_desc->add_text("setter");
  630. class_desc->pop(); // color
  631. }
  632. if (cd.properties[i].getter != "") {
  633. if (cd.properties[i].setter != "") {
  634. class_desc->push_color(symbol_color);
  635. class_desc->add_text(", ");
  636. class_desc->pop(); // color
  637. }
  638. class_desc->push_color(value_color);
  639. class_desc->add_text("getter");
  640. class_desc->pop(); // color
  641. }
  642. class_desc->push_color(symbol_color);
  643. class_desc->add_text("]");
  644. class_desc->pop(); // color
  645. }
  646. class_desc->pop();
  647. class_desc->pop();
  648. class_desc->pop();
  649. }
  650. class_desc->pop(); //table
  651. class_desc->pop();
  652. class_desc->pop(); // font
  653. class_desc->add_newline();
  654. class_desc->add_newline();
  655. }
  656. // Methods overview
  657. bool constructor_descriptions = false;
  658. bool method_descriptions = false;
  659. bool operator_descriptions = false;
  660. bool sort_methods = EditorSettings::get_singleton()->get("text_editor/help/sort_functions_alphabetically");
  661. Vector<DocData::MethodDoc> methods;
  662. for (int i = 0; i < cd.methods.size(); i++) {
  663. if (skip_methods.has(cd.methods[i].name)) {
  664. if (cd.methods[i].arguments.size() == 0 /* getter */ || (cd.methods[i].arguments.size() == 1 && cd.methods[i].return_type == "void" /* setter */)) {
  665. continue;
  666. }
  667. }
  668. // Ignore undocumented non virtual private.
  669. if (cd.methods[i].name.begins_with("_") && cd.methods[i].description.is_empty() && cd.methods[i].qualifiers.find("virtual") == -1) {
  670. continue;
  671. }
  672. methods.push_back(cd.methods[i]);
  673. }
  674. if (!cd.constructors.is_empty()) {
  675. if (sort_methods) {
  676. cd.constructors.sort();
  677. }
  678. section_line.push_back(Pair<String, int>(TTR("Constructors"), class_desc->get_line_count() - 2));
  679. class_desc->push_color(title_color);
  680. class_desc->push_font(doc_title_font);
  681. class_desc->add_text(TTR("Constructors"));
  682. _update_method_list(cd.constructors, constructor_descriptions);
  683. }
  684. if (!methods.is_empty()) {
  685. if (sort_methods) {
  686. methods.sort();
  687. }
  688. section_line.push_back(Pair<String, int>(TTR("Methods"), class_desc->get_line_count() - 2));
  689. class_desc->push_color(title_color);
  690. class_desc->push_font(doc_title_font);
  691. class_desc->add_text(TTR("Methods"));
  692. _update_method_list(methods, method_descriptions);
  693. }
  694. if (!cd.operators.is_empty()) {
  695. if (sort_methods) {
  696. cd.operators.sort();
  697. }
  698. section_line.push_back(Pair<String, int>(TTR("Operators"), class_desc->get_line_count() - 2));
  699. class_desc->push_color(title_color);
  700. class_desc->push_font(doc_title_font);
  701. class_desc->add_text(TTR("Operators"));
  702. _update_method_list(cd.operators, operator_descriptions);
  703. }
  704. // Theme properties
  705. if (!cd.theme_properties.is_empty()) {
  706. section_line.push_back(Pair<String, int>(TTR("Theme Properties"), class_desc->get_line_count() - 2));
  707. class_desc->push_color(title_color);
  708. class_desc->push_font(doc_title_font);
  709. class_desc->add_text(TTR("Theme Properties"));
  710. class_desc->pop();
  711. class_desc->pop();
  712. class_desc->push_indent(1);
  713. class_desc->push_table(2);
  714. class_desc->set_table_column_expand(1, true);
  715. for (int i = 0; i < cd.theme_properties.size(); i++) {
  716. theme_property_line[cd.theme_properties[i].name] = class_desc->get_line_count() - 2; //gets overridden if description
  717. class_desc->push_cell();
  718. class_desc->push_paragraph(RichTextLabel::ALIGN_RIGHT, Control::TEXT_DIRECTION_AUTO, "");
  719. class_desc->push_font(doc_code_font);
  720. _add_type(cd.theme_properties[i].type);
  721. class_desc->pop();
  722. class_desc->pop();
  723. class_desc->pop();
  724. class_desc->push_cell();
  725. class_desc->push_font(doc_code_font);
  726. class_desc->push_color(headline_color);
  727. _add_text(cd.theme_properties[i].name);
  728. class_desc->pop();
  729. if (cd.theme_properties[i].default_value != "") {
  730. class_desc->push_color(symbol_color);
  731. class_desc->add_text(" [" + TTR("default:") + " ");
  732. class_desc->pop();
  733. class_desc->push_color(value_color);
  734. _add_text(_fix_constant(cd.theme_properties[i].default_value));
  735. class_desc->pop();
  736. class_desc->push_color(symbol_color);
  737. class_desc->add_text("]");
  738. class_desc->pop();
  739. }
  740. class_desc->pop();
  741. if (cd.theme_properties[i].description != "") {
  742. class_desc->push_font(doc_font);
  743. class_desc->push_color(comment_color);
  744. class_desc->add_text(U" – ");
  745. _add_text(DTR(cd.theme_properties[i].description));
  746. class_desc->pop();
  747. class_desc->pop();
  748. }
  749. class_desc->pop(); // cell
  750. }
  751. class_desc->pop(); // table
  752. class_desc->pop();
  753. class_desc->add_newline();
  754. class_desc->add_newline();
  755. }
  756. // Signals
  757. if (!cd.signals.is_empty()) {
  758. if (sort_methods) {
  759. cd.signals.sort();
  760. }
  761. section_line.push_back(Pair<String, int>(TTR("Signals"), class_desc->get_line_count() - 2));
  762. class_desc->push_color(title_color);
  763. class_desc->push_font(doc_title_font);
  764. class_desc->add_text(TTR("Signals"));
  765. class_desc->pop();
  766. class_desc->pop();
  767. class_desc->add_newline();
  768. class_desc->add_newline();
  769. class_desc->push_indent(1);
  770. for (int i = 0; i < cd.signals.size(); i++) {
  771. signal_line[cd.signals[i].name] = class_desc->get_line_count() - 2; //gets overridden if description
  772. class_desc->push_font(doc_code_font); // monofont
  773. class_desc->push_color(headline_color);
  774. static const char32_t prefix[3] = { 0x25CF /* filled circle */, ' ', 0 };
  775. class_desc->add_text(String(prefix));
  776. _add_text(cd.signals[i].name);
  777. class_desc->pop();
  778. class_desc->push_color(symbol_color);
  779. class_desc->add_text("(");
  780. class_desc->pop();
  781. for (int j = 0; j < cd.signals[i].arguments.size(); j++) {
  782. class_desc->push_color(text_color);
  783. if (j > 0) {
  784. class_desc->add_text(", ");
  785. }
  786. _add_text(cd.signals[i].arguments[j].name);
  787. class_desc->add_text(": ");
  788. _add_type(cd.signals[i].arguments[j].type);
  789. if (cd.signals[i].arguments[j].default_value != "") {
  790. class_desc->push_color(symbol_color);
  791. class_desc->add_text(" = ");
  792. class_desc->pop();
  793. _add_text(cd.signals[i].arguments[j].default_value);
  794. }
  795. class_desc->pop();
  796. }
  797. class_desc->push_color(symbol_color);
  798. class_desc->add_text(")");
  799. class_desc->pop();
  800. class_desc->pop(); // end monofont
  801. if (cd.signals[i].description != "") {
  802. class_desc->push_font(doc_font);
  803. class_desc->push_color(comment_color);
  804. class_desc->push_indent(1);
  805. _add_text(DTR(cd.signals[i].description));
  806. class_desc->pop(); // indent
  807. class_desc->pop();
  808. class_desc->pop(); // font
  809. }
  810. class_desc->add_newline();
  811. class_desc->add_newline();
  812. }
  813. class_desc->pop();
  814. class_desc->add_newline();
  815. }
  816. // Constants and enums
  817. if (!cd.constants.is_empty()) {
  818. Map<String, Vector<DocData::ConstantDoc>> enums;
  819. Vector<DocData::ConstantDoc> constants;
  820. for (int i = 0; i < cd.constants.size(); i++) {
  821. if (!cd.constants[i].enumeration.is_empty()) {
  822. if (!enums.has(cd.constants[i].enumeration)) {
  823. enums[cd.constants[i].enumeration] = Vector<DocData::ConstantDoc>();
  824. }
  825. enums[cd.constants[i].enumeration].push_back(cd.constants[i]);
  826. } else {
  827. // Ignore undocumented private.
  828. if (cd.constants[i].name.begins_with("_") && cd.constants[i].description.is_empty()) {
  829. continue;
  830. }
  831. constants.push_back(cd.constants[i]);
  832. }
  833. }
  834. // Enums
  835. if (enums.size()) {
  836. section_line.push_back(Pair<String, int>(TTR("Enumerations"), class_desc->get_line_count() - 2));
  837. class_desc->push_color(title_color);
  838. class_desc->push_font(doc_title_font);
  839. class_desc->add_text(TTR("Enumerations"));
  840. class_desc->pop();
  841. class_desc->pop();
  842. class_desc->push_indent(1);
  843. class_desc->add_newline();
  844. for (KeyValue<String, Vector<DocData::ConstantDoc>> &E : enums) {
  845. enum_line[E.key] = class_desc->get_line_count() - 2;
  846. class_desc->push_font(doc_code_font);
  847. class_desc->push_color(title_color);
  848. class_desc->add_text("enum ");
  849. class_desc->pop();
  850. String e = E.key;
  851. if ((e.get_slice_count(".") > 1) && (e.get_slice(".", 0) == edited_class)) {
  852. e = e.get_slice(".", 1);
  853. }
  854. class_desc->push_color(headline_color);
  855. class_desc->add_text(e);
  856. class_desc->pop();
  857. class_desc->pop();
  858. class_desc->push_color(symbol_color);
  859. class_desc->add_text(":");
  860. class_desc->pop();
  861. class_desc->add_newline();
  862. class_desc->add_newline();
  863. // Enum description.
  864. if (e != "@unnamed_enums" && cd.enums.has(e)) {
  865. class_desc->push_color(text_color);
  866. class_desc->push_font(doc_font);
  867. class_desc->push_indent(1);
  868. _add_text(cd.enums[e]);
  869. class_desc->pop();
  870. class_desc->pop();
  871. class_desc->pop();
  872. class_desc->add_newline();
  873. class_desc->add_newline();
  874. }
  875. class_desc->push_indent(1);
  876. Vector<DocData::ConstantDoc> enum_list = E.value;
  877. Map<String, int> enumValuesContainer;
  878. int enumStartingLine = enum_line[E.key];
  879. for (int i = 0; i < enum_list.size(); i++) {
  880. if (cd.name == "@GlobalScope") {
  881. enumValuesContainer[enum_list[i].name] = enumStartingLine;
  882. }
  883. // Add the enum constant line to the constant_line map so we can locate it as a constant
  884. constant_line[enum_list[i].name] = class_desc->get_line_count() - 2;
  885. class_desc->push_font(doc_code_font);
  886. class_desc->push_color(headline_color);
  887. static const char32_t prefix[3] = { 0x25CF /* filled circle */, ' ', 0 };
  888. class_desc->add_text(String(prefix));
  889. _add_text(enum_list[i].name);
  890. class_desc->pop();
  891. class_desc->push_color(symbol_color);
  892. class_desc->add_text(" = ");
  893. class_desc->pop();
  894. class_desc->push_color(value_color);
  895. _add_text(_fix_constant(enum_list[i].value));
  896. class_desc->pop();
  897. class_desc->pop();
  898. if (enum_list[i].description != "") {
  899. class_desc->push_font(doc_font);
  900. class_desc->push_color(comment_color);
  901. class_desc->add_text(U" – ");
  902. _add_text(DTR(enum_list[i].description));
  903. class_desc->pop();
  904. class_desc->pop();
  905. if (DTR(enum_list[i].description).find("\n") > 0) {
  906. class_desc->add_newline();
  907. }
  908. }
  909. class_desc->add_newline();
  910. }
  911. if (cd.name == "@GlobalScope") {
  912. enum_values_line[E.key] = enumValuesContainer;
  913. }
  914. class_desc->pop();
  915. class_desc->add_newline();
  916. }
  917. class_desc->pop();
  918. class_desc->add_newline();
  919. }
  920. // Constants
  921. if (constants.size()) {
  922. section_line.push_back(Pair<String, int>(TTR("Constants"), class_desc->get_line_count() - 2));
  923. class_desc->push_color(title_color);
  924. class_desc->push_font(doc_title_font);
  925. class_desc->add_text(TTR("Constants"));
  926. class_desc->pop();
  927. class_desc->pop();
  928. class_desc->push_indent(1);
  929. class_desc->add_newline();
  930. for (int i = 0; i < constants.size(); i++) {
  931. constant_line[constants[i].name] = class_desc->get_line_count() - 2;
  932. class_desc->push_font(doc_code_font);
  933. if (constants[i].value.begins_with("Color(") && constants[i].value.ends_with(")")) {
  934. String stripped = constants[i].value.replace(" ", "").replace("Color(", "").replace(")", "");
  935. Vector<float> color = stripped.split_floats(",");
  936. if (color.size() >= 3) {
  937. class_desc->push_color(Color(color[0], color[1], color[2]));
  938. static const char32_t prefix[3] = { 0x25CF /* filled circle */, ' ', 0 };
  939. class_desc->add_text(String(prefix));
  940. class_desc->pop();
  941. }
  942. } else {
  943. static const char32_t prefix[3] = { 0x25CF /* filled circle */, ' ', 0 };
  944. class_desc->add_text(String(prefix));
  945. }
  946. class_desc->push_color(headline_color);
  947. _add_text(constants[i].name);
  948. class_desc->pop();
  949. class_desc->push_color(symbol_color);
  950. class_desc->add_text(" = ");
  951. class_desc->pop();
  952. class_desc->push_color(value_color);
  953. _add_text(_fix_constant(constants[i].value));
  954. class_desc->pop();
  955. class_desc->pop();
  956. if (constants[i].description != "") {
  957. class_desc->push_font(doc_font);
  958. class_desc->push_color(comment_color);
  959. class_desc->add_text(U" – ");
  960. _add_text(DTR(constants[i].description));
  961. class_desc->pop();
  962. class_desc->pop();
  963. if (DTR(constants[i].description).find("\n") > 0) {
  964. class_desc->add_newline();
  965. }
  966. }
  967. class_desc->add_newline();
  968. }
  969. class_desc->pop();
  970. class_desc->add_newline();
  971. }
  972. }
  973. // Property descriptions
  974. if (property_descr) {
  975. section_line.push_back(Pair<String, int>(TTR("Property Descriptions"), class_desc->get_line_count() - 2));
  976. class_desc->push_color(title_color);
  977. class_desc->push_font(doc_title_font);
  978. class_desc->add_text(TTR("Property Descriptions"));
  979. class_desc->pop();
  980. class_desc->pop();
  981. class_desc->add_newline();
  982. class_desc->add_newline();
  983. for (int i = 0; i < cd.properties.size(); i++) {
  984. if (cd.properties[i].overridden) {
  985. continue;
  986. }
  987. property_line[cd.properties[i].name] = class_desc->get_line_count() - 2;
  988. class_desc->push_table(2);
  989. class_desc->set_table_column_expand(1, true);
  990. class_desc->push_cell();
  991. class_desc->push_font(doc_code_font);
  992. static const char32_t prefix[3] = { 0x25CF /* filled circle */, ' ', 0 };
  993. class_desc->add_text(String(prefix));
  994. _add_type(cd.properties[i].type, cd.properties[i].enumeration);
  995. class_desc->add_text(" ");
  996. class_desc->pop(); // font
  997. class_desc->pop(); // cell
  998. class_desc->push_cell();
  999. class_desc->push_font(doc_code_font);
  1000. class_desc->push_color(headline_color);
  1001. _add_text(cd.properties[i].name);
  1002. class_desc->pop(); // color
  1003. if (cd.properties[i].default_value != "") {
  1004. class_desc->push_color(symbol_color);
  1005. class_desc->add_text(" [" + TTR("default:") + " ");
  1006. class_desc->pop(); // color
  1007. class_desc->push_color(value_color);
  1008. _add_text(_fix_constant(cd.properties[i].default_value));
  1009. class_desc->pop(); // color
  1010. class_desc->push_color(symbol_color);
  1011. class_desc->add_text("]");
  1012. class_desc->pop(); // color
  1013. }
  1014. if (cd.is_script_doc && (cd.properties[i].setter != "" || cd.properties[i].getter != "")) {
  1015. class_desc->push_color(symbol_color);
  1016. class_desc->add_text(" [" + TTR("property:") + " ");
  1017. class_desc->pop(); // color
  1018. if (cd.properties[i].setter != "") {
  1019. class_desc->push_color(value_color);
  1020. class_desc->add_text("setter");
  1021. class_desc->pop(); // color
  1022. }
  1023. if (cd.properties[i].getter != "") {
  1024. if (cd.properties[i].setter != "") {
  1025. class_desc->push_color(symbol_color);
  1026. class_desc->add_text(", ");
  1027. class_desc->pop(); // color
  1028. }
  1029. class_desc->push_color(value_color);
  1030. class_desc->add_text("getter");
  1031. class_desc->pop(); // color
  1032. }
  1033. class_desc->push_color(symbol_color);
  1034. class_desc->add_text("]");
  1035. class_desc->pop(); // color
  1036. }
  1037. class_desc->pop(); // font
  1038. class_desc->pop(); // cell
  1039. // Script doc doesn't have setter, getter.
  1040. if (!cd.is_script_doc) {
  1041. Map<String, DocData::MethodDoc> method_map;
  1042. for (int j = 0; j < methods.size(); j++) {
  1043. method_map[methods[j].name] = methods[j];
  1044. }
  1045. if (cd.properties[i].setter != "") {
  1046. class_desc->push_cell();
  1047. class_desc->pop(); // cell
  1048. class_desc->push_cell();
  1049. class_desc->push_font(doc_code_font);
  1050. class_desc->push_color(text_color);
  1051. if (method_map[cd.properties[i].setter].arguments.size() > 1) {
  1052. // Setters with additional arguments are exposed in the method list, so we link them here for quick access.
  1053. class_desc->push_meta("@method " + cd.properties[i].setter);
  1054. class_desc->add_text(cd.properties[i].setter + TTR("(value)"));
  1055. class_desc->pop();
  1056. } else {
  1057. class_desc->add_text(cd.properties[i].setter + TTR("(value)"));
  1058. }
  1059. class_desc->pop(); // color
  1060. class_desc->push_color(comment_color);
  1061. class_desc->add_text(" setter");
  1062. class_desc->pop(); // color
  1063. class_desc->pop(); // font
  1064. class_desc->pop(); // cell
  1065. method_line[cd.properties[i].setter] = property_line[cd.properties[i].name];
  1066. }
  1067. if (cd.properties[i].getter != "") {
  1068. class_desc->push_cell();
  1069. class_desc->pop(); // cell
  1070. class_desc->push_cell();
  1071. class_desc->push_font(doc_code_font);
  1072. class_desc->push_color(text_color);
  1073. if (method_map[cd.properties[i].getter].arguments.size() > 0) {
  1074. // Getters with additional arguments are exposed in the method list, so we link them here for quick access.
  1075. class_desc->push_meta("@method " + cd.properties[i].getter);
  1076. class_desc->add_text(cd.properties[i].getter + "()");
  1077. class_desc->pop();
  1078. } else {
  1079. class_desc->add_text(cd.properties[i].getter + "()");
  1080. }
  1081. class_desc->pop(); //color
  1082. class_desc->push_color(comment_color);
  1083. class_desc->add_text(" getter");
  1084. class_desc->pop(); //color
  1085. class_desc->pop(); //font
  1086. class_desc->pop(); //cell
  1087. method_line[cd.properties[i].getter] = property_line[cd.properties[i].name];
  1088. }
  1089. }
  1090. class_desc->pop(); // table
  1091. class_desc->add_newline();
  1092. class_desc->add_newline();
  1093. class_desc->push_color(text_color);
  1094. class_desc->push_font(doc_font);
  1095. class_desc->push_indent(1);
  1096. if (!cd.properties[i].description.strip_edges().is_empty()) {
  1097. _add_text(DTR(cd.properties[i].description));
  1098. } else {
  1099. class_desc->add_image(get_theme_icon(SNAME("Error"), SNAME("EditorIcons")));
  1100. class_desc->add_text(" ");
  1101. class_desc->push_color(comment_color);
  1102. if (cd.is_script_doc) {
  1103. class_desc->append_text(TTR("There is currently no description for this property."));
  1104. } else {
  1105. 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));
  1106. }
  1107. class_desc->pop();
  1108. }
  1109. class_desc->pop();
  1110. class_desc->pop();
  1111. class_desc->pop();
  1112. class_desc->add_newline();
  1113. class_desc->add_newline();
  1114. class_desc->add_newline();
  1115. }
  1116. }
  1117. // Constructor descriptions
  1118. if (constructor_descriptions) {
  1119. section_line.push_back(Pair<String, int>(TTR("Constructor Descriptions"), class_desc->get_line_count() - 2));
  1120. class_desc->push_color(title_color);
  1121. class_desc->push_font(doc_title_font);
  1122. class_desc->add_text(TTR("Constructor Descriptions"));
  1123. _update_method_descriptions(cd, cd.constructors, "constructor");
  1124. }
  1125. // Method descriptions
  1126. if (method_descriptions) {
  1127. section_line.push_back(Pair<String, int>(TTR("Method Descriptions"), class_desc->get_line_count() - 2));
  1128. class_desc->push_color(title_color);
  1129. class_desc->push_font(doc_title_font);
  1130. class_desc->add_text(TTR("Method Descriptions"));
  1131. _update_method_descriptions(cd, methods, "method");
  1132. }
  1133. // Operator descriptions
  1134. if (operator_descriptions) {
  1135. section_line.push_back(Pair<String, int>(TTR("Operator Descriptions"), class_desc->get_line_count() - 2));
  1136. class_desc->push_color(title_color);
  1137. class_desc->push_font(doc_title_font);
  1138. class_desc->add_text(TTR("Operator Descriptions"));
  1139. _update_method_descriptions(cd, cd.operators, "operator");
  1140. }
  1141. scroll_locked = false;
  1142. }
  1143. void EditorHelp::_request_help(const String &p_string) {
  1144. Error err = _goto_desc(p_string);
  1145. if (err == OK) {
  1146. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  1147. }
  1148. //100 palabras
  1149. }
  1150. void EditorHelp::_help_callback(const String &p_topic) {
  1151. String what = p_topic.get_slice(":", 0);
  1152. String clss = p_topic.get_slice(":", 1);
  1153. String name;
  1154. if (p_topic.get_slice_count(":") == 3) {
  1155. name = p_topic.get_slice(":", 2);
  1156. }
  1157. _request_help(clss); //first go to class
  1158. int line = 0;
  1159. if (what == "class_desc") {
  1160. line = description_line;
  1161. } else if (what == "class_signal") {
  1162. if (signal_line.has(name)) {
  1163. line = signal_line[name];
  1164. }
  1165. } else if (what == "class_method" || what == "class_method_desc") {
  1166. if (method_line.has(name)) {
  1167. line = method_line[name];
  1168. }
  1169. } else if (what == "class_property") {
  1170. if (property_line.has(name)) {
  1171. line = property_line[name];
  1172. }
  1173. } else if (what == "class_enum") {
  1174. if (enum_line.has(name)) {
  1175. line = enum_line[name];
  1176. }
  1177. } else if (what == "class_theme_item") {
  1178. if (theme_property_line.has(name)) {
  1179. line = theme_property_line[name];
  1180. }
  1181. } else if (what == "class_constant") {
  1182. if (constant_line.has(name)) {
  1183. line = constant_line[name];
  1184. }
  1185. } else if (what == "class_global") {
  1186. if (constant_line.has(name)) {
  1187. line = constant_line[name];
  1188. } else if (method_line.has(name)) {
  1189. line = method_line[name];
  1190. } else {
  1191. Map<String, Map<String, int>>::Element *iter = enum_values_line.front();
  1192. while (true) {
  1193. if (iter->value().has(name)) {
  1194. line = iter->value()[name];
  1195. break;
  1196. } else if (iter == enum_values_line.back()) {
  1197. break;
  1198. } else {
  1199. iter = iter->next();
  1200. }
  1201. }
  1202. }
  1203. }
  1204. class_desc->call_deferred(SNAME("scroll_to_paragraph"), line);
  1205. }
  1206. static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt) {
  1207. DocTools *doc = EditorHelp::get_doc_data();
  1208. String base_path;
  1209. Ref<Font> doc_font = p_rt->get_theme_font(SNAME("doc"), SNAME("EditorFonts"));
  1210. Ref<Font> doc_bold_font = p_rt->get_theme_font(SNAME("doc_bold"), SNAME("EditorFonts"));
  1211. Ref<Font> doc_code_font = p_rt->get_theme_font(SNAME("doc_source"), SNAME("EditorFonts"));
  1212. Ref<Font> doc_kbd_font = p_rt->get_theme_font(SNAME("doc_keyboard"), SNAME("EditorFonts"));
  1213. Color headline_color = p_rt->get_theme_color(SNAME("headline_color"), SNAME("EditorHelp"));
  1214. Color accent_color = p_rt->get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  1215. Color property_color = p_rt->get_theme_color(SNAME("property_color"), SNAME("Editor"));
  1216. Color link_color = accent_color.lerp(headline_color, 0.8);
  1217. Color code_color = accent_color.lerp(headline_color, 0.6);
  1218. Color kbd_color = accent_color.lerp(property_color, 0.6);
  1219. String bbcode = p_bbcode.dedent().replace("\t", "").replace("\r", "").strip_edges();
  1220. // Select the correct code examples
  1221. switch ((int)EDITOR_GET("text_editor/help/class_reference_examples")) {
  1222. case 0: // GDScript
  1223. bbcode = bbcode.replace("[gdscript]", "[codeblock]");
  1224. bbcode = bbcode.replace("[/gdscript]", "[/codeblock]");
  1225. for (int pos = bbcode.find("[csharp]"); pos != -1; pos = bbcode.find("[csharp]")) {
  1226. if (bbcode.find("[/csharp]") == -1) {
  1227. WARN_PRINT("Unclosed [csharp] block or parse fail in code (search for tag errors)");
  1228. break;
  1229. }
  1230. bbcode.erase(pos, bbcode.find("[/csharp]") + 9 - pos);
  1231. while (bbcode[pos] == '\n') {
  1232. bbcode.erase(pos, 1);
  1233. }
  1234. }
  1235. break;
  1236. case 1: // C#
  1237. bbcode = bbcode.replace("[csharp]", "[codeblock]");
  1238. bbcode = bbcode.replace("[/csharp]", "[/codeblock]");
  1239. for (int pos = bbcode.find("[gdscript]"); pos != -1; pos = bbcode.find("[gdscript]")) {
  1240. if (bbcode.find("[/gdscript]") == -1) {
  1241. WARN_PRINT("Unclosed [gdscript] block or parse fail in code (search for tag errors)");
  1242. break;
  1243. }
  1244. bbcode.erase(pos, bbcode.find("[/gdscript]") + 11 - pos);
  1245. while (bbcode[pos] == '\n') {
  1246. bbcode.erase(pos, 1);
  1247. }
  1248. }
  1249. break;
  1250. case 2: // GDScript and C#
  1251. bbcode = bbcode.replace("[csharp]", "[b]C#:[/b]\n[codeblock]");
  1252. bbcode = bbcode.replace("[gdscript]", "[b]GDScript:[/b]\n[codeblock]");
  1253. bbcode = bbcode.replace("[/csharp]", "[/codeblock]");
  1254. bbcode = bbcode.replace("[/gdscript]", "[/codeblock]");
  1255. break;
  1256. }
  1257. // Remove codeblocks (they would be printed otherwise)
  1258. bbcode = bbcode.replace("[codeblocks]\n", "");
  1259. bbcode = bbcode.replace("\n[/codeblocks]", "");
  1260. bbcode = bbcode.replace("[codeblocks]", "");
  1261. bbcode = bbcode.replace("[/codeblocks]", "");
  1262. // remove extra new lines around code blocks
  1263. bbcode = bbcode.replace("[codeblock]\n", "[codeblock]");
  1264. bbcode = bbcode.replace("\n[/codeblock]", "[/codeblock]");
  1265. List<String> tag_stack;
  1266. bool code_tag = false;
  1267. int pos = 0;
  1268. while (pos < bbcode.length()) {
  1269. int brk_pos = bbcode.find("[", pos);
  1270. if (brk_pos < 0) {
  1271. brk_pos = bbcode.length();
  1272. }
  1273. if (brk_pos > pos) {
  1274. String text = bbcode.substr(pos, brk_pos - pos);
  1275. if (!code_tag) {
  1276. text = text.replace("\n", "\n\n");
  1277. }
  1278. p_rt->add_text(text);
  1279. }
  1280. if (brk_pos == bbcode.length()) {
  1281. break; //nothing else to add
  1282. }
  1283. int brk_end = bbcode.find("]", brk_pos + 1);
  1284. if (brk_end == -1) {
  1285. String text = bbcode.substr(brk_pos, bbcode.length() - brk_pos);
  1286. if (!code_tag) {
  1287. text = text.replace("\n", "\n\n");
  1288. }
  1289. p_rt->add_text(text);
  1290. break;
  1291. }
  1292. String tag = bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
  1293. if (tag.begins_with("/")) {
  1294. bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length());
  1295. if (!tag_ok) {
  1296. p_rt->add_text("[");
  1297. pos = brk_pos + 1;
  1298. continue;
  1299. }
  1300. tag_stack.pop_front();
  1301. pos = brk_end + 1;
  1302. if (tag != "/img") {
  1303. p_rt->pop();
  1304. if (code_tag) {
  1305. p_rt->pop();
  1306. }
  1307. }
  1308. code_tag = false;
  1309. } else if (code_tag) {
  1310. p_rt->add_text("[");
  1311. pos = brk_pos + 1;
  1312. } else if (tag.begins_with("method ") || tag.begins_with("member ") || tag.begins_with("signal ") || tag.begins_with("enum ") || tag.begins_with("constant ")) {
  1313. int tag_end = tag.find(" ");
  1314. String link_tag = tag.substr(0, tag_end);
  1315. String link_target = tag.substr(tag_end + 1, tag.length()).lstrip(" ");
  1316. p_rt->push_color(link_color);
  1317. p_rt->push_meta("@" + link_tag + " " + link_target);
  1318. p_rt->add_text(link_target + (tag.begins_with("method ") ? "()" : ""));
  1319. p_rt->pop();
  1320. p_rt->pop();
  1321. pos = brk_end + 1;
  1322. } else if (doc->class_list.has(tag)) {
  1323. p_rt->push_color(link_color);
  1324. p_rt->push_meta("#" + tag);
  1325. p_rt->add_text(tag);
  1326. p_rt->pop();
  1327. p_rt->pop();
  1328. pos = brk_end + 1;
  1329. } else if (tag == "b") {
  1330. //use bold font
  1331. p_rt->push_font(doc_bold_font);
  1332. pos = brk_end + 1;
  1333. tag_stack.push_front(tag);
  1334. } else if (tag == "i") {
  1335. //use italics font
  1336. p_rt->push_color(headline_color);
  1337. pos = brk_end + 1;
  1338. tag_stack.push_front(tag);
  1339. } else if (tag == "code" || tag == "codeblock") {
  1340. //use monospace font
  1341. p_rt->push_font(doc_code_font);
  1342. p_rt->push_color(code_color);
  1343. code_tag = true;
  1344. pos = brk_end + 1;
  1345. tag_stack.push_front(tag);
  1346. } else if (tag == "kbd") {
  1347. //use keyboard font with custom color
  1348. p_rt->push_font(doc_kbd_font);
  1349. p_rt->push_color(kbd_color);
  1350. code_tag = true; // though not strictly a code tag, logic is similar
  1351. pos = brk_end + 1;
  1352. tag_stack.push_front(tag);
  1353. } else if (tag == "center") {
  1354. //align to center
  1355. p_rt->push_paragraph(RichTextLabel::ALIGN_CENTER, Control::TEXT_DIRECTION_AUTO, "");
  1356. pos = brk_end + 1;
  1357. tag_stack.push_front(tag);
  1358. } else if (tag == "br") {
  1359. //force a line break
  1360. p_rt->add_newline();
  1361. pos = brk_end + 1;
  1362. } else if (tag == "u") {
  1363. //use underline
  1364. p_rt->push_underline();
  1365. pos = brk_end + 1;
  1366. tag_stack.push_front(tag);
  1367. } else if (tag == "s") {
  1368. //use strikethrough
  1369. p_rt->push_strikethrough();
  1370. pos = brk_end + 1;
  1371. tag_stack.push_front(tag);
  1372. } else if (tag == "url") {
  1373. int end = bbcode.find("[", brk_end);
  1374. if (end == -1) {
  1375. end = bbcode.length();
  1376. }
  1377. String url = bbcode.substr(brk_end + 1, end - brk_end - 1);
  1378. p_rt->push_meta(url);
  1379. pos = brk_end + 1;
  1380. tag_stack.push_front(tag);
  1381. } else if (tag.begins_with("url=")) {
  1382. String url = tag.substr(4, tag.length());
  1383. p_rt->push_meta(url);
  1384. pos = brk_end + 1;
  1385. tag_stack.push_front("url");
  1386. } else if (tag == "img") {
  1387. int end = bbcode.find("[", brk_end);
  1388. if (end == -1) {
  1389. end = bbcode.length();
  1390. }
  1391. String image = bbcode.substr(brk_end + 1, end - brk_end - 1);
  1392. Ref<Texture2D> texture = ResourceLoader::load(base_path.plus_file(image), "Texture2D");
  1393. if (texture.is_valid()) {
  1394. p_rt->add_image(texture);
  1395. }
  1396. pos = end;
  1397. tag_stack.push_front(tag);
  1398. } else if (tag.begins_with("color=")) {
  1399. String col = tag.substr(6, tag.length());
  1400. Color color = Color::from_string(col, Color());
  1401. p_rt->push_color(color);
  1402. pos = brk_end + 1;
  1403. tag_stack.push_front("color");
  1404. } else if (tag.begins_with("font=")) {
  1405. String fnt = tag.substr(5, tag.length());
  1406. Ref<Font> font = ResourceLoader::load(base_path.plus_file(fnt), "Font");
  1407. if (font.is_valid()) {
  1408. p_rt->push_font(font);
  1409. } else {
  1410. p_rt->push_font(doc_font);
  1411. }
  1412. pos = brk_end + 1;
  1413. tag_stack.push_front("font");
  1414. } else {
  1415. p_rt->add_text("["); //ignore
  1416. pos = brk_pos + 1;
  1417. }
  1418. }
  1419. }
  1420. void EditorHelp::_add_text(const String &p_bbcode) {
  1421. _add_text_to_rt(p_bbcode, class_desc);
  1422. }
  1423. void EditorHelp::generate_doc() {
  1424. doc = memnew(DocTools);
  1425. doc->generate(true);
  1426. DocTools compdoc;
  1427. compdoc.load_compressed(_doc_data_compressed, _doc_data_compressed_size, _doc_data_uncompressed_size);
  1428. doc->merge_from(compdoc); //ensure all is up to date
  1429. }
  1430. void EditorHelp::_toggle_scripts_pressed() {
  1431. ScriptEditor::get_singleton()->toggle_scripts_panel();
  1432. update_toggle_scripts_button();
  1433. }
  1434. void EditorHelp::_notification(int p_what) {
  1435. switch (p_what) {
  1436. case NOTIFICATION_READY:
  1437. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1438. _update_doc();
  1439. } break;
  1440. case NOTIFICATION_THEME_CHANGED: {
  1441. if (is_inside_tree()) {
  1442. _class_desc_resized();
  1443. }
  1444. update_toggle_scripts_button();
  1445. } break;
  1446. case NOTIFICATION_VISIBILITY_CHANGED:
  1447. update_toggle_scripts_button();
  1448. break;
  1449. default:
  1450. break;
  1451. }
  1452. }
  1453. void EditorHelp::go_to_help(const String &p_help) {
  1454. _help_callback(p_help);
  1455. }
  1456. void EditorHelp::go_to_class(const String &p_class, int p_scroll) {
  1457. _goto_desc(p_class, p_scroll);
  1458. }
  1459. void EditorHelp::update_doc() {
  1460. ERR_FAIL_COND(!doc->class_list.has(edited_class));
  1461. ERR_FAIL_COND(!doc->class_list[edited_class].is_script_doc);
  1462. _update_doc();
  1463. }
  1464. Vector<Pair<String, int>> EditorHelp::get_sections() {
  1465. Vector<Pair<String, int>> sections;
  1466. for (int i = 0; i < section_line.size(); i++) {
  1467. sections.push_back(Pair<String, int>(section_line[i].first, i));
  1468. }
  1469. return sections;
  1470. }
  1471. void EditorHelp::scroll_to_section(int p_section_index) {
  1472. int line = section_line[p_section_index].second;
  1473. class_desc->scroll_to_paragraph(line);
  1474. }
  1475. void EditorHelp::popup_search() {
  1476. find_bar->popup_search();
  1477. }
  1478. String EditorHelp::get_class() {
  1479. return edited_class;
  1480. }
  1481. void EditorHelp::search_again(bool p_search_previous) {
  1482. _search(p_search_previous);
  1483. }
  1484. int EditorHelp::get_scroll() const {
  1485. return class_desc->get_v_scroll()->get_value();
  1486. }
  1487. void EditorHelp::set_scroll(int p_scroll) {
  1488. class_desc->get_v_scroll()->set_value(p_scroll);
  1489. }
  1490. void EditorHelp::update_toggle_scripts_button() {
  1491. if (is_layout_rtl()) {
  1492. toggle_scripts_button->set_icon(get_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Forward") : SNAME("Back"), SNAME("EditorIcons")));
  1493. } else {
  1494. toggle_scripts_button->set_icon(get_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Back") : SNAME("Forward"), SNAME("EditorIcons")));
  1495. }
  1496. toggle_scripts_button->set_tooltip(vformat("%s (%s)", TTR("Toggle Scripts Panel"), ED_GET_SHORTCUT("script_editor/toggle_scripts_panel")->get_as_text()));
  1497. }
  1498. void EditorHelp::_bind_methods() {
  1499. ClassDB::bind_method("_class_list_select", &EditorHelp::_class_list_select);
  1500. ClassDB::bind_method("_request_help", &EditorHelp::_request_help);
  1501. ClassDB::bind_method("_search", &EditorHelp::_search);
  1502. ClassDB::bind_method("_help_callback", &EditorHelp::_help_callback);
  1503. ADD_SIGNAL(MethodInfo("go_to_help"));
  1504. }
  1505. EditorHelp::EditorHelp() {
  1506. set_custom_minimum_size(Size2(150 * EDSCALE, 0));
  1507. EDITOR_DEF("text_editor/help/sort_functions_alphabetically", true);
  1508. class_desc = memnew(RichTextLabel);
  1509. add_child(class_desc);
  1510. class_desc->set_v_size_flags(SIZE_EXPAND_FILL);
  1511. class_desc->add_theme_color_override("selection_color", get_theme_color(SNAME("accent_color"), SNAME("Editor")) * Color(1, 1, 1, 0.4));
  1512. class_desc->connect("meta_clicked", callable_mp(this, &EditorHelp::_class_desc_select));
  1513. class_desc->connect("gui_input", callable_mp(this, &EditorHelp::_class_desc_input));
  1514. class_desc->connect("resized", callable_mp(this, &EditorHelp::_class_desc_resized));
  1515. _class_desc_resized();
  1516. // Added second so it opens at the bottom so it won't offset the entire widget.
  1517. find_bar = memnew(FindBar);
  1518. add_child(find_bar);
  1519. find_bar->hide();
  1520. find_bar->set_rich_text_label(class_desc);
  1521. status_bar = memnew(HBoxContainer);
  1522. add_child(status_bar);
  1523. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  1524. status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE));
  1525. toggle_scripts_button = memnew(Button);
  1526. toggle_scripts_button->set_flat(true);
  1527. toggle_scripts_button->connect("pressed", callable_mp(this, &EditorHelp::_toggle_scripts_pressed));
  1528. status_bar->add_child(toggle_scripts_button);
  1529. class_desc->set_selection_enabled(true);
  1530. scroll_locked = false;
  1531. select_locked = false;
  1532. class_desc->hide();
  1533. }
  1534. EditorHelp::~EditorHelp() {
  1535. }
  1536. void EditorHelpBit::_go_to_help(String p_what) {
  1537. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  1538. ScriptEditor::get_singleton()->goto_help(p_what);
  1539. emit_signal(SNAME("request_hide"));
  1540. }
  1541. void EditorHelpBit::_meta_clicked(String p_select) {
  1542. if (p_select.begins_with("$")) { //enum
  1543. String select = p_select.substr(1, p_select.length());
  1544. String class_name;
  1545. if (select.find(".") != -1) {
  1546. class_name = select.get_slice(".", 0);
  1547. } else {
  1548. class_name = "@Global";
  1549. }
  1550. _go_to_help("class_enum:" + class_name + ":" + select);
  1551. return;
  1552. } else if (p_select.begins_with("#")) {
  1553. _go_to_help("class_name:" + p_select.substr(1, p_select.length()));
  1554. return;
  1555. } else if (p_select.begins_with("@")) {
  1556. String m = p_select.substr(1, p_select.length());
  1557. if (m.find(".") != -1) {
  1558. _go_to_help("class_method:" + m.get_slice(".", 0) + ":" + m.get_slice(".", 0)); //must go somewhere else
  1559. }
  1560. }
  1561. }
  1562. void EditorHelpBit::_bind_methods() {
  1563. ClassDB::bind_method(D_METHOD("set_text", "text"), &EditorHelpBit::set_text);
  1564. ADD_SIGNAL(MethodInfo("request_hide"));
  1565. }
  1566. void EditorHelpBit::_notification(int p_what) {
  1567. switch (p_what) {
  1568. case NOTIFICATION_READY: {
  1569. rich_text->clear();
  1570. _add_text_to_rt(text, rich_text);
  1571. } break;
  1572. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1573. rich_text->add_theme_color_override("selection_color", get_theme_color(SNAME("accent_color"), SNAME("Editor")) * Color(1, 1, 1, 0.4));
  1574. } break;
  1575. default:
  1576. break;
  1577. }
  1578. }
  1579. void EditorHelpBit::set_text(const String &p_text) {
  1580. text = p_text;
  1581. rich_text->clear();
  1582. _add_text_to_rt(text, rich_text);
  1583. }
  1584. EditorHelpBit::EditorHelpBit() {
  1585. rich_text = memnew(RichTextLabel);
  1586. add_child(rich_text);
  1587. rich_text->connect("meta_clicked", callable_mp(this, &EditorHelpBit::_meta_clicked));
  1588. rich_text->add_theme_color_override("selection_color", get_theme_color(SNAME("accent_color"), SNAME("Editor")) * Color(1, 1, 1, 0.4));
  1589. rich_text->set_override_selected_font_color(false);
  1590. set_custom_minimum_size(Size2(0, 70 * EDSCALE));
  1591. }
  1592. FindBar::FindBar() {
  1593. search_text = memnew(LineEdit);
  1594. add_child(search_text);
  1595. search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  1596. search_text->set_h_size_flags(SIZE_EXPAND_FILL);
  1597. search_text->connect("text_changed", callable_mp(this, &FindBar::_search_text_changed));
  1598. search_text->connect("text_submitted", callable_mp(this, &FindBar::_search_text_submitted));
  1599. matches_label = memnew(Label);
  1600. add_child(matches_label);
  1601. matches_label->hide();
  1602. find_prev = memnew(Button);
  1603. find_prev->set_flat(true);
  1604. add_child(find_prev);
  1605. find_prev->set_focus_mode(FOCUS_NONE);
  1606. find_prev->connect("pressed", callable_mp(this, &FindBar::search_prev));
  1607. find_next = memnew(Button);
  1608. find_next->set_flat(true);
  1609. add_child(find_next);
  1610. find_next->set_focus_mode(FOCUS_NONE);
  1611. find_next->connect("pressed", callable_mp(this, &FindBar::search_next));
  1612. Control *space = memnew(Control);
  1613. add_child(space);
  1614. space->set_custom_minimum_size(Size2(4, 0) * EDSCALE);
  1615. hide_button = memnew(TextureButton);
  1616. add_child(hide_button);
  1617. hide_button->set_focus_mode(FOCUS_NONE);
  1618. hide_button->set_expand(true);
  1619. hide_button->set_stretch_mode(TextureButton::STRETCH_KEEP_CENTERED);
  1620. hide_button->connect("pressed", callable_mp(this, &FindBar::_hide_bar));
  1621. }
  1622. void FindBar::popup_search() {
  1623. show();
  1624. bool grabbed_focus = false;
  1625. if (!search_text->has_focus()) {
  1626. search_text->grab_focus();
  1627. grabbed_focus = true;
  1628. }
  1629. if (!search_text->get_text().is_empty()) {
  1630. search_text->select_all();
  1631. search_text->set_caret_column(search_text->get_text().length());
  1632. if (grabbed_focus) {
  1633. _search();
  1634. }
  1635. }
  1636. }
  1637. void FindBar::_notification(int p_what) {
  1638. switch (p_what) {
  1639. case NOTIFICATION_ENTER_TREE:
  1640. case NOTIFICATION_THEME_CHANGED: {
  1641. find_prev->set_icon(get_theme_icon(SNAME("MoveUp"), SNAME("EditorIcons")));
  1642. find_next->set_icon(get_theme_icon(SNAME("MoveDown"), SNAME("EditorIcons")));
  1643. hide_button->set_normal_texture(get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  1644. hide_button->set_hover_texture(get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  1645. hide_button->set_pressed_texture(get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  1646. hide_button->set_custom_minimum_size(hide_button->get_normal_texture()->get_size());
  1647. 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")));
  1648. } break;
  1649. case NOTIFICATION_VISIBILITY_CHANGED: {
  1650. set_process_unhandled_input(is_visible_in_tree());
  1651. } break;
  1652. }
  1653. }
  1654. void FindBar::_bind_methods() {
  1655. ADD_SIGNAL(MethodInfo("search"));
  1656. }
  1657. void FindBar::set_rich_text_label(RichTextLabel *p_rich_text_label) {
  1658. rich_text_label = p_rich_text_label;
  1659. }
  1660. bool FindBar::search_next() {
  1661. return _search();
  1662. }
  1663. bool FindBar::search_prev() {
  1664. return _search(true);
  1665. }
  1666. bool FindBar::_search(bool p_search_previous) {
  1667. String stext = search_text->get_text();
  1668. bool keep = prev_search == stext;
  1669. bool ret = rich_text_label->search(stext, keep, p_search_previous);
  1670. prev_search = stext;
  1671. if (ret) {
  1672. _update_results_count();
  1673. } else {
  1674. results_count = 0;
  1675. }
  1676. _update_matches_label();
  1677. return ret;
  1678. }
  1679. void FindBar::_update_results_count() {
  1680. results_count = 0;
  1681. String searched = search_text->get_text();
  1682. if (searched.is_empty()) {
  1683. return;
  1684. }
  1685. String full_text = rich_text_label->get_parsed_text();
  1686. int from_pos = 0;
  1687. while (true) {
  1688. int pos = full_text.findn(searched, from_pos);
  1689. if (pos == -1) {
  1690. break;
  1691. }
  1692. results_count++;
  1693. from_pos = pos + searched.length();
  1694. }
  1695. }
  1696. void FindBar::_update_matches_label() {
  1697. if (search_text->get_text().is_empty() || results_count == -1) {
  1698. matches_label->hide();
  1699. } else {
  1700. matches_label->show();
  1701. 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")));
  1702. matches_label->set_text(vformat(results_count == 1 ? TTR("%d match.") : TTR("%d matches."), results_count));
  1703. }
  1704. }
  1705. void FindBar::_hide_bar() {
  1706. if (search_text->has_focus()) {
  1707. rich_text_label->grab_focus();
  1708. }
  1709. hide();
  1710. }
  1711. void FindBar::unhandled_input(const Ref<InputEvent> &p_event) {
  1712. ERR_FAIL_COND(p_event.is_null());
  1713. Ref<InputEventKey> k = p_event;
  1714. if (k.is_valid()) {
  1715. if (k->is_pressed() && (rich_text_label->has_focus() || is_ancestor_of(get_focus_owner()))) {
  1716. bool accepted = true;
  1717. switch (k->get_keycode()) {
  1718. case KEY_ESCAPE: {
  1719. _hide_bar();
  1720. } break;
  1721. default: {
  1722. accepted = false;
  1723. } break;
  1724. }
  1725. if (accepted) {
  1726. accept_event();
  1727. }
  1728. }
  1729. }
  1730. }
  1731. void FindBar::_search_text_changed(const String &p_text) {
  1732. search_next();
  1733. }
  1734. void FindBar::_search_text_submitted(const String &p_text) {
  1735. if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1736. search_prev();
  1737. } else {
  1738. search_next();
  1739. }
  1740. }