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

メニューバー ver 0.8

目標:AIR用のNativeMenu関係をWonderflに移植する
* 
* ver 0.8:とりあえず見た目がそれっぽくなった
*
* 予定:
* 選択時のGUIの実装
* サブメニュー>サブメニューの実装
* イベントの実装
* ショートカットキーの実装
Get Adobe Flash player
by geko 26 Jul 2010
/**
 * Copyright geko ( http://wonderfl.net/user/geko )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/8dPA
 */

/*
 * 目標:AIR用のNativeMenu関係をWonderflに移植する
 * 
 * ver 0.8:とりあえず見た目がそれっぽくなった
 *
 * 予定:
 * 選択時のGUIの実装
 * サブメニュー>サブメニューの実装
 * イベントの実装
 * ショートカットキーの実装
 */
package {
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            // write as3 code here..
            Wonderfl.disable_capture();
            var menu:WonderflWindow = new WonderflWindow();
            addChild(menu);
        }
    }
}

import flash.filters.DropShadowFilter;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;

class WonderflWindow extends Sprite{
    private var _menu:WonderflMenu = new WonderflMenu();
    private var _menuBar:Sprite = new Sprite();
    private var selected:Boolean = false;
    
    public function WonderflWindow():void{
        this.addEventListener(Event.ADDED_TO_STAGE, init);
    }
    
    private function init(event:Event):void{
        this.removeEventListener(Event.ADDED_TO_STAGE, init);
        
        _menu.addSubmenu(new WonderflMenu(),"");        
        _menu.addSubmenu(new WonderflMenu(),"Wonderfl");
        _menu.addSubmenu(new WonderflMenu(),"ファイル");
        _menu.addSubmenu(new WonderflMenu(),"編集");
        _menu.addSubmenu(new WonderflMenu(),"表示");
        _menu.addSubmenu(new WonderflMenu(),"ヘルプ");
        _menu.items[0].submenu.addItem(new WonderflMenuItem("このMacについて"));
        _menu.items[0].submenu.addItem(new WonderflMenuItem("ソフトウェア・アップデート..."));
        _menu.items[0].submenu.addItem(new WonderflMenuItem("Mac OS X ソフトウェア..."));
        _menu.items[0].submenu.addItem(new WonderflMenuItem("",true));
        _menu.items[0].submenu.addItem(new WonderflMenuItem("システム環境設定..."));
        _menu.items[0].submenu.addItem(new WonderflMenuItem("Dock"));
        _menu.items[0].submenu.addItem(new WonderflMenuItem("",true));
        _menu.items[0].submenu.addItem(new WonderflMenuItem("強制終了"));
        _menu.items[0].submenu.addItem(new WonderflMenuItem("",true));
        _menu.items[0].submenu.addItem(new WonderflMenuItem("スリープ"));
        _menu.items[0].submenu.addItem(new WonderflMenuItem("再起動"));
        _menu.items[0].submenu.addItem(new WonderflMenuItem("システム終了"));
        _menu.items[0].submenu.addItem(new WonderflMenuItem("",true));
        _menu.items[0].submenu.addItem(new WonderflMenuItem("ログアウト"));
        _menu.items[1].submenu.addItem(new WonderflMenuItem("Wonderflについて"));
        _menu.items[1].submenu.addItem(new WonderflMenuItem("",true));
        _menu.items[1].submenu.addItem(new WonderflMenuItem("環境設定..."));
        _menu.items[1].submenu.addItem(new WonderflMenuItem("",true));
        _menu.items[1].submenu.addItem(new WonderflMenuItem("サービス"));
        _menu.items[1].submenu.addItem(new WonderflMenuItem("",true));
        _menu.items[1].submenu.addItem(new WonderflMenuItem("Wonderflを終了"));
        _menu.items[2].submenu.addItem(new WonderflMenuItem("新規ウインドウ"));
        _menu.items[2].submenu.addItem(new WonderflMenuItem("新規タブ"));
        _menu.items[2].submenu.addItem(new WonderflMenuItem("URLを開く..."));
        _menu.items[2].submenu.addItem(new WonderflMenuItem("ファイルを開く..."));
        _menu.items[2].submenu.addItem(new WonderflMenuItem("ウインドウを閉じる"));
        _menu.items[2].submenu.addItem(new WonderflMenuItem("",true));
        _menu.items[2].submenu.addItem(new WonderflMenuItem("別名でページを保存..."));
        _menu.items[3].submenu.addItem(new WonderflMenuItem("取り消す")).enabled = false;
        _menu.items[3].submenu.addItem(new WonderflMenuItem("やり直し")).enabled = false;
        _menu.items[3].submenu.addItem(new WonderflMenuItem("",true));
        _menu.items[3].submenu.addItem(new WonderflMenuItem("カット")).enabled = false;
        _menu.items[3].submenu.addItem(new WonderflMenuItem("コピー")).enabled = false;
        _menu.items[3].submenu.addItem(new WonderflMenuItem("ペースト"));
        _menu.items[3].submenu.addItem(new WonderflMenuItem("",true));
        _menu.items[3].submenu.addItem(new WonderflMenuItem("すべて選択"));
        _menu.items[4].submenu.addItem(new WonderflMenuItem("ツールバー"));
        _menu.items[4].submenu.addItem(new WonderflMenuItem("ステータスバー"));
        _menu.items[4].submenu.addItem(new WonderflMenuItem("",true));
        _menu.items[4].submenu.addItem(new WonderflMenuItem("フルスクリーン"));
        _menu.items[4].submenu.addItem(new WonderflMenuItem("",true));
        _menu.items[4].submenu.addItem(new WonderflMenuItem("表示オプションを表示"));
        _menu.items[5].submenu.addItem(new WonderflMenuItem("Windowsだと見難い"));
        _menu.items[5].submenu.addItem(new WonderflMenuItem("フォントは何が良いの?"));
        _menu.items[5].submenu.addItem(new WonderflMenuItem("一番左のメニュー項目はApple"));
        _menu.items[5].submenu.addItem(new WonderflMenuItem("",true));
        _menu.items[5].submenu.addItem(new WonderflMenuItem("ヘルプ"));

        _menuBar.graphics.beginFill(0xDFDFDF);
        _menuBar.graphics.drawRect(-1,0,stage.stageWidth+2,20);
        _menuBar.graphics.endFill();
        _menuBar.filters = [new DropShadowFilter(2,90,0x000000,0.6,8,8,1,3)];
        addChild(_menuBar);
        drawMenuBar();
    }

    
    /////////////////////////////////////
    //////         メソッド         //////
    /////////////////////////////////////
    
    /////////////////////////////////////
    //////    プライベートメソッド     //////
    /////////////////////////////////////
    
    private function drawMenuBar():void{
        var menuLabel:MenuLabel;
        var _x:Number = 5;
        for each(var item:WonderflMenuItem in _menu.items){
            menuLabel = new MenuLabel(item);
            menuLabel.x = _x;
            _menuBar.addChild(menuLabel);
            _x += menuLabel.width;
            menuLabel.addEventListener(Event.SELECT, select);
            menuLabel.addEventListener(MouseEvent.ROLL_OVER, mouseHandler);
            menuLabel.addEventListener(MouseEvent.ROLL_OUT, mouseHandler);
        }
    }
    
    private function select(event:Event):void{
        selected = event.target.selected;
    }
    private function mouseHandler(event:MouseEvent):void{
        if(!selected) return;
        switch(event.type){
            case MouseEvent.ROLL_OUT: event.target.selected = false; break;
            case MouseEvent.ROLL_OVER: event.target.selected = true; break;
        }

    }

    private function removeMenuBar():void{
        
    }
    
    /////////////////////////////////////
    //////         プロパティ        //////
    /////////////////////////////////////
    
    public function set menu(_menu:WonderflMenu):void{
        this._menu = _menu;
        drawMenuBar();
    }
}

class MenuLabel extends Sprite{
    private var _font:String = "_ゴシック";
    private var _txt:TextField = new TextField();
    private var _format:TextFormat = new TextFormat(_font,12,0x000000,null,null,null,null,null,"center");
    private var _submenu:Sprite = new Sprite();
    private var _selected:Boolean;
    
    public function MenuLabel(item:WonderflMenuItem){
        this.item = item;
        this.addEventListener(Event.ADDED_TO_STAGE, init);
        if(item.isSeparator) return;
        _txt = new TextField();
        _txt.mouseEnabled = false;
        _txt.defaultTextFormat = _format;
        _txt.text = item.label;
        _txt.width = _txt.textWidth+20;//(parent.parent is WonderflWindow ? 20 : 40);
        _txt.height = 20;
        this.addChild(_txt);
    }
    
    private function init(event:Event):void{
        if(item.isSeparator){
            this.graphics.clear();
            this.graphics.lineStyle(1,0xBFBFBF);
            this.graphics.moveTo(1,5);
            this.graphics.lineTo(parent.width-2,5);
            //this.graphics.beginFill(0xDFDFDF);
            //this.graphics.drawRect(0,0,parent.width,20);
            //this.graphics.endFill();
            return;
        }

        this.addEventListener(MouseEvent.CLICK, click);
        if(!item.enabled) _txt.textColor = 0xBFBFBF;
        //selected = false;

        if(item.submenu){
            //if(item.menu) _txt.appendText("▶");
            _submenu.y = 20;
            var menuLabel:MenuLabel;
            var _y:uint = 0;
            for each(var _item:WonderflMenuItem in item.submenu.items){
                menuLabel = new MenuLabel(_item);
                menuLabel.y = _y;
                _submenu.addChild(menuLabel);
                _y += _item.isSeparator ? 10 : 20;
                //menuLabel.selected = false;
                menuLabel.addEventListener(MouseEvent.ROLL_OVER, mouseHandler);
                menuLabel.addEventListener(MouseEvent.ROLL_OUT, mouseHandler);
            }
            _submenu.graphics.beginFill(0xDFDFDF);
            _submenu.graphics.drawRect(0,0,_submenu.width,_submenu.height);
            _submenu.dispatchEvent(new Event(Event.COMPLETE));
        }
    }
    
    private function click(event:MouseEvent):void{
        if(item.isSeparator) return;
        selected = !selected;
        if(!item.submenu) item.dispatchEvent(new Event(Event.SELECT));
        dispatchEvent(new Event(Event.SELECT));
    }
    private function mouseHandler(event:MouseEvent):void{
        //if(!selected) return;
        switch(event.type){
            case MouseEvent.ROLL_OUT: event.target.selected = false; break;
            case MouseEvent.ROLL_OVER: event.target.selected = true; break;
        }
    }

    public var item:WonderflMenuItem;
    public function set selected(value:Boolean):void{
        if(item.isSeparator) return;
        this.graphics.clear();
        if(value){
            if(!item.enabled) return;
            this.graphics.beginFill(0x666666);
            _txt.textColor = 0xFFFFFF;
            if(item.submenu)  this.addChild(_submenu);
        }
        else{
            this.graphics.beginFill(0xDFDFDF);
            _txt.textColor = item.enabled ? 0x000000 : 0xBFBFBF;
            if(item.submenu) this.removeChild(_submenu);
        }
        
        if(parent.parent is WonderflWindow) this.graphics.drawRect(0,0,_txt.width,20);
        else this.graphics.drawRect(1,0,parent.width-2,20);
        this.graphics.endFill();
        _selected = value;
    }
    
    public function get selected():Boolean{
        return _selected;
    }

}
/*
class SubmenuLabel extends Sprite{
    private var _font:String = "_ゴシック";
    private var _format:TextFormat = new TextFormat(_font,12,0x000000,null,null,null,null,null,"left",20);
    public function SubmenuLabel(labels:Array){
        var txt:TextField;
        var maxWidth:Number = 0;
        for each(var label:String in labels){
            txt = new TextField();
            txt.mouseEnabled = false;
            txt.defaultTextFormat = _format;
            txt.text = label;
            txt.width = txt.textWidth+20;
            txt.height = 20;
            this.addChild(txt);
            maxWidth = Math.max(maxWidth, txt.width);
        }
        

    }
}*/

//********************************************************


//NativeMenu
class WonderflMenu extends EventDispatcher{
    private var _parent:WonderflMenu = null;
    
    public function WonderflMenu(){}

    /////////////////////////////////////
    //////         メソッド         //////
    /////////////////////////////////////

    public function addItem(item:WonderflMenuItem):WonderflMenuItem{
       items.push(item);
       item.menu = this;
       return item;
    }
    
    public function addItemAt(item:WonderflMenuItem, index:int):WonderflMenuItem{
        items.splice(index, 0, item);
        item.menu = this;
        return item;
    }
    
    public function addSubmenu(submenu:WonderflMenu, label:String):WonderflMenuItem{
        var submenuItem:WonderflMenuItem = new WonderflMenuItem(label);
        submenuItem.menu = this;
        submenuItem.submenu = submenu;
        return addItem(submenuItem);
    }
    
    public function addSubmenuAt(submenu:WonderflMenu, index:int, label:String):WonderflMenuItem{
        var submenuItem:WonderflMenuItem = new WonderflMenuItem(label);
        submenuItem.menu = this;
        submenuItem.submenu = submenu;
        return addItemAt(submenuItem, index);
    }

    //未実装
    public function clone():WonderflMenu{
        return null;
    }

    
    /////////////////////////////////////
    //////    プライベートメソッド     //////
    /////////////////////////////////////

    /////////////////////////////////////
    //////         プロパティ        //////
    /////////////////////////////////////
    public var items:Array = [];
    public function get numItems():int{
        return items.length;
    }
    public function get parent():WonderflMenu{
        return _parent;
    }


}

//NativeMenuItem
class WonderflMenuItem extends EventDispatcher{
    private var _isSeparator:Boolean = false;
    
    public function WonderflMenuItem(label:String = "", isSeparator:Boolean = false){
        this.label = label;
        _isSeparator = isSeparator;
    }
    /////////////////////////////////////
    //////         メソッド         //////
    /////////////////////////////////////   
    
    /////////////////////////////////////
    //////    プライベートメソッド     //////
    /////////////////////////////////////

    /////////////////////////////////////
    //////         プロパティ        //////
    /////////////////////////////////////
    public var checked:Boolean = false;           //未実装(チェックマークの表示/非表示)
    public var data:Object = null;                //未実装(?)
    public var enabled:Boolean = true;            //(メニューの有効/無効)
    public var label:String = "";                 //(ラベル)
    public var keyEquivalent:String = "";         //未実装(キーボードショートカット)
    public var keyEquivalentModifiers:Array = []; //未実装(キーボードショートカットのモディファイアキーコードの指定)
    public var name:String = "";                  //(このメニューアイテムの名前)
    public var menu:WonderflMenu;                 //(このアイテムを含むメニュー)
    public var mnemonicIndex:int;                 //未実装(?)
    public var submenu:WonderflMenu;              //(このアイテムに関連付けられたサブメニュー)
    public function get isSeparator():Boolean{    //(セパレータの有無)
        return _isSeparator;
    }
}