editor_help.cpp 51 KB

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