PDA

View Full Version : Help with code:


MCB
01-24-2006, 08:01 PM
I am working in MX 2004 with scrollable dynamic text. Everything works great and fast until I apply a component that allows me to fully justify my text. The longer the scrollable text is the worst it gets.

The text does get fully justified but my scrollbar which was very fast before that action, becomes very slow and bumpy and the last four lines of the text do not show up. I do not know how to fix it and I would really appreciate it if anyone can take a look at it and let me know what they see wrong.

This is the code in my component. I thank you in advance for your help

MC
Attach Code

#initclip

function blocktext(){
this.MIN_WORD_DISTANCE=this.MIN_WORD_DISTANCE || 0.1;
this.MAX_LINE_TOLERANCE=this.MAX_LINE_TOLERANCE || 1.0;
this.MAX_SQUEEZE_WORDS=this.MAX_SQUEEZE_WORDS || 3;
this.ALLOW_SQUEEZE=this.ALLOW_SQUEEZE || true;
this.scrollPosition = 0;
}

var o=blocktext.prototype=new MovieClip();

Object.registerClass("blocktext",blocktext);

o.setTargetTextfield=function(textfield, scrollbar){
this._y=textfield._y;
this._x=textfield._x;
textfield._visible=false;
if (scrollbar!=null){
this.scrollbar = scrollbar;
scrollbar._visible = false;
scrollbar.addEventListener("scroll",this);
}
var newFormat=textfield.getTextFormat();
if (newFormat.size==null || newFormat.font==null) {
trace("ERROR: TextFormat non-consistant - use only one format for the whole text.");
return false;
}
this.textFormat=newFormat;
this.sourceTextfield=textfield;
this.minSpace=newFormat.size*this.MIN_WORD_DISTANC E;

//Calculate Line height
this.createTextField("test",0,0,0,0,0);
this.test.autoSize="left";
this.test.setNewTextFormat(newFormat);
this.test.text="a";
var h1= this.test._height;
this.test.text="a\na";
this.lineHeight=this.test._height-h1;
this.test.text="";


var paragraphs=textfield.text.split("\r");
if (paragraphs.length==null) return;
this.words=[];
for (var i=0;i<paragraphs.length;i++){
this.words[i]=paragraphs[i].split(" ");
}

this.setSize(textfield._width,textfield._height);

}

o.scroll = function()
{
this.sourceTextfield.scroll = this.scrollbar.scrollPosition + 1;
this.scrollPosition = this.scrollbar.scrollPosition;
this.refresh();
}

o.setSize=function(width,height){
this.sourceTextfield._width = width;
this.sourceTextfield._height = height;

this.width=width;
this.height=height-this.lineHeight;
this.maxWidth=this.MAX_LINE_TOLERANCE*this.width;
this.updateScrollbar();
this.refresh();
}

o.getSize=function(width,height){
return {width:this.width,height:this.height+this.lineHeig ht};
}


o.updateScrollbar =function(){
this.sourceTextfield.text=this.sourceTextfield.tex t;
this.sourceTextfield.scroll = this.scrollbar.scrollPosition + 1;
this.scrollbar._x = this._x + this.width + 11;
this.scrollbar._y = this._y + 7;
this.scrollbar.setScrollProperties( 1 + this.sourceTextfield.bottomScroll - this.sourceTextfield.scroll, 0 , this.sourceTextfield.maxscroll+1);
this.scrollPosition = this.scrollbar.scrollPosition = this.sourceTextfield.scroll -1;
this.scrollbar._visible =(this.sourceTextfield.maxscroll > 1);
this.scrollbar.setSize( 16, this.height);

//this.scrollbar._visible = true;

}

o.refresh=function(){
var nextDepth=0;
var nextY=0;
var nextX=0
var finished=false;
var currentParagraph=0;
var currentWord=0;
var currentLine=[];
var ignoreLines = this.scrollPosition;
while (!finished){
if (this.words[currentParagraph].length>currentWord){
this.createTextField("t"+nextDepth,nextDepth,nextX,nextY,0,0);
var txt=this["t"+nextDepth++];
txt.autoSize="left";
txt.selectable=false;
txt.setNewTextFormat(this.textFormat)
txt.embedFonts=this.sourceTextfield.embedFonts;
txt.text=this.words[currentParagraph][currentWord++];
if (nextX+txt._width>this.width){
var squeeze=false;
if (this.ALLOW_SQUEEZE && currentLine.length<this.MAX_SQUEEZE_WORDS && nextX+txt._width<this.maxWidth){
currentLine.push(txt);
squeeze=true;
}
if (squeeze || currentLine.length==1){
var letters=[];
for (var i=0;i<currentLine.length;i++){
letters=letters.concat(currentLine[i].text.split(""));
letters.push(" ");
currentLine[i].text="";
}
letters.pop();

currentLine=[];
nextX=0;

for (var i=0;i<letters.length;i++){
this.createTextField("t"+nextDepth,nextDepth,nextX,nextY,0,0);
var ltr=this["t"+nextDepth++];
ltr.autoSize="left";
ltr.selectable=false;
ltr.setNewTextFormat(this.textFormat)
ltr.embedFonts=this.sourceTextfield.embedFonts;
ltr.text=letters[i];
currentLine.push(ltr);
}

}

var spaceSize=this.width;
for (var i in currentLine){
spaceSize-=currentLine[i]._width;
}
spaceSize/=currentLine.length-1;
nextX=currentLine[0]._width+spaceSize;
for (var i=1;i<currentLine.length-1;i++){
currentLine[i]._x=nextX;
nextX+=currentLine[i]._width+spaceSize;
}
currentLine[i]._x=this.width-currentLine[i]._width;
if (!squeeze){
currentLine=[txt];
nextX=txt._width+this.minSpace;
} else {
currentLine=[];
nextX=0;
}
if (ignoreLines>0){
ignoreLines--;
for (var i=0; i<nextDepth-1;i++){
this["t"+i]._visible = false;
this["t"+i].removeMovieClip();


}
} else {
nextY+=this.lineHeight;
}
if (nextY>=this.height) {
txt.text="";
finished=true;
}
txt._x=0;
txt._y=nextY;

} else {
currentLine.push(txt);
nextX+=txt._width+this.minSpace
}
} else {
currentParagraph++;
currentWord=0;
nextX=0;
if (ignoreLines>0){
ignoreLines--;
} else {
nextY+=this.lineHeight;
}
currentLine=[];
if (nextY>=this.height || this.words.length<=currentParagraph) finished=true;
}
}
if (this.lastDepth>nextDepth){
for (var i=nextDepth;i<this.lastDepth;i++){
this["t"+i]._visible=false;
this["t"+i].removeMovieClip();
}
}
this.lastDepth=nextDepth;
}

delete o;

#endinitclip