bbcode_in_richtextlabel.rst 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. .. _doc_bbcode_in_richtextlabel:
  2. BBCode in RichTextLabel
  3. =======================
  4. Introduction
  5. ------------
  6. :ref:`class_Label` nodes are great for displaying basic text, but they have limitations.
  7. If you want to change the color of the text, or its alignment, you can only do that to
  8. the entire label. You can't make a part of the text have another color, or have a part
  9. of the text centered. To get around these limitations, you would use a :ref:`class_RichTextLabel`.
  10. :ref:`class_RichTextLabel` allows for complex formatting of text using a markup syntax or
  11. the built-in API. It uses BBCodes for the markup syntax, a system of tags that designate
  12. formatting rules for a part of the text. You may be familiar with them if you ever used
  13. forums (also known as `bulletin boards`, hence the "BB" in "BBCode").
  14. Unlike Label, RichTextLabel also comes with its own vertical scrollbar. This
  15. scrollbar is automatically displayed if the text does not fit within the
  16. control's size. The scrollbar can be disabled by unchecking the
  17. **Scroll Active** property in the RichTextLabel inspector.
  18. Note that the BBCode tags can also be used to some extent for other use cases:
  19. - BBCode can be used to :ref:`format comments in the XML source of the class reference <doc_class_reference_bbcode>`.
  20. - BBCode can be used in :ref:`GDScript documentation comments <doc_gdscript_documentation_comments_bbcode_and_class_reference>`.
  21. - BBCode can be used when :ref:`printing rich text to the Output bottom panel <doc_output_panel_printing_rich_text>`.
  22. .. seealso::
  23. You can see how BBCode in RichTextLabel works in action using the
  24. `Rich Text Label with BBCode demo project <https://github.com/godotengine/godot-demo-projects/tree/master/gui/rich_text_bbcode>`__.
  25. Using BBCode
  26. ------------
  27. By default, :ref:`class_RichTextLabel` functions like a normal :ref:`class_Label`.
  28. It has the :ref:`property_text <class_RichTextLabel_property_text>` property, which you can
  29. edit to have uniformly formatted text. To be able to use BBCode for rich text formatting,
  30. you need to turn on the BBCode mode by setting :ref:`bbcode_enabled <class_RichTextLabel_property_bbcode_enabled>`.
  31. After that, you can edit the :ref:`text <class_RichTextLabel_property_text>`
  32. property using available tags. Both properties are located at the top of the inspector
  33. after selecting a RichTextLabel node.
  34. .. image:: img/bbcode_in_richtextlabel_inspector.webp
  35. For example, ``BBCode [color=green]test[/color]`` would render the word "test" with
  36. a green color.
  37. .. image:: img/bbcode_in_richtextlabel_basic_example.webp
  38. Most BBCodes consist of 3 parts: the opening tag, the content and the closing
  39. tag. The opening tag delimits the start of the formatted part, and can also
  40. carry some configuration options. Some opening tags, like the ``color`` one
  41. shown above, also require a value to work. Other opening tags may accept
  42. multiple options (separated by spaces within the opening tag). The closing tag
  43. delimits the end of the formatted part. In some cases, both the closing tag and
  44. the content can be omitted.
  45. Unlike BBCode in HTML, leading/trailing whitespace is not removed by a
  46. RichTextLabel upon display. Duplicate spaces are also displayed as-is in the
  47. final output. This means that when displaying a code block in a RichTextLabel,
  48. you don't need to use a preformatted text tag.
  49. .. code-block:: none
  50. [tag]content[/tag]
  51. [tag=value]content[/tag]
  52. [tag option1=value1 option2=value2]content[/tag]
  53. [tag][/tag]
  54. [tag]
  55. .. note::
  56. RichTextLabel doesn't support entangled BBCode tags. For example, instead of
  57. using:
  58. ::
  59. [b]bold[i]bold italic[/b]italic[/i]
  60. Use:
  61. ::
  62. [b]bold[i]bold italic[/i][/b][i]italic[/i]
  63. .. _doc_bbcode_in_richtextlabel_handling_user_input_safely:
  64. Handling user input safely
  65. --------------------------
  66. In a scenario where users may freely input text (such as chat in a multiplayer
  67. game), you should make sure users cannot use arbitrary BBCode tags that will be
  68. parsed by RichTextLabel. This is to avoid inappropriate use of formatting, which
  69. can be problematic if ``[url]`` tags are handled by your RichTextLabel (as players
  70. may be able to create clickable links to phishing sites or similar).
  71. Using RichTextLabel's ``[lb]`` and/or ``[rb]`` tags, we can replace the opening and/or
  72. closing brackets of any BBCode tag in a message with those escaped tags. This
  73. prevents users from using BBCode that will be parsed as tags – instead, the
  74. BBCode will be displayed as text.
  75. .. figure:: img/bbcode_in_richtextlabel_escaping_user_input.webp
  76. :align: center
  77. :alt: Example of unescaped user input resulting in BBCode injection (2nd line) and escaped user input (3rd line)
  78. Example of unescaped user input resulting in BBCode injection (2nd line) and escaped user input (3rd line)
  79. The above image was created using the following script:
  80. ::
  81. extends RichTextLabel
  82. func _ready():
  83. append_chat_line("Player 1", "Hello world!")
  84. append_chat_line("Player 2", "Hello [color=red]BBCode injection[/color] (no escaping)!")
  85. append_chat_line_escaped("Player 2", "Hello [color=red]BBCode injection[/color] (with escaping)!")
  86. # Returns escaped BBCode that won't be parsed by RichTextLabel as tags.
  87. func escape_bbcode(bbcode_text):
  88. # We only need to replace opening brackets to prevent tags from being parsed.
  89. return bbcode_text.replace("[", "[lb]")
  90. # Appends the user's message as-is, without escaping. This is dangerous!
  91. func append_chat_line(username, message):
  92. append_text("%s: [color=green]%s[/color]\n" % [username, message])
  93. # Appends the user's message with escaping.
  94. # Remember to escape both the player name and message contents.
  95. func append_chat_line_escaped(username, message):
  96. append_text("%s: [color=green]%s[/color]\n" % [escape_bbcode(username), escape_bbcode(message)])
  97. Stripping BBCode tags
  98. ---------------------
  99. For certain use cases, it can be desired to remove BBCode tags from the string.
  100. This is useful when displaying the RichTextLabel's text in another Control that
  101. does not support BBCode (such as a tooltip):
  102. .. code::
  103. extends RichTextLabel
  104. func _ready():
  105. var regex = RegEx.new()
  106. regex.compile("\\[.*?\\]")
  107. var text_without_tags = regex.sub(text, "", true)
  108. # `text_without_tags` contains the text with all BBCode tags removed.
  109. .. note::
  110. Removing BBCode tags entirely isn't advised for user input, as it can
  111. modify the displayed text without users understanding why part of their
  112. message was removed.
  113. :ref:`Escaping user input <doc_bbcode_in_richtextlabel_handling_user_input_safely>`
  114. should be preferred instead.
  115. Performance
  116. -----------
  117. In most cases, you can use BBCode directly as-is since text formatting is rarely
  118. a heavy task. However, with particularly large RichTextLabels (such as console
  119. logs spanning thousands of lines), you may encounter stuttering during gameplay
  120. when the RichTextLabel's text is updated.
  121. There are several ways to alleviate this:
  122. - Use the ``append_text()`` function instead of appending to the ``text``
  123. property. This function will only parse BBCode for the added text, rather than
  124. parsing BBCode from the entire ``text`` property.
  125. - Use ``push_[tag]()`` and ``pop()`` functions to add tags to RichTextLabel instead of
  126. using BBCode.
  127. - Enable the **Threading > Threaded** property in RichTextLabel. This won't
  128. speed up processing, but it will prevent the main thread from blocking, which
  129. avoids stuttering during gameplay. Only enable threading if it's actually
  130. needed in your project, as threading has some overhead.
  131. .. _doc_bbcode_in_richtextlabel_use_functions:
  132. Using push_[tag]() and pop() functions instead of BBCode
  133. --------------------------------------------------------
  134. If you don't want to use BBCode for performance reasons, you can use functions
  135. provided by RichTextLabel to create formatting tags without writing BBCode in
  136. the text.
  137. Every BBCode tag (including effects) has a ``push_[tag]()`` function (where
  138. ``[tag]`` is the tag's name). There are also a few convenience functions
  139. available, such as ``push_bold_italics()`` that combines both ``push_bold()``
  140. and ``push_italics()`` into a single tag. See the
  141. :ref:`RichTextLabel class reference <class_RichTextLabel>` for a complete list of
  142. ``push_[tag]()`` functions.
  143. The ``pop()`` function is used to end *any* tag. Since BBCode is a tag *stack*,
  144. using ``pop()`` will close the most recently started tags first.
  145. The following script will result in the same visual output as using
  146. ``BBCode [color=green]test [i]example[/i][/color]``:
  147. ::
  148. extends RichTextLabel
  149. func _ready():
  150. append_text("BBCode ") # Trailing space separates words from each other.
  151. push_color(Color.GREEN)
  152. append_text("test ") # Trailing space separates words from each other.
  153. push_italics()
  154. append_text("example")
  155. pop() # Ends the tag opened by `push_italics()`.
  156. pop() # Ends the tag opened by `push_color()`.
  157. .. warning::
  158. Do **not** set the ``text`` property directly when using formatting functions.
  159. Appending to the ``text`` property will erase all modifications made to the
  160. RichTextLabel using the ``append_text()``, ``push_[tag]()`` and ``pop()``
  161. functions.
  162. Reference
  163. ---------
  164. .. seealso::
  165. *Some* of these BBCode tags can be used in tooltips for ``@export`` script
  166. variables as well as in the XML source of the class reference. For more
  167. information, see :ref:`Class reference BBCode <doc_class_reference_bbcode>`.
  168. .. list-table::
  169. :class: wrap-normal
  170. :width: 100%
  171. :widths: 60 40
  172. * - Tag
  173. - Example
  174. * - | **b**
  175. | Makes ``{text}`` use the bold (or bold italics) font of ``RichTextLabel``.
  176. - ``[b]{text}[/b]``
  177. * - | **i**
  178. | Makes ``{text}`` use the italics (or bold italics) font of ``RichTextLabel``.
  179. - ``[i]{text}[/i]``
  180. * - | **u**
  181. | Makes ``{text}`` underlined.
  182. - ``[u]{text}[/u]``
  183. * - | **s**
  184. | Makes ``{text}`` strikethrough.
  185. - ``[s]{text}[/s]``
  186. * - | **code**
  187. | Makes ``{text}`` use the mono font of ``RichTextLabel``.
  188. - ``[code]{text}[/code]``
  189. * - | **char**
  190. | Adds Unicode character with hexadecimal UTF-32 ``{codepoint}``.
  191. - ``[char={codepoint}]``
  192. * - | **p**
  193. | Adds new paragraph with ``{text}``. Supports configuration options,
  194. see :ref:`doc_bbcode_in_richtextlabel_paragraph_options`.
  195. - | ``[p]{text}[/p]``
  196. | ``[p {options}]{text}[/p]``
  197. * - | **br**
  198. | Adds line break in a text.
  199. - ``[br]``
  200. * - | **hr**
  201. | Adds new a horizontal rule to separate content. Supports configuration options,
  202. see :ref:`doc_bbcode_in_richtextlabel_hr_options`.
  203. - | ``[hr]``
  204. | ``[hr {options}]``
  205. * - | **center**
  206. | Makes ``{text}`` horizontally centered.
  207. | Same as ``[p align=center]``.
  208. - ``[center]{text}[/center]``
  209. * - | **left**
  210. | Makes ``{text}`` horizontally left-aligned.
  211. | Same as ``[p align=left]``.
  212. - ``[left]{text}[/left]``
  213. * - | **right**
  214. | Makes ``{text}`` horizontally right-aligned.
  215. | Same as ``[p align=right]``.
  216. - ``[right]{text}[/right]``
  217. * - | **fill**
  218. | Makes ``{text}`` fill the full width of ``RichTextLabel``.
  219. | Same as ``[p align=fill]``.
  220. - ``[fill]{text}[/fill]``
  221. * - | **indent**
  222. | Indents ``{text}`` once.
  223. The indentation width is the same as with ``[ul]`` or ``[ol]``, but without a bullet point.
  224. - ``[indent]{text}[/indent]``
  225. * - | **url**
  226. | Creates a hyperlink (underlined and clickable text). Can contain optional
  227. ``{text}`` or display ``{link}`` as is.
  228. | **Must be handled with the "meta_clicked" signal to have an effect,** see :ref:`doc_bbcode_in_richtextlabel_handling_url_tag_clicks`.
  229. - | ``[url]{link}[/url]``
  230. | ``[url={link}]{text}[/url]``
  231. * - | **hint**
  232. | Creates a tooltip hint that is displayed when hovering the text with the mouse.
  233. While not required, it's recommended to put tooltip text between double or single quotes.
  234. Note that it is not possible to escape quotes using ``\"`` or ``\'``. To use
  235. single quotes for apostrophes in the hint string, you must use double quotes
  236. to surround the string.
  237. - | ``[hint="{tooltip text displayed on hover}"]{text}[/hint]``
  238. * - | **img**
  239. | Inserts an image from the ``{path}`` (can be any valid :ref:`class_Texture2D` resource).
  240. | If ``{width}`` is provided, the image will try to fit that width maintaining
  241. the aspect ratio.
  242. | If both ``{width}`` and ``{height}`` are provided, the image will be scaled
  243. to that size.
  244. | Add ``%`` to the end of ``{width}`` or ``{height}`` value to specify it as percentages of the control width instead of pixels.
  245. | If ``{valign}`` configuration is provided, the image will try to align to the
  246. surrounding text, see :ref:`doc_bbcode_in_richtextlabel_image_and_table_alignment`.
  247. | Supports configuration options, see :ref:`doc_bbcode_in_richtextlabel_image_options`.
  248. - | ``[img]{path}[/img]``
  249. | ``[img={width}]{path}[/img]``
  250. | ``[img={width}x{height}]{path}[/img]``
  251. | ``[img={valign}]{path}[/img]``
  252. | ``[img {options}]{path}[/img]``
  253. * - | **font**
  254. | Makes ``{text}`` use a font resource from the ``{path}``.
  255. | Supports configuration options, see :ref:`doc_bbcode_in_richtextlabel_font_options`.
  256. - | ``[font={path}]{text}[/font]``
  257. | ``[font {options}]{text}[/font]``
  258. * - | **font_size**
  259. | Use custom font size for ``{text}``.
  260. - ``[font_size={size}]{text}[/font_size]``
  261. * - | **dropcap**
  262. | Use a different font size and color for ``{text}``, while making the tag's contents
  263. span multiple lines if it's large enough.
  264. | A `drop cap <https://www.computerhope.com/jargon/d/dropcap.htm>`__ is typically one
  265. uppercase character, but ``[dropcap]`` supports containing multiple characters.
  266. ``margins`` values are comma-separated and can be positive, zero or negative.
  267. Values must **not** be separated by spaces; otherwise, the values won't be parsed correctly.
  268. Negative top and bottom margins are particularly useful to allow the rest of
  269. the paragraph to display below the dropcap.
  270. - ``[dropcap font={font} font_size={size} color={color} outline_size={size} outline_color={color} margins={left},{top},{right},{bottom}]{text}[/dropcap]``
  271. * - | **opentype_features**
  272. | Enables custom OpenType font features for ``{text}``. Features must be provided as
  273. a comma-separated ``{list}``. Values must **not** be separated by spaces;
  274. otherwise, the list won't be parsed correctly.
  275. - | ``[opentype_features={list}]``
  276. | ``{text}``
  277. | ``[/opentype_features]``
  278. * - | **lang**
  279. | Overrides the language for ``{text}`` that is set by the **BiDi > Language** property
  280. in :ref:`class_RichTextLabel`. ``{code}`` must be an ISO :ref:`language code <doc_locales>`.
  281. This can be used to enforce the use of a specific script for a language without
  282. starting a new paragraph. Some font files may contain script-specific substitutes,
  283. in which case they will be used.
  284. - ``[lang={code}]{text}[/lang]``
  285. * - | **color**
  286. | Changes the color of ``{text}``. Color must be provided by a common name (see
  287. :ref:`doc_bbcode_in_richtextlabel_named_colors`) or using the HEX format (e.g.
  288. ``#ff00ff``, see :ref:`doc_bbcode_in_richtextlabel_hex_colors`).
  289. - ``[color={code/name}]{text}[/color]``
  290. * - | **bgcolor**
  291. | Draws the color behind ``{text}``. This can be used to highlight text.
  292. Accepts same values as the ``color`` tag.
  293. By default, there is a slight padding which is controlled by the
  294. ``text_highlight_h_padding`` and ``text_highlight_v_padding`` theme items
  295. in the RichTextLabel node. Set padding to ``0`` to avoid potential overlapping
  296. issues when there are background colors on neighboring lines/columns.
  297. - ``[bgcolor={code/name}]{text}[/bgcolor]``
  298. * - | **fgcolor**
  299. | Draws the color in front of ``{text}``. This can be used to "redact" text by using
  300. an opaque foreground color. Accepts same values as the ``color`` tag.
  301. By default, there is a slight padding which is controlled by the
  302. ``text_highlight_h_padding`` and ``text_highlight_v_padding`` theme items
  303. in the RichTextLabel node. Set padding to ``0`` to avoid potential overlapping
  304. issues when there are foreground colors on neighboring lines/columns.
  305. - ``[fgcolor={code/name}]{text}[/fgcolor]``
  306. * - | **outline_size**
  307. | Use custom font outline size for ``{text}``.
  308. - | ``[outline_size={size}]``
  309. | ``{text}``
  310. | ``[/outline_size]``
  311. * - | **outline_color**
  312. | Use custom outline color for ``{text}``. Accepts same values as the ``color`` tag.
  313. - | ``[outline_color={code/name}]``
  314. | ``{text}``
  315. | ``[/outline_color]``
  316. * - | **table**
  317. | Creates a table with the ``{number}`` of columns. Use the ``cell`` tag to define
  318. table cells.
  319. | If ``{valign}`` configuration is provided, the table will try to align to the
  320. surrounding text, see :ref:`doc_bbcode_in_richtextlabel_image_and_table_alignment`.
  321. | If baseline alignment is used, the table is aligned to the baseline of the row with index ``{alignment_row}`` (zero-based).
  322. - | ``[table={number}]{cells}[/table]``
  323. | ``[table={number},{valign}]{cells}[/table]``
  324. | ``[table={number},{valign},{alignment_row}]{cells}[/table]``
  325. * - | **cell**
  326. | Adds a cell with ``{text}`` to the table.
  327. | If ``{ratio}`` is provided, the cell will try to expand to that value proportionally
  328. to other cells and their ratio values.
  329. | Supports configuration options, see :ref:`doc_bbcode_in_richtextlabel_cell_options`.
  330. - | ``[cell]{text}[/cell]``
  331. | ``[cell={ratio}]{text}[/cell]``
  332. | ``[cell {options}]{text}[/cell]``
  333. * - | **ul**
  334. | Adds an unordered list. List ``{items}`` must be provided by putting one item per
  335. line of text.
  336. | The bullet point can be customized using the ``{bullet}`` parameter,
  337. see :ref:`doc_bbcode_in_richtextlabel_unordered_list_bullet`.
  338. - | ``[ul]{items}[/ul]``
  339. | ``[ul bullet={bullet}]{items}[/ul]``
  340. * - | **ol**
  341. | Adds an ordered (numbered) list of the given ``{type}`` (see :ref:`doc_bbcode_in_richtextlabel_list_types`).
  342. List ``{items}`` must be provided by putting one item per line of text.
  343. - ``[ol type={type}]{items}[/ol]``
  344. * - | **lb**, **rb**
  345. | Adds ``[`` and ``]`` respectively. Allows escaping BBCode markup.
  346. | These are self-closing tags, which means you do not need to close them
  347. (and there is no ``[/lb]`` or ``[/rb]`` closing tag).
  348. - | ``[lb]b[rb]text[lb]/b[rb]`` will display as ``[b]text[/b]``.
  349. * - | Several Unicode control characters can be added using their own self-closing tags.
  350. | This can result in easier maintenance compared to pasting those
  351. | control characters directly in the text.
  352. - | ``[lrm]`` (left-to-right mark), ``[rlm]`` (right-to-left mark), ``[lre]`` (left-to-right embedding),
  353. | ``[rle]`` (right-to-left embedding), ``[lro]`` (left-to-right override), ``[rlo]`` (right-to-left override),
  354. | ``[pdf]`` (pop directional formatting), ``[alm]`` (Arabic letter mark), ``[lri]`` (left-to-right isolate),
  355. | ``[rli]`` (right-to-left isolate), ``[fsi]`` (first strong isolate), ``[pdi]`` (pop directional isolate),
  356. | ``[zwj]`` (zero-width joiner), ``[zwnj]`` (zero-width non-joiner), ``[wj]`` (word joiner),
  357. | ``[shy]`` (soft hyphen)
  358. .. note::
  359. Tags for bold (``[b]``) and italics (``[i]``) formatting work best if the
  360. appropriate custom fonts are set up in the RichTextLabelNode's theme
  361. overrides. If no custom bold or italic fonts are defined,
  362. `faux bold and italic fonts <https://fonts.google.com/knowledge/glossary/faux_fake_pseudo_synthesized>`__
  363. will be generated by Godot. These fonts rarely look good in comparison to hand-made bold/italic font variants.
  364. The monospaced (``[code]``) tag **only** works if a custom font is set up in
  365. the RichTextLabel node's theme overrides. Otherwise, monospaced text will use the regular font.
  366. There are no BBCode tags to control vertical centering of text yet.
  367. Options can be skipped for all tags.
  368. .. _doc_bbcode_in_richtextlabel_paragraph_options:
  369. Paragraph options
  370. ~~~~~~~~~~~~~~~~~
  371. - **align**
  372. +-----------+----------------------------------------------------------------------------------------+
  373. | `Values` | ``left`` (or ``l``), ``center`` (or ``c``), ``right`` (or ``r``), ``fill`` (or ``f``) |
  374. +-----------+----------------------------------------------------------------------------------------+
  375. | `Default` | ``left`` |
  376. +-----------+----------------------------------------------------------------------------------------+
  377. Text horizontal alignment.
  378. - **bidi_override**, **st**
  379. +-----------+--------------------------------------------------------------------------------------------------------------+
  380. | `Values` | ``default`` (of ``d``), ``uri`` (or ``u``), ``file`` (or ``f``), ``email`` (or ``e``), ``list`` (or ``l``), |
  381. | | ``none`` (or ``n``), ``custom`` (or ``c``) |
  382. +-----------+--------------------------------------------------------------------------------------------------------------+
  383. | `Default` | ``default`` |
  384. +-----------+--------------------------------------------------------------------------------------------------------------+
  385. Structured text override.
  386. - **justification_flags**, **jst**
  387. +-----------+--------------------------------------------------------------------------------------------------------+
  388. | `Values` | Comma-separated list of the following values (no space after each comma): |
  389. | | ``kashida`` (or ``k``), ``word`` (or ``w``), ``trim`` (or ``tr``), ``after_last_tab`` (or ``lt``), |
  390. | | ``skip_last`` (or ``sl``), ``skip_last_with_chars`` (or ``sv``), ``do_not_skip_single`` (or ``ns``). |
  391. +-----------+--------------------------------------------------------------------------------------------------------+
  392. | `Default` | ``word,kashida,skip_last,do_not_skip_single`` |
  393. +-----------+--------------------------------------------------------------------------------------------------------+
  394. Justification (fill alignment) option. See :ref:`class_TextServer` for more details.
  395. - **direction**, **dir**
  396. +-----------+-----------------------------------------------------------------+
  397. | `Values` | ``ltr`` (or ``l``), ``rtl`` (or ``r``), ``auto`` (or ``a``) |
  398. +-----------+-----------------------------------------------------------------+
  399. | `Default` | Inherit |
  400. +-----------+-----------------------------------------------------------------+
  401. Base BiDi direction.
  402. - **language**, **lang**
  403. +-----------+--------------------------------------------+
  404. | `Values` | ISO language codes. See :ref:`doc_locales` |
  405. +-----------+--------------------------------------------+
  406. | `Default` | Inherit |
  407. +-----------+--------------------------------------------+
  408. Locale override. Some font files may contain script-specific substitutes, in which case they will be used.
  409. - **tab_stops**
  410. +-----------+----------------------------------------------------+
  411. | `Values` | List of floating-point numbers, e.g. ``10.0,30.0`` |
  412. +-----------+----------------------------------------------------+
  413. | `Default` | Width of the space character in the font |
  414. +-----------+----------------------------------------------------+
  415. Overrides the horizontal offsets for each tab character. When the end of the
  416. list is reached, the tab stops will loop over. For example, if you set
  417. ``tab_stops`` to ``10.0,30.0``, the first tab will be at ``10`` pixels, the
  418. second tab will be at ``10 + 30 = 40`` pixels, and the third tab will be at
  419. ``10 + 30 + 10 = 50`` pixels from the origin of the RichTextLabel.
  420. .. _doc_bbcode_in_richtextlabel_handling_url_tag_clicks:
  421. Handling ``[url]`` tag clicks
  422. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  423. By default, ``[url]`` tags do nothing when clicked. This is to allow flexible use
  424. of ``[url]`` tags rather than limiting them to opening URLs in a web browser.
  425. To handle clicked ``[url]`` tags, connect the ``RichTextLabel`` node's
  426. :ref:`meta_clicked <class_RichTextLabel_signal_meta_clicked>` signal to a script function.
  427. For example, the following method can be connected to ``meta_clicked`` to open
  428. clicked URLs using the user's default web browser:
  429. ::
  430. # This assumes RichTextLabel's `meta_clicked` signal was connected to
  431. # the function below using the signal connection dialog.
  432. func _richtextlabel_on_meta_clicked(meta):
  433. # `meta` is not guaranteed to be a String, so convert it to a String
  434. # to avoid script errors at runtime.
  435. OS.shell_open(str(meta))
  436. For more advanced use cases, it's also possible to store JSON in a ``[url]``
  437. tag's option and parse it in the function that handles the ``meta_clicked`` signal.
  438. For example:
  439. .. code-block:: none
  440. [url={"example": "value"}]JSON[/url]
  441. .. _doc_bbcode_in_richtextlabel_hr_options:
  442. Horizontal rule options
  443. ~~~~~~~~~~~~~~~~~~~~~~~
  444. - **color**
  445. +-----------+--------------------------------------------+
  446. | `Values` | Color name or color in HEX format |
  447. +-----------+--------------------------------------------+
  448. | `Default` | ``Color(1, 1, 1, 1)`` |
  449. +-----------+--------------------------------------------+
  450. Color tint of the rule (modulation).
  451. - **height**
  452. +-----------+--------------------------------------------+
  453. | `Values` | Integer number |
  454. +-----------+--------------------------------------------+
  455. | `Default` | ``2`` |
  456. +-----------+--------------------------------------------+
  457. Target height of the rule in pixels, add ``%`` to the end of value to specify it as percentages of the control width instead of pixels.
  458. - **width**
  459. +-----------+--------------------------------------------+
  460. | `Values` | Integer number |
  461. +-----------+--------------------------------------------+
  462. | `Default` | ``90%`` |
  463. +-----------+--------------------------------------------+
  464. Target width of the rule in pixels, add ``%`` to the end of value to specify it as percentages of the control width instead of pixels.
  465. - **align**
  466. +-----------+----------------------------------------------------------------------------------------+
  467. | `Values` | ``left`` (or ``l``), ``center`` (or ``c``), ``right`` (or ``r``) |
  468. +-----------+----------------------------------------------------------------------------------------+
  469. | `Default` | ``left`` |
  470. +-----------+----------------------------------------------------------------------------------------+
  471. Horizontal alignment.
  472. .. _doc_bbcode_in_richtextlabel_image_options:
  473. Image options
  474. ~~~~~~~~~~~~~
  475. - **color**
  476. +-----------+--------------------------------------------+
  477. | `Values` | Color name or color in HEX format |
  478. +-----------+--------------------------------------------+
  479. | `Default` | Inherit |
  480. +-----------+--------------------------------------------+
  481. Color tint of the image (modulation).
  482. - **height**
  483. +-----------+--------------------------------------------+
  484. | `Values` | Integer number |
  485. +-----------+--------------------------------------------+
  486. | `Default` | Inherit |
  487. +-----------+--------------------------------------------+
  488. Target height of the image in pixels, add ``%`` to the end of value to specify it as percentages of the control width instead of pixels.
  489. - **width**
  490. +-----------+--------------------------------------------+
  491. | `Values` | Integer number |
  492. +-----------+--------------------------------------------+
  493. | `Default` | Inherit |
  494. +-----------+--------------------------------------------+
  495. Target width of the image in pixels, add ``%`` to the end of value to specify it as percentages of the control width instead of pixels.
  496. - **region**
  497. +-----------+--------------------------------------------+
  498. | `Values` | x,y,width,height in pixels |
  499. +-----------+--------------------------------------------+
  500. | `Default` | Inherit |
  501. +-----------+--------------------------------------------+
  502. Region rect of the image. This can be used to display a single image from a spritesheet.
  503. - **pad**
  504. +-----------+--------------------------------------------+
  505. | `Values` | ``false``, ``true`` |
  506. +-----------+--------------------------------------------+
  507. | `Default` | ``false`` |
  508. +-----------+--------------------------------------------+
  509. If set to ``true``, and the image is smaller than the size specified by ``width`` and ``height``, the image padding is added to match the size instead of upscaling.
  510. - **tooltip**
  511. +-----------+--------------------------------------------+
  512. | `Values` | String |
  513. +-----------+--------------------------------------------+
  514. | `Default` | |
  515. +-----------+--------------------------------------------+
  516. Image tooltip.
  517. .. _doc_bbcode_in_richtextlabel_image_and_table_alignment:
  518. Image and table vertical alignment
  519. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  520. When a vertical alignment value is provided with the ``[img]`` or ``[table]`` tag
  521. the image/table will try to align itself against the surrounding text. Alignment is
  522. performed using a vertical point of the image and a vertical point of the text.
  523. There are 3 possible points on the image (``top``, ``center``, and ``bottom``) and 4
  524. possible points on the text and table (``top``, ``center``, ``baseline``, and ``bottom``),
  525. which can be used in any combination.
  526. To specify both points, use their full or short names as a value of the image/table tag:
  527. .. code-block:: none
  528. text [img=top,bottom]...[/img] text
  529. text [img=center,center]...[/img] text
  530. .. image:: img/bbcode_in_richtextlabel_image_align.webp
  531. .. code-block:: none
  532. text [table=3,center]...[/table] text # Center to center.
  533. text [table=3,top,bottom]...[/table] text # Top of the table to the bottom of text.
  534. text [table=3,baseline,baseline,1]...[/table] text # Baseline of the second row (rows use zero-based indexing) to the baseline of text.
  535. .. image:: img/bbcode_in_richtextlabel_table_align.webp
  536. You can also specify just one value (``top``, ``center``, or ``bottom``) to make
  537. use of a corresponding preset (``top-top``, ``center-center``, and ``bottom-bottom``
  538. respectively).
  539. Short names for the values are ``t`` (``top``), ``c`` (``center``), ``l`` (``baseline``),
  540. and ``b`` (``bottom``).
  541. .. _doc_bbcode_in_richtextlabel_font_options:
  542. Font options
  543. ~~~~~~~~~~~~
  544. - **name**, **n**
  545. +-----------+--------------------------------------------+
  546. | `Values` | A valid Font resource path. |
  547. +-----------+--------------------------------------------+
  548. | `Default` | Inherit |
  549. +-----------+--------------------------------------------+
  550. Font resource path.
  551. - **size**, **s**
  552. +-----------+--------------------------------------------+
  553. | `Values` | Number in pixels. |
  554. +-----------+--------------------------------------------+
  555. | `Default` | Inherit |
  556. +-----------+--------------------------------------------+
  557. Custom font size.
  558. - **glyph_spacing**, **gl**
  559. +-----------+--------------------------------------------+
  560. | `Values` | Number in pixels. |
  561. +-----------+--------------------------------------------+
  562. | `Default` | Inherit |
  563. +-----------+--------------------------------------------+
  564. Extra spacing for each glyph.
  565. - **space_spacing**, **sp**
  566. +-----------+--------------------------------------------+
  567. | `Values` | Number in pixels. |
  568. +-----------+--------------------------------------------+
  569. | `Default` | Inherit |
  570. +-----------+--------------------------------------------+
  571. Extra spacing for the space character.
  572. - **top_spacing**, **top**
  573. +-----------+--------------------------------------------+
  574. | `Values` | Number in pixels. |
  575. +-----------+--------------------------------------------+
  576. | `Default` | Inherit |
  577. +-----------+--------------------------------------------+
  578. Extra spacing at the top of the line.
  579. - **bottom_spacing**, **bt**
  580. +-----------+--------------------------------------------+
  581. | `Values` | Number in pixels. |
  582. +-----------+--------------------------------------------+
  583. | `Default` | Inherit |
  584. +-----------+--------------------------------------------+
  585. Extra spacing at the bottom of the line.
  586. - **embolden**, **emb**
  587. +-----------+--------------------------------------------+
  588. | `Values` | Floating-point number. |
  589. +-----------+--------------------------------------------+
  590. | `Default` | ``0.0`` |
  591. +-----------+--------------------------------------------+
  592. Font embolden strength, if it is not equal to zero, emboldens the font outlines. Negative values reduce the outline thickness.
  593. - **face_index**, **fi**
  594. +-----------+--------------------------------------------+
  595. | `Values` | Integer number. |
  596. +-----------+--------------------------------------------+
  597. | `Default` | ``0`` |
  598. +-----------+--------------------------------------------+
  599. An active face index in the TrueType / OpenType collection.
  600. - **slant**, **sln**
  601. +-----------+--------------------------------------------+
  602. | `Values` | Floating-point number. |
  603. +-----------+--------------------------------------------+
  604. | `Default` | ``0.0`` |
  605. +-----------+--------------------------------------------+
  606. Font slant strength, positive values slant glyphs to the right. Negative values to the left.
  607. - **opentype_variation**, **otv**
  608. +-----------+----------------------------------------------------------------------------------+
  609. | `Values` | Comma-separated list of the OpenType variation tags (no space after each comma). |
  610. +-----------+----------------------------------------------------------------------------------+
  611. | `Default` | |
  612. +-----------+----------------------------------------------------------------------------------+
  613. Font OpenType variation coordinates. See `OpenType variation tags <https://docs.microsoft.com/en-us/typography/opentype/spec/dvaraxisreg>`__.
  614. Note: The value should be enclosed in ``"`` to allow using ``=`` inside it:
  615. .. code-block:: none
  616. [font otv="wght=200,wdth=400"] # Sets variable font weight and width.
  617. - **opentype_features**, **otf**
  618. +-----------+--------------------------------------------------------------------------------+
  619. | `Values` | Comma-separated list of the OpenType feature tags (no space after each comma). |
  620. +-----------+--------------------------------------------------------------------------------+
  621. | `Default` | |
  622. +-----------+--------------------------------------------------------------------------------+
  623. Font OpenType features. See `OpenType features tags <https://docs.microsoft.com/en-us/typography/opentype/spec/featuretags>`__.
  624. Note: The value should be enclosed in ``"`` to allow using ``=`` inside it:
  625. .. code-block:: none
  626. [font otf="calt=0,zero=1"] # Disable contextual alternates, enable slashed zero.
  627. .. _doc_bbcode_in_richtextlabel_named_colors:
  628. Named colors
  629. ~~~~~~~~~~~~
  630. For tags that allow specifying a color by name, you can use names of the constants from
  631. the built-in :ref:`class_Color` class. Named classes can be specified in a number of
  632. styles using different casings: ``DARK_RED``, ``DarkRed``, and ``darkred`` will give
  633. the same exact result.
  634. See this image for a list of color constants:
  635. .. image:: /img/color_constants.png
  636. `View at full size <https://raw.githubusercontent.com/godotengine/godot-docs/master/img/color_constants.png>`__
  637. .. _doc_bbcode_in_richtextlabel_hex_colors:
  638. Hexadecimal color codes
  639. ~~~~~~~~~~~~~~~~~~~~~~~
  640. For opaque RGB colors, any valid 6-digit hexadecimal code is supported, e.g.
  641. ``[color=#ffffff]white[/color]``. Shorthand RGB color codes such as ``#6f2``
  642. (equivalent to ``#66ff22``) are also supported.
  643. For transparent RGB colors, any RGBA 8-digit hexadecimal code can be used,
  644. e.g. ``[color=#ffffff88]translucent white[/color]``. Note that the alpha channel
  645. is the **last** component of the color code, not the first one. Short RGBA
  646. color codes such as ``#6f28`` (equivalent to ``#66ff2288``) are supported as well.
  647. .. _doc_bbcode_in_richtextlabel_cell_options:
  648. Cell options
  649. ~~~~~~~~~~~~
  650. - **expand**
  651. +-----------+--------------------------------------------+
  652. | `Values` | Integer number |
  653. +-----------+--------------------------------------------+
  654. | `Default` | 1 |
  655. +-----------+--------------------------------------------+
  656. Cell expansion ratio. This defines which cells will try to expand to
  657. proportionally to other cells and their expansion ratios.
  658. - **border**
  659. +-----------+--------------------------------------------+
  660. | `Values` | Color name or color in HEX format |
  661. +-----------+--------------------------------------------+
  662. | `Default` | Inherit |
  663. +-----------+--------------------------------------------+
  664. Cell border color.
  665. - **bg**
  666. +-----------+--------------------------------------------+
  667. | `Values` | Color name or color in HEX format |
  668. +-----------+--------------------------------------------+
  669. | `Default` | Inherit |
  670. +-----------+--------------------------------------------+
  671. Cell background color. For alternating odd/even row backgrounds,
  672. you can use ``bg=odd_color,even_color``.
  673. - **padding**
  674. +-----------+--------------------------------------------------------------------------+
  675. | `Values` | 4 comma-separated floating-point numbers (no space after each comma) |
  676. +-----------+--------------------------------------------------------------------------+
  677. | `Default` | ``0,0,0,0`` |
  678. +-----------+--------------------------------------------------------------------------+
  679. Left, top, right, and bottom cell padding.
  680. .. _doc_bbcode_in_richtextlabel_unordered_list_bullet:
  681. Unordered list bullet
  682. ~~~~~~~~~~~~~~~~~~~~~
  683. By default, the ``[ul]`` tag uses the ``U+2022`` "Bullet" Unicode glyph as the
  684. bullet character. This behavior is similar to web browsers. The bullet character
  685. can be customized using ``[ul bullet={bullet}]``. If provided, this ``{bullet}``
  686. parameter must be a *single* character with no enclosing quotes (for example,
  687. ``[bullet=*]``). Additional characters are ignored. The bullet character's
  688. width does not affect the list's formatting.
  689. See `Bullet (typography) on Wikipedia <https://en.wikipedia.org/wiki/Bullet_(typography)>`__
  690. for a list of common bullet characters that you can paste directly in the ``bullet`` parameter.
  691. .. _doc_bbcode_in_richtextlabel_list_types:
  692. Ordered list types
  693. ~~~~~~~~~~~~~~~~~~
  694. Ordered lists can be used to automatically mark items with numbers
  695. or letters in ascending order. This tag supports the following
  696. type options:
  697. - ``1`` - Numbers, using language specific numbering system if possible.
  698. - ``a``, ``A`` - Lower and upper case Latin letters.
  699. - ``i``, ``I`` - Lower and upper case Roman numerals.
  700. Text effects
  701. ------------
  702. BBCode can also be used to create different text effects that can optionally be
  703. animated. Five customizable effects are provided out of the box, and you can
  704. easily create your own. By default, animated effects will pause
  705. :ref:`when the SceneTree is paused <doc_pausing_games>`. You can change this
  706. behavior by adjusting the RichTextLabel's **Process > Mode** property.
  707. All examples below mention the default values for options in the listed tag format.
  708. .. note::
  709. Text effects that move characters' positions may result in characters being
  710. clipped by the RichTextLabel node bounds.
  711. You can resolve this by disabling **Control > Layout > Clip Contents** in
  712. the inspector after selecting the RichTextLabel node, or ensuring there is
  713. enough margin added around the text by using line breaks above and below the
  714. line using the effect.
  715. Pulse
  716. ~~~~~
  717. .. image:: img/bbcode_in_richtextlabel_effect_pulse.webp
  718. Pulse creates an animated pulsing effect that multiplies each character's
  719. opacity and color. It can be used to bring attention to specific text. Its tag
  720. format is ``[pulse freq=1.0 color=#ffffff40 ease=-2.0]{text}[/pulse]``.
  721. ``freq`` controls the frequency of the half-pulsing cycle (higher is faster). A
  722. full pulsing cycle takes ``2 * (1.0 / freq)`` seconds. ``color`` is the target
  723. color multiplier for blinking. The default mostly fades out text, but not
  724. entirely. ``ease`` is the easing function exponent to use. Negative values
  725. provide in-out easing, which is why the default is ``-2.0``.
  726. Wave
  727. ~~~~
  728. .. image:: img/bbcode_in_richtextlabel_effect_wave.webp
  729. Wave makes the text go up and down. Its tag format is
  730. ``[wave amp=50.0 freq=5.0 connected=1]{text}[/wave]``.
  731. ``amp`` controls how high and low the effect goes, and ``freq`` controls how
  732. fast the text goes up and down. A ``freq`` value of ``0`` will result in no
  733. visible waves, and negative ``freq`` values won't display any waves either. If
  734. ``connected`` is ``1`` (default), glyphs with ligatures will be moved together.
  735. If ``connected`` is ``0``, each glyph is moved individually even if they are
  736. joined by ligatures. This can work around certain rendering issues with font
  737. ligatures.
  738. Tornado
  739. ~~~~~~~
  740. .. image:: img/bbcode_in_richtextlabel_effect_tornado.webp
  741. Tornado makes the text move around in a circle. Its tag format is
  742. ``[tornado radius=10.0 freq=1.0 connected=1]{text}[/tornado]``.
  743. ``radius`` is the radius of the circle that controls the offset, ``freq`` is how
  744. fast the text moves in a circle. A ``freq`` value of ``0`` will pause the
  745. animation, while negative ``freq`` will play the animation backwards. If
  746. ``connected`` is ``1`` (default), glyphs with ligatures will be moved together.
  747. If ``connected`` is ``0``, each glyph is moved individually even if they are
  748. joined by ligatures. This can work around certain rendering issues with font
  749. ligatures.
  750. Shake
  751. ~~~~~
  752. .. image:: img/bbcode_in_richtextlabel_effect_shake.webp
  753. Shake makes the text shake. Its tag format is
  754. ``[shake rate=20.0 level=5 connected=1]{text}[/shake]``.
  755. ``rate`` controls how fast the text shakes, ``level`` controls how far the text
  756. is offset from the origin. If ``connected`` is ``1`` (default), glyphs with
  757. ligatures will be moved together. If ``connected`` is ``0``, each glyph is moved
  758. individually even if they are joined by ligatures. This can work around certain
  759. rendering issues with font ligatures.
  760. Fade
  761. ~~~~
  762. .. image:: img/bbcode_in_richtextlabel_effect_fade.webp
  763. Fade creates a static fade effect that multiplies each character's opacity.
  764. Its tag format is ``[fade start=4 length=14]{text}[/fade]``.
  765. ``start`` controls the starting position of the falloff relative to where the fade
  766. command is inserted, ``length`` controls over how many characters should the fade
  767. out take place.
  768. Rainbow
  769. ~~~~~~~
  770. .. image:: img/bbcode_in_richtextlabel_effect_rainbow.webp
  771. Rainbow gives the text a rainbow color that changes over time. Its tag format is
  772. ``[rainbow freq=1.0 sat=0.8 val=0.8 speed=1.0]{text}[/rainbow]``.
  773. ``freq`` determines how many letters the rainbow extends over before it repeats itself,
  774. ``sat`` is the saturation of the rainbow, ``val`` is the value of the rainbow. ``speed``
  775. is the number of full rainbow cycles per second. A positive ``speed`` value will play
  776. the animation forwards, a value of ``0`` will pause the animation, and a negative
  777. ``speed`` value will play the animation backwards.
  778. Font outlines are *not* affected by the rainbow effect (they keep their original color).
  779. Existing font colors are overridden by the rainbow effect. However, CanvasItem's
  780. **Modulate** and **Self Modulate** properties will affect how the rainbow effect
  781. looks, as modulation multiplies its final colors.
  782. Custom BBCode tags and text effects
  783. -----------------------------------
  784. You can extend the :ref:`class_RichTextEffect` resource type to create your own custom
  785. BBCode tags. Create a new script file that extends the :ref:`class_RichTextEffect` resource type
  786. and give the script a ``class_name`` so that the effect can be selected in the inspector.
  787. Add the ``@tool`` annotation to your GDScript file if you wish to have these custom effects
  788. run within the editor itself. The RichTextLabel does not need to have a script attached,
  789. nor does it need to be running in ``tool`` mode. The new effect can be registered in
  790. the Inspector by adding it to the **Markup > Custom Effects** array, or in code with the
  791. :ref:`install_effect() <class_RichTextLabel_method_install_effect>` method:
  792. .. figure:: img/bbcode_in_richtextlabel_selecting_custom_richtexteffect.webp
  793. :align: center
  794. :alt: Selecting a custom RichTextEffect after saving a script that extends RichTextEffect with a ``class_name``
  795. Selecting a custom RichTextEffect after saving a script that extends RichTextEffect with a ``class_name``
  796. .. warning::
  797. If the custom effect is not registered within the RichTextLabel's
  798. **Markup > Custom Effects** property, no effect will be visible and the original
  799. tag will be left as-is.
  800. There is only one function that you need to extend: ``_process_custom_fx(char_fx)``.
  801. Optionally, you can also provide a custom BBCode identifier by adding a member
  802. name ``bbcode``. The code will check the ``bbcode`` property automatically or will
  803. use the name of the file to determine what the BBCode tag should be.
  804. ``_process_custom_fx``
  805. ~~~~~~~~~~~~~~~~~~~~~~
  806. This is where the logic of each effect takes place and is called once per glyph
  807. during the draw phase of text rendering. This passes in a :ref:`class_CharFXTransform`
  808. object, which holds a few variables to control how the associated glyph is rendered:
  809. - ``identity`` specifies which custom effect is being processed. You should use that for
  810. code flow control.
  811. - ``outline`` is ``true`` if effect is called for drawing text outline.
  812. - ``range`` tells you how far into a given custom effect block you are in as an
  813. index.
  814. - ``elapsed_time`` is the total amount of time the text effect has been running.
  815. - ``visible`` will tell you whether the glyph is visible or not and will also allow you
  816. to hide a given portion of text.
  817. - ``offset`` is an offset position relative to where the given glyph should render under
  818. normal circumstances.
  819. - ``color`` is the color of a given glyph.
  820. - ``glyph_index`` and ``font`` is glyph being drawn and font data resource used to draw it.
  821. - Finally, ``env`` is a :ref:`class_Dictionary` of parameters assigned to a given custom
  822. effect. You can use :ref:`get() <class_Dictionary_method_get>` with an optional default value
  823. to retrieve each parameter, if specified by the user. For example ``[custom_fx spread=0.5
  824. color=#FFFF00]test[/custom_fx]`` would have a float ``spread`` and Color ``color``
  825. parameters in its ``env`` Dictionary. See below for more usage examples.
  826. The last thing to note about this function is that it is necessary to return a boolean
  827. ``true`` value to verify that the effect processed correctly. This way, if there's a problem
  828. with rendering a given glyph, it will back out of rendering custom effects entirely until
  829. the user fixes whatever error cropped up in their custom effect logic.
  830. Here are some examples of custom effects:
  831. Ghost
  832. ~~~~~
  833. ::
  834. @tool
  835. extends RichTextEffect
  836. class_name RichTextGhost
  837. # Syntax: [ghost freq=5.0 span=10.0][/ghost]
  838. # Define the tag name.
  839. var bbcode = "ghost"
  840. func _process_custom_fx(char_fx):
  841. # Get parameters, or use the provided default value if missing.
  842. var speed = char_fx.env.get("freq", 5.0)
  843. var span = char_fx.env.get("span", 10.0)
  844. var alpha = sin(char_fx.elapsed_time * speed + (char_fx.range.x / span)) * 0.5 + 0.5
  845. char_fx.color.a = alpha
  846. return true
  847. Matrix
  848. ~~~~~~
  849. ::
  850. @tool
  851. extends RichTextEffect
  852. class_name RichTextMatrix
  853. # Syntax: [matrix clean=2.0 dirty=1.0 span=50][/matrix]
  854. # Define the tag name.
  855. var bbcode = "matrix"
  856. # Gets TextServer for retrieving font information.
  857. func get_text_server():
  858. return TextServerManager.get_primary_interface()
  859. func _process_custom_fx(char_fx):
  860. # Get parameters, or use the provided default value if missing.
  861. var clear_time = char_fx.env.get("clean", 2.0)
  862. var dirty_time = char_fx.env.get("dirty", 1.0)
  863. var text_span = char_fx.env.get("span", 50)
  864. var value = char_fx.glyph_index
  865. var matrix_time = fmod(char_fx.elapsed_time + (char_fx.range.x / float(text_span)), \
  866. clear_time + dirty_time)
  867. matrix_time = 0.0 if matrix_time < clear_time else \
  868. (matrix_time - clear_time) / dirty_time
  869. if matrix_time > 0.0:
  870. value = int(1 * matrix_time * (126 - 65))
  871. value %= (126 - 65)
  872. value += 65
  873. char_fx.glyph_index = get_text_server().font_get_glyph_index(char_fx.font, 1, value, 0)
  874. return true
  875. This will add a few new BBCode commands, which can be used like so:
  876. .. code-block:: none
  877. [center][ghost]This is a custom [matrix]effect[/matrix][/ghost] made in
  878. [pulse freq=5.0 height=2.0][pulse color=#00FFAA freq=2.0]GDScript[/pulse][/pulse].[/center]