DirectFrame.py 6.8 KB

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