bbcode_in_richtextlabel.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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
  14. :ref:`XML source of the class reference <doc_updating_the_class_reference>`.
  15. Using BBCode
  16. ------------
  17. For uniformly formatted text you can write in the "Text" property, but if you want
  18. to use BBCode markup you should use the "Text" property in the "Bb Code" section
  19. instead (``bbcode_text``). Writing to this property will trigger the parsing of your
  20. markup to format the text as requested. Before this happens, you need to toggle the
  21. "Enabled" checkbox in the "Bb Code" section (``bbcode_enabled``).
  22. .. image:: img/bbcodeText.png
  23. For example, ``BBCode [color=blue]blue[/color]`` would render the word "blue" with
  24. a blue color.
  25. .. image:: img/bbcodeDemo.png
  26. You'll notice that after writing in the BBCode "Text" property the regular "Text"
  27. property now has the text without the BBCode. While the text property will be updated
  28. by the BBCode property, you can't edit the text property or you'll lose the BBCode
  29. markup. All changes to the text must be done in the BBCode parameter.
  30. .. note::
  31. For BBCode tags such as ``[b]`` (bold), ``[i]`` (italics) or ``[code]`` to
  32. work, you must set up custom fonts for the RichTextLabel node first.
  33. There are no BBCode tags to control vertical centering of text yet.
  34. Reference
  35. ---------
  36. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  37. | Command | Tag | Description |
  38. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  39. | **bold** | ``[b]{text}[/b]`` | Makes {text} bold. |
  40. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  41. | **italics** | ``[i]{text}[/i]`` | Makes {text} italics. |
  42. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  43. | **underline** | ``[u]{text}[/u]`` | Makes {text} underline. |
  44. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  45. | **strikethrough** | ``[s]{text}[/s]`` | Makes {text} strikethrough. |
  46. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  47. | **code** | ``[code]{text}[/code]`` | Makes {text} use the code font (which is typically monospace). |
  48. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  49. | **center** | ``[center]{text}[/center]`` | Makes {text} horizontally centered. |
  50. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  51. | **right** | ``[right]{text}[/right]`` | Makes {text} horizontally right-aligned. |
  52. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  53. | **fill** | ``[fill]{text}[/fill]`` | Makes {text} fill the RichTextLabel's width. |
  54. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  55. | **indent** | ``[indent]{text}[/indent]`` | Increase the indentation level of {text}. |
  56. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  57. | **url** | ``[url]{url}[/url]`` | Show {url} as such, underline it and make it clickable. |
  58. | | | **Must be handled with the "meta_clicked" signal to have an effect.** |
  59. | | | See :ref:`doc_bbcode_in_richtextlabel_handling_url_tag_clicks`. |
  60. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  61. | **url (ref)** | ``[url=<url>]{text}[/url]`` | Makes {text} reference <url> (underlined and clickable). |
  62. | | | **Must be handled with the "meta_clicked" signal to have an effect.** |
  63. | | | See :ref:`doc_bbcode_in_richtextlabel_handling_url_tag_clicks`. |
  64. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  65. | **image** | ``[img]{path}[/img]`` | Insert image at resource {path}. |
  66. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  67. | **resized image** | ``[img=<width>]{path}[/img]`` | Insert image at resource {path} using <width> (keeps ratio). |
  68. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  69. | **resized image** | ``[img=<width>x<height>]{path}[/img]`` | Insert image at resource {path} using <width>×<height>. |
  70. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  71. | **font** | ``[font=<path>]{text}[/font]`` | Use custom font at <path> for {text}. |
  72. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  73. | **color** | ``[color=<code/name>]{text}[/color]`` | Change {text} color; use name or # format, such as ``#ff00ff``. |
  74. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  75. | **table** | ``[table=<number>]{cells}[/table]`` | Creates a table with <number> of columns. |
  76. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  77. | **cell** | ``[cell]{text}[/cell]`` | Adds cells with the {text} to the table. |
  78. +-----------------------+-----------------------------------------------------------+--------------------------------------------------------------------------+
  79. Built-in color names
  80. ~~~~~~~~~~~~~~~~~~~~
  81. List of valid color names for the [color=<name>] tag:
  82. - aqua
  83. - black
  84. - blue
  85. - fuchsia
  86. - gray
  87. - green
  88. - lime
  89. - maroon
  90. - navy
  91. - purple
  92. - red
  93. - silver
  94. - teal
  95. - white
  96. - yellow
  97. Hexadecimal color codes
  98. ~~~~~~~~~~~~~~~~~~~~~~~
  99. For opaque RGB colors, any valid 6-digit hexadecimal code is supported, e.g. ``[color=#ffffff]white[/color]``.
  100. Short RGB color codes such as ``#6f2`` (equivalent to ``#66ff22``) are also supported.
  101. For transparent RGB colors, any 8-digit hexadecimal code can be used, e.g. ``[color=#88ffffff]translucent white[/color]``.
  102. In this case, note that the alpha channel is the **first** component of the color code, not the last one.
  103. Short RGBA color codes such as ``#86f2`` (equivalent to ``#8866ff22``) are also supported.
  104. .. _doc_bbcode_in_richtextlabel_handling_url_tag_clicks:
  105. Handling ``[url]`` tag clicks
  106. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  107. By default, ``[url]`` tags do nothing when clicked. This is to allow flexible use
  108. of ``[url]`` tags rather than limiting them to opening URLs in a web browser.
  109. To handle clicked ``[url]`` tags, connect the RichTextLabel node's
  110. :ref:`meta_clicked <class_RichTextLabel_signal_meta_clicked>` signal to a script function.
  111. For example, the following method can be connected to ``meta_clicked`` to open
  112. clicked URLs using the user's default web browser::
  113. # This assumes RichTextLabel's `meta_clicked` signal was connected to
  114. # the function below using the signal connection dialog.
  115. func _richtextlabel_on_meta_clicked(meta):
  116. # `meta` is not guaranteed to be a String, so convert it to a String
  117. # to avoid script errors at run-time.
  118. OS.shell_open(str(meta))
  119. For more advanced use cases, it's also possible to store JSON in an ``[url]``
  120. tag's option and parse it in the function that handles the ``meta_clicked`` signal.
  121. For example: ``[url={"example": "value"}]JSON[/url]``
  122. Image vertical offset
  123. ~~~~~~~~~~~~~~~~~~~~~
  124. You use a custom font for your image in order to align it vertically.
  125. 1. Create a ``BitmapFont`` resource
  126. 2. Set this bitmap font with a positive value for the ``ascent`` property, that's your height offset
  127. 3. Set the BBCode tag this way: ``[font=<font-path>][img]{image-path}[/img][/font]``
  128. Animation effects
  129. -----------------
  130. BBCode can also be used to create different text animation effects. Five customizable
  131. effects are provided out of the box, and you can easily create your own.
  132. Wave
  133. ~~~~
  134. .. image:: img/wave.png
  135. Wave makes the text go up and down. Its tag format is ``[wave amp=50 freq=2][/wave]``.
  136. ``amp`` controls how high and low the effect goes, and ``freq`` controls how fast the
  137. text goes up and down.
  138. Tornado
  139. ~~~~~~~
  140. .. image:: img/tornado.png
  141. Tornao makes the text move around in a circle. Its tag format is
  142. ``[tornado radius=5 freq=2][/tornado]``.
  143. ``radius`` is the radius of the circle that controls the offset, ``freq`` is how
  144. fast the text moves in a circle.
  145. Shake
  146. ~~~~~
  147. .. image:: img/shake.png
  148. Shake makes the text shake. Its tag format is ``[shake rate=5 level=10][/shake]``.
  149. ``rate`` controls how fast the text shakes, ``level`` controls how far the text is
  150. offset from the origin.
  151. Fade
  152. ~~~~
  153. .. image:: img/fade.png
  154. Fade creates a fade effect over the text that is not animated. Its tag format is
  155. ``[fade start=4 length=14][/fade]``.
  156. ``start`` controls the starting position of the falloff relative to where the fade
  157. command is inserted, ``length`` controls over how many characters should the fade
  158. out take place.
  159. Rainbow
  160. ~~~~~~~
  161. .. image:: img/rainbow.png
  162. Rainbow gives the text a rainbow color that changes over time. Its tag format is
  163. ``[rainbow freq=0.2 sat=10 val=20][/rainbow]``.
  164. ``freq`` is the number of full rainbow cycles per second, ``sat`` is the saturation
  165. of the rainbow, ``val`` is the value of the rainbow.
  166. Custom BBCode tags and text effects
  167. -----------------------------------
  168. You can extend the :ref:`class_RichTextEffect` resource type to create your own custom
  169. BBCode tags. You begin by extending the :ref:`class_RichTextEffect` resource type. Add
  170. the ``tool`` prefix to your GDScript file if you wish to have these custom effects run
  171. within the editor itself. The RichTextLabel does not need to have a script attached,
  172. nor does it need to be running in ``tool`` mode. The new effect will be activable in
  173. the Inspector through the **Custom Effects** property.
  174. There is only one function that you need to extend: ``_process_custom_fx(char_fx)``.
  175. Optionally, you can also provide a custom BBCode identifier simply by adding a member
  176. name ``bbcode``. The code will check the ``bbcode`` property automatically or will
  177. use the name of the file to determine what the BBCode tag should be.
  178. ``_process_custom_fx``
  179. ~~~~~~~~~~~~~~~~~~~~~~
  180. This is where the logic of each effect takes place and is called once per character
  181. during the draw phase of text rendering. This passes in a :ref:`class_CharFXTransform`
  182. object, which holds a few variables to control how the associated character is rendered:
  183. - ``identity`` specifies which custom effect is being processed. You should use that for
  184. code flow control.
  185. - ``relative_index`` tells you how far into a given custom effect block you are in as an
  186. index.
  187. - ``absolute_index`` tells you how far into the entire text you are as an index.
  188. - ``elapsed_time`` is the total amount of time the text effect has been running.
  189. - ``visible`` will tell you whether the character is visible or not and will also allow you
  190. to hide a given portion of text.
  191. - ``offset`` is an offset position relative to where the given character should render under
  192. normal circumstances.
  193. - ``color`` is the color of a given character.
  194. - Finally, ``env`` is a :ref:`class_Dictionary` of parameters assigned to a given custom
  195. effect. You can use :ref:`get() <class_Dictionary_method_get>` with an optional default value
  196. to retrieve each parameter, if specified by the user. For example ``[custom_fx spread=0.5
  197. color=#FFFF00]test[/custom_fx]`` would have a float ``spread`` and Color ``color``
  198. parameters in its ` `env`` Dictionary. See below for more usage examples.
  199. The last thing to note about this function is that it is necessary to return a boolean
  200. ``true`` value to verify that the effect processed correctly. This way, if there's a problem
  201. with rendering a given character, it will back out of rendering custom effects entirely until
  202. the user fixes whatever error cropped up in their custom effect logic.
  203. Here are some examples of custom effects:
  204. Ghost
  205. ~~~~~
  206. ::
  207. tool
  208. extends RichTextEffect
  209. class_name RichTextGhost
  210. # Syntax: [ghost freq=5.0 span=10.0][/ghost]
  211. # Define the tag name.
  212. var bbcode = "ghost"
  213. func _process_custom_fx(char_fx):
  214. # Get parameters, or use the provided default value if missing.
  215. var speed = char_fx.env.get("freq", 5.0)
  216. var span = char_fx.env.get("span", 10.0)
  217. var alpha = sin(char_fx.elapsed_time * speed + (char_fx.absolute_index / span)) * 0.5 + 0.5
  218. char_fx.color.a = alpha
  219. return true
  220. Pulse
  221. ~~~~~
  222. ::
  223. tool
  224. extends RichTextEffect
  225. class_name RichTextPulse
  226. # Syntax: [pulse color=#00FFAA height=0.0 freq=2.0][/pulse]
  227. # Define the tag name.
  228. var bbcode = "pulse"
  229. func _process_custom_fx(char_fx):
  230. # Get parameters, or use the provided default value if missing.
  231. var color = char_fx.env.get("color", char_fx.color)
  232. var height = char_fx.env.get("height", 0.0)
  233. var freq = char_fx.env.get("freq", 2.0)
  234. var sined_time = (sin(char_fx.elapsed_time * freq) + 1.0) / 2.0
  235. var y_off = sined_time * height
  236. color.a = 1.0
  237. char_fx.color = char_fx.color.linear_interpolate(color, sined_time)
  238. char_fx.offset = Vector2(0, -1) * y_off
  239. return true
  240. Matrix
  241. ~~~~~~
  242. ::
  243. tool
  244. extends RichTextEffect
  245. class_name RichTextMatrix
  246. # Syntax: [matrix clean=2.0 dirty=1.0 span=50][/matrix]
  247. # Define the tag name.
  248. var bbcode = "matrix"
  249. func _process_custom_fx(char_fx):
  250. # Get parameters, or use the provided default value if missing.
  251. var clear_time = char_fx.env.get("clean", 2.0)
  252. var dirty_time = char_fx.env.get("dirty", 1.0)
  253. var text_span = char_fx.env.get("span", 50)
  254. var value = char_fx.character
  255. var matrix_time = fmod(char_fx.elapsed_time + (char_fx.absolute_index / float(text_span)), \
  256. clear_time + dirty_time)
  257. matrix_time = 0.0 if matrix_time < clear_time else \
  258. (matrix_time - clear_time) / dirty_time
  259. if value >= 65 && value < 126 && matrix_time > 0.0:
  260. value -= 65
  261. value = value + int(1 * matrix_time * (126 - 65))
  262. value %= (126 - 65)
  263. value += 65
  264. char_fx.character = value
  265. return true
  266. This will add a few new BBCode commands, which can be used like so:
  267. ::
  268. [center][ghost]This is a custom [matrix]effect[/matrix][/ghost] made in
  269. [pulse freq=5.0 height=2.0][pulse color=#00FFAA freq=2.0]GDScript[/pulse][/pulse].[/center]