flash on 2013-3-30

by tepe
♥0 | Line 202 | Modified 2013-03-30 10:24:09 | 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/4g0s
 */

package {
    import flash.display.Sprite;
    
    public class FlashTest extends Sprite {
        public function FlashTest() {
            // write as3 code here..
            var btn:Btn = new Btn(50,50,stage);
            btn.name = "8";
            btn.x = 55;        btn.y = 50;
            btn.text = "Bs";         
            
            var btn2:Btn = new Btn(50,50,stage);
            btn2.setBColor(0xffffff);
            btn2.setBColor(0xffffff);
            btn2.setBColor(0xffffff);
            
            
            var btn3:Btn = new Btn(50,50,stage);
            btn3.x = 100;
        }
    }
}
import flash.display.*;
import flash.text.*;
import flash.filters.GlowFilter;
import flash.events.MouseEvent;
import org.libspark.betweenas3.BetweenAS3;
import net.wonderfl.utils.FontLoader;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.easing.*;
import org.libspark.betweenas3.tweens.ITween;
class Btn extends Sprite {
    public var id:uint;
    private var shade:Shape;
    private var bottom:Shape;
    private var light:Shape;
    private var base:Shape;
    private var txt:TextField;
    private var label:String = "";
    private var _width:uint = 60;
    private var _height:uint = 20;
    private static var corner:uint = 3;
    private var type:uint = 1;
    private static var bColor:uint = 0xffffff//0x000000;//ボディカラー
    private static var sColor:uint = 0xaaaaff;//ボタンカラー2
    private static var upColor:uint = 0xaaaaaa;//0x333333;//テキストカラー1
    private static var overColor:uint = 0x333333;//0xccccff;//テキストカラー2
    private static var offColor:uint = 0xdddddd;
    private var gColor:uint = 0x00ff00;//グローカラー
    private var glow:GlowFilter;
    private var shadeGlow:GlowFilter;
    private var _clicked:Boolean = false;
    private var _enabled:Boolean = true;
    public var lightOn:Boolean = false;
    private var onSelected:Boolean = false;
    private var isEnterKey:Boolean = false;
    public function Btn(width:Number=20,height:Number=20,parent:DisplayObjectContainer=null){
        if(parent!=null)parent.addChild(this);
        _width = width;
        _height = height;
        init();
    }
    
    public function setBColor(c:uint):void{
        bColor = c;
        draw();
    }
    
    private function init():void{
        shadeGlow = new GlowFilter(sColor, 0.3, 4, 4, 2, 3, false, true);
        shade = new Shape();
        bottom = new Shape();
        light = new Shape();
        base = new Shape();
        txt = new TextField();

        addChild(shade);
        addChild(bottom);
        addChild(light);
        addChild(base);
        addChild(txt);
        draw();
    }


    private function draw():void {
        glow = new GlowFilter(gColor, 0.6, 5, 5, 2, 3, false, true);

        createBase(shade, _width, _height, corner, sColor);
        shade.filters = [shadeGlow];
        createBase(bottom, _width, _height, corner, sColor, 0.3);
        createBase(light, _width, _height, corner, gColor);
        light.filters = [glow];
        
        createBase(base, _width, _height, corner, bColor);
        //txt.x = -_width*0.5;
        txt.y = _height*0.5;
        txt.width = _width;
        txt.height = _height - 1;
        txt.type = TextFieldType.DYNAMIC;
        txt.selectable = false;
        //txt.embedFonts = true;
        //txt.antiAliasType = AntiAliasType.ADVANCED;
        var tf:TextFormat = new TextFormat();
        //tf.font = fontType;
        //tf.size = 12;
        tf.align = TextFormatAlign.CENTER;
        txt.defaultTextFormat = tf;
        txt.text = label;
        enabled = true;
        mouseChildren = false;
    }
    
    public function setColor(c:uint):void{
        gColor = c;
        glow = new GlowFilter(gColor, 0.7, 8, 8, 2, 3, false, true);
        light.filters = [glow];
    }
    
    public function get text():String{
        return txt.text;
    }
    public function set text(str:String):void{
        txt.text = str;
    }
    
    public function setLight(s:Boolean):void{
        lightOn = s;
        light.visible = s;
        if(s)txt.textColor = overColor;
        else txt.textColor = upColor;
    }
    
    public function get select():Boolean{
        return lightOn;
    }
    //イベント
    private function rollOver(evt:MouseEvent):void {
        _over();
    }
    private function rollOut(evt:MouseEvent):void {
        _up();
    }
    private function press(evt:MouseEvent):void {
        _down();
    }
    private function release(evt:MouseEvent):void {
        _up();
    }
    private function click(evt:MouseEvent):void {
    }
    
    public function _up():void {
        txt.y = 0;
        
        if(this.lightOn ==false)txt.textColor = upColor;//テキストカラー
        base.y = -1;
        if(this.lightOn ==false)light.visible = false;//ライト
        light.y = -1;
    }
    private function _over():void {
        txt.y = 0;
        txt.textColor = overColor;
        base.y = -1;
        light.visible = true;
        light.y = -1;
    }
    public function _down():void {
        txt.y = 1;
        txt.textColor = overColor;
        base.y = 0;
        light.visible = true;
        light.y = 0;
    }
    private function _off():void {
        txt.y = 0;
        if(this.lightOn ==false)txt.textColor = offColor;
        base.y = 0;
        if(this.lightOn ==false)light.visible = false;
        light.y = 0;
    }
    public function get clicked():Boolean {
        return _clicked;
    }
    public function set clicked(param:Boolean):void {
        _clicked = param;
        enabled = !_clicked;
        if (_clicked) {
            _down();
        } else {
            _up();
        }
    }
    
    public function get enabled():Boolean {
        return _enabled;
    }
    
    public function set enabled(param:Boolean):void {
        _enabled = param;
        buttonMode = _enabled;
        mouseEnabled = _enabled;
        useHandCursor = _enabled;
        if (_enabled) {
            _up();
            addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
            addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
            addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);
            addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
            addEventListener(MouseEvent.CLICK, click, false, 0, true);
        } else {
            _off();
            removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
            removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
            removeEventListener(MouseEvent.MOUSE_DOWN, press);
            removeEventListener(MouseEvent.MOUSE_UP, release);
            removeEventListener(MouseEvent.CLICK, click);
        }
    }
    
    private function createBase(target:Shape, w:uint, h:uint, c:uint, color:uint, alpha:Number = 1):void {
        with(target.graphics){
            clear();
            beginFill(color, alpha);
            drawRoundRect(0,0, w, h, c*2);
            endFill();
        }
    }
}