editor_help.cpp 50 KB

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