Hi,

Sprites are done... Need to work on text now.

We need the ability to create and fully modify RichTextLabels from within script code. Just need a few code examples.

We also need to know how to load in TTF fonts from script code.

Thanks!

Jesse www.FallenAngelSoftware.com

ALSO: This Godot project is 100% open-source You can access the project anytime at below GitHub URL: https://github.com/FallenAngelSoftware/Godot-SpaceSwap2

Its more or less the same as constructing a Sprite, just swap out Sprite with RichTextLabel and adjust RichTextLabel specific properties and functions for whatever you need. For example:

var rtl = RichTextLabel.new()
add_child(rtl)
rtl.rect_global_position = Vector2(200, 200)
rtl.bbcode_enabled = true
rtl.bbcode_text = "[b]Test text here![/b]"

To add a font via code, you need to make a Dynamic font, load the TTF, assign it, and then pass it to the RichTextLabel. Here's an example that is mostly taken from the documentation for DynamicFont:

var dynamic_font = DynamicFont.new()
dynamic_font.font_data = load("res://BarlowCondensed-Bold.ttf")
dynamic_font.size = 64
# assuming rtl is still a RichTextLabel we can access...
rtl.add_font_override("normal_font", dynamic_font)

Hi,

Stuck on something:

Say we have a RichTextLabel called: "Texts.TextImage[index]". How can we get the text width in pixels? (we need above for centering of text on screen)

Thanks!

Jesse

EDIT #1:

#----------------------------------------------------------------------------------------
func DrawText(var index, var text, var x, var y, var horizontalJustification, var size, var scaleX, var scaleY, var rotation, var red, var green, var blue, var alpha, var outlineRed, var outlineGreen, var outlineBlue):
	Texts.TextImage.append(RichTextLabel.new())

	add_child(Texts.TextImage[index])

	Texts.TextImage[index].add_font_override("normal_font", FontTTF)

	Texts.TextImage[index].text = text

	Texts.TextImage[index].rect_global_position.x = x
	Texts.TextImage[index].rect_global_position.y = y

	Texts.TextImage[index].set_size(Vector2(1024, 640), false)

	Texts.TextImage[index].rect_global_position.y = (y - (Texts.TextImage[index].get_font("normal_font").get_string_size(Texts.TextImage[index].text).y / 2))

	if horizontalJustification == 0:
		Texts.TextImage[index].ALIGN_LEFT
	elif horizontalJustification == 1:
		Texts.TextImage[index].ALIGN_CENTER
		Texts.TextImage[index].rect_global_position.x = ((VisualsCore.ScreenWidth/2) - (Texts.TextImage[index].get_font("normal_font").get_string_size(Texts.TextImage[index].text).x / 2))
	elif horizontalJustification == 2:
		Texts.TextImage[index].ALIGN_LEFT
		Texts.TextImage[index].rect_global_position.x = (VisualsCore.ScreenWidth - x - (Texts.TextImage[index].get_font("normal_font").get_string_size(Texts.TextImage[index].text).x))
	elif horizontalJustification == 4:
		Texts.TextImage[index].ALIGN_LEFT
		Texts.TextImage[index].rect_global_position.x = (x - (Texts.TextImage[index].get_font("normal_font").get_string_size(Texts.TextImage[index].text).x / 2))
	else:  Texts.TextImage[index].ALIGN_FILL

	pass

Hi,

Almost done with the text drawing using RichTextLabels

I just need script code examples for: - setting text pivot point(for centered rotations and scaling) - enabling text outline - setting text outline color - setting x and/or y scale of text

Thanks in advance!

Jesse

#----------------------------------------------------------------------------------------
func DrawText(var index, var text, var x, var y, var horizontalJustification, var fontSize, var scaleX, var scaleY, var rotation, var red, var green, var blue, var alpha, var outlineRed, var outlineGreen, var outlineBlue):
	Texts.TextImage.append(RichTextLabel.new())

	add_child(Texts.TextImage[index])

	var fontToUseIndex = 0
	if fontSize == 25:
		fontToUseIndex = 0
	elif fontSize == 60:
		fontToUseIndex = 1

	Texts.TextImage[index].add_font_override("normal_font", FontTTF[fontToUseIndex])

	Texts.TextImage[index].modulate = Color(red, green, blue, alpha)
	
	FontTTF[fontToUseIndex].outline_size = 3
	FontTTF[fontToUseIndex].outline_color = Color(outlineRed, outlineGreen, outlineBlue, alpha)

	Texts.TextImage[index].text = text

	Texts.TextImage[index].rect_global_position.x = x
	Texts.TextImage[index].rect_global_position.y = y

	Texts.TextImage[index].rect_scale = Vector2(scaleX, scaleY)

	Texts.TextImage[index].rect_rotation = rotation

	Texts.TextImage[index].set_size(Vector2(1024, 640), false)

	Texts.TextImage[index].rect_global_position.y = (y - (Texts.TextImage[index].get_font("normal_font").get_string_size(Texts.TextImage[index].text).y / 2))

	if horizontalJustification == 0:
		Texts.TextImage[index].ALIGN_LEFT
	elif horizontalJustification == 1:
		Texts.TextImage[index].ALIGN_CENTER
		Texts.TextImage[index].rect_global_position.x = ((VisualsCore.ScreenWidth/2) - (Texts.TextImage[index].get_font("normal_font").get_string_size(Texts.TextImage[index].text).x / 2))
	elif horizontalJustification == 2:
		Texts.TextImage[index].ALIGN_LEFT
		Texts.TextImage[index].rect_global_position.x = (VisualsCore.ScreenWidth - x - (Texts.TextImage[index].get_font("normal_font").get_string_size(Texts.TextImage[index].text).x))
	elif horizontalJustification == 4:
		Texts.TextImage[index].ALIGN_LEFT
		Texts.TextImage[index].rect_global_position.x = (x - (Texts.TextImage[index].get_font("normal_font").get_string_size(Texts.TextImage[index].text).x / 2))
	else:  Texts.TextImage[index].ALIGN_FILL


	TextCurrentIndex+=1

	pass

I don't think there is a way to set a pivot for Control-based nodes. You may need to add a Control node first, position to wherever you need, add the RichTextLabel as a child of the Control node, and then apply the pivot offset using rich_text_node_here.translation = Vector2(10, 10) # <- pivot offset. There may be other ways to go about it though.

2 years later