package
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
public class Scroller extends Sprite
{
private const PADDING:uint = 20;
private var _scrollItem:DisplayObject;
private var _masker:Sprite;
private var _scrollbarContainer:Sprite;
private var _scrollbar:Sprite;
private var _path:Sprite;
/**
* Constructor.
*
* @param $displayObject The object to be scrolled.
* @param $height [옵션] 길이.
* @param $color [옵션] 스크롤바 칼라.
*/
public function Scroller( $displayObject:DisplayObject, $height:uint = 300, $color = 0xffba00 )
{
_scrollItem = $displayObject;
if ( _scrollItem.parent )
{
x = _scrollItem.x;
y = _scrollItem.y;
addEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
_scrollItem.parent.addChild( this );
}
else
{
addEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
}
_scrollItem.x = 0;
_scrollItem.y = 0;
addChild( _scrollItem );
createMask( $height );
createScrollbar( $color );
trace( “스크롤바 생성및 초기화성공” );
}
/**
* Places an event listener on the stage, so we make sure the
* onStopDrag() method will be called when you release the mouse
* button anywhere on the stage.
*/
private function onAddedToStage( $e:Event = null ):void
{
stage.addEventListener( MouseEvent.MOUSE_UP, onStopDrag );
}
/**
* 드래가그 시작될때
*/
private function onStartDrag( $e:MouseEvent ):void
{
var rect:Rectangle = new Rectangle( _path.x – ( _scrollbar.width / 2),
_path.y – ( _scrollbar.height / 2 ),
0,
_path.height );
_scrollbar.startDrag( false, rect );
addEventListener( Event.ENTER_FRAME, calculateScrollPosition );
}
/**
* 드래그가 멈출때
*/
private function onStopDrag( $e:MouseEvent ):void
{
removeEventListener( Event.ENTER_FRAME, calculateScrollPosition );
_scrollbar.stopDrag();
}
/**
* 스크롤의 위치를 다시 계산다.
*/
private function calculateScrollPosition( $e:Event ):void
{
var scrollbarMax:uint = _masker.height – _scrollbar.height;
var currentPos:uint = _scrollbar.y;
var percent:uint = currentPos / scrollbarMax * 100;
var scrollItemMax:uint = ( _scrollItem.height – _masker.height );
_scrollItem.y = Math.round( scrollItemMax * percent / 100 ) * -1;
}
/**
* 마스크를 건다.
*/
private function createMask( $height:uint ):void
{
_masker = new Sprite();
_masker.graphics.beginFill( 0xFF0000 );
_masker.graphics.drawRect( 0, 0, _scrollItem.width, $height );
_masker.graphics.endFill();
addChild( _masker );
_scrollItem.mask = _masker;
}
/**
* 스크롤바를 만든다.
*/
private function createScrollbar( $color:uint ):void
{
_scrollbar = new Sprite();
_scrollbar.graphics.beginFill( $color );
_scrollbar.graphics.drawRoundRect( 0, 0, 7, 40, 10, 10 );
_scrollbar.graphics.endFill();
_scrollbar.addEventListener( MouseEvent.MOUSE_DOWN, onStartDrag );
_scrollbar.addEventListener( MouseEvent.MOUSE_UP, onStopDrag );
_scrollbar.buttonMode = true;
_path = new Sprite();
_path.graphics.beginFill( $color, 0.7 );
_path.graphics.drawRect( 0, 0, 1, _masker.height – _scrollbar.height );
_path.graphics.endFill();
_path.x = _scrollbar.width / 2;
_path.y = _scrollbar.height / 2;
_scrollbarContainer = new Sprite();
_scrollbarContainer.x = _masker.width + PADDING;
_scrollbarContainer.addChild( _path );
_scrollbarContainer.addChild( _scrollbar );
addChild( _scrollbarContainer );
}
}
}