test_DirectGui.py 5.5 KB

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