rich_text_label.cpp 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048
  1. /*************************************************************************/
  2. /* rich_text_label.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "rich_text_label.h"
  31. #include "core/math/math_defs.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/os/os.h"
  34. #include "modules/regex/regex.h"
  35. #include "scene/scene_string_names.h"
  36. #ifdef TOOLS_ENABLED
  37. #include "editor/editor_scale.h"
  38. #endif
  39. RichTextLabel::Item *RichTextLabel::_get_next_item(Item *p_item, bool p_free) {
  40. if (p_free) {
  41. if (p_item->subitems.size()) {
  42. return p_item->subitems.front()->get();
  43. } else if (!p_item->parent) {
  44. return NULL;
  45. } else if (p_item->E->next()) {
  46. return p_item->E->next()->get();
  47. } else {
  48. //go up until something with a next is found
  49. while (p_item->parent && !p_item->E->next()) {
  50. p_item = p_item->parent;
  51. }
  52. if (p_item->parent)
  53. return p_item->E->next()->get();
  54. else
  55. return NULL;
  56. }
  57. } else {
  58. if (p_item->subitems.size() && p_item->type != ITEM_TABLE) {
  59. return p_item->subitems.front()->get();
  60. } else if (p_item->type == ITEM_FRAME) {
  61. return NULL;
  62. } else if (p_item->E->next()) {
  63. return p_item->E->next()->get();
  64. } else {
  65. //go up until something with a next is found
  66. while (p_item->type != ITEM_FRAME && !p_item->E->next()) {
  67. p_item = p_item->parent;
  68. }
  69. if (p_item->type != ITEM_FRAME)
  70. return p_item->E->next()->get();
  71. else
  72. return NULL;
  73. }
  74. }
  75. return NULL;
  76. }
  77. RichTextLabel::Item *RichTextLabel::_get_prev_item(Item *p_item, bool p_free) {
  78. if (p_free) {
  79. if (p_item->subitems.size()) {
  80. return p_item->subitems.back()->get();
  81. } else if (!p_item->parent) {
  82. return NULL;
  83. } else if (p_item->E->prev()) {
  84. return p_item->E->prev()->get();
  85. } else {
  86. //go back until something with a prev is found
  87. while (p_item->parent && !p_item->E->prev()) {
  88. p_item = p_item->parent;
  89. }
  90. if (p_item->parent)
  91. return p_item->E->prev()->get();
  92. else
  93. return NULL;
  94. }
  95. } else {
  96. if (p_item->subitems.size() && p_item->type != ITEM_TABLE) {
  97. return p_item->subitems.back()->get();
  98. } else if (p_item->type == ITEM_FRAME) {
  99. return NULL;
  100. } else if (p_item->E->prev()) {
  101. return p_item->E->prev()->get();
  102. } else {
  103. //go back until something with a prev is found
  104. while (p_item->type != ITEM_FRAME && !p_item->E->prev()) {
  105. p_item = p_item->parent;
  106. }
  107. if (p_item->type != ITEM_FRAME)
  108. return p_item->E->prev()->get();
  109. else
  110. return NULL;
  111. }
  112. }
  113. return NULL;
  114. }
  115. Rect2 RichTextLabel::_get_text_rect() {
  116. Ref<StyleBox> style = get_stylebox("normal");
  117. return Rect2(style->get_offset(), get_size() - style->get_minimum_size());
  118. }
  119. int RichTextLabel::_process_line(ItemFrame *p_frame, const Vector2 &p_ofs, int &y, int p_width, int p_line, ProcessMode p_mode, const Ref<Font> &p_base_font, const Color &p_base_color, const Color &p_font_color_shadow, bool p_shadow_as_outline, const Point2 &shadow_ofs, const Point2i &p_click_pos, Item **r_click_item, int *r_click_char, bool *r_outside, int p_char_count) {
  120. ERR_FAIL_INDEX_V((int)p_mode, 3, 0);
  121. RID ci;
  122. if (r_outside)
  123. *r_outside = false;
  124. if (p_mode == PROCESS_DRAW) {
  125. ci = get_canvas_item();
  126. if (r_click_item)
  127. *r_click_item = NULL;
  128. }
  129. Line &l = p_frame->lines.write[p_line];
  130. Item *it = l.from;
  131. int line_ofs = 0;
  132. int margin = _find_margin(it, p_base_font);
  133. Align align = _find_align(it);
  134. int line = 0;
  135. int spaces = 0;
  136. int height = get_size().y;
  137. if (p_mode != PROCESS_CACHE) {
  138. ERR_FAIL_INDEX_V(line, l.offset_caches.size(), 0);
  139. line_ofs = l.offset_caches[line];
  140. }
  141. if (p_mode == PROCESS_CACHE) {
  142. l.offset_caches.clear();
  143. l.height_caches.clear();
  144. l.ascent_caches.clear();
  145. l.descent_caches.clear();
  146. l.char_count = 0;
  147. l.minimum_width = 0;
  148. l.maximum_width = 0;
  149. }
  150. int wofs = margin;
  151. int spaces_size = 0;
  152. int align_ofs = 0;
  153. if (p_mode != PROCESS_CACHE && align != ALIGN_FILL)
  154. wofs += line_ofs;
  155. int begin = margin;
  156. Ref<Font> cfont = _find_font(it);
  157. if (cfont.is_null())
  158. cfont = p_base_font;
  159. //line height should be the font height for the first time, this ensures that an empty line will never have zero height and successive newlines are displayed
  160. int line_height = cfont->get_height();
  161. int line_ascent = cfont->get_ascent();
  162. int line_descent = cfont->get_descent();
  163. int backtrack = 0; // for dynamic hidden content.
  164. int nonblank_line_count = 0; //number of nonblank lines as counted during PROCESS_DRAW
  165. Variant meta;
  166. #define RETURN return nonblank_line_count
  167. #define NEW_LINE \
  168. { \
  169. if (p_mode != PROCESS_CACHE) { \
  170. line++; \
  171. backtrack = 0; \
  172. if (!line_is_blank) { \
  173. nonblank_line_count++; \
  174. } \
  175. line_is_blank = true; \
  176. if (line < l.offset_caches.size()) \
  177. line_ofs = l.offset_caches[line]; \
  178. wofs = margin; \
  179. if (align != ALIGN_FILL) \
  180. wofs += line_ofs; \
  181. } else { \
  182. int used = wofs - margin; \
  183. switch (align) { \
  184. case ALIGN_LEFT: l.offset_caches.push_back(0); break; \
  185. case ALIGN_CENTER: l.offset_caches.push_back(((p_width - margin) - used) / 2); break; \
  186. case ALIGN_RIGHT: l.offset_caches.push_back(((p_width - margin) - used)); break; \
  187. case ALIGN_FILL: l.offset_caches.push_back(line_wrapped ? ((p_width - margin) - used) : 0); break; \
  188. } \
  189. l.height_caches.push_back(line_height); \
  190. l.ascent_caches.push_back(line_ascent); \
  191. l.descent_caches.push_back(line_descent); \
  192. l.space_caches.push_back(spaces); \
  193. } \
  194. line_wrapped = false; \
  195. y += line_height + get_constant(SceneStringNames::get_singleton()->line_separation); \
  196. line_height = 0; \
  197. line_ascent = 0; \
  198. line_descent = 0; \
  199. spaces = 0; \
  200. spaces_size = 0; \
  201. wofs = begin; \
  202. align_ofs = 0; \
  203. if (p_mode != PROCESS_CACHE) { \
  204. lh = line < l.height_caches.size() ? l.height_caches[line] : 1; \
  205. line_ascent = line < l.ascent_caches.size() ? l.ascent_caches[line] : 1; \
  206. line_descent = line < l.descent_caches.size() ? l.descent_caches[line] : 1; \
  207. if (align != ALIGN_FILL) { \
  208. if (line < l.offset_caches.size()) { \
  209. wofs = l.offset_caches[line]; \
  210. } \
  211. } \
  212. } \
  213. if (p_mode == PROCESS_POINTER && r_click_item && p_click_pos.y >= p_ofs.y + y && p_click_pos.y <= p_ofs.y + y + lh && p_click_pos.x < p_ofs.x + wofs) { \
  214. if (r_outside) *r_outside = true; \
  215. *r_click_item = it; \
  216. *r_click_char = rchar; \
  217. RETURN; \
  218. } \
  219. }
  220. #define ENSURE_WIDTH(m_width) \
  221. if (p_mode == PROCESS_CACHE) { \
  222. l.maximum_width = MAX(l.maximum_width, MIN(p_width, wofs + m_width)); \
  223. l.minimum_width = MAX(l.minimum_width, m_width); \
  224. } \
  225. if (wofs - backtrack + m_width > p_width) { \
  226. line_wrapped = true; \
  227. if (p_mode == PROCESS_CACHE) { \
  228. if (spaces > 0) \
  229. spaces -= 1; \
  230. } \
  231. const bool x_in_range = (p_click_pos.x > p_ofs.x + wofs) && (!p_frame->cell || p_click_pos.x < p_ofs.x + p_width); \
  232. if (p_mode == PROCESS_POINTER && r_click_item && p_click_pos.y >= p_ofs.y + y && p_click_pos.y <= p_ofs.y + y + lh && x_in_range) { \
  233. if (r_outside) *r_outside = true; \
  234. *r_click_item = it; \
  235. *r_click_char = rchar; \
  236. RETURN; \
  237. } \
  238. NEW_LINE \
  239. }
  240. #define ADVANCE(m_width) \
  241. { \
  242. if (p_mode == PROCESS_POINTER && r_click_item && p_click_pos.y >= p_ofs.y + y && p_click_pos.y <= p_ofs.y + y + lh && p_click_pos.x >= p_ofs.x + wofs && p_click_pos.x < p_ofs.x + wofs + m_width) { \
  243. if (r_outside) *r_outside = false; \
  244. *r_click_item = it; \
  245. *r_click_char = rchar; \
  246. RETURN; \
  247. } \
  248. wofs += m_width; \
  249. }
  250. #define CHECK_HEIGHT(m_height) \
  251. if (m_height > line_height) { \
  252. line_height = m_height; \
  253. }
  254. #define YRANGE_VISIBLE(m_top, m_height) \
  255. (m_height > 0 && ((m_top >= 0 && m_top < height) || ((m_top + m_height - 1) >= 0 && (m_top + m_height - 1) < height)))
  256. Color selection_fg;
  257. Color selection_bg;
  258. if (p_mode == PROCESS_DRAW) {
  259. selection_fg = get_color("font_color_selected");
  260. selection_bg = get_color("selection_color");
  261. }
  262. int rchar = 0;
  263. int lh = 0;
  264. bool line_is_blank = true;
  265. bool line_wrapped = false;
  266. int fh = 0;
  267. while (it) {
  268. switch (it->type) {
  269. case ITEM_ALIGN: {
  270. ItemAlign *align_it = static_cast<ItemAlign *>(it);
  271. align = align_it->align;
  272. } break;
  273. case ITEM_INDENT: {
  274. if (it != l.from) {
  275. ItemIndent *indent_it = static_cast<ItemIndent *>(it);
  276. int indent = indent_it->level * tab_size * cfont->get_char_size(' ').width;
  277. margin += indent;
  278. begin += indent;
  279. wofs += indent;
  280. }
  281. } break;
  282. case ITEM_TEXT: {
  283. ItemText *text = static_cast<ItemText *>(it);
  284. Ref<Font> font = _find_font(it);
  285. if (font.is_null())
  286. font = p_base_font;
  287. const CharType *c = text->text.c_str();
  288. const CharType *cf = c;
  289. int ascent = font->get_ascent();
  290. int descent = font->get_descent();
  291. Color color;
  292. Color font_color_shadow;
  293. bool underline = false;
  294. bool strikethrough = false;
  295. ItemFade *fade = NULL;
  296. int it_char_start = p_char_count;
  297. Vector<ItemFX *> fx_stack = Vector<ItemFX *>();
  298. _fetch_item_fx_stack(text, fx_stack);
  299. bool custom_fx_ok = true;
  300. if (p_mode == PROCESS_DRAW) {
  301. color = _find_color(text, p_base_color);
  302. font_color_shadow = _find_color(text, p_font_color_shadow);
  303. if (_find_underline(text) || (_find_meta(text, &meta) && underline_meta)) {
  304. underline = true;
  305. } else if (_find_strikethrough(text)) {
  306. strikethrough = true;
  307. }
  308. Item *fade_item = it;
  309. while (fade_item) {
  310. if (fade_item->type == ITEM_FADE) {
  311. fade = static_cast<ItemFade *>(fade_item);
  312. break;
  313. }
  314. fade_item = fade_item->parent;
  315. }
  316. } else if (p_mode == PROCESS_CACHE) {
  317. l.char_count += text->text.length();
  318. }
  319. rchar = 0;
  320. FontDrawer drawer(font, Color(1, 1, 1));
  321. while (*c) {
  322. int end = 0;
  323. int w = 0;
  324. int fw = 0;
  325. lh = 0;
  326. if (p_mode != PROCESS_CACHE) {
  327. lh = line < l.height_caches.size() ? l.height_caches[line] : 1;
  328. line_ascent = line < l.ascent_caches.size() ? l.ascent_caches[line] : 1;
  329. line_descent = line < l.descent_caches.size() ? l.descent_caches[line] : 1;
  330. }
  331. while (c[end] != 0 && !(end && c[end - 1] == ' ' && c[end] != ' ')) {
  332. int cw = font->get_char_size(c[end], c[end + 1]).width;
  333. if (c[end] == '\t') {
  334. cw = tab_size * font->get_char_size(' ').width;
  335. }
  336. if (end > 0 && w + cw + begin > p_width) {
  337. break; //don't allow lines longer than assigned width
  338. }
  339. w += cw;
  340. fw += cw;
  341. end++;
  342. }
  343. CHECK_HEIGHT(fh);
  344. ENSURE_WIDTH(w);
  345. line_ascent = MAX(line_ascent, ascent);
  346. line_descent = MAX(line_descent, descent);
  347. fh = line_ascent + line_descent;
  348. if (end && c[end - 1] == ' ') {
  349. if (p_mode == PROCESS_CACHE) {
  350. spaces_size += font->get_char_size(' ').width;
  351. } else if (align == ALIGN_FILL) {
  352. int ln = MIN(l.offset_caches.size() - 1, line);
  353. if (l.space_caches[ln]) {
  354. align_ofs = spaces * l.offset_caches[ln] / l.space_caches[ln];
  355. }
  356. }
  357. spaces++;
  358. }
  359. {
  360. int ofs = 0 - backtrack;
  361. for (int i = 0; i < end; i++) {
  362. int pofs = wofs + ofs;
  363. if (p_mode == PROCESS_POINTER && r_click_char && p_click_pos.y >= p_ofs.y + y && p_click_pos.y <= p_ofs.y + y + lh) {
  364. int cw = font->get_char_size(c[i], c[i + 1]).x;
  365. if (c[i] == '\t') {
  366. cw = tab_size * font->get_char_size(' ').width;
  367. }
  368. if (p_click_pos.x - cw / 2 > p_ofs.x + align_ofs + pofs) {
  369. rchar = int((&c[i]) - cf);
  370. }
  371. ofs += cw;
  372. } else if (p_mode == PROCESS_DRAW) {
  373. bool selected = false;
  374. Color fx_color = Color(color);
  375. Point2 fx_offset;
  376. CharType fx_char = c[i];
  377. if (selection.active) {
  378. int cofs = (&c[i]) - cf;
  379. if ((text->index > selection.from->index || (text->index == selection.from->index && cofs >= selection.from_char)) && (text->index < selection.to->index || (text->index == selection.to->index && cofs <= selection.to_char))) {
  380. selected = true;
  381. }
  382. }
  383. int cw = 0;
  384. int c_item_offset = p_char_count - it_char_start;
  385. float faded_visibility = 1.0f;
  386. if (fade) {
  387. if (c_item_offset >= fade->starting_index) {
  388. faded_visibility -= (float)(c_item_offset - fade->starting_index) / (float)fade->length;
  389. faded_visibility = faded_visibility < 0.0f ? 0.0f : faded_visibility;
  390. }
  391. fx_color.a = faded_visibility;
  392. }
  393. bool visible = visible_characters < 0 || ((p_char_count < visible_characters && YRANGE_VISIBLE(y + lh - line_descent - line_ascent, line_ascent + line_descent)) &&
  394. faded_visibility > 0.0f);
  395. const bool previously_visible = visible;
  396. for (int j = 0; j < fx_stack.size(); j++) {
  397. ItemFX *item_fx = fx_stack[j];
  398. if (item_fx->type == ITEM_CUSTOMFX && custom_fx_ok) {
  399. ItemCustomFX *item_custom = static_cast<ItemCustomFX *>(item_fx);
  400. Ref<CharFXTransform> charfx = item_custom->char_fx_transform;
  401. Ref<RichTextEffect> custom_effect = item_custom->custom_effect;
  402. if (!custom_effect.is_null()) {
  403. charfx->elapsed_time = item_custom->elapsed_time;
  404. charfx->relative_index = c_item_offset;
  405. charfx->absolute_index = p_char_count;
  406. charfx->visibility = visible;
  407. charfx->offset = fx_offset;
  408. charfx->color = fx_color;
  409. charfx->character = fx_char;
  410. bool effect_status = custom_effect->_process_effect_impl(charfx);
  411. custom_fx_ok = effect_status;
  412. fx_offset += charfx->offset;
  413. fx_color = charfx->color;
  414. visible &= charfx->visibility;
  415. fx_char = charfx->character;
  416. }
  417. } else if (item_fx->type == ITEM_SHAKE) {
  418. ItemShake *item_shake = static_cast<ItemShake *>(item_fx);
  419. uint64_t char_current_rand = item_shake->offset_random(c_item_offset);
  420. uint64_t char_previous_rand = item_shake->offset_previous_random(c_item_offset);
  421. uint64_t max_rand = 2147483647;
  422. double current_offset = Math::range_lerp(char_current_rand % max_rand, 0, max_rand, 0.0f, 2.f * (float)Math_PI);
  423. double previous_offset = Math::range_lerp(char_previous_rand % max_rand, 0, max_rand, 0.0f, 2.f * (float)Math_PI);
  424. double n_time = (double)(item_shake->elapsed_time / (0.5f / item_shake->rate));
  425. n_time = (n_time > 1.0) ? 1.0 : n_time;
  426. fx_offset += Point2(Math::lerp(Math::sin(previous_offset),
  427. Math::sin(current_offset),
  428. n_time),
  429. Math::lerp(Math::cos(previous_offset),
  430. Math::cos(current_offset),
  431. n_time)) *
  432. (float)item_shake->strength / 10.0f;
  433. } else if (item_fx->type == ITEM_WAVE) {
  434. ItemWave *item_wave = static_cast<ItemWave *>(item_fx);
  435. double value = Math::sin(item_wave->frequency * item_wave->elapsed_time + ((p_ofs.x + pofs) / 50)) * (item_wave->amplitude / 10.0f);
  436. fx_offset += Point2(0, 1) * value;
  437. } else if (item_fx->type == ITEM_TORNADO) {
  438. ItemTornado *item_tornado = static_cast<ItemTornado *>(item_fx);
  439. double torn_x = Math::sin(item_tornado->frequency * item_tornado->elapsed_time + ((p_ofs.x + pofs) / 50)) * (item_tornado->radius);
  440. double torn_y = Math::cos(item_tornado->frequency * item_tornado->elapsed_time + ((p_ofs.x + pofs) / 50)) * (item_tornado->radius);
  441. fx_offset += Point2(torn_x, torn_y);
  442. } else if (item_fx->type == ITEM_RAINBOW) {
  443. ItemRainbow *item_rainbow = static_cast<ItemRainbow *>(item_fx);
  444. fx_color = fx_color.from_hsv(item_rainbow->frequency * (item_rainbow->elapsed_time + ((p_ofs.x + pofs) / 50)),
  445. item_rainbow->saturation,
  446. item_rainbow->value,
  447. fx_color.a);
  448. }
  449. }
  450. if (visible)
  451. line_is_blank = false;
  452. if (c[i] == '\t')
  453. visible = false;
  454. if (visible) {
  455. if (selected) {
  456. cw = font->get_char_size(fx_char, c[i + 1]).x;
  457. draw_rect(Rect2(p_ofs.x + pofs, p_ofs.y + y, cw, lh), selection_bg);
  458. }
  459. if (p_font_color_shadow.a > 0) {
  460. float x_ofs_shadow = align_ofs + pofs;
  461. float y_ofs_shadow = y + lh - line_descent;
  462. font->draw_char(ci, Point2(x_ofs_shadow, y_ofs_shadow) + shadow_ofs + fx_offset, fx_char, c[i + 1], p_font_color_shadow);
  463. if (p_shadow_as_outline) {
  464. font->draw_char(ci, Point2(x_ofs_shadow, y_ofs_shadow) + Vector2(-shadow_ofs.x, shadow_ofs.y) + fx_offset, fx_char, c[i + 1], p_font_color_shadow);
  465. font->draw_char(ci, Point2(x_ofs_shadow, y_ofs_shadow) + Vector2(shadow_ofs.x, -shadow_ofs.y) + fx_offset, fx_char, c[i + 1], p_font_color_shadow);
  466. font->draw_char(ci, Point2(x_ofs_shadow, y_ofs_shadow) + Vector2(-shadow_ofs.x, -shadow_ofs.y) + fx_offset, fx_char, c[i + 1], p_font_color_shadow);
  467. }
  468. }
  469. if (selected) {
  470. drawer.draw_char(ci, p_ofs + Point2(align_ofs + pofs, y + lh - line_descent), fx_char, c[i + 1], override_selected_font_color ? selection_fg : fx_color);
  471. } else {
  472. cw = drawer.draw_char(ci, p_ofs + Point2(align_ofs + pofs, y + lh - line_descent) + fx_offset, fx_char, c[i + 1], fx_color);
  473. }
  474. } else if (previously_visible && c[i] != '\t') {
  475. backtrack += font->get_char_size(fx_char, c[i + 1]).x;
  476. }
  477. p_char_count++;
  478. if (c[i] == '\t') {
  479. cw = tab_size * font->get_char_size(' ').width;
  480. backtrack = MAX(0, backtrack - cw);
  481. }
  482. ofs += cw;
  483. }
  484. }
  485. if (underline) {
  486. Color uc = color;
  487. uc.a *= 0.5;
  488. int uy = y + lh - line_descent + 2;
  489. float underline_width = 1.0;
  490. #ifdef TOOLS_ENABLED
  491. underline_width *= EDSCALE;
  492. #endif
  493. VS::get_singleton()->canvas_item_add_line(ci, p_ofs + Point2(align_ofs + wofs, uy), p_ofs + Point2(align_ofs + wofs + w, uy), uc, underline_width);
  494. } else if (strikethrough) {
  495. Color uc = color;
  496. uc.a *= 0.5;
  497. int uy = y + lh - (line_ascent + line_descent) / 2;
  498. float strikethrough_width = 1.0;
  499. #ifdef TOOLS_ENABLED
  500. strikethrough_width *= EDSCALE;
  501. #endif
  502. VS::get_singleton()->canvas_item_add_line(ci, p_ofs + Point2(align_ofs + wofs, uy), p_ofs + Point2(align_ofs + wofs + w, uy), uc, strikethrough_width);
  503. }
  504. }
  505. ADVANCE(fw);
  506. CHECK_HEIGHT(fh); //must be done somewhere
  507. c = &c[end];
  508. }
  509. } break;
  510. case ITEM_IMAGE: {
  511. lh = 0;
  512. if (p_mode != PROCESS_CACHE)
  513. lh = line < l.height_caches.size() ? l.height_caches[line] : 1;
  514. else
  515. l.char_count += 1; //images count as chars too
  516. ItemImage *img = static_cast<ItemImage *>(it);
  517. Ref<Font> font = _find_font(it);
  518. if (font.is_null())
  519. font = p_base_font;
  520. if (p_mode == PROCESS_POINTER && r_click_char)
  521. *r_click_char = 0;
  522. ENSURE_WIDTH(img->size.width);
  523. bool visible = visible_characters < 0 || (p_char_count < visible_characters && YRANGE_VISIBLE(y + lh - font->get_descent() - img->size.height, img->size.height));
  524. if (visible)
  525. line_is_blank = false;
  526. if (p_mode == PROCESS_DRAW && visible) {
  527. img->image->draw_rect(ci, Rect2(p_ofs + Point2(align_ofs + wofs, y + lh - font->get_descent() - img->size.height), img->size));
  528. }
  529. p_char_count++;
  530. ADVANCE(img->size.width);
  531. CHECK_HEIGHT((img->size.height + font->get_descent()));
  532. } break;
  533. case ITEM_NEWLINE: {
  534. lh = 0;
  535. if (p_mode != PROCESS_CACHE) {
  536. lh = line < l.height_caches.size() ? l.height_caches[line] : 1;
  537. line_is_blank = true;
  538. }
  539. } break;
  540. case ITEM_TABLE: {
  541. lh = 0;
  542. ItemTable *table = static_cast<ItemTable *>(it);
  543. int hseparation = get_constant("table_hseparation");
  544. int vseparation = get_constant("table_vseparation");
  545. Color ccolor = _find_color(table, p_base_color);
  546. Vector2 draw_ofs = Point2(wofs, y);
  547. Color font_color_shadow = get_color("font_color_shadow");
  548. bool use_outline = get_constant("shadow_as_outline");
  549. Point2 shadow_ofs2(get_constant("shadow_offset_x"), get_constant("shadow_offset_y"));
  550. if (p_mode == PROCESS_CACHE) {
  551. int idx = 0;
  552. //set minimums to zero
  553. for (int i = 0; i < table->columns.size(); i++) {
  554. table->columns.write[i].min_width = 0;
  555. table->columns.write[i].max_width = 0;
  556. table->columns.write[i].width = 0;
  557. }
  558. //compute minimum width for each cell
  559. const int available_width = p_width - hseparation * (table->columns.size() - 1) - wofs;
  560. for (List<Item *>::Element *E = table->subitems.front(); E; E = E->next()) {
  561. ERR_CONTINUE(E->get()->type != ITEM_FRAME); //children should all be frames
  562. ItemFrame *frame = static_cast<ItemFrame *>(E->get());
  563. int column = idx % table->columns.size();
  564. int ly = 0;
  565. for (int i = 0; i < frame->lines.size(); i++) {
  566. _process_line(frame, Point2(), ly, available_width, i, PROCESS_CACHE, cfont, Color(), font_color_shadow, use_outline, shadow_ofs2);
  567. table->columns.write[column].min_width = MAX(table->columns[column].min_width, frame->lines[i].minimum_width);
  568. table->columns.write[column].max_width = MAX(table->columns[column].max_width, frame->lines[i].maximum_width);
  569. }
  570. idx++;
  571. }
  572. //compute available width and total ratio (for expanders)
  573. int total_ratio = 0;
  574. int remaining_width = available_width;
  575. table->total_width = hseparation;
  576. for (int i = 0; i < table->columns.size(); i++) {
  577. remaining_width -= table->columns[i].min_width;
  578. if (table->columns[i].max_width > table->columns[i].min_width)
  579. table->columns.write[i].expand = true;
  580. if (table->columns[i].expand)
  581. total_ratio += table->columns[i].expand_ratio;
  582. }
  583. //assign actual widths
  584. for (int i = 0; i < table->columns.size(); i++) {
  585. table->columns.write[i].width = table->columns[i].min_width;
  586. if (table->columns[i].expand && total_ratio > 0)
  587. table->columns.write[i].width += table->columns[i].expand_ratio * remaining_width / total_ratio;
  588. table->total_width += table->columns[i].width + hseparation;
  589. }
  590. //resize to max_width if needed and distribute the remaining space
  591. bool table_need_fit = true;
  592. while (table_need_fit) {
  593. table_need_fit = false;
  594. //fit slim
  595. for (int i = 0; i < table->columns.size(); i++) {
  596. if (!table->columns[i].expand)
  597. continue;
  598. int dif = table->columns[i].width - table->columns[i].max_width;
  599. if (dif > 0) {
  600. table_need_fit = true;
  601. table->columns.write[i].width = table->columns[i].max_width;
  602. table->total_width -= dif;
  603. total_ratio -= table->columns[i].expand_ratio;
  604. }
  605. }
  606. //grow
  607. remaining_width = available_width - table->total_width;
  608. if (remaining_width > 0 && total_ratio > 0) {
  609. for (int i = 0; i < table->columns.size(); i++) {
  610. if (table->columns[i].expand) {
  611. int dif = table->columns[i].max_width - table->columns[i].width;
  612. if (dif > 0) {
  613. int slice = table->columns[i].expand_ratio * remaining_width / total_ratio;
  614. int incr = MIN(dif, slice);
  615. table->columns.write[i].width += incr;
  616. table->total_width += incr;
  617. }
  618. }
  619. }
  620. }
  621. }
  622. //compute caches properly again with the right width
  623. idx = 0;
  624. for (List<Item *>::Element *E = table->subitems.front(); E; E = E->next()) {
  625. ERR_CONTINUE(E->get()->type != ITEM_FRAME); //children should all be frames
  626. ItemFrame *frame = static_cast<ItemFrame *>(E->get());
  627. int column = idx % table->columns.size();
  628. for (int i = 0; i < frame->lines.size(); i++) {
  629. int ly = 0;
  630. _process_line(frame, Point2(), ly, table->columns[column].width, i, PROCESS_CACHE, cfont, Color(), font_color_shadow, use_outline, shadow_ofs2);
  631. frame->lines.write[i].height_cache = ly; //actual height
  632. frame->lines.write[i].height_accum_cache = ly; //actual height
  633. }
  634. idx++;
  635. }
  636. }
  637. Point2 offset(align_ofs + hseparation, vseparation);
  638. int row_height = 0;
  639. //draw using computed caches
  640. int idx = 0;
  641. for (List<Item *>::Element *E = table->subitems.front(); E; E = E->next()) {
  642. ERR_CONTINUE(E->get()->type != ITEM_FRAME); //children should all be frames
  643. ItemFrame *frame = static_cast<ItemFrame *>(E->get());
  644. int column = idx % table->columns.size();
  645. int ly = 0;
  646. int yofs = 0;
  647. int lines_h = frame->lines[frame->lines.size() - 1].height_accum_cache - (frame->lines[0].height_accum_cache - frame->lines[0].height_cache);
  648. int lines_ofs = p_ofs.y + offset.y + draw_ofs.y;
  649. bool visible = lines_ofs < get_size().height && lines_ofs + lines_h >= 0;
  650. if (visible)
  651. line_is_blank = false;
  652. for (int i = 0; i < frame->lines.size(); i++) {
  653. if (visible) {
  654. if (p_mode == PROCESS_DRAW) {
  655. nonblank_line_count += _process_line(frame, p_ofs + offset + draw_ofs + Vector2(0, yofs), ly, table->columns[column].width, i, PROCESS_DRAW, cfont, ccolor, font_color_shadow, use_outline, shadow_ofs2);
  656. } else if (p_mode == PROCESS_POINTER) {
  657. _process_line(frame, p_ofs + offset + draw_ofs + Vector2(0, yofs), ly, table->columns[column].width, i, PROCESS_POINTER, cfont, ccolor, font_color_shadow, use_outline, shadow_ofs2, p_click_pos, r_click_item, r_click_char, r_outside);
  658. if (r_click_item && *r_click_item) {
  659. RETURN; // exit early
  660. }
  661. }
  662. }
  663. yofs += frame->lines[i].height_cache;
  664. if (p_mode == PROCESS_CACHE) {
  665. frame->lines.write[i].height_accum_cache = offset.y + draw_ofs.y + frame->lines[i].height_cache;
  666. }
  667. }
  668. row_height = MAX(yofs, row_height);
  669. offset.x += table->columns[column].width + hseparation;
  670. // Add row height after last column of the row or last cell of the table.
  671. if (column == table->columns.size() - 1 || E->next() == NULL) {
  672. offset.y += row_height + vseparation;
  673. offset.x = hseparation;
  674. row_height = 0;
  675. }
  676. idx++;
  677. }
  678. int total_height = offset.y;
  679. if (row_height) {
  680. total_height = row_height + vseparation;
  681. }
  682. ADVANCE(table->total_width);
  683. CHECK_HEIGHT(total_height);
  684. } break;
  685. default: {
  686. }
  687. }
  688. Item *itp = it;
  689. it = _get_next_item(it);
  690. if (it && (p_line + 1 < p_frame->lines.size()) && p_frame->lines[p_line + 1].from == it) {
  691. if (p_mode == PROCESS_POINTER && r_click_item && p_click_pos.y >= p_ofs.y + y && p_click_pos.y <= p_ofs.y + y + lh) {
  692. //went to next line, but pointer was on the previous one
  693. if (r_outside) *r_outside = true;
  694. *r_click_item = itp;
  695. *r_click_char = rchar;
  696. RETURN;
  697. }
  698. break;
  699. }
  700. }
  701. NEW_LINE;
  702. RETURN;
  703. #undef RETURN
  704. #undef NEW_LINE
  705. #undef ENSURE_WIDTH
  706. #undef ADVANCE
  707. #undef CHECK_HEIGHT
  708. }
  709. void RichTextLabel::_scroll_changed(double) {
  710. if (updating_scroll)
  711. return;
  712. if (scroll_follow && vscroll->get_value() >= (vscroll->get_max() - vscroll->get_page()))
  713. scroll_following = true;
  714. else
  715. scroll_following = false;
  716. scroll_updated = true;
  717. update();
  718. }
  719. void RichTextLabel::_update_scroll() {
  720. int total_height = get_content_height();
  721. bool exceeds = total_height > get_size().height && scroll_active;
  722. if (exceeds != scroll_visible) {
  723. if (exceeds) {
  724. scroll_visible = true;
  725. scroll_w = vscroll->get_combined_minimum_size().width;
  726. vscroll->show();
  727. vscroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -scroll_w);
  728. } else {
  729. scroll_visible = false;
  730. scroll_w = 0;
  731. vscroll->hide();
  732. }
  733. main->first_invalid_line = 0; //invalidate ALL
  734. _validate_line_caches(main);
  735. }
  736. }
  737. void RichTextLabel::_update_fx(RichTextLabel::ItemFrame *p_frame, float p_delta_time) {
  738. Item *it = p_frame;
  739. while (it) {
  740. ItemFX *ifx = NULL;
  741. if (it->type == ITEM_CUSTOMFX || it->type == ITEM_SHAKE || it->type == ITEM_WAVE || it->type == ITEM_TORNADO || it->type == ITEM_RAINBOW) {
  742. ifx = static_cast<ItemFX *>(it);
  743. }
  744. if (!ifx) {
  745. it = _get_next_item(it, true);
  746. continue;
  747. }
  748. ifx->elapsed_time += p_delta_time;
  749. ItemShake *shake = NULL;
  750. if (it->type == ITEM_SHAKE) {
  751. shake = static_cast<ItemShake *>(it);
  752. }
  753. if (shake) {
  754. bool cycle = (shake->elapsed_time > (1.0f / shake->rate));
  755. if (cycle) {
  756. shake->elapsed_time -= (1.0f / shake->rate);
  757. shake->reroll_random();
  758. }
  759. }
  760. it = _get_next_item(it, true);
  761. }
  762. }
  763. void RichTextLabel::_notification(int p_what) {
  764. switch (p_what) {
  765. case NOTIFICATION_MOUSE_EXIT: {
  766. if (meta_hovering) {
  767. meta_hovering = NULL;
  768. emit_signal("meta_hover_ended", current_meta);
  769. current_meta = false;
  770. update();
  771. }
  772. } break;
  773. case NOTIFICATION_RESIZED: {
  774. main->first_invalid_line = 0; //invalidate ALL
  775. update();
  776. } break;
  777. case NOTIFICATION_ENTER_TREE: {
  778. if (bbcode != "")
  779. set_bbcode(bbcode);
  780. main->first_invalid_line = 0; //invalidate ALL
  781. update();
  782. } break;
  783. case NOTIFICATION_THEME_CHANGED: {
  784. update();
  785. } break;
  786. case NOTIFICATION_DRAW: {
  787. _validate_line_caches(main);
  788. _update_scroll();
  789. RID ci = get_canvas_item();
  790. Size2 size = get_size();
  791. Rect2 text_rect = _get_text_rect();
  792. draw_style_box(get_stylebox("normal"), Rect2(Point2(), size));
  793. if (has_focus()) {
  794. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, true);
  795. draw_style_box(get_stylebox("focus"), Rect2(Point2(), size));
  796. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, false);
  797. }
  798. int ofs = vscroll->get_value();
  799. //todo, change to binary search
  800. int from_line = 0;
  801. int total_chars = 0;
  802. while (from_line < main->lines.size()) {
  803. if (main->lines[from_line].height_accum_cache + _get_text_rect().get_position().y >= ofs)
  804. break;
  805. total_chars += main->lines[from_line].char_count;
  806. from_line++;
  807. }
  808. if (from_line >= main->lines.size())
  809. break; //nothing to draw
  810. int y = (main->lines[from_line].height_accum_cache - main->lines[from_line].height_cache) - ofs;
  811. Ref<Font> base_font = get_font("normal_font");
  812. Color base_color = get_color("default_color");
  813. Color font_color_shadow = get_color("font_color_shadow");
  814. bool use_outline = get_constant("shadow_as_outline");
  815. Point2 shadow_ofs(get_constant("shadow_offset_x"), get_constant("shadow_offset_y"));
  816. visible_line_count = 0;
  817. while (y < size.height && from_line < main->lines.size()) {
  818. visible_line_count += _process_line(main, text_rect.get_position(), y, text_rect.get_size().width - scroll_w, from_line, PROCESS_DRAW, base_font, base_color, font_color_shadow, use_outline, shadow_ofs, Point2i(), NULL, NULL, NULL, total_chars);
  819. total_chars += main->lines[from_line].char_count;
  820. from_line++;
  821. }
  822. } break;
  823. case NOTIFICATION_INTERNAL_PROCESS: {
  824. if (is_visible_in_tree()) {
  825. float dt = get_process_delta_time();
  826. _update_fx(main, dt);
  827. update();
  828. }
  829. }
  830. }
  831. }
  832. void RichTextLabel::_find_click(ItemFrame *p_frame, const Point2i &p_click, Item **r_click_item, int *r_click_char, bool *r_outside) {
  833. if (r_click_item)
  834. *r_click_item = NULL;
  835. Rect2 text_rect = _get_text_rect();
  836. int ofs = vscroll->get_value();
  837. Color font_color_shadow = get_color("font_color_shadow");
  838. bool use_outline = get_constant("shadow_as_outline");
  839. Point2 shadow_ofs(get_constant("shadow_offset_x"), get_constant("shadow_offset_y"));
  840. //todo, change to binary search
  841. int from_line = 0;
  842. while (from_line < p_frame->lines.size()) {
  843. if (p_frame->lines[from_line].height_accum_cache >= ofs)
  844. break;
  845. from_line++;
  846. }
  847. if (from_line >= p_frame->lines.size())
  848. return;
  849. int y = (p_frame->lines[from_line].height_accum_cache - p_frame->lines[from_line].height_cache) - ofs;
  850. Ref<Font> base_font = get_font("normal_font");
  851. Color base_color = get_color("default_color");
  852. while (y < text_rect.get_size().height && from_line < p_frame->lines.size()) {
  853. _process_line(p_frame, text_rect.get_position(), y, text_rect.get_size().width - scroll_w, from_line, PROCESS_POINTER, base_font, base_color, font_color_shadow, use_outline, shadow_ofs, p_click, r_click_item, r_click_char, r_outside);
  854. if (r_click_item && *r_click_item)
  855. return;
  856. from_line++;
  857. }
  858. }
  859. Control::CursorShape RichTextLabel::get_cursor_shape(const Point2 &p_pos) const {
  860. if (!underline_meta)
  861. return CURSOR_ARROW;
  862. if (selection.click)
  863. return CURSOR_IBEAM;
  864. if (main->first_invalid_line < main->lines.size())
  865. return CURSOR_ARROW; //invalid
  866. int line = 0;
  867. Item *item = NULL;
  868. bool outside;
  869. ((RichTextLabel *)(this))->_find_click(main, p_pos, &item, &line, &outside);
  870. if (item && !outside && ((RichTextLabel *)(this))->_find_meta(item, NULL))
  871. return CURSOR_POINTING_HAND;
  872. return CURSOR_ARROW;
  873. }
  874. void RichTextLabel::_gui_input(Ref<InputEvent> p_event) {
  875. Ref<InputEventMouseButton> b = p_event;
  876. if (b.is_valid()) {
  877. if (main->first_invalid_line < main->lines.size())
  878. return;
  879. if (b->get_button_index() == BUTTON_LEFT) {
  880. if (b->is_pressed() && !b->is_doubleclick()) {
  881. scroll_updated = false;
  882. int line = 0;
  883. Item *item = NULL;
  884. bool outside;
  885. _find_click(main, b->get_position(), &item, &line, &outside);
  886. if (item) {
  887. if (selection.enabled) {
  888. selection.click = item;
  889. selection.click_char = line;
  890. // Erase previous selection.
  891. if (selection.active) {
  892. selection.from = NULL;
  893. selection.from_char = '\0';
  894. selection.to = NULL;
  895. selection.to_char = '\0';
  896. selection.active = false;
  897. update();
  898. }
  899. }
  900. }
  901. } else if (b->is_pressed() && b->is_doubleclick() && selection.enabled) {
  902. //doubleclick: select word
  903. int line = 0;
  904. Item *item = NULL;
  905. bool outside;
  906. _find_click(main, b->get_position(), &item, &line, &outside);
  907. while (item && item->type != ITEM_TEXT) {
  908. item = _get_next_item(item, true);
  909. }
  910. if (item && item->type == ITEM_TEXT) {
  911. String itext = static_cast<ItemText *>(item)->text;
  912. int beg, end;
  913. if (select_word(itext, line, beg, end)) {
  914. selection.from = item;
  915. selection.to = item;
  916. selection.from_char = beg;
  917. selection.to_char = end - 1;
  918. selection.active = true;
  919. update();
  920. }
  921. }
  922. } else if (!b->is_pressed()) {
  923. selection.click = NULL;
  924. if (!b->is_doubleclick() && !scroll_updated) {
  925. int line = 0;
  926. Item *item = NULL;
  927. bool outside;
  928. _find_click(main, b->get_position(), &item, &line, &outside);
  929. if (item) {
  930. Variant meta;
  931. if (!outside && _find_meta(item, &meta)) {
  932. //meta clicked
  933. emit_signal("meta_clicked", meta);
  934. }
  935. }
  936. }
  937. }
  938. }
  939. if (b->get_button_index() == BUTTON_WHEEL_UP) {
  940. if (scroll_active)
  941. vscroll->set_value(vscroll->get_value() - vscroll->get_page() * b->get_factor() * 0.5 / 8);
  942. }
  943. if (b->get_button_index() == BUTTON_WHEEL_DOWN) {
  944. if (scroll_active)
  945. vscroll->set_value(vscroll->get_value() + vscroll->get_page() * b->get_factor() * 0.5 / 8);
  946. }
  947. }
  948. Ref<InputEventPanGesture> pan_gesture = p_event;
  949. if (pan_gesture.is_valid()) {
  950. if (scroll_active)
  951. vscroll->set_value(vscroll->get_value() + vscroll->get_page() * pan_gesture->get_delta().y * 0.5 / 8);
  952. return;
  953. }
  954. Ref<InputEventKey> k = p_event;
  955. if (k.is_valid()) {
  956. if (k->is_pressed() && !k->get_alt() && !k->get_shift()) {
  957. bool handled = false;
  958. switch (k->get_scancode()) {
  959. case KEY_PAGEUP: {
  960. if (vscroll->is_visible_in_tree()) {
  961. vscroll->set_value(vscroll->get_value() - vscroll->get_page());
  962. handled = true;
  963. }
  964. } break;
  965. case KEY_PAGEDOWN: {
  966. if (vscroll->is_visible_in_tree()) {
  967. vscroll->set_value(vscroll->get_value() + vscroll->get_page());
  968. handled = true;
  969. }
  970. } break;
  971. case KEY_UP: {
  972. if (vscroll->is_visible_in_tree()) {
  973. vscroll->set_value(vscroll->get_value() - get_font("normal_font")->get_height());
  974. handled = true;
  975. }
  976. } break;
  977. case KEY_DOWN: {
  978. if (vscroll->is_visible_in_tree()) {
  979. vscroll->set_value(vscroll->get_value() + get_font("normal_font")->get_height());
  980. handled = true;
  981. }
  982. } break;
  983. case KEY_HOME: {
  984. if (vscroll->is_visible_in_tree()) {
  985. vscroll->set_value(0);
  986. handled = true;
  987. }
  988. } break;
  989. case KEY_END: {
  990. if (vscroll->is_visible_in_tree()) {
  991. vscroll->set_value(vscroll->get_max());
  992. handled = true;
  993. }
  994. } break;
  995. case KEY_INSERT:
  996. case KEY_C: {
  997. if (k->get_command()) {
  998. selection_copy();
  999. handled = true;
  1000. }
  1001. } break;
  1002. }
  1003. if (handled)
  1004. accept_event();
  1005. }
  1006. }
  1007. Ref<InputEventMouseMotion> m = p_event;
  1008. if (m.is_valid()) {
  1009. if (main->first_invalid_line < main->lines.size())
  1010. return;
  1011. int line = 0;
  1012. Item *item = NULL;
  1013. bool outside;
  1014. _find_click(main, m->get_position(), &item, &line, &outside);
  1015. if (selection.click) {
  1016. if (!item)
  1017. return; // do not update
  1018. selection.from = selection.click;
  1019. selection.from_char = selection.click_char;
  1020. selection.to = item;
  1021. selection.to_char = line;
  1022. bool swap = false;
  1023. if (selection.from->index > selection.to->index)
  1024. swap = true;
  1025. else if (selection.from->index == selection.to->index) {
  1026. if (selection.from_char > selection.to_char)
  1027. swap = true;
  1028. else if (selection.from_char == selection.to_char) {
  1029. selection.active = false;
  1030. return;
  1031. }
  1032. }
  1033. if (swap) {
  1034. SWAP(selection.from, selection.to);
  1035. SWAP(selection.from_char, selection.to_char);
  1036. }
  1037. selection.active = true;
  1038. update();
  1039. }
  1040. Variant meta;
  1041. ItemMeta *item_meta;
  1042. if (item && !outside && _find_meta(item, &meta, &item_meta)) {
  1043. if (meta_hovering != item_meta) {
  1044. if (meta_hovering) {
  1045. emit_signal("meta_hover_ended", current_meta);
  1046. }
  1047. meta_hovering = item_meta;
  1048. current_meta = meta;
  1049. emit_signal("meta_hover_started", meta);
  1050. }
  1051. } else if (meta_hovering) {
  1052. meta_hovering = NULL;
  1053. emit_signal("meta_hover_ended", current_meta);
  1054. current_meta = false;
  1055. }
  1056. }
  1057. }
  1058. Ref<Font> RichTextLabel::_find_font(Item *p_item) {
  1059. Item *fontitem = p_item;
  1060. while (fontitem) {
  1061. if (fontitem->type == ITEM_FONT) {
  1062. ItemFont *fi = static_cast<ItemFont *>(fontitem);
  1063. return fi->font;
  1064. }
  1065. fontitem = fontitem->parent;
  1066. }
  1067. return Ref<Font>();
  1068. }
  1069. int RichTextLabel::_find_margin(Item *p_item, const Ref<Font> &p_base_font) {
  1070. Item *item = p_item;
  1071. int margin = 0;
  1072. while (item) {
  1073. if (item->type == ITEM_INDENT) {
  1074. Ref<Font> font = _find_font(item);
  1075. if (font.is_null())
  1076. font = p_base_font;
  1077. ItemIndent *indent = static_cast<ItemIndent *>(item);
  1078. margin += indent->level * tab_size * font->get_char_size(' ').width;
  1079. } else if (item->type == ITEM_LIST) {
  1080. Ref<Font> font = _find_font(item);
  1081. if (font.is_null())
  1082. font = p_base_font;
  1083. }
  1084. item = item->parent;
  1085. }
  1086. return margin;
  1087. }
  1088. RichTextLabel::Align RichTextLabel::_find_align(Item *p_item) {
  1089. Item *item = p_item;
  1090. while (item) {
  1091. if (item->type == ITEM_ALIGN) {
  1092. ItemAlign *align = static_cast<ItemAlign *>(item);
  1093. return align->align;
  1094. }
  1095. item = item->parent;
  1096. }
  1097. return default_align;
  1098. }
  1099. Color RichTextLabel::_find_color(Item *p_item, const Color &p_default_color) {
  1100. Item *item = p_item;
  1101. while (item) {
  1102. if (item->type == ITEM_COLOR) {
  1103. ItemColor *color = static_cast<ItemColor *>(item);
  1104. return color->color;
  1105. }
  1106. item = item->parent;
  1107. }
  1108. return p_default_color;
  1109. }
  1110. bool RichTextLabel::_find_underline(Item *p_item) {
  1111. Item *item = p_item;
  1112. while (item) {
  1113. if (item->type == ITEM_UNDERLINE) {
  1114. return true;
  1115. }
  1116. item = item->parent;
  1117. }
  1118. return false;
  1119. }
  1120. bool RichTextLabel::_find_strikethrough(Item *p_item) {
  1121. Item *item = p_item;
  1122. while (item) {
  1123. if (item->type == ITEM_STRIKETHROUGH) {
  1124. return true;
  1125. }
  1126. item = item->parent;
  1127. }
  1128. return false;
  1129. }
  1130. bool RichTextLabel::_find_by_type(Item *p_item, ItemType p_type) {
  1131. ERR_FAIL_INDEX_V((int)p_type, 19, false);
  1132. Item *item = p_item;
  1133. while (item) {
  1134. if (item->type == p_type) {
  1135. return true;
  1136. }
  1137. item = item->parent;
  1138. }
  1139. return false;
  1140. }
  1141. void RichTextLabel::_fetch_item_fx_stack(Item *p_item, Vector<ItemFX *> &r_stack) {
  1142. Item *item = p_item;
  1143. while (item) {
  1144. if (item->type == ITEM_CUSTOMFX || item->type == ITEM_SHAKE || item->type == ITEM_WAVE || item->type == ITEM_TORNADO || item->type == ITEM_RAINBOW) {
  1145. r_stack.push_back(static_cast<ItemFX *>(item));
  1146. }
  1147. item = item->parent;
  1148. }
  1149. }
  1150. bool RichTextLabel::_find_meta(Item *p_item, Variant *r_meta, ItemMeta **r_item) {
  1151. Item *item = p_item;
  1152. while (item) {
  1153. if (item->type == ITEM_META) {
  1154. ItemMeta *meta = static_cast<ItemMeta *>(item);
  1155. if (r_meta)
  1156. *r_meta = meta->meta;
  1157. if (r_item)
  1158. *r_item = meta;
  1159. return true;
  1160. }
  1161. item = item->parent;
  1162. }
  1163. return false;
  1164. }
  1165. bool RichTextLabel::_find_layout_subitem(Item *from, Item *to) {
  1166. if (from && from != to) {
  1167. if (from->type != ITEM_FONT && from->type != ITEM_COLOR && from->type != ITEM_UNDERLINE && from->type != ITEM_STRIKETHROUGH)
  1168. return true;
  1169. for (List<Item *>::Element *E = from->subitems.front(); E; E = E->next()) {
  1170. bool layout = _find_layout_subitem(E->get(), to);
  1171. if (layout)
  1172. return true;
  1173. }
  1174. }
  1175. return false;
  1176. }
  1177. void RichTextLabel::_validate_line_caches(ItemFrame *p_frame) {
  1178. if (p_frame->first_invalid_line == p_frame->lines.size())
  1179. return;
  1180. //validate invalid lines
  1181. Size2 size = get_size();
  1182. if (fixed_width != -1) {
  1183. size.width = fixed_width;
  1184. }
  1185. Rect2 text_rect = _get_text_rect();
  1186. Color font_color_shadow = get_color("font_color_shadow");
  1187. bool use_outline = get_constant("shadow_as_outline");
  1188. Point2 shadow_ofs(get_constant("shadow_offset_x"), get_constant("shadow_offset_y"));
  1189. Ref<Font> base_font = get_font("normal_font");
  1190. for (int i = p_frame->first_invalid_line; i < p_frame->lines.size(); i++) {
  1191. int y = 0;
  1192. _process_line(p_frame, text_rect.get_position(), y, text_rect.get_size().width - scroll_w, i, PROCESS_CACHE, base_font, Color(), font_color_shadow, use_outline, shadow_ofs);
  1193. p_frame->lines.write[i].height_cache = y;
  1194. p_frame->lines.write[i].height_accum_cache = y;
  1195. if (i > 0)
  1196. p_frame->lines.write[i].height_accum_cache += p_frame->lines[i - 1].height_accum_cache;
  1197. }
  1198. int total_height = 0;
  1199. if (p_frame->lines.size())
  1200. total_height = p_frame->lines[p_frame->lines.size() - 1].height_accum_cache + get_stylebox("normal")->get_minimum_size().height;
  1201. main->first_invalid_line = p_frame->lines.size();
  1202. updating_scroll = true;
  1203. vscroll->set_max(total_height);
  1204. vscroll->set_page(size.height);
  1205. if (scroll_follow && scroll_following)
  1206. vscroll->set_value(total_height - size.height);
  1207. updating_scroll = false;
  1208. if (fit_content_height) {
  1209. minimum_size_changed();
  1210. }
  1211. }
  1212. void RichTextLabel::_invalidate_current_line(ItemFrame *p_frame) {
  1213. if (p_frame->lines.size() - 1 <= p_frame->first_invalid_line) {
  1214. p_frame->first_invalid_line = p_frame->lines.size() - 1;
  1215. update();
  1216. }
  1217. }
  1218. void RichTextLabel::add_text(const String &p_text) {
  1219. if (current->type == ITEM_TABLE)
  1220. return; //can't add anything here
  1221. int pos = 0;
  1222. while (pos < p_text.length()) {
  1223. int end = p_text.find("\n", pos);
  1224. String line;
  1225. bool eol = false;
  1226. if (end == -1) {
  1227. end = p_text.length();
  1228. } else {
  1229. eol = true;
  1230. }
  1231. if (pos == 0 && end == p_text.length())
  1232. line = p_text;
  1233. else
  1234. line = p_text.substr(pos, end - pos);
  1235. if (line.length() > 0) {
  1236. if (current->subitems.size() && current->subitems.back()->get()->type == ITEM_TEXT) {
  1237. //append text condition!
  1238. ItemText *ti = static_cast<ItemText *>(current->subitems.back()->get());
  1239. ti->text += line;
  1240. _invalidate_current_line(main);
  1241. } else {
  1242. //append item condition
  1243. ItemText *item = memnew(ItemText);
  1244. item->text = line;
  1245. _add_item(item, false);
  1246. }
  1247. }
  1248. if (eol) {
  1249. ItemNewline *item = memnew(ItemNewline);
  1250. item->line = current_frame->lines.size();
  1251. _add_item(item, false);
  1252. current_frame->lines.resize(current_frame->lines.size() + 1);
  1253. if (item->type != ITEM_NEWLINE)
  1254. current_frame->lines.write[current_frame->lines.size() - 1].from = item;
  1255. _invalidate_current_line(current_frame);
  1256. }
  1257. pos = end + 1;
  1258. }
  1259. }
  1260. void RichTextLabel::_add_item(Item *p_item, bool p_enter, bool p_ensure_newline) {
  1261. p_item->parent = current;
  1262. p_item->E = current->subitems.push_back(p_item);
  1263. p_item->index = current_idx++;
  1264. if (p_enter)
  1265. current = p_item;
  1266. if (p_ensure_newline) {
  1267. Item *from = current_frame->lines[current_frame->lines.size() - 1].from;
  1268. // only create a new line for Item types that generate content/layout, ignore those that represent formatting/styling
  1269. if (_find_layout_subitem(from, p_item)) {
  1270. _invalidate_current_line(current_frame);
  1271. current_frame->lines.resize(current_frame->lines.size() + 1);
  1272. }
  1273. }
  1274. if (current_frame->lines[current_frame->lines.size() - 1].from == NULL) {
  1275. current_frame->lines.write[current_frame->lines.size() - 1].from = p_item;
  1276. }
  1277. p_item->line = current_frame->lines.size() - 1;
  1278. _invalidate_current_line(current_frame);
  1279. }
  1280. void RichTextLabel::_remove_item(Item *p_item, const int p_line, const int p_subitem_line) {
  1281. int size = p_item->subitems.size();
  1282. if (size == 0) {
  1283. p_item->parent->subitems.erase(p_item);
  1284. if (p_item->type == ITEM_NEWLINE) {
  1285. current_frame->lines.remove(p_line);
  1286. for (int i = p_subitem_line; i < current->subitems.size(); i++) {
  1287. if (current->subitems[i]->line > 0)
  1288. current->subitems[i]->line--;
  1289. }
  1290. }
  1291. } else {
  1292. for (int i = 0; i < size; i++) {
  1293. _remove_item(p_item->subitems.front()->get(), p_line, p_subitem_line);
  1294. }
  1295. }
  1296. }
  1297. void RichTextLabel::add_image(const Ref<Texture> &p_image, const int p_width, const int p_height) {
  1298. if (current->type == ITEM_TABLE)
  1299. return;
  1300. ERR_FAIL_COND(p_image.is_null());
  1301. ERR_FAIL_COND(p_image->get_width() == 0);
  1302. ERR_FAIL_COND(p_image->get_height() == 0);
  1303. ItemImage *item = memnew(ItemImage);
  1304. item->image = p_image;
  1305. if (p_width > 0) {
  1306. // custom width
  1307. item->size.width = p_width;
  1308. if (p_height > 0) {
  1309. // custom height
  1310. item->size.height = p_height;
  1311. } else {
  1312. // calculate height to keep aspect ratio
  1313. item->size.height = p_image->get_height() * p_width / p_image->get_width();
  1314. }
  1315. } else {
  1316. if (p_height > 0) {
  1317. // custom height
  1318. item->size.height = p_height;
  1319. // calculate width to keep aspect ratio
  1320. item->size.width = p_image->get_width() * p_height / p_image->get_height();
  1321. } else {
  1322. // keep original width and height
  1323. item->size.height = p_image->get_height();
  1324. item->size.width = p_image->get_width();
  1325. }
  1326. }
  1327. _add_item(item, false);
  1328. }
  1329. void RichTextLabel::add_newline() {
  1330. if (current->type == ITEM_TABLE)
  1331. return;
  1332. ItemNewline *item = memnew(ItemNewline);
  1333. item->line = current_frame->lines.size();
  1334. _add_item(item, false);
  1335. current_frame->lines.resize(current_frame->lines.size() + 1);
  1336. _invalidate_current_line(current_frame);
  1337. }
  1338. bool RichTextLabel::remove_line(const int p_line) {
  1339. if (p_line >= current_frame->lines.size() || p_line < 0)
  1340. return false;
  1341. int i = 0;
  1342. while (i < current->subitems.size() && current->subitems[i]->line < p_line) {
  1343. i++;
  1344. }
  1345. bool was_newline = false;
  1346. while (i < current->subitems.size()) {
  1347. was_newline = current->subitems[i]->type == ITEM_NEWLINE;
  1348. _remove_item(current->subitems[i], current->subitems[i]->line, p_line);
  1349. if (was_newline)
  1350. break;
  1351. }
  1352. if (!was_newline) {
  1353. current_frame->lines.remove(p_line);
  1354. if (current_frame->lines.size() == 0) {
  1355. current_frame->lines.resize(1);
  1356. }
  1357. }
  1358. if (p_line == 0 && current->subitems.size() > 0)
  1359. main->lines.write[0].from = main;
  1360. main->first_invalid_line = 0;
  1361. return true;
  1362. }
  1363. void RichTextLabel::push_font(const Ref<Font> &p_font) {
  1364. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1365. ERR_FAIL_COND(p_font.is_null());
  1366. ItemFont *item = memnew(ItemFont);
  1367. item->font = p_font;
  1368. _add_item(item, true);
  1369. }
  1370. void RichTextLabel::push_normal() {
  1371. Ref<Font> normal_font = get_font("normal_font");
  1372. ERR_FAIL_COND(normal_font.is_null());
  1373. push_font(normal_font);
  1374. }
  1375. void RichTextLabel::push_bold() {
  1376. Ref<Font> bold_font = get_font("bold_font");
  1377. ERR_FAIL_COND(bold_font.is_null());
  1378. push_font(bold_font);
  1379. }
  1380. void RichTextLabel::push_bold_italics() {
  1381. Ref<Font> bold_italics_font = get_font("bold_italics_font");
  1382. ERR_FAIL_COND(bold_italics_font.is_null());
  1383. push_font(bold_italics_font);
  1384. }
  1385. void RichTextLabel::push_italics() {
  1386. Ref<Font> italics_font = get_font("italics_font");
  1387. ERR_FAIL_COND(italics_font.is_null());
  1388. push_font(italics_font);
  1389. }
  1390. void RichTextLabel::push_mono() {
  1391. Ref<Font> mono_font = get_font("mono_font");
  1392. ERR_FAIL_COND(mono_font.is_null());
  1393. push_font(mono_font);
  1394. }
  1395. void RichTextLabel::push_color(const Color &p_color) {
  1396. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1397. ItemColor *item = memnew(ItemColor);
  1398. item->color = p_color;
  1399. _add_item(item, true);
  1400. }
  1401. void RichTextLabel::push_underline() {
  1402. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1403. ItemUnderline *item = memnew(ItemUnderline);
  1404. _add_item(item, true);
  1405. }
  1406. void RichTextLabel::push_strikethrough() {
  1407. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1408. ItemStrikethrough *item = memnew(ItemStrikethrough);
  1409. _add_item(item, true);
  1410. }
  1411. void RichTextLabel::push_align(Align p_align) {
  1412. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1413. ItemAlign *item = memnew(ItemAlign);
  1414. item->align = p_align;
  1415. _add_item(item, true, true);
  1416. }
  1417. void RichTextLabel::push_indent(int p_level) {
  1418. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1419. ERR_FAIL_COND(p_level < 0);
  1420. ItemIndent *item = memnew(ItemIndent);
  1421. item->level = p_level;
  1422. _add_item(item, true, true);
  1423. }
  1424. void RichTextLabel::push_list(ListType p_list) {
  1425. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1426. ERR_FAIL_INDEX(p_list, 3);
  1427. ItemList *item = memnew(ItemList);
  1428. item->list_type = p_list;
  1429. _add_item(item, true, true);
  1430. }
  1431. void RichTextLabel::push_meta(const Variant &p_meta) {
  1432. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1433. ItemMeta *item = memnew(ItemMeta);
  1434. item->meta = p_meta;
  1435. _add_item(item, true);
  1436. }
  1437. void RichTextLabel::push_table(int p_columns) {
  1438. ERR_FAIL_COND(p_columns < 1);
  1439. ItemTable *item = memnew(ItemTable);
  1440. item->columns.resize(p_columns);
  1441. item->total_width = 0;
  1442. for (int i = 0; i < item->columns.size(); i++) {
  1443. item->columns.write[i].expand = false;
  1444. item->columns.write[i].expand_ratio = 1;
  1445. }
  1446. _add_item(item, true, true);
  1447. }
  1448. void RichTextLabel::push_fade(int p_start_index, int p_length) {
  1449. ItemFade *item = memnew(ItemFade);
  1450. item->starting_index = p_start_index;
  1451. item->length = p_length;
  1452. _add_item(item, true);
  1453. }
  1454. void RichTextLabel::push_shake(int p_strength = 10, float p_rate = 24.0f) {
  1455. ItemShake *item = memnew(ItemShake);
  1456. item->strength = p_strength;
  1457. item->rate = p_rate;
  1458. _add_item(item, true);
  1459. }
  1460. void RichTextLabel::push_wave(float p_frequency = 1.0f, float p_amplitude = 10.0f) {
  1461. ItemWave *item = memnew(ItemWave);
  1462. item->frequency = p_frequency;
  1463. item->amplitude = p_amplitude;
  1464. _add_item(item, true);
  1465. }
  1466. void RichTextLabel::push_tornado(float p_frequency = 1.0f, float p_radius = 10.0f) {
  1467. ItemTornado *item = memnew(ItemTornado);
  1468. item->frequency = p_frequency;
  1469. item->radius = p_radius;
  1470. _add_item(item, true);
  1471. }
  1472. void RichTextLabel::push_rainbow(float p_saturation, float p_value, float p_frequency) {
  1473. ItemRainbow *item = memnew(ItemRainbow);
  1474. item->frequency = p_frequency;
  1475. item->saturation = p_saturation;
  1476. item->value = p_value;
  1477. _add_item(item, true);
  1478. }
  1479. void RichTextLabel::push_customfx(Ref<RichTextEffect> p_custom_effect, Dictionary p_environment) {
  1480. ItemCustomFX *item = memnew(ItemCustomFX);
  1481. item->custom_effect = p_custom_effect;
  1482. item->char_fx_transform->environment = p_environment;
  1483. _add_item(item, true);
  1484. }
  1485. void RichTextLabel::set_table_column_expand(int p_column, bool p_expand, int p_ratio) {
  1486. ERR_FAIL_COND(current->type != ITEM_TABLE);
  1487. ItemTable *table = static_cast<ItemTable *>(current);
  1488. ERR_FAIL_INDEX(p_column, table->columns.size());
  1489. table->columns.write[p_column].expand = p_expand;
  1490. table->columns.write[p_column].expand_ratio = p_ratio;
  1491. }
  1492. void RichTextLabel::push_cell() {
  1493. ERR_FAIL_COND(current->type != ITEM_TABLE);
  1494. ItemFrame *item = memnew(ItemFrame);
  1495. item->parent_frame = current_frame;
  1496. _add_item(item, true);
  1497. current_frame = item;
  1498. item->cell = true;
  1499. item->parent_line = item->parent_frame->lines.size() - 1;
  1500. item->lines.resize(1);
  1501. item->lines.write[0].from = NULL;
  1502. item->first_invalid_line = 0;
  1503. }
  1504. int RichTextLabel::get_current_table_column() const {
  1505. ERR_FAIL_COND_V(current->type != ITEM_TABLE, -1);
  1506. ItemTable *table = static_cast<ItemTable *>(current);
  1507. return table->subitems.size() % table->columns.size();
  1508. }
  1509. void RichTextLabel::pop() {
  1510. ERR_FAIL_COND(!current->parent);
  1511. if (current->type == ITEM_FRAME) {
  1512. current_frame = static_cast<ItemFrame *>(current)->parent_frame;
  1513. }
  1514. current = current->parent;
  1515. }
  1516. void RichTextLabel::clear() {
  1517. main->_clear_children();
  1518. current = main;
  1519. current_frame = main;
  1520. main->lines.clear();
  1521. main->lines.resize(1);
  1522. main->first_invalid_line = 0;
  1523. update();
  1524. selection.click = NULL;
  1525. selection.active = false;
  1526. current_idx = 1;
  1527. if (scroll_follow) {
  1528. scroll_following = true;
  1529. }
  1530. }
  1531. void RichTextLabel::set_tab_size(int p_spaces) {
  1532. tab_size = p_spaces;
  1533. main->first_invalid_line = 0;
  1534. update();
  1535. }
  1536. int RichTextLabel::get_tab_size() const {
  1537. return tab_size;
  1538. }
  1539. void RichTextLabel::set_fit_content_height(bool p_enabled) {
  1540. if (p_enabled != fit_content_height) {
  1541. fit_content_height = p_enabled;
  1542. minimum_size_changed();
  1543. }
  1544. }
  1545. bool RichTextLabel::is_fit_content_height_enabled() const {
  1546. return fit_content_height;
  1547. }
  1548. void RichTextLabel::set_meta_underline(bool p_underline) {
  1549. underline_meta = p_underline;
  1550. update();
  1551. }
  1552. bool RichTextLabel::is_meta_underlined() const {
  1553. return underline_meta;
  1554. }
  1555. void RichTextLabel::set_override_selected_font_color(bool p_override_selected_font_color) {
  1556. override_selected_font_color = p_override_selected_font_color;
  1557. }
  1558. bool RichTextLabel::is_overriding_selected_font_color() const {
  1559. return override_selected_font_color;
  1560. }
  1561. void RichTextLabel::set_offset(int p_pixel) {
  1562. vscroll->set_value(p_pixel);
  1563. }
  1564. void RichTextLabel::set_scroll_active(bool p_active) {
  1565. if (scroll_active == p_active)
  1566. return;
  1567. scroll_active = p_active;
  1568. vscroll->set_drag_node_enabled(p_active);
  1569. update();
  1570. }
  1571. bool RichTextLabel::is_scroll_active() const {
  1572. return scroll_active;
  1573. }
  1574. void RichTextLabel::set_scroll_follow(bool p_follow) {
  1575. scroll_follow = p_follow;
  1576. if (!vscroll->is_visible_in_tree() || vscroll->get_value() >= (vscroll->get_max() - vscroll->get_page()))
  1577. scroll_following = true;
  1578. }
  1579. bool RichTextLabel::is_scroll_following() const {
  1580. return scroll_follow;
  1581. }
  1582. Error RichTextLabel::parse_bbcode(const String &p_bbcode) {
  1583. clear();
  1584. return append_bbcode(p_bbcode);
  1585. }
  1586. Error RichTextLabel::append_bbcode(const String &p_bbcode) {
  1587. int pos = 0;
  1588. List<String> tag_stack;
  1589. Ref<Font> normal_font = get_font("normal_font");
  1590. Ref<Font> bold_font = get_font("bold_font");
  1591. Ref<Font> italics_font = get_font("italics_font");
  1592. Ref<Font> bold_italics_font = get_font("bold_italics_font");
  1593. Ref<Font> mono_font = get_font("mono_font");
  1594. Color base_color = get_color("default_color");
  1595. int indent_level = 0;
  1596. bool in_bold = false;
  1597. bool in_italics = false;
  1598. set_process_internal(false);
  1599. while (pos < p_bbcode.length()) {
  1600. int brk_pos = p_bbcode.find("[", pos);
  1601. if (brk_pos < 0)
  1602. brk_pos = p_bbcode.length();
  1603. if (brk_pos > pos) {
  1604. add_text(p_bbcode.substr(pos, brk_pos - pos));
  1605. }
  1606. if (brk_pos == p_bbcode.length())
  1607. break; //nothing else to add
  1608. int brk_end = p_bbcode.find("]", brk_pos + 1);
  1609. if (brk_end == -1) {
  1610. //no close, add the rest
  1611. add_text(p_bbcode.substr(brk_pos, p_bbcode.length() - brk_pos));
  1612. break;
  1613. }
  1614. String tag = p_bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
  1615. Vector<String> split_tag_block = tag.split(" ", false);
  1616. String bbcode = !split_tag_block.empty() ? split_tag_block[0] : "";
  1617. if (tag.begins_with("/") && tag_stack.size()) {
  1618. bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length());
  1619. if (tag_stack.front()->get() == "b")
  1620. in_bold = false;
  1621. if (tag_stack.front()->get() == "i")
  1622. in_italics = false;
  1623. if (tag_stack.front()->get() == "indent")
  1624. indent_level--;
  1625. if (!tag_ok) {
  1626. add_text("[" + tag);
  1627. pos = brk_end;
  1628. continue;
  1629. }
  1630. tag_stack.pop_front();
  1631. pos = brk_end + 1;
  1632. if (tag != "/img")
  1633. pop();
  1634. } else if (tag == "b") {
  1635. //use bold font
  1636. in_bold = true;
  1637. if (in_italics)
  1638. push_font(bold_italics_font);
  1639. else
  1640. push_font(bold_font);
  1641. pos = brk_end + 1;
  1642. tag_stack.push_front(tag);
  1643. } else if (tag == "i") {
  1644. //use italics font
  1645. in_italics = true;
  1646. if (in_bold)
  1647. push_font(bold_italics_font);
  1648. else
  1649. push_font(italics_font);
  1650. pos = brk_end + 1;
  1651. tag_stack.push_front(tag);
  1652. } else if (tag == "code") {
  1653. //use monospace font
  1654. push_font(mono_font);
  1655. pos = brk_end + 1;
  1656. tag_stack.push_front(tag);
  1657. } else if (tag.begins_with("table=")) {
  1658. int columns = tag.substr(6, tag.length()).to_int();
  1659. if (columns < 1)
  1660. columns = 1;
  1661. push_table(columns);
  1662. pos = brk_end + 1;
  1663. tag_stack.push_front("table");
  1664. } else if (tag == "cell") {
  1665. push_cell();
  1666. pos = brk_end + 1;
  1667. tag_stack.push_front(tag);
  1668. } else if (tag.begins_with("cell=")) {
  1669. int ratio = tag.substr(5, tag.length()).to_int();
  1670. if (ratio < 1)
  1671. ratio = 1;
  1672. set_table_column_expand(get_current_table_column(), true, ratio);
  1673. push_cell();
  1674. pos = brk_end + 1;
  1675. tag_stack.push_front("cell");
  1676. } else if (tag == "u") {
  1677. //use underline
  1678. push_underline();
  1679. pos = brk_end + 1;
  1680. tag_stack.push_front(tag);
  1681. } else if (tag == "s") {
  1682. //use strikethrough
  1683. push_strikethrough();
  1684. pos = brk_end + 1;
  1685. tag_stack.push_front(tag);
  1686. } else if (tag == "center") {
  1687. push_align(ALIGN_CENTER);
  1688. pos = brk_end + 1;
  1689. tag_stack.push_front(tag);
  1690. } else if (tag == "fill") {
  1691. push_align(ALIGN_FILL);
  1692. pos = brk_end + 1;
  1693. tag_stack.push_front(tag);
  1694. } else if (tag == "right") {
  1695. push_align(ALIGN_RIGHT);
  1696. pos = brk_end + 1;
  1697. tag_stack.push_front(tag);
  1698. } else if (tag == "ul") {
  1699. push_list(LIST_DOTS);
  1700. pos = brk_end + 1;
  1701. tag_stack.push_front(tag);
  1702. } else if (tag == "ol") {
  1703. push_list(LIST_NUMBERS);
  1704. pos = brk_end + 1;
  1705. tag_stack.push_front(tag);
  1706. } else if (tag == "indent") {
  1707. indent_level++;
  1708. push_indent(indent_level);
  1709. pos = brk_end + 1;
  1710. tag_stack.push_front(tag);
  1711. } else if (tag == "url") {
  1712. int end = p_bbcode.find("[", brk_end);
  1713. if (end == -1)
  1714. end = p_bbcode.length();
  1715. String url = p_bbcode.substr(brk_end + 1, end - brk_end - 1);
  1716. push_meta(url);
  1717. pos = brk_end + 1;
  1718. tag_stack.push_front(tag);
  1719. } else if (tag.begins_with("url=")) {
  1720. String url = tag.substr(4, tag.length());
  1721. push_meta(url);
  1722. pos = brk_end + 1;
  1723. tag_stack.push_front("url");
  1724. } else if (tag == "img") {
  1725. int end = p_bbcode.find("[", brk_end);
  1726. if (end == -1)
  1727. end = p_bbcode.length();
  1728. String image = p_bbcode.substr(brk_end + 1, end - brk_end - 1);
  1729. Ref<Texture> texture = ResourceLoader::load(image, "Texture");
  1730. if (texture.is_valid())
  1731. add_image(texture);
  1732. pos = end;
  1733. tag_stack.push_front(tag);
  1734. } else if (tag.begins_with("img=")) {
  1735. int width = 0;
  1736. int height = 0;
  1737. String params = tag.substr(4, tag.length());
  1738. int sep = params.find("x");
  1739. if (sep == -1) {
  1740. width = params.to_int();
  1741. } else {
  1742. width = params.substr(0, sep).to_int();
  1743. height = params.substr(sep + 1, params.length()).to_int();
  1744. }
  1745. int end = p_bbcode.find("[", brk_end);
  1746. if (end == -1)
  1747. end = p_bbcode.length();
  1748. String image = p_bbcode.substr(brk_end + 1, end - brk_end - 1);
  1749. Ref<Texture> texture = ResourceLoader::load(image, "Texture");
  1750. if (texture.is_valid())
  1751. add_image(texture, width, height);
  1752. pos = end;
  1753. tag_stack.push_front("img");
  1754. } else if (tag.begins_with("color=")) {
  1755. String col = tag.substr(6, tag.length());
  1756. Color color;
  1757. if (col.begins_with("#"))
  1758. color = Color::html(col);
  1759. else if (col == "aqua")
  1760. color = Color(0, 1, 1);
  1761. else if (col == "black")
  1762. color = Color(0, 0, 0);
  1763. else if (col == "blue")
  1764. color = Color(0, 0, 1);
  1765. else if (col == "fuchsia")
  1766. color = Color(1, 0, 1);
  1767. else if (col == "gray" || col == "grey")
  1768. color = Color(0.5, 0.5, 0.5);
  1769. else if (col == "green")
  1770. color = Color(0, 0.5, 0);
  1771. else if (col == "lime")
  1772. color = Color(0, 1, 0);
  1773. else if (col == "maroon")
  1774. color = Color(0.5, 0, 0);
  1775. else if (col == "navy")
  1776. color = Color(0, 0, 0.5);
  1777. else if (col == "olive")
  1778. color = Color(0.5, 0.5, 0);
  1779. else if (col == "purple")
  1780. color = Color(0.5, 0, 0.5);
  1781. else if (col == "red")
  1782. color = Color(1, 0, 0);
  1783. else if (col == "silver")
  1784. color = Color(0.75, 0.75, 0.75);
  1785. else if (col == "teal")
  1786. color = Color(0, 0.5, 0.5);
  1787. else if (col == "white")
  1788. color = Color(1, 1, 1);
  1789. else if (col == "yellow")
  1790. color = Color(1, 1, 0);
  1791. else
  1792. color = base_color;
  1793. push_color(color);
  1794. pos = brk_end + 1;
  1795. tag_stack.push_front("color");
  1796. } else if (tag.begins_with("font=")) {
  1797. String fnt = tag.substr(5, tag.length());
  1798. Ref<Font> font = ResourceLoader::load(fnt, "Font");
  1799. if (font.is_valid())
  1800. push_font(font);
  1801. else
  1802. push_font(normal_font);
  1803. pos = brk_end + 1;
  1804. tag_stack.push_front("font");
  1805. } else if (bbcode == "fade") {
  1806. int startIndex = 0;
  1807. int length = 10;
  1808. if (split_tag_block.size() > 1) {
  1809. split_tag_block.remove(0);
  1810. for (int i = 0; i < split_tag_block.size(); i++) {
  1811. String expr = split_tag_block[i];
  1812. if (expr.begins_with("start=")) {
  1813. String start_str = expr.substr(6, expr.length());
  1814. startIndex = start_str.to_int();
  1815. } else if (expr.begins_with("length=")) {
  1816. String end_str = expr.substr(7, expr.length());
  1817. length = end_str.to_int();
  1818. }
  1819. }
  1820. }
  1821. push_fade(startIndex, length);
  1822. pos = brk_end + 1;
  1823. tag_stack.push_front("fade");
  1824. } else if (bbcode == "shake") {
  1825. int strength = 5;
  1826. float rate = 20.0f;
  1827. if (split_tag_block.size() > 1) {
  1828. split_tag_block.remove(0);
  1829. for (int i = 0; i < split_tag_block.size(); i++) {
  1830. String expr = split_tag_block[i];
  1831. if (expr.begins_with("level=")) {
  1832. String str_str = expr.substr(6, expr.length());
  1833. strength = str_str.to_int();
  1834. } else if (expr.begins_with("rate=")) {
  1835. String rate_str = expr.substr(5, expr.length());
  1836. rate = rate_str.to_float();
  1837. }
  1838. }
  1839. }
  1840. push_shake(strength, rate);
  1841. pos = brk_end + 1;
  1842. tag_stack.push_front("shake");
  1843. set_process_internal(true);
  1844. } else if (bbcode == "wave") {
  1845. float amplitude = 20.0f;
  1846. float period = 5.0f;
  1847. if (split_tag_block.size() > 1) {
  1848. split_tag_block.remove(0);
  1849. for (int i = 0; i < split_tag_block.size(); i++) {
  1850. String expr = split_tag_block[i];
  1851. if (expr.begins_with("amp=")) {
  1852. String amp_str = expr.substr(4, expr.length());
  1853. amplitude = amp_str.to_float();
  1854. } else if (expr.begins_with("freq=")) {
  1855. String period_str = expr.substr(5, expr.length());
  1856. period = period_str.to_float();
  1857. }
  1858. }
  1859. }
  1860. push_wave(period, amplitude);
  1861. pos = brk_end + 1;
  1862. tag_stack.push_front("wave");
  1863. set_process_internal(true);
  1864. } else if (bbcode == "tornado") {
  1865. float radius = 10.0f;
  1866. float frequency = 1.0f;
  1867. if (split_tag_block.size() > 1) {
  1868. split_tag_block.remove(0);
  1869. for (int i = 0; i < split_tag_block.size(); i++) {
  1870. String expr = split_tag_block[i];
  1871. if (expr.begins_with("radius=")) {
  1872. String amp_str = expr.substr(7, expr.length());
  1873. radius = amp_str.to_float();
  1874. } else if (expr.begins_with("freq=")) {
  1875. String period_str = expr.substr(5, expr.length());
  1876. frequency = period_str.to_float();
  1877. }
  1878. }
  1879. }
  1880. push_tornado(frequency, radius);
  1881. pos = brk_end + 1;
  1882. tag_stack.push_front("tornado");
  1883. set_process_internal(true);
  1884. } else if (bbcode == "rainbow") {
  1885. float saturation = 0.8f;
  1886. float value = 0.8f;
  1887. float frequency = 1.0f;
  1888. if (split_tag_block.size() > 1) {
  1889. split_tag_block.remove(0);
  1890. for (int i = 0; i < split_tag_block.size(); i++) {
  1891. String expr = split_tag_block[i];
  1892. if (expr.begins_with("sat=")) {
  1893. String sat_str = expr.substr(4, expr.length());
  1894. saturation = sat_str.to_float();
  1895. } else if (expr.begins_with("val=")) {
  1896. String val_str = expr.substr(4, expr.length());
  1897. value = val_str.to_float();
  1898. } else if (expr.begins_with("freq=")) {
  1899. String freq_str = expr.substr(5, expr.length());
  1900. frequency = freq_str.to_float();
  1901. }
  1902. }
  1903. }
  1904. push_rainbow(saturation, value, frequency);
  1905. pos = brk_end + 1;
  1906. tag_stack.push_front("rainbow");
  1907. set_process_internal(true);
  1908. } else {
  1909. Vector<String> &expr = split_tag_block;
  1910. if (expr.size() < 1) {
  1911. add_text("[");
  1912. pos = brk_pos + 1;
  1913. } else {
  1914. String identifier = expr[0];
  1915. expr.remove(0);
  1916. Dictionary properties = parse_expressions_for_values(expr);
  1917. Ref<RichTextEffect> effect = _get_custom_effect_by_code(identifier);
  1918. if (!effect.is_null()) {
  1919. push_customfx(effect, properties);
  1920. pos = brk_end + 1;
  1921. tag_stack.push_front(identifier);
  1922. set_process_internal(true);
  1923. } else {
  1924. add_text("["); //ignore
  1925. pos = brk_pos + 1;
  1926. }
  1927. }
  1928. }
  1929. }
  1930. Vector<ItemFX *> fx_items;
  1931. for (List<Item *>::Element *E = main->subitems.front(); E; E = E->next()) {
  1932. Item *subitem = static_cast<Item *>(E->get());
  1933. _fetch_item_fx_stack(subitem, fx_items);
  1934. if (fx_items.size()) {
  1935. set_process_internal(true);
  1936. break;
  1937. }
  1938. }
  1939. return OK;
  1940. }
  1941. void RichTextLabel::scroll_to_line(int p_line) {
  1942. ERR_FAIL_INDEX(p_line, main->lines.size());
  1943. _validate_line_caches(main);
  1944. vscroll->set_value(main->lines[p_line].height_accum_cache - main->lines[p_line].height_cache);
  1945. }
  1946. int RichTextLabel::get_line_count() const {
  1947. return current_frame->lines.size();
  1948. }
  1949. int RichTextLabel::get_visible_line_count() const {
  1950. if (!is_visible())
  1951. return 0;
  1952. return visible_line_count;
  1953. }
  1954. void RichTextLabel::set_selection_enabled(bool p_enabled) {
  1955. selection.enabled = p_enabled;
  1956. if (!p_enabled) {
  1957. if (selection.active) {
  1958. selection.active = false;
  1959. update();
  1960. }
  1961. set_focus_mode(FOCUS_NONE);
  1962. } else {
  1963. set_focus_mode(FOCUS_ALL);
  1964. }
  1965. }
  1966. bool RichTextLabel::search(const String &p_string, bool p_from_selection, bool p_search_previous) {
  1967. ERR_FAIL_COND_V(!selection.enabled, false);
  1968. Item *it = main;
  1969. int charidx = 0;
  1970. if (p_from_selection && selection.active) {
  1971. it = selection.to;
  1972. charidx = selection.to_char + 1;
  1973. }
  1974. while (it) {
  1975. if (it->type == ITEM_TEXT) {
  1976. ItemText *t = static_cast<ItemText *>(it);
  1977. int sp = t->text.findn(p_string, charidx);
  1978. if (sp != -1) {
  1979. selection.from = it;
  1980. selection.from_char = sp;
  1981. selection.to = it;
  1982. selection.to_char = sp + p_string.length() - 1;
  1983. selection.active = true;
  1984. update();
  1985. _validate_line_caches(main);
  1986. int fh = _find_font(t).is_valid() ? _find_font(t)->get_height() : get_font("normal_font")->get_height();
  1987. float offset = 0;
  1988. int line = t->line;
  1989. Item *item = t;
  1990. while (item) {
  1991. if (item->type == ITEM_FRAME) {
  1992. ItemFrame *frame = static_cast<ItemFrame *>(item);
  1993. if (line >= 0 && line < frame->lines.size()) {
  1994. offset += frame->lines[line].height_accum_cache - frame->lines[line].height_cache;
  1995. line = frame->line;
  1996. }
  1997. }
  1998. item = item->parent;
  1999. }
  2000. vscroll->set_value(offset - fh);
  2001. return true;
  2002. }
  2003. }
  2004. if (p_search_previous)
  2005. it = _get_prev_item(it, true);
  2006. else
  2007. it = _get_next_item(it, true);
  2008. charidx = 0;
  2009. }
  2010. return false;
  2011. }
  2012. String RichTextLabel::get_selected_text() {
  2013. if (!selection.active || !selection.enabled) {
  2014. return "";
  2015. }
  2016. String text;
  2017. RichTextLabel::Item *item = selection.from;
  2018. while (item) {
  2019. if (item->type == ITEM_TEXT) {
  2020. String itext = static_cast<ItemText *>(item)->text;
  2021. if (item == selection.from && item == selection.to) {
  2022. text += itext.substr(selection.from_char, selection.to_char - selection.from_char + 1);
  2023. } else if (item == selection.from) {
  2024. text += itext.substr(selection.from_char, itext.size());
  2025. } else if (item == selection.to) {
  2026. text += itext.substr(0, selection.to_char + 1);
  2027. } else {
  2028. text += itext;
  2029. }
  2030. } else if (item->type == ITEM_NEWLINE) {
  2031. text += "\n";
  2032. }
  2033. if (item == selection.to)
  2034. break;
  2035. item = _get_next_item(item, true);
  2036. }
  2037. return text;
  2038. }
  2039. void RichTextLabel::selection_copy() {
  2040. String text = get_selected_text();
  2041. if (text != "") {
  2042. OS::get_singleton()->set_clipboard(text);
  2043. }
  2044. }
  2045. bool RichTextLabel::is_selection_enabled() const {
  2046. return selection.enabled;
  2047. }
  2048. void RichTextLabel::set_bbcode(const String &p_bbcode) {
  2049. bbcode = p_bbcode;
  2050. if (is_inside_tree() && use_bbcode)
  2051. parse_bbcode(p_bbcode);
  2052. else { // raw text
  2053. clear();
  2054. add_text(p_bbcode);
  2055. }
  2056. }
  2057. String RichTextLabel::get_bbcode() const {
  2058. return bbcode;
  2059. }
  2060. void RichTextLabel::set_use_bbcode(bool p_enable) {
  2061. if (use_bbcode == p_enable)
  2062. return;
  2063. use_bbcode = p_enable;
  2064. set_bbcode(bbcode);
  2065. }
  2066. bool RichTextLabel::is_using_bbcode() const {
  2067. return use_bbcode;
  2068. }
  2069. String RichTextLabel::get_text() {
  2070. String text = "";
  2071. Item *it = main;
  2072. while (it) {
  2073. if (it->type == ITEM_TEXT) {
  2074. ItemText *t = static_cast<ItemText *>(it);
  2075. text += t->text;
  2076. } else if (it->type == ITEM_NEWLINE) {
  2077. text += "\n";
  2078. } else if (it->type == ITEM_INDENT) {
  2079. text += "\t";
  2080. }
  2081. it = _get_next_item(it, true);
  2082. }
  2083. return text;
  2084. }
  2085. void RichTextLabel::set_text(const String &p_string) {
  2086. clear();
  2087. add_text(p_string);
  2088. }
  2089. void RichTextLabel::set_percent_visible(float p_percent) {
  2090. if (p_percent < 0 || p_percent >= 1) {
  2091. visible_characters = -1;
  2092. percent_visible = 1;
  2093. } else {
  2094. visible_characters = get_total_character_count() * p_percent;
  2095. percent_visible = p_percent;
  2096. }
  2097. _change_notify("visible_characters");
  2098. update();
  2099. }
  2100. float RichTextLabel::get_percent_visible() const {
  2101. return percent_visible;
  2102. }
  2103. void RichTextLabel::set_effects(const Vector<Variant> &effects) {
  2104. custom_effects.clear();
  2105. for (int i = 0; i < effects.size(); i++) {
  2106. Ref<RichTextEffect> effect = Ref<RichTextEffect>(effects[i]);
  2107. custom_effects.push_back(effect);
  2108. }
  2109. if ((bbcode != "") && use_bbcode) {
  2110. parse_bbcode(bbcode);
  2111. }
  2112. }
  2113. Vector<Variant> RichTextLabel::get_effects() {
  2114. Vector<Variant> r;
  2115. for (int i = 0; i < custom_effects.size(); i++) {
  2116. r.push_back(custom_effects[i].get_ref_ptr());
  2117. }
  2118. return r;
  2119. }
  2120. void RichTextLabel::install_effect(const Variant effect) {
  2121. Ref<RichTextEffect> rteffect;
  2122. rteffect = effect;
  2123. if (rteffect.is_valid()) {
  2124. custom_effects.push_back(effect);
  2125. if ((bbcode != "") && use_bbcode) {
  2126. parse_bbcode(bbcode);
  2127. }
  2128. }
  2129. }
  2130. int RichTextLabel::get_content_height() const {
  2131. int total_height = 0;
  2132. if (main->lines.size())
  2133. total_height = main->lines[main->lines.size() - 1].height_accum_cache + get_stylebox("normal")->get_minimum_size().height;
  2134. return total_height;
  2135. }
  2136. void RichTextLabel::_bind_methods() {
  2137. ClassDB::bind_method(D_METHOD("_gui_input"), &RichTextLabel::_gui_input);
  2138. ClassDB::bind_method(D_METHOD("_scroll_changed"), &RichTextLabel::_scroll_changed);
  2139. ClassDB::bind_method(D_METHOD("get_text"), &RichTextLabel::get_text);
  2140. ClassDB::bind_method(D_METHOD("add_text", "text"), &RichTextLabel::add_text);
  2141. ClassDB::bind_method(D_METHOD("set_text", "text"), &RichTextLabel::set_text);
  2142. ClassDB::bind_method(D_METHOD("add_image", "image", "width", "height"), &RichTextLabel::add_image, DEFVAL(0), DEFVAL(0));
  2143. ClassDB::bind_method(D_METHOD("newline"), &RichTextLabel::add_newline);
  2144. ClassDB::bind_method(D_METHOD("remove_line", "line"), &RichTextLabel::remove_line);
  2145. ClassDB::bind_method(D_METHOD("push_font", "font"), &RichTextLabel::push_font);
  2146. ClassDB::bind_method(D_METHOD("push_normal"), &RichTextLabel::push_normal);
  2147. ClassDB::bind_method(D_METHOD("push_bold"), &RichTextLabel::push_bold);
  2148. ClassDB::bind_method(D_METHOD("push_bold_italics"), &RichTextLabel::push_bold_italics);
  2149. ClassDB::bind_method(D_METHOD("push_italics"), &RichTextLabel::push_italics);
  2150. ClassDB::bind_method(D_METHOD("push_mono"), &RichTextLabel::push_mono);
  2151. ClassDB::bind_method(D_METHOD("push_color", "color"), &RichTextLabel::push_color);
  2152. ClassDB::bind_method(D_METHOD("push_align", "align"), &RichTextLabel::push_align);
  2153. ClassDB::bind_method(D_METHOD("push_indent", "level"), &RichTextLabel::push_indent);
  2154. ClassDB::bind_method(D_METHOD("push_list", "type"), &RichTextLabel::push_list);
  2155. ClassDB::bind_method(D_METHOD("push_meta", "data"), &RichTextLabel::push_meta);
  2156. ClassDB::bind_method(D_METHOD("push_underline"), &RichTextLabel::push_underline);
  2157. ClassDB::bind_method(D_METHOD("push_strikethrough"), &RichTextLabel::push_strikethrough);
  2158. ClassDB::bind_method(D_METHOD("push_table", "columns"), &RichTextLabel::push_table);
  2159. ClassDB::bind_method(D_METHOD("set_table_column_expand", "column", "expand", "ratio"), &RichTextLabel::set_table_column_expand);
  2160. ClassDB::bind_method(D_METHOD("push_cell"), &RichTextLabel::push_cell);
  2161. ClassDB::bind_method(D_METHOD("pop"), &RichTextLabel::pop);
  2162. ClassDB::bind_method(D_METHOD("clear"), &RichTextLabel::clear);
  2163. ClassDB::bind_method(D_METHOD("set_meta_underline", "enable"), &RichTextLabel::set_meta_underline);
  2164. ClassDB::bind_method(D_METHOD("is_meta_underlined"), &RichTextLabel::is_meta_underlined);
  2165. ClassDB::bind_method(D_METHOD("set_override_selected_font_color", "override"), &RichTextLabel::set_override_selected_font_color);
  2166. ClassDB::bind_method(D_METHOD("is_overriding_selected_font_color"), &RichTextLabel::is_overriding_selected_font_color);
  2167. ClassDB::bind_method(D_METHOD("set_scroll_active", "active"), &RichTextLabel::set_scroll_active);
  2168. ClassDB::bind_method(D_METHOD("is_scroll_active"), &RichTextLabel::is_scroll_active);
  2169. ClassDB::bind_method(D_METHOD("set_scroll_follow", "follow"), &RichTextLabel::set_scroll_follow);
  2170. ClassDB::bind_method(D_METHOD("is_scroll_following"), &RichTextLabel::is_scroll_following);
  2171. ClassDB::bind_method(D_METHOD("get_v_scroll"), &RichTextLabel::get_v_scroll);
  2172. ClassDB::bind_method(D_METHOD("scroll_to_line", "line"), &RichTextLabel::scroll_to_line);
  2173. ClassDB::bind_method(D_METHOD("set_tab_size", "spaces"), &RichTextLabel::set_tab_size);
  2174. ClassDB::bind_method(D_METHOD("get_tab_size"), &RichTextLabel::get_tab_size);
  2175. ClassDB::bind_method(D_METHOD("set_fit_content_height", "enabled"), &RichTextLabel::set_fit_content_height);
  2176. ClassDB::bind_method(D_METHOD("is_fit_content_height_enabled"), &RichTextLabel::is_fit_content_height_enabled);
  2177. ClassDB::bind_method(D_METHOD("set_selection_enabled", "enabled"), &RichTextLabel::set_selection_enabled);
  2178. ClassDB::bind_method(D_METHOD("is_selection_enabled"), &RichTextLabel::is_selection_enabled);
  2179. ClassDB::bind_method(D_METHOD("parse_bbcode", "bbcode"), &RichTextLabel::parse_bbcode);
  2180. ClassDB::bind_method(D_METHOD("append_bbcode", "bbcode"), &RichTextLabel::append_bbcode);
  2181. ClassDB::bind_method(D_METHOD("set_bbcode", "text"), &RichTextLabel::set_bbcode);
  2182. ClassDB::bind_method(D_METHOD("get_bbcode"), &RichTextLabel::get_bbcode);
  2183. ClassDB::bind_method(D_METHOD("set_visible_characters", "amount"), &RichTextLabel::set_visible_characters);
  2184. ClassDB::bind_method(D_METHOD("get_visible_characters"), &RichTextLabel::get_visible_characters);
  2185. ClassDB::bind_method(D_METHOD("set_percent_visible", "percent_visible"), &RichTextLabel::set_percent_visible);
  2186. ClassDB::bind_method(D_METHOD("get_percent_visible"), &RichTextLabel::get_percent_visible);
  2187. ClassDB::bind_method(D_METHOD("get_total_character_count"), &RichTextLabel::get_total_character_count);
  2188. ClassDB::bind_method(D_METHOD("set_use_bbcode", "enable"), &RichTextLabel::set_use_bbcode);
  2189. ClassDB::bind_method(D_METHOD("is_using_bbcode"), &RichTextLabel::is_using_bbcode);
  2190. ClassDB::bind_method(D_METHOD("get_line_count"), &RichTextLabel::get_line_count);
  2191. ClassDB::bind_method(D_METHOD("get_visible_line_count"), &RichTextLabel::get_visible_line_count);
  2192. ClassDB::bind_method(D_METHOD("get_content_height"), &RichTextLabel::get_content_height);
  2193. ClassDB::bind_method(D_METHOD("parse_expressions_for_values", "expressions"), &RichTextLabel::parse_expressions_for_values);
  2194. ClassDB::bind_method(D_METHOD("set_effects", "effects"), &RichTextLabel::set_effects);
  2195. ClassDB::bind_method(D_METHOD("get_effects"), &RichTextLabel::get_effects);
  2196. ClassDB::bind_method(D_METHOD("install_effect", "effect"), &RichTextLabel::install_effect);
  2197. ADD_GROUP("BBCode", "bbcode_");
  2198. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bbcode_enabled"), "set_use_bbcode", "is_using_bbcode");
  2199. ADD_PROPERTY(PropertyInfo(Variant::STRING, "bbcode_text", PROPERTY_HINT_MULTILINE_TEXT), "set_bbcode", "get_bbcode");
  2200. ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_characters", PROPERTY_HINT_RANGE, "-1,128000,1"), "set_visible_characters", "get_visible_characters");
  2201. ADD_PROPERTY(PropertyInfo(Variant::REAL, "percent_visible", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_percent_visible", "get_percent_visible");
  2202. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meta_underlined"), "set_meta_underline", "is_meta_underlined");
  2203. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_size", PROPERTY_HINT_RANGE, "0,24,1"), "set_tab_size", "get_tab_size");
  2204. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  2205. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_content_height"), "set_fit_content_height", "is_fit_content_height_enabled");
  2206. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_active"), "set_scroll_active", "is_scroll_active");
  2207. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_following"), "set_scroll_follow", "is_scroll_following");
  2208. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selection_enabled"), "set_selection_enabled", "is_selection_enabled");
  2209. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_selected_font_color"), "set_override_selected_font_color", "is_overriding_selected_font_color");
  2210. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "custom_effects", PROPERTY_HINT_RESOURCE_TYPE, "17/17:RichTextEffect", (PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE), "RichTextEffect"), "set_effects", "get_effects");
  2211. ADD_SIGNAL(MethodInfo("meta_clicked", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  2212. ADD_SIGNAL(MethodInfo("meta_hover_started", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  2213. ADD_SIGNAL(MethodInfo("meta_hover_ended", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  2214. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  2215. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  2216. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  2217. BIND_ENUM_CONSTANT(ALIGN_FILL);
  2218. BIND_ENUM_CONSTANT(LIST_NUMBERS);
  2219. BIND_ENUM_CONSTANT(LIST_LETTERS);
  2220. BIND_ENUM_CONSTANT(LIST_DOTS);
  2221. BIND_ENUM_CONSTANT(ITEM_FRAME);
  2222. BIND_ENUM_CONSTANT(ITEM_TEXT);
  2223. BIND_ENUM_CONSTANT(ITEM_IMAGE);
  2224. BIND_ENUM_CONSTANT(ITEM_NEWLINE);
  2225. BIND_ENUM_CONSTANT(ITEM_FONT);
  2226. BIND_ENUM_CONSTANT(ITEM_COLOR);
  2227. BIND_ENUM_CONSTANT(ITEM_UNDERLINE);
  2228. BIND_ENUM_CONSTANT(ITEM_STRIKETHROUGH);
  2229. BIND_ENUM_CONSTANT(ITEM_ALIGN);
  2230. BIND_ENUM_CONSTANT(ITEM_INDENT);
  2231. BIND_ENUM_CONSTANT(ITEM_LIST);
  2232. BIND_ENUM_CONSTANT(ITEM_TABLE);
  2233. BIND_ENUM_CONSTANT(ITEM_FADE);
  2234. BIND_ENUM_CONSTANT(ITEM_SHAKE);
  2235. BIND_ENUM_CONSTANT(ITEM_WAVE);
  2236. BIND_ENUM_CONSTANT(ITEM_TORNADO);
  2237. BIND_ENUM_CONSTANT(ITEM_RAINBOW);
  2238. BIND_ENUM_CONSTANT(ITEM_CUSTOMFX);
  2239. BIND_ENUM_CONSTANT(ITEM_META);
  2240. }
  2241. void RichTextLabel::set_visible_characters(int p_visible) {
  2242. visible_characters = p_visible;
  2243. if (p_visible == -1) {
  2244. percent_visible = 1;
  2245. } else {
  2246. int total_char_count = get_total_character_count();
  2247. if (total_char_count > 0) {
  2248. percent_visible = (float)p_visible / (float)total_char_count;
  2249. }
  2250. }
  2251. _change_notify("percent_visible");
  2252. update();
  2253. }
  2254. int RichTextLabel::get_visible_characters() const {
  2255. return visible_characters;
  2256. }
  2257. int RichTextLabel::get_total_character_count() const {
  2258. int tc = 0;
  2259. for (int i = 0; i < current_frame->lines.size(); i++)
  2260. tc += current_frame->lines[i].char_count;
  2261. return tc;
  2262. }
  2263. void RichTextLabel::set_fixed_size_to_width(int p_width) {
  2264. fixed_width = p_width;
  2265. minimum_size_changed();
  2266. }
  2267. Size2 RichTextLabel::get_minimum_size() const {
  2268. Size2 size(0, 0);
  2269. if (fixed_width != -1) {
  2270. size.x = fixed_width;
  2271. }
  2272. if (fixed_width != -1 || fit_content_height) {
  2273. const_cast<RichTextLabel *>(this)->_validate_line_caches(main);
  2274. size.y = get_content_height();
  2275. }
  2276. return size;
  2277. }
  2278. Ref<RichTextEffect> RichTextLabel::_get_custom_effect_by_code(String p_bbcode_identifier) {
  2279. for (int i = 0; i < custom_effects.size(); i++) {
  2280. if (!custom_effects[i].is_valid())
  2281. continue;
  2282. if (custom_effects[i]->get_bbcode() == p_bbcode_identifier) {
  2283. return custom_effects[i];
  2284. }
  2285. }
  2286. return Ref<RichTextEffect>();
  2287. }
  2288. Dictionary RichTextLabel::parse_expressions_for_values(Vector<String> p_expressions) {
  2289. Dictionary d = Dictionary();
  2290. for (int i = 0; i < p_expressions.size(); i++) {
  2291. String expression = p_expressions[i];
  2292. Array a = Array();
  2293. Vector<String> parts = expression.split("=", true);
  2294. String key = parts[0];
  2295. if (parts.size() != 2) {
  2296. return d;
  2297. }
  2298. Vector<String> values = parts[1].split(",", false);
  2299. RegEx color;
  2300. color.compile("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
  2301. RegEx nodepath;
  2302. nodepath.compile("^\\$");
  2303. RegEx boolean;
  2304. boolean.compile("^(true|false)$");
  2305. RegEx decimal;
  2306. decimal.compile("^-?^.?\\d+(\\.\\d+?)?$");
  2307. RegEx numerical;
  2308. numerical.compile("^\\d+$");
  2309. for (int j = 0; j < values.size(); j++) {
  2310. if (!color.search(values[j]).is_null()) {
  2311. a.append(Color::html(values[j]));
  2312. } else if (!nodepath.search(values[j]).is_null()) {
  2313. if (values[j].begins_with("$")) {
  2314. String v = values[j].substr(1, values[j].length());
  2315. a.append(NodePath(v));
  2316. }
  2317. } else if (!boolean.search(values[j]).is_null()) {
  2318. if (values[j] == "true") {
  2319. a.append(true);
  2320. } else if (values[j] == "false") {
  2321. a.append(false);
  2322. }
  2323. } else if (!decimal.search(values[j]).is_null()) {
  2324. a.append(values[j].to_double());
  2325. } else if (!numerical.search(values[j]).is_null()) {
  2326. a.append(values[j].to_int());
  2327. } else {
  2328. a.append(values[j]);
  2329. }
  2330. }
  2331. if (values.size() > 1) {
  2332. d[key] = a;
  2333. } else if (values.size() == 1) {
  2334. d[key] = a[0];
  2335. }
  2336. }
  2337. return d;
  2338. }
  2339. RichTextLabel::RichTextLabel() {
  2340. main = memnew(ItemFrame);
  2341. main->index = 0;
  2342. current = main;
  2343. main->lines.resize(1);
  2344. main->lines.write[0].from = main;
  2345. main->first_invalid_line = 0;
  2346. current_frame = main;
  2347. tab_size = 4;
  2348. default_align = ALIGN_LEFT;
  2349. underline_meta = true;
  2350. meta_hovering = NULL;
  2351. override_selected_font_color = false;
  2352. scroll_visible = false;
  2353. scroll_follow = false;
  2354. scroll_following = false;
  2355. updating_scroll = false;
  2356. scroll_active = true;
  2357. scroll_w = 0;
  2358. scroll_updated = false;
  2359. vscroll = memnew(VScrollBar);
  2360. add_child(vscroll);
  2361. vscroll->set_drag_node(String(".."));
  2362. vscroll->set_step(1);
  2363. vscroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0);
  2364. vscroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  2365. vscroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  2366. vscroll->connect("value_changed", this, "_scroll_changed");
  2367. vscroll->set_step(1);
  2368. vscroll->hide();
  2369. current_idx = 1;
  2370. use_bbcode = false;
  2371. selection.click = NULL;
  2372. selection.active = false;
  2373. selection.enabled = false;
  2374. visible_characters = -1;
  2375. percent_visible = 1;
  2376. visible_line_count = 0;
  2377. fixed_width = -1;
  2378. fit_content_height = false;
  2379. set_clip_contents(true);
  2380. }
  2381. RichTextLabel::~RichTextLabel() {
  2382. memdelete(main);
  2383. }