Resolution independent text / instructions

I have one tiny recommendation.

This little bit seems to appear in most of the sample applications.

# Function to put instructions on the screen.
def addInstructions(pos, msg):
    return OnscreenText(text=msg, style=1, fg=(1,1,1,1),
                        pos=(-1.3, pos), align=TextNode.ALeft, scale = .05)

# Function to put title on the screen.
def addTitle(text):
    return OnscreenText(text=text, style=1, fg=(1,1,1,1),
                        pos=(1.3,-0.95), align=TextNode.ARight, scale = .07)

I ended up borrowing it for lots of small scale demos and tests with Panda3D. However I’ve noticed that it doesn’t work on wide-screen resolutions. I’ve found the following code useful, and it even may be worth adding to the sample applications.

# Function returns the width / height ratio of the window or screen
def getScreenRatio():
    props = WindowProperties(base.win.getProperties())
    return float(props.getXSize()) / float(props.getYSize())

# Function to add instructions and other information along either side
def addText(pos, msg, changeable=False, alignLeft=True, scale=0.05):
    x = -getScreenRatio() + 0.03
    if alignLeft:
        align = TextNode.ALeft
    else:
        align = TextNode.ARight
        x *= -1.0
    return OnscreenText(text=msg, style=1, fg=(1, 1, 1, 1),
                        pos=(x, pos), align=align, scale=scale,
                        mayChange=changeable)

# Function to put title on the screen.
def addTitle(text):
    addText(-0.95, text, False, False, 0.07)

You may find this variant is better at keeping the text in the upper-left corner of the screen regardless of the width of your window:

def addInstructions(pos, msg):
    return OnscreenText(text=msg, style=1, fg=(1,1,1,1),
                        pos=(0, pos), parent = base.a2dTopLeft,
                        align=TextNode.ALeft, scale = .05) 

There is also a base.a2dTopRight, a2dBottomLeft, a2dBottomRight, a2dTopCenter, and so on.

David