OnscreenText.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """OnscreenText module: contains the OnscreenText class"""
  2. from PandaObject import *
  3. class OnscreenText(PandaObject, NodePath):
  4. Font = loader.loadModelOnce("fonts/ttf-comic").node()
  5. def __init__(self, string, x=0.0, y=0.0):
  6. """__init__(self, string, float=0.0, float=0.0)
  7. Make a text node from string, put it into the 2d sg and place
  8. it at x, y in screen space
  9. """
  10. # become one with our NodePath-ness
  11. NodePath.__init__(self)
  12. # make a text node
  13. self.textNode = textNode = TextNode()
  14. textNode.setBillboard(0)
  15. textNode.setTextColor(0.0, 0.0, 0.0, 1.0)
  16. textNode.setCardColor(1.0, 1.0, 1.0, 1.0)
  17. textNode.setCardAsMargin(0.1, 0.1, 0.1, 0.1)
  18. textNode.setFrameColor(0.0, 0.0, 0.0, 1.0)
  19. textNode.setFrameAsMargin(0.1, 0.1, 0.1, 0.1)
  20. textNode.setFont(OnscreenText.Font)
  21. textNode.setText(string)
  22. # put the text node into the 2d scene graph
  23. textNodePath = render2d.attachNewNode(textNode)
  24. # we ARE this node path
  25. self.assign(textNodePath)
  26. # position ourselves
  27. self.setXY(x, y)
  28. # assume 4:3 aspect ratio
  29. self.setScale( 0.069, 1.0, 0.069)
  30. def setText(self, string):
  31. """setText(self, string)
  32. Set the text of the onscreen text
  33. """
  34. self.node().setText(string)
  35. def setXY(self, x, y):
  36. """setPos(self, float, float)
  37. Position the onscreen text in 2d screen space
  38. """
  39. # render2d has x across and z up
  40. self.setPos(x, 0.0, y)
  41. def setColor(self, color):
  42. self.textNode.setCardColor(color[0],color[1],color[2],color[3])