DirectButton.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from DirectFrame import *
  2. # DirectButton States:
  3. BUTTON_READY_STATE = PGButton.SReady # 0
  4. BUTTON_DEPRESSED_STATE = PGButton.SDepressed # 1
  5. BUTTON_ROLLOVER_STATE = PGButton.SRollover # 2
  6. BUTTON_INACTIVE_STATE = PGButton.SInactive # 3
  7. class DirectButton(DirectFrame):
  8. """
  9. DirectButton(parent) - Create a DirectGuiWidget which responds
  10. to mouse clicks and execute a callback function if defined
  11. """
  12. def __init__(self, parent = aspect2d, **kw):
  13. # Inherits from DirectFrame
  14. # A Direct Frame can have:
  15. # - A background texture (pass in path to image, or Texture Card)
  16. # - A midground geometry item (pass in geometry)
  17. # - A foreground text Node (pass in text string or Onscreen Text)
  18. # For a direct button:
  19. # Each button has 4 states (ready, press, rollover, disabled)
  20. # The same image/geom/text can be used for all four states or each
  21. # state can have a different text/geom/image
  22. # State transitions happen automatically based upon mouse interaction
  23. # Responds to click event and calls command if None
  24. optiondefs = (
  25. # Define type of DirectGuiWidget
  26. ('pgFunc', PGButton, None),
  27. ('numStates', 4, None),
  28. ('state', NORMAL, None),
  29. ('invertedFrames', (1,), None),
  30. # Command to be called on button click
  31. ('command', None, None),
  32. ('extraArgs', [], None),
  33. # Which mouse buttons can be used to click the button
  34. ('commandButtons', (LMB,), self.setCommandButtons),
  35. # Sounds to be used for button events
  36. ('rolloverSound', getDefaultRolloverSound(), self.setRolloverSound),
  37. ('clickSound', getDefaultClickSound(), self.setClickSound),
  38. # Can only be specified at time of widget contruction
  39. # Do the text/graphics appear to move when the button is clicked
  40. ('pressEffect', 1, INITOPT),
  41. )
  42. # Merge keyword options with default options
  43. self.defineoptions(kw, optiondefs)
  44. # Initialize superclasses
  45. DirectFrame.__init__(self, parent)
  46. # If specifed, add scaling to the pressed state to make it look
  47. # like the button is moving when you press it
  48. if self['pressEffect']:
  49. np = self.stateNodePath[1].attachNewNode('pressEffect')
  50. np.setScale(0.98)
  51. self.stateNodePath[1] = np
  52. # Call option initialization functions
  53. self.initialiseoptions(DirectButton)
  54. def setCommandButtons(self):
  55. # Attach command function to specified buttons
  56. # Left mouse button
  57. if LMB in self['commandButtons']:
  58. self.guiItem.addClickButton(MouseButton.one())
  59. self.bind(B1CLICK, self.commandFunc)
  60. else:
  61. self.unbind(B1CLICK)
  62. self.guiItem.removeClickButton(MouseButton.one())
  63. # Middle mouse button
  64. if MMB in self['commandButtons']:
  65. self.guiItem.addClickButton(MouseButton.two())
  66. self.bind(B2CLICK, self.commandFunc)
  67. else:
  68. self.unbind(B2CLICK)
  69. self.guiItem.removeClickButton(MouseButton.two())
  70. # Right mouse button
  71. if RMB in self['commandButtons']:
  72. self.guiItem.addClickButton(MouseButton.three())
  73. self.bind(B3CLICK, self.commandFunc)
  74. else:
  75. self.unbind(B3CLICK)
  76. self.guiItem.removeClickButton(MouseButton.three())
  77. def commandFunc(self, event):
  78. if self['command']:
  79. # Pass any extra args to command
  80. apply(self['command'], self['extraArgs'])
  81. def setClickSound(self):
  82. clickSound = self['clickSound']
  83. # Clear out sounds
  84. self.guiItem.clearSound(B1PRESS + self.guiId)
  85. self.guiItem.clearSound(B2PRESS + self.guiId)
  86. self.guiItem.clearSound(B3PRESS + self.guiId)
  87. if clickSound:
  88. if LMB in self['commandButtons']:
  89. self.guiItem.setSound(B1PRESS + self.guiId, clickSound)
  90. if MMB in self['commandButtons']:
  91. self.guiItem.setSound(B2PRESS + self.guiId, clickSound)
  92. if RMB in self['commandButtons']:
  93. self.guiItem.setSound(B3PRESS + self.guiId, clickSound)
  94. def setRolloverSound(self):
  95. rolloverSound = self['rolloverSound']
  96. if rolloverSound:
  97. self.guiItem.setSound(ENTER + self.guiId, rolloverSound)
  98. else:
  99. self.guiItem.clearSound(ENTER + self.guiId)