DirectGuiTest.py 5.5 KB

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