スクロールバー実装
スクロールバー実装の練習です
// forked from zigxzag's flash on 2009-7-23
// スクロールバー実装の練習です
package
{
import flash.display.Sprite;
import flash.events.Event;
[SWF(height=465,width=465,frameRate=30,background=0xffffff)]
public class Main extends Sprite
{
private var scrollBar:ScrollBar;
private var pageContainer:Sprite;
public function Main()
{
this.addEventListener( Event.ADDED_TO_STAGE, init );
}
private function init( evt:Event ):void
{
this.removeEventListener( Event.ADDED_TO_STAGE, init );
scrollBar = new ScrollBar();
scrollBar.x = 465 - 10;
scrollBar.addEventListener( ScrollEvent.SCROLL, scrollingHandler );
addChild( scrollBar );
pageContainer = new Sprite();
for (var i:int = 0; i < 10; i++ ) {
var sample:SamplePage = new SamplePage( i + 1 );
sample.x = 200;
sample.y = 200 + 300 * i;
pageContainer.addChild( sample );
}
addChild( pageContainer );
}
private function scrollingHandler( evt:ScrollEvent ):void
{
pageContainer.y -= ( (pageContainer.height * scrollBar.percent) + pageContainer.y ) / 6;;
}
}
}
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.display.Stage;
class ScrollBar extends Sprite
{
public var bar:Sprite;
public var rail:Shape;
private var range:Rectangle;
private var per:Number;
public function ScrollBar()
{
this.addEventListener( Event.ADDED_TO_STAGE, init );
}
private function init( evt:Event ):void
{
this.removeEventListener( Event.ADDED_TO_STAGE, init );
rail = new Shape();
with( rail.graphics ) {
beginFill( 0xcccccc, 1 );
drawRect( 0, 0, 10, 465 );
endFill();
}
addChild( rail );
bar = new Sprite();
bar.buttonMode = true;
bar.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
bar.addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
this.stage.addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
with( bar.graphics ) {
beginFill( 0x00aeff, 1);
drawRect( 0, 0, 10, 30 );
endFill();
}
var tri1:Shape = new Shape();
with( tri1.graphics ) {
beginFill( 0xffffff );
moveTo( 5, 2 );
lineTo( 8, 8 );
lineTo( 2, 8 );
endFill();
}
var tri2:Shape = new Shape();
with( tri2.graphics ) {
beginFill( 0xffffff );
moveTo( 2, 22 );
lineTo( 8, 22 );
lineTo( 5, 28 );
endFill();
}
bar.addChild( tri1 );
bar.addChild( tri2 );
addChild( bar );
range = new Rectangle( rail.x, rail.y, 0, rail.height - bar.height );
}
private function mouseDownHandler( evt:MouseEvent ):void
{
bar.startDrag( false, range );
addEventListener( Event.ENTER_FRAME, scrollingHandler );
}
private function mouseUpHandler( evt:MouseEvent ):void
{
bar.stopDrag();
removeEventListener( Event.ENTER_FRAME, scrollingHandler );
}
private function scrollingHandler( evt:Event ):void
{
per = bar.y / rail.height;
dispatchEvent( new ScrollEvent( ScrollEvent.SCROLL ) );
}
public function get percent():Number
{
return per;
}
}
class ScrollEvent extends Event
{
public static const SCROLL:String = "ScrollEvent.SCROLL";
public function ScrollEvent( type:String, bubbles:Boolean = false, cancelable:Boolean = false )
{
super( type, bubbles, cancelable );
}
public override function clone():Event
{
return new ScrollEvent( type, bubbles, cancelable );
}
public override function toString():String
{
return formatToString( "ScrollEvent", "type", "bubbles", "cancelable", "eventPhase" );
}
}
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFormat;
class SamplePage extends Sprite
{
private var shape:Shape;
private var tf:TextField;
private var tfm:TextFormat;
public function SamplePage( number:int ):void
{
shape = new Shape();
with( shape.graphics ) {
beginFill( 0xcccccc );
drawCircle( 0, 0, 100 );
endFill();
}
tfm = new TextFormat();
tfm.color = 0x00aeff;
tfm.size = 50;
tfm.font = "_sans";
tf = new TextField();
tf.defaultTextFormat = tfm;
tf.text = String( number );
tf.x -= tf.textWidth /2;
tf.y -= tf.textHeight /2;
addChild( shape );
addChild( tf );
}
}