scrollbar
Tweener使って作ってみました。
ブラウザも一緒に動いてしまう。。
package {
import flash.display.*;
import flash.events.*;
import flash.text.TextField;
import net.hires.debug.Stats;
[SWF(backgoundColor = "#FFFFFF", frameRate = "60")]
public class FlashTest extends MovieClip {
private var _scroll:ScrollBar;
public function FlashTest() {
if (stage) _init();
else addEventListener(Event.ADDED_TO_STAGE, _init);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
}
private function _init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, _init);
var _str:String = "MovieClip クラスは、Sprite、DisplayObjectContainer、InteractiveObject、DisplayObject および EventDispatcher クラスを継承します。MovieClip オブジェクトには、Sprite オブジェクトとは違ってタイムラインがあります。MovieClip クラスのメソッドは、ムービークリップをターゲットとするアクションと同じ機能を提供します。Flash オーサリングツールのアクションパネルのアクションツールボックスには同等のアクションがない追加メソッドもあります。Flash オーサリングツールのステージに配置された子インスタンスは、親インスタンスのコンストラクタ内からコードでアクセスでません。コード実行の該当時点では作成されていないためです。子にアクセスするには、親はコードを使用して子インスタンスを作成するか、子を待機するコールバック関数が Event.ADDED_TO_STAGE イベントを送出するまでアクセスを遅延させる必要があります。モーショントゥイーンが含まれている MovieClip オブジェクトの次のいずれかのプロパティを変更した場合、MovieClip オブジェクト、alpha、blendMode、filters、height、opaqueBackground、rotation、scaleX、scaleY、scale9Grid、scrollRect、transform、visible、width、x または y の再生ヘッドが停止されます。ただし、その MovieClip オブジェクトの子 MovieClip オブジェクトの再生ヘッドは停止しません。 ";
var _txt:TextField = new TextField();
_txt.mouseEnabled = false;
_txt.multiline = _txt.wordWrap = true;
_txt.autoSize = "left";
_txt.width = 200;
_txt.text = _str;
var _bd:BitmapData = new BitmapData(_txt.width, _txt.height, false);
_bd.draw(_txt);
var _bm:Bitmap = new Bitmap(_bd);
_scroll = new ScrollBar(_bm, 300, 15, 0x00CCCC, 0xEEEEEE, stage, 1, 3);
addChild(_scroll);
stage.addEventListener(Event.RESIZE, _resize, false, 0, true);
_resize();
addChild(new Stats());
}
private function _resize(e:Event = null):void
{
var _sw:Number = stage.stageWidth;
var _sh:Number = stage.stageHeight;
_scroll.x = Math.floor(_sw / 2 - _scroll.width / 2);
_scroll.y = Math.floor(_sh / 2 - _scroll._visibleHeight / 2);
}
}
}
import flash.display.*;
import flash.events.*;
import caurina.transitions.Tweener;
class ScrollBar extends MovieClip
{
private var _stage:Stage;
private var _stageForEvent:Stage;
private var _displayObject:DisplayObject;
private var _DOW:uint;
private var _DOH:uint;
public var _visibleHeight:uint;
private var _mask:Shape;
private var _bar:MovieClip;
private var _barBack:MovieClip;
private var _barW:uint;
private var _barH:uint;
private var _barColor:Number;
private var _barBackColor:Number;
private var _tweenSpeed:Number;
private var _dragSpeed:uint;
private var _wheelSpeed:uint;
private var _rate:Number;
private var _dragComplete:Boolean = true;
private var _dbbm:Number;
private var _scrollable:Boolean = true;
public function ScrollBar(
displayObject:DisplayObject,
visibleHeight:uint,
barWidth:uint = 20,
barColor:Number = 0xCCCCCC,
barBackColor:Number = 0xEEEEEE,
wheelStage:Stage = null,
tweenSpeed:Number = 1,
dragSpeed:uint = 10,
wheelSpeed:uint = 3)
{
trace("a");
if (wheelStage != null) {
_stage = wheelStage;
_stageForEvent = wheelStage;
}
trace("b");
_displayObject = displayObject;
_DOW = _displayObject.width;
_DOH = _displayObject.height;
_visibleHeight = visibleHeight;
_barW = barWidth;
_barH = Math.floor(_visibleHeight * _visibleHeight / _DOH);
if (_barH % 2 != 0) {
_barH += 1;
}
_barColor = barColor;
_barBackColor = barBackColor;
_rate = (_DOH - _visibleHeight) / (_visibleHeight - _barH);
_tweenSpeed = tweenSpeed;
_dragSpeed = dragSpeed;
_wheelSpeed = wheelSpeed;
if (stage) _init();
else addEventListener(Event.ADDED_TO_STAGE, _init);
}
private function _init(e:Event = null):void
{
if (_stage == undefined) {
_stage = stage;
_stageForEvent = this;
}
addChild(_displayObject);
_mask = new Shape();
_mask.graphics.beginFill(0x000000);
_mask.graphics.drawRect(0, 0, _DOW, _visibleHeight);
_mask.graphics.endFill();
addChild(_mask);
_displayObject.mask = _mask;
_barBack = new MovieClip();
_barBack.graphics.beginFill(_barBackColor);
_barBack.graphics.drawRect(0, 0, _barW, _visibleHeight);
_barBack.graphics.endFill();
_barBack.x = _DOW;
_barBack.buttonMode = true;
addChild(_barBack);
_bar = new MovieClip();
_bar.graphics.beginFill(_barColor);
_bar.graphics.drawRect(0, 0, _barW, _barH);
_bar.graphics.endFill();
_bar.x = _DOW;
_bar.y = 0;
_bar.buttonMode = true;
addChild(_bar);
_barBack.addEventListener(MouseEvent.CLICK, _barBackClick, false, 0, true);
_bar.addEventListener(MouseEvent.MOUSE_DOWN, _startBarDrag, false, 0, true);
_stageForEvent.addEventListener(MouseEvent.MOUSE_WHEEL, _mouseWheel, false, 0, true);
addEventListener(Event.REMOVED_FROM_STAGE, _removed, false, 0, true);
}
private function _barBackClick(e:MouseEvent):void
{
if (_dragComplete == false) {
removeEventListener(Event.ENTER_FRAME, _continueDOMove);
_dragComplete = true;
}
Tweener.removeTweens(_displayObject, _bar);
var _goalY:uint;
var _clickY:uint = e.currentTarget.mouseY;
if (_clickY < _barH / 2) {
_goalY = 0;
}else if (_clickY > (_visibleHeight - _barH / 2)) {
_goalY = _visibleHeight - _barH;
}else {
_goalY = _clickY - _barH / 2;
}
Tweener.addTween(_bar, { y:_goalY, time:_tweenSpeed, transition:"easeOutExpo" } );
Tweener.addTween(_displayObject, { y: -_goalY * _rate, time:_tweenSpeed, transition:"easeOutExpo" } );
}
private function _startBarDrag(e:MouseEvent):void
{
Tweener.removeTweens(_displayObject, _bar);
if (_dragComplete == false) {
removeEventListener(Event.ENTER_FRAME, _continueDOMove);
}
_dbbm = _barBack.mouseY - _bar.y;
_bar.removeEventListener(MouseEvent.MOUSE_DOWN, _startBarDrag);
addEventListener(Event.ENTER_FRAME, _barDrag, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_MOVE, _dragAndMouseMove, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_UP, _stopBarDrag, false, 0, true);
}
private function _dragAndMouseMove(e:MouseEvent):void
{
_dragComplete = false;
}
private function _barDrag(e:Event):void
{
_bar.y = _barBack.mouseY - _dbbm;
if (_bar.y < 0) {
_bar.y = 0;
}else if (_bar.y > _visibleHeight - _barH) {
_bar.y = _visibleHeight - _barH;
}
_DOMoveByDrag();
}
private function _DOMoveByDrag():void
{
if (_dragComplete == false) {
var _DOGoadY:Number = -_bar.y * _rate;
_displayObject.y += (_DOGoadY - _displayObject.y) / _dragSpeed;
if (Math.abs(_DOGoadY - _displayObject.y) < 0.5) {
_displayObject.y = _DOGoadY;
_dragComplete = true;
}
}
}
private function _stopBarDrag(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, _dragAndMouseMove);
if (_dragComplete == false) {
addEventListener(Event.ENTER_FRAME, _continueDOMove, false, 0, true);
}
stage.removeEventListener(MouseEvent.MOUSE_UP, _stopBarDrag);
removeEventListener(Event.ENTER_FRAME, _barDrag);
_bar.addEventListener(MouseEvent.MOUSE_DOWN, _startBarDrag);
}
private function _continueDOMove(e:Event):void
{
_DOMoveByDrag();
if (_dragComplete == true) {
removeEventListener(Event.ENTER_FRAME, _continueDOMove);
}
}
private function _mouseWheel(e:MouseEvent):void
{
Tweener.removeTweens(_displayObject, _bar);
if (_dragComplete == true) {
_dragComplete = false;
addEventListener(Event.ENTER_FRAME, _continueDOMove, false, 0, true);
}
_bar.y -= e.delta * _wheelSpeed;
if (_bar.y < 0) {
_bar.y = 0;
}else if (_bar.y > _visibleHeight - _barH) {
_bar.y = _visibleHeight - _barH;
}
}
public function resizeHeight(_height:uint):void
{
Tweener.removeTweens(_displayObject, _bar);
_stageForEvent.removeEventListener(MouseEvent.MOUSE_WHEEL, _mouseWheel);
_bar.mouseEnabled = false;
_barBack.mouseEnabled = false;
_scrollable = true;
var _barPosition:Number = _bar.y / (_visibleHeight - _bar.height);
if (_height > _DOH) {
_height = _DOH;
_scrollable = false;
_bar.alpha = 0;
_barBack.alpha = 0;
_barPosition = 1;
}
if (_dragComplete == false) {
removeEventListener(Event.ENTER_FRAME, _continueDOMove);
_dragComplete = true;
}
_visibleHeight = _height;
_barH = Math.floor(_visibleHeight * _visibleHeight / _DOH);
if (_barH % 2 != 0) {
_barH += 1;
}
_mask.graphics.clear();
_mask.graphics.beginFill(0x000000);
_mask.graphics.drawRect(0, 0, _DOW, _visibleHeight);
_mask.graphics.endFill();
_barBack.graphics.clear();
_barBack.graphics.beginFill(_barBackColor);
_barBack.graphics.drawRect(0, 0, _barW, _visibleHeight);
_barBack.graphics.endFill();
_bar.graphics.clear();
_bar.graphics.beginFill(_barColor);
_bar.graphics.drawRect(0, 0, _barW, _barH);
_bar.graphics.endFill();
_rate = (_DOH - _visibleHeight) / (_visibleHeight - _barH);
if (!_rate) {
_rate = 1;
}
_bar.y = (_visibleHeight - _bar.height) * _barPosition;
if (_bar.y < 0) {
_bar.y = 0;
}
_displayObject.y = -_bar.y * _rate;
if (_scrollable == true) {
_stageForEvent.addEventListener(MouseEvent.MOUSE_WHEEL, _mouseWheel, false, 0, true);
_bar.alpha = 1;
_bar.mouseEnabled = true;
_barBack.alpha = 1;
_barBack.mouseEnabled = true;
}
}
public function _removed(e:Event):void
{
Tweener.removeTweens(_displayObject, _bar);
if (_dragComplete == false) {
removeEventListener(Event.ENTER_FRAME, _continueDOMove);
}
if (_scrollable == true) {
_stageForEvent.removeEventListener(MouseEvent.MOUSE_WHEEL, _mouseWheel);
}
_barBack.removeEventListener(MouseEvent.CLICK, _barBackClick);
_bar.removeEventListener(MouseEvent.MOUSE_DOWN, _startBarDrag);
removeChild(_displayObject);
_displayObject = null;
removeChild(_bar);
_bar = null;
removeChild(_barBack);
_barBack = null;
removeChild(_mask);
_mask = null;
}
}