Defold-RichText


#1

Wouldn’t it be great to be able to change the font of a flowing text or the colour of a word? This and more is now possible thanks to the Defold-RichText project!

With Defold-RichText it’s possible to turn this:

"<size=3>RichText</size>Lorem <color=0,0.5,0,1>ipsum </color>dolor <color=red>sit </color><color=#ff00ffff>amet, </color><size=1.15><font=Nanum>consectetur </font></size>adipiscing elit. <b>Nunc </b>tincidunt <b><i>mattis</i> libero</b> <i>non viverra</i>.\n\nNullam ornare accumsan rhoncus.\n\nNunc placerat nibh a purus auctor, id scelerisque massa rutrum."

Into this:

NOTE: There’s still some performance improvements to make, specifically around grouping multiple words with the same style into a single text node. Also still left to do is to apply layers to generated text nodes to reduce draw calls.


Rebuilding a label letter-by-letter using gui.get_text_metrics_from_node
#2

I was actually going to make a post saying how great the new label system is (or, releatively new… i don’t know when it was introduced). I will have a look at this next time.


#3

This is really awesome! Cool work! I think I’ll use this for our game and I have a few PR ideas already:

  1. It would be cool if you could use something like <tag=my_tag>some text</tag> to tag some nodes with a tag and the function would return you a table with just the tagged nodes for each tag, so you could apply custom stuff like text shake on parts of the text.
  2. It would be great if the function would return the total width/height of the text. I’ll need this for my game as my text tends to reside in 9-slice dialog bubbles.
  3. Vertical / horizontal align.

#4

That’s a good idea.https://github.com/britzl/defold-richtext/issues/2

Hmm, how would the vertical align work?


#5

Another useful thing would be to pass the text blob through a helper function, ignore the tags in length (but still return them properly), and use a counter to return text up to the number of characters in the counter. So dialogs can easily have properly styled text but appearing one character after another.

This could also be extended to produce per character effects like

34626819-0c96f898-f213-11e7-8fbc-6cb48926a123


#6

so you could apply custom stuff like text shake on parts of the text

This would be awesome. Text shake is one of the things that I really enjoy about a game I have been playing recently - Celeste. It’s funny how much better a text shake/wave is at conveying emotion compared to the conventional boldness.

giphy

Oh and then there is the infamously funky JJ2 menu screen. :smiley:

giphy2


#7

For horizontal align, you would subtract 0, line_width / 2, line_width from the X coord of all node positions of each line for left, center, right align respectively.

For vertical align you would add 0, total_block_height / 2, total_block_height to the Y coord of all node positions for bottom, middle, top align respectively.

Unfortunately you can’t do anything that’s not bottom left align in one single pass, as node positions depend on the metrics of all the other nodes, but it’s doable in two passes.


#8

Yup, got it. Horizontally center each line.

This is for aligning within each line right? In the case where you have different sized elements?


#9

Fixed in Defold-RichText 1.4.0. richtext.create() now returns both nodes table and metrics table


#10

No. I mean aligning the whole block of text relative to the origin of the container. Similar to how the North/Center/South pivots work for normal labels.


#11

Ah, ok. If you pass in a parent node that the text nodes will be childed to then it should be trivial to vertically align the parent based on height of the entire text to achieve what you want.


#12

Fair enough. You’d have an extra parent node to manage, though. I guess implementing this in the library vs letting the user implement this is just a matter of usage convenience vs separation of concerns.


#13

Released 2.0.0 with support for getting tagged text (and custom nodes). Get all words with a <b> tag:

local words, metrics = richtext.create(...)
local bold_words = richtext.tagged(words, "b")
for _,bold_word in pairs(bold_words) do
	print(bold_word.text, bold_word.node)
end

And you can use custom tags as well:

local text = "Lorem ipsum dolor sit amet, <foo>consectetur adipiscing elit</foo>. Aliquam vehicula purus sit amet orci molestie vulputate."
local words, metrics = richtext.create(text, ...)
local foo_words = richtext.tagged(words, "foo")
for _,word in pairs(foo_words) do
	print(word.text, word.node)
end

#14

I think this would be very useful for chat applications as well.

Any chance it can support smileys/images which can be inserted within the text?


#15

Yes, already planned: https://github.com/britzl/defold-richtext/issues/4


#16

Added support for images in 2.1.0:

<img=texture:image></img>

Example:

"<size=3>RichText</size>Lorem <color=0,0.5,0,1>ipsum </color><img=smileys:zombie></img> dolor <color=red>sit </color><color=#ff00ffff>amet, </color><size=1.15><font=Nanum>consectetur </font></size>adipiscing elit. <b>Nunc </b>tincidunt <b><i>mattis</i> libero</b> <i>non viverra</i>.\n\nNullam ornare <img=smileys:hungry></img>accumsan rhoncus.\n\nNunc placerat nibh a purus auctor, id scelerisque massa <size=2>rutrum.</size>"

Result:


#17

Empty tags are now supported:

<img=texture:image/>
<br/>

#18

Nice job!!
Can the tags have multiple properties? like HTML? E.g. <img src='#joystick' anim='wiggle'/>


#19

I guess it should be possible to specify a flipbook anim from the atlas as “image” part in “texture:image”.


#20

Correct. <img=smileys:happy/> does this:

gui.set_texture(node, texture)
gui.play_flipbook(node, image)