editor_help.cpp 52 KB

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