DirectFrame.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. """Undocumented Module"""
  2. __all__ = ['DirectFrame']
  3. from panda3d.core import *
  4. from . import DirectGuiGlobals as DGG
  5. from .DirectGuiBase import *
  6. from .OnscreenImage import OnscreenImage
  7. from .OnscreenGeom import OnscreenGeom
  8. import sys
  9. if sys.version_info >= (3, 0):
  10. stringType = str
  11. else:
  12. stringType = basestring
  13. class DirectFrame(DirectGuiWidget):
  14. DefDynGroups = ('text', 'geom', 'image')
  15. def __init__(self, parent = None, **kw):
  16. # Inherits from DirectGuiWidget
  17. # A Direct Frame can have:
  18. # - A background texture (pass in path to image, or Texture Card)
  19. # - A midground geometry item (pass in geometry)
  20. # - A foreground text Node (pass in text string or Onscreen Text)
  21. # Each of these has 1 or more states
  22. # The same object can be used for all states or each
  23. # state can have a different text/geom/image (for radio button
  24. # and check button indicators, for example).
  25. optiondefs = (
  26. # Define type of DirectGuiWidget
  27. ('pgFunc', PGItem, None),
  28. ('numStates', 1, None),
  29. ('state', self.inactiveInitState, None),
  30. # Frame can have:
  31. # A background texture
  32. ('image', None, self.setImage),
  33. # A midground geometry item
  34. ('geom', None, self.setGeom),
  35. # A foreground text node
  36. ('text', None, self.setText),
  37. # Change default value of text mayChange flag from 0
  38. # (OnscreenText.py) to 1
  39. ('textMayChange', 1, None),
  40. )
  41. # Merge keyword options with default options
  42. self.defineoptions(kw, optiondefs,
  43. dynamicGroups = DirectFrame.DefDynGroups)
  44. # Initialize superclasses
  45. DirectGuiWidget.__init__(self, parent)
  46. # Call option initialization functions
  47. self.initialiseoptions(DirectFrame)
  48. def destroy(self):
  49. DirectGuiWidget.destroy(self)
  50. def setText(self):
  51. # Determine if user passed in single string or a sequence
  52. if self['text'] == None:
  53. textList = (None,) * self['numStates']
  54. elif isinstance(self['text'], stringType):
  55. # If just passing in a single string, make a tuple out of it
  56. textList = (self['text'],) * self['numStates']
  57. else:
  58. # Otherwise, hope that the user has passed in a tuple/list
  59. textList = self['text']
  60. # Create/destroy components
  61. for i in range(self['numStates']):
  62. component = 'text' + repr(i)
  63. # If fewer items specified than numStates,
  64. # just repeat last item
  65. try:
  66. text = textList[i]
  67. except IndexError:
  68. text = textList[-1]
  69. if self.hascomponent(component):
  70. if text == None:
  71. # Destroy component
  72. self.destroycomponent(component)
  73. else:
  74. self[component + '_text'] = text
  75. else:
  76. if text == None:
  77. return
  78. else:
  79. from .OnscreenText import OnscreenText
  80. self.createcomponent(
  81. component, (), 'text',
  82. OnscreenText,
  83. (), parent = self.stateNodePath[i],
  84. text = text, scale = 1, mayChange = self['textMayChange'],
  85. sort = DGG.TEXT_SORT_INDEX,
  86. )
  87. def setGeom(self):
  88. # Determine argument type
  89. geom = self['geom']
  90. if geom == None:
  91. # Passed in None
  92. geomList = (None,) * self['numStates']
  93. elif isinstance(geom, NodePath) or \
  94. isinstance(geom, stringType):
  95. # Passed in a single node path, make a tuple out of it
  96. geomList = (geom,) * self['numStates']
  97. else:
  98. # Otherwise, hope that the user has passed in a tuple/list
  99. geomList = geom
  100. # Create/destroy components
  101. for i in range(self['numStates']):
  102. component = 'geom' + repr(i)
  103. # If fewer items specified than numStates,
  104. # just repeat last item
  105. try:
  106. geom = geomList[i]
  107. except IndexError:
  108. geom = geomList[-1]
  109. if self.hascomponent(component):
  110. if geom == None:
  111. # Destroy component
  112. self.destroycomponent(component)
  113. else:
  114. self[component + '_geom'] = geom
  115. else:
  116. if geom == None:
  117. return
  118. else:
  119. self.createcomponent(
  120. component, (), 'geom',
  121. OnscreenGeom,
  122. (), parent = self.stateNodePath[i],
  123. geom = geom, scale = 1,
  124. sort = DGG.GEOM_SORT_INDEX)
  125. def setImage(self):
  126. # Determine argument type
  127. arg = self['image']
  128. if arg == None:
  129. # Passed in None
  130. imageList = (None,) * self['numStates']
  131. elif isinstance(arg, NodePath) or \
  132. isinstance(arg, Texture) or \
  133. isinstance(arg, stringType):
  134. # Passed in a single node path, make a tuple out of it
  135. imageList = (arg,) * self['numStates']
  136. else:
  137. # Otherwise, hope that the user has passed in a tuple/list
  138. if ((len(arg) == 2) and
  139. isinstance(arg[0], stringType) and
  140. isinstance(arg[1], stringType)):
  141. # Its a model/node pair of strings
  142. imageList = (arg,) * self['numStates']
  143. else:
  144. # Assume its a list of node paths
  145. imageList = arg
  146. # Create/destroy components
  147. for i in range(self['numStates']):
  148. component = 'image' + repr(i)
  149. # If fewer items specified than numStates,
  150. # just repeat last item
  151. try:
  152. image = imageList[i]
  153. except IndexError:
  154. image = imageList[-1]
  155. if self.hascomponent(component):
  156. if image == None:
  157. # Destroy component
  158. self.destroycomponent(component)
  159. else:
  160. self[component + '_image'] = image
  161. else:
  162. if image == None:
  163. return
  164. else:
  165. self.createcomponent(
  166. component, (), 'image',
  167. OnscreenImage,
  168. (), parent = self.stateNodePath[i],
  169. image = image, scale = 1,
  170. sort = DGG.IMAGE_SORT_INDEX)