スクロールバー (1)
//////////////////////////////////////////////////////////////////////////////
スクロールバー (1)
//////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/zeym
*/
////////////////////////////////////////////////////////////////////////////////
// スクロールバー (1)
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.display.InterpolationMethod;
[SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private var panel:Shape;
private var _mask:Shape;
private var scrollBar:ScrollBar;
public function Main() {
//Wonderfl.capture_delay(1);
init();
}
private function init():void {
panel = new Shape();
addChild(panel);
panel.x = 10;
panel.y = 10;
var matrix:Matrix = new Matrix();
matrix.createGradientBox(425, 1000, Math.PI/2, 0, 0);
panel.graphics.beginGradientFill(GradientType.LINEAR, [0x000000, 0xFF0000], [1, 1], [0, 255], matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0);
panel.graphics.drawRect(0, 0, 425, 1000);
panel.graphics.endFill();
_mask = new Shape();
//addChild(_mask);
_mask.x = 10;
_mask.y = 10;
_mask.graphics.beginFill(0x000000);
_mask.graphics.drawRect(0, 0, 425, 445);
_mask.graphics.endFill();
panel.mask = _mask;
//
scrollBar = new ScrollBar();
addChild(scrollBar);
scrollBar.x = 437;
scrollBar.y = 10;
scrollBar.init({height: 445});
scrollBar.setTarget(panel, _mask);
}
}
}
////////////////////////////////////////////////////////////////////////////////
// ScrollBarクラス
////////////////////////////////////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.geom.ColorTransform;
class ScrollBar extends Sprite {
public var id:uint;
private static var _width:uint = 16;
private var _height:uint;
private var track:Sprite;
private var thumb:Sprite;
private var base:Shape;
private var handle:Shape;
private static var bColor:uint = 0xEEEEEE;
private static var tColor:uint = 0x000000;
private static var hColor:uint = 0x999999;
private static var upColor:uint = 0x333333;
private static var overColor:uint = 0x444444;
private static var offColor:uint = 0xCCCCCC;
private static var upColorTrans:ColorTransform;
private static var overColorTrans:ColorTransform;
private static var offColorTrans:ColorTransform;
private static var minHeight:uint = 30;
private var maxHeight:uint;
private var thumbHeight:uint = minHeight;
private var target:DisplayObject;
private var rect:DisplayObject;
private var clickPos:Number;
private var basePos:int;
private var _enabled:Boolean = true;
public function ScrollBar() {
}
public function init(option:Object):void {
if (option.id != undefined) id = option.id;
if (option.height != undefined) _height = option.height;
}
public function setTarget(t:DisplayObject, r:DisplayObject):void {
target = t;
rect = r;
basePos = target.y;
thumbHeight = Math.max(minHeight, uint(rect.height/target.height*_height))
maxHeight = _height - thumbHeight;
draw();
}
private function draw():void {
upColorTrans = new ColorTransform();
upColorTrans.color = upColor;
overColorTrans = new ColorTransform();
overColorTrans.color = overColor;
offColorTrans = new ColorTransform();
offColorTrans.color = offColor;
track = new Sprite();
thumb = new Sprite();
base = new Shape();
handle = new Shape();
addChild(track);
addChild(thumb);
createTrack(_width, _height);
createThumb(_width, thumbHeight);
enabled = true;
//mouseChildren = false;
thumb.mouseChildren = false;
}
private function rollOver(evt:MouseEvent):void {
_over();
}
private function rollOut(evt:MouseEvent):void {
_up();
}
private function press(evt:MouseEvent):void {
_down();
thumb.addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_UP, releaseOutside, false, 0, true);
stage.addEventListener(Event.MOUSE_LEAVE, leave, false, 0, true);
clickPos = thumb.mouseY;
stage.addEventListener(MouseEvent.MOUSE_MOVE, drag, false, 0, true);
}
private function release(evt:MouseEvent):void {
_up();
thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);
stage.removeEventListener(Event.MOUSE_LEAVE, leave);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, drag);
}
private function releaseOutside(evt:MouseEvent):void {
_up();
thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);
stage.removeEventListener(Event.MOUSE_LEAVE, leave);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, drag);
}
private function leave(evt:MouseEvent):void {
_up();
thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);
stage.removeEventListener(Event.MOUSE_LEAVE, leave);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, drag);
}
private function drag(evt:MouseEvent):void {
var position:Number = stage.mouseY - clickPos;
if (position < 0) position = 0;
if (position > maxHeight) position = maxHeight;
thumb.y = position;
scrollTarget();
evt.updateAfterEvent();
}
private function click(evt:MouseEvent):void {
}
private function scrollTarget():void {
var percent:Number = thumb.y/(_height - thumbHeight);
target.y = basePos - uint((target.height - rect.height)*percent);
}
private function _up():void {
base.transform.colorTransform = upColorTrans;
}
private function _over():void {
base.transform.colorTransform = overColorTrans;
}
private function _down():void {
base.transform.colorTransform = overColorTrans;
}
private function _off():void {
base.transform.colorTransform = offColorTrans;
}
public function get enabled():Boolean {
return _enabled;
}
public function set enabled(param:Boolean):void {
_enabled = param;
thumb.buttonMode = _enabled;
thumb.mouseEnabled = _enabled;
thumb.useHandCursor = _enabled;
track.mouseEnabled = _enabled;
if (_enabled) {
_up();
thumb.addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
thumb.addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
thumb.addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);
track.addEventListener(MouseEvent.CLICK, click, false, 0, true);
} else {
_off();
thumb.removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
thumb.removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
thumb.removeEventListener(MouseEvent.MOUSE_DOWN, press);
track.removeEventListener(MouseEvent.CLICK, click);
}
}
private function createThumb(w:uint, h:uint):void {
thumb.addChild(base);
thumb.addChild(handle);
handle.y = uint(h/2);
base.graphics.beginFill(tColor);
base.graphics.drawRect(0, 0, w, h);
base.graphics.endFill();
handle.graphics.beginFill(hColor);
handle.graphics.drawRect(w/4, -4, w/2, 1);
handle.graphics.endFill();
handle.graphics.beginFill(hColor);
handle.graphics.drawRect(w/4, -2, w/2, 1);
handle.graphics.endFill();
handle.graphics.beginFill(hColor);
handle.graphics.drawRect(w/4, 0, w/2, 1);
handle.graphics.endFill();
handle.graphics.beginFill(hColor);
handle.graphics.drawRect(w/4, 2, w/2, 1);
handle.graphics.endFill();
}
private function createTrack(w:uint, h:uint):void {
track.graphics.beginFill(bColor);
track.graphics.drawRect(0, 0, w, h);
track.graphics.endFill();
}
}