Guest User

FaceTriggerAnim with Stop Support

a guest
Aug 6th, 2018
338
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // FaceTriggerAnim.js
  2. // Version: 0.0.2
  3. // Event: Lens Initialized
  4. // Description: Plays an animation layer, sound, sprite, on trigger
  5.  
  6. // @input string AnimationTrigger = MouthOpenedEvent { "widget": "combobox", "values": [ { "label": "Brows Lowered", "value": "BrowsLoweredEvent" }, { "label": "Brows Raised", "value": "BrowsRaisedEvent" }, { "label": "Brows Returned To Normal", "value": "BrowsReturnedToNormalEvent" }, { "label": "Face Found", "value": "FaceFoundEvent" }, { "label": "Face Lost", "value": "FaceLostEvent" }, { "label": "Kiss Finished", "value": "KissFinishedEvent" }, { "label": "Kiss Started", "value": "KissStartedEvent" }, { "label": "Mouth Closed", "value": "MouthClosedEvent" }, { "label": "Mouth Opened", "value": "MouthOpenedEvent" }, { "label": "Smile Finished", "value": "SmileFinishedEvent" }, { "label": "Smile Started", "value": "SmileStartedEvent" }, { "label": "Touch Start", "value": "TouchStartEvent" }, { "label": "Touch End", "value": "TouchEndEvent" }, { "label": "Tap", "value": "TapEvent" } ] }
  7. // @input string AnimationStopTrigger = MouthClosedEvent { "widget": "combobox", "values": [ { "label": "Brows Lowered", "value": "BrowsLoweredEvent" }, { "label": "Brows Raised", "value": "BrowsRaisedEvent" }, { "label": "Brows Returned To Normal", "value": "BrowsReturnedToNormalEvent" }, { "label": "Face Found", "value": "FaceFoundEvent" }, { "label": "Face Lost", "value": "FaceLostEvent" }, { "label": "Kiss Finished", "value": "KissFinishedEvent" }, { "label": "Kiss Started", "value": "KissStartedEvent" }, { "label": "Mouth Closed", "value": "MouthClosedEvent" }, { "label": "Mouth Opened", "value": "MouthOpenedEvent" }, { "label": "Smile Finished", "value": "SmileFinishedEvent" }, { "label": "Smile Started", "value": "SmileStartedEvent" }, { "label": "Touch Start", "value": "TouchStartEvent" }, { "label": "Touch End", "value": "TouchEndEvent" }, { "label": "Tap", "value": "TapEvent" } ] }
  8. // @input int faceIndex = 0
  9.  
  10. // @ui {"widget":"separator"}
  11.  
  12. //  @ui {"widget": "group_start", "label": "3D Anim Control"}
  13. //  @input Component.AnimationMixer AnimationMixer
  14. //  @input string LayerName
  15. //  @input int animationPlayCount = 1 {"label": "Play Count", "min":-1, "max":99999, "step":1} // How many times to play the animation, -1 for infinite
  16. //  @ui {"widget": "group_end"}
  17.  
  18. // @ui {"widget":"group_start", "label":"2D Anim Control"}
  19. // @input Component.SpriteVisual[] play2dSprites {"label": "Play Animations"}
  20. // @input int texturePlayCount = 1 {"label": "Play Count", "min":-1, "max":99999, "step":1}
  21. // @input float texturePlayOffset = 0 {"label": "Play Offset", "min":0.0, "max":99999.0, "step":1}
  22. // @ui {"widget":"group_end"}
  23.  
  24. // @ui {"widget":"group_start", "label":"Sound Control"}
  25. // @input Asset.AudioTrackAsset[] playAudios {"label": "Play Sounds"}
  26. // @input int audioPlayCount = 1 {"label": "Play Count", "min":-1, "max":99999, "step":1}
  27. // @ui {"widget":"group_end"}
  28.  
  29. // @ui {"widget":"separator"}
  30.  
  31. //  @input float TriggerDisableTime = 0
  32.  
  33. var triggerStartTime = getTime() - script.TriggerDisableTime;
  34.  
  35. // What happens when event is triggered
  36. function onTriggered()
  37. {
  38.     print("FaceTriggerAnim: " + script.AnimationTrigger + " triggered");
  39.  
  40.     // Called when animation ends
  41.     function animationCallback()
  42.     {
  43.         script.AnimationMixer.setWeight(script.LayerName, 0.0);
  44.     }
  45.  
  46.     // Play animation layer with sound (if available)
  47.     if (script.AnimationMixer && script.LayerName)
  48.     {
  49.         script.AnimationMixer.startWithCallback(script.LayerName, 0.0, script.animationPlayCount, animationCallback);
  50.         script.AnimationMixer.setWeight(script.LayerName, 1.0);            
  51.     }
  52.  
  53.     for (var i = 0; i < script.play2dSprites.length; i++)
  54.     {
  55.         if (script.play2dSprites[i]
  56.             && script.play2dSprites[i].mainPass.baseTex
  57.             && script.play2dSprites[i].mainPass.baseTex.control
  58.             && script.play2dSprites[i].mainPass.baseTex.control.play)
  59.         {
  60.             script.play2dSprites[i].getSceneObject().enabled = true;
  61.             script.play2dSprites[i].mainPass.baseTex.control.play(script.texturePlayCount, script.texturePlayOffset);
  62.         }
  63.     }
  64.  
  65.     for (var i = 0; i < script.playAudios.length; i++)
  66.     {
  67.         if (script.playAudios[i])
  68.         {
  69.             var audioComponent = getAudioComponentForTrack(script.playAudios[i]);
  70.             audioComponent.play(script.audioPlayCount);      
  71.         }
  72.     }
  73.    
  74. }
  75.  
  76. function onStopped()
  77. {
  78.     print("FaceTriggerAnim: " + script.AnimationStopTrigger + " stopped");
  79.  
  80.     // Play animation layer with sound (if available)
  81.     if (script.AnimationMixer && script.LayerName)
  82.     {
  83.         script.AnimationMixer.stop(script.LayerName);
  84.     }
  85.  
  86.     for (var i = 0; i < script.play2dSprites.length; i++)
  87.     {
  88.         if (script.play2dSprites[i]
  89.             && script.play2dSprites[i].mainPass.baseTex
  90.             && script.play2dSprites[i].mainPass.baseTex.control
  91.             && script.play2dSprites[i].mainPass.baseTex.control.play)
  92.         {
  93.             script.play2dSprites[i].getSceneObject().enabled = true;
  94.             script.play2dSprites[i].mainPass.baseTex.control.stop();
  95.         }
  96.     }
  97.  
  98.     for (var i = 0; i < script.playAudios.length; i++)
  99.     {
  100.         if (script.playAudios[i])
  101.         {
  102.             var audioComponent = getAudioComponentForTrack(script.playAudios[i]);
  103.             audioComponent.stop(true);      
  104.         }
  105.     }
  106. }
  107.  
  108. // Trigger an action if not in cooldown period
  109. function triggerCallback()
  110. {
  111.     if (getTime() >= triggerStartTime + script.TriggerDisableTime) {
  112.         triggerStartTime = getTime();
  113.         onTriggered();
  114.     }
  115. }
  116.  
  117. function triggerStopCallback()
  118. {
  119.     onStopped();
  120. }
  121.  
  122. // Setup the audio component if audio track defined
  123. function getAudioComponentForTrack ( audioTrackAsset )
  124. {
  125.     var trackName = audioTrackAsset.name;
  126.  
  127.     if (!global.ftAudioComponents)
  128.     {
  129.         global.ftAudioComponents = {};
  130.     }
  131.  
  132.     if (!global.ftAudioComponents[trackName])
  133.     {
  134.         var audioComponent = script.getSceneObject().createComponent("Component.AudioComponent");
  135.         audioComponent.audioTrack = audioTrackAsset;
  136.        
  137.         global.ftAudioComponents[trackName] = audioComponent;  
  138.     }
  139.  
  140.     return global.ftAudioComponents[trackName];    
  141. }
  142.  
  143. // Allow fullscreen tapping if trigger is touch based
  144. if (script.AnimationTrigger == "TouchStartEvent"
  145.     || script.AnimationTrigger == "TouchStartEvent"
  146.     || script.AnimationTrigger == "TapEvent")
  147. {
  148.     global.touchSystem.touchBlocking = true;
  149.     global.touchSystem.enableTouchBlockingException("TouchTypeDoubleTap", true);
  150.     global.touchSystem.enableTouchBlockingException("TouchTypeSwipe", true);
  151. }
  152.  
  153. var event = script.createEvent(script.AnimationTrigger);
  154. event.faceIndex = script.faceIndex;
  155. event.bind(triggerCallback);
  156.  
  157. var event = script.createEvent(script.AnimationStopTrigger);
  158. event.faceIndex = script.faceIndex;
  159. event.bind(triggerStopCallback);
RAW Paste Data