Hi all,
I was wondering how one would get the position of the 4 points that make up the boundary of a directgui object in pixels, after a few transforms have been done on it. Something like:
pixelBounds=directButton.getPixelBounds()
pixelBounds=(lowerLeft,upperRight)#<-Returned format
I know of [“frameSize”] and getBounds, getFrame, but these all return values that are in aspect2d space, whose range I’ve not been able to find and also, these values remain the same even after performing a transform on a directgui object. For example:
aButton=DirectButton(text="button",scale=(1,1,1),pos=(0,0,0))
boundsGot=aButton.getBounds()
aButton.setScale(2)
boundsGot2=aButton.getBounds()
both boundsGot and boundsGot2 just yield the same frame size. It’s a simple problem, yet I haven’t been able to find anything helpful on it. Even if I say “aButton.resetFrameSize()”, unless I’ve changed the text element, the returned frame size just stays the same.
Thanks in advance.
If I’m not much mistaken, aspect2d is essentially just a child of render2d, scaled so that it adjusts for the aspect ratio of the window.
As such, the range of the bounds of aspect2d are dependent on the scale of the window: for a square window, they should match render2d, giving a range of (-1, 1); for a non-square window, they should match render2d multiplied by the scale of aspect2d (i.e. as acquired from a call to “aspect2d.getScale()”).
This can be tricky, I fear!
One way that should, I think work more or less comes down to scaling and offsetting the bounds reported by getBounds as appropriate. That is, finding the scale and position of the DirectGUI object, relative to whatever root-node you’re interested in having your measurements relative to, and then applying them to the results of “getBounds”.
Another way that might work would be to consider the return-value of “getBounds” as being in the space of the DirectGUI object, and thus using calls to “getRelativePoint” to convert those values to your desired space.
Also, let me note that if you’re interested in pixel-coordinates, you might want to look at pixel2d, rather than aspect2d.
1 Like
This right here, did the trick:
def returnPixel2dBounds(guiObject):
guiBounds=guiObject.getBounds()
pntGt=pixel2d.getRelativePoint(guiObject,LPoint3f(guiBounds[0],0,0))
pntGt2=pixel2d.getRelativePoint(guiObject,LPoint3f(guiBounds[1],0,0))
pntGt3=pixel2d.getRelativePoint(guiObject,LPoint3f(0,0,guiBounds[2]))
pntGt4=pixel2d.getRelativePoint(guiObject,LPoint3f(0,0,guiBounds[3]))
return [pntGt.x,pntGt2.x,pntGt3.z,pntGt4.z]
“.getRelativePoint” is a truly versatile function, I didn’t suspect it could be used like this! Thank you once again for the digital theurgy, Thaumaturge!
1 Like
Hahah, it’s very much my pleasure! I’m glad that you found a solution that worked for you!
1 Like