rich_text_label.cpp 94 KB

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