forked from: wonderfl用Buttonクラス テスト

by yaha
♥0 | Line 126 | Modified 2010-03-01 16:36:44 | MIT License
play

ActionScript3 source code

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

// forked from Sharakusai's wonderfl用Buttonクラス テスト
package {
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            // write as3 code here..
            
            var btn:Button = new Button("Test");
            this.addChild(btn);
            btn.x = 200;
            btn.y = 200;
            
        }
    }

}

//************************************************************************
//
//     wonderfl 用 Button Class
//
//     Original Souce Code
//     http://i-libro.net/wpmu/blog/archives/264
//
//    マウスオーバー時にハンドカーソルに変更されるように修正
//************************************************************************

    import flash.display.GradientType;
    import flash.display.Graphics;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
    import flash.events.MouseEvent;

	
internal class Button extends Sprite
{
    //コンテント(内容物)
    private var textcontent:TextField = null;	//文字列用
    private var imgcontent:Shape = null;	//イメージ用
    private var imgrollover:Shape = null;	//イメージ用(ロールオーバー)
		
    //文字揃え
    static public const ALIGN_LEFT:int = 0;
    static public const ALIGN_CENTER:int = 1;
    static public const ALIGN_RIGHT:int = 2;
		
    //フォーマット
    private var tff:TextFormat = new TextFormat();		
    //背景色
    private var backcolor:int = 0xDDDDDD;
		
    //コンストラクタ
    //ボタンテキストを後から設定する場合は、contentにダミー文字を指定する必要がある。
    //ボタンテキストが不要な場合は""(空白文字列)を指定
    //img(ボタンイメージ)とimgrollover(ロールオーバー時のイメージ)は省略可能
	
     public function Button( content:String, 
                             img:Shape = null, 
                             imgrollover:Shape = null ):void {
         if (content != "") {
	    this.textcontent = new TextField();
	    this.textcontent.text = content;
		
            //ボタンの書式設定
	    this.textcontent.width = 80;
	    this.textcontent.height = 20;
	    this.textcontent.border = true;
	    this.textcontent.borderColor = 0x0;
	    this.textcontent.background = true;
	    this.textcontent.backgroundColor = 0xDDDDDD;
	    this.textcontent.selectable = false;
	    this.tff.align = TextFormatAlign.CENTER;
	    this.tff.font = "_sans";
	    this.tff.bold = true;
	    this.tff.size = 12;
	    this.textcontent.setTextFormat(tff);
	    this.addChild(this.textcontent);
            this.textcontent.mouseEnabled = false;
	}
		
        //ボタンイメージを設定
	if (img != null) {
	    this.imgcontent = img;
	    this.imgcontent.visible = true;
	    this.addChild( this.imgcontent );
	}
	if (imgrollover != null) {
	    this.imgrollover = imgrollover;
	    this.imgrollover.visible = false;
	    this.addChild( this.imgrollover );
	}
	
        //ボタンのイベント設定
	this.addEventListener( MouseEvent.MOUSE_OVER, this.mouseOver );
	this.addEventListener( MouseEvent.MOUSE_OUT, this.mouseLeave );
        this.buttonMode = true;
        this.useHandCursor = true;
    }
		
    //行揃え設定プロパティ(テキストコンテンツがある場合のみ有効)
    public function set alignment( setValue:int ):void {
        if (this.textcontent == null) return;
        switch( setValue ){
        case ALIGN_LEFT:
			this.tff.align = TextFormatAlign.LEFT;
	        this.textcontent.setTextFormat(tff);
	        break;
	    case ALIGN_CENTER:
	        this.tff.align = TextFormatAlign.CENTER;
	        this.textcontent.setTextFormat(tff);
	        break;
	    case ALIGN_RIGHT:
	        this.tff.align = TextFormatAlign.RIGHT;
	        this.textcontent.setTextFormat(tff);
	        break;
        }
    }
		
    //マウスオーバーとマウスリーブで背景色変更
    private function mouseOver( e:MouseEvent ):void {
        if (this.textcontent != null) {
            this.backcolor = this.textcontent.backgroundColor;
			this.textcontent.backgroundColor = 0x8888FF;
			this.tff.color = 0xFFFFFF;
            this.textcontent.setTextFormat(this.tff);
            }
        if (this.imgrollover != null) this.imgrollover.visible = true;
        if (this.imgcontent != null) this.imgcontent.visible = false;
    }
    private function mouseLeave( e:MouseEvent ):void {
        if (this.textcontent != null ) {
            this.textcontent.backgroundColor =this.backcolor;	
			this.tff.color = 0;
            this.textcontent.setTextFormat(this.tff);
            }
	if (this.imgcontent != null) this.imgcontent.visible = true;
	if (this.imgrollover != null) this.imgrollover.visible = false;
    }
	
    //テキストを設定する
    public function set text( content:String ):void {
        if (this.textcontent != null) {
			this.textcontent.text = content;
			this.textcontent.setTextFormat( this.tff);			
	}
    }
    public function get text():String {
        if (this.textcontent != null) {
			return this.textcontent.text;
            }
	return "";
    }
    //ボーダーを設定(テキストボタン時のみ有効)
    public function set border( setValue:Boolean ):void {
        if (this.textcontent != null){
            this.textcontent.border = setValue;
            }
    }
    //背景色を設定(テキストボタン時のみ有効)
    public function set backgroundColor( setValue:int ):void {
        if (this.textcontent != null){
			this.textcontent.backgroundColor = setValue;
			this.backcolor = setValue;
        }
    }
}

Forked