editor_help.cpp 53 KB

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