rich_text_label.cpp 103 KB

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