DirectGuiTest.py 5.4 KB

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