forked from bbpanzu/bb-fnf-mods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSndTV.hx
332 lines (285 loc) · 6.12 KB
/
SndTV.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import h2d.Tweenie.TType;
//praise delahee, i'll figure out what this shit means later!
enum TVVar{
TVVVolume;
TVVPan;
}
@:publicFields
class TweenV {
static var GUID = 0;
var uid = 0;
var man : SndTV;
var parent : Snd;
var n : Float;
var ln : Float;
var speed : Float;
var from : Float;
var to : Float;
var type : TType;
var plays : Int; // -1 = infini, 1 et plus = nombre d'exécutions (1 par défaut)
var varType : TVVar;
var onUpdate : Null<TweenV->Void>;
var onEnd : Null<TweenV->Void>;
var isDebug = false;
public inline function new (
parent:Snd ,
n:Float ,
ln:Float ,
varType:TVVar,
speed:Float ,
from:Float ,
to:Float ,
type:h2d.Tweenie.TType ,
plays ,
onUpdate ,
onEnd
) {
this.parent = parent ;
this.n = n ;
this.ln = ln ;
this.varType = varType ;
this.speed = speed ;
this.from = from ;
this.to = to ;
this.type = type ;
this.plays = plays ;
this.onUpdate = onUpdate ;
this.onEnd = onEnd ;
}
public inline function reset(
parent:Snd ,
n:Float ,
ln:Float ,
varType:TVVar,
speed:Float ,
from:Float ,
to:Float ,
type:TType ,
plays:Int ,
onUpdate ,
onEnd
) {
this.parent = parent ;
this.n = n ;
this.ln = ln ;
this.speed = speed ;
this.from = from ;
this.to = to ;
this.type = type ;
this.plays = plays ;
this.onUpdate = onUpdate ;
this.onEnd = onEnd ;
this.varType = varType ;
isDebug = false;
uid = GUID++;
}
public function clear(){
n = 0.0;
ln = 0.0;
speed = 0.0;
plays = 0;
from = 0.0;
to = 0.0;
parent = null;
onEnd = null;
onUpdate = null;
isDebug = false;
uid = GUID++;
}
public
inline
function apply( val ) {
switch(varType){
case TVVVolume: {
parent.volume = val;
#if debug
if( isDebug )
trace("tv:" + val);
#end
}
case TVVPan: parent.pan = val;
}
}
public inline function kill( withCbk = true ) {
if ( withCbk )
man.terminateTween( this );
else
man.forceTerminateTween( this) ;
}
}
/**
* tween order is not respected
*/
class SndTV {
static var DEFAULT_DURATION = DateTools.seconds(1);
public var fps = 60.0;
public var isDebug = false;
var tlist : hxd.Stack<TweenV>;
public function new() {
tlist = new hxd.Stack<TweenV>();
tlist.reserve(8);
}
function onError(e) {
trace(e);
}
public function count() {
return tlist.length;
}
public inline function create(parent:Snd, vartype:TVVar, to:Float, ?tp:h2d.Tweenie.TType, ?duration_ms:Float) : TweenV{
return create_(parent, vartype, to, tp, duration_ms);
}
public function exists(p:Snd) {
for (t in tlist)
if (t.parent == p )
return true;
return false;
}
public var pool : hxd.Stack<TweenV> = new hxd.Stack();
function create_(p:Snd, vartype:TVVar,to:Float, ?tp:h2d.Tweenie.TType, ?duration_ms:Float) : TweenV{
if ( duration_ms==null )
duration_ms = DEFAULT_DURATION;
#if debug
if ( p == null ) trace("tween2 creation failed to:"+to+" tp:"+tp);
#end
if ( tp==null ) tp = TEase;
{
// on supprime les tweens précédents appliqués à la même variable
for(t in tlist.backWardIterator())
if(t.parent==p && t.varType == vartype) {
forceTerminateTween(t);
}
}
var from = switch( vartype ){
case TVVVolume : p.volume;
case TVVPan : p.pan;
}
var t : TweenV;
if (pool.length == 0){
t = new TweenV(
p,
0.0,
0.0,
vartype,
1 / ( duration_ms*fps/1000 ), // une seconde
from,
to,
tp,
1,
null,
null
);
}
else {
t = pool.pop();
t.reset(
p,
0.0,
0.0,
vartype,
1 / ( duration_ms*fps/1000 ), // une seconde
from,
to,
tp,
1,
null,
null
);
}
if( t.from==t.to )
t.ln = 1; // tweening inutile : mais on s'assure ainsi qu'un update() et un end() seront bien appelés
t.man = this;
tlist.push(t);
return t;
}
public static inline
function fastPow2(n:Float):Float {
return n*n;
}
public static inline
function fastPow3(n:Float):Float {
return n*n*n;
}
public static inline
function bezier(t:Float, p0:Float, p1:Float,p2:Float, p3:Float) {
return
fastPow3(1-t)*p0 +
3*( t*fastPow2(1-t)*p1 + fastPow2(t)*(1-t)*p2 ) +
fastPow3(t)*p3;
}
// suppression du tween sans aucun appel aux callbacks onUpdate, onUpdateT et onEnd (!)
public function killWithoutCallbacks(parent:Snd) {
for (t in tlist.backWardIterator())
if (t.parent==parent ){
forceTerminateTween(t);
return true;
}
return false;
}
public function terminate(parent:Snd) {
for (t in tlist.backWardIterator())
if (t.parent==parent){
forceTerminateTween(t);
}
}
public function forceTerminateTween(t:TweenV) {
var tOk = tlist.remove(t);
if( tOk ){
t.clear();
pool.push(t);
}
}
public function terminateTween(t:TweenV, ?fl_allowLoop=false) {
var v = t.from + (t.to - t.from) * h2d.Tweenie.interp(t.type, 1);
t.apply(v);
onUpdate(t, 1);
var ouid = t.uid;
onEnd(t);
if( ouid == t.uid ){
if( fl_allowLoop && (t.plays==-1 || t.plays>1) ) {
if( t.plays!=-1 )
t.plays--;
t.n = t.ln = 0;
}
else {
forceTerminateTween(t);
}
}
}
public function terminateAll() {
for(t in tlist)
t.ln = 1;
update();
}
inline
function onUpdate(t:TweenV, n:Float) {
if ( t.onUpdate!=null )
t.onUpdate(t);
}
inline
function onEnd(t:TweenV) {
if ( t.onEnd!=null )
t.onEnd(t);
}
public function update(?tmod = 1.0) {
if ( tlist.length > 0 ) {
for (t in tlist.backWardIterator() ) {
var dist = t.to-t.from;
if (t.type==TRand)
t.ln+=if(Std.random(100)<33) t.speed * tmod else 0;
else
t.ln += t.speed * tmod;
t.n = h2d.Tweenie.interp(t.type, t.ln);
if ( t.ln<1 ) {
// en cours...
var val = t.from + t.n*dist;
t.apply(val);
onUpdate(t, t.ln);
}
else // fini !
{
terminateTween(t, true);
}
}
}
}
}