editor_help.cpp 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  1. /**************************************************************************/
  2. /* editor_help.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "editor_help.h"
  31. #include "core/os/input.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/version_generated.gen.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 vformat("%s/community/contributing/updating_the_class_reference.html", VERSION_DOCS_URL)
  40. DocData *EditorHelp::doc = nullptr;
  41. void EditorHelp::_init_colors() {
  42. title_color = get_color("accent_color", "Editor");
  43. text_color = get_color("default_color", "RichTextLabel");
  44. headline_color = get_color("headline_color", "EditorHelp");
  45. base_type_color = title_color.linear_interpolate(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_color("accent_color", "Editor").linear_interpolate(text_color, 0.5);
  51. class_desc->add_color_override("selection_color", get_color("accent_color", "Editor") * Color(1, 1, 1, 0.4));
  52. class_desc->add_constant_override("line_separation", Math::round(5 * EDSCALE));
  53. }
  54. void EditorHelp::_unhandled_key_input(const Ref<InputEvent> &p_ev) {
  55. if (!is_visible_in_tree()) {
  56. return;
  57. }
  58. Ref<InputEventKey> k = p_ev;
  59. if (k.is_valid() && k->get_control() && k->get_scancode() == KEY_F) {
  60. search->grab_focus();
  61. search->select_all();
  62. }
  63. }
  64. void EditorHelp::_search(bool p_search_previous) {
  65. if (p_search_previous) {
  66. find_bar->search_prev();
  67. } else {
  68. find_bar->search_next();
  69. }
  70. }
  71. void EditorHelp::_class_list_select(const String &p_select) {
  72. _goto_desc(p_select);
  73. }
  74. void EditorHelp::_class_desc_select(const String &p_select) {
  75. if (p_select.begins_with("$")) { // enum
  76. String select = p_select.substr(1, p_select.length());
  77. String class_name;
  78. if (select.find(".") != -1) {
  79. class_name = select.get_slice(".", 0);
  80. select = select.get_slice(".", 1);
  81. } else {
  82. class_name = "@GlobalScope";
  83. }
  84. emit_signal("go_to_help", "class_enum:" + class_name + ":" + select);
  85. return;
  86. } else if (p_select.begins_with("#")) {
  87. emit_signal("go_to_help", "class_name:" + p_select.substr(1, p_select.length()));
  88. return;
  89. } else if (p_select.begins_with("@")) {
  90. int tag_end = p_select.find(" ");
  91. String tag = p_select.substr(1, tag_end - 1);
  92. String link = p_select.substr(tag_end + 1, p_select.length()).lstrip(" ");
  93. String topic;
  94. Map<String, int> *table = nullptr;
  95. if (tag == "method") {
  96. topic = "class_method";
  97. table = &this->method_line;
  98. } else if (tag == "member") {
  99. topic = "class_property";
  100. table = &this->property_line;
  101. } else if (tag == "enum") {
  102. topic = "class_enum";
  103. table = &this->enum_line;
  104. } else if (tag == "signal") {
  105. topic = "class_signal";
  106. table = &this->signal_line;
  107. } else if (tag == "constant") {
  108. topic = "class_constant";
  109. table = &this->constant_line;
  110. } else {
  111. return;
  112. }
  113. // Case order is important here to correctly handle edge cases like Variant.Type in @GlobalScope.
  114. if (table->has(link)) {
  115. // Found in the current page.
  116. class_desc->scroll_to_line((*table)[link]);
  117. } else {
  118. // Look for link in @GlobalScope
  119. // Note that a link like @GlobalScope.enum_name won't be found in this section, only enum_name will be
  120. if (topic == "class_enum") {
  121. const DocData::ClassDoc &cd = doc->class_list["@GlobalScope"];
  122. for (int i = 0; i < cd.constants.size(); i++) {
  123. if (cd.constants[i].enumeration == link) {
  124. // Found in @GlobalScope.
  125. emit_signal("go_to_help", topic + ":@GlobalScope:" + link);
  126. return;
  127. }
  128. }
  129. } else if (topic == "class_constant") {
  130. const DocData::ClassDoc &cd = doc->class_list["@GlobalScope"];
  131. for (int i = 0; i < cd.constants.size(); i++) {
  132. if (cd.constants[i].name == link) {
  133. // Found in @GlobalScope.
  134. emit_signal("go_to_help", topic + ":@GlobalScope:" + link);
  135. return;
  136. }
  137. }
  138. }
  139. if (link.find(".") != -1) {
  140. // Parse the link as Class.X.
  141. int class_end = link.find(".");
  142. emit_signal("go_to_help", topic + ":" + link.substr(0, class_end) + ":" + link.substr(class_end + 1, link.length()));
  143. }
  144. }
  145. } else if (p_select.begins_with("http")) {
  146. OS::get_singleton()->shell_open(p_select);
  147. }
  148. }
  149. void EditorHelp::_class_desc_input(const Ref<InputEvent> &p_input) {
  150. }
  151. void EditorHelp::_class_desc_resized() {
  152. // Add extra horizontal margins for better readability.
  153. // The margins increase as the width of the editor help container increases.
  154. Ref<Font> doc_code_font = get_font("doc_source", "EditorFonts");
  155. real_t char_width = doc_code_font->get_char_size('x').width;
  156. const int display_margin = MAX(30 * EDSCALE, get_parent_anchorable_rect().size.width - char_width * 120 * EDSCALE) * 0.5;
  157. Ref<StyleBox> class_desc_stylebox = EditorNode::get_singleton()->get_theme_base()->get_stylebox("normal", "RichTextLabel")->duplicate();
  158. class_desc_stylebox->set_default_margin(MARGIN_LEFT, display_margin);
  159. class_desc_stylebox->set_default_margin(MARGIN_RIGHT, display_margin);
  160. class_desc->add_style_override("normal", class_desc_stylebox);
  161. }
  162. void EditorHelp::_add_type(const String &p_type, const String &p_enum) {
  163. String t = p_type;
  164. if (t.empty()) {
  165. t = "void";
  166. }
  167. bool can_ref = (t != "void") || !p_enum.empty();
  168. if (!p_enum.empty()) {
  169. if (p_enum.get_slice_count(".") > 1) {
  170. t = p_enum.get_slice(".", 1);
  171. } else {
  172. t = p_enum.get_slice(".", 0);
  173. }
  174. }
  175. const Color text_color = get_color("default_color", "RichTextLabel");
  176. const Color type_color = get_color("accent_color", "Editor").linear_interpolate(text_color, 0.5);
  177. class_desc->push_color(type_color);
  178. if (can_ref) {
  179. if (p_enum.empty()) {
  180. class_desc->push_meta("#" + t); // class
  181. } else {
  182. class_desc->push_meta("$" + p_enum); // class
  183. }
  184. }
  185. class_desc->add_text(t);
  186. if (can_ref) {
  187. class_desc->pop();
  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_align(RichTextLabel::ALIGN_RIGHT);
  209. } else {
  210. _add_bulletpoint();
  211. }
  212. _add_type(p_method.return_type, p_method.return_enum);
  213. if (p_overview) {
  214. class_desc->pop(); //align
  215. class_desc->pop(); //cell
  216. class_desc->push_cell();
  217. } else {
  218. class_desc->add_text(" ");
  219. }
  220. if (p_overview && p_method.description != "") {
  221. class_desc->push_meta("@method " + p_method.name);
  222. }
  223. class_desc->push_color(headline_color);
  224. _add_text(p_method.name);
  225. class_desc->pop();
  226. if (p_overview && p_method.description != "") {
  227. class_desc->pop(); //meta
  228. }
  229. class_desc->push_color(symbol_color);
  230. class_desc->add_text("(");
  231. class_desc->pop();
  232. for (int j = 0; j < p_method.arguments.size(); j++) {
  233. class_desc->push_color(text_color);
  234. if (j > 0) {
  235. class_desc->add_text(", ");
  236. }
  237. _add_text(p_method.arguments[j].name);
  238. class_desc->add_text(": ");
  239. _add_type(p_method.arguments[j].type, p_method.arguments[j].enumeration);
  240. if (p_method.arguments[j].default_value != "") {
  241. class_desc->push_color(symbol_color);
  242. class_desc->add_text(" = ");
  243. class_desc->pop();
  244. class_desc->push_color(value_color);
  245. _add_text(_fix_constant(p_method.arguments[j].default_value));
  246. class_desc->pop();
  247. }
  248. class_desc->pop();
  249. }
  250. if (is_vararg) {
  251. class_desc->push_color(text_color);
  252. if (p_method.arguments.size()) {
  253. class_desc->add_text(", ");
  254. }
  255. class_desc->push_color(symbol_color);
  256. class_desc->add_text("...");
  257. class_desc->pop();
  258. class_desc->pop();
  259. }
  260. class_desc->push_color(symbol_color);
  261. class_desc->add_text(")");
  262. class_desc->pop();
  263. if (p_method.qualifiers != "") {
  264. class_desc->push_color(qualifier_color);
  265. class_desc->add_text(" ");
  266. _add_text(p_method.qualifiers);
  267. class_desc->pop();
  268. }
  269. if (p_overview) {
  270. class_desc->pop(); //cell
  271. }
  272. }
  273. void EditorHelp::_add_bulletpoint() {
  274. static const CharType prefix[3] = { 0x25CF /* filled circle */, ' ', 0 };
  275. class_desc->add_text(String(prefix));
  276. }
  277. Error EditorHelp::_goto_desc(const String &p_class, int p_vscr) {
  278. if (!doc->class_list.has(p_class)) {
  279. return ERR_DOES_NOT_EXIST;
  280. }
  281. select_locked = true;
  282. class_desc->show();
  283. description_line = 0;
  284. if (p_class == edited_class) {
  285. return OK; // Already there.
  286. }
  287. edited_class = p_class;
  288. _update_doc();
  289. return OK;
  290. }
  291. void EditorHelp::_update_doc() {
  292. if (!doc->class_list.has(edited_class)) {
  293. return;
  294. }
  295. scroll_locked = true;
  296. class_desc->clear();
  297. method_line.clear();
  298. section_line.clear();
  299. _init_colors();
  300. DocData::ClassDoc cd = doc->class_list[edited_class]; // Make a copy, so we can sort without worrying.
  301. Ref<Font> doc_font = get_font("doc", "EditorFonts");
  302. Ref<Font> doc_bold_font = get_font("doc_bold", "EditorFonts");
  303. Ref<Font> doc_title_font = get_font("doc_title", "EditorFonts");
  304. Ref<Font> doc_code_font = get_font("doc_source", "EditorFonts");
  305. String link_color_text = title_color.to_html(false);
  306. // Class name
  307. section_line.push_back(Pair<String, int>(TTR("Top"), 0));
  308. class_desc->push_font(doc_title_font);
  309. class_desc->push_color(title_color);
  310. class_desc->add_text(TTR("Class:") + " ");
  311. class_desc->push_color(headline_color);
  312. _add_text(edited_class);
  313. class_desc->pop();
  314. class_desc->pop();
  315. class_desc->pop();
  316. class_desc->add_newline();
  317. // Inheritance tree
  318. // Ascendents
  319. if (cd.inherits != "") {
  320. class_desc->push_color(title_color);
  321. class_desc->push_font(doc_font);
  322. class_desc->add_text(TTR("Inherits:") + " ");
  323. String inherits = cd.inherits;
  324. while (inherits != "") {
  325. _add_type(inherits);
  326. inherits = doc->class_list[inherits].inherits;
  327. if (inherits != "") {
  328. class_desc->add_text(" < ");
  329. }
  330. }
  331. class_desc->pop();
  332. class_desc->pop();
  333. class_desc->add_newline();
  334. }
  335. // Descendents
  336. if (ClassDB::class_exists(cd.name)) {
  337. bool found = false;
  338. bool prev = false;
  339. class_desc->push_font(doc_font);
  340. for (Map<String, DocData::ClassDoc>::Element *E = doc->class_list.front(); E; E = E->next()) {
  341. if (E->get().inherits == cd.name) {
  342. if (!found) {
  343. class_desc->push_color(title_color);
  344. class_desc->add_text(TTR("Inherited by:") + " ");
  345. found = true;
  346. }
  347. if (prev) {
  348. class_desc->add_text(" , ");
  349. }
  350. _add_type(E->get().name);
  351. prev = true;
  352. }
  353. }
  354. class_desc->pop();
  355. if (found) {
  356. class_desc->pop();
  357. class_desc->add_newline();
  358. }
  359. }
  360. class_desc->add_newline();
  361. class_desc->add_newline();
  362. // Brief description
  363. if (cd.brief_description != "") {
  364. class_desc->push_color(text_color);
  365. class_desc->push_font(doc_bold_font);
  366. class_desc->push_indent(1);
  367. _add_text(DTR(cd.brief_description));
  368. class_desc->pop();
  369. class_desc->pop();
  370. class_desc->pop();
  371. class_desc->add_newline();
  372. class_desc->add_newline();
  373. class_desc->add_newline();
  374. }
  375. // Class description
  376. if (cd.description != "") {
  377. section_line.push_back(Pair<String, int>(TTR("Description"), class_desc->get_line_count() - 2));
  378. description_line = class_desc->get_line_count() - 2;
  379. class_desc->push_color(title_color);
  380. class_desc->push_font(doc_title_font);
  381. class_desc->add_text(TTR("Description"));
  382. class_desc->pop();
  383. class_desc->pop();
  384. class_desc->add_newline();
  385. class_desc->add_newline();
  386. class_desc->push_color(text_color);
  387. class_desc->push_font(doc_font);
  388. class_desc->push_indent(1);
  389. _add_text(DTR(cd.description));
  390. class_desc->pop();
  391. class_desc->pop();
  392. class_desc->pop();
  393. class_desc->add_newline();
  394. class_desc->add_newline();
  395. class_desc->add_newline();
  396. }
  397. // Online tutorials
  398. if (cd.tutorials.size()) {
  399. class_desc->push_color(title_color);
  400. class_desc->push_font(doc_title_font);
  401. class_desc->add_text(TTR("Online Tutorials"));
  402. class_desc->pop();
  403. class_desc->pop();
  404. class_desc->push_indent(1);
  405. class_desc->push_font(doc_code_font);
  406. class_desc->add_newline();
  407. for (int i = 0; i < cd.tutorials.size(); i++) {
  408. const String link = DTR(cd.tutorials[i].link);
  409. String linktxt = (cd.tutorials[i].title.empty()) ? link : DTR(cd.tutorials[i].title);
  410. const int seppos = linktxt.find("//");
  411. if (seppos != -1) {
  412. linktxt = link.right(seppos + 2);
  413. }
  414. class_desc->push_color(symbol_color);
  415. class_desc->append_bbcode("[url=" + link + "]" + linktxt + "[/url]");
  416. class_desc->pop();
  417. class_desc->add_newline();
  418. }
  419. class_desc->pop();
  420. class_desc->pop();
  421. class_desc->add_newline();
  422. class_desc->add_newline();
  423. }
  424. // Properties overview
  425. Set<String> skip_methods;
  426. bool property_descr = false;
  427. if (cd.properties.size()) {
  428. section_line.push_back(Pair<String, int>(TTR("Properties"), class_desc->get_line_count() - 2));
  429. class_desc->push_color(title_color);
  430. class_desc->push_font(doc_title_font);
  431. class_desc->add_text(TTR("Properties"));
  432. class_desc->pop();
  433. class_desc->pop();
  434. class_desc->add_newline();
  435. class_desc->push_font(doc_code_font);
  436. class_desc->push_indent(1);
  437. class_desc->push_table(3);
  438. class_desc->set_table_column_expand(1, true);
  439. for (int i = 0; i < cd.properties.size(); i++) {
  440. property_line[cd.properties[i].name] = class_desc->get_line_count() - 2; // Gets overridden if description.
  441. // Property type.
  442. class_desc->push_cell();
  443. class_desc->push_align(RichTextLabel::ALIGN_RIGHT);
  444. class_desc->push_font(doc_code_font);
  445. _add_type(cd.properties[i].type, cd.properties[i].enumeration);
  446. class_desc->pop();
  447. class_desc->pop();
  448. class_desc->pop(); // Cell
  449. bool describe = false;
  450. if (cd.properties[i].setter != "") {
  451. skip_methods.insert(cd.properties[i].setter);
  452. describe = true;
  453. }
  454. if (cd.properties[i].getter != "") {
  455. skip_methods.insert(cd.properties[i].getter);
  456. describe = true;
  457. }
  458. if (cd.properties[i].description != "") {
  459. describe = true;
  460. }
  461. if (cd.properties[i].overridden) {
  462. describe = false;
  463. }
  464. // Property name.
  465. class_desc->push_cell();
  466. class_desc->push_font(doc_code_font);
  467. class_desc->push_color(headline_color);
  468. if (describe) {
  469. class_desc->push_meta("@member " + cd.properties[i].name);
  470. }
  471. _add_text(cd.properties[i].name);
  472. if (describe) {
  473. class_desc->pop();
  474. property_descr = true;
  475. }
  476. class_desc->pop();
  477. class_desc->pop();
  478. class_desc->pop(); // Cell
  479. // Property value.
  480. class_desc->push_cell();
  481. class_desc->push_font(doc_code_font);
  482. if (cd.properties[i].default_value != "") {
  483. class_desc->push_color(symbol_color);
  484. if (cd.properties[i].overridden) {
  485. class_desc->add_text(" [");
  486. class_desc->push_meta("@member " + cd.properties[i].overrides + "." + cd.properties[i].name);
  487. _add_text(vformat(TTR("overrides %s:"), cd.properties[i].overrides));
  488. class_desc->pop();
  489. class_desc->add_text(" ");
  490. } else {
  491. class_desc->add_text(" [" + TTR("default:") + " ");
  492. }
  493. class_desc->pop();
  494. class_desc->push_color(value_color);
  495. _add_text(_fix_constant(cd.properties[i].default_value));
  496. class_desc->pop();
  497. class_desc->push_color(symbol_color);
  498. class_desc->add_text("]");
  499. class_desc->pop();
  500. }
  501. class_desc->pop();
  502. class_desc->pop(); // Cell
  503. }
  504. class_desc->pop(); // Table
  505. class_desc->pop();
  506. class_desc->pop(); // Font
  507. class_desc->add_newline();
  508. class_desc->add_newline();
  509. }
  510. // Methods overview
  511. bool method_descr = false;
  512. bool sort_methods = EditorSettings::get_singleton()->get("text_editor/help/sort_functions_alphabetically");
  513. Vector<DocData::MethodDoc> methods;
  514. for (int i = 0; i < cd.methods.size(); i++) {
  515. if (skip_methods.has(cd.methods[i].name)) {
  516. if (cd.methods[i].arguments.size() == 0 /* getter */ || (cd.methods[i].arguments.size() == 1 && cd.methods[i].return_type == "void" /* setter */)) {
  517. continue;
  518. }
  519. }
  520. methods.push_back(cd.methods[i]);
  521. }
  522. if (methods.size()) {
  523. if (sort_methods) {
  524. methods.sort();
  525. }
  526. section_line.push_back(Pair<String, int>(TTR("Methods"), class_desc->get_line_count() - 2));
  527. class_desc->push_color(title_color);
  528. class_desc->push_font(doc_title_font);
  529. class_desc->add_text(TTR("Methods"));
  530. class_desc->pop();
  531. class_desc->pop();
  532. class_desc->add_newline();
  533. class_desc->push_font(doc_code_font);
  534. class_desc->push_indent(1);
  535. class_desc->push_table(2);
  536. class_desc->set_table_column_expand(1, true);
  537. bool any_previous = false;
  538. for (int pass = 0; pass < 2; pass++) {
  539. Vector<DocData::MethodDoc> m;
  540. for (int i = 0; i < methods.size(); i++) {
  541. const String &q = methods[i].qualifiers;
  542. if ((pass == 0 && q.find("virtual") != -1) || (pass == 1 && q.find("virtual") == -1)) {
  543. m.push_back(methods[i]);
  544. }
  545. }
  546. if (any_previous && !m.empty()) {
  547. class_desc->push_cell();
  548. class_desc->pop(); // Cell
  549. class_desc->push_cell();
  550. class_desc->pop(); // Cell
  551. }
  552. String group_prefix;
  553. for (int i = 0; i < m.size(); i++) {
  554. const String new_prefix = m[i].name.substr(0, 3);
  555. bool is_new_group = false;
  556. if (i < m.size() - 1 && new_prefix == m[i + 1].name.substr(0, 3) && new_prefix != group_prefix) {
  557. is_new_group = i > 0;
  558. group_prefix = new_prefix;
  559. } else if (group_prefix != "" && new_prefix != group_prefix) {
  560. is_new_group = true;
  561. group_prefix = "";
  562. }
  563. if (is_new_group && pass == 1) {
  564. class_desc->push_cell();
  565. class_desc->pop(); //cell
  566. class_desc->push_cell();
  567. class_desc->pop(); //cell
  568. }
  569. if (m[i].description != "") {
  570. method_descr = true;
  571. }
  572. _add_method(m[i], true);
  573. }
  574. any_previous = !m.empty();
  575. }
  576. class_desc->pop(); //table
  577. class_desc->pop();
  578. class_desc->pop(); // font
  579. class_desc->add_newline();
  580. class_desc->add_newline();
  581. }
  582. // Theme properties
  583. if (cd.theme_properties.size()) {
  584. section_line.push_back(Pair<String, int>(TTR("Theme Properties"), class_desc->get_line_count() - 2));
  585. class_desc->push_color(title_color);
  586. class_desc->push_font(doc_title_font);
  587. class_desc->add_text(TTR("Theme Properties"));
  588. class_desc->pop();
  589. class_desc->pop();
  590. class_desc->add_newline();
  591. class_desc->add_newline();
  592. class_desc->push_indent(1);
  593. String theme_data_type;
  594. Map<String, String> data_type_names;
  595. data_type_names["color"] = TTR("Colors");
  596. data_type_names["constant"] = TTR("Constants");
  597. data_type_names["font"] = TTR("Fonts");
  598. data_type_names["icon"] = TTR("Icons");
  599. data_type_names["style"] = TTR("Styles");
  600. for (int i = 0; i < cd.theme_properties.size(); i++) {
  601. theme_property_line[cd.theme_properties[i].name] = class_desc->get_line_count() - 2; // Gets overridden if description.
  602. if (theme_data_type != cd.theme_properties[i].data_type) {
  603. theme_data_type = cd.theme_properties[i].data_type;
  604. class_desc->push_color(title_color);
  605. class_desc->push_font(doc_bold_font);
  606. if (data_type_names.has(theme_data_type)) {
  607. class_desc->add_text(data_type_names[theme_data_type]);
  608. } else {
  609. class_desc->add_text("");
  610. }
  611. class_desc->pop();
  612. class_desc->pop();
  613. class_desc->add_newline();
  614. class_desc->add_newline();
  615. }
  616. // Theme item header.
  617. class_desc->push_font(doc_code_font);
  618. _add_bulletpoint();
  619. // Theme item object type.
  620. _add_type(cd.theme_properties[i].type);
  621. // Theme item name.
  622. class_desc->push_color(headline_color);
  623. class_desc->add_text(" ");
  624. _add_text(cd.theme_properties[i].name);
  625. class_desc->pop();
  626. // Theme item default value.
  627. if (cd.theme_properties[i].default_value != "") {
  628. class_desc->push_color(symbol_color);
  629. class_desc->add_text(" [" + TTR("default:") + " ");
  630. class_desc->pop();
  631. class_desc->push_color(value_color);
  632. _add_text(_fix_constant(cd.theme_properties[i].default_value));
  633. class_desc->pop();
  634. class_desc->push_color(symbol_color);
  635. class_desc->add_text("]");
  636. class_desc->pop();
  637. }
  638. class_desc->pop(); // monofont
  639. // Theme item description.
  640. if (cd.theme_properties[i].description != "") {
  641. class_desc->push_font(doc_font);
  642. class_desc->push_color(comment_color);
  643. class_desc->push_indent(1);
  644. _add_text(DTR(cd.theme_properties[i].description));
  645. class_desc->pop(); // indent
  646. class_desc->pop(); // color
  647. class_desc->pop(); // font
  648. }
  649. class_desc->add_newline();
  650. class_desc->add_newline();
  651. }
  652. class_desc->pop();
  653. class_desc->add_newline();
  654. }
  655. // Signals
  656. if (cd.signals.size()) {
  657. if (sort_methods) {
  658. cd.signals.sort();
  659. }
  660. section_line.push_back(Pair<String, int>(TTR("Signals"), class_desc->get_line_count() - 2));
  661. class_desc->push_color(title_color);
  662. class_desc->push_font(doc_title_font);
  663. class_desc->add_text(TTR("Signals"));
  664. class_desc->pop();
  665. class_desc->pop();
  666. class_desc->add_newline();
  667. class_desc->add_newline();
  668. class_desc->push_indent(1);
  669. for (int i = 0; i < cd.signals.size(); i++) {
  670. signal_line[cd.signals[i].name] = class_desc->get_line_count() - 2; // Gets overridden if description.
  671. class_desc->push_font(doc_code_font); // monofont
  672. class_desc->push_color(headline_color);
  673. _add_bulletpoint();
  674. _add_text(cd.signals[i].name);
  675. class_desc->pop();
  676. class_desc->push_color(symbol_color);
  677. class_desc->add_text("(");
  678. class_desc->pop();
  679. for (int j = 0; j < cd.signals[i].arguments.size(); j++) {
  680. class_desc->push_color(text_color);
  681. if (j > 0) {
  682. class_desc->add_text(", ");
  683. }
  684. _add_text(cd.signals[i].arguments[j].name);
  685. class_desc->add_text(": ");
  686. _add_type(cd.signals[i].arguments[j].type);
  687. if (cd.signals[i].arguments[j].default_value != "") {
  688. class_desc->push_color(symbol_color);
  689. class_desc->add_text(" = ");
  690. class_desc->pop();
  691. _add_text(cd.signals[i].arguments[j].default_value);
  692. }
  693. class_desc->pop();
  694. }
  695. class_desc->push_color(symbol_color);
  696. class_desc->add_text(")");
  697. class_desc->pop();
  698. class_desc->pop(); // End monofont
  699. if (cd.signals[i].description != "") {
  700. class_desc->push_font(doc_font);
  701. class_desc->push_color(comment_color);
  702. class_desc->push_indent(1);
  703. _add_text(DTR(cd.signals[i].description));
  704. class_desc->pop(); // indent
  705. class_desc->pop();
  706. class_desc->pop(); // font
  707. }
  708. class_desc->add_newline();
  709. class_desc->add_newline();
  710. }
  711. class_desc->pop();
  712. class_desc->add_newline();
  713. }
  714. // Constants and enums
  715. if (cd.constants.size()) {
  716. Map<String, Vector<DocData::ConstantDoc>> enums;
  717. Vector<DocData::ConstantDoc> constants;
  718. for (int i = 0; i < cd.constants.size(); i++) {
  719. if (cd.constants[i].enumeration != String()) {
  720. if (!enums.has(cd.constants[i].enumeration)) {
  721. enums[cd.constants[i].enumeration] = Vector<DocData::ConstantDoc>();
  722. }
  723. enums[cd.constants[i].enumeration].push_back(cd.constants[i]);
  724. } else {
  725. constants.push_back(cd.constants[i]);
  726. }
  727. }
  728. // Enums
  729. if (enums.size()) {
  730. section_line.push_back(Pair<String, int>(TTR("Enumerations"), class_desc->get_line_count() - 2));
  731. class_desc->push_color(title_color);
  732. class_desc->push_font(doc_title_font);
  733. class_desc->add_text(TTR("Enumerations"));
  734. class_desc->pop();
  735. class_desc->pop();
  736. class_desc->push_indent(1);
  737. class_desc->add_newline();
  738. for (Map<String, Vector<DocData::ConstantDoc>>::Element *E = enums.front(); E; E = E->next()) {
  739. enum_line[E->key()] = class_desc->get_line_count() - 2;
  740. class_desc->push_font(doc_code_font);
  741. class_desc->push_color(title_color);
  742. class_desc->add_text("enum ");
  743. class_desc->pop();
  744. String e = E->key();
  745. if ((e.get_slice_count(".") > 1) && (e.get_slice(".", 0) == edited_class)) {
  746. e = e.get_slice(".", 1);
  747. }
  748. class_desc->push_color(headline_color);
  749. class_desc->add_text(e);
  750. class_desc->pop();
  751. class_desc->pop();
  752. class_desc->push_color(symbol_color);
  753. class_desc->add_text(":");
  754. class_desc->pop();
  755. class_desc->add_newline();
  756. class_desc->add_newline();
  757. class_desc->push_indent(1);
  758. Vector<DocData::ConstantDoc> enum_list = E->get();
  759. Map<String, int> enumValuesContainer;
  760. int enumStartingLine = enum_line[E->key()];
  761. for (int i = 0; i < enum_list.size(); i++) {
  762. if (cd.name == "@GlobalScope") {
  763. enumValuesContainer[enum_list[i].name] = enumStartingLine;
  764. }
  765. // Add the enum constant line to the constant_line map so we can locate it as a constant.
  766. constant_line[enum_list[i].name] = class_desc->get_line_count() - 2;
  767. class_desc->push_font(doc_code_font);
  768. class_desc->push_color(headline_color);
  769. _add_bulletpoint();
  770. _add_text(enum_list[i].name);
  771. class_desc->pop();
  772. class_desc->push_color(symbol_color);
  773. class_desc->add_text(" = ");
  774. class_desc->pop();
  775. class_desc->push_color(value_color);
  776. _add_text(_fix_constant(enum_list[i].value));
  777. class_desc->pop();
  778. class_desc->pop();
  779. class_desc->add_newline();
  780. if (enum_list[i].description.strip_edges() != "") {
  781. class_desc->push_font(doc_font);
  782. class_desc->push_color(comment_color);
  783. _add_text(DTR(enum_list[i].description));
  784. class_desc->pop();
  785. class_desc->pop();
  786. if (DTR(enum_list[i].description).find("\n") > 0) {
  787. class_desc->add_newline();
  788. }
  789. }
  790. class_desc->add_newline();
  791. }
  792. if (cd.name == "@GlobalScope") {
  793. enum_values_line[E->key()] = enumValuesContainer;
  794. }
  795. class_desc->pop();
  796. class_desc->add_newline();
  797. }
  798. class_desc->pop();
  799. class_desc->add_newline();
  800. }
  801. // Constants
  802. if (constants.size()) {
  803. section_line.push_back(Pair<String, int>(TTR("Constants"), class_desc->get_line_count() - 2));
  804. class_desc->push_color(title_color);
  805. class_desc->push_font(doc_title_font);
  806. class_desc->add_text(TTR("Constants"));
  807. class_desc->pop();
  808. class_desc->pop();
  809. class_desc->push_indent(1);
  810. class_desc->add_newline();
  811. for (int i = 0; i < constants.size(); i++) {
  812. constant_line[constants[i].name] = class_desc->get_line_count() - 2;
  813. class_desc->push_font(doc_code_font);
  814. if (constants[i].value.begins_with("Color(") && constants[i].value.ends_with(")")) {
  815. String stripped = constants[i].value.replace(" ", "").replace("Color(", "").replace(")", "");
  816. Vector<float> color = stripped.split_floats(",");
  817. if (color.size() >= 3) {
  818. class_desc->push_color(Color(color[0], color[1], color[2]));
  819. _add_bulletpoint();
  820. class_desc->pop();
  821. }
  822. } else {
  823. _add_bulletpoint();
  824. }
  825. class_desc->push_color(headline_color);
  826. _add_text(constants[i].name);
  827. class_desc->pop();
  828. class_desc->push_color(symbol_color);
  829. class_desc->add_text(" = ");
  830. class_desc->pop();
  831. class_desc->push_color(value_color);
  832. _add_text(_fix_constant(constants[i].value));
  833. class_desc->pop();
  834. class_desc->pop();
  835. class_desc->add_newline();
  836. if (constants[i].description != "") {
  837. class_desc->push_font(doc_font);
  838. class_desc->push_color(comment_color);
  839. _add_text(DTR(constants[i].description));
  840. class_desc->pop();
  841. class_desc->pop();
  842. if (DTR(constants[i].description).find("\n") > 0) {
  843. class_desc->add_newline();
  844. }
  845. }
  846. class_desc->add_newline();
  847. }
  848. class_desc->pop();
  849. class_desc->add_newline();
  850. }
  851. }
  852. // Property descriptions
  853. if (property_descr) {
  854. section_line.push_back(Pair<String, int>(TTR("Property Descriptions"), class_desc->get_line_count() - 2));
  855. class_desc->push_color(title_color);
  856. class_desc->push_font(doc_title_font);
  857. class_desc->add_text(TTR("Property Descriptions"));
  858. class_desc->pop();
  859. class_desc->pop();
  860. class_desc->add_newline();
  861. class_desc->add_newline();
  862. for (int i = 0; i < cd.properties.size(); i++) {
  863. if (cd.properties[i].overridden) {
  864. continue;
  865. }
  866. property_line[cd.properties[i].name] = class_desc->get_line_count() - 2;
  867. class_desc->push_table(2);
  868. class_desc->set_table_column_expand(1, true);
  869. class_desc->push_cell();
  870. class_desc->push_font(doc_code_font);
  871. _add_bulletpoint();
  872. _add_type(cd.properties[i].type, cd.properties[i].enumeration);
  873. class_desc->add_text(" ");
  874. class_desc->pop(); // font
  875. class_desc->pop(); // cell
  876. class_desc->push_cell();
  877. class_desc->push_font(doc_code_font);
  878. class_desc->push_color(headline_color);
  879. _add_text(cd.properties[i].name);
  880. class_desc->pop(); // color
  881. if (cd.properties[i].default_value != "") {
  882. class_desc->push_color(symbol_color);
  883. class_desc->add_text(" [" + TTR("default:") + " ");
  884. class_desc->pop(); // color
  885. class_desc->push_color(value_color);
  886. _add_text(_fix_constant(cd.properties[i].default_value));
  887. class_desc->pop(); // color
  888. class_desc->push_color(symbol_color);
  889. class_desc->add_text("]");
  890. class_desc->pop(); // color
  891. }
  892. class_desc->pop(); // font
  893. class_desc->pop(); // cell
  894. Map<String, DocData::MethodDoc> method_map;
  895. for (int j = 0; j < methods.size(); j++) {
  896. method_map[methods[j].name] = methods[j];
  897. }
  898. if (cd.properties[i].setter != "") {
  899. class_desc->push_cell();
  900. class_desc->pop(); // cell
  901. class_desc->push_cell();
  902. class_desc->push_font(doc_code_font);
  903. class_desc->push_color(text_color);
  904. if (method_map[cd.properties[i].setter].arguments.size() > 1) {
  905. // Setters with additional arguments are exposed in the method list, so we link them here for quick access.
  906. class_desc->push_meta("@method " + cd.properties[i].setter);
  907. class_desc->add_text(cd.properties[i].setter + TTR("(value)"));
  908. class_desc->pop();
  909. } else {
  910. class_desc->add_text(cd.properties[i].setter + TTR("(value)"));
  911. }
  912. class_desc->pop(); // color
  913. class_desc->push_color(comment_color);
  914. class_desc->add_text(" setter");
  915. class_desc->pop(); // color
  916. class_desc->pop(); // font
  917. class_desc->pop(); // cell
  918. method_line[cd.properties[i].setter] = property_line[cd.properties[i].name];
  919. }
  920. if (cd.properties[i].getter != "") {
  921. class_desc->push_cell();
  922. class_desc->pop(); // cell
  923. class_desc->push_cell();
  924. class_desc->push_font(doc_code_font);
  925. class_desc->push_color(text_color);
  926. if (method_map[cd.properties[i].getter].arguments.size() > 0) {
  927. // Getters with additional arguments are exposed in the method list, so we link them here for quick access.
  928. class_desc->push_meta("@method " + cd.properties[i].getter);
  929. class_desc->add_text(cd.properties[i].getter + "()");
  930. class_desc->pop();
  931. } else {
  932. class_desc->add_text(cd.properties[i].getter + "()");
  933. }
  934. class_desc->pop(); //color
  935. class_desc->push_color(comment_color);
  936. class_desc->add_text(" getter");
  937. class_desc->pop(); //color
  938. class_desc->pop(); //font
  939. class_desc->pop(); //cell
  940. method_line[cd.properties[i].getter] = property_line[cd.properties[i].name];
  941. }
  942. class_desc->pop(); // table
  943. class_desc->add_newline();
  944. class_desc->add_newline();
  945. class_desc->push_color(text_color);
  946. class_desc->push_font(doc_font);
  947. class_desc->push_indent(1);
  948. if (cd.properties[i].description.strip_edges() != String()) {
  949. _add_text(DTR(cd.properties[i].description));
  950. } else {
  951. class_desc->add_image(get_icon("Error", "EditorIcons"));
  952. class_desc->add_text(" ");
  953. class_desc->push_color(comment_color);
  954. class_desc->append_bbcode(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));
  955. class_desc->pop();
  956. }
  957. class_desc->pop();
  958. class_desc->pop();
  959. class_desc->pop();
  960. class_desc->add_newline();
  961. class_desc->add_newline();
  962. class_desc->add_newline();
  963. }
  964. }
  965. // Method descriptions
  966. if (method_descr) {
  967. section_line.push_back(Pair<String, int>(TTR("Method Descriptions"), class_desc->get_line_count() - 2));
  968. class_desc->push_color(title_color);
  969. class_desc->push_font(doc_title_font);
  970. class_desc->add_text(TTR("Method Descriptions"));
  971. class_desc->pop();
  972. class_desc->pop();
  973. class_desc->add_newline();
  974. class_desc->add_newline();
  975. for (int pass = 0; pass < 2; pass++) {
  976. Vector<DocData::MethodDoc> methods_filtered;
  977. for (int i = 0; i < methods.size(); i++) {
  978. const String &q = methods[i].qualifiers;
  979. if ((pass == 0 && q.find("virtual") != -1) || (pass == 1 && q.find("virtual") == -1)) {
  980. methods_filtered.push_back(methods[i]);
  981. }
  982. }
  983. for (int i = 0; i < methods_filtered.size(); i++) {
  984. class_desc->push_font(doc_code_font);
  985. _add_method(methods_filtered[i], false);
  986. class_desc->pop();
  987. class_desc->add_newline();
  988. class_desc->add_newline();
  989. class_desc->push_color(text_color);
  990. class_desc->push_font(doc_font);
  991. class_desc->push_indent(1);
  992. if (methods_filtered[i].description.strip_edges() != String()) {
  993. _add_text(DTR(methods_filtered[i].description));
  994. } else {
  995. class_desc->add_image(get_icon("Error", "EditorIcons"));
  996. class_desc->add_text(" ");
  997. class_desc->push_color(comment_color);
  998. class_desc->append_bbcode(TTR("There is currently no description for this method. Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
  999. class_desc->pop();
  1000. }
  1001. class_desc->pop();
  1002. class_desc->pop();
  1003. class_desc->pop();
  1004. class_desc->add_newline();
  1005. class_desc->add_newline();
  1006. class_desc->add_newline();
  1007. }
  1008. }
  1009. }
  1010. scroll_locked = false;
  1011. }
  1012. void EditorHelp::_request_help(const String &p_string) {
  1013. Error err = _goto_desc(p_string);
  1014. if (err == OK) {
  1015. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  1016. }
  1017. // 100 palabras
  1018. }
  1019. void EditorHelp::_help_callback(const String &p_topic) {
  1020. String what = p_topic.get_slice(":", 0);
  1021. String clss = p_topic.get_slice(":", 1);
  1022. String name;
  1023. if (p_topic.get_slice_count(":") == 3) {
  1024. name = p_topic.get_slice(":", 2);
  1025. }
  1026. _request_help(clss); // First go to class.
  1027. int line = 0;
  1028. if (what == "class_desc") {
  1029. line = description_line;
  1030. } else if (what == "class_signal") {
  1031. if (signal_line.has(name)) {
  1032. line = signal_line[name];
  1033. }
  1034. } else if (what == "class_method" || what == "class_method_desc") {
  1035. if (method_line.has(name)) {
  1036. line = method_line[name];
  1037. }
  1038. } else if (what == "class_property") {
  1039. if (property_line.has(name)) {
  1040. line = property_line[name];
  1041. }
  1042. } else if (what == "class_enum") {
  1043. if (enum_line.has(name)) {
  1044. line = enum_line[name];
  1045. }
  1046. } else if (what == "class_theme_item") {
  1047. if (theme_property_line.has(name)) {
  1048. line = theme_property_line[name];
  1049. }
  1050. } else if (what == "class_constant") {
  1051. if (constant_line.has(name)) {
  1052. line = constant_line[name];
  1053. }
  1054. } else if (what == "class_global") {
  1055. if (constant_line.has(name)) {
  1056. line = constant_line[name];
  1057. } else {
  1058. Map<String, Map<String, int>>::Element *iter = enum_values_line.front();
  1059. while (true) {
  1060. if (iter->value().has(name)) {
  1061. line = iter->value()[name];
  1062. break;
  1063. } else if (iter == enum_values_line.back()) {
  1064. break;
  1065. } else {
  1066. iter = iter->next();
  1067. }
  1068. }
  1069. }
  1070. }
  1071. class_desc->call_deferred("scroll_to_line", line);
  1072. }
  1073. static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt) {
  1074. DocData *doc = EditorHelp::get_doc_data();
  1075. String base_path;
  1076. Ref<Font> doc_font = p_rt->get_font("doc", "EditorFonts");
  1077. Ref<Font> doc_bold_font = p_rt->get_font("doc_bold", "EditorFonts");
  1078. Ref<Font> doc_code_font = p_rt->get_font("doc_source", "EditorFonts");
  1079. Color font_color_hl = p_rt->get_color("headline_color", "EditorHelp");
  1080. Color accent_color = p_rt->get_color("accent_color", "Editor");
  1081. Color link_color = accent_color.linear_interpolate(font_color_hl, 0.8);
  1082. Color code_color = accent_color.linear_interpolate(font_color_hl, 0.6);
  1083. String bbcode = p_bbcode.dedent().replace("\t", "").replace("\r", "").strip_edges();
  1084. // Remove extra new lines around code blocks.
  1085. bbcode = bbcode.replace("[codeblock]\n", "[codeblock]");
  1086. bbcode = bbcode.replace("\n[/codeblock]", "[/codeblock]");
  1087. List<String> tag_stack;
  1088. bool code_tag = false;
  1089. int pos = 0;
  1090. while (pos < bbcode.length()) {
  1091. int brk_pos = bbcode.find("[", pos);
  1092. if (brk_pos < 0) {
  1093. brk_pos = bbcode.length();
  1094. }
  1095. if (brk_pos > pos) {
  1096. String text = bbcode.substr(pos, brk_pos - pos);
  1097. if (!code_tag) {
  1098. text = text.replace("\n", "\n\n");
  1099. }
  1100. p_rt->add_text(text);
  1101. }
  1102. if (brk_pos == bbcode.length()) {
  1103. break; // Nothing else to add.
  1104. }
  1105. int brk_end = bbcode.find("]", brk_pos + 1);
  1106. if (brk_end == -1) {
  1107. String text = bbcode.substr(brk_pos, bbcode.length() - brk_pos);
  1108. if (!code_tag) {
  1109. text = text.replace("\n", "\n\n");
  1110. }
  1111. p_rt->add_text(text);
  1112. break;
  1113. }
  1114. String tag = bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
  1115. if (tag.begins_with("/")) {
  1116. bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length());
  1117. if (!tag_ok) {
  1118. p_rt->add_text("[");
  1119. pos = brk_pos + 1;
  1120. continue;
  1121. }
  1122. tag_stack.pop_front();
  1123. pos = brk_end + 1;
  1124. if (tag != "/img") {
  1125. p_rt->pop();
  1126. if (code_tag) {
  1127. p_rt->pop();
  1128. }
  1129. }
  1130. code_tag = false;
  1131. } else if (code_tag) {
  1132. p_rt->add_text("[");
  1133. pos = brk_pos + 1;
  1134. } else if (tag.begins_with("method ") || tag.begins_with("member ") || tag.begins_with("signal ") || tag.begins_with("enum ") || tag.begins_with("constant ")) {
  1135. const int tag_end = tag.find(" ");
  1136. const String link_tag = tag.substr(0, tag_end);
  1137. const String link_target = tag.substr(tag_end + 1, tag.length()).lstrip(" ");
  1138. p_rt->push_font(doc_code_font);
  1139. p_rt->push_color(link_color);
  1140. p_rt->push_meta("@" + link_tag + " " + link_target);
  1141. p_rt->add_text(link_target + (tag.begins_with("method ") ? "()" : ""));
  1142. p_rt->pop();
  1143. p_rt->pop();
  1144. p_rt->pop();
  1145. pos = brk_end + 1;
  1146. } else if (doc->class_list.has(tag)) {
  1147. // Class reference tag such as [Node2D] or [SceneTree].
  1148. p_rt->push_font(doc_code_font);
  1149. p_rt->push_color(link_color);
  1150. p_rt->push_meta("#" + tag);
  1151. p_rt->add_text(tag);
  1152. p_rt->pop();
  1153. p_rt->pop();
  1154. p_rt->pop();
  1155. pos = brk_end + 1;
  1156. } else if (tag == "b") {
  1157. // Use bold font.
  1158. p_rt->push_font(doc_bold_font);
  1159. pos = brk_end + 1;
  1160. tag_stack.push_front(tag);
  1161. } else if (tag == "i") {
  1162. // Use italics font.
  1163. p_rt->push_color(font_color_hl);
  1164. pos = brk_end + 1;
  1165. tag_stack.push_front(tag);
  1166. } else if (tag == "code" || tag == "codeblock") {
  1167. // Use monospace font.
  1168. p_rt->push_font(doc_code_font);
  1169. p_rt->push_color(code_color);
  1170. code_tag = true;
  1171. pos = brk_end + 1;
  1172. tag_stack.push_front(tag);
  1173. } else if (tag == "center") {
  1174. // Align to center.
  1175. p_rt->push_align(RichTextLabel::ALIGN_CENTER);
  1176. pos = brk_end + 1;
  1177. tag_stack.push_front(tag);
  1178. } else if (tag == "br") {
  1179. // Force a line break.
  1180. p_rt->add_newline();
  1181. pos = brk_end + 1;
  1182. } else if (tag == "u") {
  1183. // Use underline.
  1184. p_rt->push_underline();
  1185. pos = brk_end + 1;
  1186. tag_stack.push_front(tag);
  1187. } else if (tag == "s") {
  1188. // Use strikethrough.
  1189. p_rt->push_strikethrough();
  1190. pos = brk_end + 1;
  1191. tag_stack.push_front(tag);
  1192. } else if (tag == "url") {
  1193. int end = bbcode.find("[", brk_end);
  1194. if (end == -1) {
  1195. end = bbcode.length();
  1196. }
  1197. String url = bbcode.substr(brk_end + 1, end - brk_end - 1);
  1198. p_rt->push_meta(url);
  1199. pos = brk_end + 1;
  1200. tag_stack.push_front(tag);
  1201. } else if (tag.begins_with("url=")) {
  1202. String url = tag.substr(4, tag.length());
  1203. p_rt->push_meta(url);
  1204. pos = brk_end + 1;
  1205. tag_stack.push_front("url");
  1206. } else if (tag == "img") {
  1207. int end = bbcode.find("[", brk_end);
  1208. if (end == -1) {
  1209. end = bbcode.length();
  1210. }
  1211. String image = bbcode.substr(brk_end + 1, end - brk_end - 1);
  1212. Ref<Texture> texture = ResourceLoader::load(base_path.plus_file(image), "Texture");
  1213. if (texture.is_valid()) {
  1214. p_rt->add_image(texture);
  1215. }
  1216. pos = end;
  1217. tag_stack.push_front(tag);
  1218. } else if (tag.begins_with("color=")) {
  1219. String col = tag.substr(6, tag.length());
  1220. Color color;
  1221. if (col.begins_with("#")) {
  1222. color = Color::html(col);
  1223. } else if (col == "aqua") {
  1224. color = Color(0, 1, 1);
  1225. } else if (col == "black") {
  1226. color = Color(0, 0, 0);
  1227. } else if (col == "blue") {
  1228. color = Color(0, 0, 1);
  1229. } else if (col == "fuchsia") {
  1230. color = Color(1, 0, 1);
  1231. } else if (col == "gray" || col == "grey") {
  1232. color = Color(0.5, 0.5, 0.5);
  1233. } else if (col == "green") {
  1234. color = Color(0, 0.5, 0);
  1235. } else if (col == "lime") {
  1236. color = Color(0, 1, 0);
  1237. } else if (col == "maroon") {
  1238. color = Color(0.5, 0, 0);
  1239. } else if (col == "navy") {
  1240. color = Color(0, 0, 0.5);
  1241. } else if (col == "olive") {
  1242. color = Color(0.5, 0.5, 0);
  1243. } else if (col == "purple") {
  1244. color = Color(0.5, 0, 0.5);
  1245. } else if (col == "red") {
  1246. color = Color(1, 0, 0);
  1247. } else if (col == "silver") {
  1248. color = Color(0.75, 0.75, 0.75);
  1249. } else if (col == "teal") {
  1250. color = Color(0, 0.5, 0.5);
  1251. } else if (col == "white") {
  1252. color = Color(1, 1, 1);
  1253. } else if (col == "yellow") {
  1254. color = Color(1, 1, 0);
  1255. } else {
  1256. color = Color(0, 0, 0); // base_color;
  1257. }
  1258. p_rt->push_color(color);
  1259. pos = brk_end + 1;
  1260. tag_stack.push_front("color");
  1261. } else if (tag.begins_with("font=")) {
  1262. String fnt = tag.substr(5, tag.length());
  1263. Ref<Font> font = ResourceLoader::load(base_path.plus_file(fnt), "Font");
  1264. if (font.is_valid()) {
  1265. p_rt->push_font(font);
  1266. } else {
  1267. p_rt->push_font(doc_font);
  1268. }
  1269. pos = brk_end + 1;
  1270. tag_stack.push_front("font");
  1271. } else {
  1272. p_rt->add_text("["); // ignore
  1273. pos = brk_pos + 1;
  1274. }
  1275. }
  1276. }
  1277. void EditorHelp::_add_text(const String &p_bbcode) {
  1278. _add_text_to_rt(p_bbcode, class_desc);
  1279. }
  1280. void EditorHelp::generate_doc() {
  1281. doc = memnew(DocData);
  1282. doc->generate(true);
  1283. DocData compdoc;
  1284. compdoc.load_compressed(_doc_data_compressed, _doc_data_compressed_size, _doc_data_uncompressed_size);
  1285. doc->merge_from(compdoc); // Ensure all is up to date.
  1286. }
  1287. void EditorHelp::_notification(int p_what) {
  1288. switch (p_what) {
  1289. case NOTIFICATION_READY:
  1290. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1291. _update_doc();
  1292. } break;
  1293. case NOTIFICATION_THEME_CHANGED: {
  1294. if (is_inside_tree()) {
  1295. _class_desc_resized();
  1296. }
  1297. } break;
  1298. default:
  1299. break;
  1300. }
  1301. }
  1302. void EditorHelp::go_to_help(const String &p_help) {
  1303. _help_callback(p_help);
  1304. }
  1305. void EditorHelp::go_to_class(const String &p_class, int p_scroll) {
  1306. _goto_desc(p_class, p_scroll);
  1307. }
  1308. Vector<Pair<String, int>> EditorHelp::get_sections() {
  1309. Vector<Pair<String, int>> sections;
  1310. for (int i = 0; i < section_line.size(); i++) {
  1311. sections.push_back(Pair<String, int>(section_line[i].first, i));
  1312. }
  1313. return sections;
  1314. }
  1315. void EditorHelp::scroll_to_section(int p_section_index) {
  1316. int line = section_line[p_section_index].second;
  1317. class_desc->scroll_to_line(line);
  1318. }
  1319. void EditorHelp::popup_search() {
  1320. find_bar->popup_search();
  1321. }
  1322. String EditorHelp::get_class() {
  1323. return edited_class;
  1324. }
  1325. void EditorHelp::search_again(bool p_search_previous) {
  1326. _search(p_search_previous);
  1327. }
  1328. int EditorHelp::get_scroll() const {
  1329. return class_desc->get_v_scroll()->get_value();
  1330. }
  1331. void EditorHelp::set_scroll(int p_scroll) {
  1332. class_desc->get_v_scroll()->set_value(p_scroll);
  1333. }
  1334. void EditorHelp::_bind_methods() {
  1335. ClassDB::bind_method("_class_list_select", &EditorHelp::_class_list_select);
  1336. ClassDB::bind_method("_class_desc_select", &EditorHelp::_class_desc_select);
  1337. ClassDB::bind_method("_class_desc_input", &EditorHelp::_class_desc_input);
  1338. ClassDB::bind_method("_class_desc_resized", &EditorHelp::_class_desc_resized);
  1339. ClassDB::bind_method("_request_help", &EditorHelp::_request_help);
  1340. ClassDB::bind_method("_unhandled_key_input", &EditorHelp::_unhandled_key_input);
  1341. ClassDB::bind_method("_search", &EditorHelp::_search);
  1342. ClassDB::bind_method("_help_callback", &EditorHelp::_help_callback);
  1343. ADD_SIGNAL(MethodInfo("go_to_help"));
  1344. }
  1345. EditorHelp::EditorHelp() {
  1346. set_custom_minimum_size(Size2(150 * EDSCALE, 0));
  1347. EDITOR_DEF("text_editor/help/sort_functions_alphabetically", true);
  1348. class_desc = memnew(RichTextLabel);
  1349. add_child(class_desc);
  1350. class_desc->set_v_size_flags(SIZE_EXPAND_FILL);
  1351. class_desc->add_color_override("selection_color", get_color("accent_color", "Editor") * Color(1, 1, 1, 0.4));
  1352. class_desc->connect("meta_clicked", this, "_class_desc_select");
  1353. class_desc->connect("gui_input", this, "_class_desc_input");
  1354. class_desc->connect("resized", this, "_class_desc_resized");
  1355. _class_desc_resized();
  1356. // Added second so it opens at the bottom so it won't offset the entire widget.
  1357. find_bar = memnew(FindBar);
  1358. add_child(find_bar);
  1359. find_bar->hide();
  1360. find_bar->set_rich_text_label(class_desc);
  1361. class_desc->set_selection_enabled(true);
  1362. scroll_locked = false;
  1363. select_locked = false;
  1364. class_desc->hide();
  1365. }
  1366. EditorHelp::~EditorHelp() {
  1367. }
  1368. void EditorHelpBit::_go_to_help(String p_what) {
  1369. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  1370. ScriptEditor::get_singleton()->goto_help(p_what);
  1371. emit_signal("request_hide");
  1372. }
  1373. void EditorHelpBit::_meta_clicked(String p_select) {
  1374. if (p_select.begins_with("$")) { // enum
  1375. String select = p_select.substr(1, p_select.length());
  1376. String class_name;
  1377. if (select.find(".") != -1) {
  1378. class_name = select.get_slice(".", 0);
  1379. } else {
  1380. class_name = "@Global";
  1381. }
  1382. _go_to_help("class_enum:" + class_name + ":" + select);
  1383. return;
  1384. } else if (p_select.begins_with("#")) {
  1385. _go_to_help("class_name:" + p_select.substr(1, p_select.length()));
  1386. return;
  1387. } else if (p_select.begins_with("@")) {
  1388. String m = p_select.substr(1, p_select.length());
  1389. if (m.find(".") != -1) {
  1390. _go_to_help("class_method:" + m.get_slice(".", 0) + ":" + m.get_slice(".", 0)); // Must go somewhere else.
  1391. }
  1392. }
  1393. }
  1394. void EditorHelpBit::_bind_methods() {
  1395. ClassDB::bind_method("_meta_clicked", &EditorHelpBit::_meta_clicked);
  1396. ClassDB::bind_method(D_METHOD("set_text", "text"), &EditorHelpBit::set_text);
  1397. ADD_SIGNAL(MethodInfo("request_hide"));
  1398. }
  1399. void EditorHelpBit::_notification(int p_what) {
  1400. switch (p_what) {
  1401. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1402. rich_text->add_color_override("selection_color", get_color("accent_color", "Editor") * Color(1, 1, 1, 0.4));
  1403. } break;
  1404. default:
  1405. break;
  1406. }
  1407. }
  1408. void EditorHelpBit::set_text(const String &p_text) {
  1409. rich_text->clear();
  1410. _add_text_to_rt(p_text, rich_text);
  1411. }
  1412. EditorHelpBit::EditorHelpBit() {
  1413. rich_text = memnew(RichTextLabel);
  1414. add_child(rich_text);
  1415. rich_text->connect("meta_clicked", this, "_meta_clicked");
  1416. rich_text->add_color_override("selection_color", get_color("accent_color", "Editor") * Color(1, 1, 1, 0.4));
  1417. rich_text->set_override_selected_font_color(false);
  1418. set_custom_minimum_size(Size2(0, 70 * EDSCALE));
  1419. }
  1420. FindBar::FindBar() {
  1421. results_count = 0;
  1422. search_text = memnew(LineEdit);
  1423. add_child(search_text);
  1424. search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  1425. search_text->set_h_size_flags(SIZE_EXPAND_FILL);
  1426. search_text->connect("text_changed", this, "_search_text_changed");
  1427. search_text->connect("text_entered", this, "_search_text_entered");
  1428. matches_label = memnew(Label);
  1429. add_child(matches_label);
  1430. matches_label->hide();
  1431. find_prev = memnew(ToolButton);
  1432. add_child(find_prev);
  1433. find_prev->set_focus_mode(FOCUS_NONE);
  1434. find_prev->connect("pressed", this, "_search_prev");
  1435. find_next = memnew(ToolButton);
  1436. add_child(find_next);
  1437. find_next->set_focus_mode(FOCUS_NONE);
  1438. find_next->connect("pressed", this, "_search_next");
  1439. Control *space = memnew(Control);
  1440. add_child(space);
  1441. space->set_custom_minimum_size(Size2(4, 0) * EDSCALE);
  1442. hide_button = memnew(TextureButton);
  1443. add_child(hide_button);
  1444. hide_button->set_focus_mode(FOCUS_NONE);
  1445. hide_button->set_expand(true);
  1446. hide_button->set_stretch_mode(TextureButton::STRETCH_KEEP_CENTERED);
  1447. hide_button->connect("pressed", this, "_hide_pressed");
  1448. }
  1449. void FindBar::popup_search() {
  1450. show();
  1451. bool grabbed_focus = false;
  1452. if (!search_text->has_focus()) {
  1453. search_text->grab_focus();
  1454. grabbed_focus = true;
  1455. }
  1456. if (!search_text->get_text().empty()) {
  1457. search_text->select_all();
  1458. search_text->set_cursor_position(search_text->get_text().length());
  1459. if (grabbed_focus) {
  1460. _search();
  1461. }
  1462. }
  1463. }
  1464. void FindBar::_notification(int p_what) {
  1465. switch (p_what) {
  1466. case NOTIFICATION_ENTER_TREE:
  1467. case NOTIFICATION_THEME_CHANGED: {
  1468. find_prev->set_icon(get_icon("MoveUp", "EditorIcons"));
  1469. find_next->set_icon(get_icon("MoveDown", "EditorIcons"));
  1470. hide_button->set_normal_texture(get_icon("Close", "EditorIcons"));
  1471. hide_button->set_hover_texture(get_icon("Close", "EditorIcons"));
  1472. hide_button->set_pressed_texture(get_icon("Close", "EditorIcons"));
  1473. hide_button->set_custom_minimum_size(hide_button->get_normal_texture()->get_size());
  1474. matches_label->add_color_override("font_color", results_count > 0 ? get_color("font_color", "Label") : get_color("error_color", "Editor"));
  1475. } break;
  1476. case NOTIFICATION_VISIBILITY_CHANGED: {
  1477. set_process_unhandled_input(is_visible_in_tree());
  1478. } break;
  1479. }
  1480. }
  1481. void FindBar::_bind_methods() {
  1482. ClassDB::bind_method("_unhandled_input", &FindBar::_unhandled_input);
  1483. ClassDB::bind_method("_search_text_changed", &FindBar::_search_text_changed);
  1484. ClassDB::bind_method("_search_text_entered", &FindBar::_search_text_entered);
  1485. ClassDB::bind_method("_search_next", &FindBar::search_next);
  1486. ClassDB::bind_method("_search_prev", &FindBar::search_prev);
  1487. ClassDB::bind_method("_hide_pressed", &FindBar::_hide_bar);
  1488. ADD_SIGNAL(MethodInfo("search"));
  1489. }
  1490. void FindBar::set_rich_text_label(RichTextLabel *p_rich_text_label) {
  1491. rich_text_label = p_rich_text_label;
  1492. }
  1493. bool FindBar::search_next() {
  1494. return _search();
  1495. }
  1496. bool FindBar::search_prev() {
  1497. return _search(true);
  1498. }
  1499. bool FindBar::_search(bool p_search_previous) {
  1500. String stext = search_text->get_text();
  1501. bool keep = prev_search == stext;
  1502. bool ret = rich_text_label->search(stext, keep, p_search_previous);
  1503. if (!ret) {
  1504. ret = rich_text_label->search(stext, false, p_search_previous);
  1505. }
  1506. prev_search = stext;
  1507. if (ret) {
  1508. _update_results_count();
  1509. } else {
  1510. results_count = 0;
  1511. }
  1512. _update_matches_label();
  1513. return ret;
  1514. }
  1515. void FindBar::_update_results_count() {
  1516. results_count = 0;
  1517. String searched = search_text->get_text();
  1518. if (searched.empty()) {
  1519. return;
  1520. }
  1521. String full_text = rich_text_label->get_text();
  1522. int from_pos = 0;
  1523. while (true) {
  1524. int pos = full_text.findn(searched, from_pos);
  1525. if (pos == -1) {
  1526. break;
  1527. }
  1528. results_count++;
  1529. from_pos = pos + searched.length();
  1530. }
  1531. }
  1532. void FindBar::_update_matches_label() {
  1533. if (search_text->get_text().empty() || results_count == -1) {
  1534. matches_label->hide();
  1535. } else {
  1536. matches_label->show();
  1537. matches_label->add_color_override("font_color", results_count > 0 ? get_color("font_color", "Label") : get_color("error_color", "Editor"));
  1538. matches_label->set_text(vformat(results_count == 1 ? TTR("%d match.") : TTR("%d matches."), results_count));
  1539. }
  1540. }
  1541. void FindBar::_hide_bar() {
  1542. if (search_text->has_focus()) {
  1543. rich_text_label->grab_focus();
  1544. }
  1545. hide();
  1546. }
  1547. void FindBar::_unhandled_input(const Ref<InputEvent> &p_event) {
  1548. Ref<InputEventKey> k = p_event;
  1549. if (k.is_valid()) {
  1550. if (k->is_pressed() && (rich_text_label->has_focus() || is_a_parent_of(get_focus_owner()))) {
  1551. bool accepted = true;
  1552. switch (k->get_scancode()) {
  1553. case KEY_ESCAPE: {
  1554. _hide_bar();
  1555. } break;
  1556. default: {
  1557. accepted = false;
  1558. } break;
  1559. }
  1560. if (accepted) {
  1561. accept_event();
  1562. }
  1563. }
  1564. }
  1565. }
  1566. void FindBar::_search_text_changed(const String &p_text) {
  1567. search_next();
  1568. }
  1569. void FindBar::_search_text_entered(const String &p_text) {
  1570. if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1571. search_prev();
  1572. } else {
  1573. search_next();
  1574. }
  1575. }