rich_text_label.cpp 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/os/keyboard.h"
  32. #include "core/os/os.h"
  33. #include "scene/scene_string_names.h"
  34. #ifdef TOOLS_ENABLED
  35. #include "editor/editor_scale.h"
  36. #endif
  37. RichTextLabel::Item *RichTextLabel::_get_next_item(Item *p_item, bool p_free) {
  38. if (p_free) {
  39. if (p_item->subitems.size()) {
  40. return p_item->subitems.front()->get();
  41. } else if (!p_item->parent) {
  42. return NULL;
  43. } else if (p_item->E->next()) {
  44. return p_item->E->next()->get();
  45. } else {
  46. //go up until something with a next is found
  47. while (p_item->parent && !p_item->E->next()) {
  48. p_item = p_item->parent;
  49. }
  50. if (p_item->parent)
  51. return p_item->E->next()->get();
  52. else
  53. return NULL;
  54. }
  55. } else {
  56. if (p_item->subitems.size() && p_item->type != ITEM_TABLE) {
  57. return p_item->subitems.front()->get();
  58. } else if (p_item->type == ITEM_FRAME) {
  59. return NULL;
  60. } else if (p_item->E->next()) {
  61. return p_item->E->next()->get();
  62. } else {
  63. //go up until something with a next is found
  64. while (p_item->type != ITEM_FRAME && !p_item->E->next()) {
  65. p_item = p_item->parent;
  66. }
  67. if (p_item->type != ITEM_FRAME)
  68. return p_item->E->next()->get();
  69. else
  70. return NULL;
  71. }
  72. }
  73. return NULL;
  74. }
  75. RichTextLabel::Item *RichTextLabel::_get_prev_item(Item *p_item, bool p_free) {
  76. if (p_free) {
  77. if (p_item->subitems.size()) {
  78. return p_item->subitems.back()->get();
  79. } else if (!p_item->parent) {
  80. return NULL;
  81. } else if (p_item->E->prev()) {
  82. return p_item->E->prev()->get();
  83. } else {
  84. //go back until something with a prev is found
  85. while (p_item->parent && !p_item->E->prev()) {
  86. p_item = p_item->parent;
  87. }
  88. if (p_item->parent)
  89. return p_item->E->prev()->get();
  90. else
  91. return NULL;
  92. }
  93. } else {
  94. if (p_item->subitems.size() && p_item->type != ITEM_TABLE) {
  95. return p_item->subitems.back()->get();
  96. } else if (p_item->type == ITEM_FRAME) {
  97. return NULL;
  98. } else if (p_item->E->prev()) {
  99. return p_item->E->prev()->get();
  100. } else {
  101. //go back until something with a prev is found
  102. while (p_item->type != ITEM_FRAME && !p_item->E->prev()) {
  103. p_item = p_item->parent;
  104. }
  105. if (p_item->type != ITEM_FRAME)
  106. return p_item->E->prev()->get();
  107. else
  108. return NULL;
  109. }
  110. }
  111. return NULL;
  112. }
  113. Rect2 RichTextLabel::_get_text_rect() {
  114. Ref<StyleBox> style = get_stylebox("normal");
  115. return Rect2(style->get_offset(), get_size() - style->get_minimum_size());
  116. }
  117. 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) {
  118. RID ci;
  119. if (r_outside)
  120. *r_outside = false;
  121. if (p_mode == PROCESS_DRAW) {
  122. ci = get_canvas_item();
  123. if (r_click_item)
  124. *r_click_item = NULL;
  125. }
  126. Line &l = p_frame->lines.write[p_line];
  127. Item *it = l.from;
  128. int line_ofs = 0;
  129. int margin = _find_margin(it, p_base_font);
  130. Align align = _find_align(it);
  131. int line = 0;
  132. int spaces = 0;
  133. int height = get_size().y;
  134. if (p_mode != PROCESS_CACHE) {
  135. ERR_FAIL_INDEX_V(line, l.offset_caches.size(), 0);
  136. line_ofs = l.offset_caches[line];
  137. }
  138. if (p_mode == PROCESS_CACHE) {
  139. l.offset_caches.clear();
  140. l.height_caches.clear();
  141. l.ascent_caches.clear();
  142. l.descent_caches.clear();
  143. l.char_count = 0;
  144. l.minimum_width = 0;
  145. l.maximum_width = 0;
  146. }
  147. int wofs = margin;
  148. int spaces_size = 0;
  149. int align_ofs = 0;
  150. if (p_mode != PROCESS_CACHE && align != ALIGN_FILL)
  151. wofs += line_ofs;
  152. int begin = wofs;
  153. Ref<Font> cfont = _find_font(it);
  154. if (cfont.is_null())
  155. cfont = p_base_font;
  156. //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
  157. int line_height = cfont->get_height();
  158. int line_ascent = cfont->get_ascent();
  159. int line_descent = cfont->get_descent();
  160. int nonblank_line_count = 0; //number of nonblank lines as counted during PROCESS_DRAW
  161. Variant meta;
  162. #define RETURN return nonblank_line_count
  163. #define NEW_LINE \
  164. { \
  165. if (p_mode != PROCESS_CACHE) { \
  166. line++; \
  167. if (!line_is_blank) { \
  168. nonblank_line_count++; \
  169. } \
  170. line_is_blank = true; \
  171. if (line < l.offset_caches.size()) \
  172. line_ofs = l.offset_caches[line]; \
  173. wofs = margin; \
  174. if (align != ALIGN_FILL) \
  175. wofs += line_ofs; \
  176. } else { \
  177. int used = wofs - margin; \
  178. switch (align) { \
  179. case ALIGN_LEFT: l.offset_caches.push_back(0); break; \
  180. case ALIGN_CENTER: l.offset_caches.push_back(((p_width - margin) - used) / 2); break; \
  181. case ALIGN_RIGHT: l.offset_caches.push_back(((p_width - margin) - used)); break; \
  182. case ALIGN_FILL: l.offset_caches.push_back(line_wrapped ? ((p_width - margin) - used) : 0); break; \
  183. } \
  184. l.height_caches.push_back(line_height); \
  185. l.ascent_caches.push_back(line_ascent); \
  186. l.descent_caches.push_back(line_descent); \
  187. l.space_caches.push_back(spaces); \
  188. } \
  189. line_wrapped = false; \
  190. y += line_height + get_constant(SceneStringNames::get_singleton()->line_separation); \
  191. line_height = 0; \
  192. line_ascent = 0; \
  193. line_descent = 0; \
  194. spaces = 0; \
  195. spaces_size = 0; \
  196. wofs = begin; \
  197. align_ofs = 0; \
  198. if (p_mode != PROCESS_CACHE) { \
  199. lh = line < l.height_caches.size() ? l.height_caches[line] : 1; \
  200. line_ascent = line < l.ascent_caches.size() ? l.ascent_caches[line] : 1; \
  201. line_descent = line < l.descent_caches.size() ? l.descent_caches[line] : 1; \
  202. } \
  203. 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) { \
  204. if (r_outside) *r_outside = true; \
  205. *r_click_item = it; \
  206. *r_click_char = rchar; \
  207. RETURN; \
  208. } \
  209. }
  210. #define ENSURE_WIDTH(m_width) \
  211. if (p_mode == PROCESS_CACHE) { \
  212. l.maximum_width = MAX(l.maximum_width, MIN(p_width, wofs + m_width)); \
  213. l.minimum_width = MAX(l.minimum_width, m_width); \
  214. } \
  215. if (wofs + m_width > p_width) { \
  216. line_wrapped = true; \
  217. if (p_mode == PROCESS_CACHE) { \
  218. if (spaces > 0) \
  219. spaces -= 1; \
  220. } \
  221. 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) { \
  222. if (r_outside) *r_outside = true; \
  223. *r_click_item = it; \
  224. *r_click_char = rchar; \
  225. RETURN; \
  226. } \
  227. NEW_LINE \
  228. }
  229. #define ADVANCE(m_width) \
  230. { \
  231. if (p_mode == PROCESS_POINTER && r_click_item && p_click_pos.y >= p_ofs.y + y && p_click_pos.y <= p_ofs.y + y + lh && p_click_pos.x >= p_ofs.x + wofs && p_click_pos.x < p_ofs.x + wofs + m_width) { \
  232. if (r_outside) *r_outside = false; \
  233. *r_click_item = it; \
  234. *r_click_char = rchar; \
  235. RETURN; \
  236. } \
  237. wofs += m_width; \
  238. }
  239. #define CHECK_HEIGHT(m_height) \
  240. if (m_height > line_height) { \
  241. line_height = m_height; \
  242. }
  243. #define YRANGE_VISIBLE(m_top, m_height) \
  244. (m_height > 0 && ((m_top >= 0 && m_top < height) || ((m_top + m_height - 1) >= 0 && (m_top + m_height - 1) < height)))
  245. Color selection_fg;
  246. Color selection_bg;
  247. if (p_mode == PROCESS_DRAW) {
  248. selection_fg = get_color("font_color_selected");
  249. selection_bg = get_color("selection_color");
  250. }
  251. int rchar = 0;
  252. int lh = 0;
  253. bool line_is_blank = true;
  254. bool line_wrapped = false;
  255. int fh = 0;
  256. while (it) {
  257. switch (it->type) {
  258. case ITEM_TEXT: {
  259. ItemText *text = static_cast<ItemText *>(it);
  260. Ref<Font> font = _find_font(it);
  261. if (font.is_null())
  262. font = p_base_font;
  263. const CharType *c = text->text.c_str();
  264. const CharType *cf = c;
  265. int ascent = font->get_ascent();
  266. int descent = font->get_descent();
  267. Color color;
  268. Color font_color_shadow;
  269. bool underline = false;
  270. bool strikethrough = false;
  271. if (p_mode == PROCESS_DRAW) {
  272. color = _find_color(text, p_base_color);
  273. font_color_shadow = _find_color(text, p_font_color_shadow);
  274. if (_find_underline(text) || (_find_meta(text, &meta) && underline_meta)) {
  275. underline = true;
  276. } else if (_find_strikethrough(text)) {
  277. strikethrough = true;
  278. }
  279. } else if (p_mode == PROCESS_CACHE) {
  280. l.char_count += text->text.length();
  281. }
  282. rchar = 0;
  283. FontDrawer drawer(font, Color(1, 1, 1));
  284. while (*c) {
  285. int end = 0;
  286. int w = 0;
  287. int fw = 0;
  288. lh = 0;
  289. if (p_mode != PROCESS_CACHE) {
  290. lh = line < l.height_caches.size() ? l.height_caches[line] : 1;
  291. line_ascent = line < l.ascent_caches.size() ? l.ascent_caches[line] : 1;
  292. line_descent = line < l.descent_caches.size() ? l.descent_caches[line] : 1;
  293. }
  294. while (c[end] != 0 && !(end && c[end - 1] == ' ' && c[end] != ' ')) {
  295. int cw = font->get_char_size(c[end], c[end + 1]).width;
  296. if (c[end] == '\t') {
  297. cw = tab_size * font->get_char_size(' ').width;
  298. }
  299. if (end > 0 && w + cw + begin > p_width) {
  300. break; //don't allow lines longer than assigned width
  301. }
  302. w += cw;
  303. fw += cw;
  304. end++;
  305. }
  306. CHECK_HEIGHT(fh);
  307. ENSURE_WIDTH(w);
  308. line_ascent = MAX(line_ascent, ascent);
  309. line_descent = MAX(line_descent, descent);
  310. fh = line_ascent + line_descent;
  311. if (end && c[end - 1] == ' ') {
  312. if (p_mode == PROCESS_CACHE) {
  313. spaces_size += font->get_char_size(' ').width;
  314. } else if (align == ALIGN_FILL) {
  315. int ln = MIN(l.offset_caches.size() - 1, line);
  316. if (l.space_caches[ln]) {
  317. align_ofs = spaces * l.offset_caches[ln] / l.space_caches[ln];
  318. }
  319. }
  320. spaces++;
  321. }
  322. {
  323. int ofs = 0;
  324. for (int i = 0; i < end; i++) {
  325. int pofs = wofs + ofs;
  326. 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) {
  327. //int o = (wofs+w)-p_click_pos.x;
  328. int cw = font->get_char_size(c[i], c[i + 1]).x;
  329. if (c[i] == '\t') {
  330. cw = tab_size * font->get_char_size(' ').width;
  331. }
  332. if (p_click_pos.x - cw / 2 > p_ofs.x + align_ofs + pofs) {
  333. rchar = int((&c[i]) - cf);
  334. }
  335. ofs += cw;
  336. } else if (p_mode == PROCESS_DRAW) {
  337. bool selected = false;
  338. if (selection.active) {
  339. int cofs = (&c[i]) - cf;
  340. 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))) {
  341. selected = true;
  342. }
  343. }
  344. int cw = 0;
  345. bool visible = visible_characters < 0 || (p_char_count < visible_characters && YRANGE_VISIBLE(y + lh - line_descent - line_ascent, line_ascent + line_descent));
  346. if (visible)
  347. line_is_blank = false;
  348. if (c[i] == '\t')
  349. visible = false;
  350. if (visible) {
  351. if (selected) {
  352. cw = font->get_char_size(c[i], c[i + 1]).x;
  353. draw_rect(Rect2(p_ofs.x + pofs, p_ofs.y + y, cw, lh), selection_bg);
  354. }
  355. if (p_font_color_shadow.a > 0) {
  356. float x_ofs_shadow = align_ofs + pofs;
  357. float y_ofs_shadow = y + lh - line_descent;
  358. float move = font->draw_char(ci, Point2(x_ofs_shadow, y_ofs_shadow) + shadow_ofs, c[i], c[i + 1], p_font_color_shadow);
  359. if (p_shadow_as_outline) {
  360. font->draw_char(ci, Point2(x_ofs_shadow, y_ofs_shadow) + Vector2(-shadow_ofs.x, shadow_ofs.y), c[i], c[i + 1], p_font_color_shadow);
  361. font->draw_char(ci, Point2(x_ofs_shadow, y_ofs_shadow) + Vector2(shadow_ofs.x, -shadow_ofs.y), c[i], c[i + 1], p_font_color_shadow);
  362. font->draw_char(ci, Point2(x_ofs_shadow, y_ofs_shadow) + Vector2(-shadow_ofs.x, -shadow_ofs.y), c[i], c[i + 1], p_font_color_shadow);
  363. }
  364. x_ofs_shadow += move;
  365. }
  366. if (selected) {
  367. drawer.draw_char(ci, p_ofs + Point2(align_ofs + pofs, y + lh - line_descent), c[i], c[i + 1], override_selected_font_color ? selection_fg : color);
  368. } else {
  369. cw = drawer.draw_char(ci, p_ofs + Point2(align_ofs + pofs, y + lh - line_descent), c[i], c[i + 1], color);
  370. }
  371. }
  372. p_char_count++;
  373. if (c[i] == '\t') {
  374. cw = tab_size * font->get_char_size(' ').width;
  375. }
  376. ofs += cw;
  377. }
  378. }
  379. if (underline) {
  380. Color uc = color;
  381. uc.a *= 0.5;
  382. int uy = y + lh - line_descent + 2;
  383. float underline_width = 1.0;
  384. #ifdef TOOLS_ENABLED
  385. underline_width *= EDSCALE;
  386. #endif
  387. 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);
  388. } else if (strikethrough) {
  389. Color uc = color;
  390. uc.a *= 0.5;
  391. int uy = y + lh / 2 - line_descent + 2;
  392. float strikethrough_width = 1.0;
  393. #ifdef TOOLS_ENABLED
  394. strikethrough_width *= EDSCALE;
  395. #endif
  396. 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);
  397. }
  398. }
  399. ADVANCE(fw);
  400. CHECK_HEIGHT(fh); //must be done somewhere
  401. c = &c[end];
  402. }
  403. } break;
  404. case ITEM_IMAGE: {
  405. lh = 0;
  406. if (p_mode != PROCESS_CACHE)
  407. lh = line < l.height_caches.size() ? l.height_caches[line] : 1;
  408. else
  409. l.char_count += 1; //images count as chars too
  410. ItemImage *img = static_cast<ItemImage *>(it);
  411. Ref<Font> font = _find_font(it);
  412. if (font.is_null())
  413. font = p_base_font;
  414. if (p_mode == PROCESS_POINTER && r_click_char)
  415. *r_click_char = 0;
  416. ENSURE_WIDTH(img->image->get_width());
  417. bool visible = visible_characters < 0 || (p_char_count < visible_characters && YRANGE_VISIBLE(y + lh - font->get_descent() - img->image->get_height(), img->image->get_height()));
  418. if (visible)
  419. line_is_blank = false;
  420. if (p_mode == PROCESS_DRAW && visible) {
  421. img->image->draw(ci, p_ofs + Point2(align_ofs + wofs, y + lh - font->get_descent() - img->image->get_height()));
  422. }
  423. p_char_count++;
  424. ADVANCE(img->image->get_width());
  425. CHECK_HEIGHT((img->image->get_height() + font->get_descent()));
  426. } break;
  427. case ITEM_NEWLINE: {
  428. lh = 0;
  429. if (p_mode != PROCESS_CACHE) {
  430. lh = line < l.height_caches.size() ? l.height_caches[line] : 1;
  431. line_is_blank = true;
  432. }
  433. } break;
  434. case ITEM_TABLE: {
  435. lh = 0;
  436. ItemTable *table = static_cast<ItemTable *>(it);
  437. int hseparation = get_constant("table_hseparation");
  438. int vseparation = get_constant("table_vseparation");
  439. Color ccolor = _find_color(table, p_base_color);
  440. Vector2 draw_ofs = Point2(wofs, y);
  441. Color font_color_shadow = get_color("font_color_shadow");
  442. bool use_outline = get_constant("shadow_as_outline");
  443. Point2 shadow_ofs(get_constant("shadow_offset_x"), get_constant("shadow_offset_y"));
  444. if (p_mode == PROCESS_CACHE) {
  445. int idx = 0;
  446. //set minimums to zero
  447. for (int i = 0; i < table->columns.size(); i++) {
  448. table->columns.write[i].min_width = 0;
  449. table->columns.write[i].max_width = 0;
  450. table->columns.write[i].width = 0;
  451. }
  452. //compute minimum width for each cell
  453. const int available_width = p_width - hseparation * (table->columns.size() - 1) - wofs;
  454. for (List<Item *>::Element *E = table->subitems.front(); E; E = E->next()) {
  455. ERR_CONTINUE(E->get()->type != ITEM_FRAME); //children should all be frames
  456. ItemFrame *frame = static_cast<ItemFrame *>(E->get());
  457. int column = idx % table->columns.size();
  458. int ly = 0;
  459. for (int i = 0; i < frame->lines.size(); i++) {
  460. _process_line(frame, Point2(), ly, available_width, i, PROCESS_CACHE, cfont, Color(), font_color_shadow, use_outline, shadow_ofs);
  461. table->columns.write[column].min_width = MAX(table->columns[column].min_width, frame->lines[i].minimum_width);
  462. table->columns.write[column].max_width = MAX(table->columns[column].max_width, frame->lines[i].maximum_width);
  463. }
  464. idx++;
  465. }
  466. //compute available width and total ratio (for expanders)
  467. int total_ratio = 0;
  468. int remaining_width = available_width;
  469. table->total_width = hseparation;
  470. for (int i = 0; i < table->columns.size(); i++) {
  471. remaining_width -= table->columns[i].min_width;
  472. if (table->columns[i].max_width > table->columns[i].min_width)
  473. table->columns.write[i].expand = true;
  474. if (table->columns[i].expand)
  475. total_ratio += table->columns[i].expand_ratio;
  476. }
  477. //assign actual widths
  478. for (int i = 0; i < table->columns.size(); i++) {
  479. table->columns.write[i].width = table->columns[i].min_width;
  480. if (table->columns[i].expand)
  481. table->columns.write[i].width += table->columns[i].expand_ratio * remaining_width / total_ratio;
  482. table->total_width += table->columns[i].width + hseparation;
  483. }
  484. //resize to max_width if needed and distribute the remaining space
  485. bool table_need_fit = true;
  486. while (table_need_fit) {
  487. table_need_fit = false;
  488. //fit slim
  489. for (int i = 0; i < table->columns.size(); i++) {
  490. if (!table->columns[i].expand)
  491. continue;
  492. int dif = table->columns[i].width - table->columns[i].max_width;
  493. if (dif > 0) {
  494. table_need_fit = true;
  495. table->columns.write[i].width = table->columns[i].max_width;
  496. table->total_width -= dif;
  497. total_ratio -= table->columns[i].expand_ratio;
  498. }
  499. }
  500. //grow
  501. remaining_width = available_width - table->total_width;
  502. if (remaining_width > 0 && total_ratio > 0) {
  503. for (int i = 0; i < table->columns.size(); i++) {
  504. if (table->columns[i].expand) {
  505. int dif = table->columns[i].max_width - table->columns[i].width;
  506. if (dif > 0) {
  507. int slice = table->columns[i].expand_ratio * remaining_width / total_ratio;
  508. int incr = MIN(dif, slice);
  509. table->columns.write[i].width += incr;
  510. table->total_width += incr;
  511. }
  512. }
  513. }
  514. }
  515. }
  516. //compute caches properly again with the right width
  517. idx = 0;
  518. for (List<Item *>::Element *E = table->subitems.front(); E; E = E->next()) {
  519. ERR_CONTINUE(E->get()->type != ITEM_FRAME); //children should all be frames
  520. ItemFrame *frame = static_cast<ItemFrame *>(E->get());
  521. int column = idx % table->columns.size();
  522. for (int i = 0; i < frame->lines.size(); i++) {
  523. int ly = 0;
  524. _process_line(frame, Point2(), ly, table->columns[column].width, i, PROCESS_CACHE, cfont, Color(), font_color_shadow, use_outline, shadow_ofs);
  525. frame->lines.write[i].height_cache = ly; //actual height
  526. frame->lines.write[i].height_accum_cache = ly; //actual height
  527. }
  528. idx++;
  529. }
  530. }
  531. Point2 offset(align_ofs + hseparation, vseparation);
  532. int row_height = 0;
  533. //draw using computed caches
  534. int idx = 0;
  535. for (List<Item *>::Element *E = table->subitems.front(); E; E = E->next()) {
  536. ERR_CONTINUE(E->get()->type != ITEM_FRAME); //children should all be frames
  537. ItemFrame *frame = static_cast<ItemFrame *>(E->get());
  538. int column = idx % table->columns.size();
  539. int ly = 0;
  540. int yofs = 0;
  541. int lines_h = frame->lines[frame->lines.size() - 1].height_accum_cache - (frame->lines[0].height_accum_cache - frame->lines[0].height_cache);
  542. int lines_ofs = p_ofs.y + offset.y + draw_ofs.y;
  543. bool visible = lines_ofs < get_size().height && lines_ofs + lines_h >= 0;
  544. if (visible)
  545. line_is_blank = false;
  546. for (int i = 0; i < frame->lines.size(); i++) {
  547. if (visible) {
  548. if (p_mode == PROCESS_DRAW) {
  549. 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_ofs);
  550. } else if (p_mode == PROCESS_POINTER) {
  551. _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_ofs, p_click_pos, r_click_item, r_click_char, r_outside);
  552. if (r_click_item && *r_click_item) {
  553. RETURN; // exit early
  554. }
  555. }
  556. }
  557. yofs += frame->lines[i].height_cache;
  558. if (p_mode == PROCESS_CACHE) {
  559. frame->lines.write[i].height_accum_cache = offset.y + draw_ofs.y + frame->lines[i].height_cache;
  560. }
  561. }
  562. row_height = MAX(yofs, row_height);
  563. offset.x += table->columns[column].width + hseparation;
  564. if (column == table->columns.size() - 1) {
  565. offset.y += row_height + vseparation;
  566. offset.x = hseparation;
  567. row_height = 0;
  568. }
  569. idx++;
  570. }
  571. int total_height = offset.y;
  572. if (row_height) {
  573. total_height = row_height + vseparation;
  574. }
  575. ADVANCE(table->total_width);
  576. CHECK_HEIGHT(total_height);
  577. } break;
  578. default: {}
  579. }
  580. Item *itp = it;
  581. it = _get_next_item(it);
  582. if (it && (p_line + 1 < p_frame->lines.size()) && p_frame->lines[p_line + 1].from == it) {
  583. 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) {
  584. //went to next line, but pointer was on the previous one
  585. if (r_outside) *r_outside = true;
  586. *r_click_item = itp;
  587. *r_click_char = rchar;
  588. RETURN;
  589. }
  590. break;
  591. }
  592. }
  593. NEW_LINE;
  594. RETURN;
  595. #undef RETURN
  596. #undef NEW_LINE
  597. #undef ENSURE_WIDTH
  598. #undef ADVANCE
  599. #undef CHECK_HEIGHT
  600. }
  601. void RichTextLabel::_scroll_changed(double) {
  602. if (updating_scroll)
  603. return;
  604. if (scroll_follow && vscroll->get_value() >= (vscroll->get_max() - vscroll->get_page()))
  605. scroll_following = true;
  606. else
  607. scroll_following = false;
  608. update();
  609. }
  610. void RichTextLabel::_update_scroll() {
  611. int total_height = get_content_height();
  612. bool exceeds = total_height > get_size().height && scroll_active;
  613. if (exceeds != scroll_visible) {
  614. if (exceeds) {
  615. scroll_visible = true;
  616. scroll_w = vscroll->get_combined_minimum_size().width;
  617. vscroll->show();
  618. vscroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -scroll_w);
  619. } else {
  620. scroll_visible = false;
  621. scroll_w = 0;
  622. vscroll->hide();
  623. }
  624. main->first_invalid_line = 0; //invalidate ALL
  625. _validate_line_caches(main);
  626. }
  627. scroll_updated = true;
  628. }
  629. void RichTextLabel::_notification(int p_what) {
  630. switch (p_what) {
  631. case NOTIFICATION_RESIZED: {
  632. main->first_invalid_line = 0; //invalidate ALL
  633. update();
  634. } break;
  635. case NOTIFICATION_ENTER_TREE: {
  636. if (bbcode != "")
  637. set_bbcode(bbcode);
  638. main->first_invalid_line = 0; //invalidate ALL
  639. update();
  640. } break;
  641. case NOTIFICATION_THEME_CHANGED: {
  642. if (is_inside_tree() && use_bbcode) {
  643. parse_bbcode(bbcode);
  644. //first_invalid_line=0; //invalidate ALL
  645. //update();
  646. }
  647. } break;
  648. case NOTIFICATION_DRAW: {
  649. _validate_line_caches(main);
  650. _update_scroll();
  651. RID ci = get_canvas_item();
  652. Size2 size = get_size();
  653. Rect2 text_rect = _get_text_rect();
  654. draw_style_box(get_stylebox("normal"), Rect2(Point2(), size));
  655. if (has_focus()) {
  656. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, true);
  657. draw_style_box(get_stylebox("focus"), Rect2(Point2(), size));
  658. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, false);
  659. }
  660. int ofs = vscroll->get_value();
  661. //todo, change to binary search
  662. int from_line = 0;
  663. int total_chars = 0;
  664. while (from_line < main->lines.size()) {
  665. if (main->lines[from_line].height_accum_cache + _get_text_rect().get_position().y >= ofs)
  666. break;
  667. total_chars += main->lines[from_line].char_count;
  668. from_line++;
  669. }
  670. if (from_line >= main->lines.size())
  671. break; //nothing to draw
  672. int y = (main->lines[from_line].height_accum_cache - main->lines[from_line].height_cache) - ofs;
  673. Ref<Font> base_font = get_font("normal_font");
  674. Color base_color = get_color("default_color");
  675. Color font_color_shadow = get_color("font_color_shadow");
  676. bool use_outline = get_constant("shadow_as_outline");
  677. Point2 shadow_ofs(get_constant("shadow_offset_x"), get_constant("shadow_offset_y"));
  678. visible_line_count = 0;
  679. while (y < size.height && from_line < main->lines.size()) {
  680. 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);
  681. total_chars += main->lines[from_line].char_count;
  682. from_line++;
  683. }
  684. }
  685. }
  686. }
  687. void RichTextLabel::_find_click(ItemFrame *p_frame, const Point2i &p_click, Item **r_click_item, int *r_click_char, bool *r_outside) {
  688. if (r_click_item)
  689. *r_click_item = NULL;
  690. Rect2 text_rect = _get_text_rect();
  691. int ofs = vscroll->get_value();
  692. Color font_color_shadow = get_color("font_color_shadow");
  693. bool use_outline = get_constant("shadow_as_outline");
  694. Point2 shadow_ofs(get_constant("shadow_offset_x"), get_constant("shadow_offset_y"));
  695. //todo, change to binary search
  696. int from_line = 0;
  697. while (from_line < p_frame->lines.size()) {
  698. if (p_frame->lines[from_line].height_accum_cache >= ofs)
  699. break;
  700. from_line++;
  701. }
  702. if (from_line >= p_frame->lines.size())
  703. return;
  704. int y = (p_frame->lines[from_line].height_accum_cache - p_frame->lines[from_line].height_cache) - ofs;
  705. Ref<Font> base_font = get_font("normal_font");
  706. Color base_color = get_color("default_color");
  707. while (y < text_rect.get_size().height && from_line < p_frame->lines.size()) {
  708. _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);
  709. if (r_click_item && *r_click_item)
  710. return;
  711. from_line++;
  712. }
  713. }
  714. Control::CursorShape RichTextLabel::get_cursor_shape(const Point2 &p_pos) const {
  715. if (!underline_meta || selection.click)
  716. return CURSOR_ARROW;
  717. if (main->first_invalid_line < main->lines.size())
  718. return CURSOR_ARROW; //invalid
  719. int line = 0;
  720. Item *item = NULL;
  721. ((RichTextLabel *)(this))->_find_click(main, p_pos, &item, &line);
  722. if (item && ((RichTextLabel *)(this))->_find_meta(item, NULL))
  723. return CURSOR_POINTING_HAND;
  724. return CURSOR_ARROW;
  725. }
  726. void RichTextLabel::_gui_input(Ref<InputEvent> p_event) {
  727. Ref<InputEventMouseButton> b = p_event;
  728. if (b.is_valid()) {
  729. if (main->first_invalid_line < main->lines.size())
  730. return;
  731. if (b->get_button_index() == BUTTON_LEFT) {
  732. if (true) {
  733. if (b->is_pressed() && !b->is_doubleclick()) {
  734. scroll_updated = false;
  735. int line = 0;
  736. Item *item = NULL;
  737. bool outside;
  738. _find_click(main, b->get_position(), &item, &line, &outside);
  739. if (item) {
  740. if (selection.enabled) {
  741. selection.click = item;
  742. selection.click_char = line;
  743. // Erase previous selection.
  744. if (selection.active) {
  745. selection.from = NULL;
  746. selection.from_char = '\0';
  747. selection.to = NULL;
  748. selection.to_char = '\0';
  749. selection.active = false;
  750. update();
  751. }
  752. }
  753. }
  754. } else if (b->is_pressed() && b->is_doubleclick() && selection.enabled) {
  755. //doubleclick: select word
  756. int line = 0;
  757. Item *item = NULL;
  758. bool outside;
  759. _find_click(main, b->get_position(), &item, &line, &outside);
  760. while (item && item->type != ITEM_TEXT) {
  761. item = _get_next_item(item, true);
  762. }
  763. if (item && item->type == ITEM_TEXT) {
  764. String itext = static_cast<ItemText *>(item)->text;
  765. int beg, end;
  766. if (select_word(itext, line, beg, end)) {
  767. selection.from = item;
  768. selection.to = item;
  769. selection.from_char = beg;
  770. selection.to_char = end - 1;
  771. selection.active = true;
  772. update();
  773. }
  774. }
  775. } else if (!b->is_pressed()) {
  776. selection.click = NULL;
  777. if (!b->is_doubleclick() && !scroll_updated) {
  778. int line = 0;
  779. Item *item = NULL;
  780. bool outside;
  781. _find_click(main, b->get_position(), &item, &line, &outside);
  782. if (item) {
  783. Variant meta;
  784. if (!outside && _find_meta(item, &meta)) {
  785. //meta clicked
  786. emit_signal("meta_clicked", meta);
  787. }
  788. }
  789. }
  790. }
  791. }
  792. }
  793. if (b->get_button_index() == BUTTON_WHEEL_UP) {
  794. if (scroll_active)
  795. vscroll->set_value(vscroll->get_value() - vscroll->get_page() * b->get_factor() * 0.5 / 8);
  796. }
  797. if (b->get_button_index() == BUTTON_WHEEL_DOWN) {
  798. if (scroll_active)
  799. vscroll->set_value(vscroll->get_value() + vscroll->get_page() * b->get_factor() * 0.5 / 8);
  800. }
  801. }
  802. Ref<InputEventPanGesture> pan_gesture = p_event;
  803. if (pan_gesture.is_valid()) {
  804. if (scroll_active)
  805. vscroll->set_value(vscroll->get_value() + vscroll->get_page() * pan_gesture->get_delta().y * 0.5 / 8);
  806. return;
  807. }
  808. Ref<InputEventKey> k = p_event;
  809. if (k.is_valid()) {
  810. if (k->is_pressed() && !k->get_alt() && !k->get_shift()) {
  811. bool handled = true;
  812. switch (k->get_scancode()) {
  813. case KEY_PAGEUP: {
  814. if (vscroll->is_visible_in_tree())
  815. vscroll->set_value(vscroll->get_value() - vscroll->get_page());
  816. } break;
  817. case KEY_PAGEDOWN: {
  818. if (vscroll->is_visible_in_tree())
  819. vscroll->set_value(vscroll->get_value() + vscroll->get_page());
  820. } break;
  821. case KEY_UP: {
  822. if (vscroll->is_visible_in_tree())
  823. vscroll->set_value(vscroll->get_value() - get_font("normal_font")->get_height());
  824. } break;
  825. case KEY_DOWN: {
  826. if (vscroll->is_visible_in_tree())
  827. vscroll->set_value(vscroll->get_value() + get_font("normal_font")->get_height());
  828. } break;
  829. case KEY_HOME: {
  830. if (vscroll->is_visible_in_tree())
  831. vscroll->set_value(0);
  832. } break;
  833. case KEY_END: {
  834. if (vscroll->is_visible_in_tree())
  835. vscroll->set_value(vscroll->get_max());
  836. } break;
  837. case KEY_INSERT:
  838. case KEY_C: {
  839. if (k->get_command()) {
  840. selection_copy();
  841. } else {
  842. handled = false;
  843. }
  844. } break;
  845. default: handled = false;
  846. }
  847. if (handled)
  848. accept_event();
  849. }
  850. }
  851. Ref<InputEventMouseMotion> m = p_event;
  852. if (m.is_valid()) {
  853. if (main->first_invalid_line < main->lines.size())
  854. return;
  855. int line = 0;
  856. Item *item = NULL;
  857. bool outside;
  858. _find_click(main, m->get_position(), &item, &line, &outside);
  859. if (selection.click) {
  860. if (!item)
  861. return; // do not update
  862. selection.from = selection.click;
  863. selection.from_char = selection.click_char;
  864. selection.to = item;
  865. selection.to_char = line;
  866. bool swap = false;
  867. if (selection.from->index > selection.to->index)
  868. swap = true;
  869. else if (selection.from->index == selection.to->index) {
  870. if (selection.from_char > selection.to_char)
  871. swap = true;
  872. else if (selection.from_char == selection.to_char) {
  873. selection.active = false;
  874. return;
  875. }
  876. }
  877. if (swap) {
  878. SWAP(selection.from, selection.to);
  879. SWAP(selection.from_char, selection.to_char);
  880. }
  881. selection.active = true;
  882. update();
  883. }
  884. Variant meta;
  885. if (item && !outside && _find_meta(item, &meta)) {
  886. if (meta_hovering != item) {
  887. if (meta_hovering) {
  888. emit_signal("meta_hover_ended", current_meta);
  889. }
  890. meta_hovering = static_cast<ItemMeta *>(item);
  891. current_meta = meta;
  892. emit_signal("meta_hover_started", meta);
  893. }
  894. } else if (meta_hovering) {
  895. emit_signal("meta_hover_ended", current_meta);
  896. meta_hovering = NULL;
  897. current_meta = false;
  898. }
  899. }
  900. }
  901. Ref<Font> RichTextLabel::_find_font(Item *p_item) {
  902. Item *fontitem = p_item;
  903. while (fontitem) {
  904. if (fontitem->type == ITEM_FONT) {
  905. ItemFont *fi = static_cast<ItemFont *>(fontitem);
  906. return fi->font;
  907. }
  908. fontitem = fontitem->parent;
  909. }
  910. return Ref<Font>();
  911. }
  912. int RichTextLabel::_find_margin(Item *p_item, const Ref<Font> &p_base_font) {
  913. Item *item = p_item;
  914. int margin = 0;
  915. while (item) {
  916. if (item->type == ITEM_INDENT) {
  917. Ref<Font> font = _find_font(item);
  918. if (font.is_null())
  919. font = p_base_font;
  920. ItemIndent *indent = static_cast<ItemIndent *>(item);
  921. margin += indent->level * tab_size * font->get_char_size(' ').width;
  922. } else if (item->type == ITEM_LIST) {
  923. Ref<Font> font = _find_font(item);
  924. if (font.is_null())
  925. font = p_base_font;
  926. }
  927. item = item->parent;
  928. }
  929. return margin;
  930. }
  931. RichTextLabel::Align RichTextLabel::_find_align(Item *p_item) {
  932. Item *item = p_item;
  933. while (item) {
  934. if (item->type == ITEM_ALIGN) {
  935. ItemAlign *align = static_cast<ItemAlign *>(item);
  936. return align->align;
  937. }
  938. item = item->parent;
  939. }
  940. return default_align;
  941. }
  942. Color RichTextLabel::_find_color(Item *p_item, const Color &p_default_color) {
  943. Item *item = p_item;
  944. while (item) {
  945. if (item->type == ITEM_COLOR) {
  946. ItemColor *color = static_cast<ItemColor *>(item);
  947. return color->color;
  948. }
  949. item = item->parent;
  950. }
  951. return p_default_color;
  952. }
  953. bool RichTextLabel::_find_underline(Item *p_item) {
  954. Item *item = p_item;
  955. while (item) {
  956. if (item->type == ITEM_UNDERLINE) {
  957. return true;
  958. }
  959. item = item->parent;
  960. }
  961. return false;
  962. }
  963. bool RichTextLabel::_find_strikethrough(Item *p_item) {
  964. Item *item = p_item;
  965. while (item) {
  966. if (item->type == ITEM_STRIKETHROUGH) {
  967. return true;
  968. }
  969. item = item->parent;
  970. }
  971. return false;
  972. }
  973. bool RichTextLabel::_find_meta(Item *p_item, Variant *r_meta) {
  974. Item *item = p_item;
  975. while (item) {
  976. if (item->type == ITEM_META) {
  977. ItemMeta *meta = static_cast<ItemMeta *>(item);
  978. if (r_meta)
  979. *r_meta = meta->meta;
  980. return true;
  981. }
  982. item = item->parent;
  983. }
  984. return false;
  985. }
  986. void RichTextLabel::_validate_line_caches(ItemFrame *p_frame) {
  987. if (p_frame->first_invalid_line == p_frame->lines.size())
  988. return;
  989. //validate invalid lines
  990. Size2 size = get_size();
  991. if (fixed_width != -1) {
  992. size.width = fixed_width;
  993. }
  994. Rect2 text_rect = _get_text_rect();
  995. Color font_color_shadow = get_color("font_color_shadow");
  996. bool use_outline = get_constant("shadow_as_outline");
  997. Point2 shadow_ofs(get_constant("shadow_offset_x"), get_constant("shadow_offset_y"));
  998. Ref<Font> base_font = get_font("normal_font");
  999. for (int i = p_frame->first_invalid_line; i < p_frame->lines.size(); i++) {
  1000. int y = 0;
  1001. _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);
  1002. p_frame->lines.write[i].height_cache = y;
  1003. p_frame->lines.write[i].height_accum_cache = y;
  1004. if (i > 0)
  1005. p_frame->lines.write[i].height_accum_cache += p_frame->lines[i - 1].height_accum_cache;
  1006. }
  1007. int total_height = 0;
  1008. if (p_frame->lines.size())
  1009. total_height = p_frame->lines[p_frame->lines.size() - 1].height_accum_cache + get_stylebox("normal")->get_minimum_size().height;
  1010. main->first_invalid_line = p_frame->lines.size();
  1011. updating_scroll = true;
  1012. vscroll->set_max(total_height);
  1013. vscroll->set_page(size.height);
  1014. if (scroll_follow && scroll_following)
  1015. vscroll->set_value(total_height - size.height);
  1016. updating_scroll = false;
  1017. }
  1018. void RichTextLabel::_invalidate_current_line(ItemFrame *p_frame) {
  1019. if (p_frame->lines.size() - 1 <= p_frame->first_invalid_line) {
  1020. p_frame->first_invalid_line = p_frame->lines.size() - 1;
  1021. update();
  1022. }
  1023. }
  1024. void RichTextLabel::add_text(const String &p_text) {
  1025. if (current->type == ITEM_TABLE)
  1026. return; //can't add anything here
  1027. int pos = 0;
  1028. while (pos < p_text.length()) {
  1029. int end = p_text.find("\n", pos);
  1030. String line;
  1031. bool eol = false;
  1032. if (end == -1) {
  1033. end = p_text.length();
  1034. } else {
  1035. eol = true;
  1036. }
  1037. if (pos == 0 && end == p_text.length())
  1038. line = p_text;
  1039. else
  1040. line = p_text.substr(pos, end - pos);
  1041. if (line.length() > 0) {
  1042. if (current->subitems.size() && current->subitems.back()->get()->type == ITEM_TEXT) {
  1043. //append text condition!
  1044. ItemText *ti = static_cast<ItemText *>(current->subitems.back()->get());
  1045. ti->text += line;
  1046. _invalidate_current_line(main);
  1047. } else {
  1048. //append item condition
  1049. ItemText *item = memnew(ItemText);
  1050. item->text = line;
  1051. _add_item(item, false);
  1052. }
  1053. }
  1054. if (eol) {
  1055. ItemNewline *item = memnew(ItemNewline);
  1056. item->line = current_frame->lines.size();
  1057. _add_item(item, false);
  1058. current_frame->lines.resize(current_frame->lines.size() + 1);
  1059. if (item->type != ITEM_NEWLINE)
  1060. current_frame->lines.write[current_frame->lines.size() - 1].from = item;
  1061. _invalidate_current_line(current_frame);
  1062. }
  1063. pos = end + 1;
  1064. }
  1065. }
  1066. void RichTextLabel::_add_item(Item *p_item, bool p_enter, bool p_ensure_newline) {
  1067. p_item->parent = current;
  1068. p_item->E = current->subitems.push_back(p_item);
  1069. p_item->index = current_idx++;
  1070. if (p_enter)
  1071. current = p_item;
  1072. if (p_ensure_newline && current_frame->lines[current_frame->lines.size() - 1].from) {
  1073. _invalidate_current_line(current_frame);
  1074. current_frame->lines.resize(current_frame->lines.size() + 1);
  1075. }
  1076. if (current_frame->lines[current_frame->lines.size() - 1].from == NULL) {
  1077. current_frame->lines.write[current_frame->lines.size() - 1].from = p_item;
  1078. }
  1079. p_item->line = current_frame->lines.size() - 1;
  1080. _invalidate_current_line(current_frame);
  1081. }
  1082. void RichTextLabel::_remove_item(Item *p_item, const int p_line, const int p_subitem_line) {
  1083. int size = p_item->subitems.size();
  1084. if (size == 0) {
  1085. p_item->parent->subitems.erase(p_item);
  1086. if (p_item->type == ITEM_NEWLINE) {
  1087. current_frame->lines.remove(p_line);
  1088. for (int i = p_subitem_line; i < current->subitems.size(); i++) {
  1089. if (current->subitems[i]->line > 0)
  1090. current->subitems[i]->line--;
  1091. }
  1092. }
  1093. } else {
  1094. for (int i = 0; i < size; i++) {
  1095. _remove_item(p_item->subitems.front()->get(), p_line, p_subitem_line);
  1096. }
  1097. }
  1098. }
  1099. void RichTextLabel::add_image(const Ref<Texture> &p_image) {
  1100. if (current->type == ITEM_TABLE)
  1101. return;
  1102. ERR_FAIL_COND(p_image.is_null());
  1103. ItemImage *item = memnew(ItemImage);
  1104. item->image = p_image;
  1105. _add_item(item, false);
  1106. }
  1107. void RichTextLabel::add_newline() {
  1108. if (current->type == ITEM_TABLE)
  1109. return;
  1110. ItemNewline *item = memnew(ItemNewline);
  1111. item->line = current_frame->lines.size();
  1112. _add_item(item, false);
  1113. current_frame->lines.resize(current_frame->lines.size() + 1);
  1114. _invalidate_current_line(current_frame);
  1115. }
  1116. bool RichTextLabel::remove_line(const int p_line) {
  1117. if (p_line >= current_frame->lines.size() || p_line < 0)
  1118. return false;
  1119. int i = 0;
  1120. while (i < current->subitems.size() && current->subitems[i]->line < p_line) {
  1121. i++;
  1122. }
  1123. bool was_newline = false;
  1124. while (i < current->subitems.size()) {
  1125. was_newline = current->subitems[i]->type == ITEM_NEWLINE;
  1126. _remove_item(current->subitems[i], current->subitems[i]->line, p_line);
  1127. if (was_newline)
  1128. break;
  1129. }
  1130. if (!was_newline) {
  1131. current_frame->lines.remove(p_line);
  1132. }
  1133. if (p_line == 0 && current->subitems.size() > 0)
  1134. main->lines.write[0].from = main;
  1135. main->first_invalid_line = 0;
  1136. return true;
  1137. }
  1138. void RichTextLabel::push_font(const Ref<Font> &p_font) {
  1139. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1140. ERR_FAIL_COND(p_font.is_null());
  1141. ItemFont *item = memnew(ItemFont);
  1142. item->font = p_font;
  1143. _add_item(item, true);
  1144. }
  1145. void RichTextLabel::push_color(const Color &p_color) {
  1146. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1147. ItemColor *item = memnew(ItemColor);
  1148. item->color = p_color;
  1149. _add_item(item, true);
  1150. }
  1151. void RichTextLabel::push_underline() {
  1152. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1153. ItemUnderline *item = memnew(ItemUnderline);
  1154. _add_item(item, true);
  1155. }
  1156. void RichTextLabel::push_strikethrough() {
  1157. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1158. ItemStrikethrough *item = memnew(ItemStrikethrough);
  1159. _add_item(item, true);
  1160. }
  1161. void RichTextLabel::push_align(Align p_align) {
  1162. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1163. ItemAlign *item = memnew(ItemAlign);
  1164. item->align = p_align;
  1165. _add_item(item, true, true);
  1166. }
  1167. void RichTextLabel::push_indent(int p_level) {
  1168. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1169. ERR_FAIL_COND(p_level < 0);
  1170. ItemIndent *item = memnew(ItemIndent);
  1171. item->level = p_level;
  1172. _add_item(item, true, true);
  1173. }
  1174. void RichTextLabel::push_list(ListType p_list) {
  1175. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1176. ERR_FAIL_INDEX(p_list, 3);
  1177. ItemList *item = memnew(ItemList);
  1178. item->list_type = p_list;
  1179. _add_item(item, true, true);
  1180. }
  1181. void RichTextLabel::push_meta(const Variant &p_meta) {
  1182. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1183. ItemMeta *item = memnew(ItemMeta);
  1184. item->meta = p_meta;
  1185. _add_item(item, true);
  1186. }
  1187. void RichTextLabel::push_table(int p_columns) {
  1188. ERR_FAIL_COND(p_columns < 1);
  1189. ItemTable *item = memnew(ItemTable);
  1190. item->columns.resize(p_columns);
  1191. item->total_width = 0;
  1192. for (int i = 0; i < item->columns.size(); i++) {
  1193. item->columns.write[i].expand = false;
  1194. item->columns.write[i].expand_ratio = 1;
  1195. }
  1196. _add_item(item, true, true);
  1197. }
  1198. void RichTextLabel::set_table_column_expand(int p_column, bool p_expand, int p_ratio) {
  1199. ERR_FAIL_COND(current->type != ITEM_TABLE);
  1200. ItemTable *table = static_cast<ItemTable *>(current);
  1201. ERR_FAIL_INDEX(p_column, table->columns.size());
  1202. table->columns.write[p_column].expand = p_expand;
  1203. table->columns.write[p_column].expand_ratio = p_ratio;
  1204. }
  1205. void RichTextLabel::push_cell() {
  1206. ERR_FAIL_COND(current->type != ITEM_TABLE);
  1207. ItemFrame *item = memnew(ItemFrame);
  1208. item->parent_frame = current_frame;
  1209. _add_item(item, true);
  1210. current_frame = item;
  1211. item->cell = true;
  1212. item->parent_line = item->parent_frame->lines.size() - 1;
  1213. item->lines.resize(1);
  1214. item->lines.write[0].from = NULL;
  1215. item->first_invalid_line = 0;
  1216. }
  1217. int RichTextLabel::get_current_table_column() const {
  1218. ERR_FAIL_COND_V(current->type != ITEM_TABLE, -1);
  1219. ItemTable *table = static_cast<ItemTable *>(current);
  1220. return table->subitems.size() % table->columns.size();
  1221. }
  1222. void RichTextLabel::pop() {
  1223. ERR_FAIL_COND(!current->parent);
  1224. if (current->type == ITEM_FRAME) {
  1225. current_frame = static_cast<ItemFrame *>(current)->parent_frame;
  1226. }
  1227. current = current->parent;
  1228. }
  1229. void RichTextLabel::clear() {
  1230. main->_clear_children();
  1231. current = main;
  1232. current_frame = main;
  1233. main->lines.clear();
  1234. main->lines.resize(1);
  1235. main->first_invalid_line = 0;
  1236. update();
  1237. selection.click = NULL;
  1238. selection.active = false;
  1239. current_idx = 1;
  1240. }
  1241. void RichTextLabel::set_tab_size(int p_spaces) {
  1242. tab_size = p_spaces;
  1243. main->first_invalid_line = 0;
  1244. update();
  1245. }
  1246. int RichTextLabel::get_tab_size() const {
  1247. return tab_size;
  1248. }
  1249. void RichTextLabel::set_meta_underline(bool p_underline) {
  1250. underline_meta = p_underline;
  1251. update();
  1252. }
  1253. bool RichTextLabel::is_meta_underlined() const {
  1254. return underline_meta;
  1255. }
  1256. void RichTextLabel::set_override_selected_font_color(bool p_override_selected_font_color) {
  1257. override_selected_font_color = p_override_selected_font_color;
  1258. }
  1259. bool RichTextLabel::is_overriding_selected_font_color() const {
  1260. return override_selected_font_color;
  1261. }
  1262. void RichTextLabel::set_offset(int p_pixel) {
  1263. vscroll->set_value(p_pixel);
  1264. }
  1265. void RichTextLabel::set_scroll_active(bool p_active) {
  1266. if (scroll_active == p_active)
  1267. return;
  1268. scroll_active = p_active;
  1269. update();
  1270. }
  1271. bool RichTextLabel::is_scroll_active() const {
  1272. return scroll_active;
  1273. }
  1274. void RichTextLabel::set_scroll_follow(bool p_follow) {
  1275. scroll_follow = p_follow;
  1276. if (!vscroll->is_visible_in_tree() || vscroll->get_value() >= (vscroll->get_max() - vscroll->get_page()))
  1277. scroll_following = true;
  1278. }
  1279. bool RichTextLabel::is_scroll_following() const {
  1280. return scroll_follow;
  1281. }
  1282. Error RichTextLabel::parse_bbcode(const String &p_bbcode) {
  1283. clear();
  1284. return append_bbcode(p_bbcode);
  1285. }
  1286. Error RichTextLabel::append_bbcode(const String &p_bbcode) {
  1287. int pos = 0;
  1288. List<String> tag_stack;
  1289. Ref<Font> normal_font = get_font("normal_font");
  1290. Ref<Font> bold_font = get_font("bold_font");
  1291. Ref<Font> italics_font = get_font("italics_font");
  1292. Ref<Font> bold_italics_font = get_font("bold_italics_font");
  1293. Ref<Font> mono_font = get_font("mono_font");
  1294. Color base_color = get_color("default_color");
  1295. int indent_level = 0;
  1296. bool in_bold = false;
  1297. bool in_italics = false;
  1298. while (pos < p_bbcode.length()) {
  1299. int brk_pos = p_bbcode.find("[", pos);
  1300. if (brk_pos < 0)
  1301. brk_pos = p_bbcode.length();
  1302. if (brk_pos > pos) {
  1303. add_text(p_bbcode.substr(pos, brk_pos - pos));
  1304. }
  1305. if (brk_pos == p_bbcode.length())
  1306. break; //nothing else to add
  1307. int brk_end = p_bbcode.find("]", brk_pos + 1);
  1308. if (brk_end == -1) {
  1309. //no close, add the rest
  1310. add_text(p_bbcode.substr(brk_pos, p_bbcode.length() - brk_pos));
  1311. break;
  1312. }
  1313. String tag = p_bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
  1314. if (tag.begins_with("/") && tag_stack.size()) {
  1315. bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length());
  1316. if (tag_stack.front()->get() == "b")
  1317. in_bold = false;
  1318. if (tag_stack.front()->get() == "i")
  1319. in_italics = false;
  1320. if (tag_stack.front()->get() == "indent")
  1321. indent_level--;
  1322. if (!tag_ok) {
  1323. add_text("[");
  1324. pos++;
  1325. continue;
  1326. }
  1327. tag_stack.pop_front();
  1328. pos = brk_end + 1;
  1329. if (tag != "/img")
  1330. pop();
  1331. } else if (tag == "b") {
  1332. //use bold font
  1333. in_bold = true;
  1334. if (in_italics)
  1335. push_font(bold_italics_font);
  1336. else
  1337. push_font(bold_font);
  1338. pos = brk_end + 1;
  1339. tag_stack.push_front(tag);
  1340. } else if (tag == "i") {
  1341. //use italics font
  1342. in_italics = true;
  1343. if (in_bold)
  1344. push_font(bold_italics_font);
  1345. else
  1346. push_font(italics_font);
  1347. pos = brk_end + 1;
  1348. tag_stack.push_front(tag);
  1349. } else if (tag == "code") {
  1350. //use monospace font
  1351. push_font(mono_font);
  1352. pos = brk_end + 1;
  1353. tag_stack.push_front(tag);
  1354. } else if (tag.begins_with("table=")) {
  1355. int columns = tag.substr(6, tag.length()).to_int();
  1356. if (columns < 1)
  1357. columns = 1;
  1358. push_table(columns);
  1359. pos = brk_end + 1;
  1360. tag_stack.push_front("table");
  1361. } else if (tag == "cell") {
  1362. push_cell();
  1363. pos = brk_end + 1;
  1364. tag_stack.push_front(tag);
  1365. } else if (tag.begins_with("cell=")) {
  1366. int ratio = tag.substr(5, tag.length()).to_int();
  1367. if (ratio < 1)
  1368. ratio = 1;
  1369. set_table_column_expand(get_current_table_column(), true, ratio);
  1370. push_cell();
  1371. pos = brk_end + 1;
  1372. tag_stack.push_front("cell");
  1373. } else if (tag == "u") {
  1374. //use underline
  1375. push_underline();
  1376. pos = brk_end + 1;
  1377. tag_stack.push_front(tag);
  1378. } else if (tag == "s") {
  1379. //use strikethrough
  1380. push_strikethrough();
  1381. pos = brk_end + 1;
  1382. tag_stack.push_front(tag);
  1383. } else if (tag == "center") {
  1384. push_align(ALIGN_CENTER);
  1385. pos = brk_end + 1;
  1386. tag_stack.push_front(tag);
  1387. } else if (tag == "fill") {
  1388. push_align(ALIGN_FILL);
  1389. pos = brk_end + 1;
  1390. tag_stack.push_front(tag);
  1391. } else if (tag == "right") {
  1392. push_align(ALIGN_RIGHT);
  1393. pos = brk_end + 1;
  1394. tag_stack.push_front(tag);
  1395. } else if (tag == "ul") {
  1396. push_list(LIST_DOTS);
  1397. pos = brk_end + 1;
  1398. tag_stack.push_front(tag);
  1399. } else if (tag == "ol") {
  1400. push_list(LIST_NUMBERS);
  1401. pos = brk_end + 1;
  1402. tag_stack.push_front(tag);
  1403. } else if (tag == "indent") {
  1404. indent_level++;
  1405. push_indent(indent_level);
  1406. pos = brk_end + 1;
  1407. tag_stack.push_front(tag);
  1408. } else if (tag == "url") {
  1409. int end = p_bbcode.find("[", brk_end);
  1410. if (end == -1)
  1411. end = p_bbcode.length();
  1412. String url = p_bbcode.substr(brk_end + 1, end - brk_end - 1);
  1413. push_meta(url);
  1414. pos = brk_end + 1;
  1415. tag_stack.push_front(tag);
  1416. } else if (tag.begins_with("url=")) {
  1417. String url = tag.substr(4, tag.length());
  1418. push_meta(url);
  1419. pos = brk_end + 1;
  1420. tag_stack.push_front("url");
  1421. } else if (tag == "img") {
  1422. int end = p_bbcode.find("[", brk_end);
  1423. if (end == -1)
  1424. end = p_bbcode.length();
  1425. String image = p_bbcode.substr(brk_end + 1, end - brk_end - 1);
  1426. Ref<Texture> texture = ResourceLoader::load(image, "Texture");
  1427. if (texture.is_valid())
  1428. add_image(texture);
  1429. pos = end;
  1430. tag_stack.push_front(tag);
  1431. } else if (tag.begins_with("color=")) {
  1432. String col = tag.substr(6, tag.length());
  1433. Color color;
  1434. if (col.begins_with("#"))
  1435. color = Color::html(col);
  1436. else if (col == "aqua")
  1437. color = Color::html("#00FFFF");
  1438. else if (col == "black")
  1439. color = Color::html("#000000");
  1440. else if (col == "blue")
  1441. color = Color::html("#0000FF");
  1442. else if (col == "fuchsia")
  1443. color = Color::html("#FF00FF");
  1444. else if (col == "gray" || col == "grey")
  1445. color = Color::html("#808080");
  1446. else if (col == "green")
  1447. color = Color::html("#008000");
  1448. else if (col == "lime")
  1449. color = Color::html("#00FF00");
  1450. else if (col == "maroon")
  1451. color = Color::html("#800000");
  1452. else if (col == "navy")
  1453. color = Color::html("#000080");
  1454. else if (col == "olive")
  1455. color = Color::html("#808000");
  1456. else if (col == "purple")
  1457. color = Color::html("#800080");
  1458. else if (col == "red")
  1459. color = Color::html("#FF0000");
  1460. else if (col == "silver")
  1461. color = Color::html("#C0C0C0");
  1462. else if (col == "teal")
  1463. color = Color::html("#008008");
  1464. else if (col == "white")
  1465. color = Color::html("#FFFFFF");
  1466. else if (col == "yellow")
  1467. color = Color::html("#FFFF00");
  1468. else
  1469. color = base_color;
  1470. push_color(color);
  1471. pos = brk_end + 1;
  1472. tag_stack.push_front("color");
  1473. } else if (tag.begins_with("font=")) {
  1474. String fnt = tag.substr(5, tag.length());
  1475. Ref<Font> font = ResourceLoader::load(fnt, "Font");
  1476. if (font.is_valid())
  1477. push_font(font);
  1478. else
  1479. push_font(normal_font);
  1480. pos = brk_end + 1;
  1481. tag_stack.push_front("font");
  1482. } else {
  1483. add_text("["); //ignore
  1484. pos = brk_pos + 1;
  1485. }
  1486. }
  1487. return OK;
  1488. }
  1489. void RichTextLabel::scroll_to_line(int p_line) {
  1490. ERR_FAIL_INDEX(p_line, main->lines.size());
  1491. _validate_line_caches(main);
  1492. vscroll->set_value(main->lines[p_line].height_accum_cache - main->lines[p_line].height_cache);
  1493. }
  1494. int RichTextLabel::get_line_count() const {
  1495. return current_frame->lines.size();
  1496. }
  1497. int RichTextLabel::get_visible_line_count() const {
  1498. if (!is_visible())
  1499. return 0;
  1500. return visible_line_count;
  1501. }
  1502. void RichTextLabel::set_selection_enabled(bool p_enabled) {
  1503. selection.enabled = p_enabled;
  1504. if (!p_enabled) {
  1505. if (selection.active) {
  1506. selection.active = false;
  1507. update();
  1508. }
  1509. set_focus_mode(FOCUS_NONE);
  1510. } else {
  1511. set_focus_mode(FOCUS_ALL);
  1512. }
  1513. }
  1514. bool RichTextLabel::search(const String &p_string, bool p_from_selection, bool p_search_previous) {
  1515. ERR_FAIL_COND_V(!selection.enabled, false);
  1516. Item *it = main;
  1517. int charidx = 0;
  1518. if (p_from_selection && selection.active && selection.enabled) {
  1519. it = selection.to;
  1520. charidx = selection.to_char + 1;
  1521. }
  1522. while (it) {
  1523. if (it->type == ITEM_TEXT) {
  1524. ItemText *t = static_cast<ItemText *>(it);
  1525. int sp = t->text.find(p_string, charidx);
  1526. if (sp != -1) {
  1527. selection.from = it;
  1528. selection.from_char = sp;
  1529. selection.to = it;
  1530. selection.to_char = sp + p_string.length() - 1;
  1531. selection.active = true;
  1532. update();
  1533. _validate_line_caches(main);
  1534. int fh = _find_font(t).is_valid() ? _find_font(t)->get_height() : get_font("normal_font")->get_height();
  1535. float offset = 0;
  1536. int line = t->line;
  1537. Item *item = t;
  1538. while (item) {
  1539. if (item->type == ITEM_FRAME) {
  1540. ItemFrame *frame = static_cast<ItemFrame *>(item);
  1541. if (line >= 0 && line < frame->lines.size()) {
  1542. offset += frame->lines[line].height_accum_cache - frame->lines[line].height_cache;
  1543. line = frame->line;
  1544. }
  1545. }
  1546. item = item->parent;
  1547. }
  1548. vscroll->set_value(offset - fh);
  1549. return true;
  1550. }
  1551. }
  1552. if (p_search_previous)
  1553. it = _get_prev_item(it, true);
  1554. else
  1555. it = _get_next_item(it, true);
  1556. charidx = 0;
  1557. }
  1558. return false;
  1559. }
  1560. void RichTextLabel::selection_copy() {
  1561. if (!selection.active || !selection.enabled)
  1562. return;
  1563. String text;
  1564. RichTextLabel::Item *item = selection.from;
  1565. while (item) {
  1566. if (item->type == ITEM_TEXT) {
  1567. String itext = static_cast<ItemText *>(item)->text;
  1568. if (item == selection.from && item == selection.to) {
  1569. text += itext.substr(selection.from_char, selection.to_char - selection.from_char + 1);
  1570. } else if (item == selection.from) {
  1571. text += itext.substr(selection.from_char, itext.size());
  1572. } else if (item == selection.to) {
  1573. text += itext.substr(0, selection.to_char + 1);
  1574. } else {
  1575. text += itext;
  1576. }
  1577. } else if (item->type == ITEM_NEWLINE) {
  1578. text += "\n";
  1579. }
  1580. if (item == selection.to)
  1581. break;
  1582. item = _get_next_item(item, true);
  1583. }
  1584. if (text != "") {
  1585. OS::get_singleton()->set_clipboard(text);
  1586. }
  1587. }
  1588. bool RichTextLabel::is_selection_enabled() const {
  1589. return selection.enabled;
  1590. }
  1591. void RichTextLabel::set_bbcode(const String &p_bbcode) {
  1592. bbcode = p_bbcode;
  1593. if (is_inside_tree() && use_bbcode)
  1594. parse_bbcode(p_bbcode);
  1595. else { // raw text
  1596. clear();
  1597. add_text(p_bbcode);
  1598. }
  1599. }
  1600. String RichTextLabel::get_bbcode() const {
  1601. return bbcode;
  1602. }
  1603. void RichTextLabel::set_use_bbcode(bool p_enable) {
  1604. if (use_bbcode == p_enable)
  1605. return;
  1606. use_bbcode = p_enable;
  1607. set_bbcode(bbcode);
  1608. }
  1609. bool RichTextLabel::is_using_bbcode() const {
  1610. return use_bbcode;
  1611. }
  1612. String RichTextLabel::get_text() {
  1613. String text = "";
  1614. Item *it = main;
  1615. while (it) {
  1616. if (it->type == ITEM_TEXT) {
  1617. ItemText *t = static_cast<ItemText *>(it);
  1618. text += t->text;
  1619. } else if (it->type == ITEM_NEWLINE) {
  1620. text += "\n";
  1621. } else if (it->type == ITEM_INDENT) {
  1622. text += "\t";
  1623. }
  1624. it = _get_next_item(it, true);
  1625. }
  1626. return text;
  1627. }
  1628. void RichTextLabel::set_text(const String &p_string) {
  1629. clear();
  1630. add_text(p_string);
  1631. }
  1632. void RichTextLabel::set_percent_visible(float p_percent) {
  1633. if (p_percent < 0 || p_percent >= 1) {
  1634. visible_characters = -1;
  1635. percent_visible = 1;
  1636. } else {
  1637. visible_characters = get_total_character_count() * p_percent;
  1638. percent_visible = p_percent;
  1639. }
  1640. update();
  1641. }
  1642. float RichTextLabel::get_percent_visible() const {
  1643. return percent_visible;
  1644. }
  1645. int RichTextLabel::get_content_height() {
  1646. int total_height = 0;
  1647. if (main->lines.size())
  1648. total_height = main->lines[main->lines.size() - 1].height_accum_cache + get_stylebox("normal")->get_minimum_size().height;
  1649. return total_height;
  1650. }
  1651. void RichTextLabel::_bind_methods() {
  1652. ClassDB::bind_method(D_METHOD("_gui_input"), &RichTextLabel::_gui_input);
  1653. ClassDB::bind_method(D_METHOD("_scroll_changed"), &RichTextLabel::_scroll_changed);
  1654. ClassDB::bind_method(D_METHOD("get_text"), &RichTextLabel::get_text);
  1655. ClassDB::bind_method(D_METHOD("add_text", "text"), &RichTextLabel::add_text);
  1656. ClassDB::bind_method(D_METHOD("set_text", "text"), &RichTextLabel::set_text);
  1657. ClassDB::bind_method(D_METHOD("add_image", "image"), &RichTextLabel::add_image);
  1658. ClassDB::bind_method(D_METHOD("newline"), &RichTextLabel::add_newline);
  1659. ClassDB::bind_method(D_METHOD("remove_line", "line"), &RichTextLabel::remove_line);
  1660. ClassDB::bind_method(D_METHOD("push_font", "font"), &RichTextLabel::push_font);
  1661. ClassDB::bind_method(D_METHOD("push_color", "color"), &RichTextLabel::push_color);
  1662. ClassDB::bind_method(D_METHOD("push_align", "align"), &RichTextLabel::push_align);
  1663. ClassDB::bind_method(D_METHOD("push_indent", "level"), &RichTextLabel::push_indent);
  1664. ClassDB::bind_method(D_METHOD("push_list", "type"), &RichTextLabel::push_list);
  1665. ClassDB::bind_method(D_METHOD("push_meta", "data"), &RichTextLabel::push_meta);
  1666. ClassDB::bind_method(D_METHOD("push_underline"), &RichTextLabel::push_underline);
  1667. ClassDB::bind_method(D_METHOD("push_strikethrough"), &RichTextLabel::push_strikethrough);
  1668. ClassDB::bind_method(D_METHOD("push_table", "columns"), &RichTextLabel::push_table);
  1669. ClassDB::bind_method(D_METHOD("set_table_column_expand", "column", "expand", "ratio"), &RichTextLabel::set_table_column_expand);
  1670. ClassDB::bind_method(D_METHOD("push_cell"), &RichTextLabel::push_cell);
  1671. ClassDB::bind_method(D_METHOD("pop"), &RichTextLabel::pop);
  1672. ClassDB::bind_method(D_METHOD("clear"), &RichTextLabel::clear);
  1673. ClassDB::bind_method(D_METHOD("set_meta_underline", "enable"), &RichTextLabel::set_meta_underline);
  1674. ClassDB::bind_method(D_METHOD("is_meta_underlined"), &RichTextLabel::is_meta_underlined);
  1675. ClassDB::bind_method(D_METHOD("set_override_selected_font_color", "override"), &RichTextLabel::set_override_selected_font_color);
  1676. ClassDB::bind_method(D_METHOD("is_overriding_selected_font_color"), &RichTextLabel::is_overriding_selected_font_color);
  1677. ClassDB::bind_method(D_METHOD("set_scroll_active", "active"), &RichTextLabel::set_scroll_active);
  1678. ClassDB::bind_method(D_METHOD("is_scroll_active"), &RichTextLabel::is_scroll_active);
  1679. ClassDB::bind_method(D_METHOD("set_scroll_follow", "follow"), &RichTextLabel::set_scroll_follow);
  1680. ClassDB::bind_method(D_METHOD("is_scroll_following"), &RichTextLabel::is_scroll_following);
  1681. ClassDB::bind_method(D_METHOD("get_v_scroll"), &RichTextLabel::get_v_scroll);
  1682. ClassDB::bind_method(D_METHOD("scroll_to_line", "line"), &RichTextLabel::scroll_to_line);
  1683. ClassDB::bind_method(D_METHOD("set_tab_size", "spaces"), &RichTextLabel::set_tab_size);
  1684. ClassDB::bind_method(D_METHOD("get_tab_size"), &RichTextLabel::get_tab_size);
  1685. ClassDB::bind_method(D_METHOD("set_selection_enabled", "enabled"), &RichTextLabel::set_selection_enabled);
  1686. ClassDB::bind_method(D_METHOD("is_selection_enabled"), &RichTextLabel::is_selection_enabled);
  1687. ClassDB::bind_method(D_METHOD("parse_bbcode", "bbcode"), &RichTextLabel::parse_bbcode);
  1688. ClassDB::bind_method(D_METHOD("append_bbcode", "bbcode"), &RichTextLabel::append_bbcode);
  1689. ClassDB::bind_method(D_METHOD("set_bbcode", "text"), &RichTextLabel::set_bbcode);
  1690. ClassDB::bind_method(D_METHOD("get_bbcode"), &RichTextLabel::get_bbcode);
  1691. ClassDB::bind_method(D_METHOD("set_visible_characters", "amount"), &RichTextLabel::set_visible_characters);
  1692. ClassDB::bind_method(D_METHOD("get_visible_characters"), &RichTextLabel::get_visible_characters);
  1693. ClassDB::bind_method(D_METHOD("set_percent_visible", "percent_visible"), &RichTextLabel::set_percent_visible);
  1694. ClassDB::bind_method(D_METHOD("get_percent_visible"), &RichTextLabel::get_percent_visible);
  1695. ClassDB::bind_method(D_METHOD("get_total_character_count"), &RichTextLabel::get_total_character_count);
  1696. ClassDB::bind_method(D_METHOD("set_use_bbcode", "enable"), &RichTextLabel::set_use_bbcode);
  1697. ClassDB::bind_method(D_METHOD("is_using_bbcode"), &RichTextLabel::is_using_bbcode);
  1698. ClassDB::bind_method(D_METHOD("get_line_count"), &RichTextLabel::get_line_count);
  1699. ClassDB::bind_method(D_METHOD("get_visible_line_count"), &RichTextLabel::get_visible_line_count);
  1700. ClassDB::bind_method(D_METHOD("get_content_height"), &RichTextLabel::get_content_height);
  1701. ADD_GROUP("BBCode", "bbcode_");
  1702. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bbcode_enabled"), "set_use_bbcode", "is_using_bbcode");
  1703. ADD_PROPERTY(PropertyInfo(Variant::STRING, "bbcode_text", PROPERTY_HINT_MULTILINE_TEXT), "set_bbcode", "get_bbcode");
  1704. ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_characters", PROPERTY_HINT_RANGE, "-1,128000,1"), "set_visible_characters", "get_visible_characters");
  1705. ADD_PROPERTY(PropertyInfo(Variant::REAL, "percent_visible", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_percent_visible", "get_percent_visible");
  1706. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meta_underlined"), "set_meta_underline", "is_meta_underlined");
  1707. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_size", PROPERTY_HINT_RANGE, "0,24,1"), "set_tab_size", "get_tab_size");
  1708. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  1709. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_active"), "set_scroll_active", "is_scroll_active");
  1710. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_following"), "set_scroll_follow", "is_scroll_following");
  1711. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selection_enabled"), "set_selection_enabled", "is_selection_enabled");
  1712. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_selected_font_color"), "set_override_selected_font_color", "is_overriding_selected_font_color");
  1713. ADD_SIGNAL(MethodInfo("meta_clicked", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1714. ADD_SIGNAL(MethodInfo("meta_hover_started", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1715. ADD_SIGNAL(MethodInfo("meta_hover_ended", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1716. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  1717. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  1718. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  1719. BIND_ENUM_CONSTANT(ALIGN_FILL);
  1720. BIND_ENUM_CONSTANT(LIST_NUMBERS);
  1721. BIND_ENUM_CONSTANT(LIST_LETTERS);
  1722. BIND_ENUM_CONSTANT(LIST_DOTS);
  1723. BIND_ENUM_CONSTANT(ITEM_FRAME);
  1724. BIND_ENUM_CONSTANT(ITEM_TEXT);
  1725. BIND_ENUM_CONSTANT(ITEM_IMAGE);
  1726. BIND_ENUM_CONSTANT(ITEM_NEWLINE);
  1727. BIND_ENUM_CONSTANT(ITEM_FONT);
  1728. BIND_ENUM_CONSTANT(ITEM_COLOR);
  1729. BIND_ENUM_CONSTANT(ITEM_UNDERLINE);
  1730. BIND_ENUM_CONSTANT(ITEM_STRIKETHROUGH);
  1731. BIND_ENUM_CONSTANT(ITEM_ALIGN);
  1732. BIND_ENUM_CONSTANT(ITEM_INDENT);
  1733. BIND_ENUM_CONSTANT(ITEM_LIST);
  1734. BIND_ENUM_CONSTANT(ITEM_TABLE);
  1735. BIND_ENUM_CONSTANT(ITEM_META);
  1736. }
  1737. void RichTextLabel::set_visible_characters(int p_visible) {
  1738. visible_characters = p_visible;
  1739. update();
  1740. }
  1741. int RichTextLabel::get_visible_characters() const {
  1742. return visible_characters;
  1743. }
  1744. int RichTextLabel::get_total_character_count() const {
  1745. int tc = 0;
  1746. for (int i = 0; i < current_frame->lines.size(); i++)
  1747. tc += current_frame->lines[i].char_count;
  1748. return tc;
  1749. }
  1750. void RichTextLabel::set_fixed_size_to_width(int p_width) {
  1751. fixed_width = p_width;
  1752. minimum_size_changed();
  1753. }
  1754. Size2 RichTextLabel::get_minimum_size() const {
  1755. if (fixed_width != -1) {
  1756. const_cast<RichTextLabel *>(this)->_validate_line_caches(main);
  1757. return Size2(fixed_width, const_cast<RichTextLabel *>(this)->get_content_height());
  1758. }
  1759. return Size2();
  1760. }
  1761. RichTextLabel::RichTextLabel() {
  1762. main = memnew(ItemFrame);
  1763. main->index = 0;
  1764. current = main;
  1765. main->lines.resize(1);
  1766. main->lines.write[0].from = main;
  1767. main->first_invalid_line = 0;
  1768. current_frame = main;
  1769. tab_size = 4;
  1770. default_align = ALIGN_LEFT;
  1771. underline_meta = true;
  1772. override_selected_font_color = false;
  1773. scroll_visible = false;
  1774. scroll_follow = false;
  1775. scroll_following = false;
  1776. updating_scroll = false;
  1777. scroll_active = true;
  1778. scroll_w = 0;
  1779. scroll_updated = false;
  1780. vscroll = memnew(VScrollBar);
  1781. add_child(vscroll);
  1782. vscroll->set_drag_node(String(".."));
  1783. vscroll->set_step(1);
  1784. vscroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0);
  1785. vscroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  1786. vscroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  1787. vscroll->connect("value_changed", this, "_scroll_changed");
  1788. vscroll->set_step(1);
  1789. vscroll->hide();
  1790. current_idx = 1;
  1791. use_bbcode = false;
  1792. selection.click = NULL;
  1793. selection.active = false;
  1794. selection.enabled = false;
  1795. visible_characters = -1;
  1796. percent_visible = 1;
  1797. visible_line_count = 0;
  1798. fixed_width = -1;
  1799. set_clip_contents(true);
  1800. }
  1801. RichTextLabel::~RichTextLabel() {
  1802. memdelete(main);
  1803. }