Created
August 5, 2023 13:15
-
-
Save brombres/116ca0c90ad61abd68048ab95de744d2 to your computer and use it in GitHub Desktop.
A Godot script to auto-scale the text of a RichTextLabel node as the RTL size changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# AutoscaleRichTextLabel.gd | |
# | |
# ABOUT | |
# 2023.08.04 by Brom Bresenham | |
# Tested on Godot 4.2-dev2 | |
# | |
# USAGE | |
# 1. [Inspector] Create a RichTextLabel node and attach this script to it. | |
# 2. [Editor] Save and reload the scene to activate this as a @tool script | |
# that will automatically set the Node Reference Height. | |
# 3. [Inspector] Control > Layout > Layout Mode -> Anchors | |
# 4. [Inspector] Control > Layout > Anchors Preset -> Full Rect | |
# 5. [Inspector] RichTextLabel > BBCode Enabled -> On | |
# 6. [Inspector] RichTextLabel > Text -> "[center]My Text ..." etc. | |
# 7. [Inspector] RichTextLabel > Scroll Active -> Off (optional) | |
# 8. [Inspector] Control > Theme Overrides > Font Sizes > Normal Font Size (etc.) -> ... | |
# 9. [Editor] Use the red dots to position the transform bounding box. | |
# NOTE: Be sure to leave the node 'scale' at (1,1) in the inspector. | |
# 10. [Editor] Drag the green anchor spikes to snap onto the red dots. | |
# In the Inspector, confirm that the Anchor Offets are (0,0,0,0). | |
# The Anchor Points will be arbitrary values in the range 0.0-1.0. | |
@tool # allows this script to set its own node_reference_height | |
extends RichTextLabel | |
## Enable to have [member node_reference_height] automatically set as you | |
## position this [RichTextLabel] node. | |
@export var auto_set_reference_height:bool = true | |
## The transform height of the [RichTextLabel] node that the font size was designed for. | |
## This is used to scale the font based on the current height of the node. | |
@export var node_reference_height:float = 0 | |
## The fonts that will be auto-scaled for this node. | |
@export var fonts:Array[String] = \ | |
["normal","bold","italics","bold_italics","mono"] | |
# The original size of each font. | |
var _nominal_font_size:Array[float] = [0,0,0,0,0] | |
func _ready(): | |
if not Engine.is_editor_hint(): | |
# Save the original size of each font | |
while _nominal_font_size.size() < fonts.size(): | |
_nominal_font_size.push_back(0) | |
for i in range(fonts.size()): | |
var font_name = fonts[i] + "_font_size" | |
_nominal_font_size[i] = get_theme_font_size( font_name ) | |
# Start listening for bounds changes. | |
item_rect_changed.connect( _on_item_rect_changed ) | |
func _process(_delta): | |
# Auto-set the reference height. | |
if Engine.is_editor_hint() and auto_set_reference_height: | |
node_reference_height = size.y | |
func _on_item_rect_changed(): | |
if node_reference_height: | |
var cur_scale = (size.y / node_reference_height) | |
# Override the size of each font | |
for i in range(fonts.size()): | |
var cur_size = _nominal_font_size[i] * cur_scale | |
add_theme_font_size_override( fonts[i]+"_font_size", cur_size ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment