rich_text_label.cpp 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014
  1. /*************************************************************************/
  2. /* rich_text_label.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "rich_text_label.h"
  31. #include "core/math/math_defs.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/os/os.h"
  34. #include "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. if (p_font_color_shadow.a > 0) {
  509. const Point2 shadow_base_pos = p_ofs + Point2(align_ofs + pofs, y + lh - line_descent);
  510. font->draw_char(ci, shadow_base_pos + shadow_ofs + fx_offset, fx_char, c[i + 1], p_font_color_shadow);
  511. if (p_shadow_as_outline) {
  512. font->draw_char(ci, shadow_base_pos + Vector2(-shadow_ofs.x, shadow_ofs.y) + fx_offset, fx_char, c[i + 1], p_font_color_shadow);
  513. font->draw_char(ci, shadow_base_pos + Vector2(shadow_ofs.x, -shadow_ofs.y) + fx_offset, fx_char, c[i + 1], p_font_color_shadow);
  514. font->draw_char(ci, shadow_base_pos + Vector2(-shadow_ofs.x, -shadow_ofs.y) + fx_offset, fx_char, c[i + 1], p_font_color_shadow);
  515. }
  516. }
  517. if (selected) {
  518. drawer.draw_char(ci, p_ofs + Point2(align_ofs + pofs, y + lh - line_descent), fx_char, c[i + 1], override_selected_font_color ? selection_fg : fx_color);
  519. } else {
  520. cw = drawer.draw_char(ci, p_ofs + Point2(align_ofs + pofs, y + lh - line_descent) + fx_offset, fx_char, c[i + 1], fx_color);
  521. }
  522. } else if (previously_visible && c[i] != '\t') {
  523. backtrack += font->get_char_size(fx_char, c[i + 1]).x;
  524. }
  525. p_char_count++;
  526. if (c[i] == '\t') {
  527. cw = tab_size * font->get_char_size(' ').width;
  528. backtrack = MAX(0, backtrack - cw);
  529. }
  530. ofs += cw;
  531. }
  532. }
  533. #ifdef TOOLS_ENABLED
  534. const float line_width = EDSCALE;
  535. #else
  536. const float line_width = 1.0f;
  537. #endif
  538. if (underline) {
  539. Color uc = color;
  540. uc.a *= 0.5;
  541. const int line_y = y + lh - (line_descent - 2);
  542. const Point2 from = p_ofs + Point2(align_ofs + wofs, line_y);
  543. const Point2 to = from + Point2(w + (is_at_line_wrap ? 0 : align_spacing), 0);
  544. VS::get_singleton()->canvas_item_add_line(ci, from, to, uc, line_width);
  545. }
  546. if (strikethrough) {
  547. Color uc = color;
  548. uc.a *= 0.5;
  549. const int line_y = y + lh - (line_ascent + line_descent) / 2;
  550. const Point2 from = p_ofs + Point2(align_ofs + wofs, line_y);
  551. const Point2 to = from + Point2(w + (is_at_line_wrap ? 0 : align_spacing), 0);
  552. VS::get_singleton()->canvas_item_add_line(ci, from, to, uc, line_width);
  553. }
  554. }
  555. ADVANCE(fw);
  556. CHECK_HEIGHT(fh); //must be done somewhere
  557. c = &c[end];
  558. }
  559. } break;
  560. case ITEM_IMAGE: {
  561. lh = 0;
  562. if (p_mode != PROCESS_CACHE) {
  563. lh = line < l.height_caches.size() ? l.height_caches[line] : 1;
  564. } else {
  565. l.char_count += 1; //images count as chars too
  566. }
  567. ItemImage *img = static_cast<ItemImage *>(it);
  568. Ref<Font> font = _find_font(it);
  569. if (font.is_null()) {
  570. font = p_base_font;
  571. }
  572. if (p_mode == PROCESS_POINTER && r_click_char) {
  573. *r_click_char = 0;
  574. }
  575. ENSURE_WIDTH(img->size.width);
  576. bool visible = visible_characters < 0 || (p_char_count < visible_characters && YRANGE_VISIBLE(y + lh - font->get_descent() - img->size.height, img->size.height));
  577. if (visible) {
  578. line_is_blank = false;
  579. }
  580. if (p_mode == PROCESS_DRAW && visible) {
  581. img->image->draw_rect(ci, Rect2(p_ofs + Point2(align_ofs + wofs, y + lh - font->get_descent() - img->size.height), img->size));
  582. }
  583. p_char_count++;
  584. ADVANCE(img->size.width);
  585. CHECK_HEIGHT((img->size.height + font->get_descent()));
  586. } break;
  587. case ITEM_NEWLINE: {
  588. lh = 0;
  589. if (p_mode != PROCESS_CACHE) {
  590. lh = line < l.height_caches.size() ? l.height_caches[line] : 1;
  591. line_is_blank = true;
  592. }
  593. } break;
  594. case ITEM_TABLE: {
  595. lh = 0;
  596. ItemTable *table = static_cast<ItemTable *>(it);
  597. int hseparation = get_constant("table_hseparation");
  598. int vseparation = get_constant("table_vseparation");
  599. Color ccolor = _find_color(table, p_base_color);
  600. Vector2 draw_ofs = Point2(wofs, y);
  601. Color font_color_shadow = get_color("font_color_shadow");
  602. bool use_outline = get_constant("shadow_as_outline");
  603. Point2 shadow_ofs2(get_constant("shadow_offset_x"), get_constant("shadow_offset_y"));
  604. if (p_mode == PROCESS_CACHE) {
  605. int idx = 0;
  606. //set minimums to zero
  607. for (int i = 0; i < table->columns.size(); i++) {
  608. table->columns.write[i].min_width = 0;
  609. table->columns.write[i].max_width = 0;
  610. table->columns.write[i].width = 0;
  611. }
  612. //compute minimum width for each cell
  613. const int available_width = p_width - hseparation * (table->columns.size() - 1) - wofs;
  614. for (List<Item *>::Element *E = table->subitems.front(); E; E = E->next()) {
  615. ERR_CONTINUE(E->get()->type != ITEM_FRAME); //children should all be frames
  616. ItemFrame *frame = static_cast<ItemFrame *>(E->get());
  617. int column = idx % table->columns.size();
  618. int ly = 0;
  619. for (int i = 0; i < frame->lines.size(); i++) {
  620. _process_line(frame, Point2(), ly, available_width, i, PROCESS_CACHE, cfont, Color(), font_color_shadow, use_outline, shadow_ofs2);
  621. table->columns.write[column].min_width = MAX(table->columns[column].min_width, frame->lines[i].minimum_width);
  622. table->columns.write[column].max_width = MAX(table->columns[column].max_width, frame->lines[i].maximum_width);
  623. }
  624. idx++;
  625. }
  626. //compute available width and total ratio (for expanders)
  627. int total_ratio = 0;
  628. int remaining_width = available_width;
  629. table->total_width = hseparation;
  630. for (int i = 0; i < table->columns.size(); i++) {
  631. remaining_width -= table->columns[i].min_width;
  632. if (table->columns[i].max_width > table->columns[i].min_width) {
  633. table->columns.write[i].expand = true;
  634. }
  635. if (table->columns[i].expand) {
  636. total_ratio += table->columns[i].expand_ratio;
  637. }
  638. }
  639. //assign actual widths
  640. for (int i = 0; i < table->columns.size(); i++) {
  641. table->columns.write[i].width = table->columns[i].min_width;
  642. if (table->columns[i].expand && total_ratio > 0) {
  643. table->columns.write[i].width += table->columns[i].expand_ratio * remaining_width / total_ratio;
  644. }
  645. table->total_width += table->columns[i].width + hseparation;
  646. }
  647. //resize to max_width if needed and distribute the remaining space
  648. bool table_need_fit = true;
  649. while (table_need_fit) {
  650. table_need_fit = false;
  651. //fit slim
  652. for (int i = 0; i < table->columns.size(); i++) {
  653. if (!table->columns[i].expand) {
  654. continue;
  655. }
  656. int dif = table->columns[i].width - table->columns[i].max_width;
  657. if (dif > 0) {
  658. table_need_fit = true;
  659. table->columns.write[i].width = table->columns[i].max_width;
  660. table->total_width -= dif;
  661. total_ratio -= table->columns[i].expand_ratio;
  662. }
  663. }
  664. //grow
  665. remaining_width = available_width - table->total_width;
  666. if (remaining_width > 0 && total_ratio > 0) {
  667. for (int i = 0; i < table->columns.size(); i++) {
  668. if (table->columns[i].expand) {
  669. int dif = table->columns[i].max_width - table->columns[i].width;
  670. if (dif > 0) {
  671. int slice = table->columns[i].expand_ratio * remaining_width / total_ratio;
  672. int incr = MIN(dif, slice);
  673. table->columns.write[i].width += incr;
  674. table->total_width += incr;
  675. }
  676. }
  677. }
  678. }
  679. }
  680. //compute caches properly again with the right width
  681. idx = 0;
  682. for (List<Item *>::Element *E = table->subitems.front(); E; E = E->next()) {
  683. ERR_CONTINUE(E->get()->type != ITEM_FRAME); //children should all be frames
  684. ItemFrame *frame = static_cast<ItemFrame *>(E->get());
  685. int column = idx % table->columns.size();
  686. for (int i = 0; i < frame->lines.size(); i++) {
  687. int ly = 0;
  688. _process_line(frame, Point2(), ly, table->columns[column].width, i, PROCESS_CACHE, cfont, Color(), font_color_shadow, use_outline, shadow_ofs2);
  689. frame->lines.write[i].height_cache = ly; //actual height
  690. frame->lines.write[i].height_accum_cache = ly; //actual height
  691. }
  692. idx++;
  693. }
  694. }
  695. Point2 offset(align_ofs + hseparation, vseparation);
  696. int row_height = 0;
  697. //draw using computed caches
  698. int idx = 0;
  699. for (List<Item *>::Element *E = table->subitems.front(); E; E = E->next()) {
  700. ERR_CONTINUE(E->get()->type != ITEM_FRAME); //children should all be frames
  701. ItemFrame *frame = static_cast<ItemFrame *>(E->get());
  702. int column = idx % table->columns.size();
  703. int ly = 0;
  704. int yofs = 0;
  705. int lines_h = frame->lines[frame->lines.size() - 1].height_accum_cache - (frame->lines[0].height_accum_cache - frame->lines[0].height_cache);
  706. int lines_ofs = p_ofs.y + offset.y + draw_ofs.y;
  707. bool visible = lines_ofs < get_size().height && lines_ofs + lines_h >= 0;
  708. if (visible) {
  709. line_is_blank = false;
  710. }
  711. for (int i = 0; i < frame->lines.size(); i++) {
  712. if (visible) {
  713. if (p_mode == PROCESS_DRAW) {
  714. 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);
  715. } else if (p_mode == PROCESS_POINTER) {
  716. _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);
  717. if (r_click_item && *r_click_item) {
  718. RETURN; // exit early
  719. }
  720. }
  721. }
  722. yofs += frame->lines[i].height_cache;
  723. if (p_mode == PROCESS_CACHE) {
  724. frame->lines.write[i].height_accum_cache = offset.y + draw_ofs.y + frame->lines[i].height_cache;
  725. }
  726. }
  727. row_height = MAX(yofs, row_height);
  728. offset.x += table->columns[column].width + hseparation;
  729. // Add row height after last column of the row or last cell of the table.
  730. if (column == table->columns.size() - 1 || E->next() == nullptr) {
  731. offset.y += row_height + vseparation;
  732. offset.x = hseparation;
  733. row_height = 0;
  734. }
  735. idx++;
  736. }
  737. int total_height = offset.y;
  738. if (row_height) {
  739. total_height = row_height + vseparation;
  740. }
  741. ADVANCE(table->total_width);
  742. CHECK_HEIGHT(total_height);
  743. } break;
  744. default: {
  745. }
  746. }
  747. Item *itp = it;
  748. it = _get_next_item(it);
  749. if (it && (p_line + 1 < p_frame->lines.size()) && p_frame->lines[p_line + 1].from == it) {
  750. 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) {
  751. //went to next line, but pointer was on the previous one
  752. if (r_outside) {
  753. *r_outside = true;
  754. }
  755. *r_click_item = itp;
  756. *r_click_char = rchar;
  757. RETURN;
  758. }
  759. break;
  760. }
  761. }
  762. NEW_LINE;
  763. RETURN;
  764. #undef RETURN
  765. #undef NEW_LINE
  766. #undef ENSURE_WIDTH
  767. #undef ADVANCE
  768. #undef CHECK_HEIGHT
  769. }
  770. void RichTextLabel::_scroll_changed(double) {
  771. if (updating_scroll) {
  772. return;
  773. }
  774. if (scroll_follow && vscroll->get_value() >= (vscroll->get_max() - vscroll->get_page())) {
  775. scroll_following = true;
  776. } else {
  777. scroll_following = false;
  778. }
  779. scroll_updated = true;
  780. update();
  781. }
  782. void RichTextLabel::_update_scroll() {
  783. int total_height = get_content_height();
  784. bool exceeds = total_height > get_size().height && scroll_active;
  785. if (exceeds != scroll_visible) {
  786. if (exceeds) {
  787. scroll_visible = true;
  788. scroll_w = vscroll->get_combined_minimum_size().width;
  789. vscroll->show();
  790. vscroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -scroll_w);
  791. } else {
  792. scroll_visible = false;
  793. scroll_w = 0;
  794. vscroll->hide();
  795. }
  796. main->first_invalid_line = 0; //invalidate ALL
  797. _validate_line_caches(main);
  798. }
  799. }
  800. void RichTextLabel::_update_fx(RichTextLabel::ItemFrame *p_frame, float p_delta_time) {
  801. Item *it = p_frame;
  802. while (it) {
  803. ItemFX *ifx = nullptr;
  804. if (it->type == ITEM_CUSTOMFX || it->type == ITEM_SHAKE || it->type == ITEM_WAVE || it->type == ITEM_TORNADO || it->type == ITEM_RAINBOW) {
  805. ifx = static_cast<ItemFX *>(it);
  806. }
  807. if (!ifx) {
  808. it = _get_next_item(it, true);
  809. continue;
  810. }
  811. ifx->elapsed_time += p_delta_time;
  812. ItemShake *shake = nullptr;
  813. if (it->type == ITEM_SHAKE) {
  814. shake = static_cast<ItemShake *>(it);
  815. }
  816. if (shake) {
  817. bool cycle = (shake->elapsed_time > (1.0f / shake->rate));
  818. if (cycle) {
  819. shake->elapsed_time -= (1.0f / shake->rate);
  820. shake->reroll_random();
  821. }
  822. }
  823. it = _get_next_item(it, true);
  824. }
  825. }
  826. void RichTextLabel::_notification(int p_what) {
  827. switch (p_what) {
  828. case NOTIFICATION_MOUSE_EXIT: {
  829. if (meta_hovering) {
  830. meta_hovering = nullptr;
  831. emit_signal("meta_hover_ended", current_meta);
  832. current_meta = false;
  833. update();
  834. }
  835. } break;
  836. case NOTIFICATION_RESIZED: {
  837. main->first_invalid_line = 0; //invalidate ALL
  838. update();
  839. } break;
  840. case NOTIFICATION_THEME_CHANGED:
  841. case NOTIFICATION_ENTER_TREE: {
  842. if (bbcode != "") {
  843. set_bbcode(bbcode);
  844. }
  845. main->first_invalid_line = 0; //invalidate ALL
  846. update();
  847. } break;
  848. case NOTIFICATION_DRAW: {
  849. _validate_line_caches(main);
  850. _update_scroll();
  851. RID ci = get_canvas_item();
  852. Size2 size = get_size();
  853. Rect2 text_rect = _get_text_rect();
  854. draw_style_box(get_stylebox("normal"), Rect2(Point2(), size));
  855. if (has_focus()) {
  856. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, true);
  857. draw_style_box(get_stylebox("focus"), Rect2(Point2(), size));
  858. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, false);
  859. }
  860. int ofs = vscroll->get_value();
  861. //todo, change to binary search
  862. int from_line = 0;
  863. int total_chars = 0;
  864. while (from_line < main->lines.size()) {
  865. if (main->lines[from_line].height_accum_cache + _get_text_rect().get_position().y >= ofs) {
  866. break;
  867. }
  868. total_chars += main->lines[from_line].char_count;
  869. from_line++;
  870. }
  871. if (from_line >= main->lines.size()) {
  872. break; //nothing to draw
  873. }
  874. int y = (main->lines[from_line].height_accum_cache - main->lines[from_line].height_cache) - ofs;
  875. Ref<Font> base_font = get_font("normal_font");
  876. Color base_color = get_color("default_color");
  877. Color font_color_shadow = get_color("font_color_shadow");
  878. bool use_outline = get_constant("shadow_as_outline");
  879. Point2 shadow_ofs(get_constant("shadow_offset_x"), get_constant("shadow_offset_y"));
  880. visible_line_count = 0;
  881. while (y < size.height && from_line < main->lines.size()) {
  882. 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);
  883. total_chars += main->lines[from_line].char_count;
  884. from_line++;
  885. }
  886. } break;
  887. case NOTIFICATION_INTERNAL_PROCESS: {
  888. if (is_visible_in_tree()) {
  889. float dt = get_process_delta_time();
  890. _update_fx(main, dt);
  891. update();
  892. }
  893. }
  894. }
  895. }
  896. void RichTextLabel::_find_click(ItemFrame *p_frame, const Point2i &p_click, Item **r_click_item, int *r_click_char, bool *r_outside) {
  897. if (r_click_item) {
  898. *r_click_item = nullptr;
  899. }
  900. Rect2 text_rect = _get_text_rect();
  901. int ofs = vscroll->get_value();
  902. Color font_color_shadow = get_color("font_color_shadow");
  903. bool use_outline = get_constant("shadow_as_outline");
  904. Point2 shadow_ofs(get_constant("shadow_offset_x"), get_constant("shadow_offset_y"));
  905. //todo, change to binary search
  906. int from_line = 0;
  907. while (from_line < p_frame->lines.size()) {
  908. if (p_frame->lines[from_line].height_accum_cache >= ofs) {
  909. break;
  910. }
  911. from_line++;
  912. }
  913. if (from_line >= p_frame->lines.size()) {
  914. return;
  915. }
  916. int y = (p_frame->lines[from_line].height_accum_cache - p_frame->lines[from_line].height_cache) - ofs;
  917. Ref<Font> base_font = get_font("normal_font");
  918. Color base_color = get_color("default_color");
  919. while (y < text_rect.get_size().height && from_line < p_frame->lines.size()) {
  920. _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);
  921. if (r_click_item && *r_click_item) {
  922. return;
  923. }
  924. from_line++;
  925. }
  926. }
  927. Control::CursorShape RichTextLabel::get_cursor_shape(const Point2 &p_pos) const {
  928. if (!underline_meta) {
  929. return get_default_cursor_shape();
  930. }
  931. if (selection.click) {
  932. return CURSOR_IBEAM;
  933. }
  934. if (main->first_invalid_line < main->lines.size()) {
  935. return get_default_cursor_shape(); //invalid
  936. }
  937. int line = 0;
  938. Item *item = nullptr;
  939. bool outside;
  940. ((RichTextLabel *)(this))->_find_click(main, p_pos, &item, &line, &outside);
  941. if (item && !outside && ((RichTextLabel *)(this))->_find_meta(item, nullptr)) {
  942. return CURSOR_POINTING_HAND;
  943. }
  944. return get_default_cursor_shape();
  945. }
  946. void RichTextLabel::_gui_input(Ref<InputEvent> p_event) {
  947. Ref<InputEventMouseButton> b = p_event;
  948. if (b.is_valid()) {
  949. if (main->first_invalid_line < main->lines.size()) {
  950. return;
  951. }
  952. if (b->get_button_index() == BUTTON_LEFT) {
  953. if (b->is_pressed() && !b->is_doubleclick()) {
  954. scroll_updated = false;
  955. int line = 0;
  956. Item *item = nullptr;
  957. bool outside;
  958. _find_click(main, b->get_position(), &item, &line, &outside);
  959. if (item) {
  960. if (selection.enabled) {
  961. selection.click = item;
  962. selection.click_char = line;
  963. // Erase previous selection.
  964. if (selection.active) {
  965. selection.from = nullptr;
  966. selection.from_char = '\0';
  967. selection.to = nullptr;
  968. selection.to_char = '\0';
  969. selection.active = false;
  970. update();
  971. }
  972. }
  973. }
  974. } else if (b->is_pressed() && b->is_doubleclick() && selection.enabled) {
  975. //doubleclick: select word
  976. int line = 0;
  977. Item *item = nullptr;
  978. bool outside;
  979. _find_click(main, b->get_position(), &item, &line, &outside);
  980. while (item && item->type != ITEM_TEXT) {
  981. item = _get_next_item(item, true);
  982. }
  983. if (item && item->type == ITEM_TEXT) {
  984. String itext = static_cast<ItemText *>(item)->text;
  985. int beg, end;
  986. if (select_word(itext, line, beg, end)) {
  987. selection.from = item;
  988. selection.to = item;
  989. selection.from_char = beg;
  990. selection.to_char = end - 1;
  991. selection.active = true;
  992. update();
  993. }
  994. }
  995. } else if (!b->is_pressed()) {
  996. selection.click = nullptr;
  997. if (!b->is_doubleclick() && !scroll_updated) {
  998. int line = 0;
  999. Item *item = nullptr;
  1000. bool outside;
  1001. _find_click(main, b->get_position(), &item, &line, &outside);
  1002. if (item) {
  1003. Variant meta;
  1004. if (!outside && _find_meta(item, &meta)) {
  1005. //meta clicked
  1006. emit_signal("meta_clicked", meta);
  1007. }
  1008. }
  1009. }
  1010. }
  1011. }
  1012. if (b->get_button_index() == BUTTON_WHEEL_UP) {
  1013. if (scroll_active) {
  1014. vscroll->set_value(vscroll->get_value() - vscroll->get_page() * b->get_factor() * 0.5 / 8);
  1015. }
  1016. }
  1017. if (b->get_button_index() == BUTTON_WHEEL_DOWN) {
  1018. if (scroll_active) {
  1019. vscroll->set_value(vscroll->get_value() + vscroll->get_page() * b->get_factor() * 0.5 / 8);
  1020. }
  1021. }
  1022. }
  1023. Ref<InputEventPanGesture> pan_gesture = p_event;
  1024. if (pan_gesture.is_valid()) {
  1025. if (scroll_active) {
  1026. vscroll->set_value(vscroll->get_value() + vscroll->get_page() * pan_gesture->get_delta().y * 0.5 / 8);
  1027. }
  1028. return;
  1029. }
  1030. Ref<InputEventKey> k = p_event;
  1031. if (k.is_valid()) {
  1032. if (k->is_pressed() && !k->get_alt() && !k->get_shift()) {
  1033. bool handled = false;
  1034. switch (k->get_scancode()) {
  1035. case KEY_PAGEUP: {
  1036. if (vscroll->is_visible_in_tree()) {
  1037. vscroll->set_value(vscroll->get_value() - vscroll->get_page());
  1038. handled = true;
  1039. }
  1040. } break;
  1041. case KEY_PAGEDOWN: {
  1042. if (vscroll->is_visible_in_tree()) {
  1043. vscroll->set_value(vscroll->get_value() + vscroll->get_page());
  1044. handled = true;
  1045. }
  1046. } break;
  1047. case KEY_UP: {
  1048. if (vscroll->is_visible_in_tree()) {
  1049. vscroll->set_value(vscroll->get_value() - get_font("normal_font")->get_height());
  1050. handled = true;
  1051. }
  1052. } break;
  1053. case KEY_DOWN: {
  1054. if (vscroll->is_visible_in_tree()) {
  1055. vscroll->set_value(vscroll->get_value() + get_font("normal_font")->get_height());
  1056. handled = true;
  1057. }
  1058. } break;
  1059. case KEY_HOME: {
  1060. if (vscroll->is_visible_in_tree()) {
  1061. vscroll->set_value(0);
  1062. handled = true;
  1063. }
  1064. } break;
  1065. case KEY_END: {
  1066. if (vscroll->is_visible_in_tree()) {
  1067. vscroll->set_value(vscroll->get_max());
  1068. handled = true;
  1069. }
  1070. } break;
  1071. case KEY_INSERT:
  1072. case KEY_C: {
  1073. if (k->get_command()) {
  1074. selection_copy();
  1075. handled = true;
  1076. }
  1077. } break;
  1078. }
  1079. if (handled) {
  1080. accept_event();
  1081. }
  1082. }
  1083. }
  1084. Ref<InputEventMouseMotion> m = p_event;
  1085. if (m.is_valid()) {
  1086. if (main->first_invalid_line < main->lines.size()) {
  1087. return;
  1088. }
  1089. int line = 0;
  1090. Item *item = nullptr;
  1091. bool outside;
  1092. _find_click(main, m->get_position(), &item, &line, &outside);
  1093. if (selection.click) {
  1094. if (!item) {
  1095. return; // do not update
  1096. }
  1097. selection.from = selection.click;
  1098. selection.from_char = selection.click_char;
  1099. selection.to = item;
  1100. selection.to_char = line;
  1101. bool swap = false;
  1102. if (selection.from->index > selection.to->index) {
  1103. swap = true;
  1104. } else if (selection.from->index == selection.to->index) {
  1105. if (selection.from_char > selection.to_char) {
  1106. swap = true;
  1107. } else if (selection.from_char == selection.to_char) {
  1108. selection.active = false;
  1109. return;
  1110. }
  1111. }
  1112. if (swap) {
  1113. SWAP(selection.from, selection.to);
  1114. SWAP(selection.from_char, selection.to_char);
  1115. }
  1116. selection.active = true;
  1117. update();
  1118. }
  1119. Variant meta;
  1120. ItemMeta *item_meta;
  1121. if (item && !outside && _find_meta(item, &meta, &item_meta)) {
  1122. if (meta_hovering != item_meta) {
  1123. if (meta_hovering) {
  1124. emit_signal("meta_hover_ended", current_meta);
  1125. }
  1126. meta_hovering = item_meta;
  1127. current_meta = meta;
  1128. emit_signal("meta_hover_started", meta);
  1129. }
  1130. } else if (meta_hovering) {
  1131. meta_hovering = nullptr;
  1132. emit_signal("meta_hover_ended", current_meta);
  1133. current_meta = false;
  1134. }
  1135. }
  1136. }
  1137. Ref<Font> RichTextLabel::_find_font(Item *p_item) {
  1138. Item *fontitem = p_item;
  1139. while (fontitem) {
  1140. if (fontitem->type == ITEM_FONT) {
  1141. ItemFont *fi = static_cast<ItemFont *>(fontitem);
  1142. return fi->font;
  1143. }
  1144. fontitem = fontitem->parent;
  1145. }
  1146. return Ref<Font>();
  1147. }
  1148. int RichTextLabel::_find_margin(Item *p_item, const Ref<Font> &p_base_font) {
  1149. Item *item = p_item;
  1150. int margin = 0;
  1151. while (item) {
  1152. if (item->type == ITEM_INDENT) {
  1153. Ref<Font> font = _find_font(item);
  1154. if (font.is_null()) {
  1155. font = p_base_font;
  1156. }
  1157. ItemIndent *indent = static_cast<ItemIndent *>(item);
  1158. margin += indent->level * tab_size * font->get_char_size(' ').width;
  1159. } else if (item->type == ITEM_LIST) {
  1160. Ref<Font> font = _find_font(item);
  1161. if (font.is_null()) {
  1162. font = p_base_font;
  1163. }
  1164. }
  1165. item = item->parent;
  1166. }
  1167. return margin;
  1168. }
  1169. RichTextLabel::Align RichTextLabel::_find_align(Item *p_item) {
  1170. Item *item = p_item;
  1171. while (item) {
  1172. if (item->type == ITEM_ALIGN) {
  1173. ItemAlign *align = static_cast<ItemAlign *>(item);
  1174. return align->align;
  1175. }
  1176. item = item->parent;
  1177. }
  1178. return default_align;
  1179. }
  1180. Color RichTextLabel::_find_color(Item *p_item, const Color &p_default_color) {
  1181. Item *item = p_item;
  1182. while (item) {
  1183. if (item->type == ITEM_COLOR) {
  1184. ItemColor *color = static_cast<ItemColor *>(item);
  1185. return color->color;
  1186. }
  1187. item = item->parent;
  1188. }
  1189. return p_default_color;
  1190. }
  1191. bool RichTextLabel::_find_underline(Item *p_item) {
  1192. Item *item = p_item;
  1193. while (item) {
  1194. if (item->type == ITEM_UNDERLINE) {
  1195. return true;
  1196. }
  1197. item = item->parent;
  1198. }
  1199. return false;
  1200. }
  1201. bool RichTextLabel::_find_strikethrough(Item *p_item) {
  1202. Item *item = p_item;
  1203. while (item) {
  1204. if (item->type == ITEM_STRIKETHROUGH) {
  1205. return true;
  1206. }
  1207. item = item->parent;
  1208. }
  1209. return false;
  1210. }
  1211. bool RichTextLabel::_find_by_type(Item *p_item, ItemType p_type) {
  1212. ERR_FAIL_INDEX_V((int)p_type, 19, false);
  1213. Item *item = p_item;
  1214. while (item) {
  1215. if (item->type == p_type) {
  1216. return true;
  1217. }
  1218. item = item->parent;
  1219. }
  1220. return false;
  1221. }
  1222. void RichTextLabel::_fetch_item_fx_stack(Item *p_item, Vector<ItemFX *> &r_stack) {
  1223. Item *item = p_item;
  1224. while (item) {
  1225. if (item->type == ITEM_CUSTOMFX || item->type == ITEM_SHAKE || item->type == ITEM_WAVE || item->type == ITEM_TORNADO || item->type == ITEM_RAINBOW) {
  1226. r_stack.push_back(static_cast<ItemFX *>(item));
  1227. }
  1228. item = item->parent;
  1229. }
  1230. }
  1231. bool RichTextLabel::_find_meta(Item *p_item, Variant *r_meta, ItemMeta **r_item) {
  1232. Item *item = p_item;
  1233. while (item) {
  1234. if (item->type == ITEM_META) {
  1235. ItemMeta *meta = static_cast<ItemMeta *>(item);
  1236. if (r_meta) {
  1237. *r_meta = meta->meta;
  1238. }
  1239. if (r_item) {
  1240. *r_item = meta;
  1241. }
  1242. return true;
  1243. }
  1244. item = item->parent;
  1245. }
  1246. return false;
  1247. }
  1248. bool RichTextLabel::_find_layout_subitem(Item *from, Item *to) {
  1249. if (from && from != to) {
  1250. if (from->type != ITEM_FONT && from->type != ITEM_COLOR && from->type != ITEM_UNDERLINE && from->type != ITEM_STRIKETHROUGH) {
  1251. return true;
  1252. }
  1253. for (List<Item *>::Element *E = from->subitems.front(); E; E = E->next()) {
  1254. bool layout = _find_layout_subitem(E->get(), to);
  1255. if (layout) {
  1256. return true;
  1257. }
  1258. }
  1259. }
  1260. return false;
  1261. }
  1262. void RichTextLabel::_validate_line_caches(ItemFrame *p_frame) {
  1263. if (p_frame->first_invalid_line == p_frame->lines.size()) {
  1264. return;
  1265. }
  1266. //validate invalid lines
  1267. Size2 size = get_size();
  1268. if (fixed_width != -1) {
  1269. size.width = fixed_width;
  1270. }
  1271. Rect2 text_rect = _get_text_rect();
  1272. Color font_color_shadow = get_color("font_color_shadow");
  1273. bool use_outline = get_constant("shadow_as_outline");
  1274. Point2 shadow_ofs(get_constant("shadow_offset_x"), get_constant("shadow_offset_y"));
  1275. Ref<Font> base_font = get_font("normal_font");
  1276. for (int i = p_frame->first_invalid_line; i < p_frame->lines.size(); i++) {
  1277. int y = 0;
  1278. _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);
  1279. p_frame->lines.write[i].height_cache = y;
  1280. p_frame->lines.write[i].height_accum_cache = y;
  1281. if (i > 0) {
  1282. p_frame->lines.write[i].height_accum_cache += p_frame->lines[i - 1].height_accum_cache;
  1283. }
  1284. }
  1285. int total_height = 0;
  1286. if (p_frame->lines.size()) {
  1287. total_height = p_frame->lines[p_frame->lines.size() - 1].height_accum_cache + get_stylebox("normal")->get_minimum_size().height;
  1288. }
  1289. main->first_invalid_line = p_frame->lines.size();
  1290. updating_scroll = true;
  1291. vscroll->set_max(total_height);
  1292. vscroll->set_page(size.height);
  1293. if (scroll_follow && scroll_following) {
  1294. vscroll->set_value(total_height - size.height);
  1295. }
  1296. updating_scroll = false;
  1297. if (fit_content_height) {
  1298. minimum_size_changed();
  1299. }
  1300. }
  1301. void RichTextLabel::_invalidate_current_line(ItemFrame *p_frame) {
  1302. if (p_frame->lines.size() - 1 <= p_frame->first_invalid_line) {
  1303. p_frame->first_invalid_line = p_frame->lines.size() - 1;
  1304. update();
  1305. }
  1306. }
  1307. void RichTextLabel::add_text(const String &p_text) {
  1308. if (current->type == ITEM_TABLE) {
  1309. return; //can't add anything here
  1310. }
  1311. int pos = 0;
  1312. while (pos < p_text.length()) {
  1313. int end = p_text.find("\n", pos);
  1314. String line;
  1315. bool eol = false;
  1316. if (end == -1) {
  1317. end = p_text.length();
  1318. } else {
  1319. eol = true;
  1320. }
  1321. if (pos == 0 && end == p_text.length()) {
  1322. line = p_text;
  1323. } else {
  1324. line = p_text.substr(pos, end - pos);
  1325. }
  1326. if (line.length() > 0) {
  1327. if (current->subitems.size() && current->subitems.back()->get()->type == ITEM_TEXT) {
  1328. //append text condition!
  1329. ItemText *ti = static_cast<ItemText *>(current->subitems.back()->get());
  1330. ti->text += line;
  1331. _invalidate_current_line(main);
  1332. } else {
  1333. //append item condition
  1334. ItemText *item = memnew(ItemText);
  1335. item->text = line;
  1336. _add_item(item, false);
  1337. }
  1338. }
  1339. if (eol) {
  1340. ItemNewline *item = memnew(ItemNewline);
  1341. item->line = current_frame->lines.size();
  1342. _add_item(item, false);
  1343. current_frame->lines.resize(current_frame->lines.size() + 1);
  1344. if (item->type != ITEM_NEWLINE) {
  1345. current_frame->lines.write[current_frame->lines.size() - 1].from = item;
  1346. }
  1347. _invalidate_current_line(current_frame);
  1348. }
  1349. pos = end + 1;
  1350. }
  1351. }
  1352. void RichTextLabel::_add_item(Item *p_item, bool p_enter, bool p_ensure_newline) {
  1353. p_item->parent = current;
  1354. p_item->E = current->subitems.push_back(p_item);
  1355. p_item->index = current_idx++;
  1356. if (p_enter) {
  1357. current = p_item;
  1358. }
  1359. if (p_ensure_newline) {
  1360. Item *from = current_frame->lines[current_frame->lines.size() - 1].from;
  1361. // only create a new line for Item types that generate content/layout, ignore those that represent formatting/styling
  1362. if (_find_layout_subitem(from, p_item)) {
  1363. _invalidate_current_line(current_frame);
  1364. current_frame->lines.resize(current_frame->lines.size() + 1);
  1365. }
  1366. }
  1367. if (current_frame->lines[current_frame->lines.size() - 1].from == nullptr) {
  1368. current_frame->lines.write[current_frame->lines.size() - 1].from = p_item;
  1369. }
  1370. p_item->line = current_frame->lines.size() - 1;
  1371. _invalidate_current_line(current_frame);
  1372. }
  1373. void RichTextLabel::_remove_item(Item *p_item, const int p_line, const int p_subitem_line) {
  1374. int size = p_item->subitems.size();
  1375. if (size == 0) {
  1376. p_item->parent->subitems.erase(p_item);
  1377. if (p_item->type == ITEM_NEWLINE) {
  1378. current_frame->lines.remove(p_line);
  1379. for (int i = p_subitem_line; i < current->subitems.size(); i++) {
  1380. if (current->subitems[i]->line > 0) {
  1381. current->subitems[i]->line--;
  1382. }
  1383. }
  1384. }
  1385. } else {
  1386. for (int i = 0; i < size; i++) {
  1387. _remove_item(p_item->subitems.front()->get(), p_line, p_subitem_line);
  1388. }
  1389. }
  1390. memdelete(p_item);
  1391. }
  1392. void RichTextLabel::add_image(const Ref<Texture> &p_image, const int p_width, const int p_height) {
  1393. if (current->type == ITEM_TABLE) {
  1394. return;
  1395. }
  1396. ERR_FAIL_COND(p_image.is_null());
  1397. ERR_FAIL_COND(p_image->get_width() == 0);
  1398. ERR_FAIL_COND(p_image->get_height() == 0);
  1399. ItemImage *item = memnew(ItemImage);
  1400. item->image = p_image;
  1401. if (p_width > 0) {
  1402. // custom width
  1403. item->size.width = p_width;
  1404. if (p_height > 0) {
  1405. // custom height
  1406. item->size.height = p_height;
  1407. } else {
  1408. // calculate height to keep aspect ratio
  1409. item->size.height = p_image->get_height() * p_width / p_image->get_width();
  1410. }
  1411. } else {
  1412. if (p_height > 0) {
  1413. // custom height
  1414. item->size.height = p_height;
  1415. // calculate width to keep aspect ratio
  1416. item->size.width = p_image->get_width() * p_height / p_image->get_height();
  1417. } else {
  1418. // keep original width and height
  1419. item->size.height = p_image->get_height();
  1420. item->size.width = p_image->get_width();
  1421. }
  1422. }
  1423. _add_item(item, false);
  1424. }
  1425. void RichTextLabel::add_newline() {
  1426. if (current->type == ITEM_TABLE) {
  1427. return;
  1428. }
  1429. ItemNewline *item = memnew(ItemNewline);
  1430. item->line = current_frame->lines.size();
  1431. _add_item(item, false);
  1432. current_frame->lines.resize(current_frame->lines.size() + 1);
  1433. _invalidate_current_line(current_frame);
  1434. }
  1435. bool RichTextLabel::remove_line(const int p_line) {
  1436. if (p_line >= current_frame->lines.size() || p_line < 0) {
  1437. return false;
  1438. }
  1439. int i = 0;
  1440. while (i < current->subitems.size() && current->subitems[i]->line < p_line) {
  1441. i++;
  1442. }
  1443. bool was_newline = false;
  1444. while (i < current->subitems.size()) {
  1445. was_newline = current->subitems[i]->type == ITEM_NEWLINE;
  1446. _remove_item(current->subitems[i], current->subitems[i]->line, p_line);
  1447. if (was_newline) {
  1448. break;
  1449. }
  1450. }
  1451. if (!was_newline) {
  1452. current_frame->lines.remove(p_line);
  1453. if (current_frame->lines.size() == 0) {
  1454. current_frame->lines.resize(1);
  1455. }
  1456. }
  1457. if (p_line == 0 && current->subitems.size() > 0) {
  1458. main->lines.write[0].from = main;
  1459. }
  1460. main->first_invalid_line = 0;
  1461. return true;
  1462. }
  1463. void RichTextLabel::push_font(const Ref<Font> &p_font) {
  1464. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1465. ERR_FAIL_COND(p_font.is_null());
  1466. ItemFont *item = memnew(ItemFont);
  1467. item->font = p_font;
  1468. _add_item(item, true);
  1469. }
  1470. void RichTextLabel::push_normal() {
  1471. Ref<Font> normal_font = get_font("normal_font");
  1472. ERR_FAIL_COND(normal_font.is_null());
  1473. push_font(normal_font);
  1474. }
  1475. void RichTextLabel::push_bold() {
  1476. Ref<Font> bold_font = get_font("bold_font");
  1477. ERR_FAIL_COND(bold_font.is_null());
  1478. push_font(bold_font);
  1479. }
  1480. void RichTextLabel::push_bold_italics() {
  1481. Ref<Font> bold_italics_font = get_font("bold_italics_font");
  1482. ERR_FAIL_COND(bold_italics_font.is_null());
  1483. push_font(bold_italics_font);
  1484. }
  1485. void RichTextLabel::push_italics() {
  1486. Ref<Font> italics_font = get_font("italics_font");
  1487. ERR_FAIL_COND(italics_font.is_null());
  1488. push_font(italics_font);
  1489. }
  1490. void RichTextLabel::push_mono() {
  1491. Ref<Font> mono_font = get_font("mono_font");
  1492. ERR_FAIL_COND(mono_font.is_null());
  1493. push_font(mono_font);
  1494. }
  1495. void RichTextLabel::push_color(const Color &p_color) {
  1496. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1497. ItemColor *item = memnew(ItemColor);
  1498. item->color = p_color;
  1499. _add_item(item, true);
  1500. }
  1501. void RichTextLabel::push_underline() {
  1502. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1503. ItemUnderline *item = memnew(ItemUnderline);
  1504. _add_item(item, true);
  1505. }
  1506. void RichTextLabel::push_strikethrough() {
  1507. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1508. ItemStrikethrough *item = memnew(ItemStrikethrough);
  1509. _add_item(item, true);
  1510. }
  1511. void RichTextLabel::push_align(Align p_align) {
  1512. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1513. ItemAlign *item = memnew(ItemAlign);
  1514. item->align = p_align;
  1515. _add_item(item, true, true);
  1516. }
  1517. void RichTextLabel::push_indent(int p_level) {
  1518. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1519. ERR_FAIL_COND(p_level < 0);
  1520. ItemIndent *item = memnew(ItemIndent);
  1521. item->level = p_level;
  1522. _add_item(item, true, true);
  1523. }
  1524. void RichTextLabel::push_list(ListType p_list) {
  1525. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1526. ERR_FAIL_INDEX(p_list, 3);
  1527. ItemList *item = memnew(ItemList);
  1528. item->list_type = p_list;
  1529. _add_item(item, true, true);
  1530. }
  1531. void RichTextLabel::push_meta(const Variant &p_meta) {
  1532. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1533. ItemMeta *item = memnew(ItemMeta);
  1534. item->meta = p_meta;
  1535. _add_item(item, true);
  1536. }
  1537. void RichTextLabel::push_table(int p_columns) {
  1538. ERR_FAIL_COND(p_columns < 1);
  1539. ItemTable *item = memnew(ItemTable);
  1540. item->columns.resize(p_columns);
  1541. item->total_width = 0;
  1542. for (int i = 0; i < item->columns.size(); i++) {
  1543. item->columns.write[i].expand = false;
  1544. item->columns.write[i].expand_ratio = 1;
  1545. }
  1546. _add_item(item, true, true);
  1547. }
  1548. void RichTextLabel::push_fade(int p_start_index, int p_length) {
  1549. ItemFade *item = memnew(ItemFade);
  1550. item->starting_index = p_start_index;
  1551. item->length = p_length;
  1552. _add_item(item, true);
  1553. }
  1554. void RichTextLabel::push_shake(int p_strength = 10, float p_rate = 24.0f) {
  1555. ItemShake *item = memnew(ItemShake);
  1556. item->strength = p_strength;
  1557. item->rate = p_rate;
  1558. _add_item(item, true);
  1559. }
  1560. void RichTextLabel::push_wave(float p_frequency = 1.0f, float p_amplitude = 10.0f) {
  1561. ItemWave *item = memnew(ItemWave);
  1562. item->frequency = p_frequency;
  1563. item->amplitude = p_amplitude;
  1564. _add_item(item, true);
  1565. }
  1566. void RichTextLabel::push_tornado(float p_frequency = 1.0f, float p_radius = 10.0f) {
  1567. ItemTornado *item = memnew(ItemTornado);
  1568. item->frequency = p_frequency;
  1569. item->radius = p_radius;
  1570. _add_item(item, true);
  1571. }
  1572. void RichTextLabel::push_rainbow(float p_saturation, float p_value, float p_frequency) {
  1573. ItemRainbow *item = memnew(ItemRainbow);
  1574. item->frequency = p_frequency;
  1575. item->saturation = p_saturation;
  1576. item->value = p_value;
  1577. _add_item(item, true);
  1578. }
  1579. void RichTextLabel::push_customfx(Ref<RichTextEffect> p_custom_effect, Dictionary p_environment) {
  1580. ItemCustomFX *item = memnew(ItemCustomFX);
  1581. item->custom_effect = p_custom_effect;
  1582. item->char_fx_transform->environment = p_environment;
  1583. _add_item(item, true);
  1584. }
  1585. void RichTextLabel::set_table_column_expand(int p_column, bool p_expand, int p_ratio) {
  1586. ERR_FAIL_COND(current->type != ITEM_TABLE);
  1587. ItemTable *table = static_cast<ItemTable *>(current);
  1588. ERR_FAIL_INDEX(p_column, table->columns.size());
  1589. table->columns.write[p_column].expand = p_expand;
  1590. table->columns.write[p_column].expand_ratio = p_ratio;
  1591. }
  1592. void RichTextLabel::push_cell() {
  1593. ERR_FAIL_COND(current->type != ITEM_TABLE);
  1594. ItemFrame *item = memnew(ItemFrame);
  1595. item->parent_frame = current_frame;
  1596. _add_item(item, true);
  1597. current_frame = item;
  1598. item->cell = true;
  1599. item->parent_line = item->parent_frame->lines.size() - 1;
  1600. item->lines.resize(1);
  1601. item->lines.write[0].from = nullptr;
  1602. item->first_invalid_line = 0;
  1603. }
  1604. int RichTextLabel::get_current_table_column() const {
  1605. ERR_FAIL_COND_V(current->type != ITEM_TABLE, -1);
  1606. ItemTable *table = static_cast<ItemTable *>(current);
  1607. return table->subitems.size() % table->columns.size();
  1608. }
  1609. void RichTextLabel::pop() {
  1610. ERR_FAIL_COND(!current->parent);
  1611. if (current->type == ITEM_FRAME) {
  1612. current_frame = static_cast<ItemFrame *>(current)->parent_frame;
  1613. }
  1614. current = current->parent;
  1615. }
  1616. void RichTextLabel::clear() {
  1617. main->_clear_children();
  1618. current = main;
  1619. current_frame = main;
  1620. main->lines.clear();
  1621. main->lines.resize(1);
  1622. main->first_invalid_line = 0;
  1623. update();
  1624. selection.click = nullptr;
  1625. selection.active = false;
  1626. current_idx = 1;
  1627. if (scroll_follow) {
  1628. scroll_following = true;
  1629. }
  1630. }
  1631. void RichTextLabel::set_tab_size(int p_spaces) {
  1632. tab_size = p_spaces;
  1633. main->first_invalid_line = 0;
  1634. update();
  1635. }
  1636. int RichTextLabel::get_tab_size() const {
  1637. return tab_size;
  1638. }
  1639. void RichTextLabel::set_fit_content_height(bool p_enabled) {
  1640. if (p_enabled != fit_content_height) {
  1641. fit_content_height = p_enabled;
  1642. minimum_size_changed();
  1643. }
  1644. }
  1645. bool RichTextLabel::is_fit_content_height_enabled() const {
  1646. return fit_content_height;
  1647. }
  1648. void RichTextLabel::set_meta_underline(bool p_underline) {
  1649. underline_meta = p_underline;
  1650. update();
  1651. }
  1652. bool RichTextLabel::is_meta_underlined() const {
  1653. return underline_meta;
  1654. }
  1655. void RichTextLabel::set_override_selected_font_color(bool p_override_selected_font_color) {
  1656. override_selected_font_color = p_override_selected_font_color;
  1657. }
  1658. bool RichTextLabel::is_overriding_selected_font_color() const {
  1659. return override_selected_font_color;
  1660. }
  1661. void RichTextLabel::set_offset(int p_pixel) {
  1662. vscroll->set_value(p_pixel);
  1663. }
  1664. void RichTextLabel::set_scroll_active(bool p_active) {
  1665. if (scroll_active == p_active) {
  1666. return;
  1667. }
  1668. scroll_active = p_active;
  1669. vscroll->set_drag_node_enabled(p_active);
  1670. update();
  1671. }
  1672. bool RichTextLabel::is_scroll_active() const {
  1673. return scroll_active;
  1674. }
  1675. void RichTextLabel::set_scroll_follow(bool p_follow) {
  1676. scroll_follow = p_follow;
  1677. if (!vscroll->is_visible_in_tree() || vscroll->get_value() >= (vscroll->get_max() - vscroll->get_page())) {
  1678. scroll_following = true;
  1679. }
  1680. }
  1681. bool RichTextLabel::is_scroll_following() const {
  1682. return scroll_follow;
  1683. }
  1684. Error RichTextLabel::parse_bbcode(const String &p_bbcode) {
  1685. clear();
  1686. return append_bbcode(p_bbcode);
  1687. }
  1688. Error RichTextLabel::append_bbcode(const String &p_bbcode) {
  1689. int pos = 0;
  1690. List<String> tag_stack;
  1691. Ref<Font> normal_font = get_font("normal_font");
  1692. Ref<Font> bold_font = get_font("bold_font");
  1693. Ref<Font> italics_font = get_font("italics_font");
  1694. Ref<Font> bold_italics_font = get_font("bold_italics_font");
  1695. Ref<Font> mono_font = get_font("mono_font");
  1696. Color base_color = get_color("default_color");
  1697. int indent_level = 0;
  1698. bool in_bold = false;
  1699. bool in_italics = false;
  1700. set_process_internal(false);
  1701. while (pos < p_bbcode.length()) {
  1702. int brk_pos = p_bbcode.find("[", pos);
  1703. if (brk_pos < 0) {
  1704. brk_pos = p_bbcode.length();
  1705. }
  1706. if (brk_pos > pos) {
  1707. add_text(p_bbcode.substr(pos, brk_pos - pos));
  1708. }
  1709. if (brk_pos == p_bbcode.length()) {
  1710. break; //nothing else to add
  1711. }
  1712. int brk_end = p_bbcode.find("]", brk_pos + 1);
  1713. if (brk_end == -1) {
  1714. //no close, add the rest
  1715. add_text(p_bbcode.substr(brk_pos, p_bbcode.length() - brk_pos));
  1716. break;
  1717. }
  1718. String tag = p_bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
  1719. Vector<String> split_tag_block = tag.split(" ", false);
  1720. String bbcode = !split_tag_block.empty() ? split_tag_block[0] : "";
  1721. if (tag.begins_with("/") && tag_stack.size()) {
  1722. bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length());
  1723. if (tag_stack.front()->get() == "b") {
  1724. in_bold = false;
  1725. }
  1726. if (tag_stack.front()->get() == "i") {
  1727. in_italics = false;
  1728. }
  1729. if (tag_stack.front()->get() == "indent") {
  1730. indent_level--;
  1731. }
  1732. if (!tag_ok) {
  1733. add_text("[" + tag);
  1734. pos = brk_end;
  1735. continue;
  1736. }
  1737. tag_stack.pop_front();
  1738. pos = brk_end + 1;
  1739. if (tag != "/img") {
  1740. pop();
  1741. }
  1742. } else if (tag == "b") {
  1743. //use bold font
  1744. in_bold = true;
  1745. if (in_italics) {
  1746. push_font(bold_italics_font);
  1747. } else {
  1748. push_font(bold_font);
  1749. }
  1750. pos = brk_end + 1;
  1751. tag_stack.push_front(tag);
  1752. } else if (tag == "i") {
  1753. //use italics font
  1754. in_italics = true;
  1755. if (in_bold) {
  1756. push_font(bold_italics_font);
  1757. } else {
  1758. push_font(italics_font);
  1759. }
  1760. pos = brk_end + 1;
  1761. tag_stack.push_front(tag);
  1762. } else if (tag == "code") {
  1763. //use monospace font
  1764. push_font(mono_font);
  1765. pos = brk_end + 1;
  1766. tag_stack.push_front(tag);
  1767. } else if (tag.begins_with("table=")) {
  1768. int columns = tag.substr(6, tag.length()).to_int();
  1769. if (columns < 1) {
  1770. columns = 1;
  1771. }
  1772. push_table(columns);
  1773. pos = brk_end + 1;
  1774. tag_stack.push_front("table");
  1775. } else if (tag == "cell") {
  1776. push_cell();
  1777. pos = brk_end + 1;
  1778. tag_stack.push_front(tag);
  1779. } else if (tag.begins_with("cell=")) {
  1780. int ratio = tag.substr(5, tag.length()).to_int();
  1781. if (ratio < 1) {
  1782. ratio = 1;
  1783. }
  1784. set_table_column_expand(get_current_table_column(), true, ratio);
  1785. push_cell();
  1786. pos = brk_end + 1;
  1787. tag_stack.push_front("cell");
  1788. } else if (tag == "u") {
  1789. //use underline
  1790. push_underline();
  1791. pos = brk_end + 1;
  1792. tag_stack.push_front(tag);
  1793. } else if (tag == "s") {
  1794. //use strikethrough
  1795. push_strikethrough();
  1796. pos = brk_end + 1;
  1797. tag_stack.push_front(tag);
  1798. } else if (tag == "center") {
  1799. push_align(ALIGN_CENTER);
  1800. pos = brk_end + 1;
  1801. tag_stack.push_front(tag);
  1802. } else if (tag == "fill") {
  1803. push_align(ALIGN_FILL);
  1804. pos = brk_end + 1;
  1805. tag_stack.push_front(tag);
  1806. } else if (tag == "right") {
  1807. push_align(ALIGN_RIGHT);
  1808. pos = brk_end + 1;
  1809. tag_stack.push_front(tag);
  1810. } else if (tag == "ul") {
  1811. push_list(LIST_DOTS);
  1812. pos = brk_end + 1;
  1813. tag_stack.push_front(tag);
  1814. } else if (tag == "ol") {
  1815. push_list(LIST_NUMBERS);
  1816. pos = brk_end + 1;
  1817. tag_stack.push_front(tag);
  1818. } else if (tag == "indent") {
  1819. indent_level++;
  1820. push_indent(indent_level);
  1821. pos = brk_end + 1;
  1822. tag_stack.push_front(tag);
  1823. } else if (tag == "url") {
  1824. int end = p_bbcode.find("[", brk_end);
  1825. if (end == -1) {
  1826. end = p_bbcode.length();
  1827. }
  1828. String url = p_bbcode.substr(brk_end + 1, end - brk_end - 1);
  1829. push_meta(url);
  1830. pos = brk_end + 1;
  1831. tag_stack.push_front(tag);
  1832. } else if (tag.begins_with("url=")) {
  1833. String url = tag.substr(4, tag.length());
  1834. push_meta(url);
  1835. pos = brk_end + 1;
  1836. tag_stack.push_front("url");
  1837. } else if (tag == "img") {
  1838. int end = p_bbcode.find("[", brk_end);
  1839. if (end == -1) {
  1840. end = p_bbcode.length();
  1841. }
  1842. String image = p_bbcode.substr(brk_end + 1, end - brk_end - 1);
  1843. Ref<Texture> texture = ResourceLoader::load(image, "Texture");
  1844. if (texture.is_valid()) {
  1845. add_image(texture);
  1846. }
  1847. pos = end;
  1848. tag_stack.push_front(tag);
  1849. } else if (tag.begins_with("img=")) {
  1850. int width = 0;
  1851. int height = 0;
  1852. String params = tag.substr(4, tag.length());
  1853. int sep = params.find("x");
  1854. if (sep == -1) {
  1855. width = params.to_int();
  1856. } else {
  1857. width = params.substr(0, sep).to_int();
  1858. height = params.substr(sep + 1, params.length()).to_int();
  1859. }
  1860. int end = p_bbcode.find("[", brk_end);
  1861. if (end == -1) {
  1862. end = p_bbcode.length();
  1863. }
  1864. String image = p_bbcode.substr(brk_end + 1, end - brk_end - 1);
  1865. Ref<Texture> texture = ResourceLoader::load(image, "Texture");
  1866. if (texture.is_valid()) {
  1867. add_image(texture, width, height);
  1868. }
  1869. pos = end;
  1870. tag_stack.push_front("img");
  1871. } else if (tag.begins_with("color=")) {
  1872. String col = tag.substr(6, tag.length());
  1873. Color color;
  1874. if (col.begins_with("#")) {
  1875. color = Color::html(col);
  1876. } else if (col == "aqua") {
  1877. color = Color(0, 1, 1);
  1878. } else if (col == "black") {
  1879. color = Color(0, 0, 0);
  1880. } else if (col == "blue") {
  1881. color = Color(0, 0, 1);
  1882. } else if (col == "fuchsia") {
  1883. color = Color(1, 0, 1);
  1884. } else if (col == "gray" || col == "grey") {
  1885. color = Color(0.5, 0.5, 0.5);
  1886. } else if (col == "green") {
  1887. color = Color(0, 0.5, 0);
  1888. } else if (col == "lime") {
  1889. color = Color(0, 1, 0);
  1890. } else if (col == "maroon") {
  1891. color = Color(0.5, 0, 0);
  1892. } else if (col == "navy") {
  1893. color = Color(0, 0, 0.5);
  1894. } else if (col == "olive") {
  1895. color = Color(0.5, 0.5, 0);
  1896. } else if (col == "purple") {
  1897. color = Color(0.5, 0, 0.5);
  1898. } else if (col == "red") {
  1899. color = Color(1, 0, 0);
  1900. } else if (col == "silver") {
  1901. color = Color(0.75, 0.75, 0.75);
  1902. } else if (col == "teal") {
  1903. color = Color(0, 0.5, 0.5);
  1904. } else if (col == "white") {
  1905. color = Color(1, 1, 1);
  1906. } else if (col == "yellow") {
  1907. color = Color(1, 1, 0);
  1908. } else {
  1909. color = base_color;
  1910. }
  1911. push_color(color);
  1912. pos = brk_end + 1;
  1913. tag_stack.push_front("color");
  1914. } else if (tag.begins_with("font=")) {
  1915. String fnt = tag.substr(5, tag.length());
  1916. Ref<Font> font = ResourceLoader::load(fnt, "Font");
  1917. if (font.is_valid()) {
  1918. push_font(font);
  1919. } else {
  1920. push_font(normal_font);
  1921. }
  1922. pos = brk_end + 1;
  1923. tag_stack.push_front("font");
  1924. } else if (bbcode == "fade") {
  1925. int startIndex = 0;
  1926. int length = 10;
  1927. if (split_tag_block.size() > 1) {
  1928. split_tag_block.remove(0);
  1929. for (int i = 0; i < split_tag_block.size(); i++) {
  1930. String expr = split_tag_block[i];
  1931. if (expr.begins_with("start=")) {
  1932. String start_str = expr.substr(6, expr.length());
  1933. startIndex = start_str.to_int();
  1934. } else if (expr.begins_with("length=")) {
  1935. String end_str = expr.substr(7, expr.length());
  1936. length = end_str.to_int();
  1937. }
  1938. }
  1939. }
  1940. push_fade(startIndex, length);
  1941. pos = brk_end + 1;
  1942. tag_stack.push_front("fade");
  1943. } else if (bbcode == "shake") {
  1944. int strength = 5;
  1945. float rate = 20.0f;
  1946. if (split_tag_block.size() > 1) {
  1947. split_tag_block.remove(0);
  1948. for (int i = 0; i < split_tag_block.size(); i++) {
  1949. String expr = split_tag_block[i];
  1950. if (expr.begins_with("level=")) {
  1951. String str_str = expr.substr(6, expr.length());
  1952. strength = str_str.to_int();
  1953. } else if (expr.begins_with("rate=")) {
  1954. String rate_str = expr.substr(5, expr.length());
  1955. rate = rate_str.to_float();
  1956. }
  1957. }
  1958. }
  1959. push_shake(strength, rate);
  1960. pos = brk_end + 1;
  1961. tag_stack.push_front("shake");
  1962. set_process_internal(true);
  1963. } else if (bbcode == "wave") {
  1964. float amplitude = 20.0f;
  1965. float period = 5.0f;
  1966. if (split_tag_block.size() > 1) {
  1967. split_tag_block.remove(0);
  1968. for (int i = 0; i < split_tag_block.size(); i++) {
  1969. String expr = split_tag_block[i];
  1970. if (expr.begins_with("amp=")) {
  1971. String amp_str = expr.substr(4, expr.length());
  1972. amplitude = amp_str.to_float();
  1973. } else if (expr.begins_with("freq=")) {
  1974. String period_str = expr.substr(5, expr.length());
  1975. period = period_str.to_float();
  1976. }
  1977. }
  1978. }
  1979. push_wave(period, amplitude);
  1980. pos = brk_end + 1;
  1981. tag_stack.push_front("wave");
  1982. set_process_internal(true);
  1983. } else if (bbcode == "tornado") {
  1984. float radius = 10.0f;
  1985. float frequency = 1.0f;
  1986. if (split_tag_block.size() > 1) {
  1987. split_tag_block.remove(0);
  1988. for (int i = 0; i < split_tag_block.size(); i++) {
  1989. String expr = split_tag_block[i];
  1990. if (expr.begins_with("radius=")) {
  1991. String amp_str = expr.substr(7, expr.length());
  1992. radius = amp_str.to_float();
  1993. } else if (expr.begins_with("freq=")) {
  1994. String period_str = expr.substr(5, expr.length());
  1995. frequency = period_str.to_float();
  1996. }
  1997. }
  1998. }
  1999. push_tornado(frequency, radius);
  2000. pos = brk_end + 1;
  2001. tag_stack.push_front("tornado");
  2002. set_process_internal(true);
  2003. } else if (bbcode == "rainbow") {
  2004. float saturation = 0.8f;
  2005. float value = 0.8f;
  2006. float frequency = 1.0f;
  2007. if (split_tag_block.size() > 1) {
  2008. split_tag_block.remove(0);
  2009. for (int i = 0; i < split_tag_block.size(); i++) {
  2010. String expr = split_tag_block[i];
  2011. if (expr.begins_with("sat=")) {
  2012. String sat_str = expr.substr(4, expr.length());
  2013. saturation = sat_str.to_float();
  2014. } else if (expr.begins_with("val=")) {
  2015. String val_str = expr.substr(4, expr.length());
  2016. value = val_str.to_float();
  2017. } else if (expr.begins_with("freq=")) {
  2018. String freq_str = expr.substr(5, expr.length());
  2019. frequency = freq_str.to_float();
  2020. }
  2021. }
  2022. }
  2023. push_rainbow(saturation, value, frequency);
  2024. pos = brk_end + 1;
  2025. tag_stack.push_front("rainbow");
  2026. set_process_internal(true);
  2027. } else {
  2028. Vector<String> &expr = split_tag_block;
  2029. if (expr.size() < 1) {
  2030. add_text("[");
  2031. pos = brk_pos + 1;
  2032. } else {
  2033. String identifier = expr[0];
  2034. expr.remove(0);
  2035. Dictionary properties = parse_expressions_for_values(expr);
  2036. Ref<RichTextEffect> effect = _get_custom_effect_by_code(identifier);
  2037. if (!effect.is_null()) {
  2038. push_customfx(effect, properties);
  2039. pos = brk_end + 1;
  2040. tag_stack.push_front(identifier);
  2041. set_process_internal(true);
  2042. } else {
  2043. add_text("["); //ignore
  2044. pos = brk_pos + 1;
  2045. }
  2046. }
  2047. }
  2048. }
  2049. Vector<ItemFX *> fx_items;
  2050. for (List<Item *>::Element *E = main->subitems.front(); E; E = E->next()) {
  2051. Item *subitem = static_cast<Item *>(E->get());
  2052. _fetch_item_fx_stack(subitem, fx_items);
  2053. if (fx_items.size()) {
  2054. set_process_internal(true);
  2055. break;
  2056. }
  2057. }
  2058. return OK;
  2059. }
  2060. void RichTextLabel::scroll_to_line(int p_line) {
  2061. ERR_FAIL_INDEX(p_line, main->lines.size());
  2062. _validate_line_caches(main);
  2063. vscroll->set_value(main->lines[p_line].height_accum_cache - main->lines[p_line].height_cache);
  2064. }
  2065. int RichTextLabel::get_line_count() const {
  2066. return current_frame->lines.size();
  2067. }
  2068. int RichTextLabel::get_visible_line_count() const {
  2069. if (!is_visible()) {
  2070. return 0;
  2071. }
  2072. return visible_line_count;
  2073. }
  2074. void RichTextLabel::set_selection_enabled(bool p_enabled) {
  2075. selection.enabled = p_enabled;
  2076. if (!p_enabled) {
  2077. if (selection.active) {
  2078. selection.active = false;
  2079. update();
  2080. }
  2081. set_focus_mode(FOCUS_NONE);
  2082. } else {
  2083. set_focus_mode(FOCUS_ALL);
  2084. }
  2085. }
  2086. bool RichTextLabel::search(const String &p_string, bool p_from_selection, bool p_search_previous) {
  2087. ERR_FAIL_COND_V(!selection.enabled, false);
  2088. Item *it = main;
  2089. int charidx = 0;
  2090. if (p_from_selection && selection.active) {
  2091. it = selection.to;
  2092. charidx = p_search_previous ? selection.from_char - 1 : selection.to_char + 1;
  2093. }
  2094. if (charidx == -1) {
  2095. // At beginning of line and searching backwards
  2096. it = _get_prev_item(it, true);
  2097. }
  2098. while (it) {
  2099. if (it->type == ITEM_TEXT) {
  2100. ItemText *t = static_cast<ItemText *>(it);
  2101. int sp = p_search_previous ? t->text.rfindn(p_string, charidx) : t->text.findn(p_string, charidx);
  2102. if (sp != -1) {
  2103. selection.from = it;
  2104. selection.from_char = sp;
  2105. selection.to = it;
  2106. selection.to_char = sp + p_string.length() - 1;
  2107. selection.active = true;
  2108. update();
  2109. _validate_line_caches(main);
  2110. int fh = _find_font(t).is_valid() ? _find_font(t)->get_height() : get_font("normal_font")->get_height();
  2111. float offset = 0;
  2112. int line = t->line;
  2113. Item *item = t;
  2114. while (item) {
  2115. if (item->type == ITEM_FRAME) {
  2116. ItemFrame *frame = static_cast<ItemFrame *>(item);
  2117. if (line >= 0 && line < frame->lines.size()) {
  2118. offset += frame->lines[line].height_accum_cache - frame->lines[line].height_cache;
  2119. line = frame->line;
  2120. }
  2121. }
  2122. item = item->parent;
  2123. }
  2124. vscroll->set_value(offset - fh);
  2125. return true;
  2126. }
  2127. }
  2128. if (p_search_previous) {
  2129. it = _get_prev_item(it, true);
  2130. charidx = -1;
  2131. } else {
  2132. it = _get_next_item(it, true);
  2133. charidx = 0;
  2134. }
  2135. }
  2136. return false;
  2137. }
  2138. String RichTextLabel::get_selected_text() {
  2139. if (!selection.active || !selection.enabled) {
  2140. return "";
  2141. }
  2142. String text;
  2143. RichTextLabel::Item *item = selection.from;
  2144. while (item) {
  2145. if (item->type == ITEM_TEXT) {
  2146. String itext = static_cast<ItemText *>(item)->text;
  2147. if (item == selection.from && item == selection.to) {
  2148. text += itext.substr(selection.from_char, selection.to_char - selection.from_char + 1);
  2149. } else if (item == selection.from) {
  2150. text += itext.substr(selection.from_char, itext.size());
  2151. } else if (item == selection.to) {
  2152. text += itext.substr(0, selection.to_char + 1);
  2153. } else {
  2154. text += itext;
  2155. }
  2156. } else if (item->type == ITEM_NEWLINE) {
  2157. text += "\n";
  2158. }
  2159. if (item == selection.to) {
  2160. break;
  2161. }
  2162. item = _get_next_item(item, true);
  2163. }
  2164. return text;
  2165. }
  2166. void RichTextLabel::selection_copy() {
  2167. String text = get_selected_text();
  2168. if (text != "") {
  2169. OS::get_singleton()->set_clipboard(text);
  2170. }
  2171. }
  2172. bool RichTextLabel::is_selection_enabled() const {
  2173. return selection.enabled;
  2174. }
  2175. void RichTextLabel::set_bbcode(const String &p_bbcode) {
  2176. bbcode = p_bbcode;
  2177. if (is_inside_tree() && use_bbcode) {
  2178. parse_bbcode(p_bbcode);
  2179. } else { // raw text
  2180. clear();
  2181. add_text(p_bbcode);
  2182. }
  2183. }
  2184. String RichTextLabel::get_bbcode() const {
  2185. return bbcode;
  2186. }
  2187. void RichTextLabel::set_use_bbcode(bool p_enable) {
  2188. if (use_bbcode == p_enable) {
  2189. return;
  2190. }
  2191. use_bbcode = p_enable;
  2192. set_bbcode(bbcode);
  2193. }
  2194. bool RichTextLabel::is_using_bbcode() const {
  2195. return use_bbcode;
  2196. }
  2197. String RichTextLabel::get_text() {
  2198. String text = "";
  2199. Item *it = main;
  2200. while (it) {
  2201. if (it->type == ITEM_TEXT) {
  2202. ItemText *t = static_cast<ItemText *>(it);
  2203. text += t->text;
  2204. } else if (it->type == ITEM_NEWLINE) {
  2205. text += "\n";
  2206. } else if (it->type == ITEM_INDENT) {
  2207. text += "\t";
  2208. }
  2209. it = _get_next_item(it, true);
  2210. }
  2211. return text;
  2212. }
  2213. void RichTextLabel::set_text(const String &p_string) {
  2214. clear();
  2215. add_text(p_string);
  2216. }
  2217. void RichTextLabel::set_percent_visible(float p_percent) {
  2218. if (p_percent < 0 || p_percent >= 1) {
  2219. visible_characters = -1;
  2220. percent_visible = 1;
  2221. } else {
  2222. visible_characters = get_total_character_count() * p_percent;
  2223. percent_visible = p_percent;
  2224. }
  2225. _change_notify("visible_characters");
  2226. update();
  2227. }
  2228. float RichTextLabel::get_percent_visible() const {
  2229. return percent_visible;
  2230. }
  2231. void RichTextLabel::set_effects(const Vector<Variant> &effects) {
  2232. custom_effects.clear();
  2233. for (int i = 0; i < effects.size(); i++) {
  2234. Ref<RichTextEffect> effect = Ref<RichTextEffect>(effects[i]);
  2235. custom_effects.push_back(effect);
  2236. }
  2237. if ((bbcode != "") && use_bbcode) {
  2238. parse_bbcode(bbcode);
  2239. }
  2240. }
  2241. Vector<Variant> RichTextLabel::get_effects() {
  2242. Vector<Variant> r;
  2243. for (int i = 0; i < custom_effects.size(); i++) {
  2244. r.push_back(custom_effects[i].get_ref_ptr());
  2245. }
  2246. return r;
  2247. }
  2248. void RichTextLabel::install_effect(const Variant effect) {
  2249. Ref<RichTextEffect> rteffect;
  2250. rteffect = effect;
  2251. if (rteffect.is_valid()) {
  2252. custom_effects.push_back(effect);
  2253. if ((bbcode != "") && use_bbcode) {
  2254. parse_bbcode(bbcode);
  2255. }
  2256. }
  2257. }
  2258. int RichTextLabel::get_content_height() const {
  2259. int total_height = 0;
  2260. if (main->lines.size()) {
  2261. total_height = main->lines[main->lines.size() - 1].height_accum_cache + get_stylebox("normal")->get_minimum_size().height;
  2262. }
  2263. return total_height;
  2264. }
  2265. void RichTextLabel::_bind_methods() {
  2266. ClassDB::bind_method(D_METHOD("_gui_input"), &RichTextLabel::_gui_input);
  2267. ClassDB::bind_method(D_METHOD("_scroll_changed"), &RichTextLabel::_scroll_changed);
  2268. ClassDB::bind_method(D_METHOD("get_text"), &RichTextLabel::get_text);
  2269. ClassDB::bind_method(D_METHOD("add_text", "text"), &RichTextLabel::add_text);
  2270. ClassDB::bind_method(D_METHOD("set_text", "text"), &RichTextLabel::set_text);
  2271. ClassDB::bind_method(D_METHOD("add_image", "image", "width", "height"), &RichTextLabel::add_image, DEFVAL(0), DEFVAL(0));
  2272. ClassDB::bind_method(D_METHOD("newline"), &RichTextLabel::add_newline);
  2273. ClassDB::bind_method(D_METHOD("remove_line", "line"), &RichTextLabel::remove_line);
  2274. ClassDB::bind_method(D_METHOD("push_font", "font"), &RichTextLabel::push_font);
  2275. ClassDB::bind_method(D_METHOD("push_normal"), &RichTextLabel::push_normal);
  2276. ClassDB::bind_method(D_METHOD("push_bold"), &RichTextLabel::push_bold);
  2277. ClassDB::bind_method(D_METHOD("push_bold_italics"), &RichTextLabel::push_bold_italics);
  2278. ClassDB::bind_method(D_METHOD("push_italics"), &RichTextLabel::push_italics);
  2279. ClassDB::bind_method(D_METHOD("push_mono"), &RichTextLabel::push_mono);
  2280. ClassDB::bind_method(D_METHOD("push_color", "color"), &RichTextLabel::push_color);
  2281. ClassDB::bind_method(D_METHOD("push_align", "align"), &RichTextLabel::push_align);
  2282. ClassDB::bind_method(D_METHOD("push_indent", "level"), &RichTextLabel::push_indent);
  2283. ClassDB::bind_method(D_METHOD("push_list", "type"), &RichTextLabel::push_list);
  2284. ClassDB::bind_method(D_METHOD("push_meta", "data"), &RichTextLabel::push_meta);
  2285. ClassDB::bind_method(D_METHOD("push_underline"), &RichTextLabel::push_underline);
  2286. ClassDB::bind_method(D_METHOD("push_strikethrough"), &RichTextLabel::push_strikethrough);
  2287. ClassDB::bind_method(D_METHOD("push_table", "columns"), &RichTextLabel::push_table);
  2288. ClassDB::bind_method(D_METHOD("set_table_column_expand", "column", "expand", "ratio"), &RichTextLabel::set_table_column_expand);
  2289. ClassDB::bind_method(D_METHOD("push_cell"), &RichTextLabel::push_cell);
  2290. ClassDB::bind_method(D_METHOD("pop"), &RichTextLabel::pop);
  2291. ClassDB::bind_method(D_METHOD("clear"), &RichTextLabel::clear);
  2292. ClassDB::bind_method(D_METHOD("set_meta_underline", "enable"), &RichTextLabel::set_meta_underline);
  2293. ClassDB::bind_method(D_METHOD("is_meta_underlined"), &RichTextLabel::is_meta_underlined);
  2294. ClassDB::bind_method(D_METHOD("set_override_selected_font_color", "override"), &RichTextLabel::set_override_selected_font_color);
  2295. ClassDB::bind_method(D_METHOD("is_overriding_selected_font_color"), &RichTextLabel::is_overriding_selected_font_color);
  2296. ClassDB::bind_method(D_METHOD("set_scroll_active", "active"), &RichTextLabel::set_scroll_active);
  2297. ClassDB::bind_method(D_METHOD("is_scroll_active"), &RichTextLabel::is_scroll_active);
  2298. ClassDB::bind_method(D_METHOD("set_scroll_follow", "follow"), &RichTextLabel::set_scroll_follow);
  2299. ClassDB::bind_method(D_METHOD("is_scroll_following"), &RichTextLabel::is_scroll_following);
  2300. ClassDB::bind_method(D_METHOD("get_v_scroll"), &RichTextLabel::get_v_scroll);
  2301. ClassDB::bind_method(D_METHOD("scroll_to_line", "line"), &RichTextLabel::scroll_to_line);
  2302. ClassDB::bind_method(D_METHOD("set_tab_size", "spaces"), &RichTextLabel::set_tab_size);
  2303. ClassDB::bind_method(D_METHOD("get_tab_size"), &RichTextLabel::get_tab_size);
  2304. ClassDB::bind_method(D_METHOD("set_fit_content_height", "enabled"), &RichTextLabel::set_fit_content_height);
  2305. ClassDB::bind_method(D_METHOD("is_fit_content_height_enabled"), &RichTextLabel::is_fit_content_height_enabled);
  2306. ClassDB::bind_method(D_METHOD("set_selection_enabled", "enabled"), &RichTextLabel::set_selection_enabled);
  2307. ClassDB::bind_method(D_METHOD("is_selection_enabled"), &RichTextLabel::is_selection_enabled);
  2308. ClassDB::bind_method(D_METHOD("parse_bbcode", "bbcode"), &RichTextLabel::parse_bbcode);
  2309. ClassDB::bind_method(D_METHOD("append_bbcode", "bbcode"), &RichTextLabel::append_bbcode);
  2310. ClassDB::bind_method(D_METHOD("set_bbcode", "text"), &RichTextLabel::set_bbcode);
  2311. ClassDB::bind_method(D_METHOD("get_bbcode"), &RichTextLabel::get_bbcode);
  2312. ClassDB::bind_method(D_METHOD("set_visible_characters", "amount"), &RichTextLabel::set_visible_characters);
  2313. ClassDB::bind_method(D_METHOD("get_visible_characters"), &RichTextLabel::get_visible_characters);
  2314. ClassDB::bind_method(D_METHOD("set_percent_visible", "percent_visible"), &RichTextLabel::set_percent_visible);
  2315. ClassDB::bind_method(D_METHOD("get_percent_visible"), &RichTextLabel::get_percent_visible);
  2316. ClassDB::bind_method(D_METHOD("get_total_character_count"), &RichTextLabel::get_total_character_count);
  2317. ClassDB::bind_method(D_METHOD("set_use_bbcode", "enable"), &RichTextLabel::set_use_bbcode);
  2318. ClassDB::bind_method(D_METHOD("is_using_bbcode"), &RichTextLabel::is_using_bbcode);
  2319. ClassDB::bind_method(D_METHOD("get_line_count"), &RichTextLabel::get_line_count);
  2320. ClassDB::bind_method(D_METHOD("get_visible_line_count"), &RichTextLabel::get_visible_line_count);
  2321. ClassDB::bind_method(D_METHOD("get_content_height"), &RichTextLabel::get_content_height);
  2322. ClassDB::bind_method(D_METHOD("parse_expressions_for_values", "expressions"), &RichTextLabel::parse_expressions_for_values);
  2323. ClassDB::bind_method(D_METHOD("set_effects", "effects"), &RichTextLabel::set_effects);
  2324. ClassDB::bind_method(D_METHOD("get_effects"), &RichTextLabel::get_effects);
  2325. ClassDB::bind_method(D_METHOD("install_effect", "effect"), &RichTextLabel::install_effect);
  2326. ADD_GROUP("BBCode", "bbcode_");
  2327. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bbcode_enabled"), "set_use_bbcode", "is_using_bbcode");
  2328. ADD_PROPERTY(PropertyInfo(Variant::STRING, "bbcode_text", PROPERTY_HINT_MULTILINE_TEXT), "set_bbcode", "get_bbcode");
  2329. ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_characters", PROPERTY_HINT_RANGE, "-1,128000,1"), "set_visible_characters", "get_visible_characters");
  2330. ADD_PROPERTY(PropertyInfo(Variant::REAL, "percent_visible", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_percent_visible", "get_percent_visible");
  2331. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meta_underlined"), "set_meta_underline", "is_meta_underlined");
  2332. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_size", PROPERTY_HINT_RANGE, "0,24,1"), "set_tab_size", "get_tab_size");
  2333. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  2334. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_content_height"), "set_fit_content_height", "is_fit_content_height_enabled");
  2335. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_active"), "set_scroll_active", "is_scroll_active");
  2336. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_following"), "set_scroll_follow", "is_scroll_following");
  2337. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selection_enabled"), "set_selection_enabled", "is_selection_enabled");
  2338. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_selected_font_color"), "set_override_selected_font_color", "is_overriding_selected_font_color");
  2339. 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");
  2340. ADD_SIGNAL(MethodInfo("meta_clicked", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  2341. ADD_SIGNAL(MethodInfo("meta_hover_started", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  2342. ADD_SIGNAL(MethodInfo("meta_hover_ended", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  2343. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  2344. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  2345. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  2346. BIND_ENUM_CONSTANT(ALIGN_FILL);
  2347. BIND_ENUM_CONSTANT(LIST_NUMBERS);
  2348. BIND_ENUM_CONSTANT(LIST_LETTERS);
  2349. BIND_ENUM_CONSTANT(LIST_DOTS);
  2350. BIND_ENUM_CONSTANT(ITEM_FRAME);
  2351. BIND_ENUM_CONSTANT(ITEM_TEXT);
  2352. BIND_ENUM_CONSTANT(ITEM_IMAGE);
  2353. BIND_ENUM_CONSTANT(ITEM_NEWLINE);
  2354. BIND_ENUM_CONSTANT(ITEM_FONT);
  2355. BIND_ENUM_CONSTANT(ITEM_COLOR);
  2356. BIND_ENUM_CONSTANT(ITEM_UNDERLINE);
  2357. BIND_ENUM_CONSTANT(ITEM_STRIKETHROUGH);
  2358. BIND_ENUM_CONSTANT(ITEM_ALIGN);
  2359. BIND_ENUM_CONSTANT(ITEM_INDENT);
  2360. BIND_ENUM_CONSTANT(ITEM_LIST);
  2361. BIND_ENUM_CONSTANT(ITEM_TABLE);
  2362. BIND_ENUM_CONSTANT(ITEM_FADE);
  2363. BIND_ENUM_CONSTANT(ITEM_SHAKE);
  2364. BIND_ENUM_CONSTANT(ITEM_WAVE);
  2365. BIND_ENUM_CONSTANT(ITEM_TORNADO);
  2366. BIND_ENUM_CONSTANT(ITEM_RAINBOW);
  2367. BIND_ENUM_CONSTANT(ITEM_CUSTOMFX);
  2368. BIND_ENUM_CONSTANT(ITEM_META);
  2369. }
  2370. void RichTextLabel::set_visible_characters(int p_visible) {
  2371. visible_characters = p_visible;
  2372. if (p_visible == -1) {
  2373. percent_visible = 1;
  2374. } else {
  2375. int total_char_count = get_total_character_count();
  2376. if (total_char_count > 0) {
  2377. percent_visible = (float)p_visible / (float)total_char_count;
  2378. }
  2379. }
  2380. _change_notify("percent_visible");
  2381. update();
  2382. }
  2383. int RichTextLabel::get_visible_characters() const {
  2384. return visible_characters;
  2385. }
  2386. int RichTextLabel::get_total_character_count() const {
  2387. int tc = 0;
  2388. for (int i = 0; i < current_frame->lines.size(); i++) {
  2389. tc += current_frame->lines[i].char_count;
  2390. }
  2391. return tc;
  2392. }
  2393. void RichTextLabel::set_fixed_size_to_width(int p_width) {
  2394. fixed_width = p_width;
  2395. minimum_size_changed();
  2396. }
  2397. Size2 RichTextLabel::get_minimum_size() const {
  2398. Size2 size(0, 0);
  2399. if (fixed_width != -1) {
  2400. size.x = fixed_width;
  2401. }
  2402. if (fixed_width != -1 || fit_content_height) {
  2403. const_cast<RichTextLabel *>(this)->_validate_line_caches(main);
  2404. size.y = get_content_height();
  2405. }
  2406. return size;
  2407. }
  2408. Ref<RichTextEffect> RichTextLabel::_get_custom_effect_by_code(String p_bbcode_identifier) {
  2409. for (int i = 0; i < custom_effects.size(); i++) {
  2410. if (!custom_effects[i].is_valid()) {
  2411. continue;
  2412. }
  2413. if (custom_effects[i]->get_bbcode() == p_bbcode_identifier) {
  2414. return custom_effects[i];
  2415. }
  2416. }
  2417. return Ref<RichTextEffect>();
  2418. }
  2419. Dictionary RichTextLabel::parse_expressions_for_values(Vector<String> p_expressions) {
  2420. Dictionary d = Dictionary();
  2421. for (int i = 0; i < p_expressions.size(); i++) {
  2422. String expression = p_expressions[i];
  2423. Array a = Array();
  2424. Vector<String> parts = expression.split("=", true);
  2425. String key = parts[0];
  2426. if (parts.size() != 2) {
  2427. return d;
  2428. }
  2429. Vector<String> values = parts[1].split(",", false);
  2430. #ifdef MODULE_REGEX_ENABLED
  2431. RegEx color;
  2432. color.compile("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
  2433. RegEx nodepath;
  2434. nodepath.compile("^\\$");
  2435. RegEx boolean;
  2436. boolean.compile("^(true|false)$");
  2437. RegEx decimal;
  2438. decimal.compile("^-?^.?\\d+(\\.\\d+?)?$");
  2439. RegEx numerical;
  2440. numerical.compile("^\\d+$");
  2441. for (int j = 0; j < values.size(); j++) {
  2442. if (!color.search(values[j]).is_null()) {
  2443. a.append(Color::html(values[j]));
  2444. } else if (!nodepath.search(values[j]).is_null()) {
  2445. if (values[j].begins_with("$")) {
  2446. String v = values[j].substr(1, values[j].length());
  2447. a.append(NodePath(v));
  2448. }
  2449. } else if (!boolean.search(values[j]).is_null()) {
  2450. if (values[j] == "true") {
  2451. a.append(true);
  2452. } else if (values[j] == "false") {
  2453. a.append(false);
  2454. }
  2455. } else if (!decimal.search(values[j]).is_null()) {
  2456. a.append(values[j].to_double());
  2457. } else if (!numerical.search(values[j]).is_null()) {
  2458. a.append(values[j].to_int());
  2459. } else {
  2460. a.append(values[j]);
  2461. }
  2462. }
  2463. #endif
  2464. if (values.size() > 1) {
  2465. d[key] = a;
  2466. } else if (values.size() == 1) {
  2467. d[key] = a[0];
  2468. }
  2469. }
  2470. return d;
  2471. }
  2472. RichTextLabel::RichTextLabel() {
  2473. main = memnew(ItemFrame);
  2474. main->index = 0;
  2475. current = main;
  2476. main->lines.resize(1);
  2477. main->lines.write[0].from = main;
  2478. main->first_invalid_line = 0;
  2479. current_frame = main;
  2480. tab_size = 4;
  2481. default_align = ALIGN_LEFT;
  2482. underline_meta = true;
  2483. meta_hovering = nullptr;
  2484. override_selected_font_color = false;
  2485. scroll_visible = false;
  2486. scroll_follow = false;
  2487. scroll_following = false;
  2488. updating_scroll = false;
  2489. scroll_active = true;
  2490. scroll_w = 0;
  2491. scroll_updated = false;
  2492. vscroll = memnew(VScrollBar);
  2493. add_child(vscroll);
  2494. vscroll->set_drag_node(String(".."));
  2495. vscroll->set_step(1);
  2496. vscroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0);
  2497. vscroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  2498. vscroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  2499. vscroll->connect("value_changed", this, "_scroll_changed");
  2500. vscroll->set_step(1);
  2501. vscroll->hide();
  2502. current_idx = 1;
  2503. use_bbcode = false;
  2504. selection.click = nullptr;
  2505. selection.active = false;
  2506. selection.enabled = false;
  2507. visible_characters = -1;
  2508. percent_visible = 1;
  2509. visible_line_count = 0;
  2510. fixed_width = -1;
  2511. fit_content_height = false;
  2512. set_clip_contents(true);
  2513. }
  2514. RichTextLabel::~RichTextLabel() {
  2515. memdelete(main);
  2516. }