rich_text_label.cpp 91 KB

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