I made a class to help translate gui coordinates to panda gui coordinates. Here’s the code if someone is interested in using it.
from math import *
class GuiSpace:
def __init__(self,left=0,bottom=0,right=1,top=1):
self.pscale = (2.0,2.0)
self.ptransl = (-1.0, -1.0)
self.scale = (right-left, top-bottom)
self.sfactor = (self.pscale[0]/self.scale[0],
self.pscale[1]/self.scale[1])
self.transl = (left, bottom)
def pos(self,x,y):
return (self.posx(x), self.posy(y))
def posx(self,x):
return (x - self.transl[0]) * self.sfactor[0] + self.ptransl[0]
def posy(self,y):
return (y - self.transl[1]) * self.sfactor[1] + self.ptransl[1]
def dim(self,x,y):
return (self.dimx(x), self.dimy(y))
def dimx(self,x):
return fabs(x * self.sfactor[0])
def dimy(self,y):
return fabs(y * self.sfactor[1])
First you have to specify a gui coordinate system, for example, 800x600 with screen coordinates where y is upside down.
gsp = GuiSpace(left=0,bottom=600,right=800,top=0)
Notice that top < bottom which will result in y being inverted.
To use this object, whenever you have to pass panda gui coordinates you can now reason logically in terms of a familiar coordinate system (800x600 screen). Like this:
return OnscreenText(
text=msg,
style=1,
fg=(1,1,1,1),
pos=gsp.pos(10, 10),
align=TextNode.ALeft,
scale = gsp.dimy(16))