UI components: Scaling
Members - Reputation: 216
Posted 21 November 2012 - 01:08 PM
I have made a little UI system to render UI elements (button, combo boxes etc.) in my DirectX 9 game.
Now I struggle a little bit with scaling of my components. To make my problem more clear I made a sketch:
Lets say I start my game windowed with resolution of 2000x1000. The red rectangle is my UI component. Now If i resize the window down to 1000x500 I guess it would be desirable to scale down the UI component proportionally.
So my algorithm could be something like this: After resize of window, resize all UI components proportionally horizontally and then vertically.
But then I have a problem when the user only applies a resize of the window in one dimension (bottom image). If I applied my algorithm the UI component would be distorted - clearly something I do not want.
So how could I implemented a solid scaling mechanism that scales my UI elements when the resolution changes?
Members - Reputation: 785
Posted 21 November 2012 - 01:39 PM
Crossbones+ - Reputation: 1694
Posted 21 November 2012 - 01:45 PM
Also allow the scales to be proportional to either the corresponding axis of resolution, OR both scale axis proportional to only 1 resolution axis like x and y.
Moderators - Reputation: 11463
Posted 21 November 2012 - 02:32 PM
Probably the best is to have the UI based on the corners and center lines of the screen, scaled (keeping their aspect ratio) based on resolution. A long narrow screen like you posted means there is a bigger gap between the left and right side, and the screen height is the controlling scale factor.
Another is to normalize the screen to a fixed aspect ratio, drawing black bars over the two sides that are out of bounds. The entire screen could be based on a 4:3 aspect ratio, and any other display ratio gets black bars.
Moderators - Reputation: 11463
Posted 21 November 2012 - 04:06 PM
Let's try an example.
@frob: Could you please elaborate your first approach a little bit more? Do you mean I determine whether I scale a component based on the smaller dimension of the window?
We'll have a button bar, built as a 600x100 strip, rooted at a corner, occupying up to 40% of the screen horizontally and up to 10% of the screen vertically.
Let's start with a 640x480 screen.
40% * 640 = 256px max width
10% * 480 = 48px max height
The width is the constraining value, so the UI element scales to become a 256x42 px bar.
Let's try with a 1024x768 screen
40% * 1024 =409px max width
10% * 768 = 76px max height
The width is the constraining value, so the UI element scales to become a 409x68px bar.
Let's try with a widescreen 1680x1050 screen
40% * 1680 = 672px max width
10% * 1050 = 105px max height
... Things just got interesting ...
Neither value constrains.
So we can choose to scale, height is the constraining value so we scale up to 630x105
Or we can choose to not scale, making our 600x100 button bar full size regardless of the larger screen space.
The titles I've worked on that did this always used normalized screen coordinates. So the UI element may have the UI element's corner be rooted at (0,0) or (1,1) or some other corner, or it may have it's center point rooted at (0.5, 0.5) to be exactly centered, or perhaps a little experience shows you should be a little higher on the screen with its center point rooted at (0.5, 0.4).
All the UI elements can be specified in an XML configuration file or other scripts so designers can manipulate them. They are also named constants so it is easy to move as a batch.
Naturally this format has its own set of drawbacks; all layout systems have their own sets of pros and cons.