Fitting GUI in fixed aspect ratio

I’ve done fixed aspect ratio render scripts (with black bars) multiple times, but apparently I never did much GUI work with them. Now I’m having issues getting the GUI to render inside my desired viewport and without distortion.

This is what my test GUI looks like, just two purple boxes, pivoted and anchored to the bottom left and top right, respectively. All nodes are set to ‘Fit’ adjust mode.

If I set the viewport and the orthographic projection to the cropped size and offset, there’s no distortion, but the GUI is fit to the window edges, and therefore cropped.


If I set the viewport to the cropped size and offset, but the projection to the full window size then the placement looks right but everything is stretched


If I set both the viewport and the projection to the full window size then it looks as I expect, no distortion or issues, but of course the placement is based on the full window size, which is not what I want.


If I set the viewport to the full window but the projection to the cropped area then I get the worst of both worlds: the GUI is stretched and cropped outside the window (I won’t bother showing this one).

Is there some way to make this work properly?


[Edit] Here’s the relevant chunk of my render script, nothing fancy.

-- GUI Rendering

-- `vp` is a table with the data for my desired fixed-aspect-ratio viewport
--    x, y offset, and width and height

render.set_viewport(vp.x, vp.y, vp.width, vp.height)
-- (x, y, width, height)
self.gui_proj = vmath.matrix4_orthographic(vp.x, vp.x + vp.width, vp.y, vp.y + vp.height, -1, 1)
-- (left, right, bottom, top, near, far)

--render.set_viewport(0, 0, render.get_window_width(), render.get_window_height())
--self.gui_proj = vmath.matrix4_orthographic(0, render.get_window_width(), 0, render.get_window_height(), -1, 1)

render.set_view(IDENTITY_MATRIX)
render.set_projection(self.gui_proj)

-- then actually draw gui stuff . . . 

What if you try:

self.gui_proj = vmath.matrix4_orthographic(0, vp.width,0, vp.height, -1, 1)

Hmm, I think I did try that at some point. It crops and stretches things as well.

At this point I’m thinking there isn’t a problem in my render script, Defold just isn’t meant to work this way. The GUI seems to adjust to the full window size all by itself, and displaying it differently with the render script doesn’t change that.

Correct. If you select the root of a Gui scene there’s an Adjust Reference dropdown in the Properties window. If you set Adjust Reference to Disabled it will behave as you want to:

41

Eeehhh, not really. Then the nodes stay in the same places, so if you shrink the window they get cropped and if you enlarge it they’re somewhere in the middle. What I would like is for the GUI to do all it’s automatic adjusting, only inside the area that I specify. For some reason I thought you had control over that, but it seems to be “hard-coded” to use the window resolution for its adjustments.

If I give the initial window resolution the same aspect ratio and leave the GUI nodes anchored to the center, then it works great, no matter how I change the window, but that fails if I want to change the aspect ratio at runtime. I guess I can live with that though.

I’m working on a render script/camera module library similar to your ‘Orthographic’ library, which is why I’m trying to cover every possible situation.