Use fixed font size in text node?

Is there a way to specify a font size and then force the text to adhere to it regardless of the size of a text node? I’d like to have all fonts for example appear at a certain size and if they overrun the text box, I’d like to attempt to reword those texts to fit more properly. However, the only way I see to do this is to calculate multiple scales and do a ton of mathematics in order to get a constant font size. And even then, if it’s off by even a small fraction then the sub-pixel smoothing appears slightly different and results in irregularities. Does anyone know a way to work around this?

GUI text node? Are you applying a scale on the node?

1 Like

Not on the node itself, but on the nodes it’s nested within. I’m trying to figure out how to create a scalable UI but while keeping the font sizes a fixed size so they look nicer. The issue is if I nest a text node within parent nodes which are scaled, then the font becomes scaled as well even if the scale is 1:1. However, I want the text to move relative to the parent node it’s within, just not respect the scale transformations.

Ah, I see. I guess you need:

But perhaps not a ton of mathematics really. Isn’t it enough to get the scale of the parent nodes (all the way up to the root) and divide the text node scale with that scale? Something like this:

local get_inherited_scale
get_inherited_scale = function(node)
	local scale = gui.get_scale(node)
	local parent = gui.get_parent(node)
	if parent then
		scale = vmath.mul_per_elem(scale, get_inherited_scale(parent))
	end
	return scale
end

-- unscale = clever new word I came up with just now
local function unscale(node)
	local scale = get_inherited_scale(gui.get_parent(node))
	scale.x = 1 / scale.x
	scale.y = 1 / scale.y
	gui.set_scale(node, scale)
end

function init(self)
    -- make sure the node "foo" uses a scale of 1.0 regardless of inherited scale of parent nodes
	unscale(gui.get_node("foo"))
end
2 Likes

True this works, but there’s not an easy way to preview in the editor for it. Thanks though, this is probably what I’ll end up going with :smiley:

I guess it depends on the UI you are trying to create, but can’t you use the different adjust modes to achieve what you want instead of applying scale on certain nodes?