editor_help.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161
  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/core_constants.h"
  32. #include "core/input/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/version_generated.gen.h"
  35. #include "doc_data_compressed.gen.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_scale.h"
  38. #include "editor/editor_settings.h"
  39. #include "editor/plugins/script_editor_plugin.h"
  40. #define CONTRIBUTE_URL vformat("%s/community/contributing/updating_the_class_reference.html", VERSION_DOCS_URL)
  41. DocTools *EditorHelp::doc = nullptr;
  42. void EditorHelp::_update_theme() {
  43. text_color = get_theme_color(SNAME("text_color"), SNAME("EditorHelp"));
  44. title_color = get_theme_color(SNAME("title_color"), SNAME("EditorHelp"));
  45. headline_color = get_theme_color(SNAME("headline_color"), SNAME("EditorHelp"));
  46. comment_color = get_theme_color(SNAME("comment_color"), SNAME("EditorHelp"));
  47. symbol_color = get_theme_color(SNAME("symbol_color"), SNAME("EditorHelp"));
  48. value_color = get_theme_color(SNAME("value_color"), SNAME("EditorHelp"));
  49. qualifier_color = get_theme_color(SNAME("qualifier_color"), SNAME("EditorHelp"));
  50. type_color = get_theme_color(SNAME("type_color"), SNAME("EditorHelp"));
  51. class_desc->add_theme_color_override("selection_color", get_theme_color(SNAME("selection_color"), SNAME("EditorHelp")));
  52. class_desc->add_theme_constant_override("line_separation", get_theme_constant(SNAME("line_separation"), SNAME("EditorHelp")));
  53. class_desc->add_theme_constant_override("table_hseparation", get_theme_constant(SNAME("table_hseparation"), SNAME("EditorHelp")));
  54. class_desc->add_theme_constant_override("table_vseparation", get_theme_constant(SNAME("table_vseparation"), SNAME("EditorHelp")));
  55. doc_font = get_theme_font(SNAME("doc"), SNAME("EditorFonts"));
  56. doc_bold_font = get_theme_font(SNAME("doc_bold"), SNAME("EditorFonts"));
  57. doc_title_font = get_theme_font(SNAME("doc_title"), SNAME("EditorFonts"));
  58. doc_code_font = get_theme_font(SNAME("doc_source"), SNAME("EditorFonts"));
  59. }
  60. void EditorHelp::_search(bool p_search_previous) {
  61. if (p_search_previous) {
  62. find_bar->search_prev();
  63. } else {
  64. find_bar->search_next();
  65. }
  66. }
  67. void EditorHelp::_class_list_select(const String &p_select) {
  68. _goto_desc(p_select);
  69. }
  70. void EditorHelp::_class_desc_select(const String &p_select) {
  71. if (p_select.begins_with("$")) { //enum
  72. String select = p_select.substr(1, p_select.length());
  73. String class_name;
  74. if (select.contains(".")) {
  75. class_name = select.get_slice(".", 0);
  76. select = select.get_slice(".", 1);
  77. } else {
  78. class_name = "@GlobalScope";
  79. }
  80. emit_signal(SNAME("go_to_help"), "class_enum:" + class_name + ":" + select);
  81. return;
  82. } else if (p_select.begins_with("#")) {
  83. emit_signal(SNAME("go_to_help"), "class_name:" + p_select.substr(1, p_select.length()));
  84. return;
  85. } else if (p_select.begins_with("@")) {
  86. int tag_end = p_select.find(" ");
  87. String tag = p_select.substr(1, tag_end - 1);
  88. String link = p_select.substr(tag_end + 1, p_select.length()).lstrip(" ");
  89. String topic;
  90. Map<String, int> *table = nullptr;
  91. if (tag == "method") {
  92. topic = "class_method";
  93. table = &this->method_line;
  94. } else if (tag == "member") {
  95. topic = "class_property";
  96. table = &this->property_line;
  97. } else if (tag == "enum") {
  98. topic = "class_enum";
  99. table = &this->enum_line;
  100. } else if (tag == "signal") {
  101. topic = "class_signal";
  102. table = &this->signal_line;
  103. } else if (tag == "constant") {
  104. topic = "class_constant";
  105. table = &this->constant_line;
  106. } else if (tag == "theme_item") {
  107. topic = "theme_item";
  108. table = &this->theme_property_line;
  109. } else {
  110. return;
  111. }
  112. if (link.contains(".")) {
  113. emit_signal(SNAME("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_paragraph((*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(SNAME("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(SNAME("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(bool p_force_update_theme) {
  149. // Add extra horizontal margins for better readability.
  150. // The margins increase as the width of the editor help container increases.
  151. Ref<Font> doc_code_font = get_theme_font(SNAME("doc_source"), SNAME("EditorFonts"));
  152. int font_size = get_theme_font_size(SNAME("doc_source_size"), SNAME("EditorFonts"));
  153. real_t char_width = doc_code_font->get_char_size('x', 0, font_size).width;
  154. const int new_display_margin = MAX(30 * EDSCALE, get_parent_anchorable_rect().size.width - char_width * 120 * EDSCALE) * 0.5;
  155. if (display_margin != new_display_margin || p_force_update_theme) {
  156. display_margin = new_display_margin;
  157. Ref<StyleBox> class_desc_stylebox = EditorNode::get_singleton()->get_theme_base()->get_theme_stylebox(SNAME("normal"), SNAME("RichTextLabel"))->duplicate();
  158. class_desc_stylebox->set_default_margin(SIDE_LEFT, display_margin);
  159. class_desc_stylebox->set_default_margin(SIDE_RIGHT, display_margin);
  160. class_desc->add_theme_style_override("normal", class_desc_stylebox);
  161. }
  162. }
  163. void EditorHelp::_add_type(const String &p_type, const String &p_enum) {
  164. String t = p_type;
  165. if (t.is_empty()) {
  166. t = "void";
  167. }
  168. bool can_ref = (t != "void" && !t.contains("*")) || !p_enum.is_empty();
  169. if (!p_enum.is_empty()) {
  170. if (p_enum.get_slice_count(".") > 1) {
  171. t = p_enum.get_slice(".", 1);
  172. } else {
  173. t = p_enum.get_slice(".", 0);
  174. }
  175. }
  176. class_desc->push_color(type_color);
  177. bool add_array = false;
  178. if (can_ref) {
  179. if (t.ends_with("[]")) {
  180. add_array = true;
  181. t = t.replace("[]", "");
  182. }
  183. if (p_enum.is_empty()) {
  184. class_desc->push_meta("#" + t); //class
  185. } else {
  186. class_desc->push_meta("$" + p_enum); //class
  187. }
  188. }
  189. class_desc->add_text(t);
  190. if (can_ref) {
  191. class_desc->pop();
  192. if (add_array) {
  193. class_desc->add_text(" ");
  194. class_desc->push_meta("#Array"); //class
  195. class_desc->add_text("[]");
  196. class_desc->pop();
  197. }
  198. }
  199. class_desc->pop();
  200. }
  201. String EditorHelp::_fix_constant(const String &p_constant) const {
  202. if (p_constant.strip_edges() == "4294967295") {
  203. return "0xFFFFFFFF";
  204. }
  205. if (p_constant.strip_edges() == "2147483647") {
  206. return "0x7FFFFFFF";
  207. }
  208. if (p_constant.strip_edges() == "1048575") {
  209. return "0xFFFFF";
  210. }
  211. return p_constant;
  212. }
  213. void EditorHelp::_add_method(const DocData::MethodDoc &p_method, bool p_overview) {
  214. method_line[p_method.name] = class_desc->get_line_count() - 2; //gets overridden if description
  215. const bool is_vararg = p_method.qualifiers.contains("vararg");
  216. if (p_overview) {
  217. class_desc->push_cell();
  218. class_desc->push_paragraph(HORIZONTAL_ALIGNMENT_RIGHT, Control::TEXT_DIRECTION_AUTO, "");
  219. } else {
  220. _add_bulletpoint();
  221. }
  222. _add_type(p_method.return_type, p_method.return_enum);
  223. if (p_overview) {
  224. class_desc->pop(); //align
  225. class_desc->pop(); //cell
  226. class_desc->push_cell();
  227. } else {
  228. class_desc->add_text(" ");
  229. }
  230. if (p_overview && !p_method.description.is_empty()) {
  231. class_desc->push_meta("@method " + p_method.name);
  232. }
  233. class_desc->push_color(headline_color);
  234. _add_text(p_method.name);
  235. class_desc->pop();
  236. if (p_overview && !p_method.description.is_empty()) {
  237. class_desc->pop(); //meta
  238. }
  239. class_desc->push_color(symbol_color);
  240. class_desc->add_text("(");
  241. class_desc->pop();
  242. for (int j = 0; j < p_method.arguments.size(); j++) {
  243. class_desc->push_color(text_color);
  244. if (j > 0) {
  245. class_desc->add_text(", ");
  246. }
  247. _add_text(p_method.arguments[j].name);
  248. class_desc->add_text(": ");
  249. _add_type(p_method.arguments[j].type, p_method.arguments[j].enumeration);
  250. if (!p_method.arguments[j].default_value.is_empty()) {
  251. class_desc->push_color(symbol_color);
  252. class_desc->add_text(" = ");
  253. class_desc->pop();
  254. class_desc->push_color(value_color);
  255. _add_text(_fix_constant(p_method.arguments[j].default_value));
  256. class_desc->pop();
  257. }
  258. class_desc->pop();
  259. }
  260. if (is_vararg) {
  261. class_desc->push_color(text_color);
  262. if (p_method.arguments.size()) {
  263. class_desc->add_text(", ");
  264. }
  265. class_desc->push_color(symbol_color);
  266. class_desc->add_text("...");
  267. class_desc->pop();
  268. class_desc->pop();
  269. }
  270. class_desc->push_color(symbol_color);
  271. class_desc->add_text(")");
  272. class_desc->pop();
  273. if (!p_method.qualifiers.is_empty()) {
  274. class_desc->push_color(qualifier_color);
  275. class_desc->add_text(" ");
  276. _add_text(p_method.qualifiers);
  277. class_desc->pop();
  278. }
  279. if (p_overview) {
  280. class_desc->pop(); //cell
  281. }
  282. }
  283. void EditorHelp::_add_bulletpoint() {
  284. static const char32_t prefix[3] = { 0x25CF /* filled circle */, ' ', 0 };
  285. class_desc->add_text(String(prefix));
  286. }
  287. Error EditorHelp::_goto_desc(const String &p_class, int p_vscr) {
  288. if (!doc->class_list.has(p_class)) {
  289. return ERR_DOES_NOT_EXIST;
  290. }
  291. select_locked = true;
  292. class_desc->show();
  293. description_line = 0;
  294. if (p_class == edited_class) {
  295. return OK; //already there
  296. }
  297. edited_class = p_class;
  298. _update_doc();
  299. return OK;
  300. }
  301. void EditorHelp::_update_method_list(const Vector<DocData::MethodDoc> p_methods, bool &r_method_descrpitons) {
  302. Ref<Font> doc_code_font = get_theme_font(SNAME("doc_source"), SNAME("EditorFonts"));
  303. class_desc->pop();
  304. class_desc->pop();
  305. class_desc->add_newline();
  306. class_desc->push_font(doc_code_font);
  307. class_desc->push_indent(1);
  308. class_desc->push_table(2);
  309. class_desc->set_table_column_expand(1, true);
  310. bool any_previous = false;
  311. for (int pass = 0; pass < 2; pass++) {
  312. Vector<DocData::MethodDoc> m;
  313. for (int i = 0; i < p_methods.size(); i++) {
  314. const String &q = p_methods[i].qualifiers;
  315. if ((pass == 0 && q.contains("virtual")) || (pass == 1 && !q.contains("virtual"))) {
  316. m.push_back(p_methods[i]);
  317. }
  318. }
  319. if (any_previous && !m.is_empty()) {
  320. class_desc->push_cell();
  321. class_desc->pop(); //cell
  322. class_desc->push_cell();
  323. class_desc->pop(); //cell
  324. }
  325. String group_prefix;
  326. for (int i = 0; i < m.size(); i++) {
  327. const String new_prefix = m[i].name.substr(0, 3);
  328. bool is_new_group = false;
  329. if (i < m.size() - 1 && new_prefix == m[i + 1].name.substr(0, 3) && new_prefix != group_prefix) {
  330. is_new_group = i > 0;
  331. group_prefix = new_prefix;
  332. } else if (!group_prefix.is_empty() && new_prefix != group_prefix) {
  333. is_new_group = true;
  334. group_prefix = "";
  335. }
  336. if (is_new_group && pass == 1) {
  337. class_desc->push_cell();
  338. class_desc->pop(); //cell
  339. class_desc->push_cell();
  340. class_desc->pop(); //cell
  341. }
  342. if (!m[i].description.is_empty() || m[i].errors_returned.size() > 0) {
  343. r_method_descrpitons = true;
  344. }
  345. _add_method(m[i], true);
  346. }
  347. any_previous = !m.is_empty();
  348. }
  349. class_desc->pop(); //table
  350. class_desc->pop();
  351. class_desc->pop(); // font
  352. class_desc->add_newline();
  353. class_desc->add_newline();
  354. }
  355. void EditorHelp::_update_method_descriptions(const DocData::ClassDoc p_classdoc, const Vector<DocData::MethodDoc> p_methods, const String &p_method_type) {
  356. Ref<Font> doc_font = get_theme_font(SNAME("doc"), SNAME("EditorFonts"));
  357. Ref<Font> doc_bold_font = get_theme_font(SNAME("doc_bold"), SNAME("EditorFonts"));
  358. Ref<Font> doc_code_font = get_theme_font(SNAME("doc_source"), SNAME("EditorFonts"));
  359. String link_color_text = title_color.to_html(false);
  360. class_desc->pop();
  361. class_desc->pop();
  362. class_desc->add_newline();
  363. class_desc->add_newline();
  364. for (int pass = 0; pass < 2; pass++) {
  365. Vector<DocData::MethodDoc> methods_filtered;
  366. for (int i = 0; i < p_methods.size(); i++) {
  367. const String &q = p_methods[i].qualifiers;
  368. if ((pass == 0 && q.contains("virtual")) || (pass == 1 && !q.contains("virtual"))) {
  369. methods_filtered.push_back(p_methods[i]);
  370. }
  371. }
  372. for (int i = 0; i < methods_filtered.size(); i++) {
  373. class_desc->push_font(doc_code_font);
  374. _add_method(methods_filtered[i], false);
  375. class_desc->pop();
  376. class_desc->add_newline();
  377. class_desc->add_newline();
  378. class_desc->push_color(text_color);
  379. class_desc->push_font(doc_font);
  380. class_desc->push_indent(1);
  381. if (methods_filtered[i].errors_returned.size()) {
  382. class_desc->append_text(TTR("Error codes returned:"));
  383. class_desc->add_newline();
  384. class_desc->push_list(0, RichTextLabel::LIST_DOTS, false);
  385. for (int j = 0; j < methods_filtered[i].errors_returned.size(); j++) {
  386. if (j > 0) {
  387. class_desc->add_newline();
  388. }
  389. int val = methods_filtered[i].errors_returned[j];
  390. String text = itos(val);
  391. for (int k = 0; k < CoreConstants::get_global_constant_count(); k++) {
  392. if (CoreConstants::get_global_constant_value(k) == val && CoreConstants::get_global_constant_enum(k) == SNAME("Error")) {
  393. text = CoreConstants::get_global_constant_name(k);
  394. break;
  395. }
  396. }
  397. class_desc->push_bold();
  398. class_desc->append_text(text);
  399. class_desc->pop();
  400. }
  401. class_desc->pop();
  402. class_desc->add_newline();
  403. class_desc->add_newline();
  404. }
  405. if (!methods_filtered[i].description.strip_edges().is_empty()) {
  406. _add_text(DTR(methods_filtered[i].description));
  407. } else {
  408. class_desc->add_image(get_theme_icon(SNAME("Error"), SNAME("EditorIcons")));
  409. class_desc->add_text(" ");
  410. class_desc->push_color(comment_color);
  411. if (p_classdoc.is_script_doc) {
  412. class_desc->append_text(TTR("There is currently no description for this " + p_method_type + "."));
  413. } else {
  414. class_desc->append_text(TTR("There is currently no description for this " + p_method_type + ". Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
  415. }
  416. class_desc->pop();
  417. }
  418. class_desc->pop();
  419. class_desc->pop();
  420. class_desc->pop();
  421. class_desc->add_newline();
  422. class_desc->add_newline();
  423. class_desc->add_newline();
  424. }
  425. }
  426. }
  427. void EditorHelp::_update_doc() {
  428. if (!doc->class_list.has(edited_class)) {
  429. return;
  430. }
  431. scroll_locked = true;
  432. class_desc->clear();
  433. method_line.clear();
  434. section_line.clear();
  435. _update_theme();
  436. String link_color_text = title_color.to_html(false);
  437. DocData::ClassDoc cd = doc->class_list[edited_class]; // Make a copy, so we can sort without worrying.
  438. // Class name
  439. section_line.push_back(Pair<String, int>(TTR("Top"), 0));
  440. class_desc->push_font(doc_title_font);
  441. class_desc->push_color(title_color);
  442. class_desc->add_text(TTR("Class:") + " ");
  443. class_desc->push_color(headline_color);
  444. _add_text(edited_class);
  445. class_desc->pop();
  446. class_desc->pop();
  447. class_desc->pop();
  448. class_desc->add_newline();
  449. // Inheritance tree
  450. // Ascendents
  451. if (!cd.inherits.is_empty()) {
  452. class_desc->push_color(title_color);
  453. class_desc->push_font(doc_font);
  454. class_desc->add_text(TTR("Inherits:") + " ");
  455. String inherits = cd.inherits;
  456. while (!inherits.is_empty()) {
  457. _add_type(inherits);
  458. inherits = doc->class_list[inherits].inherits;
  459. if (!inherits.is_empty()) {
  460. class_desc->add_text(" < ");
  461. }
  462. }
  463. class_desc->pop();
  464. class_desc->pop();
  465. class_desc->add_newline();
  466. }
  467. // Descendents
  468. if (cd.is_script_doc || ClassDB::class_exists(cd.name)) {
  469. bool found = false;
  470. bool prev = false;
  471. class_desc->push_font(doc_font);
  472. for (const KeyValue<String, DocData::ClassDoc> &E : doc->class_list) {
  473. if (E.value.inherits == cd.name) {
  474. if (!found) {
  475. class_desc->push_color(title_color);
  476. class_desc->add_text(TTR("Inherited by:") + " ");
  477. found = true;
  478. }
  479. if (prev) {
  480. class_desc->add_text(" , ");
  481. }
  482. _add_type(E.value.name);
  483. prev = true;
  484. }
  485. }
  486. class_desc->pop();
  487. if (found) {
  488. class_desc->pop();
  489. class_desc->add_newline();
  490. }
  491. }
  492. class_desc->add_newline();
  493. class_desc->add_newline();
  494. // Brief description
  495. if (!cd.brief_description.is_empty()) {
  496. class_desc->push_color(text_color);
  497. class_desc->push_font(doc_bold_font);
  498. class_desc->push_indent(1);
  499. _add_text(DTR(cd.brief_description));
  500. class_desc->pop();
  501. class_desc->pop();
  502. class_desc->pop();
  503. class_desc->add_newline();
  504. class_desc->add_newline();
  505. class_desc->add_newline();
  506. }
  507. // Class description
  508. if (!cd.description.is_empty()) {
  509. section_line.push_back(Pair<String, int>(TTR("Description"), class_desc->get_line_count() - 2));
  510. description_line = class_desc->get_line_count() - 2;
  511. class_desc->push_color(title_color);
  512. class_desc->push_font(doc_title_font);
  513. class_desc->add_text(TTR("Description"));
  514. class_desc->pop();
  515. class_desc->pop();
  516. class_desc->add_newline();
  517. class_desc->add_newline();
  518. class_desc->push_color(text_color);
  519. class_desc->push_font(doc_font);
  520. class_desc->push_indent(1);
  521. _add_text(DTR(cd.description));
  522. class_desc->pop();
  523. class_desc->pop();
  524. class_desc->pop();
  525. class_desc->add_newline();
  526. class_desc->add_newline();
  527. class_desc->add_newline();
  528. }
  529. // Online tutorials
  530. if (cd.tutorials.size()) {
  531. class_desc->push_color(title_color);
  532. class_desc->push_font(doc_title_font);
  533. class_desc->add_text(TTR("Online Tutorials"));
  534. class_desc->pop();
  535. class_desc->pop();
  536. class_desc->push_indent(1);
  537. class_desc->push_font(doc_code_font);
  538. class_desc->add_newline();
  539. for (int i = 0; i < cd.tutorials.size(); i++) {
  540. const String link = DTR(cd.tutorials[i].link);
  541. String linktxt = (cd.tutorials[i].title.is_empty()) ? link : DTR(cd.tutorials[i].title);
  542. const int seppos = linktxt.find("//");
  543. if (seppos != -1) {
  544. linktxt = link.substr(seppos + 2);
  545. }
  546. class_desc->push_color(symbol_color);
  547. class_desc->append_text("[url=" + link + "]" + linktxt + "[/url]");
  548. class_desc->pop();
  549. class_desc->add_newline();
  550. }
  551. class_desc->pop();
  552. class_desc->pop();
  553. class_desc->add_newline();
  554. class_desc->add_newline();
  555. }
  556. // Properties overview
  557. Set<String> skip_methods;
  558. bool property_descr = false;
  559. bool has_properties = cd.properties.size() != 0;
  560. if (cd.is_script_doc) {
  561. has_properties = false;
  562. for (int i = 0; i < cd.properties.size(); i++) {
  563. if (cd.properties[i].name.begins_with("_") && cd.properties[i].description.is_empty()) {
  564. continue;
  565. }
  566. has_properties = true;
  567. break;
  568. }
  569. }
  570. if (has_properties) {
  571. section_line.push_back(Pair<String, int>(TTR("Properties"), class_desc->get_line_count() - 2));
  572. class_desc->push_color(title_color);
  573. class_desc->push_font(doc_title_font);
  574. class_desc->add_text(TTR("Properties"));
  575. class_desc->pop();
  576. class_desc->pop();
  577. class_desc->add_newline();
  578. class_desc->push_font(doc_code_font);
  579. class_desc->push_indent(1);
  580. class_desc->push_table(4);
  581. class_desc->set_table_column_expand(1, true);
  582. for (int i = 0; i < cd.properties.size(); i++) {
  583. // Ignore undocumented private.
  584. if (cd.properties[i].name.begins_with("_") && cd.properties[i].description.is_empty()) {
  585. continue;
  586. }
  587. property_line[cd.properties[i].name] = class_desc->get_line_count() - 2; //gets overridden if description
  588. // Property type.
  589. class_desc->push_cell();
  590. class_desc->push_paragraph(HORIZONTAL_ALIGNMENT_RIGHT, Control::TEXT_DIRECTION_AUTO, "");
  591. class_desc->push_font(doc_code_font);
  592. _add_type(cd.properties[i].type, cd.properties[i].enumeration);
  593. class_desc->pop();
  594. class_desc->pop();
  595. class_desc->pop(); // cell
  596. bool describe = false;
  597. if (!cd.properties[i].setter.is_empty()) {
  598. skip_methods.insert(cd.properties[i].setter);
  599. describe = true;
  600. }
  601. if (!cd.properties[i].getter.is_empty()) {
  602. skip_methods.insert(cd.properties[i].getter);
  603. describe = true;
  604. }
  605. if (!cd.properties[i].description.is_empty()) {
  606. describe = true;
  607. }
  608. if (cd.properties[i].overridden) {
  609. describe = false;
  610. }
  611. // Property name.
  612. class_desc->push_cell();
  613. class_desc->push_font(doc_code_font);
  614. class_desc->push_color(headline_color);
  615. if (describe) {
  616. class_desc->push_meta("@member " + cd.properties[i].name);
  617. }
  618. _add_text(cd.properties[i].name);
  619. if (describe) {
  620. class_desc->pop();
  621. property_descr = true;
  622. }
  623. class_desc->pop();
  624. class_desc->pop();
  625. class_desc->pop(); // cell
  626. // Property value.
  627. class_desc->push_cell();
  628. class_desc->push_font(doc_code_font);
  629. if (!cd.properties[i].default_value.is_empty()) {
  630. class_desc->push_color(symbol_color);
  631. if (cd.properties[i].overridden) {
  632. class_desc->add_text(" [");
  633. class_desc->push_meta("@member " + cd.properties[i].overrides + "." + cd.properties[i].name);
  634. _add_text(vformat(TTR("overrides %s:"), cd.properties[i].overrides));
  635. class_desc->pop();
  636. class_desc->add_text(" ");
  637. } else {
  638. class_desc->add_text(" [" + TTR("default:") + " ");
  639. }
  640. class_desc->pop();
  641. class_desc->push_color(value_color);
  642. _add_text(_fix_constant(cd.properties[i].default_value));
  643. class_desc->pop();
  644. class_desc->push_color(symbol_color);
  645. class_desc->add_text("]");
  646. class_desc->pop();
  647. }
  648. class_desc->pop();
  649. class_desc->pop(); // cell
  650. // Property setters and getters.
  651. class_desc->push_cell();
  652. class_desc->push_font(doc_code_font);
  653. if (cd.is_script_doc && (!cd.properties[i].setter.is_empty() || !cd.properties[i].getter.is_empty())) {
  654. class_desc->push_color(symbol_color);
  655. class_desc->add_text(" [" + TTR("property:") + " ");
  656. class_desc->pop(); // color
  657. if (!cd.properties[i].setter.is_empty()) {
  658. class_desc->push_color(value_color);
  659. class_desc->add_text("setter");
  660. class_desc->pop(); // color
  661. }
  662. if (!cd.properties[i].getter.is_empty()) {
  663. if (!cd.properties[i].setter.is_empty()) {
  664. class_desc->push_color(symbol_color);
  665. class_desc->add_text(", ");
  666. class_desc->pop(); // color
  667. }
  668. class_desc->push_color(value_color);
  669. class_desc->add_text("getter");
  670. class_desc->pop(); // color
  671. }
  672. class_desc->push_color(symbol_color);
  673. class_desc->add_text("]");
  674. class_desc->pop(); // color
  675. }
  676. class_desc->pop();
  677. class_desc->pop(); // cell
  678. }
  679. class_desc->pop(); // table
  680. class_desc->pop();
  681. class_desc->pop(); // font
  682. class_desc->add_newline();
  683. class_desc->add_newline();
  684. }
  685. // Methods overview
  686. bool constructor_descriptions = false;
  687. bool method_descriptions = false;
  688. bool operator_descriptions = false;
  689. bool sort_methods = EditorSettings::get_singleton()->get("text_editor/help/sort_functions_alphabetically");
  690. Vector<DocData::MethodDoc> methods;
  691. for (int i = 0; i < cd.methods.size(); i++) {
  692. if (skip_methods.has(cd.methods[i].name)) {
  693. if (cd.methods[i].arguments.size() == 0 /* getter */ || (cd.methods[i].arguments.size() == 1 && cd.methods[i].return_type == "void" /* setter */)) {
  694. continue;
  695. }
  696. }
  697. // Ignore undocumented non virtual private.
  698. if (cd.methods[i].name.begins_with("_") && cd.methods[i].description.is_empty() && !cd.methods[i].qualifiers.contains("virtual")) {
  699. continue;
  700. }
  701. methods.push_back(cd.methods[i]);
  702. }
  703. if (!cd.constructors.is_empty()) {
  704. if (sort_methods) {
  705. cd.constructors.sort();
  706. }
  707. section_line.push_back(Pair<String, int>(TTR("Constructors"), class_desc->get_line_count() - 2));
  708. class_desc->push_color(title_color);
  709. class_desc->push_font(doc_title_font);
  710. class_desc->add_text(TTR("Constructors"));
  711. _update_method_list(cd.constructors, constructor_descriptions);
  712. }
  713. if (!methods.is_empty()) {
  714. if (sort_methods) {
  715. methods.sort();
  716. }
  717. section_line.push_back(Pair<String, int>(TTR("Methods"), class_desc->get_line_count() - 2));
  718. class_desc->push_color(title_color);
  719. class_desc->push_font(doc_title_font);
  720. class_desc->add_text(TTR("Methods"));
  721. _update_method_list(methods, method_descriptions);
  722. }
  723. if (!cd.operators.is_empty()) {
  724. if (sort_methods) {
  725. cd.operators.sort();
  726. }
  727. section_line.push_back(Pair<String, int>(TTR("Operators"), class_desc->get_line_count() - 2));
  728. class_desc->push_color(title_color);
  729. class_desc->push_font(doc_title_font);
  730. class_desc->add_text(TTR("Operators"));
  731. _update_method_list(cd.operators, operator_descriptions);
  732. }
  733. // Theme properties
  734. if (!cd.theme_properties.is_empty()) {
  735. section_line.push_back(Pair<String, int>(TTR("Theme Properties"), class_desc->get_line_count() - 2));
  736. class_desc->push_color(title_color);
  737. class_desc->push_font(doc_title_font);
  738. class_desc->add_text(TTR("Theme Properties"));
  739. class_desc->pop();
  740. class_desc->pop();
  741. class_desc->add_newline();
  742. class_desc->add_newline();
  743. class_desc->push_indent(1);
  744. String theme_data_type;
  745. Map<String, String> data_type_names;
  746. data_type_names["color"] = TTR("Colors");
  747. data_type_names["constant"] = TTR("Constants");
  748. data_type_names["font"] = TTR("Fonts");
  749. data_type_names["font_size"] = TTR("Font Sizes");
  750. data_type_names["icon"] = TTR("Icons");
  751. data_type_names["style"] = TTR("Styles");
  752. for (int i = 0; i < cd.theme_properties.size(); i++) {
  753. theme_property_line[cd.theme_properties[i].name] = class_desc->get_line_count() - 2; //gets overridden if description
  754. if (theme_data_type != cd.theme_properties[i].data_type) {
  755. theme_data_type = cd.theme_properties[i].data_type;
  756. class_desc->push_color(title_color);
  757. class_desc->push_font(doc_title_font);
  758. if (data_type_names.has(theme_data_type)) {
  759. class_desc->add_text(data_type_names[theme_data_type]);
  760. } else {
  761. class_desc->add_text("");
  762. }
  763. class_desc->pop();
  764. class_desc->pop();
  765. class_desc->add_newline();
  766. class_desc->add_newline();
  767. }
  768. // Theme item header.
  769. class_desc->push_font(doc_code_font);
  770. _add_bulletpoint();
  771. // Theme item object type.
  772. _add_type(cd.theme_properties[i].type);
  773. // Theme item name.
  774. class_desc->push_color(headline_color);
  775. class_desc->add_text(" ");
  776. _add_text(cd.theme_properties[i].name);
  777. class_desc->pop();
  778. // Theme item default value.
  779. if (!cd.theme_properties[i].default_value.is_empty()) {
  780. class_desc->push_color(symbol_color);
  781. class_desc->add_text(" [" + TTR("default:") + " ");
  782. class_desc->pop();
  783. class_desc->push_color(value_color);
  784. _add_text(_fix_constant(cd.theme_properties[i].default_value));
  785. class_desc->pop();
  786. class_desc->push_color(symbol_color);
  787. class_desc->add_text("]");
  788. class_desc->pop();
  789. }
  790. class_desc->pop(); // monofont
  791. // Theme item description.
  792. if (!cd.theme_properties[i].description.is_empty()) {
  793. class_desc->push_font(doc_font);
  794. class_desc->push_color(comment_color);
  795. class_desc->push_indent(1);
  796. _add_text(DTR(cd.theme_properties[i].description));
  797. class_desc->pop(); // indent
  798. class_desc->pop(); // color
  799. class_desc->pop(); // font
  800. }
  801. class_desc->add_newline();
  802. class_desc->add_newline();
  803. }
  804. class_desc->pop();
  805. class_desc->add_newline();
  806. }
  807. // Signals
  808. if (!cd.signals.is_empty()) {
  809. if (sort_methods) {
  810. cd.signals.sort();
  811. }
  812. section_line.push_back(Pair<String, int>(TTR("Signals"), class_desc->get_line_count() - 2));
  813. class_desc->push_color(title_color);
  814. class_desc->push_font(doc_title_font);
  815. class_desc->add_text(TTR("Signals"));
  816. class_desc->pop();
  817. class_desc->pop();
  818. class_desc->add_newline();
  819. class_desc->add_newline();
  820. class_desc->push_indent(1);
  821. for (int i = 0; i < cd.signals.size(); i++) {
  822. signal_line[cd.signals[i].name] = class_desc->get_line_count() - 2; //gets overridden if description
  823. class_desc->push_font(doc_code_font); // monofont
  824. class_desc->push_color(headline_color);
  825. _add_bulletpoint();
  826. _add_text(cd.signals[i].name);
  827. class_desc->pop();
  828. class_desc->push_color(symbol_color);
  829. class_desc->add_text("(");
  830. class_desc->pop();
  831. for (int j = 0; j < cd.signals[i].arguments.size(); j++) {
  832. class_desc->push_color(text_color);
  833. if (j > 0) {
  834. class_desc->add_text(", ");
  835. }
  836. _add_text(cd.signals[i].arguments[j].name);
  837. class_desc->add_text(": ");
  838. _add_type(cd.signals[i].arguments[j].type);
  839. if (!cd.signals[i].arguments[j].default_value.is_empty()) {
  840. class_desc->push_color(symbol_color);
  841. class_desc->add_text(" = ");
  842. class_desc->pop();
  843. _add_text(cd.signals[i].arguments[j].default_value);
  844. }
  845. class_desc->pop();
  846. }
  847. class_desc->push_color(symbol_color);
  848. class_desc->add_text(")");
  849. class_desc->pop();
  850. class_desc->pop(); // end monofont
  851. if (!cd.signals[i].description.is_empty()) {
  852. class_desc->push_font(doc_font);
  853. class_desc->push_color(comment_color);
  854. class_desc->push_indent(1);
  855. _add_text(DTR(cd.signals[i].description));
  856. class_desc->pop(); // indent
  857. class_desc->pop();
  858. class_desc->pop(); // font
  859. }
  860. class_desc->add_newline();
  861. class_desc->add_newline();
  862. }
  863. class_desc->pop();
  864. class_desc->add_newline();
  865. }
  866. // Constants and enums
  867. if (!cd.constants.is_empty()) {
  868. Map<String, Vector<DocData::ConstantDoc>> enums;
  869. Vector<DocData::ConstantDoc> constants;
  870. for (int i = 0; i < cd.constants.size(); i++) {
  871. if (!cd.constants[i].enumeration.is_empty()) {
  872. if (!enums.has(cd.constants[i].enumeration)) {
  873. enums[cd.constants[i].enumeration] = Vector<DocData::ConstantDoc>();
  874. }
  875. enums[cd.constants[i].enumeration].push_back(cd.constants[i]);
  876. } else {
  877. // Ignore undocumented private.
  878. if (cd.constants[i].name.begins_with("_") && cd.constants[i].description.is_empty()) {
  879. continue;
  880. }
  881. constants.push_back(cd.constants[i]);
  882. }
  883. }
  884. // Enums
  885. if (enums.size()) {
  886. section_line.push_back(Pair<String, int>(TTR("Enumerations"), class_desc->get_line_count() - 2));
  887. class_desc->push_color(title_color);
  888. class_desc->push_font(doc_title_font);
  889. class_desc->add_text(TTR("Enumerations"));
  890. class_desc->pop();
  891. class_desc->pop();
  892. class_desc->push_indent(1);
  893. class_desc->add_newline();
  894. for (KeyValue<String, Vector<DocData::ConstantDoc>> &E : enums) {
  895. enum_line[E.key] = class_desc->get_line_count() - 2;
  896. class_desc->push_font(doc_code_font);
  897. class_desc->push_color(title_color);
  898. class_desc->add_text("enum ");
  899. class_desc->pop();
  900. String e = E.key;
  901. if ((e.get_slice_count(".") > 1) && (e.get_slice(".", 0) == edited_class)) {
  902. e = e.get_slice(".", 1);
  903. }
  904. class_desc->push_color(headline_color);
  905. class_desc->add_text(e);
  906. class_desc->pop();
  907. class_desc->pop();
  908. class_desc->push_color(symbol_color);
  909. class_desc->add_text(":");
  910. class_desc->pop();
  911. class_desc->add_newline();
  912. class_desc->add_newline();
  913. // Enum description.
  914. if (e != "@unnamed_enums" && cd.enums.has(e)) {
  915. class_desc->push_color(text_color);
  916. class_desc->push_font(doc_font);
  917. class_desc->push_indent(1);
  918. _add_text(cd.enums[e]);
  919. class_desc->pop();
  920. class_desc->pop();
  921. class_desc->pop();
  922. class_desc->add_newline();
  923. class_desc->add_newline();
  924. }
  925. class_desc->push_indent(1);
  926. Vector<DocData::ConstantDoc> enum_list = E.value;
  927. Map<String, int> enumValuesContainer;
  928. int enumStartingLine = enum_line[E.key];
  929. for (int i = 0; i < enum_list.size(); i++) {
  930. if (cd.name == "@GlobalScope") {
  931. enumValuesContainer[enum_list[i].name] = enumStartingLine;
  932. }
  933. // Add the enum constant line to the constant_line map so we can locate it as a constant
  934. constant_line[enum_list[i].name] = class_desc->get_line_count() - 2;
  935. class_desc->push_font(doc_code_font);
  936. class_desc->push_color(headline_color);
  937. _add_bulletpoint();
  938. _add_text(enum_list[i].name);
  939. class_desc->pop();
  940. class_desc->push_color(symbol_color);
  941. class_desc->add_text(" = ");
  942. class_desc->pop();
  943. class_desc->push_color(value_color);
  944. _add_text(_fix_constant(enum_list[i].value));
  945. class_desc->pop();
  946. class_desc->pop();
  947. class_desc->add_newline();
  948. if (!enum_list[i].description.strip_edges().is_empty()) {
  949. class_desc->push_font(doc_font);
  950. class_desc->push_color(comment_color);
  951. _add_text(DTR(enum_list[i].description));
  952. class_desc->pop();
  953. class_desc->pop();
  954. if (DTR(enum_list[i].description).find("\n") > 0) {
  955. class_desc->add_newline();
  956. }
  957. }
  958. class_desc->add_newline();
  959. }
  960. if (cd.name == "@GlobalScope") {
  961. enum_values_line[E.key] = enumValuesContainer;
  962. }
  963. class_desc->pop();
  964. class_desc->add_newline();
  965. }
  966. class_desc->pop();
  967. class_desc->add_newline();
  968. }
  969. // Constants
  970. if (constants.size()) {
  971. section_line.push_back(Pair<String, int>(TTR("Constants"), class_desc->get_line_count() - 2));
  972. class_desc->push_color(title_color);
  973. class_desc->push_font(doc_title_font);
  974. class_desc->add_text(TTR("Constants"));
  975. class_desc->pop();
  976. class_desc->pop();
  977. class_desc->push_indent(1);
  978. class_desc->add_newline();
  979. for (int i = 0; i < constants.size(); i++) {
  980. constant_line[constants[i].name] = class_desc->get_line_count() - 2;
  981. class_desc->push_font(doc_code_font);
  982. if (constants[i].value.begins_with("Color(") && constants[i].value.ends_with(")")) {
  983. String stripped = constants[i].value.replace(" ", "").replace("Color(", "").replace(")", "");
  984. Vector<float> color = stripped.split_floats(",");
  985. if (color.size() >= 3) {
  986. class_desc->push_color(Color(color[0], color[1], color[2]));
  987. _add_bulletpoint();
  988. class_desc->pop();
  989. }
  990. } else {
  991. _add_bulletpoint();
  992. }
  993. class_desc->push_color(headline_color);
  994. _add_text(constants[i].name);
  995. class_desc->pop();
  996. class_desc->push_color(symbol_color);
  997. class_desc->add_text(" = ");
  998. class_desc->pop();
  999. class_desc->push_color(value_color);
  1000. _add_text(_fix_constant(constants[i].value));
  1001. class_desc->pop();
  1002. class_desc->pop();
  1003. class_desc->add_newline();
  1004. if (!constants[i].description.is_empty()) {
  1005. class_desc->push_font(doc_font);
  1006. class_desc->push_color(comment_color);
  1007. _add_text(DTR(constants[i].description));
  1008. class_desc->pop();
  1009. class_desc->pop();
  1010. if (DTR(constants[i].description).find("\n") > 0) {
  1011. class_desc->add_newline();
  1012. }
  1013. }
  1014. class_desc->add_newline();
  1015. }
  1016. class_desc->pop();
  1017. class_desc->add_newline();
  1018. }
  1019. }
  1020. // Property descriptions
  1021. if (property_descr) {
  1022. section_line.push_back(Pair<String, int>(TTR("Property Descriptions"), class_desc->get_line_count() - 2));
  1023. class_desc->push_color(title_color);
  1024. class_desc->push_font(doc_title_font);
  1025. class_desc->add_text(TTR("Property Descriptions"));
  1026. class_desc->pop();
  1027. class_desc->pop();
  1028. class_desc->add_newline();
  1029. class_desc->add_newline();
  1030. for (int i = 0; i < cd.properties.size(); i++) {
  1031. if (cd.properties[i].overridden) {
  1032. continue;
  1033. }
  1034. property_line[cd.properties[i].name] = class_desc->get_line_count() - 2;
  1035. class_desc->push_table(2);
  1036. class_desc->set_table_column_expand(1, true);
  1037. class_desc->push_cell();
  1038. class_desc->push_font(doc_code_font);
  1039. _add_bulletpoint();
  1040. _add_type(cd.properties[i].type, cd.properties[i].enumeration);
  1041. class_desc->add_text(" ");
  1042. class_desc->pop(); // font
  1043. class_desc->pop(); // cell
  1044. class_desc->push_cell();
  1045. class_desc->push_font(doc_code_font);
  1046. class_desc->push_color(headline_color);
  1047. _add_text(cd.properties[i].name);
  1048. class_desc->pop(); // color
  1049. if (!cd.properties[i].default_value.is_empty()) {
  1050. class_desc->push_color(symbol_color);
  1051. class_desc->add_text(" [" + TTR("default:") + " ");
  1052. class_desc->pop(); // color
  1053. class_desc->push_color(value_color);
  1054. _add_text(_fix_constant(cd.properties[i].default_value));
  1055. class_desc->pop(); // color
  1056. class_desc->push_color(symbol_color);
  1057. class_desc->add_text("]");
  1058. class_desc->pop(); // color
  1059. }
  1060. if (cd.is_script_doc && (!cd.properties[i].setter.is_empty() || !cd.properties[i].getter.is_empty())) {
  1061. class_desc->push_color(symbol_color);
  1062. class_desc->add_text(" [" + TTR("property:") + " ");
  1063. class_desc->pop(); // color
  1064. if (!cd.properties[i].setter.is_empty()) {
  1065. class_desc->push_color(value_color);
  1066. class_desc->add_text("setter");
  1067. class_desc->pop(); // color
  1068. }
  1069. if (!cd.properties[i].getter.is_empty()) {
  1070. if (!cd.properties[i].setter.is_empty()) {
  1071. class_desc->push_color(symbol_color);
  1072. class_desc->add_text(", ");
  1073. class_desc->pop(); // color
  1074. }
  1075. class_desc->push_color(value_color);
  1076. class_desc->add_text("getter");
  1077. class_desc->pop(); // color
  1078. }
  1079. class_desc->push_color(symbol_color);
  1080. class_desc->add_text("]");
  1081. class_desc->pop(); // color
  1082. }
  1083. class_desc->pop(); // font
  1084. class_desc->pop(); // cell
  1085. // Script doc doesn't have setter, getter.
  1086. if (!cd.is_script_doc) {
  1087. Map<String, DocData::MethodDoc> method_map;
  1088. for (int j = 0; j < methods.size(); j++) {
  1089. method_map[methods[j].name] = methods[j];
  1090. }
  1091. if (!cd.properties[i].setter.is_empty()) {
  1092. class_desc->push_cell();
  1093. class_desc->pop(); // cell
  1094. class_desc->push_cell();
  1095. class_desc->push_font(doc_code_font);
  1096. class_desc->push_color(text_color);
  1097. if (method_map[cd.properties[i].setter].arguments.size() > 1) {
  1098. // Setters with additional arguments are exposed in the method list, so we link them here for quick access.
  1099. class_desc->push_meta("@method " + cd.properties[i].setter);
  1100. class_desc->add_text(cd.properties[i].setter + TTR("(value)"));
  1101. class_desc->pop();
  1102. } else {
  1103. class_desc->add_text(cd.properties[i].setter + TTR("(value)"));
  1104. }
  1105. class_desc->pop(); // color
  1106. class_desc->push_color(comment_color);
  1107. class_desc->add_text(" setter");
  1108. class_desc->pop(); // color
  1109. class_desc->pop(); // font
  1110. class_desc->pop(); // cell
  1111. method_line[cd.properties[i].setter] = property_line[cd.properties[i].name];
  1112. }
  1113. if (!cd.properties[i].getter.is_empty()) {
  1114. class_desc->push_cell();
  1115. class_desc->pop(); // cell
  1116. class_desc->push_cell();
  1117. class_desc->push_font(doc_code_font);
  1118. class_desc->push_color(text_color);
  1119. if (method_map[cd.properties[i].getter].arguments.size() > 0) {
  1120. // Getters with additional arguments are exposed in the method list, so we link them here for quick access.
  1121. class_desc->push_meta("@method " + cd.properties[i].getter);
  1122. class_desc->add_text(cd.properties[i].getter + "()");
  1123. class_desc->pop();
  1124. } else {
  1125. class_desc->add_text(cd.properties[i].getter + "()");
  1126. }
  1127. class_desc->pop(); //color
  1128. class_desc->push_color(comment_color);
  1129. class_desc->add_text(" getter");
  1130. class_desc->pop(); //color
  1131. class_desc->pop(); //font
  1132. class_desc->pop(); //cell
  1133. method_line[cd.properties[i].getter] = property_line[cd.properties[i].name];
  1134. }
  1135. }
  1136. class_desc->pop(); // table
  1137. class_desc->add_newline();
  1138. class_desc->add_newline();
  1139. class_desc->push_color(text_color);
  1140. class_desc->push_font(doc_font);
  1141. class_desc->push_indent(1);
  1142. if (!cd.properties[i].description.strip_edges().is_empty()) {
  1143. _add_text(DTR(cd.properties[i].description));
  1144. } else {
  1145. class_desc->add_image(get_theme_icon(SNAME("Error"), SNAME("EditorIcons")));
  1146. class_desc->add_text(" ");
  1147. class_desc->push_color(comment_color);
  1148. if (cd.is_script_doc) {
  1149. class_desc->append_text(TTR("There is currently no description for this property."));
  1150. } else {
  1151. class_desc->append_text(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));
  1152. }
  1153. class_desc->pop();
  1154. }
  1155. class_desc->pop();
  1156. class_desc->pop();
  1157. class_desc->pop();
  1158. class_desc->add_newline();
  1159. class_desc->add_newline();
  1160. class_desc->add_newline();
  1161. }
  1162. }
  1163. // Constructor descriptions
  1164. if (constructor_descriptions) {
  1165. section_line.push_back(Pair<String, int>(TTR("Constructor Descriptions"), class_desc->get_line_count() - 2));
  1166. class_desc->push_color(title_color);
  1167. class_desc->push_font(doc_title_font);
  1168. class_desc->add_text(TTR("Constructor Descriptions"));
  1169. _update_method_descriptions(cd, cd.constructors, "constructor");
  1170. }
  1171. // Method descriptions
  1172. if (method_descriptions) {
  1173. section_line.push_back(Pair<String, int>(TTR("Method Descriptions"), class_desc->get_line_count() - 2));
  1174. class_desc->push_color(title_color);
  1175. class_desc->push_font(doc_title_font);
  1176. class_desc->add_text(TTR("Method Descriptions"));
  1177. _update_method_descriptions(cd, methods, "method");
  1178. }
  1179. // Operator descriptions
  1180. if (operator_descriptions) {
  1181. section_line.push_back(Pair<String, int>(TTR("Operator Descriptions"), class_desc->get_line_count() - 2));
  1182. class_desc->push_color(title_color);
  1183. class_desc->push_font(doc_title_font);
  1184. class_desc->add_text(TTR("Operator Descriptions"));
  1185. _update_method_descriptions(cd, cd.operators, "operator");
  1186. }
  1187. scroll_locked = false;
  1188. }
  1189. void EditorHelp::_request_help(const String &p_string) {
  1190. Error err = _goto_desc(p_string);
  1191. if (err == OK) {
  1192. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  1193. }
  1194. //100 palabras
  1195. }
  1196. void EditorHelp::_help_callback(const String &p_topic) {
  1197. String what = p_topic.get_slice(":", 0);
  1198. String clss = p_topic.get_slice(":", 1);
  1199. String name;
  1200. if (p_topic.get_slice_count(":") == 3) {
  1201. name = p_topic.get_slice(":", 2);
  1202. }
  1203. _request_help(clss); //first go to class
  1204. int line = 0;
  1205. if (what == "class_desc") {
  1206. line = description_line;
  1207. } else if (what == "class_signal") {
  1208. if (signal_line.has(name)) {
  1209. line = signal_line[name];
  1210. }
  1211. } else if (what == "class_method" || what == "class_method_desc") {
  1212. if (method_line.has(name)) {
  1213. line = method_line[name];
  1214. }
  1215. } else if (what == "class_property") {
  1216. if (property_line.has(name)) {
  1217. line = property_line[name];
  1218. }
  1219. } else if (what == "class_enum") {
  1220. if (enum_line.has(name)) {
  1221. line = enum_line[name];
  1222. }
  1223. } else if (what == "class_theme_item") {
  1224. if (theme_property_line.has(name)) {
  1225. line = theme_property_line[name];
  1226. }
  1227. } else if (what == "class_constant") {
  1228. if (constant_line.has(name)) {
  1229. line = constant_line[name];
  1230. }
  1231. } else if (what == "class_global") {
  1232. if (constant_line.has(name)) {
  1233. line = constant_line[name];
  1234. } else if (method_line.has(name)) {
  1235. line = method_line[name];
  1236. } else {
  1237. Map<String, Map<String, int>>::Element *iter = enum_values_line.front();
  1238. while (true) {
  1239. if (iter->value().has(name)) {
  1240. line = iter->value()[name];
  1241. break;
  1242. } else if (iter == enum_values_line.back()) {
  1243. break;
  1244. } else {
  1245. iter = iter->next();
  1246. }
  1247. }
  1248. }
  1249. }
  1250. class_desc->call_deferred(SNAME("scroll_to_paragraph"), line);
  1251. }
  1252. static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt) {
  1253. DocTools *doc = EditorHelp::get_doc_data();
  1254. String base_path;
  1255. Ref<Font> doc_font = p_rt->get_theme_font(SNAME("doc"), SNAME("EditorFonts"));
  1256. Ref<Font> doc_bold_font = p_rt->get_theme_font(SNAME("doc_bold"), SNAME("EditorFonts"));
  1257. Ref<Font> doc_code_font = p_rt->get_theme_font(SNAME("doc_source"), SNAME("EditorFonts"));
  1258. Ref<Font> doc_kbd_font = p_rt->get_theme_font(SNAME("doc_keyboard"), SNAME("EditorFonts"));
  1259. Color headline_color = p_rt->get_theme_color(SNAME("headline_color"), SNAME("EditorHelp"));
  1260. Color link_color = p_rt->get_theme_color(SNAME("link_color"), SNAME("EditorHelp"));
  1261. Color code_color = p_rt->get_theme_color(SNAME("code_color"), SNAME("EditorHelp"));
  1262. Color kbd_color = p_rt->get_theme_color(SNAME("kbd_color"), SNAME("EditorHelp"));
  1263. String bbcode = p_bbcode.dedent().replace("\t", "").replace("\r", "").strip_edges();
  1264. // Select the correct code examples
  1265. switch ((int)EDITOR_GET("text_editor/help/class_reference_examples")) {
  1266. case 0: // GDScript
  1267. bbcode = bbcode.replace("[gdscript]", "[codeblock]");
  1268. bbcode = bbcode.replace("[/gdscript]", "[/codeblock]");
  1269. for (int pos = bbcode.find("[csharp]"); pos != -1; pos = bbcode.find("[csharp]")) {
  1270. int end_pos = bbcode.find("[/csharp]");
  1271. if (end_pos == -1) {
  1272. WARN_PRINT("Unclosed [csharp] block or parse fail in code (search for tag errors)");
  1273. break;
  1274. }
  1275. bbcode = bbcode.left(pos) + bbcode.substr(end_pos + 9); // 9 is length of "[/csharp]".
  1276. while (bbcode[pos] == '\n') {
  1277. bbcode = bbcode.left(pos) + bbcode.substr(pos + 1);
  1278. }
  1279. }
  1280. break;
  1281. case 1: // C#
  1282. bbcode = bbcode.replace("[csharp]", "[codeblock]");
  1283. bbcode = bbcode.replace("[/csharp]", "[/codeblock]");
  1284. for (int pos = bbcode.find("[gdscript]"); pos != -1; pos = bbcode.find("[gdscript]")) {
  1285. int end_pos = bbcode.find("[/gdscript]");
  1286. if (end_pos == -1) {
  1287. WARN_PRINT("Unclosed [gdscript] block or parse fail in code (search for tag errors)");
  1288. break;
  1289. }
  1290. bbcode = bbcode.left(pos) + bbcode.substr(end_pos + 11); // 11 is length of "[/gdscript]".
  1291. while (bbcode[pos] == '\n') {
  1292. bbcode = bbcode.left(pos) + bbcode.substr(pos + 1);
  1293. }
  1294. }
  1295. break;
  1296. case 2: // GDScript and C#
  1297. bbcode = bbcode.replace("[csharp]", "[b]C#:[/b]\n[codeblock]");
  1298. bbcode = bbcode.replace("[gdscript]", "[b]GDScript:[/b]\n[codeblock]");
  1299. bbcode = bbcode.replace("[/csharp]", "[/codeblock]");
  1300. bbcode = bbcode.replace("[/gdscript]", "[/codeblock]");
  1301. break;
  1302. }
  1303. // Remove codeblocks (they would be printed otherwise)
  1304. bbcode = bbcode.replace("[codeblocks]\n", "");
  1305. bbcode = bbcode.replace("\n[/codeblocks]", "");
  1306. bbcode = bbcode.replace("[codeblocks]", "");
  1307. bbcode = bbcode.replace("[/codeblocks]", "");
  1308. // remove extra new lines around code blocks
  1309. bbcode = bbcode.replace("[codeblock]\n", "[codeblock]");
  1310. bbcode = bbcode.replace("\n[/codeblock]", "[/codeblock]");
  1311. List<String> tag_stack;
  1312. bool code_tag = false;
  1313. int pos = 0;
  1314. while (pos < bbcode.length()) {
  1315. int brk_pos = bbcode.find("[", pos);
  1316. if (brk_pos < 0) {
  1317. brk_pos = bbcode.length();
  1318. }
  1319. if (brk_pos > pos) {
  1320. String text = bbcode.substr(pos, brk_pos - pos);
  1321. if (!code_tag) {
  1322. text = text.replace("\n", "\n\n");
  1323. }
  1324. p_rt->add_text(text);
  1325. }
  1326. if (brk_pos == bbcode.length()) {
  1327. break; //nothing else to add
  1328. }
  1329. int brk_end = bbcode.find("]", brk_pos + 1);
  1330. if (brk_end == -1) {
  1331. String text = bbcode.substr(brk_pos, bbcode.length() - brk_pos);
  1332. if (!code_tag) {
  1333. text = text.replace("\n", "\n\n");
  1334. }
  1335. p_rt->add_text(text);
  1336. break;
  1337. }
  1338. String tag = bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
  1339. if (tag.begins_with("/")) {
  1340. bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length());
  1341. if (!tag_ok) {
  1342. p_rt->add_text("[");
  1343. pos = brk_pos + 1;
  1344. continue;
  1345. }
  1346. tag_stack.pop_front();
  1347. pos = brk_end + 1;
  1348. if (tag != "/img") {
  1349. p_rt->pop();
  1350. if (code_tag) {
  1351. p_rt->pop();
  1352. }
  1353. }
  1354. code_tag = false;
  1355. } else if (code_tag) {
  1356. p_rt->add_text("[");
  1357. pos = brk_pos + 1;
  1358. } else if (tag.begins_with("method ") || tag.begins_with("member ") || tag.begins_with("signal ") || tag.begins_with("enum ") || tag.begins_with("constant ") || tag.begins_with("theme_item ")) {
  1359. const int tag_end = tag.find(" ");
  1360. const String link_tag = tag.substr(0, tag_end);
  1361. const String link_target = tag.substr(tag_end + 1, tag.length()).lstrip(" ");
  1362. p_rt->push_font(doc_code_font);
  1363. p_rt->push_color(link_color);
  1364. p_rt->push_meta("@" + link_tag + " " + link_target);
  1365. p_rt->add_text(link_target + (tag.begins_with("method ") ? "()" : ""));
  1366. p_rt->pop();
  1367. p_rt->pop();
  1368. p_rt->pop();
  1369. pos = brk_end + 1;
  1370. } else if (doc->class_list.has(tag)) {
  1371. // Class reference tag such as [Node2D] or [SceneTree].
  1372. p_rt->push_font(doc_code_font);
  1373. p_rt->push_color(link_color);
  1374. p_rt->push_meta("#" + tag);
  1375. p_rt->add_text(tag);
  1376. p_rt->pop();
  1377. p_rt->pop();
  1378. p_rt->pop();
  1379. pos = brk_end + 1;
  1380. } else if (tag == "b") {
  1381. //use bold font
  1382. p_rt->push_font(doc_bold_font);
  1383. pos = brk_end + 1;
  1384. tag_stack.push_front(tag);
  1385. } else if (tag == "i") {
  1386. //use italics font
  1387. p_rt->push_color(headline_color);
  1388. pos = brk_end + 1;
  1389. tag_stack.push_front(tag);
  1390. } else if (tag == "code" || tag == "codeblock") {
  1391. //use monospace font
  1392. p_rt->push_font(doc_code_font);
  1393. p_rt->push_color(code_color);
  1394. code_tag = true;
  1395. pos = brk_end + 1;
  1396. tag_stack.push_front(tag);
  1397. } else if (tag == "kbd") {
  1398. //use keyboard font with custom color
  1399. p_rt->push_font(doc_kbd_font);
  1400. p_rt->push_color(kbd_color);
  1401. code_tag = true; // though not strictly a code tag, logic is similar
  1402. pos = brk_end + 1;
  1403. tag_stack.push_front(tag);
  1404. } else if (tag == "center") {
  1405. //align to center
  1406. p_rt->push_paragraph(HORIZONTAL_ALIGNMENT_CENTER, Control::TEXT_DIRECTION_AUTO, "");
  1407. pos = brk_end + 1;
  1408. tag_stack.push_front(tag);
  1409. } else if (tag == "br") {
  1410. //force a line break
  1411. p_rt->add_newline();
  1412. pos = brk_end + 1;
  1413. } else if (tag == "u") {
  1414. //use underline
  1415. p_rt->push_underline();
  1416. pos = brk_end + 1;
  1417. tag_stack.push_front(tag);
  1418. } else if (tag == "s") {
  1419. //use strikethrough
  1420. p_rt->push_strikethrough();
  1421. pos = brk_end + 1;
  1422. tag_stack.push_front(tag);
  1423. } else if (tag == "url") {
  1424. int end = bbcode.find("[", brk_end);
  1425. if (end == -1) {
  1426. end = bbcode.length();
  1427. }
  1428. String url = bbcode.substr(brk_end + 1, end - brk_end - 1);
  1429. p_rt->push_meta(url);
  1430. pos = brk_end + 1;
  1431. tag_stack.push_front(tag);
  1432. } else if (tag.begins_with("url=")) {
  1433. String url = tag.substr(4, tag.length());
  1434. p_rt->push_meta(url);
  1435. pos = brk_end + 1;
  1436. tag_stack.push_front("url");
  1437. } else if (tag == "img") {
  1438. int end = bbcode.find("[", brk_end);
  1439. if (end == -1) {
  1440. end = bbcode.length();
  1441. }
  1442. String image = bbcode.substr(brk_end + 1, end - brk_end - 1);
  1443. Ref<Texture2D> texture = ResourceLoader::load(base_path.plus_file(image), "Texture2D");
  1444. if (texture.is_valid()) {
  1445. p_rt->add_image(texture);
  1446. }
  1447. pos = end;
  1448. tag_stack.push_front(tag);
  1449. } else if (tag.begins_with("color=")) {
  1450. String col = tag.substr(6, tag.length());
  1451. Color color = Color::from_string(col, Color());
  1452. p_rt->push_color(color);
  1453. pos = brk_end + 1;
  1454. tag_stack.push_front("color");
  1455. } else if (tag.begins_with("font=")) {
  1456. String fnt = tag.substr(5, tag.length());
  1457. Ref<Font> font = ResourceLoader::load(base_path.plus_file(fnt), "Font");
  1458. if (font.is_valid()) {
  1459. p_rt->push_font(font);
  1460. } else {
  1461. p_rt->push_font(doc_font);
  1462. }
  1463. pos = brk_end + 1;
  1464. tag_stack.push_front("font");
  1465. } else {
  1466. p_rt->add_text("["); //ignore
  1467. pos = brk_pos + 1;
  1468. }
  1469. }
  1470. }
  1471. void EditorHelp::_add_text(const String &p_bbcode) {
  1472. _add_text_to_rt(p_bbcode, class_desc);
  1473. }
  1474. Thread EditorHelp::thread;
  1475. void EditorHelp::_wait_for_thread() {
  1476. if (thread.is_started()) {
  1477. thread.wait_to_finish();
  1478. }
  1479. }
  1480. void EditorHelp::_gen_doc_thread(void *p_udata) {
  1481. DocTools compdoc;
  1482. compdoc.load_compressed(_doc_data_compressed, _doc_data_compressed_size, _doc_data_uncompressed_size);
  1483. doc->merge_from(compdoc); //ensure all is up to date
  1484. }
  1485. static bool doc_gen_use_threads = true;
  1486. void EditorHelp::generate_doc() {
  1487. doc = memnew(DocTools);
  1488. // Not doable on threads unfortunately, since it instantiates all sorts of classes to get default values.
  1489. doc->generate(true);
  1490. if (doc_gen_use_threads) {
  1491. thread.start(_gen_doc_thread, nullptr);
  1492. } else {
  1493. _gen_doc_thread(nullptr);
  1494. }
  1495. }
  1496. void EditorHelp::_toggle_scripts_pressed() {
  1497. ScriptEditor::get_singleton()->toggle_scripts_panel();
  1498. update_toggle_scripts_button();
  1499. }
  1500. void EditorHelp::_notification(int p_what) {
  1501. switch (p_what) {
  1502. case NOTIFICATION_READY:
  1503. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1504. _wait_for_thread();
  1505. _update_doc();
  1506. } break;
  1507. case NOTIFICATION_THEME_CHANGED: {
  1508. if (is_inside_tree()) {
  1509. _class_desc_resized(true);
  1510. }
  1511. update_toggle_scripts_button();
  1512. } break;
  1513. case NOTIFICATION_VISIBILITY_CHANGED: {
  1514. update_toggle_scripts_button();
  1515. } break;
  1516. }
  1517. }
  1518. void EditorHelp::go_to_help(const String &p_help) {
  1519. _wait_for_thread();
  1520. _help_callback(p_help);
  1521. }
  1522. void EditorHelp::go_to_class(const String &p_class, int p_scroll) {
  1523. _wait_for_thread();
  1524. _goto_desc(p_class, p_scroll);
  1525. }
  1526. void EditorHelp::update_doc() {
  1527. _wait_for_thread();
  1528. ERR_FAIL_COND(!doc->class_list.has(edited_class));
  1529. ERR_FAIL_COND(!doc->class_list[edited_class].is_script_doc);
  1530. _update_doc();
  1531. }
  1532. void EditorHelp::cleanup_doc() {
  1533. _wait_for_thread();
  1534. if (doc_gen_use_threads) {
  1535. thread.wait_to_finish();
  1536. }
  1537. memdelete(doc);
  1538. }
  1539. Vector<Pair<String, int>> EditorHelp::get_sections() {
  1540. _wait_for_thread();
  1541. Vector<Pair<String, int>> sections;
  1542. for (int i = 0; i < section_line.size(); i++) {
  1543. sections.push_back(Pair<String, int>(section_line[i].first, i));
  1544. }
  1545. return sections;
  1546. }
  1547. void EditorHelp::scroll_to_section(int p_section_index) {
  1548. _wait_for_thread();
  1549. int line = section_line[p_section_index].second;
  1550. class_desc->scroll_to_paragraph(line);
  1551. }
  1552. void EditorHelp::popup_search() {
  1553. _wait_for_thread();
  1554. find_bar->popup_search();
  1555. }
  1556. String EditorHelp::get_class() {
  1557. return edited_class;
  1558. }
  1559. void EditorHelp::search_again(bool p_search_previous) {
  1560. _search(p_search_previous);
  1561. }
  1562. int EditorHelp::get_scroll() const {
  1563. return class_desc->get_v_scroll_bar()->get_value();
  1564. }
  1565. void EditorHelp::set_scroll(int p_scroll) {
  1566. class_desc->get_v_scroll_bar()->set_value(p_scroll);
  1567. }
  1568. void EditorHelp::update_toggle_scripts_button() {
  1569. if (is_layout_rtl()) {
  1570. toggle_scripts_button->set_icon(get_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Forward") : SNAME("Back"), SNAME("EditorIcons")));
  1571. } else {
  1572. toggle_scripts_button->set_icon(get_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Back") : SNAME("Forward"), SNAME("EditorIcons")));
  1573. }
  1574. toggle_scripts_button->set_tooltip(vformat("%s (%s)", TTR("Toggle Scripts Panel"), ED_GET_SHORTCUT("script_editor/toggle_scripts_panel")->get_as_text()));
  1575. }
  1576. void EditorHelp::_bind_methods() {
  1577. ClassDB::bind_method("_class_list_select", &EditorHelp::_class_list_select);
  1578. ClassDB::bind_method("_request_help", &EditorHelp::_request_help);
  1579. ClassDB::bind_method("_search", &EditorHelp::_search);
  1580. ClassDB::bind_method("_help_callback", &EditorHelp::_help_callback);
  1581. ADD_SIGNAL(MethodInfo("go_to_help"));
  1582. }
  1583. EditorHelp::EditorHelp() {
  1584. set_custom_minimum_size(Size2(150 * EDSCALE, 0));
  1585. EDITOR_DEF("text_editor/help/sort_functions_alphabetically", true);
  1586. class_desc = memnew(RichTextLabel);
  1587. add_child(class_desc);
  1588. class_desc->set_v_size_flags(SIZE_EXPAND_FILL);
  1589. class_desc->add_theme_color_override("selection_color", get_theme_color(SNAME("accent_color"), SNAME("Editor")) * Color(1, 1, 1, 0.4));
  1590. class_desc->connect("meta_clicked", callable_mp(this, &EditorHelp::_class_desc_select));
  1591. class_desc->connect("gui_input", callable_mp(this, &EditorHelp::_class_desc_input));
  1592. class_desc->connect("resized", callable_mp(this, &EditorHelp::_class_desc_resized), varray(false));
  1593. _class_desc_resized(false);
  1594. // Added second so it opens at the bottom so it won't offset the entire widget.
  1595. find_bar = memnew(FindBar);
  1596. add_child(find_bar);
  1597. find_bar->hide();
  1598. find_bar->set_rich_text_label(class_desc);
  1599. status_bar = memnew(HBoxContainer);
  1600. add_child(status_bar);
  1601. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  1602. status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE));
  1603. toggle_scripts_button = memnew(Button);
  1604. toggle_scripts_button->set_flat(true);
  1605. toggle_scripts_button->connect("pressed", callable_mp(this, &EditorHelp::_toggle_scripts_pressed));
  1606. status_bar->add_child(toggle_scripts_button);
  1607. class_desc->set_selection_enabled(true);
  1608. scroll_locked = false;
  1609. select_locked = false;
  1610. class_desc->hide();
  1611. }
  1612. EditorHelp::~EditorHelp() {
  1613. }
  1614. DocTools *EditorHelp::get_doc_data() {
  1615. _wait_for_thread();
  1616. return doc;
  1617. }
  1618. //// EditorHelpBit ///
  1619. void EditorHelpBit::_go_to_help(String p_what) {
  1620. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  1621. ScriptEditor::get_singleton()->goto_help(p_what);
  1622. emit_signal(SNAME("request_hide"));
  1623. }
  1624. void EditorHelpBit::_meta_clicked(String p_select) {
  1625. if (p_select.begins_with("$")) { //enum
  1626. String select = p_select.substr(1, p_select.length());
  1627. String class_name;
  1628. if (select.contains(".")) {
  1629. class_name = select.get_slice(".", 0);
  1630. } else {
  1631. class_name = "@Global";
  1632. }
  1633. _go_to_help("class_enum:" + class_name + ":" + select);
  1634. return;
  1635. } else if (p_select.begins_with("#")) {
  1636. _go_to_help("class_name:" + p_select.substr(1, p_select.length()));
  1637. return;
  1638. } else if (p_select.begins_with("@")) {
  1639. String m = p_select.substr(1, p_select.length());
  1640. if (m.contains(".")) {
  1641. _go_to_help("class_method:" + m.get_slice(".", 0) + ":" + m.get_slice(".", 0)); //must go somewhere else
  1642. }
  1643. }
  1644. }
  1645. void EditorHelpBit::_bind_methods() {
  1646. ClassDB::bind_method(D_METHOD("set_text", "text"), &EditorHelpBit::set_text);
  1647. ADD_SIGNAL(MethodInfo("request_hide"));
  1648. }
  1649. void EditorHelpBit::_notification(int p_what) {
  1650. switch (p_what) {
  1651. case NOTIFICATION_ENTER_TREE:
  1652. case NOTIFICATION_THEME_CHANGED: {
  1653. rich_text->add_theme_color_override("selection_color", get_theme_color(SNAME("selection_color"), SNAME("EditorHelp")));
  1654. rich_text->clear();
  1655. _add_text_to_rt(text, rich_text);
  1656. rich_text->reset_size(); // Force recalculating size after parsing bbcode.
  1657. } break;
  1658. }
  1659. }
  1660. void EditorHelpBit::set_text(const String &p_text) {
  1661. text = p_text;
  1662. rich_text->clear();
  1663. _add_text_to_rt(text, rich_text);
  1664. }
  1665. EditorHelpBit::EditorHelpBit() {
  1666. rich_text = memnew(RichTextLabel);
  1667. add_child(rich_text);
  1668. rich_text->connect("meta_clicked", callable_mp(this, &EditorHelpBit::_meta_clicked));
  1669. rich_text->set_override_selected_font_color(false);
  1670. rich_text->set_fit_content_height(true);
  1671. set_custom_minimum_size(Size2(0, 50 * EDSCALE));
  1672. }
  1673. //// FindBar ///
  1674. FindBar::FindBar() {
  1675. search_text = memnew(LineEdit);
  1676. add_child(search_text);
  1677. search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  1678. search_text->set_h_size_flags(SIZE_EXPAND_FILL);
  1679. search_text->connect("text_changed", callable_mp(this, &FindBar::_search_text_changed));
  1680. search_text->connect("text_submitted", callable_mp(this, &FindBar::_search_text_submitted));
  1681. matches_label = memnew(Label);
  1682. add_child(matches_label);
  1683. matches_label->hide();
  1684. find_prev = memnew(Button);
  1685. find_prev->set_flat(true);
  1686. add_child(find_prev);
  1687. find_prev->set_focus_mode(FOCUS_NONE);
  1688. find_prev->connect("pressed", callable_mp(this, &FindBar::search_prev));
  1689. find_next = memnew(Button);
  1690. find_next->set_flat(true);
  1691. add_child(find_next);
  1692. find_next->set_focus_mode(FOCUS_NONE);
  1693. find_next->connect("pressed", callable_mp(this, &FindBar::search_next));
  1694. Control *space = memnew(Control);
  1695. add_child(space);
  1696. space->set_custom_minimum_size(Size2(4, 0) * EDSCALE);
  1697. hide_button = memnew(TextureButton);
  1698. add_child(hide_button);
  1699. hide_button->set_focus_mode(FOCUS_NONE);
  1700. hide_button->set_ignore_texture_size(true);
  1701. hide_button->set_stretch_mode(TextureButton::STRETCH_KEEP_CENTERED);
  1702. hide_button->connect("pressed", callable_mp(this, &FindBar::_hide_bar));
  1703. }
  1704. void FindBar::popup_search() {
  1705. show();
  1706. bool grabbed_focus = false;
  1707. if (!search_text->has_focus()) {
  1708. search_text->grab_focus();
  1709. grabbed_focus = true;
  1710. }
  1711. if (!search_text->get_text().is_empty()) {
  1712. search_text->select_all();
  1713. search_text->set_caret_column(search_text->get_text().length());
  1714. if (grabbed_focus) {
  1715. _search();
  1716. }
  1717. }
  1718. }
  1719. void FindBar::_notification(int p_what) {
  1720. switch (p_what) {
  1721. case NOTIFICATION_ENTER_TREE:
  1722. case NOTIFICATION_THEME_CHANGED: {
  1723. find_prev->set_icon(get_theme_icon(SNAME("MoveUp"), SNAME("EditorIcons")));
  1724. find_next->set_icon(get_theme_icon(SNAME("MoveDown"), SNAME("EditorIcons")));
  1725. hide_button->set_normal_texture(get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  1726. hide_button->set_hover_texture(get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  1727. hide_button->set_pressed_texture(get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  1728. hide_button->set_custom_minimum_size(hide_button->get_normal_texture()->get_size());
  1729. matches_label->add_theme_color_override("font_color", results_count > 0 ? get_theme_color(SNAME("font_color"), SNAME("Label")) : get_theme_color(SNAME("error_color"), SNAME("Editor")));
  1730. } break;
  1731. case NOTIFICATION_VISIBILITY_CHANGED: {
  1732. set_process_unhandled_input(is_visible_in_tree());
  1733. } break;
  1734. }
  1735. }
  1736. void FindBar::_bind_methods() {
  1737. ADD_SIGNAL(MethodInfo("search"));
  1738. }
  1739. void FindBar::set_rich_text_label(RichTextLabel *p_rich_text_label) {
  1740. rich_text_label = p_rich_text_label;
  1741. }
  1742. bool FindBar::search_next() {
  1743. return _search();
  1744. }
  1745. bool FindBar::search_prev() {
  1746. return _search(true);
  1747. }
  1748. bool FindBar::_search(bool p_search_previous) {
  1749. String stext = search_text->get_text();
  1750. bool keep = prev_search == stext;
  1751. bool ret = rich_text_label->search(stext, keep, p_search_previous);
  1752. prev_search = stext;
  1753. if (ret) {
  1754. _update_results_count();
  1755. } else {
  1756. results_count = 0;
  1757. }
  1758. _update_matches_label();
  1759. return ret;
  1760. }
  1761. void FindBar::_update_results_count() {
  1762. results_count = 0;
  1763. String searched = search_text->get_text();
  1764. if (searched.is_empty()) {
  1765. return;
  1766. }
  1767. String full_text = rich_text_label->get_parsed_text();
  1768. int from_pos = 0;
  1769. while (true) {
  1770. int pos = full_text.findn(searched, from_pos);
  1771. if (pos == -1) {
  1772. break;
  1773. }
  1774. results_count++;
  1775. from_pos = pos + searched.length();
  1776. }
  1777. }
  1778. void FindBar::_update_matches_label() {
  1779. if (search_text->get_text().is_empty() || results_count == -1) {
  1780. matches_label->hide();
  1781. } else {
  1782. matches_label->show();
  1783. matches_label->add_theme_color_override("font_color", results_count > 0 ? get_theme_color(SNAME("font_color"), SNAME("Label")) : get_theme_color(SNAME("error_color"), SNAME("Editor")));
  1784. matches_label->set_text(vformat(results_count == 1 ? TTR("%d match.") : TTR("%d matches."), results_count));
  1785. }
  1786. }
  1787. void FindBar::_hide_bar() {
  1788. if (search_text->has_focus()) {
  1789. rich_text_label->grab_focus();
  1790. }
  1791. hide();
  1792. }
  1793. void FindBar::unhandled_input(const Ref<InputEvent> &p_event) {
  1794. ERR_FAIL_COND(p_event.is_null());
  1795. Ref<InputEventKey> k = p_event;
  1796. if (k.is_valid()) {
  1797. if (k->is_pressed() && (rich_text_label->has_focus() || is_ancestor_of(get_viewport()->gui_get_focus_owner()))) {
  1798. bool accepted = true;
  1799. switch (k->get_keycode()) {
  1800. case Key::ESCAPE: {
  1801. _hide_bar();
  1802. } break;
  1803. default: {
  1804. accepted = false;
  1805. } break;
  1806. }
  1807. if (accepted) {
  1808. accept_event();
  1809. }
  1810. }
  1811. }
  1812. }
  1813. void FindBar::_search_text_changed(const String &p_text) {
  1814. search_next();
  1815. }
  1816. void FindBar::_search_text_submitted(const String &p_text) {
  1817. if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  1818. search_prev();
  1819. } else {
  1820. search_next();
  1821. }
  1822. }