case [8] 9818 (その3)

by ProjectNya
case [8] 9818 (その3)
♥0 | Line 172 | Modified 2011-01-06 18:48:50 | MIT License
play

ActionScript3 source code

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

////////////////////////////////////////////////////////////////////////////////
// case [8] 9818 (その3)
////////////////////////////////////////////////////////////////////////////////

package {

    import flash.display.Sprite;
    import flash.net.URLLoader;
    import flash.net.URLLoaderDataFormat;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.display.Loader;
    import flash.system.LoaderContext;
    import flash.events.MouseEvent;
    import flash.net.navigateToURL;

    [SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="30")]

    public class Main extends Sprite {
        private var label:Label;
        private var txtlist:Array;
        private var txtId:uint = 0;
        private var _loader:URLLoader;
        private var timer:Timer;
        private var loaders:Array;
        private var linklist:Array;
        private var photolist:Array;
        private var loaded:uint = 0;
        private var max:uint = 0;
        private static var interval:uint = 2000;
        private static var basePath:String = "http://www.project-nya.jp/";
        private static var filePath:String = "test/t898183.txt";
        private static var CR:String = String.fromCharCode(13);
        private static var LF:String = String.fromCharCode(10);

        public function Main() {
            //Wonderfl.capture_delay(1);
            init();
        }

        private function init():void {
            label = new Label(200, 200);
            addChild(label);
            label.x = 10;
            label.y = 10;
            label.textColor = 0x000000;
            label.text = "";
            //
            _loader = new URLLoader();
            _loader.dataFormat = URLLoaderDataFormat.TEXT;
            _loader.addEventListener(Event.COMPLETE, complete, false, 0, true);
            _loader.load(new URLRequest(basePath + filePath));
        }
        private function complete(evt:Event):void {
            parse(evt.target.data);
        }
        private function parse(src:String):void {
            var str:String = unifyLineFeedCode(src);
            var list:Array = str.split(LF+LF);
            if (list[list.length - 1] == "EOF") list.pop();
            txtlist = new Array();
            linklist = new Array();
            photolist = new Array();
            for (var n:uint = 0; n < list.length; n++) {
                var element:String = list[n];
                var elements:Array = element.split(LF);
                txtlist.push(elements[2]);
                linklist.push(elements[3]);
                if (elements[1] == "Display=yes") {
                    photolist[n] = elements[4];
                }
            }
            load();
        }
        private function load():void {
            loaders = new Array();
            for (var n:uint = 0; n < photolist.length; n++) {
                var photoPath:String = photolist[n];
                if (photoPath) {
                    max ++;
                    var container:Sprite = new Sprite();
                    addChild(container);
                    container.name = String(n);
                    container.x = 10;
                    container.y = 50;
                    var loader:Loader = new Loader();
                    container.addChild(loader);
                    loader.visible = false;
                    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, setup, false, 0, true);
                    loader.load(new URLRequest(photoPath), new LoaderContext(true));
                    loaders[n] = loader;
                    container.buttonMode = true;
                    container.mouseChildren = false;
                    container.addEventListener(MouseEvent.CLICK, click, false, 0, true);
                }
            }
        }
        private function setup(evt:Event):void {
            loaded ++;
            if (loaded > max - 1) {
                start();
            }
        }
        private function unifyLineFeedCode(str:String):String {
            str = str.split(CR+LF).join(LF);
            str = str.split(CR).join(LF);
            return str;
        }
        private function start():void {
            show(txtId);
            timer = new Timer(interval);
            timer.addEventListener(TimerEvent.TIMER, transit, false, 0, true);
            timer.start();
        }
        private function transit(evt:TimerEvent):void {
            txtId = (txtId + 1)%txtlist.length;
            show(txtId);
        }
        private function show(id:uint):void {
            label.text = txtlist[id];
            for (var n:uint = 0; n < loaders.length; n++) {
                var loader:Loader = loaders[n];
                if (loader) {
                    if (n == id) {
                        loader.visible = true;
                    } else {
                        loader.visible = false;
                    }
                }
            }
        }
        private function click(evt:MouseEvent):void {
            var linkPath:String = linklist[evt.target.name];
            navigateToURL(new URLRequest(linkPath), "_blank");
        }

    }

}


//////////////////////////////////////////////////
// Labelクラス
//////////////////////////////////////////////////

import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;

class Label extends Sprite {
    private var txt:TextField;
    private static var fontType:String = "_ゴシック";
    private var _width:uint = 20;
    private var _height:uint = 20;
    private var size:uint = 12;
    public static const LEFT:String = TextFormatAlign.LEFT;
    public static const CENTER:String = TextFormatAlign.CENTER;
    public static const RIGHT:String = TextFormatAlign.RIGHT;

    public function Label(w:uint, h:uint, s:uint = 12, align:String = LEFT) {
        _width = w;
        _height = h;
        size = s;
        draw(align);
    }

    private function draw(align:String):void {
        txt = new TextField();
        addChild(txt);
        txt.width = _width;
        txt.height = _height;
        txt.autoSize = align;
        txt.type = TextFieldType.DYNAMIC;
        txt.selectable = false;
        //txt.embedFonts = true;
        //txt.antiAliasType = AntiAliasType.ADVANCED;
        var tf:TextFormat = new TextFormat();
        tf.font = fontType;
        tf.size = size;
        tf.align = align;
        txt.defaultTextFormat = tf;
        textColor = 0x000000;
    }
    public function set text(param:String):void {
        txt.text = param;
    }
    public function set textColor(param:uint):void {
        txt.textColor = param;
    }

}