forked from: スクロールバー (1)

by tepe forked from スクロールバー (1) (diff: 91)
スクロールバー (1)
♥0 | Line 234 | Modified 2013-12-13 11:05:27 | MIT License
play

ActionScript3 source code

/**
 * Copyright tepe ( http://wonderfl.net/user/tepe )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/lEmi
 */

// forked from ProjectNya's スクロールバー (1)
////////////////////////////////////////////////////////////////////////////////
//  スクロールバー (1)
////////////////////////////////////////////////////////////////////////////////

package {
    import flash.text.TextField;

    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:vScrollBar;

        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, 1425, 50000);
            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;
            //
            
            var t:TextField = new TextField();
            //scrollBar.setTF(t);
            t.border = true;
            t.text = "aa";
            t.multiline=true;
            t.type ="input";
            addChild(t);
            scrollBar = new vScrollBar();
            scrollBar.setTF(t);
        }

    }

}


////////////////////////////////////////////////////////////////////////////////
//  ScrollBarクラス
////////////////////////////////////////////////////////////////////////////////

import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
class vScrollBar 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 = 0x333333;
    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 = 20;

    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;
    private var txt:TextField;

    public function vScrollBar() {
        init();
    }

    
    
    public function setTF(tf:TextField):void{
        txt = tf;
        txt.addEventListener(KeyboardEvent.KEY_DOWN,onKey);
        tf.parent.addChild(this);
        onKey();
        
    }
    private function onKey(e:KeyboardEvent=null):void{
        onChange();
        var pos:Number =  maxHeight*(txt.scrollV / txt.maxScrollV);
        if (txt.scrollV == 1) pos = 0;
        if (txt.maxScrollV == txt.scrollV) pos = maxHeight;
        thumb.y = pos;
        e.updateAfterEvent();
    }

    public function onChange(e:Event=null):void{
        this.x = txt.x+txt.width;// -_width;
        this.y = txt.y;
        _height = txt.height;
        var n:Number;
        if(txt.height<txt.textHeight){ 
            n = txt.height/txt.textHeight;
            txt.parent.addChild(this);
        }
        else{ 
            n = 1;
            txt.parent.removeChild(this);
        }
        thumbHeight = Math.max(minHeight,_height*n);
        maxHeight = _height - thumbHeight;
        if (thumb.y < 0) thumb.y = 0;
        if (thumb.y > maxHeight) thumb.y = maxHeight;
        createTrack(_width,_height);
        createThumb(_width,thumbHeight);  
    }
    private function init():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);
        enabled = true;
        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;
        txt.scrollV = Math.round(txt.maxScrollV*(position/maxHeight));
        evt.updateAfterEvent();
    }
    private function click(evt:MouseEvent):void {}
    
    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.clear();
        base.graphics.beginFill(tColor);
        base.graphics.drawRect(0, 0, w, h+1);
        base.graphics.endFill();
        
        handle.graphics.clear();
        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.clear();
        track.graphics.beginFill(bColor,0.5);
        track.graphics.drawRect(0, 0, w, h+1);
        track.graphics.endFill();
    }

}

Forked