DialogBox.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from DirectObject import *
  2. from ShowBaseGlobal import *
  3. from GuiGlobals import *
  4. import DirectNotifyGlobal
  5. import string
  6. import OnscreenText
  7. import Button
  8. import StateData
  9. import OnscreenPanel
  10. # No buttons at all
  11. NoButtons = 0
  12. # just an OK button
  13. Acknowledge = 1
  14. # OK and CANCEL buttons
  15. TwoChoice = 2
  16. class DialogBox(OnscreenPanel.OnscreenPanel):
  17. notify = DirectNotifyGlobal.directNotify.newCategory("DialogBox")
  18. def __init__(self, message = "", doneEvent = None, style = NoButtons,
  19. font = getDefaultFont(), wordwrap = 12, okButtonText = "OK",
  20. cancelButtonText = "Cancel"):
  21. """___init___(self, Event, string="", int, model, int=12)"""
  22. # Sanity check
  23. if (doneEvent == None) and (style != NoButtons):
  24. self.notify.error("Boxes with buttons must specify a doneEvent.")
  25. self.__doneEvent = doneEvent
  26. self.message = message
  27. self.style = style
  28. self.font = font
  29. self.wordwrap = wordwrap
  30. self.okButtonText = okButtonText
  31. self.cancelButtonText = cancelButtonText
  32. # initialize our OnscreenPanel essence
  33. # NOTE: all db's have the same name so we can kill them easily
  34. OnscreenPanel.OnscreenPanel.__init__(self, "globalDialog")
  35. # make the panel
  36. self.makePanel(rect = (-0.5, 0.5, -0.4, 0.4),
  37. drawOrder = 32000, font = self.font,
  38. bg = (0.8, 0.8, 0.8, 1.0))
  39. # create a message
  40. self.makeText(self.message, wordwrap = self.wordwrap, scale = 0.08,
  41. pos = (0.0, 0.25))
  42. if (self.style == TwoChoice):
  43. # create OK and CANCEL buttons
  44. self.makeButton(self.okButtonText,
  45. pos = (-0.325, -0.25),
  46. func = self.handleOk,
  47. event = "ok")
  48. self.makeButton(self.cancelButtonText,
  49. pos = (0.2, -0.25),
  50. func = self.handleCancel,
  51. event = "cancel")
  52. elif (self.style == Acknowledge):
  53. # create a centered OK button
  54. self.makeButton(self.okButtonText,
  55. pos = (0.0, -0.25),
  56. func = self.handleOk,
  57. event = "ok")
  58. elif (self.style == NoButtons):
  59. # No buttons at all
  60. pass
  61. else:
  62. "Sanity check"
  63. self.notify.error("No such style as: " + str(self.style))
  64. return None
  65. def show(self):
  66. """show(self)
  67. """
  68. base.transitions.fadeScreen()
  69. OnscreenPanel.OnscreenPanel.show(self)
  70. return None
  71. def hide(self):
  72. """hide(self)
  73. """
  74. base.transitions.noTransitions()
  75. OnscreenPanel.OnscreenPanel.hide(self)
  76. return None
  77. def cleanup(self):
  78. """unload(self)
  79. """
  80. self.hide()
  81. OnscreenPanel.OnscreenPanel.cleanup(self)
  82. return None
  83. # def handleRollover(self):
  84. # return None
  85. def handleOk(self, okButton, item):
  86. assert(self.style != NoButtons)
  87. if (okButton == item):
  88. self.doneStatus = "ok"
  89. messenger.send(self.__doneEvent)
  90. def handleCancel(self, cancelButton, item):
  91. assert(self.style == TwoChoice)
  92. if (cancelButton == item):
  93. self.doneStatus = "cancel"
  94. messenger.send(self.__doneEvent)
  95. def setMessage(self, message):
  96. """setMessage(self, string)
  97. """
  98. self.panelText[0].setText(message)