DialogBox.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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):
  20. """___init___(self, Event, string="", int, model, int=12)"""
  21. # Sanity check
  22. if (doneEvent == None) and (style != NoButtons):
  23. self.notify.error("Boxes with buttons must specify a doneEvent.")
  24. self.__doneEvent = doneEvent
  25. self.message = message
  26. self.style = style
  27. self.font = font
  28. self.wordwrap = wordwrap
  29. # initialize our OnscreenPanel essence
  30. # NOTE: all db's have the same name so we can kill them easily
  31. OnscreenPanel.OnscreenPanel.__init__(self, "globalDialog")
  32. # make the panel
  33. self.makePanel(rect = (-0.5, 0.5, -0.4, 0.4),
  34. drawOrder = 32000, font = self.font,
  35. bg = (0.8, 0.8, 0.8, 1.0))
  36. # create a message
  37. self.makeText(self.message, wordwrap = self.wordwrap, scale = 0.08,
  38. pos = (0.0, 0.25))
  39. if (self.style == TwoChoice):
  40. # create OK and CANCEL buttons
  41. self.makeButton("OK", pos = (-0.325, -0.25),
  42. func = self.handleOk, event = "ok")
  43. self.makeButton("Cancel", pos = (0.2, -0.25),
  44. func = self.handleCancel, event = "cancel")
  45. elif (self.style == Acknowledge):
  46. # create a centered OK button
  47. self.makeButton("OK", pos = (0.0, -0.25), func = self.handleOk,
  48. event = "ok")
  49. elif (self.style == NoButtons):
  50. # No buttons at all
  51. pass
  52. else:
  53. "Sanity check"
  54. self.notify.error("No such style as: " + str(self.style))
  55. return None
  56. def show(self):
  57. """show(self)
  58. """
  59. base.transitions.fadeScreen()
  60. OnscreenPanel.OnscreenPanel.show(self)
  61. return None
  62. def hide(self):
  63. """hide(self)
  64. """
  65. base.transitions.noTransitions()
  66. OnscreenPanel.OnscreenPanel.hide(self)
  67. return None
  68. def cleanup(self):
  69. """unload(self)
  70. """
  71. self.hide()
  72. OnscreenPanel.OnscreenPanel.cleanup(self)
  73. return None
  74. # def handleRollover(self):
  75. # return None
  76. def handleOk(self, okButton, item):
  77. assert(self.style != NoButtons)
  78. if (okButton == item):
  79. self.doneStatus = "ok"
  80. messenger.send(self.__doneEvent)
  81. def handleCancel(self, cancelButton, item):
  82. assert(self.style == TwoChoice)
  83. if (cancelButton == item):
  84. self.doneStatus = "cancel"
  85. messenger.send(self.__doneEvent)
  86. def setMessage(self, message):
  87. """setMessage(self, string)
  88. """
  89. self.panelText[0].setText(message)