CInterval_extensions.py 4.2 KB

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