editor_help.cpp 56 KB

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