CInterval_extensions.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from extension_native_helpers import *
  2. try:
  3. Dtool_PreloadDLL("libp3direct")
  4. from libp3direct import *
  5. except:
  6. Dtool_PreloadDLL("libdirect")
  7. from libdirect import *
  8. #####################################################################
  9. from direct.directnotify.DirectNotifyGlobal import directNotify
  10. notify = directNotify.newCategory("Interval")
  11. Dtool_ObjectToDict(CInterval,"notify", notify)
  12. del notify
  13. #####################################################################
  14. def setT(self, t):
  15. # Overridden from the C++ function to call privPostEvent
  16. # afterward. We do this by renaming the C++ function in
  17. # FFIRename.
  18. self.setT_Old(t)
  19. self.privPostEvent()
  20. Dtool_ObjectToDict(CInterval, "setT_Old", CInterval.setT)
  21. Dtool_funcToMethod(setT, CInterval)
  22. del setT
  23. #####################################################################
  24. def play(self, t0 = 0.0, duration = None, scale = 1.0):
  25. self.notify.error("using deprecated CInterval.play() interface")
  26. if duration: # None or 0 implies full length
  27. self.start(t0, t0 + duration, scale)
  28. else:
  29. self.start(t0, -1, scale)
  30. Dtool_funcToMethod(play, CInterval)
  31. del play
  32. #####################################################################
  33. def stop(self):
  34. self.notify.error("using deprecated CInterval.stop() interface")
  35. self.finish()
  36. Dtool_funcToMethod(stop, CInterval)
  37. del stop
  38. #####################################################################
  39. def setFinalT(self):
  40. self.notify.error("using deprecated CInterval.setFinalT() interface")
  41. self.finish()
  42. Dtool_funcToMethod(setFinalT, CInterval)
  43. del setFinalT
  44. #####################################################################
  45. def privPostEvent(self):
  46. # Call after calling any of the priv* methods to do any required
  47. # Python finishing steps.
  48. t = self.getT()
  49. if hasattr(self, "setTHooks"):
  50. for func in self.setTHooks:
  51. func(t)
  52. Dtool_funcToMethod(privPostEvent, CInterval)
  53. del privPostEvent
  54. #####################################################################
  55. def popupControls(self, tl = None):
  56. """
  57. Popup control panel for interval.
  58. """
  59. from direct.showbase.TkGlobal import Toplevel, Frame, Button, LEFT, X, Pmw
  60. import math
  61. from direct.tkwidgets import EntryScale
  62. if tl == None:
  63. tl = Toplevel()
  64. tl.title('Interval Controls')
  65. outerFrame = Frame(tl)
  66. def entryScaleCommand(t, s=self):
  67. s.setT(t)
  68. s.pause()
  69. self.es = es = EntryScale.EntryScale(
  70. outerFrame, text = self.getName(),
  71. min = 0, max = math.floor(self.getDuration() * 100) / 100,
  72. command = entryScaleCommand)
  73. es.set(self.getT(), fCommand = 0)
  74. es.pack(expand = 1, fill = X)
  75. bf = Frame(outerFrame)
  76. # Jump to start and end
  77. def toStart(s=self, es=es):
  78. s.setT(0.0)
  79. s.pause()
  80. def toEnd(s=self):
  81. s.setT(s.getDuration())
  82. s.pause()
  83. jumpToStart = Button(bf, text = '<<', command = toStart)
  84. # Stop/play buttons
  85. def doPlay(s=self, es=es):
  86. s.resume(es.get())
  87. stop = Button(bf, text = 'Stop',
  88. command = lambda s=self: s.pause())
  89. play = Button(
  90. bf, text = 'Play',
  91. command = doPlay)
  92. jumpToEnd = Button(bf, text = '>>', command = toEnd)
  93. jumpToStart.pack(side = LEFT, expand = 1, fill = X)
  94. play.pack(side = LEFT, expand = 1, fill = X)
  95. stop.pack(side = LEFT, expand = 1, fill = X)
  96. jumpToEnd.pack(side = LEFT, expand = 1, fill = X)
  97. bf.pack(expand = 1, fill = X)
  98. outerFrame.pack(expand = 1, fill = X)
  99. # Add function to update slider during setT calls
  100. def update(t, es=es):
  101. es.set(t, fCommand = 0)
  102. if not hasattr(self, "setTHooks"):
  103. self.setTHooks = []
  104. self.setTHooks.append(update)
  105. self.setWantsTCallback(1)
  106. # Clear out function on destroy
  107. def onDestroy(e, s=self, u=update):
  108. if u in s.setTHooks:
  109. s.setTHooks.remove(u)
  110. tl.bind('<Destroy>', onDestroy)
  111. Dtool_funcToMethod(popupControls, CInterval)
  112. del popupControls
  113. #####################################################################