home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK2.toast / Development Kits (Disc 2) / ScriptX / Draggable ScriptX Folders / utils / DTK / Examples / Custom Classes / instrmnt.cls < prev    next >
Encoding:
Text File  |  1995-10-27  |  4.1 KB  |  166 lines  |  [TEXT/ttxt]

  1. --<<<
  2. format debug "--  Compiling Instrument Class . . .\n" undefined undefined
  3. class InstDap(DigitalAudioPlayer)
  4. inst vars
  5.     owner
  6. end
  7.  
  8. method syncYourself self {class InstDap} state ->
  9. (
  10.     syncYourself self.owner state
  11. )
  12.  
  13. class Instrument(TwoDShape)
  14. inst vars
  15.     bitmaps
  16.     dap
  17.     activated
  18.     mouseUp
  19.     mouseCross
  20.     loopCB
  21.     name
  22. end
  23.  
  24. method init self {class Instrument} #rest args #key castNum: lingo: ->
  25. (
  26.     local bitmaps := new HashTable
  27.     add bitmaps @off castList[castNum].boundary
  28.     local onCast := castFromName(findSXKey(lingo,"hilite"))
  29.     if onCast = empty then (
  30.         report generalError "hilite keyword invalid or missing."
  31.     ) else (
  32.         add bitmaps @on onCast.boundary
  33.     )
  34.     local acName := findSXKey(lingo,"sound")
  35.     self.name := acName
  36.     format debug "instrument sound: %*\n" acName @unadorned
  37.     local audioCast := castFromName(acName)
  38.     if audioCast = empty then (
  39.         report generalError "sound keyword invalid or missing."
  40.     )
  41.     apply nextMethod self boundary:(bitmaps[@off]) args
  42.     local myDap := new InstDap  mediaStream:audioCast bufSize:100
  43.     myDap.owner := self
  44.     self.dap := myDap
  45.     self.bitmaps := bitmaps
  46.     self.activated := false
  47.     return self
  48. )
  49.  
  50. method afterInit self {class Instrument} #rest args ->
  51. (
  52.     local myPlayer := self.dap
  53.     gotoBegin myPlayer
  54.     playprepare myPlayer 1
  55.     return self
  56. )
  57.  
  58. method mouseUpSelect self {class Instrument} theInterest theEvent ->
  59. (
  60.     local activated := (self.activated = false)
  61.     local myPlayer := self.dap
  62.     if myPlayer.masterClock = undefined do
  63.         myPlayer.masterClock := self.presentedBy.sceneMasterClock
  64.     if activated then (
  65.         self.boundary := self.bitmaps[@on]
  66.         if self.loopCB = undefined do (
  67.             self.loopCB := addTimeCallback myPlayer gotoBegin myPlayer #() (myPlayer.duration) false
  68.             format debug "Slave callback for %*\n" self.name @unadorned
  69.         )
  70.         playPrepare myPlayer 1
  71.         play myPlayer
  72.         removeEventInterest self.mouseCross
  73.     ) else (
  74.         self.boundary := self.bitmaps[@off]
  75.         stop myPlayer
  76.         gotoBegin myPlayer
  77.         addEventInterest self.mouseCross
  78.     )
  79.     self.activated := activated
  80. )
  81.  
  82. method mouseRollover self {class Instrument} theInterest theEvent ->
  83. (
  84. --    local activated := self.activated
  85. --    if not activated do ( 
  86.     case theEvent.crossingType of
  87.         @enter: self.boundary := self.bitmaps[@on]
  88.         @leave: self.boundary := self.bitmaps[@off]
  89.     end
  90. --    )
  91. )
  92.  
  93. method hook self {class Instrument} ->
  94. (
  95.     local mouseDev := new MouseDevice
  96.     local muEvent := self.mouseUp
  97.     if not (isAKindOf muEvent MouseUpEvent) do (
  98.         muEvent := new MouseUpEvent
  99.         muEvent.eventReceiver := mouseUpSelect
  100.         muEvent.authorData := self
  101.         muEvent.device := mouseDev
  102.         muEvent.presenter := self
  103.         self.mouseUp := muEvent
  104.     )
  105.     addEventInterest muEvent
  106.     local mcEvent := self.mouseCross
  107.     if not (isAKindOf mcEvent MouseCrossingEvent) do (
  108.         mcEvent := new MouseCrossingEvent
  109.         mcEvent.eventReceiver := mouseRollover
  110.         mcEvent.authorData := self
  111.         mcEvent.device := mouseDev
  112.         mcEvent.presenter := self
  113.         self.mouseCross := mcEvent
  114.     )
  115.     addEventInterest mcEvent
  116. )
  117.  
  118. method unhook self {class Instrument} ->
  119. (
  120.     removeEventInterest self.mouseUp
  121.     removeEventInterest self.mouseCross
  122.     local cb := self.loopCB
  123.     if cb <> undefined do (
  124.         cancel cb
  125.         self.loopCB := undefined
  126.     )
  127.     self.boundary := self.bitmaps[@off]
  128.     stop self.dap
  129.     gotoBegin self.dap
  130. )
  131.  
  132. method slaveTo self {class Instrument} newMaster ->
  133. (
  134.     self.dap.masterClock := newMaster
  135.     format debug "Slaving %1 to %2\n" #(self.name, newMaster) #(@unadorned,@unadorned)
  136. )
  137.  
  138. method syncYourself self {class Instrument} state ->
  139. (
  140.     local cb := self.loopCB
  141.     local dap := self.dap
  142.     case state of
  143.         @sync:    (
  144.                     if cb <> undefined do (
  145.                         cancel cb
  146.                         self.loopCB := undefined
  147.                     )
  148.                     dap.time := 0
  149.                 )
  150.         @nosync:(
  151.                     if cb = undefined do (
  152.                         self.loopCB := addTimeCallback dap gotoBegin dap #() (dap.duration) false
  153.                         format debug "Slave callback for %*\n" self.name @unadorned
  154.                         if dap.duration < dap.time do gotoBegin dap
  155.                     )
  156.                 )
  157.         otherwise:    (
  158.                     format debug "%1 state: %2\n" #(self.name, state)
  159.                     report GeneralError "invalid state"
  160.                 )
  161.     end
  162.     format debug "state  of %1 to %2\n" #(self.name, state) #(@unadorned,@unadorned)
  163. )
  164.  
  165. #(Instrument, #("activated","bitmaps"), #("hook","unhook"))
  166.