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