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