PURPOSE:These two prototypes, getTextData and addScroll, are applied to a movieclip to load external text into the movieclip's associated text field and when loaded, to add a vertical scroll bar if needed.
SPECIAL NOTE: The addScroll requires a SWiSHmax build of 2004.04.30 or later.
PROTOTYPE:CODE
// Load data from url into text field
movieclip.prototype.getTextData = function(url) {
this.tfLoader=new LoadVars();
this.tfLoader.self=this;
this.tfLoader.onData=function(s) {
delete this.self.onEnterFrame;
if(typeOf(s)=="string") {
if(!this.self._text.html) {
s=s.split("\r").join("");
this.self._text.text=s;
} else this.self._text.htmltext=s;
this.self.addScroll();
}
delete this.self.tfLoader;
};
this.tfLoader.load(url);
this.onEnterFrame=function() {
this._text.text=((x=this.tfLoader.getBytesTotal())>0Math.ceil(100*this.tfLoader.getBytesLoaded()/x):0) add "% loaded";
};
};
// Add vertical scroll bar to text field and show if needed
movieclip.prototype.addScroll=function(sbw,sbfc,sbbc) {
if(typeOf(this._text.onScroller)!="function") {
var obj=this._text;
if(typeOf(obj)!="object") return;
if(sbw==undefined) var sbw=14;
if(sbfc==undefined) var sbfc=0xCCCCCC;
if(sbbc==undefined) var sbbc=0x000000;
obj.ow=obj._width;
obj._width-=sbw;
obj.lms=0;
var mc=obj._parent.createEmptyMovieClip(obj._name+"_sb_",999);
mc._x=obj._x+obj._width;
mc._y=obj._y;
mc.ttf=obj;
var ss=mc.createEmptyMovieClip("_ss_",1);
ss.beginFill(0xFFFFFF,100);
ss.lineStyle(1,sbbc,100);
ss.lineTo(0,obj._height );
ss.lineTo(sbw,obj._height);
ss.lineTo(sbw,0);
ss.lineTo(0,0);
ss.endFill();
db=function(o) {
o.lineStyle(1,sbbc,100);
o.beginFill(sbfc,100);
o.lineTo(0,sbw);
o.lineTo(sbw,sbw);
o.lineTo(sbw,0);
o.lineTo(0,0);
o.endFill();
o.beginFill(0,100);
o.moveTo(sbw/2,sbw/4);
o.lineTo(sbw/4,3*sbw/4);
o.lineTo(3*sbw/4,3*sbw/4);
o.lineTo(sbw/2,sbw/4);
o.endFill();
};
var up=mc.createEmptyMovieClip("_up_",2);
db(up);
var dn=mc.createEmptyMovieClip("_dn_",3);
db(dn);
dn._rotation=180;
dn._y=obj._height;
dn._x=sbw;
var br=mc.createEmptyMovieClip("_br_",4);
br.onPress=function() {
this._parent.ttf.onScroller=null;
this.startDragUnlocked(this._x,this._x,sbw,de);
this.onEnterFrame=function() {
this.spos=Math.floor(this._parent.ttf.maxscroll*(this._y-sbw+1)/(de-sbw+1));
if(this.spos>this._parent.ttf.maxscroll/2) this.spos++;
if(this.spos!=this._parent.ttf.scroll) this._parent.ttf.scroll=this.spos;
};
};
br.onRelease=br.onReleaseOutside=function() {
this.stopDrag();
delete this.onEnterFrame;
this._parent.ttf.onScroller=this._parent.ttf.saved;
this._parent.ttf.onScroller();
};
dn.onPress=function() {
mc.onEnterFrame=function() {
if(this.ttf.scroll+1>this.ttf.maxscroll) delete this.onEnterFrame;
else this.ttf.scroll+=1;
};
};
dn.onRelease=dn.onReleaseOutside=function() { delete mc.onEnterFrame; };
up.onPress=function() {
mc.onEnterFrame=function() {
if(this.ttf.scroll<2) delete this.onEnterFrame;
else this.ttf.scroll-=1;
};
};
up.onRelease=up.onReleaseOutside=function() { delete mc.onEnterFrame; };
ss.onPress=function() {
if(this._ymouse<br._y)
this._parent.ttf.scroll=Math.max(1,2*this._parent.ttf.scroll-this._parent.ttf.bottomScroll-1);
else
this._parent.ttf.scroll=Math.min(this._parent.ttf.maxscroll,this._parent.ttf.bottomScroll+1);
};
obj.saved=obj.onScroller=function() {
br._y=sbw+(this._height-sp-2*sbw)*(this.scroll-1)/(this.maxscroll-1);
if(this.maxscroll==1) {
mc._visible=false;
this._width=this.ow;
} else {
if(mc._visible==false) {
mc._visible=true;
this._width-=sbw;
}
if(this.maxscroll!=this.lms) {
this.lms=this.maxscroll;
x=this.bottomScroll-this.scroll+1;
sp=MAth.round((this._height-2*sbw)*(x)/(x+this.maxscroll));
de=obj._height-sp-sbw;
br.clear();
br.lineStyle(1,sbbc,100);
br.beginFill(sbfc,100);
br.lineTo(sbw,0);
br.lineTo(sbw,sp);
br.lineTo(0,sp);
br.lineTo(0,0);
br.endFill();
}
}
};
} else this._text.onScroller();
};
USAGE: textfield.getTextData( "textfield.txt" );
EXAMPLE:CODE
// SWiSH text field initialization
textfield1.getTextData( "textfield1.txt" );
// ActionScript text field initialization
o=createEmptyMovieClip("textfield2",1);
o.createTextField("_text",0,200,40,120,80);
(o=o._text).border=1;
o.multiline = true;
o.wordWrap = true;
o.html = true;
textfield2.getTextData( "textfield2.txt" );