-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto.as
496 lines (417 loc) · 10.4 KB
/
proto.as
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
History:
17-6 2008 - Added Array.select
17-6 2008 - Added Array.detect
17-6 2008 - Added Array.include and includes
6-5 2008 - Added Array.collect
6-5 2008 - Added MovieClip.drawRectangle()
*/
//-------------------------------------------------------------------
// OBJECT
//-------------------------------------------------------------------
Object.prototype.toNumber = function():Number
{
return Number( this )
}
Object.prototype.toInt = function():Number
{
return int( this )
}
Object.prototype.eachProperty = function()
{
var thisObject = this
var iterator = arguments[0]
if( arguments.length > 1 )
{
thisObject = arguments[0]
iterator = arguments[1]
}
for( var p in this )
{
iterator.call( thisObject, this[p], p )
}
}
Object.prototype.inspect = function():String
{
var s = this.toString()+" {\n"
for( var p in this )
{
s += "\t"+p+": "+this[p]+"\n"
}
s += "}"
return s
}
// TODO: publicMethods
//-------------------------------------------------------------------
// XML
//-------------------------------------------------------------------
XMLNode.prototype.__resolve = function( name:String ):XMLNode
{
var l = this.childNodes.length;
for( var i=0; i<l; i++ )
{
if( this.childNodes[i].nodeName == name )
{
this[name] = this.childNodes[i];
return this.childNodes[i];
}
}
}
XMLNode.prototype.toObject = function():Object
{
return utils.XMLParser.toObject( this )
}
//-------------------------------------------------------------------
// NUMBER
//-------------------------------------------------------------------
/*
* Rails like number to date properties. Scenario:
*
* n = 23
* trace( n.days.ago )
*/
Number.prototype.addProperty( "seconds", function() { return this*1000 }, null )
Number.prototype.addProperty( "second", function() { return this.seconds }, null )
Number.prototype.addProperty( "minutes", function() { return this*1000*60 }, null )
Number.prototype.addProperty( "minute", function() { return this.minutes }, null )
Number.prototype.addProperty( "hours", function() { return this*1000*60*60 }, null )
Number.prototype.addProperty( "hours", function() { return this.hours }, null )
Number.prototype.addProperty( "days", function() { return this.hours*24 }, null )
Number.prototype.addProperty( "day", function() { return this.days }, null )
Number.prototype.addProperty( "weeks", function() { return this.days*7 }, null )
Number.prototype.addProperty( "week", function() { return this.weeks }, null )
Number.prototype.addProperty( "ago", function() { return new Date( new Date().valueOf() - this ) }, null )
Number.prototype.addProperty( "fromNow", function() { return new Date( new Date().valueOf() + this ) }, null )
Number.prototype.addProperty( "isEven", function() { return (this % 2 == 0) }, null )
Number.prototype.addProperty( "isOdd", function() { return (this % 2 != 0) }, null )
Number.prototype.round = function():Number
{
return Math.round( this )
}
Number.prototype.zerofy = function():String
{
return this > 9 ? this.toString() : "0"+this
}
//-------------------------------------------------------------------
// STRING
//-------------------------------------------------------------------
String.prototype.replace = function( replaceWord, withWord ):String
{
return this.split( replaceWord ).join( withWord )
}
//-------------------------------------------------------------------
// MovieClip
//-------------------------------------------------------------------
MovieClip.prototype.addProperty( "isMouseOver", function() { return this.hitTest(_root._xmouse, _root._ymouse, true) }, null )
MovieClip.prototype.drawRectangle = function( w:Number, h:Number, color:Number, alpha:Number )
{
color = color || 0x000000
alpha = alpha || 100
this.beginFill( color, alpha )
this.lineTo( w, 0 )
this.lineTo( w, h )
this.lineTo( 0, h )
this.endFill()
}
MovieClip.prototype.trueWidth = function():Number
{
var currentXScale = this._xscale
var currentYScale = this._yscale
this._xscale = this._yscale = 100
var truth = this._width
this._xscale = currentXScale
this._yscale = currentYScale
return truth
}
MovieClip.prototype.trueHeight = function():Number
{
var currentXScale = this._xscale
var currentYScale = this._yscale
this._xscale = this._yscale = 100
var truth = this._height
this._xscale = currentXScale
this._yscale = currentYScale
return truth
}
MovieClip.prototype.buttonFix = function()
{
this.onPress = function()
{
this._focusrect = false
Selection.setFocus( this )
}
}
MovieClip.prototype.proportionalResize = function( w, h )
{
var oldXScale = this._xscale
var oldYScale = this._yscale
this._xscale = this._yscale = 100
if( w > h )
{
var percent = w / this._width
this._width = w
this._height *= percent
}
else
{
var percent = h / this._height
this._height = h
this._width *= percent
}
}
MovieClip.prototype.fadeUp = function( speed:Number ):Void
{
if( this._alpha == 100 )
{
this.onFadeUpComplete()
return
}
speed = speed || 5;
clearInterval( this.fadeId );
this.fadeId = setInterval( this, "fade", 30, 100, speed );
}
MovieClip.prototype.fadeDown = function( speed:Number ):Void
{
if( this._alpha == 0 )
{
this.onFadeDownComplete()
return
}
speed = speed || 5;
clearInterval( this.fadeId );
this.fadeId = setInterval( this, "fade", 30, 0, speed );
}
MovieClip.prototype.onFadeDownComplete = function()
{
//trace( "OVERRIDE?!??" );
}
MovieClip.prototype.onFadeUpComplete = function()
{
//trace( "OVERRIDE?!??" );
}
MovieClip.prototype.fade = function( to:Number, difference:Number ):Void
{
if( this._alpha < to )
{
this._alpha += difference;
if( this._alpha >= to )
{
this._alpha = to;
clearInterval( this.fadeId );
this.onFadeUpComplete();
}
}
else
{
this._alpha -= difference;
if( this._alpha <= to )
{
this._alpha = to;
clearInterval( this.fadeId );
this.onFadeDownComplete();
}
}
}
MovieClip.prototype.eachChild = function()
{
var thisObject = this
var iterator = arguments[0]
if( arguments.length > 1 )
{
thisObject = arguments[0]
iterator = arguments[1]
}
for( var p in this )
{
if( this[p] instanceof MovieClip || this[p] instanceof TextField) iterator.call( thisObject, this[p], p )
}
}
MovieClip.prototype.addProperty( "centerX", function() { return (Stage.width/2)-(this._width/2) }, null )
MovieClip.prototype.addProperty( "centerY", function() { return (Stage.height/2)-(this._height/2) }, null )
//-------------------------------------------------------------------
// Array
//-------------------------------------------------------------------
Array.prototype.merge = function( a2:Array ):Array
{
var separator = "%|DELIMITER|%"
var s1 = this.join( separator )
var s2 = a2.join( separator )
var merged = s1+separator+s2
return merged.split( separator )
}
// Usage:
// 1: array.each( function( item ) { trace( item ) }
// 2: array.each( this, function( item ) { trace( this+": "+item ) }
Array.prototype.each = function()
{
var thisObject = this
var iterator = arguments[0]
if( arguments.length > 1 )
{
thisObject = arguments[0]
iterator = arguments[1]
}
var len = this.length;
for( var i=0; i<len; i++ )
{
iterator.call( thisObject, this[i] )
}
}
Array.prototype.eachWithIndex = function()
{
var thisObject = this
var iterator = arguments[0]
if( arguments.length > 1 )
{
thisObject = arguments[0]
iterator = arguments[1]
}
var len = this.length;
for( var i=0; i<len; i++ )
{
iterator.call( thisObject, this[i], i )
}
}
/*
=== Collect
var a = [
{name:"David", age:23},
{name:"Ola", age:5},
{name:"Jimmy", age:28}
]
a.collect( function( element ) {
return element.name
})
=> ["David", "Ola", "Jimmy"]
*/
Array.prototype.collect = function( block:Function ):Array
{
var l = this.length
var newArray:Array = new Array()
for( var i=0; i<l; i++ )
{
newArray.push( block.call( this, this[i] ) )
}
return newArray
}
Array.prototype.include = function( o:Object ):Boolean
{
var l = this.length
for( var i=0; i<l; i++ )
{
if( this[i] == o )
{
return true
}
}
return false
}
Array.prototype.includes = function( o:Object ):Boolean
{
return this.include( o )
}
Array.prototype.select = function( block:Function ):Array
{
var selected:Array = new Array()
var l = this.length
for( var i=0; i<l; i++ )
{
if( block(this[i]) ) selected.push( this[i] )
}
return selected.length > 0 ? selected : null
}
Array.prototype.detect = function( block:Function ):Object
{
var l = this.length
for( var i=0; i<l; i++ )
{
if( block(this[i]) ) return this[i]
}
return null
}
Array.prototype.findByProperty = function( propertyName:String, value ):Object
{
var l = this.length
for( var i=0; i<l; i++ )
{
if( this[i][propertyName] == value ) return this[i]
}
}
Array.prototype.find = function( searchFunction:Function ):Object
{
var l = this.length
for( var i=0; i<l; i++ )
{
if( searchFunction( this[i] ) ) return this[i]
}
}
Array.prototype.findAll = function( searchFunction:Function ):Array
{
var findings:Array = new Array()
var l = this.length
for( var i=0; i<l; i++ )
{
if( searchFunction( this[i] ) ) findings.push( this[i] )
}
return findings
}
Array.prototype.findAllByProperty = function( propertyName:String, value ):Array
{
var findings:Array = new Array()
var l = this.length
for( var i=0; i<l; i++ )
{
if( this[i][propertyName] == value ) findings.push( this[i] )
}
return findings
}
Array.prototype.reject = function( rejectFunction:Function ):Array
{
var notRejected:Array = new Array()
var l = this.length
for( var i=0; i<l; i++ )
{
if( !rejectFunction( this[i] ) ) notRejected.push( this[i] )
}
return notRejected
}
Array.prototype.rejectByProperty = function( propertyName:String, value ):Array
{
var notRejected:Array = new Array()
var l = this.length
for( var i=0; i<l; i++ )
{
if( this[i][propertyName] != value ) notRejected.push( this[i] )
}
return notRejected
}
//Array.prototype.addProperty( "first", function() { return this[0] }, null )
//Array.prototype.addProperty( "last", function() { return this[this.length-1] }, null )
Array.prototype.randomize = function()
{
var i = this.length;
if (i == 0) return;
while (--i)
{
var j = Math.floor(Math.random()*(i+1));
var tmp1 = this[i];
var tmp2 = this[j];
this[i] = tmp2;
this[j] = tmp1;
}
return this;
}
Array.prototype.remove = function( o:Object ):Object
{
var l:Number = this.length;
while(l--)
{
if(this[l] == o)
{
var value = this[l]
this.splice(l, 1);
return value;
}
}
return null;
}