Skip to content

Commit a92e018

Browse files
author
Max van Leeuwen
committed
no message
1 parent 32b41c0 commit a92e018

File tree

3 files changed

+195
-162
lines changed

3 files changed

+195
-162
lines changed

LSQuickScripts/Example Project/LSQuickScripts Examples/Assets/LSQuickScripts/LSQuickScripts.js

Lines changed: 89 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ui {"widget":"label"}
22
//@ui {"widget":"separator"}
3-
//@ui {"widget":"label", "label":"<big><b>📜 LSQuickScripts 2.33</b> <small>by Max van Leeuwen"}
3+
//@ui {"widget":"label", "label":"<big><b>📜 LSQuickScripts 2.34</b> <small>by Max van Leeuwen"}
44
//@ui {"widget":"label", "label":"See this script for more info!"}
55
//@ui {"widget":"label"}
66
//@ui {"widget":"label", "label":"<small><a href=\"https://www.maxvanleeuwen.com/lsquickscripts\">maxvanleeuwen.com/LSQuickScripts</a>"}
@@ -25,8 +25,6 @@
2525
// -------------------
2626
// Snap Inc.
2727
//
28-
// chatGPT :)
29-
//
3028
// Tween.js - Licensed under the MIT license
3129
// https://github.com/tweenjs/tween.js
3230
// See https://github.com/tweenjs/tween.js/graphs/contributors for the full list of contributors.
@@ -120,6 +118,8 @@
120118
// anim.setCallbackAtTime(v, f) // registers a callback function on the first frame that v >= t (or v <= t if playing reversed). only 1 callback is supported at this time. call without arguments to clear. v: the linear animation time (0-1) at which to call this callback. f: the function to call.
121119
// anim.start(atTime, skipDelay) // start the animation. atTime: (optional) time ratio (0-1) to start playing from. skipDelay: (optional) ignore the delay value.
122120
// anim.stop(callEndFunction) // stop the animation. callEndFunction: (optional) whether to call the .endFunction (if animation was still playing), default is false.
121+
// anim.pause() // pause amimation (if playing)
122+
// anim.resume() // resume amimation (if paused)
123123
//
124124
// Example, smoothly animating transform 'trf' one unit to the right (default duration is 1 second)
125125
//
@@ -293,10 +293,12 @@
293293
// delayed.byTime(10) // this will call the function in 10 seconds (function is called instantly if no argument given or if arg is '0') - the DoDelay instance is returned
294294
// delayed.now() // call the function with the given arguments now
295295
// delayed.stop() // this will cancel the scheduled function
296-
// delayed.isWaiting() // returns true if currently counting down to call the function
296+
// delayed.pause() // pause (if running)
297+
// delayed.resume() // resume (if paused)
298+
// delayed.isRunning() // returns true if currently counting down to call the function
297299
// delayed.createdAtTime // the time at which this instance was created
298300
// delayed.getTimeLeft() // get the time left before the function is called (null if unused)
299-
// delayed.getFramesLeft() // the frames left before the function is called (null if unused)
301+
// delayed.getFramesLeft() // get the frames left before the function is called (null if unused)
300302
// delayed.getGivenTime() // get the amount of time that was last given to wait (null if none yet)
301303
// delayed.getGivenFrames() // get the amount of frames that was last given to wait (null if none yet)
302304
//
@@ -306,8 +308,8 @@
306308
//
307309
//
308310
//
309-
// stopAllDelays() : DoDelay array
310-
// Instantly stops all delays created using 'DoDelay'. This is useful if you want to create a quick reset function for your lens without managing all the created delays throughout your project.
311+
// getAllDelays() : DoDelay array
312+
// Returns all delays created using 'DoDelay'. This includes expired delays.
311313
//
312314
//
313315
//
@@ -328,8 +330,8 @@
328330
//
329331
//
330332
//
331-
// stopAllSoundInstances() : AudioComponent array
332-
// Instantly stops all sound instances created using 'instSound'. This is useful if you want to create a quick reset function for your lens without managing all the created sounds throughout your project.
333+
// getAllSoundInstances() : AudioComponent array
334+
// Returns all sound instances created using 'instSound'.
333335
//
334336
//
335337
//
@@ -529,20 +531,6 @@
529531
//
530532
//
531533
//
532-
// PerformanceStopwatch() : PerformanceStopwatch object
533-
// Debugging tool. Prints precise time measures to see how well a function performs. Has built-in rolling average!
534-
//
535-
// Example, showing all properties
536-
// var stopwatch = new PerformanceStopwatch() // create new PerformanceStopwatch object
537-
// stopwatch.start() // starts the stopwatch
538-
// // < do something to measure on this line >
539-
// stopwatch.stop() // stops the stopwatch, prints the result (and a rolling average of previous results) to the console
540-
//
541-
//
542-
// -
543-
//
544-
//
545-
//
546534
// setAllChildrenToLayer(sceneObj [sceneObject], layer [LayerSet])
547535
// Sets the sceneObject and all objects underneath it to a specific render layer (by LayerSet).
548536
//
@@ -589,21 +577,24 @@
589577
//
590578
//
591579
//
592-
// getAllComponents(componentName (optional) [string]
580+
// getAllChildObjects(componentName (optional) [string]
593581
// startObj (optional) [SceneObject]
594582
// dontIncludeStartObj (optional) [bool]
595583
// maxCount (optional) [number]
596584
// ) : Array (Components)
597585
// Returns an array containing all components of type componentNames, also those on child objects.
598-
// If no componentName is given, it returns SceneObjects instead.
599-
// If no startObj is given, it searches the whole scene.
586+
// If no componentName is given, all SceneObjects and child SceneObjects are returned instead.
587+
// If no startObj is given, the whole scene is searched.
600588
// If dontIncludeStartObj is true, the startObj will not be included in the final list.
601589
// If maxCount is given, the search stops after having found a specific amount of components.
602590
//
603591
// Example
604-
// var components = getAllComponents("Component.VFXComponent")
592+
// var components = getAllChildObjects("Component.VFXComponent")
605593
// components == [Array of all VFX Component in the scene]
606594
//
595+
// var objects = getAllChildObjects("", obj)
596+
// objects == [Array of all SceneObjects under 'obj']
597+
//
607598
//
608599
//
609600
// -
@@ -1142,7 +1133,9 @@ global.AnimateProperty = function(updateFunction){
11421133
stopDelayedStart();
11431134

11441135
function begin(){
1145-
if(self.startFunction) self.startFunction(!reversed);
1136+
stopDelayedStart();
1137+
1138+
if(self.startFunction && !pause) self.startFunction(!reversed); // play startFunction is this is not a resumed play
11461139
if(atTime != null){ // custom time ratio given
11471140
pulse(atTime, true);
11481141
}else{
@@ -1163,7 +1156,7 @@ global.AnimateProperty = function(updateFunction){
11631156
var delay = self.delay;
11641157
if(reversed && typeof(self.reverseDelay) != 'undefined') delay = self.reverseDelay; // if reverse, use custom delay (if any)
11651158
if(!skipDelay && delay > 0){ // start after delay (if any)
1166-
delayedStart = new global.DoDelay(begin)
1159+
delayedStart = new global.DoDelay(begin);
11671160
delayedStart.byTime(delay);
11681161
}else{
11691162
begin();
@@ -1175,12 +1168,46 @@ global.AnimateProperty = function(updateFunction){
11751168
* @param {boolean} callEndFunction callEndFunction: (optional) whether to call the .endFunction (if animation was still playing), default is false.
11761169
*/
11771170
this.stop = function(callEndFunction){
1171+
pause = null;
11781172
stopAnimEvent();
11791173
var wasPlaying = isPlaying;
11801174
isPlaying = false;
11811175
if(wasPlaying && callEndFunction) self.endFunction(!reversed);
11821176
}
11831177

1178+
/**
1179+
* @description pause the animation (if running)
1180+
*/
1181+
var pause;
1182+
this.pause = function(){
1183+
if(!self.isPlaying()) return;
1184+
1185+
const timeLeft = self.getTimeRatio();
1186+
if(delayedStart){
1187+
delayedStart.pause();
1188+
pause = {
1189+
time : null
1190+
}
1191+
}else{
1192+
stopAnimEvent();
1193+
isPlaying = false;
1194+
1195+
pause = {
1196+
time : timeLeft
1197+
}
1198+
}
1199+
}
1200+
1201+
/**
1202+
* @description resume the animation (if paused)
1203+
*/
1204+
this.resume = function(){
1205+
if(!pause) return;
1206+
if(delayedStart) delayedStart.resume();
1207+
else self.start(pause.time, skipDelay=true);
1208+
pause = null;
1209+
}
1210+
11841211

11851212
// private
11861213

@@ -2558,7 +2585,7 @@ global.DoDelay = function(func, args){
25582585
/**
25592586
* @description returns true if currently counting down to call the function
25602587
*/
2561-
this.isWaiting = () => !!waitEvent;
2588+
this.isRunning = () => !!waitEvent;
25622589

25632590
/**
25642591
* @type {number}
@@ -2605,6 +2632,8 @@ global.DoDelay = function(func, args){
26052632
* @description schedule a function by n frames (int Number, will be rounded)
26062633
*/
26072634
this.byFrame = function(n){
2635+
paused = null;
2636+
26082637
if(!this.func){
26092638
var trace = new Error().stack;
26102639
throw new Error("No function set to delay!" + '\n' + trace.toString());
@@ -2650,6 +2679,8 @@ global.DoDelay = function(func, args){
26502679
* @description schedule a function by t seconds (Number)
26512680
*/
26522681
this.byTime = function(t){
2682+
paused = null;
2683+
26532684
const keepAlive = {
26542685
exec: function(){
26552686
var _args = self.args;
@@ -2679,6 +2710,7 @@ global.DoDelay = function(func, args){
26792710
* @description call the function now
26802711
*/
26812712
this.now = function(){
2713+
paused = null;
26822714
const keepAlive = {
26832715
exec: function(){
26842716
var _args = self.args;
@@ -2701,20 +2733,39 @@ global.DoDelay = function(func, args){
27012733
* @description stop the scheduled delay
27022734
*/
27032735
this.stop = function(){
2736+
paused = null;
27042737
stopWaitEvent();
27052738
}
27062739

2740+
/**
2741+
* @description pauses the scheduled delay (if running)
2742+
*/
2743+
var paused;
2744+
this.pause = function(){
2745+
if(!waitEvent) return;
2746+
paused = {
2747+
frames : self.getFramesLeft(),
2748+
time : self.getTimeLeft()
2749+
}
2750+
stopWaitEvent();
2751+
}
2752+
2753+
/**
2754+
* @description resumes the scheduled delay (if paused)
2755+
*/
2756+
this.resume = function(){
2757+
if(!paused) return;
2758+
if(paused.frames) self.byFrame(paused.frames);
2759+
else if(paused.time) self.byTime(paused.time);
2760+
}
2761+
27072762
allDelays.push(this);
27082763
}
27092764

27102765

27112766

27122767

2713-
global.stopAllDelays = function(){
2714-
for(var i = 0; i < allDelays.length; i++){
2715-
var delay = allDelays[i];
2716-
if(delay && delay.stop) delay.stop();
2717-
}
2768+
global.getAllDelays = function(){
27182769
return allDelays;
27192770
}
27202771

@@ -2743,7 +2794,7 @@ global.instSound = function(audioAsset, volume, fadeInTime, fadeOutTime, offset,
27432794
function destroyAudioComponent(audioComp){
27442795
audioComp.stop(false); // stop playing
27452796
new global.DoDelay(function(){
2746-
if(audioComp && !isNullPatch(audioComp)) audioComp.destroy(); // destroy if it still exists (might have been deleted using stopAllSoundInstances)
2797+
if(audioComp && !isNullPatch(audioComp)) audioComp.destroy(); // destroy if it still exists
27472798
}).byFrame(); // delete on next frame
27482799
}
27492800
new DoDelay( destroyAudioComponent, [audioComp]).byTime(audioComp.duration + .1); // stop playing after audio asset duration
@@ -2755,14 +2806,7 @@ global.instSound = function(audioAsset, volume, fadeInTime, fadeOutTime, offset,
27552806

27562807

27572808

2758-
global.stopAllSoundInstances = function(){
2759-
for(var i = 0; i < allSoundInstances.length; i++){
2760-
var soundInstance = allSoundInstances[i];
2761-
if(soundInstance && !isNullPatch(soundInstance)){
2762-
soundInstance.stop(false);
2763-
soundInstance.destroy();
2764-
}
2765-
}
2809+
global.getAllSoundInstances = function(){
27662810
return allSoundInstances;
27672811
}
27682812

@@ -3123,30 +3167,6 @@ global.MovingAverage = function(){
31233167

31243168

31253169

3126-
global.PerformanceStopwatch = function(){
3127-
var stopwatchStart;
3128-
var avg = new global.MovingAverage();
3129-
3130-
/**
3131-
* @description starts this stopwatch
3132-
*/
3133-
this.start = function(){
3134-
stopwatchStart = performance.now();
3135-
}
3136-
3137-
/**
3138-
* @description stops this stopwatch, prints the result to the console
3139-
*/
3140-
this.stop = function(){
3141-
var diff = (performance.now() - stopwatchStart)/1000; // differents in seconds
3142-
var thisAvg = avg.add(diff);
3143-
print('duration: ' + diff.toString() + '\n' + 'rolling avg: ' + thisAvg.toString());
3144-
}
3145-
}
3146-
3147-
3148-
3149-
31503170
global.setAllChildrenToLayer = function(sceneObj, layer){
31513171
for(var i = 0; i < sceneObj.getChildrenCount(); i++){
31523172
sceneObj.getChild(i).layer = layer;
@@ -3205,7 +3225,7 @@ global.measureWorldPos = function(screenPos, screenTrf, cam, dist){
32053225

32063226

32073227

3208-
global.getAllComponents = function(componentName, startObj, dontIncludeStartObj, maxCount){
3228+
global.getAllChildObjects = function(componentName, startObj, dontIncludeStartObj, maxCount){
32093229
var found = [];
32103230
if(maxCount == null) maxCount = Infinity;
32113231

0 commit comments

Comments
 (0)