rich_text_label.cpp 98 KB

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