DirectGuiTest.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. """Undocumented Module"""
  2. __all__ = []
  3. if __name__ == "__main__":
  4. from direct.showbase.ShowBase import ShowBase
  5. from DirectGui import *
  6. #from whrandom import *
  7. from random import *
  8. base = ShowBase()
  9. # EXAMPLE CODE
  10. # Load a model
  11. smiley = loader.loadModel('models/misc/smiley')
  12. # Here we specify the button's command
  13. def dummyCmd(index):
  14. print 'Button %d POW!!!!' % index
  15. # Define some commands to bind to enter, exit and click events
  16. def shrink(db):
  17. db['text2_text'] = 'Hi!'
  18. taskMgr.remove('shrink')
  19. taskMgr.remove('expand')
  20. # Get a handle on the geometry for the rollover state
  21. rolloverSmiley = db.component('geom2')
  22. rolloverSmiley.setScale(db.component('geom0').getScale()[0])
  23. rolloverSmiley.lerpScale(.1, .1, .1, 1.0, blendType = 'easeInOut',
  24. task = 'shrink')
  25. def expand(db):
  26. db['text0_text'] = 'Bye!'
  27. taskMgr.remove('shrink')
  28. taskMgr.remove('expand')
  29. db.component('geom0').setScale(db.component('geom2').getScale()[0])
  30. db.component('geom0').lerpScale(1, 1, 1, 1, blendType = 'easeInOut',
  31. task = 'expand')
  32. db.component('geom2').clearColor()
  33. def ouch(db):
  34. taskMgr.remove('shrink')
  35. taskMgr.remove('expand')
  36. taskMgr.remove('runAway')
  37. db.component('geom0').setScale(db.component('geom2').getScale()[0])
  38. db.component('geom1').setScale(db.component('geom2').getScale()[0])
  39. db['text2_text'] = 'Ouch!'
  40. db['geom2_color'] = Vec4(1, 0, 0, 1)
  41. newX = -1.0 + random() * 2.0
  42. newZ = -1.0 + random() * 2.0
  43. db.lerpPos(Point3(newX, 0, newZ), 1.0, task = 'runAway',
  44. blendType = 'easeOut')
  45. dl = DirectFrame(image = 'models/maps/noise.rgb')
  46. dl.setScale(.5)
  47. # Create a button with a background image, smiley as a geometry element,
  48. # and a text overlay, set a different text for the four button states:
  49. # (normal, press, rollover, and disabled), set scale = .15, and relief raised
  50. dbArray = []
  51. for i in range(10):
  52. db = DirectButton(parent = dl,
  53. image = 'models/maps/noise.rgb',
  54. geom = smiley,
  55. text = ('Hi!', 'Ouch!', 'Bye!', 'ZZZZ!'),
  56. scale = .15, relief = 'raised',
  57. # Here we set an option for a component of the button
  58. geom1_color = Vec4(1, 0, 0, 1),
  59. # Here is an example of a component group option
  60. text_pos = (.6, -.8),
  61. # Set audio characteristics
  62. clickSound = DirectGuiGlobals.getDefaultClickSound(),
  63. rolloverSound = DirectGuiGlobals.getDefaultRolloverSound()
  64. )
  65. # You can set component or component group options after a gui item
  66. # has been created
  67. db['text_scale'] = 0.5
  68. db['command'] = lambda i = i: dummyCmd(i)
  69. # Bind the commands
  70. db.bind(DirectGuiGlobals.ENTER, lambda x, db = db: shrink(db))
  71. db.bind(DirectGuiGlobals.EXIT, lambda x, db = db: expand(db))
  72. db.bind(DirectGuiGlobals.B1PRESS, lambda x, db = db: ouch(db))
  73. # Pop up placer when button 2 is pressed
  74. db.bind(DirectGuiGlobals.B3PRESS, lambda x, db = db: db.place())
  75. dbArray.append(db)
  76. # To get rid of button and clear out hooks call:
  77. # db.destroy()
  78. # DIRECT ENTRY EXAMPLE
  79. def printEntryText(text):
  80. print 'Text:', text
  81. # Here we create an entry, and specify everything up front
  82. # CALL de1.get() and de1.set('new text') to get and set entry contents
  83. de1 = DirectEntry(initialText = 'Hello, how are you?',
  84. image = 'models/maps/noise.rgb',
  85. image_pos = (4.55, 0, -2.55),
  86. image_scale = (5.5, 1, 4),
  87. command = printEntryText,
  88. pos = (-1.1875, 0, 0.879167),
  89. scale = 0.0707855,
  90. cursorKeys = 1,
  91. )
  92. # DIRECT DIALOG EXAMPLE
  93. def printDialogValue(value):
  94. print 'Value:', value
  95. simpleDialog = YesNoDialog(text = 'Simple',
  96. command = printDialogValue)
  97. customValues = YesNoDialog(text = 'Not Quite So Simple',
  98. buttonValueList = ['Yes', 'No'],
  99. command = printDialogValue)
  100. fancyDialog = YesNoDialog(text = 'Testing Direct Dialog',
  101. geom = smiley,
  102. geom_scale = .1,
  103. geom_pos = (-0.3, 0, 0),
  104. command = printDialogValue)
  105. customDialog = DirectDialog(text = 'Pick a number',
  106. buttonTextList = [str(i) for i in range(10)],
  107. buttonValueList = range(10),
  108. command = printDialogValue)
  109. # NOTE: There are some utility functions which help you get size
  110. # of a direct gui widget. These can be used to position and scale an
  111. # image after you've created the entry. scale = (width/2, 1, height/2)
  112. print 'BOUNDS:', de1.getBounds()
  113. print 'WIDTH:', de1.getWidth()
  114. print 'HEIGHT:', de1.getHeight()
  115. print 'CENTER:', de1.getCenter()
  116. run()