editor_help.cpp 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599
  1. /**************************************************************************/
  2. /* editor_help.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #include "scene/gui/line_edit.h"
  41. #define CONTRIBUTE_URL vformat("%s/contributing/documentation/updating_the_class_reference.html", VERSION_DOCS_URL)
  42. DocTools *EditorHelp::doc = nullptr;
  43. void EditorHelp::_update_theme_item_cache() {
  44. VBoxContainer::_update_theme_item_cache();
  45. theme_cache.text_color = get_theme_color(SNAME("text_color"), SNAME("EditorHelp"));
  46. theme_cache.title_color = get_theme_color(SNAME("title_color"), SNAME("EditorHelp"));
  47. theme_cache.headline_color = get_theme_color(SNAME("headline_color"), SNAME("EditorHelp"));
  48. theme_cache.comment_color = get_theme_color(SNAME("comment_color"), SNAME("EditorHelp"));
  49. theme_cache.symbol_color = get_theme_color(SNAME("symbol_color"), SNAME("EditorHelp"));
  50. theme_cache.value_color = get_theme_color(SNAME("value_color"), SNAME("EditorHelp"));
  51. theme_cache.qualifier_color = get_theme_color(SNAME("qualifier_color"), SNAME("EditorHelp"));
  52. theme_cache.type_color = get_theme_color(SNAME("type_color"), SNAME("EditorHelp"));
  53. theme_cache.doc_font = get_theme_font(SNAME("doc"), SNAME("EditorFonts"));
  54. theme_cache.doc_bold_font = get_theme_font(SNAME("doc_bold"), SNAME("EditorFonts"));
  55. theme_cache.doc_italic_font = get_theme_font(SNAME("doc_italic"), SNAME("EditorFonts"));
  56. theme_cache.doc_title_font = get_theme_font(SNAME("doc_title"), SNAME("EditorFonts"));
  57. theme_cache.doc_code_font = get_theme_font(SNAME("doc_source"), SNAME("EditorFonts"));
  58. theme_cache.doc_kbd_font = get_theme_font(SNAME("doc_keyboard"), SNAME("EditorFonts"));
  59. theme_cache.doc_font_size = get_theme_font_size(SNAME("doc_size"), SNAME("EditorFonts"));
  60. theme_cache.doc_title_font_size = get_theme_font_size(SNAME("doc_title_size"), SNAME("EditorFonts"));
  61. theme_cache.doc_code_font_size = get_theme_font_size(SNAME("doc_source_size"), SNAME("EditorFonts"));
  62. theme_cache.doc_kbd_font_size = get_theme_font_size(SNAME("doc_keyboard_size"), SNAME("EditorFonts"));
  63. theme_cache.background_style = get_theme_stylebox(SNAME("background"), SNAME("EditorHelp"));
  64. class_desc->add_theme_font_override("normal_font", theme_cache.doc_font);
  65. class_desc->add_theme_font_size_override("normal_font_size", theme_cache.doc_font_size);
  66. class_desc->add_theme_color_override("selection_color", get_theme_color(SNAME("selection_color"), SNAME("EditorHelp")));
  67. class_desc->add_theme_constant_override("line_separation", get_theme_constant(SNAME("line_separation"), SNAME("EditorHelp")));
  68. class_desc->add_theme_constant_override("table_h_separation", get_theme_constant(SNAME("table_h_separation"), SNAME("EditorHelp")));
  69. class_desc->add_theme_constant_override("table_v_separation", get_theme_constant(SNAME("table_v_separation"), SNAME("EditorHelp")));
  70. class_desc->add_theme_constant_override("text_highlight_h_padding", get_theme_constant(SNAME("text_highlight_h_padding"), SNAME("EditorHelp")));
  71. class_desc->add_theme_constant_override("text_highlight_v_padding", get_theme_constant(SNAME("text_highlight_v_padding"), SNAME("EditorHelp")));
  72. }
  73. void EditorHelp::_search(bool p_search_previous) {
  74. if (p_search_previous) {
  75. find_bar->search_prev();
  76. } else {
  77. find_bar->search_next();
  78. }
  79. }
  80. void EditorHelp::_class_desc_finished() {
  81. if (scroll_to >= 0) {
  82. class_desc->scroll_to_paragraph(scroll_to);
  83. }
  84. scroll_to = -1;
  85. }
  86. void EditorHelp::_class_list_select(const String &p_select) {
  87. _goto_desc(p_select);
  88. }
  89. void EditorHelp::_class_desc_select(const String &p_select) {
  90. if (p_select.begins_with("$")) { //enum
  91. String select = p_select.substr(1, p_select.length());
  92. String class_name;
  93. if (select.contains(".")) {
  94. class_name = select.get_slice(".", 0);
  95. select = select.get_slice(".", 1);
  96. } else {
  97. class_name = "@GlobalScope";
  98. }
  99. emit_signal(SNAME("go_to_help"), "class_enum:" + class_name + ":" + select);
  100. return;
  101. } else if (p_select.begins_with("#")) {
  102. emit_signal(SNAME("go_to_help"), "class_name:" + p_select.substr(1, p_select.length()));
  103. return;
  104. } else if (p_select.begins_with("@")) {
  105. int tag_end = p_select.find(" ");
  106. String tag = p_select.substr(1, tag_end - 1);
  107. String link = p_select.substr(tag_end + 1, p_select.length()).lstrip(" ");
  108. String topic;
  109. HashMap<String, int> *table = nullptr;
  110. if (tag == "method") {
  111. topic = "class_method";
  112. table = &this->method_line;
  113. } else if (tag == "member") {
  114. topic = "class_property";
  115. table = &this->property_line;
  116. } else if (tag == "enum") {
  117. topic = "class_enum";
  118. table = &this->enum_line;
  119. } else if (tag == "signal") {
  120. topic = "class_signal";
  121. table = &this->signal_line;
  122. } else if (tag == "constant") {
  123. topic = "class_constant";
  124. table = &this->constant_line;
  125. } else if (tag == "annotation") {
  126. topic = "class_annotation";
  127. table = &this->annotation_line;
  128. } else if (tag == "theme_item") {
  129. topic = "theme_item";
  130. table = &this->theme_property_line;
  131. } else {
  132. return;
  133. }
  134. // Case order is important here to correctly handle edge cases like Variant.Type in @GlobalScope.
  135. if (table->has(link)) {
  136. // Found in the current page.
  137. if (class_desc->is_ready()) {
  138. class_desc->scroll_to_paragraph((*table)[link]);
  139. } else {
  140. scroll_to = (*table)[link];
  141. }
  142. } else {
  143. // Look for link in @GlobalScope.
  144. // Note that a link like @GlobalScope.enum_name will not be found in this section, only enum_name will be.
  145. if (topic == "class_enum") {
  146. const DocData::ClassDoc &cd = doc->class_list["@GlobalScope"];
  147. for (int i = 0; i < cd.constants.size(); i++) {
  148. if (cd.constants[i].enumeration == link) {
  149. // Found in @GlobalScope.
  150. emit_signal(SNAME("go_to_help"), topic + ":@GlobalScope:" + link);
  151. return;
  152. }
  153. }
  154. } else if (topic == "class_constant") {
  155. const DocData::ClassDoc &cd = doc->class_list["@GlobalScope"];
  156. for (int i = 0; i < cd.constants.size(); i++) {
  157. if (cd.constants[i].name == link) {
  158. // Found in @GlobalScope.
  159. emit_signal(SNAME("go_to_help"), topic + ":@GlobalScope:" + link);
  160. return;
  161. }
  162. }
  163. }
  164. if (link.contains(".")) {
  165. int class_end = link.find(".");
  166. emit_signal(SNAME("go_to_help"), topic + ":" + link.substr(0, class_end) + ":" + link.substr(class_end + 1, link.length()));
  167. }
  168. }
  169. } else if (p_select.begins_with("http")) {
  170. OS::get_singleton()->shell_open(p_select);
  171. }
  172. }
  173. void EditorHelp::_class_desc_input(const Ref<InputEvent> &p_input) {
  174. }
  175. void EditorHelp::_class_desc_resized(bool p_force_update_theme) {
  176. // Add extra horizontal margins for better readability.
  177. // The margins increase as the width of the editor help container increases.
  178. real_t char_width = theme_cache.doc_code_font->get_char_size('x', theme_cache.doc_code_font_size).width;
  179. const int new_display_margin = MAX(30 * EDSCALE, get_parent_anchorable_rect().size.width - char_width * 120 * EDSCALE) * 0.5;
  180. if (display_margin != new_display_margin || p_force_update_theme) {
  181. display_margin = new_display_margin;
  182. Ref<StyleBox> class_desc_stylebox = theme_cache.background_style->duplicate();
  183. class_desc_stylebox->set_content_margin(SIDE_LEFT, display_margin);
  184. class_desc_stylebox->set_content_margin(SIDE_RIGHT, display_margin);
  185. class_desc->add_theme_style_override("normal", class_desc_stylebox);
  186. class_desc->add_theme_style_override("focused", class_desc_stylebox);
  187. }
  188. }
  189. void EditorHelp::_add_type(const String &p_type, const String &p_enum) {
  190. if (p_type.is_empty() || p_type == "void") {
  191. class_desc->push_color(Color(theme_cache.type_color, 0.5));
  192. class_desc->push_hint(TTR("No return value."));
  193. class_desc->add_text("void");
  194. class_desc->pop();
  195. class_desc->pop();
  196. return;
  197. }
  198. bool is_enum_type = !p_enum.is_empty();
  199. bool can_ref = !p_type.contains("*") || is_enum_type;
  200. String t = p_type;
  201. if (is_enum_type) {
  202. if (p_enum.get_slice_count(".") > 1) {
  203. t = p_enum.get_slice(".", 1);
  204. } else {
  205. t = p_enum.get_slice(".", 0);
  206. }
  207. }
  208. class_desc->push_color(theme_cache.type_color);
  209. bool add_array = false;
  210. if (can_ref) {
  211. if (t.ends_with("[]")) {
  212. add_array = true;
  213. t = t.replace("[]", "");
  214. class_desc->push_meta("#Array"); //class
  215. class_desc->add_text("Array");
  216. class_desc->pop();
  217. class_desc->add_text("[");
  218. }
  219. if (is_enum_type) {
  220. class_desc->push_meta("$" + p_enum); //class
  221. } else {
  222. class_desc->push_meta("#" + t); //class
  223. }
  224. }
  225. class_desc->add_text(t);
  226. if (can_ref) {
  227. class_desc->pop(); // Pushed meta above.
  228. if (add_array) {
  229. class_desc->add_text("]");
  230. }
  231. }
  232. class_desc->pop();
  233. }
  234. void EditorHelp::_add_type_icon(const String &p_type, int p_size) {
  235. Ref<Texture2D> icon;
  236. if (has_theme_icon(p_type, SNAME("EditorIcons"))) {
  237. icon = get_theme_icon(p_type, SNAME("EditorIcons"));
  238. } else if (ClassDB::class_exists(p_type) && ClassDB::is_parent_class(p_type, "Object")) {
  239. icon = get_theme_icon(SNAME("Object"), SNAME("EditorIcons"));
  240. } else {
  241. icon = get_theme_icon(SNAME("ArrowRight"), SNAME("EditorIcons"));
  242. }
  243. Vector2i size = Vector2i(icon->get_width(), icon->get_height());
  244. if (p_size > 0) {
  245. // Ensures icon scales proportionally on both axis, based on icon height.
  246. float ratio = p_size / float(size.height);
  247. size.width *= ratio;
  248. size.height *= ratio;
  249. }
  250. class_desc->add_image(icon, size.width, size.height);
  251. }
  252. String EditorHelp::_fix_constant(const String &p_constant) const {
  253. if (p_constant.strip_edges() == "4294967295") {
  254. return "0xFFFFFFFF";
  255. }
  256. if (p_constant.strip_edges() == "2147483647") {
  257. return "0x7FFFFFFF";
  258. }
  259. if (p_constant.strip_edges() == "1048575") {
  260. return "0xFFFFF";
  261. }
  262. return p_constant;
  263. }
  264. // Macros for assigning the deprecation/experimental information to class members
  265. #define DEPRECATED_DOC_TAG \
  266. class_desc->push_color(get_theme_color(SNAME("error_color"), SNAME("Editor"))); \
  267. Ref<Texture2D> error_icon = get_theme_icon(SNAME("StatusError"), SNAME("EditorIcons")); \
  268. class_desc->add_text(" "); \
  269. class_desc->add_image(error_icon, error_icon->get_width(), error_icon->get_height()); \
  270. class_desc->add_text(" (" + TTR("Deprecated") + ")"); \
  271. class_desc->pop();
  272. #define EXPERIMENTAL_DOC_TAG \
  273. class_desc->push_color(get_theme_color(SNAME("warning_color"), SNAME("Editor"))); \
  274. Ref<Texture2D> warning_icon = get_theme_icon(SNAME("NodeWarning"), SNAME("EditorIcons")); \
  275. class_desc->add_text(" "); \
  276. class_desc->add_image(warning_icon, warning_icon->get_width(), warning_icon->get_height()); \
  277. class_desc->add_text(" (" + TTR("Experimental") + ")"); \
  278. class_desc->pop();
  279. void EditorHelp::_add_method(const DocData::MethodDoc &p_method, bool p_overview) {
  280. method_line[p_method.name] = class_desc->get_paragraph_count() - 2; //gets overridden if description
  281. const bool is_vararg = p_method.qualifiers.contains("vararg");
  282. if (p_overview) {
  283. class_desc->push_cell();
  284. class_desc->push_paragraph(HORIZONTAL_ALIGNMENT_RIGHT, Control::TEXT_DIRECTION_AUTO, "");
  285. } else {
  286. _add_bulletpoint();
  287. }
  288. _add_type(p_method.return_type, p_method.return_enum);
  289. if (p_overview) {
  290. class_desc->pop(); //align
  291. class_desc->pop(); //cell
  292. class_desc->push_cell();
  293. } else {
  294. class_desc->add_text(" ");
  295. }
  296. if (p_overview && !p_method.description.strip_edges().is_empty()) {
  297. class_desc->push_meta("@method " + p_method.name);
  298. }
  299. class_desc->push_color(theme_cache.headline_color);
  300. _add_text(p_method.name);
  301. class_desc->pop();
  302. if (p_overview && !p_method.description.strip_edges().is_empty()) {
  303. class_desc->pop(); //meta
  304. }
  305. class_desc->push_color(theme_cache.symbol_color);
  306. class_desc->add_text("(");
  307. class_desc->pop();
  308. for (int j = 0; j < p_method.arguments.size(); j++) {
  309. class_desc->push_color(theme_cache.text_color);
  310. if (j > 0) {
  311. class_desc->add_text(", ");
  312. }
  313. _add_text(p_method.arguments[j].name);
  314. class_desc->add_text(": ");
  315. _add_type(p_method.arguments[j].type, p_method.arguments[j].enumeration);
  316. if (!p_method.arguments[j].default_value.is_empty()) {
  317. class_desc->push_color(theme_cache.symbol_color);
  318. class_desc->add_text(" = ");
  319. class_desc->pop();
  320. class_desc->push_color(theme_cache.value_color);
  321. _add_text(_fix_constant(p_method.arguments[j].default_value));
  322. class_desc->pop();
  323. }
  324. class_desc->pop();
  325. }
  326. if (is_vararg) {
  327. class_desc->push_color(theme_cache.text_color);
  328. if (p_method.arguments.size()) {
  329. class_desc->add_text(", ");
  330. }
  331. class_desc->push_color(theme_cache.symbol_color);
  332. class_desc->add_text("...");
  333. class_desc->pop();
  334. class_desc->pop();
  335. }
  336. class_desc->push_color(theme_cache.symbol_color);
  337. class_desc->add_text(")");
  338. class_desc->pop();
  339. if (!p_method.qualifiers.is_empty()) {
  340. class_desc->push_color(theme_cache.qualifier_color);
  341. PackedStringArray qualifiers = p_method.qualifiers.split_spaces();
  342. for (const String &qualifier : qualifiers) {
  343. String hint;
  344. if (qualifier == "vararg") {
  345. hint = TTR("This method supports a variable number of arguments.");
  346. } else if (qualifier == "virtual") {
  347. hint = TTR("This method is called by the engine.\nIt can be overridden to customize built-in behavior.");
  348. } else if (qualifier == "const") {
  349. hint = TTR("This method has no side effects.\nIt does not modify the object in any way.");
  350. } else if (qualifier == "static") {
  351. hint = TTR("This method does not need an instance to be called.\nIt can be called directly using the class name.");
  352. }
  353. class_desc->add_text(" ");
  354. if (!hint.is_empty()) {
  355. class_desc->push_hint(hint);
  356. class_desc->add_text(qualifier);
  357. class_desc->pop();
  358. } else {
  359. class_desc->add_text(qualifier);
  360. }
  361. }
  362. class_desc->pop();
  363. }
  364. if (p_method.is_deprecated) {
  365. DEPRECATED_DOC_TAG;
  366. }
  367. if (p_method.is_experimental) {
  368. EXPERIMENTAL_DOC_TAG;
  369. }
  370. if (p_overview) {
  371. class_desc->pop(); //cell
  372. }
  373. }
  374. void EditorHelp::_add_bulletpoint() {
  375. static const char32_t prefix[3] = { 0x25CF /* filled circle */, ' ', 0 };
  376. class_desc->add_text(String(prefix));
  377. }
  378. void EditorHelp::_push_normal_font() {
  379. class_desc->push_font(theme_cache.doc_font);
  380. class_desc->push_font_size(theme_cache.doc_font_size);
  381. }
  382. void EditorHelp::_pop_normal_font() {
  383. class_desc->pop();
  384. class_desc->pop();
  385. }
  386. void EditorHelp::_push_title_font() {
  387. class_desc->push_color(theme_cache.title_color);
  388. class_desc->push_font(theme_cache.doc_title_font);
  389. class_desc->push_font_size(theme_cache.doc_title_font_size);
  390. }
  391. void EditorHelp::_pop_title_font() {
  392. class_desc->pop();
  393. class_desc->pop();
  394. class_desc->pop();
  395. }
  396. void EditorHelp::_push_code_font() {
  397. class_desc->push_font(theme_cache.doc_code_font);
  398. class_desc->push_font_size(theme_cache.doc_code_font_size);
  399. }
  400. void EditorHelp::_pop_code_font() {
  401. class_desc->pop();
  402. class_desc->pop();
  403. }
  404. Error EditorHelp::_goto_desc(const String &p_class, int p_vscr) {
  405. if (!doc->class_list.has(p_class)) {
  406. return ERR_DOES_NOT_EXIST;
  407. }
  408. select_locked = true;
  409. class_desc->show();
  410. description_line = 0;
  411. if (p_class == edited_class) {
  412. return OK; // Already there.
  413. }
  414. edited_class = p_class;
  415. _update_doc();
  416. return OK;
  417. }
  418. void EditorHelp::_update_method_list(const Vector<DocData::MethodDoc> p_methods) {
  419. class_desc->add_newline();
  420. _push_code_font();
  421. class_desc->push_indent(1);
  422. class_desc->push_table(2);
  423. class_desc->set_table_column_expand(1, true);
  424. bool any_previous = false;
  425. for (int pass = 0; pass < 2; pass++) {
  426. Vector<DocData::MethodDoc> m;
  427. for (int i = 0; i < p_methods.size(); i++) {
  428. const String &q = p_methods[i].qualifiers;
  429. if ((pass == 0 && q.contains("virtual")) || (pass == 1 && !q.contains("virtual"))) {
  430. m.push_back(p_methods[i]);
  431. }
  432. }
  433. if (any_previous && !m.is_empty()) {
  434. class_desc->push_cell();
  435. class_desc->pop(); //cell
  436. class_desc->push_cell();
  437. class_desc->pop(); //cell
  438. }
  439. String group_prefix;
  440. for (int i = 0; i < m.size(); i++) {
  441. const String new_prefix = m[i].name.substr(0, 3);
  442. bool is_new_group = false;
  443. if (i < m.size() - 1 && new_prefix == m[i + 1].name.substr(0, 3) && new_prefix != group_prefix) {
  444. is_new_group = i > 0;
  445. group_prefix = new_prefix;
  446. } else if (!group_prefix.is_empty() && new_prefix != group_prefix) {
  447. is_new_group = true;
  448. group_prefix = "";
  449. }
  450. if (is_new_group && pass == 1) {
  451. class_desc->push_cell();
  452. class_desc->pop(); //cell
  453. class_desc->push_cell();
  454. class_desc->pop(); //cell
  455. }
  456. _add_method(m[i], true);
  457. }
  458. any_previous = !m.is_empty();
  459. }
  460. class_desc->pop(); //table
  461. class_desc->pop();
  462. _pop_code_font();
  463. class_desc->add_newline();
  464. class_desc->add_newline();
  465. }
  466. void EditorHelp::_update_method_descriptions(const DocData::ClassDoc p_classdoc, const Vector<DocData::MethodDoc> p_methods, const String &p_method_type) {
  467. String link_color_text = theme_cache.title_color.to_html(false);
  468. class_desc->add_newline();
  469. class_desc->add_newline();
  470. for (int pass = 0; pass < 2; pass++) {
  471. Vector<DocData::MethodDoc> methods_filtered;
  472. for (int i = 0; i < p_methods.size(); i++) {
  473. const String &q = p_methods[i].qualifiers;
  474. if ((pass == 0 && q.contains("virtual")) || (pass == 1 && !q.contains("virtual"))) {
  475. methods_filtered.push_back(p_methods[i]);
  476. }
  477. }
  478. for (int i = 0; i < methods_filtered.size(); i++) {
  479. _push_code_font();
  480. _add_method(methods_filtered[i], false);
  481. _pop_code_font();
  482. class_desc->add_newline();
  483. class_desc->add_newline();
  484. class_desc->push_color(theme_cache.text_color);
  485. _push_normal_font();
  486. class_desc->push_indent(1);
  487. if (methods_filtered[i].errors_returned.size()) {
  488. class_desc->append_text(TTR("Error codes returned:"));
  489. class_desc->add_newline();
  490. class_desc->push_list(0, RichTextLabel::LIST_DOTS, false);
  491. for (int j = 0; j < methods_filtered[i].errors_returned.size(); j++) {
  492. if (j > 0) {
  493. class_desc->add_newline();
  494. }
  495. int val = methods_filtered[i].errors_returned[j];
  496. String text = itos(val);
  497. for (int k = 0; k < CoreConstants::get_global_constant_count(); k++) {
  498. if (CoreConstants::get_global_constant_value(k) == val && CoreConstants::get_global_constant_enum(k) == SNAME("Error")) {
  499. text = CoreConstants::get_global_constant_name(k);
  500. break;
  501. }
  502. }
  503. class_desc->push_bold();
  504. class_desc->append_text(text);
  505. class_desc->pop();
  506. }
  507. class_desc->pop();
  508. class_desc->add_newline();
  509. class_desc->add_newline();
  510. }
  511. if (!methods_filtered[i].description.strip_edges().is_empty()) {
  512. _add_text(DTR(methods_filtered[i].description));
  513. } else {
  514. class_desc->add_image(get_theme_icon(SNAME("Error"), SNAME("EditorIcons")));
  515. class_desc->add_text(" ");
  516. class_desc->push_color(theme_cache.comment_color);
  517. if (p_classdoc.is_script_doc) {
  518. class_desc->append_text(vformat(TTR("There is currently no description for this %s."), p_method_type));
  519. } else {
  520. class_desc->append_text(vformat(TTR("There is currently no description for this %s. Please help us by [color=$color][url=$url]contributing one[/url][/color]!"), p_method_type).replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
  521. }
  522. class_desc->pop();
  523. }
  524. class_desc->pop();
  525. _pop_normal_font();
  526. class_desc->pop();
  527. class_desc->add_newline();
  528. class_desc->add_newline();
  529. class_desc->add_newline();
  530. }
  531. }
  532. }
  533. void EditorHelp::_update_doc() {
  534. if (!doc->class_list.has(edited_class)) {
  535. return;
  536. }
  537. scroll_locked = true;
  538. class_desc->clear();
  539. method_line.clear();
  540. section_line.clear();
  541. String link_color_text = theme_cache.title_color.to_html(false);
  542. DocData::ClassDoc cd = doc->class_list[edited_class]; // Make a copy, so we can sort without worrying.
  543. // Class name
  544. section_line.push_back(Pair<String, int>(TTR("Top"), 0));
  545. _push_title_font();
  546. class_desc->add_text(TTR("Class:") + " ");
  547. _add_type_icon(edited_class, theme_cache.doc_title_font_size);
  548. class_desc->add_text(" ");
  549. class_desc->push_color(theme_cache.headline_color);
  550. _add_text(edited_class);
  551. class_desc->pop(); // color
  552. _pop_title_font();
  553. if (cd.is_deprecated) {
  554. class_desc->add_text(" ");
  555. Ref<Texture2D> error_icon = get_theme_icon(SNAME("StatusError"), SNAME("EditorIcons"));
  556. class_desc->add_image(error_icon, error_icon->get_width(), error_icon->get_height());
  557. }
  558. if (cd.is_experimental) {
  559. class_desc->add_text(" ");
  560. Ref<Texture2D> warning_icon = get_theme_icon(SNAME("NodeWarning"), SNAME("EditorIcons"));
  561. class_desc->add_image(warning_icon, warning_icon->get_width(), warning_icon->get_height());
  562. }
  563. class_desc->add_newline();
  564. const String non_breaking_space = String::chr(160);
  565. // Inheritance tree
  566. // Ascendents
  567. if (!cd.inherits.is_empty()) {
  568. class_desc->push_color(theme_cache.title_color);
  569. _push_normal_font();
  570. class_desc->add_text(TTR("Inherits:") + " ");
  571. String inherits = cd.inherits;
  572. while (!inherits.is_empty()) {
  573. _add_type_icon(inherits);
  574. class_desc->add_text(non_breaking_space); // Otherwise icon borrows hyperlink from _add_type().
  575. _add_type(inherits);
  576. inherits = doc->class_list[inherits].inherits;
  577. if (!inherits.is_empty()) {
  578. class_desc->add_text(" < ");
  579. }
  580. }
  581. _pop_normal_font();
  582. class_desc->pop();
  583. class_desc->add_newline();
  584. }
  585. // Descendents
  586. if (cd.is_script_doc || ClassDB::class_exists(cd.name)) {
  587. bool found = false;
  588. bool prev = false;
  589. _push_normal_font();
  590. for (const KeyValue<String, DocData::ClassDoc> &E : doc->class_list) {
  591. if (E.value.inherits == cd.name) {
  592. if (!found) {
  593. class_desc->push_color(theme_cache.title_color);
  594. class_desc->add_text(TTR("Inherited by:") + " ");
  595. found = true;
  596. }
  597. if (prev) {
  598. class_desc->add_text(" , ");
  599. }
  600. _add_type_icon(E.value.name);
  601. class_desc->add_text(non_breaking_space); // Otherwise icon borrows hyperlink from _add_type().
  602. _add_type(E.value.name);
  603. prev = true;
  604. }
  605. }
  606. _pop_normal_font();
  607. if (found) {
  608. class_desc->pop();
  609. class_desc->add_newline();
  610. }
  611. }
  612. // Note if deprecated.
  613. if (cd.is_deprecated) {
  614. Ref<Texture2D> error_icon = get_theme_icon(SNAME("StatusError"), SNAME("EditorIcons"));
  615. class_desc->push_color(get_theme_color(SNAME("error_color"), SNAME("Editor")));
  616. class_desc->add_image(error_icon, error_icon->get_width(), error_icon->get_height());
  617. class_desc->add_text(String(" ") + TTR("This class is marked as deprecated. It will be removed in future versions."));
  618. class_desc->pop();
  619. class_desc->add_newline();
  620. }
  621. // Note if experimental.
  622. if (cd.is_experimental) {
  623. Ref<Texture2D> warning_icon = get_theme_icon(SNAME("NodeWarning"), SNAME("EditorIcons"));
  624. class_desc->push_color(get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  625. class_desc->add_image(warning_icon, warning_icon->get_width(), warning_icon->get_height());
  626. class_desc->add_text(String(" ") + TTR("This class is marked as experimental. It is subject to likely change or possible removal in future versions. Use at your own discretion."));
  627. class_desc->pop();
  628. class_desc->add_newline();
  629. }
  630. bool has_description = false;
  631. class_desc->add_newline();
  632. class_desc->add_newline();
  633. // Brief description
  634. if (!cd.brief_description.strip_edges().is_empty()) {
  635. has_description = true;
  636. class_desc->push_color(theme_cache.text_color);
  637. class_desc->push_font(theme_cache.doc_bold_font);
  638. class_desc->push_indent(1);
  639. _add_text(DTR(cd.brief_description));
  640. class_desc->pop();
  641. class_desc->pop();
  642. class_desc->pop();
  643. class_desc->add_newline();
  644. class_desc->add_newline();
  645. class_desc->add_newline();
  646. }
  647. // Class description
  648. if (!cd.description.strip_edges().is_empty()) {
  649. has_description = true;
  650. section_line.push_back(Pair<String, int>(TTR("Description"), class_desc->get_paragraph_count() - 2));
  651. description_line = class_desc->get_paragraph_count() - 2;
  652. _push_title_font();
  653. class_desc->add_text(TTR("Description"));
  654. _pop_title_font();
  655. class_desc->add_newline();
  656. class_desc->add_newline();
  657. class_desc->push_color(theme_cache.text_color);
  658. _push_normal_font();
  659. class_desc->push_indent(1);
  660. _add_text(DTR(cd.description));
  661. class_desc->pop();
  662. _pop_normal_font();
  663. class_desc->pop();
  664. class_desc->add_newline();
  665. class_desc->add_newline();
  666. class_desc->add_newline();
  667. }
  668. if (!has_description) {
  669. class_desc->add_image(get_theme_icon(SNAME("Error"), SNAME("EditorIcons")));
  670. class_desc->add_text(" ");
  671. class_desc->push_color(theme_cache.comment_color);
  672. if (cd.is_script_doc) {
  673. class_desc->append_text(TTR("There is currently no description for this class."));
  674. } else {
  675. class_desc->append_text(TTR("There is currently no description for this class. Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
  676. }
  677. class_desc->pop();
  678. class_desc->add_newline();
  679. class_desc->add_newline();
  680. }
  681. // Online tutorials
  682. if (cd.tutorials.size()) {
  683. _push_title_font();
  684. class_desc->add_text(TTR("Online Tutorials"));
  685. _pop_title_font();
  686. class_desc->add_newline();
  687. class_desc->push_indent(1);
  688. _push_code_font();
  689. for (int i = 0; i < cd.tutorials.size(); i++) {
  690. const String link = DTR(cd.tutorials[i].link);
  691. String linktxt = (cd.tutorials[i].title.is_empty()) ? link : DTR(cd.tutorials[i].title);
  692. const int seppos = linktxt.find("//");
  693. if (seppos != -1) {
  694. linktxt = link.substr(seppos + 2);
  695. }
  696. class_desc->push_color(theme_cache.symbol_color);
  697. class_desc->append_text("[url=" + link + "]" + linktxt + "[/url]");
  698. class_desc->pop();
  699. class_desc->add_newline();
  700. }
  701. _pop_code_font();
  702. class_desc->pop();
  703. class_desc->add_newline();
  704. class_desc->add_newline();
  705. }
  706. // Properties overview
  707. HashSet<String> skip_methods;
  708. bool has_properties = cd.properties.size() != 0;
  709. if (cd.is_script_doc) {
  710. has_properties = false;
  711. for (int i = 0; i < cd.properties.size(); i++) {
  712. if (cd.properties[i].name.begins_with("_") && cd.properties[i].description.strip_edges().is_empty()) {
  713. continue;
  714. }
  715. has_properties = true;
  716. break;
  717. }
  718. }
  719. if (has_properties) {
  720. section_line.push_back(Pair<String, int>(TTR("Properties"), class_desc->get_paragraph_count() - 2));
  721. _push_title_font();
  722. class_desc->add_text(TTR("Properties"));
  723. _pop_title_font();
  724. class_desc->add_newline();
  725. _push_code_font();
  726. class_desc->push_indent(1);
  727. class_desc->push_table(4);
  728. class_desc->set_table_column_expand(1, true);
  729. for (int i = 0; i < cd.properties.size(); i++) {
  730. // Ignore undocumented private.
  731. if (cd.properties[i].name.begins_with("_") && cd.properties[i].description.strip_edges().is_empty()) {
  732. continue;
  733. }
  734. property_line[cd.properties[i].name] = class_desc->get_paragraph_count() - 2; //gets overridden if description
  735. // Property type.
  736. class_desc->push_cell();
  737. class_desc->push_paragraph(HORIZONTAL_ALIGNMENT_RIGHT, Control::TEXT_DIRECTION_AUTO, "");
  738. _push_code_font();
  739. _add_type(cd.properties[i].type, cd.properties[i].enumeration);
  740. _pop_code_font();
  741. class_desc->pop();
  742. class_desc->pop(); // cell
  743. bool describe = false;
  744. if (!cd.properties[i].setter.is_empty()) {
  745. skip_methods.insert(cd.properties[i].setter);
  746. describe = true;
  747. }
  748. if (!cd.properties[i].getter.is_empty()) {
  749. skip_methods.insert(cd.properties[i].getter);
  750. describe = true;
  751. }
  752. if (!cd.properties[i].description.strip_edges().is_empty()) {
  753. describe = true;
  754. }
  755. if (cd.properties[i].overridden) {
  756. describe = false;
  757. }
  758. // Property name.
  759. class_desc->push_cell();
  760. _push_code_font();
  761. class_desc->push_color(theme_cache.headline_color);
  762. if (describe) {
  763. class_desc->push_meta("@member " + cd.properties[i].name);
  764. }
  765. _add_text(cd.properties[i].name);
  766. if (describe) {
  767. class_desc->pop();
  768. }
  769. class_desc->pop();
  770. _pop_code_font();
  771. class_desc->pop(); // cell
  772. // Property value.
  773. class_desc->push_cell();
  774. _push_code_font();
  775. if (!cd.properties[i].default_value.is_empty()) {
  776. class_desc->push_color(theme_cache.symbol_color);
  777. if (cd.properties[i].overridden) {
  778. class_desc->add_text(" [");
  779. class_desc->push_meta("@member " + cd.properties[i].overrides + "." + cd.properties[i].name);
  780. _add_text(vformat(TTR("overrides %s:"), cd.properties[i].overrides));
  781. class_desc->pop();
  782. class_desc->add_text(" ");
  783. } else {
  784. class_desc->add_text(" [" + TTR("default:") + " ");
  785. }
  786. class_desc->pop();
  787. class_desc->push_color(theme_cache.value_color);
  788. _add_text(_fix_constant(cd.properties[i].default_value));
  789. class_desc->pop();
  790. class_desc->push_color(theme_cache.symbol_color);
  791. class_desc->add_text("]");
  792. class_desc->pop();
  793. }
  794. if (cd.properties[i].is_deprecated) {
  795. DEPRECATED_DOC_TAG;
  796. }
  797. if (cd.properties[i].is_experimental) {
  798. EXPERIMENTAL_DOC_TAG;
  799. }
  800. _pop_code_font();
  801. class_desc->pop(); // cell
  802. // Property setters and getters.
  803. class_desc->push_cell();
  804. _push_code_font();
  805. if (cd.is_script_doc && (!cd.properties[i].setter.is_empty() || !cd.properties[i].getter.is_empty())) {
  806. class_desc->push_color(theme_cache.symbol_color);
  807. class_desc->add_text(" [" + TTR("property:") + " ");
  808. class_desc->pop(); // color
  809. if (!cd.properties[i].setter.is_empty()) {
  810. class_desc->push_color(theme_cache.value_color);
  811. class_desc->add_text("setter");
  812. class_desc->pop(); // color
  813. }
  814. if (!cd.properties[i].getter.is_empty()) {
  815. if (!cd.properties[i].setter.is_empty()) {
  816. class_desc->push_color(theme_cache.symbol_color);
  817. class_desc->add_text(", ");
  818. class_desc->pop(); // color
  819. }
  820. class_desc->push_color(theme_cache.value_color);
  821. class_desc->add_text("getter");
  822. class_desc->pop(); // color
  823. }
  824. class_desc->push_color(theme_cache.symbol_color);
  825. class_desc->add_text("]");
  826. class_desc->pop(); // color
  827. }
  828. _pop_code_font();
  829. class_desc->pop(); // cell
  830. }
  831. class_desc->pop(); // table
  832. class_desc->pop();
  833. _pop_code_font();
  834. class_desc->add_newline();
  835. class_desc->add_newline();
  836. }
  837. // Methods overview
  838. bool sort_methods = EDITOR_GET("text_editor/help/sort_functions_alphabetically");
  839. Vector<DocData::MethodDoc> methods;
  840. for (int i = 0; i < cd.methods.size(); i++) {
  841. if (skip_methods.has(cd.methods[i].name)) {
  842. if (cd.methods[i].arguments.size() == 0 /* getter */ || (cd.methods[i].arguments.size() == 1 && cd.methods[i].return_type == "void" /* setter */)) {
  843. continue;
  844. }
  845. }
  846. // Ignore undocumented non virtual private.
  847. if (cd.methods[i].name.begins_with("_") && cd.methods[i].description.strip_edges().is_empty() && !cd.methods[i].qualifiers.contains("virtual")) {
  848. continue;
  849. }
  850. methods.push_back(cd.methods[i]);
  851. }
  852. if (!cd.constructors.is_empty()) {
  853. if (sort_methods) {
  854. cd.constructors.sort();
  855. }
  856. section_line.push_back(Pair<String, int>(TTR("Constructors"), class_desc->get_paragraph_count() - 2));
  857. _push_title_font();
  858. class_desc->add_text(TTR("Constructors"));
  859. _pop_title_font();
  860. _update_method_list(cd.constructors);
  861. }
  862. if (!methods.is_empty()) {
  863. if (sort_methods) {
  864. methods.sort();
  865. }
  866. section_line.push_back(Pair<String, int>(TTR("Methods"), class_desc->get_paragraph_count() - 2));
  867. _push_title_font();
  868. class_desc->add_text(TTR("Methods"));
  869. _pop_title_font();
  870. _update_method_list(methods);
  871. }
  872. if (!cd.operators.is_empty()) {
  873. if (sort_methods) {
  874. cd.operators.sort();
  875. }
  876. section_line.push_back(Pair<String, int>(TTR("Operators"), class_desc->get_paragraph_count() - 2));
  877. _push_title_font();
  878. class_desc->add_text(TTR("Operators"));
  879. _pop_title_font();
  880. _update_method_list(cd.operators);
  881. }
  882. // Theme properties
  883. if (!cd.theme_properties.is_empty()) {
  884. section_line.push_back(Pair<String, int>(TTR("Theme Properties"), class_desc->get_paragraph_count() - 2));
  885. _push_title_font();
  886. class_desc->add_text(TTR("Theme Properties"));
  887. _pop_title_font();
  888. class_desc->add_newline();
  889. class_desc->add_newline();
  890. class_desc->push_indent(1);
  891. String theme_data_type;
  892. HashMap<String, String> data_type_names;
  893. data_type_names["color"] = TTR("Colors");
  894. data_type_names["constant"] = TTR("Constants");
  895. data_type_names["font"] = TTR("Fonts");
  896. data_type_names["font_size"] = TTR("Font Sizes");
  897. data_type_names["icon"] = TTR("Icons");
  898. data_type_names["style"] = TTR("Styles");
  899. for (int i = 0; i < cd.theme_properties.size(); i++) {
  900. theme_property_line[cd.theme_properties[i].name] = class_desc->get_paragraph_count() - 2; // Gets overridden if description.
  901. if (theme_data_type != cd.theme_properties[i].data_type) {
  902. theme_data_type = cd.theme_properties[i].data_type;
  903. _push_title_font();
  904. if (data_type_names.has(theme_data_type)) {
  905. class_desc->add_text(data_type_names[theme_data_type]);
  906. } else {
  907. class_desc->add_text("");
  908. }
  909. _pop_title_font();
  910. class_desc->add_newline();
  911. class_desc->add_newline();
  912. }
  913. // Theme item header.
  914. _push_code_font();
  915. _add_bulletpoint();
  916. // Theme item object type.
  917. _add_type(cd.theme_properties[i].type);
  918. // Theme item name.
  919. class_desc->push_color(theme_cache.headline_color);
  920. class_desc->add_text(" ");
  921. _add_text(cd.theme_properties[i].name);
  922. class_desc->pop();
  923. // Theme item default value.
  924. if (!cd.theme_properties[i].default_value.is_empty()) {
  925. class_desc->push_color(theme_cache.symbol_color);
  926. class_desc->add_text(" [" + TTR("default:") + " ");
  927. class_desc->pop();
  928. class_desc->push_color(theme_cache.value_color);
  929. _add_text(_fix_constant(cd.theme_properties[i].default_value));
  930. class_desc->pop();
  931. class_desc->push_color(theme_cache.symbol_color);
  932. class_desc->add_text("]");
  933. class_desc->pop();
  934. }
  935. _pop_code_font();
  936. // Theme item description.
  937. if (!cd.theme_properties[i].description.strip_edges().is_empty()) {
  938. class_desc->push_color(theme_cache.comment_color);
  939. _push_normal_font();
  940. class_desc->push_indent(1);
  941. _add_text(DTR(cd.theme_properties[i].description));
  942. class_desc->pop(); // indent
  943. _pop_normal_font();
  944. class_desc->pop(); // color
  945. }
  946. class_desc->add_newline();
  947. class_desc->add_newline();
  948. }
  949. class_desc->pop();
  950. class_desc->add_newline();
  951. }
  952. // Signals
  953. if (!cd.signals.is_empty()) {
  954. if (sort_methods) {
  955. cd.signals.sort();
  956. }
  957. section_line.push_back(Pair<String, int>(TTR("Signals"), class_desc->get_paragraph_count() - 2));
  958. _push_title_font();
  959. class_desc->add_text(TTR("Signals"));
  960. _pop_title_font();
  961. class_desc->add_newline();
  962. class_desc->add_newline();
  963. class_desc->push_indent(1);
  964. for (int i = 0; i < cd.signals.size(); i++) {
  965. signal_line[cd.signals[i].name] = class_desc->get_paragraph_count() - 2; // Gets overridden if description.
  966. _push_code_font();
  967. _add_bulletpoint();
  968. class_desc->push_color(theme_cache.headline_color);
  969. _add_text(cd.signals[i].name);
  970. class_desc->pop();
  971. class_desc->push_color(theme_cache.symbol_color);
  972. class_desc->add_text("(");
  973. class_desc->pop();
  974. for (int j = 0; j < cd.signals[i].arguments.size(); j++) {
  975. class_desc->push_color(theme_cache.text_color);
  976. if (j > 0) {
  977. class_desc->add_text(", ");
  978. }
  979. _add_text(cd.signals[i].arguments[j].name);
  980. class_desc->add_text(": ");
  981. _add_type(cd.signals[i].arguments[j].type);
  982. if (!cd.signals[i].arguments[j].default_value.is_empty()) {
  983. class_desc->push_color(theme_cache.symbol_color);
  984. class_desc->add_text(" = ");
  985. class_desc->pop();
  986. _add_text(cd.signals[i].arguments[j].default_value);
  987. }
  988. class_desc->pop();
  989. }
  990. class_desc->push_color(theme_cache.symbol_color);
  991. class_desc->add_text(")");
  992. if (cd.signals[i].is_deprecated) {
  993. DEPRECATED_DOC_TAG;
  994. }
  995. if (cd.signals[i].is_experimental) {
  996. EXPERIMENTAL_DOC_TAG;
  997. }
  998. class_desc->pop();
  999. _pop_code_font();
  1000. if (!cd.signals[i].description.strip_edges().is_empty()) {
  1001. class_desc->push_color(theme_cache.comment_color);
  1002. _push_normal_font();
  1003. class_desc->push_indent(1);
  1004. _add_text(DTR(cd.signals[i].description));
  1005. class_desc->pop(); // indent
  1006. _pop_normal_font();
  1007. class_desc->pop(); // color
  1008. }
  1009. class_desc->add_newline();
  1010. class_desc->add_newline();
  1011. }
  1012. class_desc->pop();
  1013. class_desc->add_newline();
  1014. }
  1015. // Constants and enums
  1016. if (!cd.constants.is_empty()) {
  1017. HashMap<String, Vector<DocData::ConstantDoc>> enums;
  1018. Vector<DocData::ConstantDoc> constants;
  1019. for (int i = 0; i < cd.constants.size(); i++) {
  1020. if (!cd.constants[i].enumeration.is_empty()) {
  1021. if (!enums.has(cd.constants[i].enumeration)) {
  1022. enums[cd.constants[i].enumeration] = Vector<DocData::ConstantDoc>();
  1023. }
  1024. enums[cd.constants[i].enumeration].push_back(cd.constants[i]);
  1025. } else {
  1026. // Ignore undocumented private.
  1027. if (cd.constants[i].name.begins_with("_") && cd.constants[i].description.strip_edges().is_empty()) {
  1028. continue;
  1029. }
  1030. constants.push_back(cd.constants[i]);
  1031. }
  1032. }
  1033. // Enums
  1034. if (enums.size()) {
  1035. section_line.push_back(Pair<String, int>(TTR("Enumerations"), class_desc->get_paragraph_count() - 2));
  1036. _push_title_font();
  1037. class_desc->add_text(TTR("Enumerations"));
  1038. _pop_title_font();
  1039. class_desc->push_indent(1);
  1040. class_desc->add_newline();
  1041. for (KeyValue<String, Vector<DocData::ConstantDoc>> &E : enums) {
  1042. enum_line[E.key] = class_desc->get_paragraph_count() - 2;
  1043. _push_code_font();
  1044. class_desc->push_color(theme_cache.title_color);
  1045. if (E.value.size() && E.value[0].is_bitfield) {
  1046. class_desc->add_text("flags ");
  1047. } else {
  1048. class_desc->add_text("enum ");
  1049. }
  1050. class_desc->pop();
  1051. String e = E.key;
  1052. if ((e.get_slice_count(".") > 1) && (e.get_slice(".", 0) == edited_class)) {
  1053. e = e.get_slice(".", 1);
  1054. }
  1055. class_desc->push_color(theme_cache.headline_color);
  1056. class_desc->add_text(e);
  1057. class_desc->pop();
  1058. _pop_code_font();
  1059. class_desc->push_color(theme_cache.symbol_color);
  1060. class_desc->add_text(":");
  1061. class_desc->pop();
  1062. class_desc->add_newline();
  1063. class_desc->add_newline();
  1064. // Enum description.
  1065. if (e != "@unnamed_enums" && cd.enums.has(e)) {
  1066. class_desc->push_color(theme_cache.text_color);
  1067. _push_normal_font();
  1068. class_desc->push_indent(1);
  1069. _add_text(cd.enums[e]);
  1070. class_desc->pop();
  1071. _pop_normal_font();
  1072. class_desc->pop();
  1073. class_desc->add_newline();
  1074. class_desc->add_newline();
  1075. }
  1076. class_desc->push_indent(1);
  1077. Vector<DocData::ConstantDoc> enum_list = E.value;
  1078. HashMap<String, int> enumValuesContainer;
  1079. int enumStartingLine = enum_line[E.key];
  1080. for (int i = 0; i < enum_list.size(); i++) {
  1081. if (cd.name == "@GlobalScope") {
  1082. enumValuesContainer[enum_list[i].name] = enumStartingLine;
  1083. }
  1084. // Add the enum constant line to the constant_line map so we can locate it as a constant.
  1085. constant_line[enum_list[i].name] = class_desc->get_paragraph_count() - 2;
  1086. _push_code_font();
  1087. _add_bulletpoint();
  1088. class_desc->push_color(theme_cache.headline_color);
  1089. _add_text(enum_list[i].name);
  1090. class_desc->pop();
  1091. class_desc->push_color(theme_cache.symbol_color);
  1092. class_desc->add_text(" = ");
  1093. class_desc->pop();
  1094. class_desc->push_color(theme_cache.value_color);
  1095. _add_text(_fix_constant(enum_list[i].value));
  1096. class_desc->pop();
  1097. _pop_code_font();
  1098. if (enum_list[i].is_deprecated) {
  1099. DEPRECATED_DOC_TAG;
  1100. }
  1101. if (enum_list[i].is_experimental) {
  1102. EXPERIMENTAL_DOC_TAG;
  1103. }
  1104. class_desc->add_newline();
  1105. if (!enum_list[i].description.strip_edges().is_empty()) {
  1106. class_desc->push_color(theme_cache.comment_color);
  1107. _push_normal_font();
  1108. _add_text(DTR(enum_list[i].description));
  1109. _pop_normal_font();
  1110. class_desc->pop();
  1111. if (DTR(enum_list[i].description).find("\n") > 0) {
  1112. class_desc->add_newline();
  1113. }
  1114. }
  1115. class_desc->add_newline();
  1116. }
  1117. if (cd.name == "@GlobalScope") {
  1118. enum_values_line[E.key] = enumValuesContainer;
  1119. }
  1120. class_desc->pop();
  1121. class_desc->add_newline();
  1122. }
  1123. class_desc->pop();
  1124. class_desc->add_newline();
  1125. }
  1126. // Constants
  1127. if (constants.size()) {
  1128. section_line.push_back(Pair<String, int>(TTR("Constants"), class_desc->get_paragraph_count() - 2));
  1129. _push_title_font();
  1130. class_desc->add_text(TTR("Constants"));
  1131. _pop_title_font();
  1132. class_desc->push_indent(1);
  1133. class_desc->add_newline();
  1134. for (int i = 0; i < constants.size(); i++) {
  1135. constant_line[constants[i].name] = class_desc->get_paragraph_count() - 2;
  1136. _push_code_font();
  1137. if (constants[i].value.begins_with("Color(") && constants[i].value.ends_with(")")) {
  1138. String stripped = constants[i].value.replace(" ", "").replace("Color(", "").replace(")", "");
  1139. PackedFloat64Array color = stripped.split_floats(",");
  1140. if (color.size() >= 3) {
  1141. class_desc->push_color(Color(color[0], color[1], color[2]));
  1142. _add_bulletpoint();
  1143. class_desc->pop();
  1144. }
  1145. } else {
  1146. _add_bulletpoint();
  1147. }
  1148. class_desc->push_color(theme_cache.headline_color);
  1149. _add_text(constants[i].name);
  1150. class_desc->pop();
  1151. class_desc->push_color(theme_cache.symbol_color);
  1152. class_desc->add_text(" = ");
  1153. class_desc->pop();
  1154. class_desc->push_color(theme_cache.value_color);
  1155. _add_text(_fix_constant(constants[i].value));
  1156. class_desc->pop();
  1157. _pop_code_font();
  1158. if (constants[i].is_deprecated) {
  1159. DEPRECATED_DOC_TAG;
  1160. }
  1161. if (constants[i].is_experimental) {
  1162. EXPERIMENTAL_DOC_TAG;
  1163. }
  1164. class_desc->add_newline();
  1165. if (!constants[i].description.strip_edges().is_empty()) {
  1166. class_desc->push_color(theme_cache.comment_color);
  1167. _push_normal_font();
  1168. _add_text(DTR(constants[i].description));
  1169. _pop_normal_font();
  1170. class_desc->pop();
  1171. if (DTR(constants[i].description).find("\n") > 0) {
  1172. class_desc->add_newline();
  1173. }
  1174. }
  1175. class_desc->add_newline();
  1176. }
  1177. class_desc->pop();
  1178. class_desc->add_newline();
  1179. }
  1180. }
  1181. // Annotations
  1182. if (!cd.annotations.is_empty()) {
  1183. if (sort_methods) {
  1184. cd.annotations.sort();
  1185. }
  1186. section_line.push_back(Pair<String, int>(TTR("Annotations"), class_desc->get_paragraph_count() - 2));
  1187. _push_title_font();
  1188. class_desc->add_text(TTR("Annotations"));
  1189. _pop_title_font();
  1190. class_desc->add_newline();
  1191. class_desc->add_newline();
  1192. class_desc->push_indent(1);
  1193. for (int i = 0; i < cd.annotations.size(); i++) {
  1194. annotation_line[cd.annotations[i].name] = class_desc->get_paragraph_count() - 2; // Gets overridden if description.
  1195. _push_code_font();
  1196. _add_bulletpoint();
  1197. class_desc->push_color(theme_cache.headline_color);
  1198. _add_text(cd.annotations[i].name);
  1199. class_desc->pop();
  1200. if (cd.annotations[i].arguments.size() > 0) {
  1201. class_desc->push_color(theme_cache.symbol_color);
  1202. class_desc->add_text("(");
  1203. class_desc->pop();
  1204. for (int j = 0; j < cd.annotations[i].arguments.size(); j++) {
  1205. class_desc->push_color(theme_cache.text_color);
  1206. if (j > 0) {
  1207. class_desc->add_text(", ");
  1208. }
  1209. _add_text(cd.annotations[i].arguments[j].name);
  1210. class_desc->add_text(": ");
  1211. _add_type(cd.annotations[i].arguments[j].type);
  1212. if (!cd.annotations[i].arguments[j].default_value.is_empty()) {
  1213. class_desc->push_color(theme_cache.symbol_color);
  1214. class_desc->add_text(" = ");
  1215. class_desc->pop();
  1216. _add_text(cd.annotations[i].arguments[j].default_value);
  1217. }
  1218. class_desc->pop();
  1219. }
  1220. if (cd.annotations[i].qualifiers.contains("vararg")) {
  1221. class_desc->push_color(theme_cache.text_color);
  1222. if (cd.annotations[i].arguments.size()) {
  1223. class_desc->add_text(", ");
  1224. }
  1225. class_desc->push_color(theme_cache.symbol_color);
  1226. class_desc->add_text("...");
  1227. class_desc->pop();
  1228. class_desc->pop();
  1229. }
  1230. class_desc->push_color(theme_cache.symbol_color);
  1231. class_desc->add_text(")");
  1232. class_desc->pop();
  1233. }
  1234. if (!cd.annotations[i].qualifiers.is_empty()) {
  1235. class_desc->push_color(theme_cache.qualifier_color);
  1236. class_desc->add_text(" ");
  1237. _add_text(cd.annotations[i].qualifiers);
  1238. class_desc->pop();
  1239. }
  1240. _pop_code_font();
  1241. if (!cd.annotations[i].description.strip_edges().is_empty()) {
  1242. class_desc->push_color(theme_cache.comment_color);
  1243. _push_normal_font();
  1244. class_desc->push_indent(1);
  1245. _add_text(DTR(cd.annotations[i].description));
  1246. class_desc->pop(); // indent
  1247. _pop_normal_font();
  1248. class_desc->pop(); // color
  1249. } else {
  1250. class_desc->push_indent(1);
  1251. class_desc->add_image(get_theme_icon(SNAME("Error"), SNAME("EditorIcons")));
  1252. class_desc->add_text(" ");
  1253. class_desc->push_color(theme_cache.comment_color);
  1254. if (cd.is_script_doc) {
  1255. class_desc->append_text(TTR("There is currently no description for this annotation."));
  1256. } else {
  1257. class_desc->append_text(TTR("There is currently no description for this annotation. Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
  1258. }
  1259. class_desc->pop();
  1260. class_desc->pop(); // indent
  1261. }
  1262. class_desc->add_newline();
  1263. class_desc->add_newline();
  1264. }
  1265. class_desc->pop();
  1266. class_desc->add_newline();
  1267. }
  1268. // Property descriptions
  1269. if (has_properties) {
  1270. section_line.push_back(Pair<String, int>(TTR("Property Descriptions"), class_desc->get_paragraph_count() - 2));
  1271. _push_title_font();
  1272. class_desc->add_text(TTR("Property Descriptions"));
  1273. _pop_title_font();
  1274. class_desc->add_newline();
  1275. class_desc->add_newline();
  1276. for (int i = 0; i < cd.properties.size(); i++) {
  1277. if (cd.properties[i].overridden) {
  1278. continue;
  1279. }
  1280. // Ignore undocumented private.
  1281. if (cd.properties[i].name.begins_with("_") && cd.properties[i].description.strip_edges().is_empty()) {
  1282. continue;
  1283. }
  1284. property_line[cd.properties[i].name] = class_desc->get_paragraph_count() - 2;
  1285. class_desc->push_table(2);
  1286. class_desc->set_table_column_expand(1, true);
  1287. class_desc->push_cell();
  1288. _push_code_font();
  1289. _add_bulletpoint();
  1290. _add_type(cd.properties[i].type, cd.properties[i].enumeration);
  1291. class_desc->add_text(" ");
  1292. _pop_code_font();
  1293. class_desc->pop(); // cell
  1294. class_desc->push_cell();
  1295. _push_code_font();
  1296. class_desc->push_color(theme_cache.headline_color);
  1297. _add_text(cd.properties[i].name);
  1298. class_desc->pop(); // color
  1299. if (!cd.properties[i].default_value.is_empty()) {
  1300. class_desc->push_color(theme_cache.symbol_color);
  1301. class_desc->add_text(" [" + TTR("default:") + " ");
  1302. class_desc->pop(); // color
  1303. class_desc->push_color(theme_cache.value_color);
  1304. _add_text(_fix_constant(cd.properties[i].default_value));
  1305. class_desc->pop(); // color
  1306. class_desc->push_color(theme_cache.symbol_color);
  1307. class_desc->add_text("]");
  1308. class_desc->pop(); // color
  1309. }
  1310. if (cd.properties[i].is_deprecated) {
  1311. DEPRECATED_DOC_TAG;
  1312. }
  1313. if (cd.properties[i].is_experimental) {
  1314. EXPERIMENTAL_DOC_TAG;
  1315. }
  1316. if (cd.is_script_doc && (!cd.properties[i].setter.is_empty() || !cd.properties[i].getter.is_empty())) {
  1317. class_desc->push_color(theme_cache.symbol_color);
  1318. class_desc->add_text(" [" + TTR("property:") + " ");
  1319. class_desc->pop(); // color
  1320. if (!cd.properties[i].setter.is_empty()) {
  1321. class_desc->push_color(theme_cache.value_color);
  1322. class_desc->add_text("setter");
  1323. class_desc->pop(); // color
  1324. }
  1325. if (!cd.properties[i].getter.is_empty()) {
  1326. if (!cd.properties[i].setter.is_empty()) {
  1327. class_desc->push_color(theme_cache.symbol_color);
  1328. class_desc->add_text(", ");
  1329. class_desc->pop(); // color
  1330. }
  1331. class_desc->push_color(theme_cache.value_color);
  1332. class_desc->add_text("getter");
  1333. class_desc->pop(); // color
  1334. }
  1335. class_desc->push_color(theme_cache.symbol_color);
  1336. class_desc->add_text("]");
  1337. class_desc->pop(); // color
  1338. }
  1339. _pop_code_font();
  1340. class_desc->pop(); // cell
  1341. // Script doc doesn't have setter, getter.
  1342. if (!cd.is_script_doc) {
  1343. HashMap<String, DocData::MethodDoc> method_map;
  1344. for (int j = 0; j < methods.size(); j++) {
  1345. method_map[methods[j].name] = methods[j];
  1346. }
  1347. if (!cd.properties[i].setter.is_empty()) {
  1348. class_desc->push_cell();
  1349. class_desc->pop(); // cell
  1350. class_desc->push_cell();
  1351. _push_code_font();
  1352. class_desc->push_color(theme_cache.text_color);
  1353. if (method_map[cd.properties[i].setter].arguments.size() > 1) {
  1354. // Setters with additional arguments are exposed in the method list, so we link them here for quick access.
  1355. class_desc->push_meta("@method " + cd.properties[i].setter);
  1356. class_desc->add_text(cd.properties[i].setter + TTR("(value)"));
  1357. class_desc->pop();
  1358. } else {
  1359. class_desc->add_text(cd.properties[i].setter + TTR("(value)"));
  1360. }
  1361. class_desc->pop(); // color
  1362. class_desc->push_color(theme_cache.comment_color);
  1363. class_desc->add_text(" setter");
  1364. class_desc->pop(); // color
  1365. _pop_code_font();
  1366. class_desc->pop(); // cell
  1367. method_line[cd.properties[i].setter] = property_line[cd.properties[i].name];
  1368. }
  1369. if (!cd.properties[i].getter.is_empty()) {
  1370. class_desc->push_cell();
  1371. class_desc->pop(); // cell
  1372. class_desc->push_cell();
  1373. _push_code_font();
  1374. class_desc->push_color(theme_cache.text_color);
  1375. if (method_map[cd.properties[i].getter].arguments.size() > 0) {
  1376. // Getters with additional arguments are exposed in the method list, so we link them here for quick access.
  1377. class_desc->push_meta("@method " + cd.properties[i].getter);
  1378. class_desc->add_text(cd.properties[i].getter + "()");
  1379. class_desc->pop();
  1380. } else {
  1381. class_desc->add_text(cd.properties[i].getter + "()");
  1382. }
  1383. class_desc->pop(); // color
  1384. class_desc->push_color(theme_cache.comment_color);
  1385. class_desc->add_text(" getter");
  1386. class_desc->pop(); // color
  1387. _pop_code_font();
  1388. class_desc->pop(); // cell
  1389. method_line[cd.properties[i].getter] = property_line[cd.properties[i].name];
  1390. }
  1391. }
  1392. class_desc->pop(); // table
  1393. class_desc->add_newline();
  1394. class_desc->add_newline();
  1395. class_desc->push_color(theme_cache.text_color);
  1396. _push_normal_font();
  1397. class_desc->push_indent(1);
  1398. if (!cd.properties[i].description.strip_edges().is_empty()) {
  1399. _add_text(DTR(cd.properties[i].description));
  1400. } else {
  1401. class_desc->add_image(get_theme_icon(SNAME("Error"), SNAME("EditorIcons")));
  1402. class_desc->add_text(" ");
  1403. class_desc->push_color(theme_cache.comment_color);
  1404. if (cd.is_script_doc) {
  1405. class_desc->append_text(TTR("There is currently no description for this property."));
  1406. } else {
  1407. 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));
  1408. }
  1409. class_desc->pop();
  1410. }
  1411. class_desc->pop();
  1412. _pop_normal_font();
  1413. class_desc->pop();
  1414. class_desc->add_newline();
  1415. class_desc->add_newline();
  1416. class_desc->add_newline();
  1417. }
  1418. }
  1419. // Constructor descriptions
  1420. if (!cd.constructors.is_empty()) {
  1421. section_line.push_back(Pair<String, int>(TTR("Constructor Descriptions"), class_desc->get_paragraph_count() - 2));
  1422. _push_title_font();
  1423. class_desc->add_text(TTR("Constructor Descriptions"));
  1424. _pop_title_font();
  1425. _update_method_descriptions(cd, cd.constructors, "constructor");
  1426. }
  1427. // Method descriptions
  1428. if (!methods.is_empty()) {
  1429. section_line.push_back(Pair<String, int>(TTR("Method Descriptions"), class_desc->get_paragraph_count() - 2));
  1430. _push_title_font();
  1431. class_desc->add_text(TTR("Method Descriptions"));
  1432. _pop_title_font();
  1433. _update_method_descriptions(cd, methods, "method");
  1434. }
  1435. // Operator descriptions
  1436. if (!cd.operators.is_empty()) {
  1437. section_line.push_back(Pair<String, int>(TTR("Operator Descriptions"), class_desc->get_paragraph_count() - 2));
  1438. _push_title_font();
  1439. class_desc->add_text(TTR("Operator Descriptions"));
  1440. _pop_title_font();
  1441. _update_method_descriptions(cd, cd.operators, "operator");
  1442. }
  1443. // Free the scroll.
  1444. scroll_locked = false;
  1445. }
  1446. void EditorHelp::_request_help(const String &p_string) {
  1447. Error err = _goto_desc(p_string);
  1448. if (err == OK) {
  1449. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  1450. }
  1451. //100 palabras
  1452. }
  1453. void EditorHelp::_help_callback(const String &p_topic) {
  1454. String what = p_topic.get_slice(":", 0);
  1455. String clss = p_topic.get_slice(":", 1);
  1456. String name;
  1457. if (p_topic.get_slice_count(":") == 3) {
  1458. name = p_topic.get_slice(":", 2);
  1459. }
  1460. _request_help(clss); // First go to class.
  1461. int line = 0;
  1462. if (what == "class_desc") {
  1463. line = description_line;
  1464. } else if (what == "class_signal") {
  1465. if (signal_line.has(name)) {
  1466. line = signal_line[name];
  1467. }
  1468. } else if (what == "class_method" || what == "class_method_desc") {
  1469. if (method_line.has(name)) {
  1470. line = method_line[name];
  1471. }
  1472. } else if (what == "class_property") {
  1473. if (property_line.has(name)) {
  1474. line = property_line[name];
  1475. }
  1476. } else if (what == "class_enum") {
  1477. if (enum_line.has(name)) {
  1478. line = enum_line[name];
  1479. }
  1480. } else if (what == "class_theme_item") {
  1481. if (theme_property_line.has(name)) {
  1482. line = theme_property_line[name];
  1483. }
  1484. } else if (what == "class_constant") {
  1485. if (constant_line.has(name)) {
  1486. line = constant_line[name];
  1487. }
  1488. } else if (what == "class_annotation") {
  1489. if (annotation_line.has(name)) {
  1490. line = annotation_line[name];
  1491. }
  1492. } else if (what == "class_global") {
  1493. if (constant_line.has(name)) {
  1494. line = constant_line[name];
  1495. } else if (method_line.has(name)) {
  1496. line = method_line[name];
  1497. } else {
  1498. HashMap<String, HashMap<String, int>>::Iterator iter = enum_values_line.begin();
  1499. while (true) {
  1500. if (iter->value.has(name)) {
  1501. line = iter->value[name];
  1502. break;
  1503. } else if (iter == enum_values_line.last()) {
  1504. break;
  1505. } else {
  1506. ++iter;
  1507. }
  1508. }
  1509. }
  1510. }
  1511. if (class_desc->is_ready()) {
  1512. class_desc->call_deferred(SNAME("scroll_to_paragraph"), line);
  1513. } else {
  1514. scroll_to = line;
  1515. }
  1516. }
  1517. static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt, Control *p_owner_node) {
  1518. DocTools *doc = EditorHelp::get_doc_data();
  1519. String base_path;
  1520. Ref<Font> doc_font = p_owner_node->get_theme_font(SNAME("doc"), SNAME("EditorFonts"));
  1521. Ref<Font> doc_bold_font = p_owner_node->get_theme_font(SNAME("doc_bold"), SNAME("EditorFonts"));
  1522. Ref<Font> doc_italic_font = p_owner_node->get_theme_font(SNAME("doc_italic"), SNAME("EditorFonts"));
  1523. Ref<Font> doc_code_font = p_owner_node->get_theme_font(SNAME("doc_source"), SNAME("EditorFonts"));
  1524. Ref<Font> doc_kbd_font = p_owner_node->get_theme_font(SNAME("doc_keyboard"), SNAME("EditorFonts"));
  1525. int doc_code_font_size = p_owner_node->get_theme_font_size(SNAME("doc_source_size"), SNAME("EditorFonts"));
  1526. int doc_kbd_font_size = p_owner_node->get_theme_font_size(SNAME("doc_keyboard_size"), SNAME("EditorFonts"));
  1527. const Color type_color = p_owner_node->get_theme_color(SNAME("type_color"), SNAME("EditorHelp"));
  1528. const Color code_color = p_owner_node->get_theme_color(SNAME("code_color"), SNAME("EditorHelp"));
  1529. const Color kbd_color = p_owner_node->get_theme_color(SNAME("kbd_color"), SNAME("EditorHelp"));
  1530. const Color code_dark_color = Color(code_color, 0.8);
  1531. const Color link_color = p_owner_node->get_theme_color(SNAME("link_color"), SNAME("EditorHelp"));
  1532. const Color link_method_color = p_owner_node->get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  1533. const Color link_property_color = link_color.lerp(p_owner_node->get_theme_color(SNAME("accent_color"), SNAME("Editor")), 0.25);
  1534. const Color link_annotation_color = link_color.lerp(p_owner_node->get_theme_color(SNAME("accent_color"), SNAME("Editor")), 0.5);
  1535. const Color code_bg_color = p_owner_node->get_theme_color(SNAME("code_bg_color"), SNAME("EditorHelp"));
  1536. const Color kbd_bg_color = p_owner_node->get_theme_color(SNAME("kbd_bg_color"), SNAME("EditorHelp"));
  1537. const Color param_bg_color = p_owner_node->get_theme_color(SNAME("param_bg_color"), SNAME("EditorHelp"));
  1538. String bbcode = p_bbcode.dedent().replace("\t", "").replace("\r", "").strip_edges();
  1539. // Select the correct code examples.
  1540. switch ((int)EDITOR_GET("text_editor/help/class_reference_examples")) {
  1541. case 0: // GDScript
  1542. bbcode = bbcode.replace("[gdscript]", "[codeblock]");
  1543. bbcode = bbcode.replace("[/gdscript]", "[/codeblock]");
  1544. for (int pos = bbcode.find("[csharp]"); pos != -1; pos = bbcode.find("[csharp]")) {
  1545. int end_pos = bbcode.find("[/csharp]");
  1546. if (end_pos == -1) {
  1547. WARN_PRINT("Unclosed [csharp] block or parse fail in code (search for tag errors)");
  1548. break;
  1549. }
  1550. bbcode = bbcode.left(pos) + bbcode.substr(end_pos + 9); // 9 is length of "[/csharp]".
  1551. while (bbcode[pos] == '\n') {
  1552. bbcode = bbcode.left(pos) + bbcode.substr(pos + 1);
  1553. }
  1554. }
  1555. break;
  1556. case 1: // C#
  1557. bbcode = bbcode.replace("[csharp]", "[codeblock]");
  1558. bbcode = bbcode.replace("[/csharp]", "[/codeblock]");
  1559. for (int pos = bbcode.find("[gdscript]"); pos != -1; pos = bbcode.find("[gdscript]")) {
  1560. int end_pos = bbcode.find("[/gdscript]");
  1561. if (end_pos == -1) {
  1562. WARN_PRINT("Unclosed [gdscript] block or parse fail in code (search for tag errors)");
  1563. break;
  1564. }
  1565. bbcode = bbcode.left(pos) + bbcode.substr(end_pos + 11); // 11 is length of "[/gdscript]".
  1566. while (bbcode[pos] == '\n') {
  1567. bbcode = bbcode.left(pos) + bbcode.substr(pos + 1);
  1568. }
  1569. }
  1570. break;
  1571. case 2: // GDScript and C#
  1572. bbcode = bbcode.replace("[csharp]", "[b]C#:[/b]\n[codeblock]");
  1573. bbcode = bbcode.replace("[gdscript]", "[b]GDScript:[/b]\n[codeblock]");
  1574. bbcode = bbcode.replace("[/csharp]", "[/codeblock]");
  1575. bbcode = bbcode.replace("[/gdscript]", "[/codeblock]");
  1576. break;
  1577. }
  1578. // Remove codeblocks (they would be printed otherwise).
  1579. bbcode = bbcode.replace("[codeblocks]\n", "");
  1580. bbcode = bbcode.replace("\n[/codeblocks]", "");
  1581. bbcode = bbcode.replace("[codeblocks]", "");
  1582. bbcode = bbcode.replace("[/codeblocks]", "");
  1583. // Remove extra new lines around code blocks.
  1584. bbcode = bbcode.replace("[codeblock]\n", "[codeblock]");
  1585. bbcode = bbcode.replace("\n[/codeblock]", "[/codeblock]");
  1586. List<String> tag_stack;
  1587. bool code_tag = false;
  1588. bool codeblock_tag = false;
  1589. int pos = 0;
  1590. while (pos < bbcode.length()) {
  1591. int brk_pos = bbcode.find("[", pos);
  1592. if (brk_pos < 0) {
  1593. brk_pos = bbcode.length();
  1594. }
  1595. if (brk_pos > pos) {
  1596. String text = bbcode.substr(pos, brk_pos - pos);
  1597. if (!code_tag && !codeblock_tag) {
  1598. text = text.replace("\n", "\n\n");
  1599. }
  1600. p_rt->add_text(text);
  1601. }
  1602. if (brk_pos == bbcode.length()) {
  1603. break; // Nothing else to add.
  1604. }
  1605. int brk_end = bbcode.find("]", brk_pos + 1);
  1606. if (brk_end == -1) {
  1607. String text = bbcode.substr(brk_pos, bbcode.length() - brk_pos);
  1608. if (!code_tag && !codeblock_tag) {
  1609. text = text.replace("\n", "\n\n");
  1610. }
  1611. p_rt->add_text(text);
  1612. break;
  1613. }
  1614. String tag = bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
  1615. if (tag.begins_with("/")) {
  1616. bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length());
  1617. if (!tag_ok) {
  1618. p_rt->add_text("[");
  1619. pos = brk_pos + 1;
  1620. continue;
  1621. }
  1622. tag_stack.pop_front();
  1623. pos = brk_end + 1;
  1624. if (tag != "/img") {
  1625. p_rt->pop();
  1626. if (code_tag) {
  1627. p_rt->pop(); // font size
  1628. // Pop both color and background color.
  1629. p_rt->pop();
  1630. p_rt->pop();
  1631. } else if (codeblock_tag) {
  1632. p_rt->pop(); // font size
  1633. // Pop color, cell and table.
  1634. p_rt->pop();
  1635. p_rt->pop();
  1636. p_rt->pop();
  1637. }
  1638. }
  1639. code_tag = false;
  1640. codeblock_tag = false;
  1641. } else if (code_tag || codeblock_tag) {
  1642. p_rt->add_text("[");
  1643. pos = brk_pos + 1;
  1644. } 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("annotation ") || tag.begins_with("theme_item ")) {
  1645. const int tag_end = tag.find(" ");
  1646. const String link_tag = tag.substr(0, tag_end);
  1647. const String link_target = tag.substr(tag_end + 1, tag.length()).lstrip(" ");
  1648. // Use monospace font to make clickable references
  1649. // easier to distinguish from inline code and other text.
  1650. p_rt->push_font(doc_code_font);
  1651. p_rt->push_font_size(doc_code_font_size);
  1652. Color target_color = link_color;
  1653. if (link_tag == "method") {
  1654. target_color = link_method_color;
  1655. } else if (link_tag == "member" || link_tag == "signal" || link_tag == "theme property") {
  1656. target_color = link_property_color;
  1657. } else if (link_tag == "annotation") {
  1658. target_color = link_annotation_color;
  1659. }
  1660. p_rt->push_color(target_color);
  1661. p_rt->push_meta("@" + link_tag + " " + link_target);
  1662. p_rt->add_text(link_target + (link_tag == "method" ? "()" : ""));
  1663. p_rt->pop();
  1664. p_rt->pop();
  1665. p_rt->pop(); // font size
  1666. p_rt->pop(); // font
  1667. pos = brk_end + 1;
  1668. } else if (tag.begins_with("param ")) {
  1669. const int tag_end = tag.find(" ");
  1670. const String param_name = tag.substr(tag_end + 1, tag.length()).lstrip(" ");
  1671. // Use monospace font with translucent background color to make code easier to distinguish from other text.
  1672. p_rt->push_font(doc_code_font);
  1673. p_rt->push_font_size(doc_code_font_size);
  1674. p_rt->push_bgcolor(param_bg_color);
  1675. p_rt->push_color(code_color);
  1676. p_rt->add_text(param_name);
  1677. p_rt->pop();
  1678. p_rt->pop();
  1679. p_rt->pop(); // font size
  1680. p_rt->pop(); // font
  1681. pos = brk_end + 1;
  1682. } else if (doc->class_list.has(tag)) {
  1683. // Class reference tag such as [Node2D] or [SceneTree].
  1684. // Use monospace font to make clickable references
  1685. // easier to distinguish from inline code and other text.
  1686. p_rt->push_font(doc_code_font);
  1687. p_rt->push_font_size(doc_code_font_size);
  1688. p_rt->push_color(type_color);
  1689. p_rt->push_meta("#" + tag);
  1690. p_rt->add_text(tag);
  1691. p_rt->pop();
  1692. p_rt->pop();
  1693. p_rt->pop(); // font size
  1694. p_rt->pop(); // font
  1695. pos = brk_end + 1;
  1696. } else if (tag == "b") {
  1697. // Use bold font.
  1698. p_rt->push_font(doc_bold_font);
  1699. pos = brk_end + 1;
  1700. tag_stack.push_front(tag);
  1701. } else if (tag == "i") {
  1702. // Use italics font.
  1703. p_rt->push_font(doc_italic_font);
  1704. pos = brk_end + 1;
  1705. tag_stack.push_front(tag);
  1706. } else if (tag == "code") {
  1707. // Use monospace font with darkened background color to make code easier to distinguish from other text.
  1708. p_rt->push_font(doc_code_font);
  1709. p_rt->push_font_size(doc_code_font_size);
  1710. p_rt->push_bgcolor(code_bg_color);
  1711. p_rt->push_color(code_color.lerp(p_owner_node->get_theme_color(SNAME("error_color"), SNAME("Editor")), 0.6));
  1712. code_tag = true;
  1713. pos = brk_end + 1;
  1714. tag_stack.push_front(tag);
  1715. } else if (tag == "codeblock") {
  1716. // Use monospace font with darkened background color to make code easier to distinguish from other text.
  1717. // Use a single-column table with cell row background color instead of `[bgcolor]`.
  1718. // This makes the background color highlight cover the entire block, rather than individual lines.
  1719. p_rt->push_font(doc_code_font);
  1720. p_rt->push_font_size(doc_code_font_size);
  1721. p_rt->push_table(1);
  1722. p_rt->push_cell();
  1723. p_rt->set_cell_row_background_color(code_bg_color, Color(code_bg_color, 0.99));
  1724. p_rt->set_cell_padding(Rect2(10 * EDSCALE, 10 * EDSCALE, 10 * EDSCALE, 10 * EDSCALE));
  1725. p_rt->push_color(code_dark_color);
  1726. codeblock_tag = true;
  1727. pos = brk_end + 1;
  1728. tag_stack.push_front(tag);
  1729. } else if (tag == "kbd") {
  1730. // Use keyboard font with custom color and background color.
  1731. p_rt->push_font(doc_kbd_font);
  1732. p_rt->push_font_size(doc_kbd_font_size);
  1733. p_rt->push_bgcolor(kbd_bg_color);
  1734. p_rt->push_color(kbd_color);
  1735. code_tag = true; // Though not strictly a code tag, logic is similar.
  1736. pos = brk_end + 1;
  1737. tag_stack.push_front(tag);
  1738. } else if (tag == "center") {
  1739. // Align to center.
  1740. p_rt->push_paragraph(HORIZONTAL_ALIGNMENT_CENTER, Control::TEXT_DIRECTION_AUTO, "");
  1741. pos = brk_end + 1;
  1742. tag_stack.push_front(tag);
  1743. } else if (tag == "br") {
  1744. // Force a line break.
  1745. p_rt->add_newline();
  1746. pos = brk_end + 1;
  1747. } else if (tag == "u") {
  1748. // Use underline.
  1749. p_rt->push_underline();
  1750. pos = brk_end + 1;
  1751. tag_stack.push_front(tag);
  1752. } else if (tag == "s") {
  1753. // Use strikethrough.
  1754. p_rt->push_strikethrough();
  1755. pos = brk_end + 1;
  1756. tag_stack.push_front(tag);
  1757. } else if (tag == "url") {
  1758. int end = bbcode.find("[", brk_end);
  1759. if (end == -1) {
  1760. end = bbcode.length();
  1761. }
  1762. String url = bbcode.substr(brk_end + 1, end - brk_end - 1);
  1763. p_rt->push_meta(url);
  1764. pos = brk_end + 1;
  1765. tag_stack.push_front(tag);
  1766. } else if (tag.begins_with("url=")) {
  1767. String url = tag.substr(4, tag.length());
  1768. p_rt->push_meta(url);
  1769. pos = brk_end + 1;
  1770. tag_stack.push_front("url");
  1771. } else if (tag == "img") {
  1772. int end = bbcode.find("[", brk_end);
  1773. if (end == -1) {
  1774. end = bbcode.length();
  1775. }
  1776. String image = bbcode.substr(brk_end + 1, end - brk_end - 1);
  1777. Ref<Texture2D> texture = ResourceLoader::load(base_path.path_join(image), "Texture2D");
  1778. if (texture.is_valid()) {
  1779. p_rt->add_image(texture);
  1780. }
  1781. pos = end;
  1782. tag_stack.push_front(tag);
  1783. } else if (tag.begins_with("color=")) {
  1784. String col = tag.substr(6, tag.length());
  1785. Color color = Color::from_string(col, Color());
  1786. p_rt->push_color(color);
  1787. pos = brk_end + 1;
  1788. tag_stack.push_front("color");
  1789. } else if (tag.begins_with("font=")) {
  1790. String fnt = tag.substr(5, tag.length());
  1791. Ref<Font> font = ResourceLoader::load(base_path.path_join(fnt), "Font");
  1792. if (font.is_valid()) {
  1793. p_rt->push_font(font);
  1794. } else {
  1795. p_rt->push_font(doc_font);
  1796. }
  1797. pos = brk_end + 1;
  1798. tag_stack.push_front("font");
  1799. } else {
  1800. p_rt->add_text("["); //ignore
  1801. pos = brk_pos + 1;
  1802. }
  1803. }
  1804. }
  1805. void EditorHelp::_add_text(const String &p_bbcode) {
  1806. _add_text_to_rt(p_bbcode, class_desc, this);
  1807. }
  1808. Thread EditorHelp::thread;
  1809. void EditorHelp::_wait_for_thread() {
  1810. if (thread.is_started()) {
  1811. thread.wait_to_finish();
  1812. }
  1813. }
  1814. void EditorHelp::_gen_doc_thread(void *p_udata) {
  1815. DocTools compdoc;
  1816. compdoc.load_compressed(_doc_data_compressed, _doc_data_compressed_size, _doc_data_uncompressed_size);
  1817. doc->merge_from(compdoc); // Ensure all is up to date.
  1818. }
  1819. static bool doc_gen_use_threads = true;
  1820. void EditorHelp::generate_doc() {
  1821. doc = memnew(DocTools);
  1822. // Not doable on threads unfortunately, since it instantiates all sorts of classes to get default values.
  1823. doc->generate(true);
  1824. if (doc_gen_use_threads) {
  1825. thread.start(_gen_doc_thread, nullptr);
  1826. } else {
  1827. _gen_doc_thread(nullptr);
  1828. }
  1829. }
  1830. void EditorHelp::_toggle_scripts_pressed() {
  1831. ScriptEditor::get_singleton()->toggle_scripts_panel();
  1832. update_toggle_scripts_button();
  1833. }
  1834. void EditorHelp::_notification(int p_what) {
  1835. switch (p_what) {
  1836. case NOTIFICATION_POSTINITIALIZE: {
  1837. // Requires theme to be up to date.
  1838. _class_desc_resized(false);
  1839. } break;
  1840. case NOTIFICATION_READY:
  1841. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1842. _wait_for_thread();
  1843. _update_doc();
  1844. } break;
  1845. case NOTIFICATION_THEME_CHANGED: {
  1846. if (is_inside_tree()) {
  1847. _class_desc_resized(true);
  1848. }
  1849. update_toggle_scripts_button();
  1850. } break;
  1851. case NOTIFICATION_VISIBILITY_CHANGED: {
  1852. update_toggle_scripts_button();
  1853. } break;
  1854. }
  1855. }
  1856. void EditorHelp::go_to_help(const String &p_help) {
  1857. _wait_for_thread();
  1858. _help_callback(p_help);
  1859. }
  1860. void EditorHelp::go_to_class(const String &p_class, int p_scroll) {
  1861. _wait_for_thread();
  1862. _goto_desc(p_class, p_scroll);
  1863. }
  1864. void EditorHelp::update_doc() {
  1865. _wait_for_thread();
  1866. ERR_FAIL_COND(!doc->class_list.has(edited_class));
  1867. ERR_FAIL_COND(!doc->class_list[edited_class].is_script_doc);
  1868. _update_doc();
  1869. }
  1870. void EditorHelp::cleanup_doc() {
  1871. _wait_for_thread();
  1872. if (doc_gen_use_threads) {
  1873. thread.wait_to_finish();
  1874. }
  1875. memdelete(doc);
  1876. }
  1877. Vector<Pair<String, int>> EditorHelp::get_sections() {
  1878. _wait_for_thread();
  1879. Vector<Pair<String, int>> sections;
  1880. for (int i = 0; i < section_line.size(); i++) {
  1881. sections.push_back(Pair<String, int>(section_line[i].first, i));
  1882. }
  1883. return sections;
  1884. }
  1885. void EditorHelp::scroll_to_section(int p_section_index) {
  1886. _wait_for_thread();
  1887. int line = section_line[p_section_index].second;
  1888. if (class_desc->is_ready()) {
  1889. class_desc->scroll_to_paragraph(line);
  1890. } else {
  1891. scroll_to = line;
  1892. }
  1893. }
  1894. void EditorHelp::popup_search() {
  1895. _wait_for_thread();
  1896. find_bar->popup_search();
  1897. }
  1898. String EditorHelp::get_class() {
  1899. return edited_class;
  1900. }
  1901. void EditorHelp::search_again(bool p_search_previous) {
  1902. _search(p_search_previous);
  1903. }
  1904. int EditorHelp::get_scroll() const {
  1905. return class_desc->get_v_scroll_bar()->get_value();
  1906. }
  1907. void EditorHelp::set_scroll(int p_scroll) {
  1908. class_desc->get_v_scroll_bar()->set_value(p_scroll);
  1909. }
  1910. void EditorHelp::update_toggle_scripts_button() {
  1911. if (is_layout_rtl()) {
  1912. toggle_scripts_button->set_icon(get_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Forward") : SNAME("Back"), SNAME("EditorIcons")));
  1913. } else {
  1914. toggle_scripts_button->set_icon(get_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Back") : SNAME("Forward"), SNAME("EditorIcons")));
  1915. }
  1916. toggle_scripts_button->set_tooltip_text(vformat("%s (%s)", TTR("Toggle Scripts Panel"), ED_GET_SHORTCUT("script_editor/toggle_scripts_panel")->get_as_text()));
  1917. }
  1918. void EditorHelp::_bind_methods() {
  1919. ClassDB::bind_method("_class_list_select", &EditorHelp::_class_list_select);
  1920. ClassDB::bind_method("_request_help", &EditorHelp::_request_help);
  1921. ClassDB::bind_method("_search", &EditorHelp::_search);
  1922. ClassDB::bind_method("_help_callback", &EditorHelp::_help_callback);
  1923. ADD_SIGNAL(MethodInfo("go_to_help"));
  1924. }
  1925. EditorHelp::EditorHelp() {
  1926. set_custom_minimum_size(Size2(150 * EDSCALE, 0));
  1927. EDITOR_DEF("text_editor/help/sort_functions_alphabetically", true);
  1928. class_desc = memnew(RichTextLabel);
  1929. add_child(class_desc);
  1930. class_desc->set_threaded(true);
  1931. class_desc->set_v_size_flags(SIZE_EXPAND_FILL);
  1932. class_desc->connect("finished", callable_mp(this, &EditorHelp::_class_desc_finished));
  1933. class_desc->connect("meta_clicked", callable_mp(this, &EditorHelp::_class_desc_select));
  1934. class_desc->connect("gui_input", callable_mp(this, &EditorHelp::_class_desc_input));
  1935. class_desc->connect("resized", callable_mp(this, &EditorHelp::_class_desc_resized).bind(false));
  1936. // Added second so it opens at the bottom so it won't offset the entire widget.
  1937. find_bar = memnew(FindBar);
  1938. add_child(find_bar);
  1939. find_bar->hide();
  1940. find_bar->set_rich_text_label(class_desc);
  1941. status_bar = memnew(HBoxContainer);
  1942. add_child(status_bar);
  1943. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  1944. status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE));
  1945. toggle_scripts_button = memnew(Button);
  1946. toggle_scripts_button->set_flat(true);
  1947. toggle_scripts_button->connect("pressed", callable_mp(this, &EditorHelp::_toggle_scripts_pressed));
  1948. status_bar->add_child(toggle_scripts_button);
  1949. class_desc->set_selection_enabled(true);
  1950. class_desc->hide();
  1951. }
  1952. EditorHelp::~EditorHelp() {
  1953. }
  1954. DocTools *EditorHelp::get_doc_data() {
  1955. _wait_for_thread();
  1956. return doc;
  1957. }
  1958. //// EditorHelpBit ///
  1959. void EditorHelpBit::_go_to_help(String p_what) {
  1960. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  1961. ScriptEditor::get_singleton()->goto_help(p_what);
  1962. emit_signal(SNAME("request_hide"));
  1963. }
  1964. void EditorHelpBit::_meta_clicked(String p_select) {
  1965. if (p_select.begins_with("$")) { //enum
  1966. String select = p_select.substr(1, p_select.length());
  1967. String class_name;
  1968. if (select.contains(".")) {
  1969. class_name = select.get_slice(".", 0);
  1970. } else {
  1971. class_name = "@Global";
  1972. }
  1973. _go_to_help("class_enum:" + class_name + ":" + select);
  1974. return;
  1975. } else if (p_select.begins_with("#")) {
  1976. _go_to_help("class_name:" + p_select.substr(1, p_select.length()));
  1977. return;
  1978. } else if (p_select.begins_with("@")) {
  1979. String m = p_select.substr(1, p_select.length());
  1980. if (m.contains(".")) {
  1981. _go_to_help("class_method:" + m.get_slice(".", 0) + ":" + m.get_slice(".", 0)); // Must go somewhere else.
  1982. }
  1983. }
  1984. }
  1985. void EditorHelpBit::_bind_methods() {
  1986. ClassDB::bind_method(D_METHOD("set_text", "text"), &EditorHelpBit::set_text);
  1987. ADD_SIGNAL(MethodInfo("request_hide"));
  1988. }
  1989. void EditorHelpBit::_notification(int p_what) {
  1990. switch (p_what) {
  1991. case NOTIFICATION_THEME_CHANGED: {
  1992. rich_text->add_theme_color_override("selection_color", get_theme_color(SNAME("selection_color"), SNAME("EditorHelp")));
  1993. rich_text->clear();
  1994. _add_text_to_rt(text, rich_text, this);
  1995. rich_text->reset_size(); // Force recalculating size after parsing bbcode.
  1996. } break;
  1997. }
  1998. }
  1999. void EditorHelpBit::set_text(const String &p_text) {
  2000. text = p_text;
  2001. rich_text->clear();
  2002. _add_text_to_rt(text, rich_text, this);
  2003. }
  2004. EditorHelpBit::EditorHelpBit() {
  2005. rich_text = memnew(RichTextLabel);
  2006. add_child(rich_text);
  2007. rich_text->connect("meta_clicked", callable_mp(this, &EditorHelpBit::_meta_clicked));
  2008. rich_text->set_fit_content(true);
  2009. set_custom_minimum_size(Size2(0, 50 * EDSCALE));
  2010. }
  2011. //// FindBar ///
  2012. FindBar::FindBar() {
  2013. search_text = memnew(LineEdit);
  2014. add_child(search_text);
  2015. search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  2016. search_text->set_h_size_flags(SIZE_EXPAND_FILL);
  2017. search_text->connect("text_changed", callable_mp(this, &FindBar::_search_text_changed));
  2018. search_text->connect("text_submitted", callable_mp(this, &FindBar::_search_text_submitted));
  2019. matches_label = memnew(Label);
  2020. add_child(matches_label);
  2021. matches_label->hide();
  2022. find_prev = memnew(Button);
  2023. find_prev->set_flat(true);
  2024. add_child(find_prev);
  2025. find_prev->set_focus_mode(FOCUS_NONE);
  2026. find_prev->connect("pressed", callable_mp(this, &FindBar::search_prev));
  2027. find_next = memnew(Button);
  2028. find_next->set_flat(true);
  2029. add_child(find_next);
  2030. find_next->set_focus_mode(FOCUS_NONE);
  2031. find_next->connect("pressed", callable_mp(this, &FindBar::search_next));
  2032. Control *space = memnew(Control);
  2033. add_child(space);
  2034. space->set_custom_minimum_size(Size2(4, 0) * EDSCALE);
  2035. hide_button = memnew(TextureButton);
  2036. add_child(hide_button);
  2037. hide_button->set_focus_mode(FOCUS_NONE);
  2038. hide_button->set_ignore_texture_size(true);
  2039. hide_button->set_stretch_mode(TextureButton::STRETCH_KEEP_CENTERED);
  2040. hide_button->connect("pressed", callable_mp(this, &FindBar::_hide_bar));
  2041. }
  2042. void FindBar::popup_search() {
  2043. show();
  2044. bool grabbed_focus = false;
  2045. if (!search_text->has_focus()) {
  2046. search_text->grab_focus();
  2047. grabbed_focus = true;
  2048. }
  2049. if (!search_text->get_text().is_empty()) {
  2050. search_text->select_all();
  2051. search_text->set_caret_column(search_text->get_text().length());
  2052. if (grabbed_focus) {
  2053. _search();
  2054. }
  2055. }
  2056. }
  2057. void FindBar::_notification(int p_what) {
  2058. switch (p_what) {
  2059. case NOTIFICATION_THEME_CHANGED: {
  2060. find_prev->set_icon(get_theme_icon(SNAME("MoveUp"), SNAME("EditorIcons")));
  2061. find_next->set_icon(get_theme_icon(SNAME("MoveDown"), SNAME("EditorIcons")));
  2062. hide_button->set_texture_normal(get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  2063. hide_button->set_texture_hover(get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  2064. hide_button->set_texture_pressed(get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  2065. hide_button->set_custom_minimum_size(hide_button->get_texture_normal()->get_size());
  2066. 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")));
  2067. } break;
  2068. case NOTIFICATION_VISIBILITY_CHANGED: {
  2069. set_process_unhandled_input(is_visible_in_tree());
  2070. } break;
  2071. }
  2072. }
  2073. void FindBar::set_rich_text_label(RichTextLabel *p_rich_text_label) {
  2074. rich_text_label = p_rich_text_label;
  2075. }
  2076. bool FindBar::search_next() {
  2077. return _search();
  2078. }
  2079. bool FindBar::search_prev() {
  2080. return _search(true);
  2081. }
  2082. bool FindBar::_search(bool p_search_previous) {
  2083. String stext = search_text->get_text();
  2084. bool keep = prev_search == stext;
  2085. bool ret = rich_text_label->search(stext, keep, p_search_previous);
  2086. prev_search = stext;
  2087. if (ret) {
  2088. _update_results_count();
  2089. } else {
  2090. results_count = 0;
  2091. }
  2092. _update_matches_label();
  2093. return ret;
  2094. }
  2095. void FindBar::_update_results_count() {
  2096. results_count = 0;
  2097. String searched = search_text->get_text();
  2098. if (searched.is_empty()) {
  2099. return;
  2100. }
  2101. String full_text = rich_text_label->get_parsed_text();
  2102. int from_pos = 0;
  2103. while (true) {
  2104. int pos = full_text.findn(searched, from_pos);
  2105. if (pos == -1) {
  2106. break;
  2107. }
  2108. results_count++;
  2109. from_pos = pos + searched.length();
  2110. }
  2111. }
  2112. void FindBar::_update_matches_label() {
  2113. if (search_text->get_text().is_empty() || results_count == -1) {
  2114. matches_label->hide();
  2115. } else {
  2116. matches_label->show();
  2117. 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")));
  2118. matches_label->set_text(vformat(results_count == 1 ? TTR("%d match.") : TTR("%d matches."), results_count));
  2119. }
  2120. }
  2121. void FindBar::_hide_bar() {
  2122. if (search_text->has_focus()) {
  2123. rich_text_label->grab_focus();
  2124. }
  2125. hide();
  2126. }
  2127. void FindBar::unhandled_input(const Ref<InputEvent> &p_event) {
  2128. ERR_FAIL_COND(p_event.is_null());
  2129. Ref<InputEventKey> k = p_event;
  2130. if (k.is_valid()) {
  2131. if (k->is_pressed() && (rich_text_label->has_focus() || is_ancestor_of(get_viewport()->gui_get_focus_owner()))) {
  2132. bool accepted = true;
  2133. switch (k->get_keycode()) {
  2134. case Key::ESCAPE: {
  2135. _hide_bar();
  2136. } break;
  2137. default: {
  2138. accepted = false;
  2139. } break;
  2140. }
  2141. if (accepted) {
  2142. accept_event();
  2143. }
  2144. }
  2145. }
  2146. }
  2147. void FindBar::_search_text_changed(const String &p_text) {
  2148. search_next();
  2149. }
  2150. void FindBar::_search_text_submitted(const String &p_text) {
  2151. if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  2152. search_prev();
  2153. } else {
  2154. search_next();
  2155. }
  2156. }