2
0

rich_text_label.cpp 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  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_ofs2(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_ofs2);
  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_ofs2);
  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_ofs2);
  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_ofs2, 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. ItemMeta *item_meta;
  886. if (item && !outside && _find_meta(item, &meta, &item_meta)) {
  887. if (meta_hovering != item_meta) {
  888. if (meta_hovering) {
  889. emit_signal("meta_hover_ended", current_meta);
  890. }
  891. meta_hovering = item_meta;
  892. current_meta = meta;
  893. emit_signal("meta_hover_started", meta);
  894. }
  895. } else if (meta_hovering) {
  896. emit_signal("meta_hover_ended", current_meta);
  897. meta_hovering = NULL;
  898. current_meta = false;
  899. }
  900. }
  901. }
  902. Ref<Font> RichTextLabel::_find_font(Item *p_item) {
  903. Item *fontitem = p_item;
  904. while (fontitem) {
  905. if (fontitem->type == ITEM_FONT) {
  906. ItemFont *fi = static_cast<ItemFont *>(fontitem);
  907. return fi->font;
  908. }
  909. fontitem = fontitem->parent;
  910. }
  911. return Ref<Font>();
  912. }
  913. int RichTextLabel::_find_margin(Item *p_item, const Ref<Font> &p_base_font) {
  914. Item *item = p_item;
  915. int margin = 0;
  916. while (item) {
  917. if (item->type == ITEM_INDENT) {
  918. Ref<Font> font = _find_font(item);
  919. if (font.is_null())
  920. font = p_base_font;
  921. ItemIndent *indent = static_cast<ItemIndent *>(item);
  922. margin += indent->level * tab_size * font->get_char_size(' ').width;
  923. } else if (item->type == ITEM_LIST) {
  924. Ref<Font> font = _find_font(item);
  925. if (font.is_null())
  926. font = p_base_font;
  927. }
  928. item = item->parent;
  929. }
  930. return margin;
  931. }
  932. RichTextLabel::Align RichTextLabel::_find_align(Item *p_item) {
  933. Item *item = p_item;
  934. while (item) {
  935. if (item->type == ITEM_ALIGN) {
  936. ItemAlign *align = static_cast<ItemAlign *>(item);
  937. return align->align;
  938. }
  939. item = item->parent;
  940. }
  941. return default_align;
  942. }
  943. Color RichTextLabel::_find_color(Item *p_item, const Color &p_default_color) {
  944. Item *item = p_item;
  945. while (item) {
  946. if (item->type == ITEM_COLOR) {
  947. ItemColor *color = static_cast<ItemColor *>(item);
  948. return color->color;
  949. }
  950. item = item->parent;
  951. }
  952. return p_default_color;
  953. }
  954. bool RichTextLabel::_find_underline(Item *p_item) {
  955. Item *item = p_item;
  956. while (item) {
  957. if (item->type == ITEM_UNDERLINE) {
  958. return true;
  959. }
  960. item = item->parent;
  961. }
  962. return false;
  963. }
  964. bool RichTextLabel::_find_strikethrough(Item *p_item) {
  965. Item *item = p_item;
  966. while (item) {
  967. if (item->type == ITEM_STRIKETHROUGH) {
  968. return true;
  969. }
  970. item = item->parent;
  971. }
  972. return false;
  973. }
  974. bool RichTextLabel::_find_meta(Item *p_item, Variant *r_meta, ItemMeta **r_item) {
  975. Item *item = p_item;
  976. while (item) {
  977. if (item->type == ITEM_META) {
  978. ItemMeta *meta = static_cast<ItemMeta *>(item);
  979. if (r_meta)
  980. *r_meta = meta->meta;
  981. if (r_item)
  982. *r_item = meta;
  983. return true;
  984. }
  985. item = item->parent;
  986. }
  987. return false;
  988. }
  989. void RichTextLabel::_validate_line_caches(ItemFrame *p_frame) {
  990. if (p_frame->first_invalid_line == p_frame->lines.size())
  991. return;
  992. //validate invalid lines
  993. Size2 size = get_size();
  994. if (fixed_width != -1) {
  995. size.width = fixed_width;
  996. }
  997. Rect2 text_rect = _get_text_rect();
  998. Color font_color_shadow = get_color("font_color_shadow");
  999. bool use_outline = get_constant("shadow_as_outline");
  1000. Point2 shadow_ofs(get_constant("shadow_offset_x"), get_constant("shadow_offset_y"));
  1001. Ref<Font> base_font = get_font("normal_font");
  1002. for (int i = p_frame->first_invalid_line; i < p_frame->lines.size(); i++) {
  1003. int y = 0;
  1004. _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);
  1005. p_frame->lines.write[i].height_cache = y;
  1006. p_frame->lines.write[i].height_accum_cache = y;
  1007. if (i > 0)
  1008. p_frame->lines.write[i].height_accum_cache += p_frame->lines[i - 1].height_accum_cache;
  1009. }
  1010. int total_height = 0;
  1011. if (p_frame->lines.size())
  1012. total_height = p_frame->lines[p_frame->lines.size() - 1].height_accum_cache + get_stylebox("normal")->get_minimum_size().height;
  1013. main->first_invalid_line = p_frame->lines.size();
  1014. updating_scroll = true;
  1015. vscroll->set_max(total_height);
  1016. vscroll->set_page(size.height);
  1017. if (scroll_follow && scroll_following)
  1018. vscroll->set_value(total_height - size.height);
  1019. updating_scroll = false;
  1020. }
  1021. void RichTextLabel::_invalidate_current_line(ItemFrame *p_frame) {
  1022. if (p_frame->lines.size() - 1 <= p_frame->first_invalid_line) {
  1023. p_frame->first_invalid_line = p_frame->lines.size() - 1;
  1024. update();
  1025. }
  1026. }
  1027. void RichTextLabel::add_text(const String &p_text) {
  1028. if (current->type == ITEM_TABLE)
  1029. return; //can't add anything here
  1030. int pos = 0;
  1031. while (pos < p_text.length()) {
  1032. int end = p_text.find("\n", pos);
  1033. String line;
  1034. bool eol = false;
  1035. if (end == -1) {
  1036. end = p_text.length();
  1037. } else {
  1038. eol = true;
  1039. }
  1040. if (pos == 0 && end == p_text.length())
  1041. line = p_text;
  1042. else
  1043. line = p_text.substr(pos, end - pos);
  1044. if (line.length() > 0) {
  1045. if (current->subitems.size() && current->subitems.back()->get()->type == ITEM_TEXT) {
  1046. //append text condition!
  1047. ItemText *ti = static_cast<ItemText *>(current->subitems.back()->get());
  1048. ti->text += line;
  1049. _invalidate_current_line(main);
  1050. } else {
  1051. //append item condition
  1052. ItemText *item = memnew(ItemText);
  1053. item->text = line;
  1054. _add_item(item, false);
  1055. }
  1056. }
  1057. if (eol) {
  1058. ItemNewline *item = memnew(ItemNewline);
  1059. item->line = current_frame->lines.size();
  1060. _add_item(item, false);
  1061. current_frame->lines.resize(current_frame->lines.size() + 1);
  1062. if (item->type != ITEM_NEWLINE)
  1063. current_frame->lines.write[current_frame->lines.size() - 1].from = item;
  1064. _invalidate_current_line(current_frame);
  1065. }
  1066. pos = end + 1;
  1067. }
  1068. }
  1069. void RichTextLabel::_add_item(Item *p_item, bool p_enter, bool p_ensure_newline) {
  1070. p_item->parent = current;
  1071. p_item->E = current->subitems.push_back(p_item);
  1072. p_item->index = current_idx++;
  1073. if (p_enter)
  1074. current = p_item;
  1075. if (p_ensure_newline && current_frame->lines[current_frame->lines.size() - 1].from) {
  1076. _invalidate_current_line(current_frame);
  1077. current_frame->lines.resize(current_frame->lines.size() + 1);
  1078. }
  1079. if (current_frame->lines[current_frame->lines.size() - 1].from == NULL) {
  1080. current_frame->lines.write[current_frame->lines.size() - 1].from = p_item;
  1081. }
  1082. p_item->line = current_frame->lines.size() - 1;
  1083. _invalidate_current_line(current_frame);
  1084. }
  1085. void RichTextLabel::_remove_item(Item *p_item, const int p_line, const int p_subitem_line) {
  1086. int size = p_item->subitems.size();
  1087. if (size == 0) {
  1088. p_item->parent->subitems.erase(p_item);
  1089. if (p_item->type == ITEM_NEWLINE) {
  1090. current_frame->lines.remove(p_line);
  1091. for (int i = p_subitem_line; i < current->subitems.size(); i++) {
  1092. if (current->subitems[i]->line > 0)
  1093. current->subitems[i]->line--;
  1094. }
  1095. }
  1096. } else {
  1097. for (int i = 0; i < size; i++) {
  1098. _remove_item(p_item->subitems.front()->get(), p_line, p_subitem_line);
  1099. }
  1100. }
  1101. }
  1102. void RichTextLabel::add_image(const Ref<Texture> &p_image) {
  1103. if (current->type == ITEM_TABLE)
  1104. return;
  1105. ERR_FAIL_COND(p_image.is_null());
  1106. ItemImage *item = memnew(ItemImage);
  1107. item->image = p_image;
  1108. _add_item(item, false);
  1109. }
  1110. void RichTextLabel::add_newline() {
  1111. if (current->type == ITEM_TABLE)
  1112. return;
  1113. ItemNewline *item = memnew(ItemNewline);
  1114. item->line = current_frame->lines.size();
  1115. _add_item(item, false);
  1116. current_frame->lines.resize(current_frame->lines.size() + 1);
  1117. _invalidate_current_line(current_frame);
  1118. }
  1119. bool RichTextLabel::remove_line(const int p_line) {
  1120. if (p_line >= current_frame->lines.size() || p_line < 0)
  1121. return false;
  1122. int i = 0;
  1123. while (i < current->subitems.size() && current->subitems[i]->line < p_line) {
  1124. i++;
  1125. }
  1126. bool was_newline = false;
  1127. while (i < current->subitems.size()) {
  1128. was_newline = current->subitems[i]->type == ITEM_NEWLINE;
  1129. _remove_item(current->subitems[i], current->subitems[i]->line, p_line);
  1130. if (was_newline)
  1131. break;
  1132. }
  1133. if (!was_newline) {
  1134. current_frame->lines.remove(p_line);
  1135. }
  1136. if (p_line == 0 && current->subitems.size() > 0)
  1137. main->lines.write[0].from = main;
  1138. main->first_invalid_line = 0;
  1139. return true;
  1140. }
  1141. void RichTextLabel::push_font(const Ref<Font> &p_font) {
  1142. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1143. ERR_FAIL_COND(p_font.is_null());
  1144. ItemFont *item = memnew(ItemFont);
  1145. item->font = p_font;
  1146. _add_item(item, true);
  1147. }
  1148. void RichTextLabel::push_color(const Color &p_color) {
  1149. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1150. ItemColor *item = memnew(ItemColor);
  1151. item->color = p_color;
  1152. _add_item(item, true);
  1153. }
  1154. void RichTextLabel::push_underline() {
  1155. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1156. ItemUnderline *item = memnew(ItemUnderline);
  1157. _add_item(item, true);
  1158. }
  1159. void RichTextLabel::push_strikethrough() {
  1160. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1161. ItemStrikethrough *item = memnew(ItemStrikethrough);
  1162. _add_item(item, true);
  1163. }
  1164. void RichTextLabel::push_align(Align p_align) {
  1165. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1166. ItemAlign *item = memnew(ItemAlign);
  1167. item->align = p_align;
  1168. _add_item(item, true, true);
  1169. }
  1170. void RichTextLabel::push_indent(int p_level) {
  1171. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1172. ERR_FAIL_COND(p_level < 0);
  1173. ItemIndent *item = memnew(ItemIndent);
  1174. item->level = p_level;
  1175. _add_item(item, true, true);
  1176. }
  1177. void RichTextLabel::push_list(ListType p_list) {
  1178. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1179. ERR_FAIL_INDEX(p_list, 3);
  1180. ItemList *item = memnew(ItemList);
  1181. item->list_type = p_list;
  1182. _add_item(item, true, true);
  1183. }
  1184. void RichTextLabel::push_meta(const Variant &p_meta) {
  1185. ERR_FAIL_COND(current->type == ITEM_TABLE);
  1186. ItemMeta *item = memnew(ItemMeta);
  1187. item->meta = p_meta;
  1188. _add_item(item, true);
  1189. }
  1190. void RichTextLabel::push_table(int p_columns) {
  1191. ERR_FAIL_COND(p_columns < 1);
  1192. ItemTable *item = memnew(ItemTable);
  1193. item->columns.resize(p_columns);
  1194. item->total_width = 0;
  1195. for (int i = 0; i < item->columns.size(); i++) {
  1196. item->columns.write[i].expand = false;
  1197. item->columns.write[i].expand_ratio = 1;
  1198. }
  1199. _add_item(item, true, true);
  1200. }
  1201. void RichTextLabel::set_table_column_expand(int p_column, bool p_expand, int p_ratio) {
  1202. ERR_FAIL_COND(current->type != ITEM_TABLE);
  1203. ItemTable *table = static_cast<ItemTable *>(current);
  1204. ERR_FAIL_INDEX(p_column, table->columns.size());
  1205. table->columns.write[p_column].expand = p_expand;
  1206. table->columns.write[p_column].expand_ratio = p_ratio;
  1207. }
  1208. void RichTextLabel::push_cell() {
  1209. ERR_FAIL_COND(current->type != ITEM_TABLE);
  1210. ItemFrame *item = memnew(ItemFrame);
  1211. item->parent_frame = current_frame;
  1212. _add_item(item, true);
  1213. current_frame = item;
  1214. item->cell = true;
  1215. item->parent_line = item->parent_frame->lines.size() - 1;
  1216. item->lines.resize(1);
  1217. item->lines.write[0].from = NULL;
  1218. item->first_invalid_line = 0;
  1219. }
  1220. int RichTextLabel::get_current_table_column() const {
  1221. ERR_FAIL_COND_V(current->type != ITEM_TABLE, -1);
  1222. ItemTable *table = static_cast<ItemTable *>(current);
  1223. return table->subitems.size() % table->columns.size();
  1224. }
  1225. void RichTextLabel::pop() {
  1226. ERR_FAIL_COND(!current->parent);
  1227. if (current->type == ITEM_FRAME) {
  1228. current_frame = static_cast<ItemFrame *>(current)->parent_frame;
  1229. }
  1230. current = current->parent;
  1231. }
  1232. void RichTextLabel::clear() {
  1233. main->_clear_children();
  1234. current = main;
  1235. current_frame = main;
  1236. main->lines.clear();
  1237. main->lines.resize(1);
  1238. main->first_invalid_line = 0;
  1239. update();
  1240. selection.click = NULL;
  1241. selection.active = false;
  1242. current_idx = 1;
  1243. }
  1244. void RichTextLabel::set_tab_size(int p_spaces) {
  1245. tab_size = p_spaces;
  1246. main->first_invalid_line = 0;
  1247. update();
  1248. }
  1249. int RichTextLabel::get_tab_size() const {
  1250. return tab_size;
  1251. }
  1252. void RichTextLabel::set_meta_underline(bool p_underline) {
  1253. underline_meta = p_underline;
  1254. update();
  1255. }
  1256. bool RichTextLabel::is_meta_underlined() const {
  1257. return underline_meta;
  1258. }
  1259. void RichTextLabel::set_override_selected_font_color(bool p_override_selected_font_color) {
  1260. override_selected_font_color = p_override_selected_font_color;
  1261. }
  1262. bool RichTextLabel::is_overriding_selected_font_color() const {
  1263. return override_selected_font_color;
  1264. }
  1265. void RichTextLabel::set_offset(int p_pixel) {
  1266. vscroll->set_value(p_pixel);
  1267. }
  1268. void RichTextLabel::set_scroll_active(bool p_active) {
  1269. if (scroll_active == p_active)
  1270. return;
  1271. scroll_active = p_active;
  1272. update();
  1273. }
  1274. bool RichTextLabel::is_scroll_active() const {
  1275. return scroll_active;
  1276. }
  1277. void RichTextLabel::set_scroll_follow(bool p_follow) {
  1278. scroll_follow = p_follow;
  1279. if (!vscroll->is_visible_in_tree() || vscroll->get_value() >= (vscroll->get_max() - vscroll->get_page()))
  1280. scroll_following = true;
  1281. }
  1282. bool RichTextLabel::is_scroll_following() const {
  1283. return scroll_follow;
  1284. }
  1285. Error RichTextLabel::parse_bbcode(const String &p_bbcode) {
  1286. clear();
  1287. return append_bbcode(p_bbcode);
  1288. }
  1289. Error RichTextLabel::append_bbcode(const String &p_bbcode) {
  1290. int pos = 0;
  1291. List<String> tag_stack;
  1292. Ref<Font> normal_font = get_font("normal_font");
  1293. Ref<Font> bold_font = get_font("bold_font");
  1294. Ref<Font> italics_font = get_font("italics_font");
  1295. Ref<Font> bold_italics_font = get_font("bold_italics_font");
  1296. Ref<Font> mono_font = get_font("mono_font");
  1297. Color base_color = get_color("default_color");
  1298. int indent_level = 0;
  1299. bool in_bold = false;
  1300. bool in_italics = false;
  1301. while (pos < p_bbcode.length()) {
  1302. int brk_pos = p_bbcode.find("[", pos);
  1303. if (brk_pos < 0)
  1304. brk_pos = p_bbcode.length();
  1305. if (brk_pos > pos) {
  1306. add_text(p_bbcode.substr(pos, brk_pos - pos));
  1307. }
  1308. if (brk_pos == p_bbcode.length())
  1309. break; //nothing else to add
  1310. int brk_end = p_bbcode.find("]", brk_pos + 1);
  1311. if (brk_end == -1) {
  1312. //no close, add the rest
  1313. add_text(p_bbcode.substr(brk_pos, p_bbcode.length() - brk_pos));
  1314. break;
  1315. }
  1316. String tag = p_bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
  1317. if (tag.begins_with("/") && tag_stack.size()) {
  1318. bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length());
  1319. if (tag_stack.front()->get() == "b")
  1320. in_bold = false;
  1321. if (tag_stack.front()->get() == "i")
  1322. in_italics = false;
  1323. if (tag_stack.front()->get() == "indent")
  1324. indent_level--;
  1325. if (!tag_ok) {
  1326. add_text("[");
  1327. pos++;
  1328. continue;
  1329. }
  1330. tag_stack.pop_front();
  1331. pos = brk_end + 1;
  1332. if (tag != "/img")
  1333. pop();
  1334. } else if (tag == "b") {
  1335. //use bold font
  1336. in_bold = true;
  1337. if (in_italics)
  1338. push_font(bold_italics_font);
  1339. else
  1340. push_font(bold_font);
  1341. pos = brk_end + 1;
  1342. tag_stack.push_front(tag);
  1343. } else if (tag == "i") {
  1344. //use italics font
  1345. in_italics = true;
  1346. if (in_bold)
  1347. push_font(bold_italics_font);
  1348. else
  1349. push_font(italics_font);
  1350. pos = brk_end + 1;
  1351. tag_stack.push_front(tag);
  1352. } else if (tag == "code") {
  1353. //use monospace font
  1354. push_font(mono_font);
  1355. pos = brk_end + 1;
  1356. tag_stack.push_front(tag);
  1357. } else if (tag.begins_with("table=")) {
  1358. int columns = tag.substr(6, tag.length()).to_int();
  1359. if (columns < 1)
  1360. columns = 1;
  1361. push_table(columns);
  1362. pos = brk_end + 1;
  1363. tag_stack.push_front("table");
  1364. } else if (tag == "cell") {
  1365. push_cell();
  1366. pos = brk_end + 1;
  1367. tag_stack.push_front(tag);
  1368. } else if (tag.begins_with("cell=")) {
  1369. int ratio = tag.substr(5, tag.length()).to_int();
  1370. if (ratio < 1)
  1371. ratio = 1;
  1372. set_table_column_expand(get_current_table_column(), true, ratio);
  1373. push_cell();
  1374. pos = brk_end + 1;
  1375. tag_stack.push_front("cell");
  1376. } else if (tag == "u") {
  1377. //use underline
  1378. push_underline();
  1379. pos = brk_end + 1;
  1380. tag_stack.push_front(tag);
  1381. } else if (tag == "s") {
  1382. //use strikethrough
  1383. push_strikethrough();
  1384. pos = brk_end + 1;
  1385. tag_stack.push_front(tag);
  1386. } else if (tag == "center") {
  1387. push_align(ALIGN_CENTER);
  1388. pos = brk_end + 1;
  1389. tag_stack.push_front(tag);
  1390. } else if (tag == "fill") {
  1391. push_align(ALIGN_FILL);
  1392. pos = brk_end + 1;
  1393. tag_stack.push_front(tag);
  1394. } else if (tag == "right") {
  1395. push_align(ALIGN_RIGHT);
  1396. pos = brk_end + 1;
  1397. tag_stack.push_front(tag);
  1398. } else if (tag == "ul") {
  1399. push_list(LIST_DOTS);
  1400. pos = brk_end + 1;
  1401. tag_stack.push_front(tag);
  1402. } else if (tag == "ol") {
  1403. push_list(LIST_NUMBERS);
  1404. pos = brk_end + 1;
  1405. tag_stack.push_front(tag);
  1406. } else if (tag == "indent") {
  1407. indent_level++;
  1408. push_indent(indent_level);
  1409. pos = brk_end + 1;
  1410. tag_stack.push_front(tag);
  1411. } else if (tag == "url") {
  1412. int end = p_bbcode.find("[", brk_end);
  1413. if (end == -1)
  1414. end = p_bbcode.length();
  1415. String url = p_bbcode.substr(brk_end + 1, end - brk_end - 1);
  1416. push_meta(url);
  1417. pos = brk_end + 1;
  1418. tag_stack.push_front(tag);
  1419. } else if (tag.begins_with("url=")) {
  1420. String url = tag.substr(4, tag.length());
  1421. push_meta(url);
  1422. pos = brk_end + 1;
  1423. tag_stack.push_front("url");
  1424. } else if (tag == "img") {
  1425. int end = p_bbcode.find("[", brk_end);
  1426. if (end == -1)
  1427. end = p_bbcode.length();
  1428. String image = p_bbcode.substr(brk_end + 1, end - brk_end - 1);
  1429. Ref<Texture> texture = ResourceLoader::load(image, "Texture");
  1430. if (texture.is_valid())
  1431. add_image(texture);
  1432. pos = end;
  1433. tag_stack.push_front(tag);
  1434. } else if (tag.begins_with("color=")) {
  1435. String col = tag.substr(6, tag.length());
  1436. Color color;
  1437. if (col.begins_with("#"))
  1438. color = Color::html(col);
  1439. else if (col == "aqua")
  1440. color = Color::html("#00FFFF");
  1441. else if (col == "black")
  1442. color = Color::html("#000000");
  1443. else if (col == "blue")
  1444. color = Color::html("#0000FF");
  1445. else if (col == "fuchsia")
  1446. color = Color::html("#FF00FF");
  1447. else if (col == "gray" || col == "grey")
  1448. color = Color::html("#808080");
  1449. else if (col == "green")
  1450. color = Color::html("#008000");
  1451. else if (col == "lime")
  1452. color = Color::html("#00FF00");
  1453. else if (col == "maroon")
  1454. color = Color::html("#800000");
  1455. else if (col == "navy")
  1456. color = Color::html("#000080");
  1457. else if (col == "olive")
  1458. color = Color::html("#808000");
  1459. else if (col == "purple")
  1460. color = Color::html("#800080");
  1461. else if (col == "red")
  1462. color = Color::html("#FF0000");
  1463. else if (col == "silver")
  1464. color = Color::html("#C0C0C0");
  1465. else if (col == "teal")
  1466. color = Color::html("#008008");
  1467. else if (col == "white")
  1468. color = Color::html("#FFFFFF");
  1469. else if (col == "yellow")
  1470. color = Color::html("#FFFF00");
  1471. else
  1472. color = base_color;
  1473. push_color(color);
  1474. pos = brk_end + 1;
  1475. tag_stack.push_front("color");
  1476. } else if (tag.begins_with("font=")) {
  1477. String fnt = tag.substr(5, tag.length());
  1478. Ref<Font> font = ResourceLoader::load(fnt, "Font");
  1479. if (font.is_valid())
  1480. push_font(font);
  1481. else
  1482. push_font(normal_font);
  1483. pos = brk_end + 1;
  1484. tag_stack.push_front("font");
  1485. } else {
  1486. add_text("["); //ignore
  1487. pos = brk_pos + 1;
  1488. }
  1489. }
  1490. return OK;
  1491. }
  1492. void RichTextLabel::scroll_to_line(int p_line) {
  1493. ERR_FAIL_INDEX(p_line, main->lines.size());
  1494. _validate_line_caches(main);
  1495. vscroll->set_value(main->lines[p_line].height_accum_cache - main->lines[p_line].height_cache);
  1496. }
  1497. int RichTextLabel::get_line_count() const {
  1498. return current_frame->lines.size();
  1499. }
  1500. int RichTextLabel::get_visible_line_count() const {
  1501. if (!is_visible())
  1502. return 0;
  1503. return visible_line_count;
  1504. }
  1505. void RichTextLabel::set_selection_enabled(bool p_enabled) {
  1506. selection.enabled = p_enabled;
  1507. if (!p_enabled) {
  1508. if (selection.active) {
  1509. selection.active = false;
  1510. update();
  1511. }
  1512. set_focus_mode(FOCUS_NONE);
  1513. } else {
  1514. set_focus_mode(FOCUS_ALL);
  1515. }
  1516. }
  1517. bool RichTextLabel::search(const String &p_string, bool p_from_selection, bool p_search_previous) {
  1518. ERR_FAIL_COND_V(!selection.enabled, false);
  1519. Item *it = main;
  1520. int charidx = 0;
  1521. if (p_from_selection && selection.active && selection.enabled) {
  1522. it = selection.to;
  1523. charidx = selection.to_char + 1;
  1524. }
  1525. while (it) {
  1526. if (it->type == ITEM_TEXT) {
  1527. ItemText *t = static_cast<ItemText *>(it);
  1528. int sp = t->text.find(p_string, charidx);
  1529. if (sp != -1) {
  1530. selection.from = it;
  1531. selection.from_char = sp;
  1532. selection.to = it;
  1533. selection.to_char = sp + p_string.length() - 1;
  1534. selection.active = true;
  1535. update();
  1536. _validate_line_caches(main);
  1537. int fh = _find_font(t).is_valid() ? _find_font(t)->get_height() : get_font("normal_font")->get_height();
  1538. float offset = 0;
  1539. int line = t->line;
  1540. Item *item = t;
  1541. while (item) {
  1542. if (item->type == ITEM_FRAME) {
  1543. ItemFrame *frame = static_cast<ItemFrame *>(item);
  1544. if (line >= 0 && line < frame->lines.size()) {
  1545. offset += frame->lines[line].height_accum_cache - frame->lines[line].height_cache;
  1546. line = frame->line;
  1547. }
  1548. }
  1549. item = item->parent;
  1550. }
  1551. vscroll->set_value(offset - fh);
  1552. return true;
  1553. }
  1554. }
  1555. if (p_search_previous)
  1556. it = _get_prev_item(it, true);
  1557. else
  1558. it = _get_next_item(it, true);
  1559. charidx = 0;
  1560. }
  1561. return false;
  1562. }
  1563. void RichTextLabel::selection_copy() {
  1564. if (!selection.active || !selection.enabled)
  1565. return;
  1566. String text;
  1567. RichTextLabel::Item *item = selection.from;
  1568. while (item) {
  1569. if (item->type == ITEM_TEXT) {
  1570. String itext = static_cast<ItemText *>(item)->text;
  1571. if (item == selection.from && item == selection.to) {
  1572. text += itext.substr(selection.from_char, selection.to_char - selection.from_char + 1);
  1573. } else if (item == selection.from) {
  1574. text += itext.substr(selection.from_char, itext.size());
  1575. } else if (item == selection.to) {
  1576. text += itext.substr(0, selection.to_char + 1);
  1577. } else {
  1578. text += itext;
  1579. }
  1580. } else if (item->type == ITEM_NEWLINE) {
  1581. text += "\n";
  1582. }
  1583. if (item == selection.to)
  1584. break;
  1585. item = _get_next_item(item, true);
  1586. }
  1587. if (text != "") {
  1588. OS::get_singleton()->set_clipboard(text);
  1589. }
  1590. }
  1591. bool RichTextLabel::is_selection_enabled() const {
  1592. return selection.enabled;
  1593. }
  1594. void RichTextLabel::set_bbcode(const String &p_bbcode) {
  1595. bbcode = p_bbcode;
  1596. if (is_inside_tree() && use_bbcode)
  1597. parse_bbcode(p_bbcode);
  1598. else { // raw text
  1599. clear();
  1600. add_text(p_bbcode);
  1601. }
  1602. }
  1603. String RichTextLabel::get_bbcode() const {
  1604. return bbcode;
  1605. }
  1606. void RichTextLabel::set_use_bbcode(bool p_enable) {
  1607. if (use_bbcode == p_enable)
  1608. return;
  1609. use_bbcode = p_enable;
  1610. set_bbcode(bbcode);
  1611. }
  1612. bool RichTextLabel::is_using_bbcode() const {
  1613. return use_bbcode;
  1614. }
  1615. String RichTextLabel::get_text() {
  1616. String text = "";
  1617. Item *it = main;
  1618. while (it) {
  1619. if (it->type == ITEM_TEXT) {
  1620. ItemText *t = static_cast<ItemText *>(it);
  1621. text += t->text;
  1622. } else if (it->type == ITEM_NEWLINE) {
  1623. text += "\n";
  1624. } else if (it->type == ITEM_INDENT) {
  1625. text += "\t";
  1626. }
  1627. it = _get_next_item(it, true);
  1628. }
  1629. return text;
  1630. }
  1631. void RichTextLabel::set_text(const String &p_string) {
  1632. clear();
  1633. add_text(p_string);
  1634. }
  1635. void RichTextLabel::set_percent_visible(float p_percent) {
  1636. if (p_percent < 0 || p_percent >= 1) {
  1637. visible_characters = -1;
  1638. percent_visible = 1;
  1639. } else {
  1640. visible_characters = get_total_character_count() * p_percent;
  1641. percent_visible = p_percent;
  1642. }
  1643. update();
  1644. }
  1645. float RichTextLabel::get_percent_visible() const {
  1646. return percent_visible;
  1647. }
  1648. int RichTextLabel::get_content_height() {
  1649. int total_height = 0;
  1650. if (main->lines.size())
  1651. total_height = main->lines[main->lines.size() - 1].height_accum_cache + get_stylebox("normal")->get_minimum_size().height;
  1652. return total_height;
  1653. }
  1654. void RichTextLabel::_bind_methods() {
  1655. ClassDB::bind_method(D_METHOD("_gui_input"), &RichTextLabel::_gui_input);
  1656. ClassDB::bind_method(D_METHOD("_scroll_changed"), &RichTextLabel::_scroll_changed);
  1657. ClassDB::bind_method(D_METHOD("get_text"), &RichTextLabel::get_text);
  1658. ClassDB::bind_method(D_METHOD("add_text", "text"), &RichTextLabel::add_text);
  1659. ClassDB::bind_method(D_METHOD("set_text", "text"), &RichTextLabel::set_text);
  1660. ClassDB::bind_method(D_METHOD("add_image", "image"), &RichTextLabel::add_image);
  1661. ClassDB::bind_method(D_METHOD("newline"), &RichTextLabel::add_newline);
  1662. ClassDB::bind_method(D_METHOD("remove_line", "line"), &RichTextLabel::remove_line);
  1663. ClassDB::bind_method(D_METHOD("push_font", "font"), &RichTextLabel::push_font);
  1664. ClassDB::bind_method(D_METHOD("push_color", "color"), &RichTextLabel::push_color);
  1665. ClassDB::bind_method(D_METHOD("push_align", "align"), &RichTextLabel::push_align);
  1666. ClassDB::bind_method(D_METHOD("push_indent", "level"), &RichTextLabel::push_indent);
  1667. ClassDB::bind_method(D_METHOD("push_list", "type"), &RichTextLabel::push_list);
  1668. ClassDB::bind_method(D_METHOD("push_meta", "data"), &RichTextLabel::push_meta);
  1669. ClassDB::bind_method(D_METHOD("push_underline"), &RichTextLabel::push_underline);
  1670. ClassDB::bind_method(D_METHOD("push_strikethrough"), &RichTextLabel::push_strikethrough);
  1671. ClassDB::bind_method(D_METHOD("push_table", "columns"), &RichTextLabel::push_table);
  1672. ClassDB::bind_method(D_METHOD("set_table_column_expand", "column", "expand", "ratio"), &RichTextLabel::set_table_column_expand);
  1673. ClassDB::bind_method(D_METHOD("push_cell"), &RichTextLabel::push_cell);
  1674. ClassDB::bind_method(D_METHOD("pop"), &RichTextLabel::pop);
  1675. ClassDB::bind_method(D_METHOD("clear"), &RichTextLabel::clear);
  1676. ClassDB::bind_method(D_METHOD("set_meta_underline", "enable"), &RichTextLabel::set_meta_underline);
  1677. ClassDB::bind_method(D_METHOD("is_meta_underlined"), &RichTextLabel::is_meta_underlined);
  1678. ClassDB::bind_method(D_METHOD("set_override_selected_font_color", "override"), &RichTextLabel::set_override_selected_font_color);
  1679. ClassDB::bind_method(D_METHOD("is_overriding_selected_font_color"), &RichTextLabel::is_overriding_selected_font_color);
  1680. ClassDB::bind_method(D_METHOD("set_scroll_active", "active"), &RichTextLabel::set_scroll_active);
  1681. ClassDB::bind_method(D_METHOD("is_scroll_active"), &RichTextLabel::is_scroll_active);
  1682. ClassDB::bind_method(D_METHOD("set_scroll_follow", "follow"), &RichTextLabel::set_scroll_follow);
  1683. ClassDB::bind_method(D_METHOD("is_scroll_following"), &RichTextLabel::is_scroll_following);
  1684. ClassDB::bind_method(D_METHOD("get_v_scroll"), &RichTextLabel::get_v_scroll);
  1685. ClassDB::bind_method(D_METHOD("scroll_to_line", "line"), &RichTextLabel::scroll_to_line);
  1686. ClassDB::bind_method(D_METHOD("set_tab_size", "spaces"), &RichTextLabel::set_tab_size);
  1687. ClassDB::bind_method(D_METHOD("get_tab_size"), &RichTextLabel::get_tab_size);
  1688. ClassDB::bind_method(D_METHOD("set_selection_enabled", "enabled"), &RichTextLabel::set_selection_enabled);
  1689. ClassDB::bind_method(D_METHOD("is_selection_enabled"), &RichTextLabel::is_selection_enabled);
  1690. ClassDB::bind_method(D_METHOD("parse_bbcode", "bbcode"), &RichTextLabel::parse_bbcode);
  1691. ClassDB::bind_method(D_METHOD("append_bbcode", "bbcode"), &RichTextLabel::append_bbcode);
  1692. ClassDB::bind_method(D_METHOD("set_bbcode", "text"), &RichTextLabel::set_bbcode);
  1693. ClassDB::bind_method(D_METHOD("get_bbcode"), &RichTextLabel::get_bbcode);
  1694. ClassDB::bind_method(D_METHOD("set_visible_characters", "amount"), &RichTextLabel::set_visible_characters);
  1695. ClassDB::bind_method(D_METHOD("get_visible_characters"), &RichTextLabel::get_visible_characters);
  1696. ClassDB::bind_method(D_METHOD("set_percent_visible", "percent_visible"), &RichTextLabel::set_percent_visible);
  1697. ClassDB::bind_method(D_METHOD("get_percent_visible"), &RichTextLabel::get_percent_visible);
  1698. ClassDB::bind_method(D_METHOD("get_total_character_count"), &RichTextLabel::get_total_character_count);
  1699. ClassDB::bind_method(D_METHOD("set_use_bbcode", "enable"), &RichTextLabel::set_use_bbcode);
  1700. ClassDB::bind_method(D_METHOD("is_using_bbcode"), &RichTextLabel::is_using_bbcode);
  1701. ClassDB::bind_method(D_METHOD("get_line_count"), &RichTextLabel::get_line_count);
  1702. ClassDB::bind_method(D_METHOD("get_visible_line_count"), &RichTextLabel::get_visible_line_count);
  1703. ClassDB::bind_method(D_METHOD("get_content_height"), &RichTextLabel::get_content_height);
  1704. ADD_GROUP("BBCode", "bbcode_");
  1705. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bbcode_enabled"), "set_use_bbcode", "is_using_bbcode");
  1706. ADD_PROPERTY(PropertyInfo(Variant::STRING, "bbcode_text", PROPERTY_HINT_MULTILINE_TEXT), "set_bbcode", "get_bbcode");
  1707. ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_characters", PROPERTY_HINT_RANGE, "-1,128000,1"), "set_visible_characters", "get_visible_characters");
  1708. ADD_PROPERTY(PropertyInfo(Variant::REAL, "percent_visible", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_percent_visible", "get_percent_visible");
  1709. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meta_underlined"), "set_meta_underline", "is_meta_underlined");
  1710. ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_size", PROPERTY_HINT_RANGE, "0,24,1"), "set_tab_size", "get_tab_size");
  1711. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  1712. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_active"), "set_scroll_active", "is_scroll_active");
  1713. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_following"), "set_scroll_follow", "is_scroll_following");
  1714. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selection_enabled"), "set_selection_enabled", "is_selection_enabled");
  1715. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_selected_font_color"), "set_override_selected_font_color", "is_overriding_selected_font_color");
  1716. ADD_SIGNAL(MethodInfo("meta_clicked", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1717. ADD_SIGNAL(MethodInfo("meta_hover_started", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1718. ADD_SIGNAL(MethodInfo("meta_hover_ended", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1719. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  1720. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  1721. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  1722. BIND_ENUM_CONSTANT(ALIGN_FILL);
  1723. BIND_ENUM_CONSTANT(LIST_NUMBERS);
  1724. BIND_ENUM_CONSTANT(LIST_LETTERS);
  1725. BIND_ENUM_CONSTANT(LIST_DOTS);
  1726. BIND_ENUM_CONSTANT(ITEM_FRAME);
  1727. BIND_ENUM_CONSTANT(ITEM_TEXT);
  1728. BIND_ENUM_CONSTANT(ITEM_IMAGE);
  1729. BIND_ENUM_CONSTANT(ITEM_NEWLINE);
  1730. BIND_ENUM_CONSTANT(ITEM_FONT);
  1731. BIND_ENUM_CONSTANT(ITEM_COLOR);
  1732. BIND_ENUM_CONSTANT(ITEM_UNDERLINE);
  1733. BIND_ENUM_CONSTANT(ITEM_STRIKETHROUGH);
  1734. BIND_ENUM_CONSTANT(ITEM_ALIGN);
  1735. BIND_ENUM_CONSTANT(ITEM_INDENT);
  1736. BIND_ENUM_CONSTANT(ITEM_LIST);
  1737. BIND_ENUM_CONSTANT(ITEM_TABLE);
  1738. BIND_ENUM_CONSTANT(ITEM_META);
  1739. }
  1740. void RichTextLabel::set_visible_characters(int p_visible) {
  1741. visible_characters = p_visible;
  1742. update();
  1743. }
  1744. int RichTextLabel::get_visible_characters() const {
  1745. return visible_characters;
  1746. }
  1747. int RichTextLabel::get_total_character_count() const {
  1748. int tc = 0;
  1749. for (int i = 0; i < current_frame->lines.size(); i++)
  1750. tc += current_frame->lines[i].char_count;
  1751. return tc;
  1752. }
  1753. void RichTextLabel::set_fixed_size_to_width(int p_width) {
  1754. fixed_width = p_width;
  1755. minimum_size_changed();
  1756. }
  1757. Size2 RichTextLabel::get_minimum_size() const {
  1758. if (fixed_width != -1) {
  1759. const_cast<RichTextLabel *>(this)->_validate_line_caches(main);
  1760. return Size2(fixed_width, const_cast<RichTextLabel *>(this)->get_content_height());
  1761. }
  1762. return Size2();
  1763. }
  1764. RichTextLabel::RichTextLabel() {
  1765. main = memnew(ItemFrame);
  1766. main->index = 0;
  1767. current = main;
  1768. main->lines.resize(1);
  1769. main->lines.write[0].from = main;
  1770. main->first_invalid_line = 0;
  1771. current_frame = main;
  1772. tab_size = 4;
  1773. default_align = ALIGN_LEFT;
  1774. underline_meta = true;
  1775. override_selected_font_color = false;
  1776. scroll_visible = false;
  1777. scroll_follow = false;
  1778. scroll_following = false;
  1779. updating_scroll = false;
  1780. scroll_active = true;
  1781. scroll_w = 0;
  1782. scroll_updated = false;
  1783. vscroll = memnew(VScrollBar);
  1784. add_child(vscroll);
  1785. vscroll->set_drag_node(String(".."));
  1786. vscroll->set_step(1);
  1787. vscroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0);
  1788. vscroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  1789. vscroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  1790. vscroll->connect("value_changed", this, "_scroll_changed");
  1791. vscroll->set_step(1);
  1792. vscroll->hide();
  1793. current_idx = 1;
  1794. use_bbcode = false;
  1795. selection.click = NULL;
  1796. selection.active = false;
  1797. selection.enabled = false;
  1798. visible_characters = -1;
  1799. percent_visible = 1;
  1800. visible_line_count = 0;
  1801. fixed_width = -1;
  1802. set_clip_contents(true);
  1803. }
  1804. RichTextLabel::~RichTextLabel() {
  1805. memdelete(main);
  1806. }