Button Coordinate confusion :(

I am really confused :frowning: I have created a flat button at (-1, 0, -1) and it does not appear at those coords, but very close to them on screen. Ive dumped out some info on the button but I do not see what could be wrong.

--Button Info--
coords : -1.000000, 0.000000, -1.000000
size   : 0.580000, 0.320000
Frame : -0.266667 0.313333 -0.0533333 0.266667
pg0.setPosHprScale(-1.00, 0.00, -1.00, 0.00, 0.00, 0.00, 1.00, 1.00, 1.00)

PandaNode state_0
state_0.setPosHprScale(0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 1.00, 1.00)

GeomNode flat (1 geoms)
flat.setPosHprScale(0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 1.00, 1.00)

TextNode
  with font Nimbus Sans L Regular Condensed
  alignment is A_center
  text color is 0 0 0 1

  transform is:
  scale: 0.4 1 0.4
  shear: 0 0 0
    hpr: 0 0 0
  trans: 0 0 0
  in coordinate system default

  text is test
.setPosHprScale(0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 1.00, 1.00)

I would like to know where the buttons coords are relative to the button, i.e. is the button coord the top-left corner or perhaps in the middle?

Any help greatly appreciated.

The ā€œposā€ of the button is, as for any node, really the position of its originā€“the (0, 0, 0) point within the button itself. The visible part of the button is framed around this point, and the exact placement depends on how the button was defined (e.g. with text_align = TextNode.ACenter, itā€™s in a different place than with text_align = TextNode.ALeft).

The final authority is the frameSize parameter, which specifies the (left, right, bottom, top) extents of the frame. For instance, if you specify a frameSize of (-0.5, 0.5, -0.2, 0.2), then the buttonā€™s lower-left corner will be at (-0.5, 0, -0.2) and its upper-right corner will be at (0.5, , 0.2) within the buttonā€™s own coordinate spaceā€“so if you then position the button at (-1, 0, -1), the corners will be from (-1.5, 0, -1.2) to (-0.5, 0, -0.8) on the screen.

If you do not specify a frameSize parameter, one is derived for you based on the text or geometry you specify to the button.

David

I did think it was meant to be the origin but I am still having a problem.
Am I right in thinking the coordinate system should be -1 to 1 for aspect2d ?

Here is a real simple test code to display the below problem;

import direct.directbase.DirectStart
from direct.gui.DirectGui import *

myButton = DirectButton(parent = aspect2d,
                        text          = 'test',
                        text_scale    = (0.4, 0.4),
                        pos           = (-1, 0, 0),
                        relief        = FLAT,
                        pressEffect   = 1)

run()

Now I would expect the button to only be half visible on the left side of the screen but its actually 25% out from the left. I had a scan through ShowBase and I noticed that aspect2d has a scale on the x which is 1 / aspectRatio. Taking this scale away from aspect2d then makes the button display in what I think would have been the correct place.

It seems most odd.

Comepletely off-topic, I was trying to find out how the button width is scaled to the textNode or visa-versa.

The closest I could get was thisā€¦


    newTextScaleX = (self.buttonWidth - 0.04 ) /
                     self.textNodePath[0].node().getWidth()
    newTextScaleZ = ((self.buttonHeight - 0.025) /
                     self.textNodePath[0].node().getHeight()) * 2
    self.setTextScaleX(newTextScaleX)
    self.setTextScaleZ(newTextScaleZ)

This is for after a resize to a button.

The text on a button is slightly smaller than the background card but the figures in the c++ code did not seem to tally when I tried them. It also appeared that the Z scale is doubled. Any insight into this is greatly appreciated.

Actually, no. The coordinate system is -1 to 1 for render2d. If you want to use a coordinate system in this range, you should parent your DirectGUI objects to render2d (but they will be stretched).

Since aspect2d is scaled to compensate for the screenā€™s aspect ratio, which is 4/3 by default, that makes the width of aspect2d be in the range -1.333 to 1.333, when the height is -1 to 1. In general, for any given aspect ratio, the range of aspect2d is (base.a2dLeft ā€¦ base.a2dRight) and (base.a2dBottom ā€¦ base.a2dTop).

Actually, youā€™ve lost me a bit on this one. The button width has little to do with scale of the textNode, except indirectly. The width of the button is actually specified by the frameSize parameter, and if you donā€™t specify frameSize, then the width is derived from the bounding box of the generated text, plus whatever pad is specified.

If you want to make the button bigger or smaller without rescaling its text, you should probably just set its frameSize explicitly, or change the pad value, rather than scaling the whole button.

David

re: screen dimensions,
Thanks for clearing that up. Thought I was going mad for a moment :slight_smile:

re: off-topic
Scale was defo a bad word to use. I mean re-size. I was not over-clear in the post - sorry.

When a button is created the size of it is calculated by the text scale. Something is added onto this to create a background geom. What I am trying to do is work it backwards but I think I see the problem nowā€¦ My head is not screwed on today :slight_smile:

Cheers.