OnscreenText.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. """OnscreenText module: contains the OnscreenText class"""
  2. __all__ = ['OnscreenText', 'Plain', 'ScreenTitle', 'ScreenPrompt', 'NameConfirm', 'BlackOnWhite']
  3. from panda3d.core import *
  4. from . import DirectGuiGlobals as DGG
  5. import sys
  6. ## These are the styles of text we might commonly see. They set the
  7. ## overall appearance of the text according to one of a number of
  8. ## pre-canned styles. You can further customize the appearance of the
  9. ## text by specifying individual parameters as well.
  10. Plain = 1
  11. ScreenTitle = 2
  12. ScreenPrompt = 3
  13. NameConfirm = 4
  14. BlackOnWhite = 5
  15. class OnscreenText(NodePath):
  16. def __init__(self, text = '',
  17. style = Plain,
  18. pos = (0, 0),
  19. roll = 0,
  20. scale = None,
  21. fg = None,
  22. bg = None,
  23. shadow = None,
  24. shadowOffset = (0.04, 0.04),
  25. frame = None,
  26. align = None,
  27. wordwrap = None,
  28. drawOrder = None,
  29. decal = 0,
  30. font = None,
  31. parent = None,
  32. sort = 0,
  33. mayChange = True,
  34. direction = None):
  35. """
  36. Make a text node from string, put it into the 2d sg and set it
  37. up with all the indicated parameters.
  38. The parameters are as follows:
  39. text: the actual text to display. This may be omitted and
  40. specified later via setText() if you don't have it
  41. available, but it is better to specify it up front.
  42. style: one of the pre-canned style parameters defined at the
  43. head of this file. This sets up the default values for
  44. many of the remaining parameters if they are
  45. unspecified; however, a parameter may still be specified
  46. to explicitly set it, overriding the pre-canned style.
  47. pos: the x, y position of the text on the screen.
  48. scale: the size of the text. This may either be a single
  49. float (and it will usually be a small number like 0.07)
  50. or it may be a 2-tuple of floats, specifying a different
  51. x, y scale.
  52. fg: the (r, g, b, a) foreground color of the text. This is
  53. normally a 4-tuple of floats or ints.
  54. bg: the (r, g, b, a) background color of the text. If the
  55. fourth value, a, is nonzero, a card is created to place
  56. behind the text and set to the given color.
  57. shadow: the (r, g, b, a) color of the shadow behind the text.
  58. If the fourth value, a, is nonzero, a little drop shadow
  59. is created and placed behind the text.
  60. frame: the (r, g, b, a) color of the frame drawn around the
  61. text. If the fourth value, a, is nonzero, a frame is
  62. created around the text.
  63. align: one of TextNode.ALeft, TextNode.ARight, or TextNode.ACenter.
  64. wordwrap: either the width to wordwrap the text at, or None
  65. to specify no automatic word wrapping.
  66. drawOrder: the drawing order of this text with respect to
  67. all other things in the 'fixed' bin within render2d.
  68. The text will actually use drawOrder through drawOrder +
  69. 2.
  70. decal: if this is True, the text is decalled onto its
  71. background card. Useful when the text will be parented
  72. into the 3-D scene graph.
  73. font: the font to use for the text.
  74. parent: the NodePath to parent the text to initially.
  75. mayChange: pass true if the text or its properties may need
  76. to be changed at runtime, false if it is static once
  77. created (which leads to better memory optimization).
  78. direction: this can be set to 'ltr' or 'rtl' to override the
  79. direction of the text.
  80. """
  81. if parent == None:
  82. parent = aspect2d
  83. # make a text node
  84. textNode = TextNode('')
  85. self.textNode = textNode
  86. # We ARE a node path. Initially, we're an empty node path.
  87. NodePath.__init__(self)
  88. # Choose the default parameters according to the selected
  89. # style.
  90. if style == Plain:
  91. scale = scale or 0.07
  92. fg = fg or (0, 0, 0, 1)
  93. bg = bg or (0, 0, 0, 0)
  94. shadow = shadow or (0, 0, 0, 0)
  95. frame = frame or (0, 0, 0, 0)
  96. if align == None:
  97. align = TextNode.ACenter
  98. elif style == ScreenTitle:
  99. scale = scale or 0.15
  100. fg = fg or (1, 0.2, 0.2, 1)
  101. bg = bg or (0, 0, 0, 0)
  102. shadow = shadow or (0, 0, 0, 1)
  103. frame = frame or (0, 0, 0, 0)
  104. if align == None:
  105. align = TextNode.ACenter
  106. elif style == ScreenPrompt:
  107. scale = scale or 0.1
  108. fg = fg or (1, 1, 0, 1)
  109. bg = bg or (0, 0, 0, 0)
  110. shadow = shadow or (0, 0, 0, 1)
  111. frame = frame or (0, 0, 0, 0)
  112. if align == None:
  113. align = TextNode.ACenter
  114. elif style == NameConfirm:
  115. scale = scale or 0.1
  116. fg = fg or (0, 1, 0, 1)
  117. bg = bg or (0, 0, 0, 0)
  118. shadow = shadow or (0, 0, 0, 0)
  119. frame = frame or (0, 0, 0, 0)
  120. if align == None:
  121. align = TextNode.ACenter
  122. elif style == BlackOnWhite:
  123. scale = scale or 0.1
  124. fg = fg or (0, 0, 0, 1)
  125. bg = bg or (1, 1, 1, 1)
  126. shadow = shadow or (0, 0, 0, 0)
  127. frame = frame or (0, 0, 0, 0)
  128. if align == None:
  129. align = TextNode.ACenter
  130. else:
  131. raise ValueError
  132. if not isinstance(scale, tuple):
  133. # If the scale is already a tuple, it's a 2-d (x, y) scale.
  134. # Otherwise, it's a uniform scale--make it a tuple.
  135. scale = (scale, scale)
  136. # Save some of the parameters for posterity.
  137. self.__scale = scale
  138. self.__pos = pos
  139. self.__roll = roll
  140. self.__wordwrap = wordwrap
  141. if decal:
  142. textNode.setCardDecal(1)
  143. if font == None:
  144. font = DGG.getDefaultFont()
  145. textNode.setFont(font)
  146. textNode.setTextColor(fg[0], fg[1], fg[2], fg[3])
  147. textNode.setAlign(align)
  148. if wordwrap:
  149. textNode.setWordwrap(wordwrap)
  150. if bg[3] != 0:
  151. # If we have a background color, create a card.
  152. textNode.setCardColor(bg[0], bg[1], bg[2], bg[3])
  153. textNode.setCardAsMargin(0.1, 0.1, 0.1, 0.1)
  154. if shadow[3] != 0:
  155. # If we have a shadow color, create a shadow.
  156. # Can't use the *shadow interface because it might be a VBase4.
  157. #textNode.setShadowColor(*shadow)
  158. textNode.setShadowColor(shadow[0], shadow[1], shadow[2], shadow[3])
  159. textNode.setShadow(*shadowOffset)
  160. if frame[3] != 0:
  161. # If we have a frame color, create a frame.
  162. textNode.setFrameColor(frame[0], frame[1], frame[2], frame[3])
  163. textNode.setFrameAsMargin(0.1, 0.1, 0.1, 0.1)
  164. if direction is not None:
  165. if isinstance(direction, str):
  166. direction = direction.lower()
  167. if direction == 'rtl':
  168. direction = TextProperties.D_rtl
  169. elif direction == 'ltr':
  170. direction = TextProperties.D_ltr
  171. else:
  172. raise ValueError('invalid direction')
  173. textNode.setDirection(direction)
  174. # Create a transform for the text for our scale and position.
  175. # We'd rather do it here, on the text itself, rather than on
  176. # our NodePath, so we have one fewer transforms in the scene
  177. # graph.
  178. self.updateTransformMat()
  179. if drawOrder != None:
  180. textNode.setBin('fixed')
  181. textNode.setDrawOrder(drawOrder)
  182. self.setText(text)
  183. if not text:
  184. # If we don't have any text, assume we'll be changing it later.
  185. self.mayChange = 1
  186. else:
  187. self.mayChange = mayChange
  188. # Ok, now update the node.
  189. if not self.mayChange:
  190. # If we aren't going to change the text later, we can
  191. # throw away the TextNode.
  192. self.textNode = textNode.generate()
  193. self.isClean = 0
  194. # Set ourselves up as the NodePath that points to this node.
  195. self.assign(parent.attachNewNode(self.textNode, sort))
  196. def cleanup(self):
  197. self.textNode = None
  198. if self.isClean == 0:
  199. self.isClean = 1
  200. self.removeNode()
  201. def destroy(self):
  202. self.cleanup()
  203. def freeze(self):
  204. pass
  205. def thaw(self):
  206. pass
  207. # Allow changing of several of the parameters after the text has
  208. # been created. These should be used with caution; it is better
  209. # to set all the parameters up front. These functions are
  210. # primarily intended for interactive placement of the initial
  211. # text, and for those rare occasions when you actually want to
  212. # change a text's property after it has been created.
  213. def setDecal(self, decal):
  214. self.textNode.setCardDecal(decal)
  215. def getDecal(self):
  216. return self.textNode.getCardDecal()
  217. decal = property(getDecal, setDecal)
  218. def setFont(self, font):
  219. self.textNode.setFont(font)
  220. def getFont(self):
  221. return self.textNode.getFont()
  222. font = property(getFont, setFont)
  223. def clearText(self):
  224. self.textNode.clearText()
  225. def setText(self, text):
  226. if sys.version_info >= (3, 0):
  227. assert not isinstance(text, bytes)
  228. self.unicodeText = True
  229. else:
  230. self.unicodeText = isinstance(text, unicode)
  231. if self.unicodeText:
  232. self.textNode.setWtext(text)
  233. else:
  234. self.textNode.setText(text)
  235. def appendText(self, text):
  236. if sys.version_info >= (3, 0):
  237. assert not isinstance(text, bytes)
  238. self.unicodeText = True
  239. else:
  240. self.unicodeText = isinstance(text, unicode)
  241. if self.unicodeText:
  242. self.textNode.appendWtext(text)
  243. else:
  244. self.textNode.appendText(text)
  245. def getText(self):
  246. if self.unicodeText:
  247. return self.textNode.getWtext()
  248. else:
  249. return self.textNode.getText()
  250. text = property(getText, setText)
  251. def setX(self, x):
  252. self.setPos(x, self.__pos[1])
  253. def setY(self, y):
  254. self.setPos(self.__pos[0], y)
  255. def setPos(self, x, y):
  256. """setPos(self, float, float)
  257. Position the onscreen text in 2d screen space
  258. """
  259. self.__pos = (x, y)
  260. self.updateTransformMat()
  261. def getPos(self):
  262. return self.__pos
  263. pos = property(getPos, setPos)
  264. def setRoll(self, roll):
  265. """setRoll(self, float)
  266. Rotate the onscreen text around the screen's normal
  267. """
  268. self.__roll = roll
  269. self.updateTransformMat()
  270. def getRoll(self):
  271. return self.__roll
  272. roll = property(getRoll, setRoll)
  273. def setScale(self, sx, sy = None):
  274. """setScale(self, float, float)
  275. Scale the text in 2d space. You may specify either a single
  276. uniform scale, or two scales, or a tuple of two scales.
  277. """
  278. if sy == None:
  279. if isinstance(sx, tuple):
  280. self.__scale = sx
  281. else:
  282. self.__scale = (sx, sx)
  283. else:
  284. self.__scale = (sx, sy)
  285. self.updateTransformMat()
  286. def updateTransformMat(self):
  287. assert(isinstance(self.textNode, TextNode))
  288. mat = (
  289. Mat4.scaleMat(Vec3.rfu(self.__scale[0], 1, self.__scale[1])) *
  290. Mat4.rotateMat(self.__roll, Vec3.back()) *
  291. Mat4.translateMat(Point3.rfu(self.__pos[0], 0, self.__pos[1]))
  292. )
  293. self.textNode.setTransform(mat)
  294. def getScale(self):
  295. return self.__scale
  296. scale = property(getScale, setScale)
  297. def setWordwrap(self, wordwrap):
  298. self.__wordwrap = wordwrap
  299. if wordwrap:
  300. self.textNode.setWordwrap(wordwrap)
  301. else:
  302. self.textNode.clearWordwrap()
  303. def getWordwrap(self):
  304. return self.__wordwrap
  305. wordwrap = property(getWordwrap, setWordwrap)
  306. def __getFg(self):
  307. return self.textNode.getTextColor()
  308. def setFg(self, fg):
  309. self.textNode.setTextColor(fg[0], fg[1], fg[2], fg[3])
  310. fg = property(__getFg, setFg)
  311. def __getBg(self):
  312. if self.textNode.hasCard():
  313. return self.textNode.getCardColor()
  314. else:
  315. return LColor(0)
  316. def setBg(self, bg):
  317. if bg[3] != 0:
  318. # If we have a background color, create a card.
  319. self.textNode.setCardColor(bg[0], bg[1], bg[2], bg[3])
  320. self.textNode.setCardAsMargin(0.1, 0.1, 0.1, 0.1)
  321. else:
  322. # Otherwise, remove the card.
  323. self.textNode.clearCard()
  324. bg = property(__getBg, setBg)
  325. def __getShadow(self):
  326. return self.textNode.getShadowColor()
  327. def setShadow(self, shadow):
  328. if shadow[3] != 0:
  329. # If we have a shadow color, create a shadow.
  330. self.textNode.setShadowColor(shadow[0], shadow[1], shadow[2], shadow[3])
  331. self.textNode.setShadow(0.04, 0.04)
  332. else:
  333. # Otherwise, remove the shadow.
  334. self.textNode.clearShadow()
  335. shadow = property(__getShadow, setShadow)
  336. def __getFrame(self):
  337. return self.textNode.getFrameColor()
  338. def setFrame(self, frame):
  339. if frame[3] != 0:
  340. # If we have a frame color, create a frame.
  341. self.textNode.setFrameColor(frame[0], frame[1], frame[2], frame[3])
  342. self.textNode.setFrameAsMargin(0.1, 0.1, 0.1, 0.1)
  343. else:
  344. # Otherwise, remove the frame.
  345. self.textNode.clearFrame()
  346. frame = property(__getFrame, setFrame)
  347. def configure(self, option=None, **kw):
  348. # These is for compatibility with DirectGui functions
  349. if not self.mayChange:
  350. print('OnscreenText.configure: mayChange == 0')
  351. return
  352. for option, value in kw.items():
  353. # Use option string to access setter function
  354. try:
  355. setter = getattr(self, 'set' + option[0].upper() + option[1:])
  356. if setter == self.setPos:
  357. setter(value[0], value[1])
  358. else:
  359. setter(value)
  360. except AttributeError:
  361. print('OnscreenText.configure: invalid option: %s' % option)
  362. # Allow index style references
  363. def __setitem__(self, key, value):
  364. self.configure(*(), **{key: value})
  365. def cget(self, option):
  366. # Get current configuration setting.
  367. # This is for compatibility with DirectGui functions
  368. getter = getattr(self, 'get' + option[0].upper() + option[1:])
  369. return getter()
  370. def __getAlign(self):
  371. return self.textNode.getAlign()
  372. def setAlign(self, align):
  373. self.textNode.setAlign(align)
  374. align = property(__getAlign, setAlign)
  375. # Allow index style refererences
  376. __getitem__ = cget