DirectDialog.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from DirectGuiGlobals import *
  2. from DirectFrame import *
  3. from DirectButton import *
  4. class DirectDialog(DirectFrame):
  5. def __init__(self, parent = guiTop, **kw):
  6. # Inherits from DirectFrame
  7. optiondefs = (
  8. # Define type of DirectGuiWidget
  9. ('pad', (0.4, 0.4), None),
  10. ('text', '', None),
  11. ('text_align', TMALIGNCENTER, None),
  12. ('geom', getDefaultDialogGeom(), None),
  13. ('relief', None, None),
  14. ('buttons', [], INITOPT),
  15. ('buttonOffset', 0.2, None),
  16. ('button_pad', (.1,.1), None),
  17. ('button_relief', RAISED, None),
  18. ('button_frameSize', (-1,1,-.2,.9), None),
  19. ('command', None, None),
  20. ('extraArgs', [], None),
  21. )
  22. # Merge keyword options with default options
  23. self.defineoptions(kw, optiondefs, dynamicGroups = ("button",))
  24. # Initialize superclasses
  25. DirectFrame.__init__(self, parent)
  26. self.numButtons = len(self['buttons'])
  27. self.buttonList = []
  28. for button in self['buttons']:
  29. name = button + 'Button'
  30. button = self.createcomponent(
  31. name, (), "button",
  32. DirectButton, (self,),
  33. text = button,
  34. command = lambda s = self, b = button: s.__buttonCommand(b)
  35. )
  36. self.buttonList.append(button)
  37. self.numButtons += 1
  38. self.initialiseoptions(DirectDialog)
  39. # Position buttons and text
  40. # Get size of text
  41. text = self.component('text0')
  42. bounds = text.getTightBounds()
  43. pad = self['pad']
  44. l = bounds[0][0] - pad[0]
  45. r = bounds[1][0] + pad[0]
  46. b = bounds[0][2] - pad[1]
  47. t = bounds[1][2] + pad[1]
  48. text.setPos((l+r)/2.0, (b+t)/2.0)
  49. # Move buttons
  50. if self['button_frameSize']:
  51. frameSize = self['button_frameSize']
  52. bl = frameSize[0]
  53. br = frameSize[1]
  54. bb = frameSize[2]
  55. bt = frameSize[3]
  56. else:
  57. # Get bounds of union of buttons
  58. bl = br = bb = bt = 0
  59. for button in self.buttonList:
  60. bounds = button.stateNodePath[0].getTightBounds()
  61. bl = min(bl, bounds[0][0])
  62. br = max(br, bounds[1][0])
  63. bb = min(bb, bounds[0][2])
  64. bt = max(bt, bounds[1][2])
  65. bWidth = br - bl
  66. bSpacing = 1.1 * bWidth
  67. bHeight = bt - bb
  68. index = 0
  69. if self.numButtons == 0:
  70. return
  71. bPos = bSpacing * (self.numButtons - 1)/2.0
  72. for button in self.buttonList:
  73. button.setPos(bPos + index * bSpacing, 0,
  74. b - self['buttonOffset'] - bt)
  75. index += 1
  76. # Resize frame
  77. b = b - self['buttonOffset'] - bHeight - pad[1]
  78. frame = self.component('geom0')
  79. frame.setScale(r - l, 1, t - b)
  80. frame.setPos((l+r)/2.0, 0.0, (b+t)/2.0)
  81. self.resetFrameSize()
  82. def __buttonCommand(self, button):
  83. if self['command']:
  84. self['command'](button)
  85. def destroy(self):
  86. DirectFrame.destroy(self)
  87. class YesNoDialog(DirectDialog):
  88. def __init__(self, parent = guiTop, **kw):
  89. # Inherits from DirectFrame
  90. optiondefs = (
  91. # Define type of DirectGuiWidget
  92. ('buttons', ['Yes', 'No'], INITOPT),
  93. ('text', 'Yes or No?', None),
  94. )
  95. # Merge keyword options with default options
  96. self.defineoptions(kw, optiondefs)
  97. apply(DirectDialog.__init__, (self, parent), kw)
  98. self.initialiseoptions(YesNoDialog)