editor_help.cpp 47 KB

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