rich_text_label.cpp 73 KB

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