Beautifl - Flash Gallery

Thumbnail : HueCircleTest 色相環テスト
HueCircleTest 色相環テスト
ton 2011-02-07 All rights reserved

再生するにはFlash Playerが必要です。デスクトップのブラウザでご覧ください。

package {
    import caurina.transitions.Tweener;
    import com.bit101.components.Label;
    import flash.display.DisplayObject;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.BlurFilter;
    import flash.filters.DropShadowFilter;
    import flash.filters.GlowFilter;
    import flash.net.URLRequest;
    import flash.system.LoaderContext;
    import flash.text.TextFieldAutoSize;
    import flash.utils.getTimer;
    import frocessing.color.ColorHSV;
    import net.wonderfl.utils.WonderflAPI;
    
    [SWF(width=465,height=465,backgroundColor=0x111111,frameRate=30)]
    public class HueCircleTest extends Sprite {
        static public const N:int = 24;
        static public const SIZE:int = 36;
        static public const SCORE_URL:String = "http://swf.wonderfl.net/swf/usercode/5/57/579a/579a46e1306b5770d429a3738349291f05fec4f3.swf";
        static public const TWEET:String = "HueCircleTest time:%SCORE%sec #wonderfl";

        private var panels:Array = [];
        private var bases:Array = [];
        private var ansPanels:Array = [];

        private var centerX:int;
        private var centerY:int;
        private var r:int;

        private var count:int = 0;
        private var countColors:Array = [0xff0033, 0xffd700, 0x1e90ff];

        private var timeLabel:Label;
        private var startTime:int;
        private var nowTime:int;

        private var efCnt:int = 0;
        private var glow:GlowFilter = new GlowFilter(0xff0000, 1, 8, 8, 4);
        private var efColor:ColorHSV = new ColorHSV();
        
        private var loader:Loader;
        private var scoreWindow:DisplayObject;

        public function HueCircleTest():void {
            loader = new Loader();
            loader.load(new URLRequest(SCORE_URL), new LoaderContext(true));
            
            centerX = stage.stageWidth / 2;
            centerY = SIZE + stage.stageHeight / 2;
            r = stage.stageWidth / 3;

            timeLabel = new Label(this);
            timeLabel.text = "0.0";
            timeLabel.scaleX = timeLabel.scaleY = 4;
            timeLabel.x = centerX - 30;
            timeLabel.y = centerY - timeLabel.height * timeLabel.scaleY / 2;

            for (var i:int = 0; i < 360; i += 360 / N){
                var color:ColorHSV = new ColorHSV(i, 0.8);
                var panel:Panel = new Panel(color, SIZE);
                panel.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
                panels.push(panel);

                var base:BasePanel = new BasePanel(i - 90, SIZE);
                var rad:Number = base.rotation * 2 * Math.PI / 360;
                base.x = centerX + r * Math.cos(rad);
                base.y = centerY + r * Math.sin(rad);
                addChild(base);
                bases.push(base);
            }

            addEventListener(Event.ENTER_FRAME, countDown);
        }

        private function countDown(e:Event):void {
            var time:int = count / N;
            var color:uint = countColors[time];
            var index:int = count % N;
            var base:BasePanel = bases[index];

            base.color = color;

            count++;

            if (count >= 3 * N){
                for (var i:int = 0; i < N; i++){
                    bases[i].color = BasePanel.DEF_COLOR;
                }
                removeEventListener(e.type, arguments.callee);
                start();
            }
        }

        private function start():void {
            shuffle(panels);

            for (var i:int = 0; i < 2; i++){
                for (var j:int = 0; j < N / 2; j++){
                    var p:Panel = panels[j + i * N / 2];
                    p.initX = j * SIZE + p.width / 2;
                    p.initY = i * SIZE + p.height / 2;
                    p.initRotation = p.rotation;
                    Tweener.addTween(p, {x: p.initX, y: p.initY, time: Math.random()});
                    addChild(p);
                }
            }

            startTime = getTimer();
            addEventListener(Event.ENTER_FRAME, timer);
        }

        private function timer(e:Event):void {
            nowTime = getTimer() - startTime;
            nowTime = nowTime / 100;
            timeLabel.text = nowTime / 10 + "";

            timeLabel.x = centerX - 40;
            timeLabel.y = centerY - timeLabel.height * timeLabel.scaleY / 2;
        }

        private function change(target:Panel):void {
            for (var i:int = 0; i < bases.length; i++){
                var base:BasePanel = bases[i];
                //枠にハメる
                if (base.hitTestPoint(mouseX, mouseY)){
                    target.x = base.x;
                    target.y = base.y;
                    target.rotation = base.rotation;

                    var element:Panel = ansPanels[i];
                    var index:int = ansPanels.indexOf(target);

                    if (element){
                        element.init(target.initX, target.initY, target.initRotation);
                        Tweener.addTween(element, {x: target.initX, y: target.initY, rotation: target.initRotation, time: 1});

                        if (index != -1)
                            ansPanels[index] = element;

                    } else {
                        if (index != -1)
                            ansPanels[index] = null;
                    }

                    ansPanels[i] = target;
                    target.init(target.x, target.y, target.rotation);

                    if (checkResult())
                        complete();
                    return;
                }
            }

            Tweener.addTween(target, {x: target.initX, y: target.initY, rotation: target.initRotation, time: 1});
        }

        private function complete():void {
            removeEventListener(Event.ENTER_FRAME, timer);

            for (var i:int = 0; i < N; i++){
                var panel:Panel = panels[i];
                panel.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
            }
            
            scoreWindow = Object(loader.content).makeScoreWindow(new WonderflAPI(loaderInfo.parameters), nowTime, "HueCircleTest", 10, TWEET, "CLEAR TIME", "sec", 99, 0);
            addChild(scoreWindow);
            
            timeLabel.filters = [new DropShadowFilter(4, 45, 0xff0000)];
            addEventListener(Event.ENTER_FRAME, completeEffects);
        }

        private function completeEffects(e:Event):void {
            var h:int = efCnt*10 % 360;
            var index:int = efCnt % N;
            var base:BasePanel = bases[index];
            efColor.h = h;
            glow.color = efColor.value;
            base.filters = [glow];
            efCnt++;
        }

        private function checkResult():Boolean {
            if (!ansPanels[0])
                return false;

            var prev:int = ansPanels[0].color.h;
            for (var i:int = 1; i < N; i++){
                if (!ansPanels[i])
                    return false;

                var now:int = ansPanels[i].color.h;
                if (Math.abs(prev - now) == 360 / N){
                    prev = now;
                } else if (Math.abs(prev - now) == 360 - 360 / N){
                    prev = now;
                } else {
                    return false;
                }
            }
            return true;
        }

        private function mouseDown(e:MouseEvent):void {
            var target:Panel = e.currentTarget as Panel;
            target.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
            target.addEventListener(MouseEvent.MOUSE_UP, mouseUp);

            target.startDrag(true);
            addChild(target);

            if (Tweener.isTweening(target))
                Tweener.pauseTweens(target);
        }

        private function mouseUp(e:MouseEvent):void {
            var target:Panel = e.currentTarget as Panel;
            target.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
            target.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);

            target.stopDrag();
            change(target);
        }

        private function mouseMove(e:MouseEvent):void {
            var target:Panel = e.currentTarget as Panel;

            for (var i:int = 0; i < bases.length; i++){
                var base:BasePanel = bases[i];

                if (base.hitTestPoint(mouseX, mouseY)){
                    target.rotation = base.rotation;
                    break;
                }
            }

            e.updateAfterEvent();
        }

        private function shuffle(arr:Array):void {
            var length:int = arr.length;
            while (length--){
                var i:int = Math.floor(Math.random() * (length + 1));
                var element:Object = arr[i];
                arr[i] = arr[length];
                arr[length] = element;
            }
        }
    }
}

import flash.display.Sprite
import frocessing.color.ColorHSV;

class Panel extends Sprite {
    private var _color:ColorHSV;
    private var _initX:int;
    private var _initY:int;
    private var _initRotation:Number;

    public function Panel(color:ColorHSV, size:int){
        _color = color;

        graphics.beginFill(color.value);
        graphics.drawRoundRect(-size / 2, -size / 2, size, size, 10);
        graphics.endFill();

        this.buttonMode = true;
    }

    public function init(x:int, y:int, rotation:Number):void {
        _initX = x;
        _initY = y;
        _initRotation = rotation;
    }

    public function get initX():int {
        return _initX;
    }

    public function set initX(value:int):void {
        _initX = value;
    }

    public function get initY():int {
        return _initY;
    }

    public function set initY(value:int):void {
        _initY = value;
    }

    public function get initRotation():Number {
        return _initRotation;
    }

    public function set initRotation(value:Number):void {
        _initRotation = value;
    }

    public function get color():ColorHSV {
        return _color;
    }

    public function set color(value:ColorHSV):void {
        _color = value;
    }
}

class BasePanel extends Sprite {
    public static const DEF_COLOR:uint = 0xaaaaaa;
    private var size:int;

    public function BasePanel(angle:Number, size:int){
        this.size = size;
        rotation = angle;
    }

    public function set color(rgb:uint):void {
        graphics.clear();
        graphics.beginFill(rgb, 0.9);
        graphics.drawRoundRect(-size / 2, -size / 2, size, size, 10);
        graphics.endFill();
    }
}