Use Slice 9 when GUI node's Adjust Mode is set to Stretch

Nodes are deformed if set to Strech, even if they’re defined to scale with Slice 9. It would make sense if resized nodes always respected Slice 9, if defined.

5 Likes

Original thread: Render.get_window_width() vs. GUI width

2 Likes

Bumped into this one again when trying to have a slice 9 border around the screen. +1, if allowed. :slight_smile:

2 Likes

Bumped into this again, so added an issue: https://github.com/defold/defold/issues/5197

As I understand it, the developers of Defold do not consider this a problem, the GUI scaling system literally implies this.

This stretching behavior will occur in a node with slice9, a text node, etc.

I found this solution for myself:

you create a node, specify the fit scale mode for it, and apply this code on it at the start and with each window reuse. The Result is that the node behaves like a stretch, but actually adjusts its size to the desired size (thanks to which slice9 does not break).

local window_x, window_y = window.get_size()
local gui_w, gui_h = gui.get_width(), gui.get_height()

local stretch_x = window_x / gui_w
local stretch_y = window_y / gui_h
local stretch_koef = math.min(stretch_x, stretch_y)

local koef_x = window_x / (gui_w * stretch_koef)
local koef_y = window_y / (gui_h * stretch_koef)

local panel = node --your node
local initial_size = initial_size --the initial size of your node
gui.set_size(panel, vmath.vector3(initial_size.x * koef_x, initial_size.y * koef_y, 0))
2 Likes

Thanks for the code! One thought though: won’t we need to reposition the node as well?

1 Like

Yes, I’ve used this mostly on nodes that are rigidly anchored to the edge of the screen, so I’ve had no problems with it.

But now I’ve tested what happens if you freely place two nodes, one of which is stretch and the other is fit + my script, indeed, they behave a little differently, the fit node may shift relative to the desired position.

Maybe someone can refine my script so that it also correctly takes into account the node offset?