/** * Copyright (c) 2006 Peter Goodman * Licensed under the MIT License: * http://www.opensource.org/licenses/mit-license.php */ class FA.query { private var _o; private var _d; public var filearts = 'FileArts v2.0'; public function query(o,d) { _o = o; _d = d; } // ooh.. this is evil :P public function $(e, c) : FA.query { return _global.$(e, c); } /** * Main Functions */ public function size(e) : Number { var s:Number; if(e == undefined) { s = _o.length || 0; } else { s = this.get(e).length; } return s; } public function each(f:Function, s) { for(var i in _o) { f.apply((_o[i][s] || _o[i]), [i]); } return this; } public function attr(k, v, z) { return (v != undefined || k.constructor == Object) ? this.each(function(i) { if(v == undefined) { var t; for(var p in k) { this[p] = k[p]; } } else { this[k] = v; } }, z) : (this.size() > 1 ? function(e) { var c:Array = []; e.each(function() { c.push(this[k]); }, z); return c; }(this) : (z ? _o[0][z][k] : _o[0][k])); } public function _get(z) { return _o[z] || _o; } public function get(k) { return this.attr(k); } public function set(k, v) { return this.attr(k, v); } /** * Events */ public function bind(e:String, f:Function) : FA.query { return this.each(function(i) { FA.event.addEvent(this, e, f); this[e] = function() : Void { var es:Array = FA.event.getEvents(this, e); for(var ff in es) { es[ff].apply(this, arguments); } }; }); } public function hover(h:Function, l) : FA.query { return this.each(function(i){ if (this instanceof MovieClip) { _global.$(this).bind("onRollOver", function() : Void { if (this.hitTest(_root._xmouse, _root._ymouse, true)) { h.apply(this, []); } else { if(l != undefined) { l.apply(this, []); } } }); if (l != undefined) { _global.$(this).bind("onRollOut", l); } } }); } public function click(p:Function, r) : FA.query { return this.each(function(i){ if (this instanceof MovieClip) { _global.$(this).bind("onPress", p); if (r != undefined) { _global.$(this).bind("onRelease", r); } } }); } public function loader(l:Function, f:Function, fr) : FA.query { if (_d) { if (_framesloaded >= _totalframes) { if (f != undefined) { f.apply(null, []); } else { gotoAndPlay(fr || 1); } } else { var lb:Number = _root.getBytesLoaded(); var tb:Number = _root.getBytesTotal(); l.apply(null, [Math.ceil((lb / tb) * 100)]); } } return this; } /** * Movie Clips */ private function parent(o:MovieClip) : String { var a:String = ''; if(o._parent != undefined and o._parent instanceof MovieClip) { if(o._parent._name != '') { a += '.' + o._parent._name; a += this.parent(o._parent); } } return a; } private function cache(m:MovieClip) : MovieClip { if(_global.fa_cache[m._name] == undefined) { var as:Array = this.parent(m).split('.'); as.reverse(); _global.fa_cache[m._name] = ('_root.' + as.join('.')) + m._name; } return m; } public function movieClip(n, d) : MovieClip { if(_o[0] instanceof MovieClip) { if(!_global.mc_cache[n] && !this.get(n) && _o[0]._name != n) { _o[0].createEmptyMovieClip(n, (d || ++_global.fa_depth)); } } return this.cache(this.get(n)); } public function duplicate(n, d) : MovieClip { if (_o[0] instanceof MovieClip) { _o[0].duplicateMovieClip(n, (d || ++_global.fa_depth)); } return this.cache(_o[0]._parent[n]); } public function mask(m:String) { var e:MovieClip = _o[0]._parent[m]; if(_o[0] instanceof MovieClip && e instanceof MovieClip) { e.setMask(_o[0]); } return this; } public function text(e, d) : FA.query { return this.each(function(i){ if (this instanceof MovieClip) { this.createTextField(e._name, (d || ++_global.fa_depth), e._x, e._y, e._width, e._height); var fi:TextField = this[e._name]; var fo:TextFormat = new TextFormat(); var n = true; var vf:Object = { align: n, blockIndent: n, bold: n, bullet: n, color: n, font: n, indent: n, italic: n, leading: n, leftMargin: n, rightMargin: n, tabStops: n, target: n, size: n, underline: n, url: n }; for (var p in e) { if (vf[p]) { fo[p] = e[p]; } else { fi[p] = e[p]; } } fi.setTextFormat(fo); } else if (this instanceof XML) { this.createTextNode(e); } }); } public function movie(e:Object) : FA.query { return this.each(function(i){ if(this instanceof MovieClip) { this.loadMovie(e.url, e.method || e.variables); } }); } public function unmovie() : FA.query { return this.each(function(i){ if(this instanceof MovieClip) { this.unloadMovie(); } }); } public function audio(e) : FA.query { return this.each(function(i){ if(this instanceof MovieClip) { this.attachAudio(e); } }); } public function unaudio() : FA.query { return this.audio(false); } /** * XML Functions */ public function load(e, f) : FA.query { if (_d) { _o = [new XML()]; } return this.each(function(i){ if (this instanceof XML || this instanceof XMLSocket) { if (e.constructor == Function) { _global.$(this).bind("onLoad", function(s:Boolean) : Void { if(s) { e.apply(this,[s]); } else { if(f) { f.apply(this,[false]); } } }); } } else { _global.$(this).bind("onLoad", function(s) : Void { e.apply(this,[s]); }); } }); } public function loadXML(e:String) : FA.query { return this.each(function(i){ this.load(e); }); } public function loading(li:Function, ld) : FA.query { if(_o[0] instanceof XML) { if(_o[0].getBytesLoaded() < _o[0].getBytesTotal()) { li.apply(_o[0], []); } this.bind("onLoad", function(s){ if(s && ld) { ld.apply(_o[0], []); } }); } return this; } public function data(e:Function) : FA.query { return this.each(function(i){ if (this.onData != undefined) { if (this instanceof XML or this instanceof XMLSocket) { this.onData = e; } } }); } public function xml(e:Function) : FA.query { return this.each(function(i){ if (this instanceof XML or this instanceof XMLSocket) { if (this instanceof XMLSocket) { this.onXML = e; } } }); } public function attrib(k:String, v) { return this.attr(k, v, 'attributes'); } public function close() : FA.query { return this.each(function(i){ if(this instanceof XMLSocket) { this.close(); } }); } /** * Drawing functions. */ private var _i:Object; private var line_width:Number; private var line_color:Number; private var line_transparency:Number; private var fill_transparency:Number; //-------------------------------------------- // Draw a shape. //-------------------------------------------- public function draw(e:Object) : FA.query { var s:String = e.shape.toLowerCase(); var _s:FA.query = this; _i = e; return this.each(function(i) { if(_s[s].constructor == Function && this instanceof MovieClip) { line_width = e.lineWidth || 1; line_color = e.lineColor || 0x000000; line_transparency = e.lineAlpha || 100; fill_transparency = e.fillAlpha || 100; if(e.fillColor != undefined) { this.beginFill(e.fillColor, fill_transparency); } this.lineStyle(line_width, line_color, line_transparency); _s[s].apply(_s, [this]); if(e.fillColor != undefined) { this.endFill(); } } }); } //-------------------------------------------- // Draw a circle. Format: // { // radius: 10, // _x: 100, // _y: 100 // } //-------------------------------------------- private function circle(e) : Void { var x:Number = 0, y:Number = 0, a:Number = 0; for (a = 0; a <= 360; a++) { x = _i._x + _i.radius * Math.cos(a * Math.PI / 180); y = _i._y + _i.radius * Math.sin(a * Math.PI / 180); if (a == 0) { e.moveTo(x, y); } e.lineTo(x, y); } } //-------------------------------------------- // Draw a rectangle. Format: // { // width: 100, // height: 100, // } //-------------------------------------------- private function rectangle(e) : Void { e.moveTo(_i._x, _i._y); e.lineTo(_i.width, _i._y); e.lineTo(_i.width, _i.height); e.lineTo(_i._x, _i.height); } //-------------------------------------------- // Draw a triangle. // { // a: [x, y], // left // b: [x, y], // right // _x: 100, // _y: 100 // } //-------------------------------------------- private function triangle(e) : Void { e.moveTo(_i._x, _i._y); e.lineTo(_i.b[1], _i.b[1]); // c e.lineTo(_i.a[0], _i.a[1]); // a e.lineTo(_i._x, _i._y); // b } /** * Effects */ public function show() : FA.query { return this.set('_visible', true); } public function hide() : FA.query { return this.set('_visible', false); } }