DirectGuiTest.py 4.7 KB

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