Text and screen borders

I’m sure this has been answered before but I can’t find it. I’ve found a LOT of stuff that seems close, but nothing that makes sense to me.

All I’m trying to do is align a text box with the edge of the screen. I’m using OnscreenText to do this.

data = OnscreenText(text="x = 0.0\ny = 0",
    fg=(1,1,1,1),
    bg=(0,0,0,1),    
    parent = base.a2dTopRight,
    #align = TextNode.ARight,
    )

I’m seeing several issues with this. First off, the text is right aligned within the box, I need it left aligned. If I change the align value to left though, it aligns the entire box to the left, and off the screen. Also, the text box is pushing the first line, “x = 0.0” above the window.

Please, what am I not seeing here??

Thanks in advance,
Slippyblade

Sorry to do this, but I’m still beating my head against this.

Soooooo, bumpity, bump, bump.

To align at edge of screen, set base.a2dBottomLeft (there are many others) or so.

Part of the problem is you want to align the right edge of the text box with the right edge of the screen, but you want the text itself to be left-aligned.

Try placing the text based on its own size, which you can get after you have created the TextNode. Something like:

text = TextNode('text')
text.setText("x = 0.0\ny = 0")
tnp = base.a2dTopRight.attachNewNode(text)
scale = 0.1
tnp.setScale(scale)
tnp.setPos(-text.getWidth() * scale, 0, -text.getFont().getLineHeight() * scale)

I’m using the lower-level TextNode interface here, because it’s a little easier to get to these low-level functions that way. (The OnscreenText object wasn’t really designed with this in mind, and gets in the way.)

David

@drwr

I see what you are doing there, thank you for the nudge. I tried your snippet in my code and it worked like a charm. Then all I had to do was toss a card behind the text and it’s perfect.

Thanks much!