In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

ダイヤルインターフェースのテスト

使い方:

Wheel.value → 角度に対応した値。デフォルトでは0~100。Wheel.maxで変更可能。
Wheel.turn → 回転数。1周毎に1pt変化
Wheel.max → valueの最大値。デフォルトでは100。
Wheel.size → ホイールの表示サイズ。
Get Adobe Flash player
by tepe 19 Mar 2011

    Tags

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

package {
    import flash.text.TextField;
    import flash.display.Sprite;
    import flash.events.*;
    public class FlashTest extends Sprite {
        public var text1:TextField;
        public var wheel:Wheel = new Wheel();
        
        public function FlashTest() {
            // write as3 code here..
            
            var s:Sprite = new Sprite();
            s.graphics.beginFill(0x00ff00);
            s.graphics.drawRect(0,0,300,300);
            s.graphics.endFill();
            addChild(s);
            s.addChild(wheel);
            
            text1 = new TextField();
            s.addChild(text1);
            text1.x = 240;
            text1.y = 150;
            text1.text = "0";
            

            
            
            //ダイヤル設定
            wheel.x = 220;    wheel.y = 220;// 表示位置
            //wheel.scaleX = 2;
            //addChild(wheel);
            wheel.max = 100;
            //wheel.enabled = false;
            wheel.addEventListener(CompoEvent.CHANGE, change);
            //wheel.addEventListener(Event.ENTER_FRAME,change);
        }
        private function change(evt:CompoEvent):void {
            text1.text = String(wheel.turn);
            text1.appendText(" "+String(wheel.value));            
            
        }
    }
}


import flash.display.Sprite;
import flash.display.Shape;
import flash.filters.DropShadowFilter;
import flash.geom.ColorTransform;
import flash.events.Event;
import flash.events.MouseEvent;


//ホイールインターフェース
class Wheel extends Sprite {
    private var base:Sprite;
    private var thumb:Sprite;
    private var point:Shape;
    private var hit:Shape;
    private var zeroPos:Shape;
    //カラー設定
    public var bColor:uint = 0xFFFFFF;//ホイールの色
    private var cColor:uint = 0x999999;//つまみの色
    private var sColor:uint = 0x000000;//影の色
    private var pColor:uint = 0x666666;//つまみの色(ロールオーバー時)
    private var offColor:uint = 0xCCCCCC;//つまみの色(操作不可)
    private var cColorTrans:ColorTransform;
    private var pColorTrans:ColorTransform;
    private var offColorTrans:ColorTransform;
    //プロパティ
    
    private var zero:Number = 0;
    private var angle:Number = 0;//つまみの角度
    private var shade:DropShadowFilter;//影
    private var initValue:Number = 0;//値
    public var value:Number = 0;
    public var turn:int = 0;//回転数
    private var _enabled:Boolean = true;
    public var size:uint = 30;//描画サイズ
    public var max:Number = 100;//最大値
    public var dec:Boolean = false;//小数点以下を求める場合はtrue

    //コンストラクタ
    public function Wheel() {
        shade = new DropShadowFilter(1, 90, sColor, 0.4, 4, 4, 2, 3, false, false);
        cColorTrans = new ColorTransform();
        cColorTrans.color = cColor;
        pColorTrans = new ColorTransform();
        pColorTrans.color = pColor;
        offColorTrans = new ColorTransform();
        offColorTrans.color = offColor;
        
        base = new Sprite();
        thumb = new Sprite();
        point = new Shape();
        hit = new Shape();
        
        addChild(base);
        base.addChild(thumb);
        thumb.addChild(point);//ホイールのつまみ部分
        thumb.addChild(hit);
        addChild(thumb);

        draw();
    }

    //描画
    private function draw():void {
        //位置関係
        base.x = thumb.x = 0;
        base.y = thumb.y = 0;
        createDonut(base, size+size/3, size-size/3);//ホイール描画
        base.filters = [shade];
        point.x = size;
        createCircle(point, size/6, bColor, 1);//つまみの描画
        hit.x = size;
        createCircle(hit, size/3, bColor, 0);//操作エリア
        reset();
        _up();
        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(MouseEvent.MOUSE_MOVE, change, false, 0, true);
        stage.addEventListener(Event.MOUSE_LEAVE, leave, false, 0, true);
    }
    
    //イベント:マウスアップ
    private function release(evt:MouseEvent):void {
        _up();
        checkValue();
        var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);
        dispatchEvent(e);
        thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
        stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, change);
        stage.removeEventListener(Event.MOUSE_LEAVE, leave);
    }
    //エリア外でボタンリリース
    private function releaseOutside(evt:MouseEvent):void {
        _up();
        checkValue();
        var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);
        dispatchEvent(e);
        thumb.removeEventListener(MouseEvent.MOUSE_UP, release);
        stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, change);
        stage.removeEventListener(Event.MOUSE_LEAVE, leave);
    }
    
    
    private function leave(evt:Event):void {
        _up();
        checkValue();
        var e:CompoEvent = new CompoEvent(CompoEvent.SELECT, value);
        dispatchEvent(e);
        thumb.removeEventListener(MouseEvent.MOUSE_UP, release);//リリースイベント解除
        stage.removeEventListener(MouseEvent.MOUSE_UP, releaseOutside);//エリア外リリースイベント解除
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, change);//カーソル移動イベント解除
        stage.removeEventListener(Event.MOUSE_LEAVE, leave);//エリア外移動イベント解除
    }
    
    
    private function _up():void {
        point.transform.colorTransform = cColorTrans;
    }
    private function _over():void {
        point.transform.colorTransform = pColorTrans;
    }
    private function _down():void {
        point.transform.colorTransform = pColorTrans;
    }
    private function _off():void {
        point.transform.colorTransform = offColorTrans;
    }
    
    //ダイヤル操作
    private function change(evt:MouseEvent):void {
        _down();
        thumb.rotation = Math.atan2(base.mouseY, base.mouseX)/Math.PI*180;
        evt.updateAfterEvent();//画面更新
        checkValue();
        var e:CompoEvent = new CompoEvent(CompoEvent.CHANGE, value);
        dispatchEvent(e);//イベント発生
    }
    
    //ダイヤルの角度から値を決定
    private function checkValue():void {
        var prev:Number = value;
        value = (((thumb.rotation+450-angle)%360)/360)*max;
        //回転数
        if(prev < max/2){
            if(max - max/3 < value)turn--;
        }
        else{
            if(value < max/3)turn++;
        }

 
        if(dec == false)value = Math.round(value);//小数点以下の扱い
    }
    
    
    //操作受付の状態 true:可 false:不可
    public function get enabled():Boolean {
        return _enabled;
    }
    
    public function set enabled(param:Boolean):void {
        _enabled = param;
        if (!_enabled) _off();
        thumb.buttonMode = _enabled;
        thumb.mouseEnabled = _enabled;
        thumb.useHandCursor = _enabled;
        
        if (_enabled) {//操作可
            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);
        } else {//操作不可
            thumb.removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
            thumb.removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
            thumb.removeEventListener(MouseEvent.MOUSE_DOWN, press);
        }
    }
    
    //状態更新
    public function reset():void {
        value = initValue;
        thumb.rotation = value - 90 + zero;
    }
    
    //描画1
    private function createCircle(target:Shape, r:uint, color:uint, alpha:Number):void {
        target.graphics.beginFill(color, alpha);
        target.graphics.drawCircle(0, 0, r);
        target.graphics.endFill();
    }
    
    //描画2
    private function createDonut(target:Sprite, outer:uint, inner:uint):void {
        target.graphics.beginFill(bColor);
        target.graphics.drawCircle(0, 0, outer);//外縁
        target.graphics.drawCircle(0, 0, inner);//内縁
        target.graphics.endFill();
    }

}

//イベント
import flash.events.Event;
class CompoEvent extends Event {
    public static const SELECT:String = "select";
    public static const CHANGE:String = "change";
    public var value:*;

    public function CompoEvent(type:String, value:*) {
        super(type);
        this.value = value;
    }

    public override function clone():Event {
        return new CompoEvent(type, value);
    }

}