editor_help.cpp 46 KB

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