editor_help.cpp 51 KB

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