Relative sizing in UI - bug?
by Keith Johnston · in Torque Game Engine · 02/17/2004 (5:52 am) · 2 replies
I've recently switched to using relative sizing / positioning for all the controls in the UI. However, sometimes the sizes seem to change in an odd way, even in the same resolution - buttons become skewed, for example. Is this a bug? I'm using the 1.2 release - maybe this has been fixed in a later release?
#2
The initial sizes are loaded from a script file, right, so why not reload them from the script file before each resize, that way they won't be contaminated by the previous resizes?
03/09/2004 (10:02 am)
Just a thought...The initial sizes are loaded from a script file, right, so why not reload them from the script file before each resize, that way they won't be contaminated by the previous resizes?
Torque Owner Andy
The cause is related to the position and extant information being stored as integers and can rapidly accumulate errors due to rounding. In my case, I discovered it while checking my layout with the Gui editor and switching resolutions.
I had 48x48 bitmap buttons at 1024x768, but after switching resolutions 1024x768 --> 800x600 --> 640x480 --> 1024x768 my buttons were various permutations ranging from 48x49 through 51x50 or so.
I have a simple patch that uses floating-point and rounding during the horizontal and vertical rescale computation. It reduces the problem somewhat, but doesn't really eliminate it.
A true fix looks like it could be rather invasive.
Here's the diff relative to 1.2.1 (sorry if it gets whitespace mangled):
Index: torque/engine/gui/guiControl.cc =================================================================== RCS file: /home/cvsroot/garagegames/torque/engine/gui/guiControl.cc,v retrieving revision 1.1.1.2 retrieving revision 1.1.1.2.2.1 diff -a -u -r1.1.1.2 -r1.1.1.2.2.1 --- torque/engine/gui/guiControl.cc 2004/02/06 21:03:44 1.1.1.2 +++ torque/engine/gui/guiControl.cc 2004/03/04 16:57:46 1.1.1.2.2.1 @@ -252,8 +252,8 @@ newPosition.x += deltaX; else if (mHorizSizing == horizResizeRelative && oldParentExtent.x != 0) { - S32 newLeft = (newPosition.x * newParentExtent.x) / oldParentExtent.x; - S32 newRight = ((newPosition.x + newExtent.x) * newParentExtent.x) / oldParentExtent.x; + S32 newLeft = (S32) (((newPosition.x * newParentExtent.x) / (F64) oldParentExtent.x) + 0.5); + S32 newRight = (S32) ((((newPosition.x + newExtent.x) * newParentExtent.x) / (F64) oldParentExtent.x) + 0.5); newPosition.x = newLeft; newExtent.x = newRight - newLeft; @@ -268,8 +268,8 @@ else if(mVertSizing == vertResizeRelative && oldParentExtent.y != 0) { - S32 newTop = (newPosition.y * newParentExtent.y) / oldParentExtent.y; - S32 newBottom = ((newPosition.y + newExtent.y) * newParentExtent.y) / oldParentExtent.y; + S32 newTop = (S32) (((newPosition.y * newParentExtent.y) / (F64) oldParentExtent.y) + 0.5); + S32 newBottom = (S32) ((((newPosition.y + newExtent.y) * newParentExtent.y) / (F64) oldParentExtent.y) + 0.5); newPosition.y = newTop; newExtent.y = newBottom - newTop;