bbcode_in_richtextlabel.rst 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. .. _doc_bbcode_in_richtextlabel:
  2. BBCode in RichTextLabel
  3. =======================
  4. Introduction
  5. ------------
  6. Label nodes are great for displaying basic text, but they have limits. If you want
  7. to change the color of the text, or its alignment, that change affects all of the
  8. text in the Label node. You can't have only one part of the text be one color, or
  9. only one part of the text be centered. To get around this limitation you would use
  10. a :ref:`class_RichTextLabel`.
  11. :ref:`class_RichTextLabel` allows the display of complex text markup in a Control.
  12. It has a built-in API for generating the markup, but can also parse a BBCode.
  13. Note that the BBCode tags can also be used, to some extent, in the XML source of
  14. the class reference. For more information, see
  15. :ref:`doc_class_reference_writing_guidelines`.
  16. Using BBCode
  17. ------------
  18. For uniformly formatted text you can write in the "Text" property, but if you want
  19. to use BBCode markup you should use the "Text" property in the "Bb Code" section
  20. instead (``bbcode_text``). Writing to this property will trigger the parsing of your
  21. markup to format the text as requested. Before this happens, you need to toggle the
  22. "Enabled" checkbox in the "Bb Code" section (``bbcode_enabled``).
  23. .. image:: img/bbcodeText.png
  24. For example, ``BBCode [color=blue]blue[/color]`` would render the word "blue" with
  25. a blue color.
  26. .. image:: img/bbcodeDemo.png
  27. You'll notice that after writing in the BBCode "Text" property the regular "Text"
  28. property now has the text without the BBCode. While the text property will be updated
  29. by the BBCode property, you can't edit the text property or you'll lose the BBCode
  30. markup. All changes to the text must be done in the BBCode parameter.
  31. .. note::
  32. For BBCode tags such as ``[b]`` (bold), ``[i]`` (italics) or ``[code]`` to
  33. work, you must set up custom fonts for the RichTextLabel node first.
  34. There are no BBCode tags to control vertical centering of text yet.
  35. Reference
  36. ---------
  37. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  38. | Command | Tag | Description |
  39. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  40. | **bold** | ``[b]{text}[/b]`` | Makes {text} bold. |
  41. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  42. | **italics** | ``[i]{text}[/i]`` | Makes {text} italics. |
  43. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  44. | **underline** | ``[u]{text}[/u]`` | Makes {text} underline. |
  45. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  46. | **strikethrough** | ``[s]{text}[/s]`` | Makes {text} strikethrough. |
  47. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  48. | **code** | ``[code]{text}[/code]`` | Makes {text} use the code font (which is typically monospace). |
  49. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  50. | **p** | ``[p {options}]{text}[/p]`` | Adds new paragraph with {text}. See paragraph options for more info. |
  51. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  52. | **center** | ``[center]{text}[/center]`` | Makes {text} horizontally centered. Same as ``[p align=center]``. |
  53. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  54. | **left** | ``[left]{text}[/left]`` | Makes {text} horizontally right-aligned. Same as ``[p align=left]``. |
  55. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  56. | **right** | ``[right]{text}[/right]`` | Makes {text} horizontally right-aligned. Same as ``[p align=right]``. |
  57. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  58. | **fill** | ``[fill]{text}[/fill]`` | Makes {text} fill the RichTextLabel's width. Same as ``[p align=fill]``. |
  59. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  60. | **indent** | ``[indent]{text}[/indent]`` | Increase the indentation level of {text}. |
  61. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  62. | **url** | ``[url]{url}[/url]`` | Show {url} as such, underline it and make it clickable. |
  63. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  64. | **url (ref)** | ``[url=<url>]{text}[/url]`` | Makes {text} reference <url> (underlined and clickable). |
  65. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  66. | **image** | ``[img]{path}[/img]`` | Insert image at resource {path}. |
  67. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  68. | **resized image** | ``[img=<width>]{path}[/img]`` | Insert image at resource {path} using <width> (keeps ratio). |
  69. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  70. | **resized image** | ``[img=<width>x<height>]{path}[/img]`` | Insert image at resource {path} using <width>×<height>. |
  71. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  72. | **aligned image** | ``[img align=<valign>]{path}[/img]`` | Insert image at resource {path} using <valign> vertical alignment. |
  73. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  74. | **font** | ``[font=<path>]{text}[/font]`` | Use custom font at <path> for {text}. |
  75. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  76. | **font options** | ``[font {options}]{text}[/font]`` | Use custom font options for the {text}. See font options for more info. |
  77. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  78. | **font size** | ``[font_size=nn]{text}[/font_size]`` | Use custom font size for {text}. |
  79. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  80. | **opentype features** | ``[opentype_features=ftr,ftr]{text}[/opentype_features]`` | Use custom OpenType font features (comma separated list) for {text}. |
  81. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  82. | **outline size** | ``[outline_size=<size>]{text}[/outline_size]`` | Use custom font outline size for {text}. |
  83. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  84. | **outline color** | ``[outline_color=<color>]{text}[/outline_color]`` | Use custom outline color for {text}. |
  85. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  86. | **color** | ``[color=<code/name>]{text}[/color]`` | Change {text} color; use name or # format, such as ``#ff00ff``. |
  87. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  88. | **table** | ``[table=<number>]{cells}[/table]`` | Creates a table with <number> of columns. |
  89. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  90. | **cell** | ``[cell=<expand ratio>]{text}[/cell]`` | Adds cells with the {text} to the table. |
  91. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  92. | **cell** | ``[cell {options}]{text}[/cell]`` | Adds cells with the {text} to the table. See cell options for more info. |
  93. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  94. | **list** | ``[ul]{one item per line}[/ul]`` | Add unnumbered list. |
  95. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  96. | **list** | ``[ol type=<type>]{one item per line}[/ol]`` | Add numbered list. See list types for more info. |
  97. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  98. | **unicode control** | ``[lrm]``, ``[rlm]``, ``[lre]``, ``[rle]``, ``[lro]``, | Adds Unicode control character. |
  99. | | ``[rlo]``, ``[pdf]``, ``[alm]``, ``[lri]``, ``[rli]``, | |
  100. | | ``[fsi]``, ``[pdi]``, ``[zwj]``, ``[zwnj]``, ``[wj]`` | |
  101. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  102. Note: Options are optional for all tags.
  103. Paragraph options
  104. ~~~~~~~~~~~~~~~~~
  105. +----------------------------+---------------------------------------------------------------------------+----------------------------+
  106. | Option | Supported Values | Description |
  107. +----------------------------+---------------------------------------------------------------------------+----------------------------+
  108. | ``align`` | ``left``, ``center``, ``right``, ``fill`` | Text horizontal alignment. |
  109. +----------------------------+---------------------------------------------------------------------------+----------------------------+
  110. | ``direction`` or ``dir`` | ``ltr``, ``rtl``, ``auto`` | Base BiDi direction. |
  111. +----------------------------+---------------------------------------------------------------------------+----------------------------+
  112. | ``language`` or ``lang`` | ISO language codes | Locale override. |
  113. +----------------------------+---------------------------------------------------------------------------+----------------------------+
  114. | ``bidi_override`` or `st` | ``default``, ``uri``, ``file``, ``email``, ``list``, ``none``, ``custom`` | Structured text override. |
  115. +----------------------------+---------------------------------------------------------------------------+----------------------------+
  116. Font options
  117. ~~~~~~~~~~~~
  118. +----------------------------+-------------------------------------------------+
  119. | Option | Description |
  120. +----------------------------+-------------------------------------------------+
  121. | ``name`` or ``n`` | Font resource path. |
  122. +----------------------------+-------------------------------------------------+
  123. | ``size`` or ``s`` | Font size. |
  124. +----------------------------+-------------------------------------------------+
  125. Cell options
  126. ~~~~~~~~~~~~
  127. - ``expand`` - Column expansion ratio, the column expands in proportion to its expansion ratio versus the other columns' ratios.
  128. - ``border`` - Sets cell border color.
  129. - ``bg`` - Sets cell background color, for alternating odd/even row backgrounds use ``bg=odd_color,even_color``.
  130. List types
  131. ~~~~~~~~~~
  132. Supported list types:
  133. - ``1`` - Numbers, using language specific numbering system if possible.
  134. - ``a`` and ``A`` - Lower and upper case Latin letters.
  135. - ``i`` and ``I`` - Lower and upper case Roman numerals.
  136. Named colors
  137. ~~~~~~~~~~~~
  138. You can use the constants of the :ref:`class_Color` class for the ``[color=<name>]`` tag.
  139. The case and style of the name is not important: ``DARK_RED``, ``DarkRed`` and ``darkred`` will give the same result.
  140. Hexadecimal color codes
  141. ~~~~~~~~~~~~~~~~~~~~~~~
  142. For opaque RGB colors, any valid 6-digit hexadecimal code is supported, e.g. ``[color=#ffffff]white[/color]``.
  143. For transparent RGB colors, any 8-digit hexadecimal code can be used, e.g. ``[color=#88ffffff]translucent white[/color]``.
  144. In this case, note that the alpha channel is the **first** component of the color code, not the last one.
  145. Image vertical offset
  146. ~~~~~~~~~~~~~~~~~~~~~
  147. Use ``[img=align]...[/img]`` to set vertical alignment of the image, where ``align`` is ``t`` (top), ``c`` (center) or ``b`` (bottom).
  148. Animation effects
  149. -----------------
  150. BBCode can also be used to create different text animation effects. Five customizable
  151. effects are provided out of the box, and you can easily create your own.
  152. Wave
  153. ~~~~
  154. .. image:: img/wave.png
  155. Wave makes the text go up and down. Its tag format is ``[wave amp=50 freq=2][/wave]``.
  156. ``amp`` controls how high and low the effect goes, and ``freq`` controls how fast the
  157. text goes up and down.
  158. Tornado
  159. ~~~~~~~
  160. .. image:: img/tornado.png
  161. Tornao makes the text move around in a circle. Its tag format is
  162. ``[tornado radius=5 freq=2][/tornado]``.
  163. ``radius`` is the radius of the circle that controls the offset, ``freq`` is how
  164. fast the text moves in a circle.
  165. Shake
  166. ~~~~~
  167. .. image:: img/shake.png
  168. Shake makes the text shake. Its tag format is ``[shake rate=5 level=10][/shake]``.
  169. ``rate`` controls how fast the text shakes, ``level`` controls how far the text is
  170. offset from the origin.
  171. Fade
  172. ~~~~
  173. .. image:: img/fade.png
  174. Fade creates a fade effect over the text that is not animated. Its tag format is
  175. ``[fade start=4 length=14][/fade]``.
  176. ``start`` controls the starting position of the falloff relative to where the fade
  177. command is inserted, ``length`` controls over how many characters should the fade
  178. out take place.
  179. Rainbow
  180. ~~~~~~~
  181. .. image:: img/rainbow.png
  182. Rainbow gives the text a rainbow color that changes over time. Its tag format is
  183. ``[rainbow freq=0.2 sat=10 val=20][/rainbow]``.
  184. ``freq`` is the number of full rainbow cycles per second, ``sat`` is the saturation
  185. of the rainbow, ``val`` is the value of the rainbow.
  186. Custom BBCode tags and text effects
  187. -----------------------------------
  188. You can extend the :ref:`class_RichTextEffect` resource type to create your own custom
  189. BBCode tags. You begin by extending the :ref:`class_RichTextEffect` resource type. Add
  190. the ``tool`` prefix to your GDScript file if you wish to have these custom effects run
  191. within the editor itself. The RichTextLabel does not need to have a script attached,
  192. nor does it need to be running in ``tool`` mode. The new effect will be activable in
  193. the Inspector through the **Custom Effects** property.
  194. There is only one function that you need to extend: ``_process_custom_fx(char_fx)``.
  195. Optionally, you can also provide a custom BBCode identifier simply by adding a member
  196. name ``bbcode``. The code will check the ``bbcode`` property automatically or will
  197. use the name of the file to determine what the BBCode tag should be.
  198. ``_process_custom_fx``
  199. ~~~~~~~~~~~~~~~~~~~~~~
  200. This is where the logic of each effect takes place and is called once per glyph
  201. during the draw phase of text rendering. This passes in a :ref:`class_CharFXTransform`
  202. object, which holds a few variables to control how the associated glyph is rendered:
  203. - ``identity`` specifies which custom effect is being processed. You should use that for
  204. code flow control.
  205. - ``outline`` is ``true`` if effect is called for drawing text outline.
  206. - ``range`` tells you how far into a given custom effect block you are in as an
  207. index.
  208. - ``elapsed_time`` is the total amount of time the text effect has been running.
  209. - ``visible`` will tell you whether the glyph is visible or not and will also allow you
  210. to hide a given portion of text.
  211. - ``offset`` is an offset position relative to where the given glyph should render under
  212. normal circumstances.
  213. - ``color`` is the color of a given glyph.
  214. - ``glyph_index`` and ``font`` is glyph being drawn and font data resource used to draw it.
  215. - Finally, ``env`` is a :ref:`class_Dictionary` of parameters assigned to a given custom
  216. effect. You can use :ref:`get() <class_Dictionary_method_get>` with an optional default value
  217. to retrieve each parameter, if specified by the user. For example ``[custom_fx spread=0.5
  218. color=#FFFF00]test[/custom_fx]`` would have a float ``spread`` and Color ``color``
  219. parameters in its ` `env`` Dictionary. See below for more usage examples.
  220. The last thing to note about this function is that it is necessary to return a boolean
  221. ``true`` value to verify that the effect processed correctly. This way, if there's a problem
  222. with rendering a given glyph, it will back out of rendering custom effects entirely until
  223. the user fixes whatever error cropped up in their custom effect logic.
  224. Here are some examples of custom effects:
  225. Ghost
  226. ~~~~~
  227. ::
  228. tool
  229. extends RichTextEffect
  230. class_name RichTextGhost
  231. # Syntax: [ghost freq=5.0 span=10.0][/ghost]
  232. # Define the tag name.
  233. var bbcode = "ghost"
  234. func _process_custom_fx(char_fx):
  235. # Get parameters, or use the provided default value if missing.
  236. var speed = char_fx.env.get("freq", 5.0)
  237. var span = char_fx.env.get("span", 10.0)
  238. var alpha = sin(char_fx.elapsed_time * speed + (char_fx.absolute_index / span)) * 0.5 + 0.5
  239. char_fx.color.a = alpha
  240. return true
  241. Pulse
  242. ~~~~~
  243. ::
  244. tool
  245. extends RichTextEffect
  246. class_name RichTextPulse
  247. # Syntax: [pulse color=#00FFAA height=0.0 freq=2.0][/pulse]
  248. # Define the tag name.
  249. var bbcode = "pulse"
  250. func _process_custom_fx(char_fx):
  251. # Get parameters, or use the provided default value if missing.
  252. var color = char_fx.env.get("color", char_fx.color)
  253. var height = char_fx.env.get("height", 0.0)
  254. var freq = char_fx.env.get("freq", 2.0)
  255. var sined_time = (sin(char_fx.elapsed_time * freq) + 1.0) / 2.0
  256. var y_off = sined_time * height
  257. color.a = 1.0
  258. char_fx.color = char_fx.color.linear_interpolate(color, sined_time)
  259. char_fx.offset = Vector2(0, -1) * y_off
  260. return true
  261. Matrix
  262. ~~~~~~
  263. ::
  264. tool
  265. extends RichTextEffect
  266. class_name RichTextMatrix
  267. # Syntax: [matrix clean=2.0 dirty=1.0 span=50][/matrix]
  268. # Define the tag name.
  269. var bbcode = "matrix"
  270. func _process_custom_fx(char_fx):
  271. # Get parameters, or use the provided default value if missing.
  272. var clear_time = char_fx.env.get("clean", 2.0)
  273. var dirty_time = char_fx.env.get("dirty", 1.0)
  274. var text_span = char_fx.env.get("span", 50)
  275. var matrix_time = fmod(char_fx.elapsed_time + (char_fx.range.x / float(text_span)), \
  276. clear_time + dirty_time)
  277. matrix_time = 0.0 if matrix_time < clear_time else \
  278. (matrix_time - clear_time) / dirty_time
  279. if matrix_time > 0.0:
  280. value = int(1 * matrix_time * (126 - 65))
  281. value %= (126 - 65)
  282. value += 65
  283. char_fx.glyph_index = TextServer.font_get_glyph_index(char_fx.font, value)
  284. return true
  285. This will add a few new BBCode commands, which can be used like so:
  286. ::
  287. [center][ghost]This is a custom [matrix]effect[/matrix][/ghost] made in
  288. [pulse freq=5.0 height=2.0][pulse color=#00FFAA freq=2.0]GDScript[/pulse][/pulse].[/center]