Skip to content

Instantly share code, notes, and snippets.

@lynxerzhang
Last active October 11, 2015 09:57
Show Gist options
  • Save lynxerzhang/3841225 to your computer and use it in GitHub Desktop.
Save lynxerzhang/3841225 to your computer and use it in GitHub Desktop.
base on the bit101's TextArea Component
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.display.Stage;
public class TextScroll {
private var txt:TextField;
private var thumb:MovieClip;
private var dragBar:MovieClip;
private var group:MovieClip;
/**
* TODO
* base on the bit101's TextArea Component
*
* @param t
* @param g contain a dragThumb and a dragBarBackGround
*
* @example
* new TextScroll(txt, scrollGroup);
*/
public function TextScroll(t:TextField, g:MovieClip):void {
this.txt = t;
this.group = g;
this.thumb = this.group.getChildByName("scrollThumb") as MovieClip; //the dragThumb
this.dragBar = this.group.getChildByName("scrollBar") as MovieClip; //the scrollBar
this.txt.background = true;
this.txt.backgroundColor = 0xFFFFFF;
addTxtChange(this.txt);
attachThumbBar(this.txt, this.group);
thumbSizeUpdate(this.txt);
this.thumb.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
}
private var downMouseY:Number = 0;
private function mouseDownHandler(evt:MouseEvent):void{
var yRatio:Number = thumb.transform.matrix.d;
downMouseY = thumb.mouseY * yRatio;
thumb.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
thumb.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
private function mouseUpHandler(evt:MouseEvent):void{
thumb.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
thumb.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
private function mouseMoveHandler(evt:MouseEvent):void{
var yRatio:Number = this.dragBar.transform.matrix.d;
var r:Number = this.dragBar.mouseY * yRatio - downMouseY;
thumb.y = Math.max(0, Math.min(r, this.dragBar.height - thumb.height));
var dr:Number = thumb.y / (this.dragBar.height - thumb.height);
txt.scrollV = Math.round(dr * (txt.maxScrollV - 1) + 1);
}
private function attachThumbBar(t:TextField, thumbBarGroup:MovieClip):void{
thumbBarGroup.x = t.x + t.width;
thumbBarGroup.y = t.y;
this.dragBar.height = t.height;
}
private function thumbSizeUpdate(t:TextField):void{
var max:int = t.numLines;
var maxSv:int = t.maxScrollV;
var ratio:Number = (max - maxSv + 1) / max;
thumb.height = this.dragBar.height * ratio;
if(txt.scrollV > txt.maxScrollV){
txt.scrollV = txt.maxScrollV;
}
if (txt.scrollV < 1) {
txt.scrollV = 1;
}
thumb.y = ((txt.scrollV - 1) / (txt.maxScrollV - 1)) * (this.dragBar.height - thumb.height);
}
private function addTxtChange(t:TextField):void{
t.addEventListener(Event.CHANGE, function(evt:Event):void{
thumbSizeUpdate(t);
});
/*
t.addEventListener(Event.SCROLL, function(evt:Event):void{
//thumbSizeUpdate(t);
//evt.preventDefault();// unable prevent textField's scroll
});
/*/
t.addEventListener(MouseEvent.MOUSE_WHEEL, function(evt:MouseEvent):void {
t.scrollV -= evt.delta; //important
thumbSizeUpdate(t);
});
//*/
}
}
}
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.display.Stage;
public class TextScroll {
private var txt:TextField;
private var thumb:MovieClip;
private var dragBar:MovieClip;
private var group:MovieClip;
/**
* TODO
* base on the bit101's TextArea Component
*
* @param t
* @param g contain a dragThumb and a dragBarBackGround
*
* @example
* new TextScroll(txt, scrollGroup);
*/
public function TextScroll(t:TextField, g:MovieClip):void {
this.txt = t;
this.group = g;
this.thumb = this.group.getChildByName("scrollThumb") as MovieClip; //the dragThumb
this.dragBar = this.group.getChildByName("scrollBar") as MovieClip; //the scrollBar
this.txt.background = true;
this.txt.backgroundColor = 0xFFFFFF;
addTxtChange(this.txt);
attachThumbBar(this.txt, this.group);
thumbSizeUpdate(this.txt);
this.thumb.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
}
private var downMouseY:Number = 0;
private function mouseDownHandler(evt:MouseEvent):void{
var yRatio:Number = thumb.transform.matrix.d;
downMouseY = thumb.mouseY * yRatio;
thumb.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
thumb.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
private function mouseUpHandler(evt:MouseEvent):void{
thumb.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
thumb.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
private function mouseMoveHandler(evt:MouseEvent):void{
var yRatio:Number = this.dragBar.transform.matrix.d;
var r:Number = this.dragBar.mouseY * yRatio - downMouseY;
thumb.y = Math.max(0, Math.min(r, this.dragBar.height - thumb.height));
var dr:Number = thumb.y / (this.dragBar.height - thumb.height);
txt.scrollV = Math.round(dr * (txt.maxScrollV - 1) + 1);
}
private function attachThumbBar(t:TextField, thumbBarGroup:MovieClip):void{
thumbBarGroup.x = t.x + t.width;
thumbBarGroup.y = t.y;
this.dragBar.height = t.height;
}
private function thumbSizeUpdate(t:TextField):void{
var max:int = t.numLines;
var maxSv:int = t.maxScrollV;
var ratio:Number = (max - maxSv + 1) / max;
thumb.height = this.dragBar.height * ratio;
if(txt.scrollV > txt.maxScrollV){
txt.scrollV = txt.maxScrollV;
}
if (txt.scrollV < 1) {
txt.scrollV = 1;
}
thumb.y = ((txt.scrollV - 1) / (txt.maxScrollV - 1)) * (this.dragBar.height - thumb.height);
}
private function addTxtChange(t:TextField):void{
t.addEventListener(Event.CHANGE, function(evt:Event):void{
thumbSizeUpdate(t);
});
t.addEventListener(Event.SCROLL, function(evt:Event):void{
thumbSizeUpdate(t);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment