チェンジセット 3313: as3/Metasequoia/branches

差分発生行の前後
無視リスト:
コミット日時:
2010/01/20 00:41:30 (2 年前)
コミッタ:
tarotarorg
ログメッセージ:

@b2oxさんの変更を反映

ファイル:

凡例:

変更無し
追加
削除
更新
コピー
移動
  • as3/Metasequoia/branches/tarotarorg/src/org/tarotaro/flash/pv3d/ImageManager.as

    r615 r3313  
    11/* 
    2  *  Copyright 2008 tarotarorg 
    3  *  
    4  *  Licensed under the Apache License, Version 2.0 (the "License"); 
    5  *  you may not use this file except in compliance with the License. 
    6  *  You may obtain a copy of the License at 
    7  *  
    8  *      http://www.apache.org/licenses/LICENSE-2.0 
    9  *  
    10  *  Unless required by applicable law or agreed to in writing, software 
    11  *  distributed under the License is distributed on an "AS IS" BASIS, 
    12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    13  *  See the License for the specific language governing permissions and 
    14  *  limitations under the License. 
    15  */ 
     2*  Copyright 2008 tarotarorg 
     3
     4*  Licensed under the Apache License, Version 2.0 (the "License"); 
     5*  you may not use this file except in compliance with the License. 
     6*  You may obtain a copy of the License at 
     7
     8*      http://www.apache.org/licenses/LICENSE-2.0 
     9
     10*  Unless required by applicable law or agreed to in writing, software 
     11*  distributed under the License is distributed on an "AS IS" BASIS, 
     12*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
     13*  See the License for the specific language governing permissions and 
     14*  limitations under the License. 
     15*/ 
    1616package org.tarotaro.flash.pv3d { 
    1717        import flash.display.BitmapData; 
     
    2424        import flash.display.Loader; 
    2525        import flash.events.EventDispatcher; 
    26         import flash.net.URLLoader; 
    2726        import flash.net.URLLoaderDataFormat; 
    2827        import flash.net.URLRequest; 
     
    3433 
    3534        public class ImageManager extends EventDispatcher{ 
    36 //////////////////////////////////////////////////////////////////////////////////////// 
    37 //クラス変数定義 
    38 //////////////////////////////////////////////////////////////////////////////////////// 
     35               //////////////////////////////////////////////////////////////////////////////////////// 
     36               //クラス変数定義 
     37               //////////////////////////////////////////////////////////////////////////////////////// 
    3938                /** 
    40                 * ロードしたビットマップのキャッシュ 
    41                 * ロード待ち中:null 
    42                 * ロード済み:BitmapData 
    43                 * ロード中:この画像をロードしているLoader 
    44                 */ 
     39                * ロードしたビットマップのキャッシュ 
     40                * ロード待ち中:null 
     41                * ロード済み:BitmapData 
     42                * ロード中:この画像をロードしているLoader 
     43                */ 
    4544                private static var _loadedBitmaps:Dictionary = new Dictionary(); 
    46 //////////////////////////////////////////////////////////////////////////////////////// 
    47 //メンバ変数定義 
    48 //////////////////////////////////////////////////////////////////////////////////////// 
     45               //////////////////////////////////////////////////////////////////////////////////////// 
     46               //メンバ変数定義 
     47               //////////////////////////////////////////////////////////////////////////////////////// 
    4948                private var _bitmapData:BitmapData; 
    5049                private var _path:String; 
    5150                private var _name:String; 
    52                  
     51 
    5352                /** 
    54                 * ロードした画像をBitmapData形式で返す 
    55                 * @return 
    56                 */ 
     53                * ロードした画像をBitmapData形式で返す 
     54                * @return 
     55                */ 
    5756                public function get bitmapData() :BitmapData { 
    5857                        return this._bitmapData != null ? this._bitmapData.clone() : null; 
    5958                } 
    60                  
     59 
    6160                /** 
    62                 * ローダ名 
    63                 * @return 
    64                 */ 
     61                * ローダ名 
     62                * @return 
     63                */ 
    6564                public function get name():String { 
    6665                        return this._name; 
    6766                } 
    68                  
     67 
    6968                /** 
    70                 * ロード対象のパス 
    71                 * @return 
    72                 */ 
     69                * ロード対象のパス 
     70                * @return 
     71                */ 
    7372                public function get path():String { 
    7473                        return this._path; 
    7574                } 
    76                  
     75 
    7776                public function ImageManager(name:String):void { 
    7877                        this._name = name; 
    7978                } 
    80                  
     79 
    8180                public function getImage(path:String) : void { 
     81                        if (path.search( /\.(jpg|png|gif|tga|bmp)$/i) == -1) { 
     82                                throw new Error("非対応の画像形式です"); 
     83                        } 
    8284                        var bd:BitmapData = _loadedBitmaps[this.name] as BitmapData; 
    8385                        if ( bd ) { 
     
    9496                private function load(path:String):void { 
    9597                        this._path = path; 
    96                          
    97                         //拡張子によって呼び出す関数を判断 
    98                         if (path.toLowerCase().search( /\.(jpg|png|gif)$/) != -1) { 
    99                                 this.loadImage(); 
    100                         } else { 
    101                                 this.loadBinary(); 
    102                         } 
     98                        this.loadBinary(); 
    10399                } 
    104                  
     100 
    105101                /** 
    106                 * バイナリからエンコードする必要のある画像をロードする 
    107                 * @return 
    108                 */ 
     102                * バイナリからエンコードする必要のある画像をロードする 
     103                * @return 
     104                */ 
    109105                private function loadBinary():void { 
    110106                        var loader:ZipLoader = _loadedBitmaps[this.name] as ZipLoader; 
     
    126122                        } 
    127123                } 
    128                  
    129                 /** 
    130                 * JPG/PNG/GIF画像をロードする 
    131                 * @return 
    132                 */ 
    133                 private function loadImage():void { 
    134                         var loader:Loader =  _loadedBitmaps[this.name] as Loader; 
    135                         if (loader == null ) { 
    136                                 //新規にロードを実行 
    137                                 loader = new Loader(); 
    138                                 _loadedBitmaps[this.name] = loader; 
    139                                 loader.contentLoaderInfo.addEventListener(Event.COMPLETE , imgLoadComplete,false,int.MAX_VALUE); 
    140                                 loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR , defaultHandler); 
    141                                 loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR , defaultHandler); 
    142                                 loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS , defaultHandler); 
    143                                 loader.load(new URLRequest(this._path)); 
    144                         } else { 
    145                                 //現在ロード中なのでちょっと待ってみる 
    146                                 var waiting:Timer = new Timer(100 , 1); 
    147                                 waiting.addEventListener(TimerEvent.TIMER_COMPLETE , waitingComplete); 
    148                                 waiting.start(); 
    149                         } 
    150                 } 
    151                  
     124 
    152125                /** 
    153126                 * 画像ロード完了待ち 
     
    159132 
    160133                /** 
    161                 * デフォルトのイベント処理 
    162                 * @param      evt 
    163                 * @return 
    164                 */ 
     134                * デフォルトのイベント処理 
     135                * @param     evt 
     136                * @return 
     137                */ 
    165138                private function defaultHandler(evt:Event):void { 
    166139                        dispatchEvent( evt.clone()); 
    167140                } 
    168                  
     141 
    169142                /** 
    170                 * 画像をバイト列として読み込み、デコードする 
    171                 * @param      evt 
    172                 * @return 
    173                 */ 
     143                * 画像をバイト列として読み込み、デコードする 
     144                * @param     evt 
     145                * @return 
     146                */ 
    174147                private function binLoadComplete(evt:Event):void { 
    175                         if (this.path.toLowerCase().search(/\.tga$/) != -1) { 
     148                        if (this.path.search( /\.(jpg|png|gif)$/i) != -1) { 
     149                                var ldr:Loader = new Loader(); 
     150                                ldr.addEventListener(Event.COMPLETE, function (e:Event):void { 
     151                                        this._bitmapData = (ldr.content as Bitmap).bitmapData; 
     152                                        _loadedBitmaps[this.name] = this._bitmapData; 
     153                                        dispatchEvent( new ImageManagerEvent(this)); 
     154                                } ); 
     155                                ldr.loadBytes(evt.target.data); 
     156                        } else if (this.path.search(/\.tga$/i) != -1) { 
    176157                                var tga:TGADecoder = new TGADecoder(evt.target.data); 
    177                                 this._bitmapData = tga.bitmap;   
    178                         } else if (this.path.toLowerCase().search(/\.bmp$/) != -1) { 
     158                                this._bitmapData = tga.bitmap; 
     159                                _loadedBitmaps[this.name] = this._bitmapData; 
     160                                dispatchEvent( new ImageManagerEvent(this)); 
     161                        } else if (this.path.search(/\.bmp$/i) != -1) { 
    179162                                var bmpdec:BMPDecoder = new BMPDecoder(); 
    180163                                this._bitmapData = bmpdec.decode(evt.target.data); 
     164                                _loadedBitmaps[this.name] = this._bitmapData; 
     165                                dispatchEvent( new ImageManagerEvent(this)); 
     166                        } else { 
     167                                throw new Error("非対応の画像形式です"); 
    181168                        } 
    182                         _loadedBitmaps[this.name] = this._bitmapData; 
    183                         dispatchEvent( new ImageManagerEvent(this)); 
    184                 } 
    185                  
    186                 /** 
    187                 * JPG/PNG/GIF画像読み込み完了時 
    188                 * @param        evt 
    189                 * @return 
    190                 */ 
    191                 private function imgLoadComplete(evt:Event):void { 
    192                         var loader:Loader = evt.target.loader as Loader; 
    193                         this._bitmapData = (loader.content as Bitmap).bitmapData; 
    194                         _loadedBitmaps[this.name] = this._bitmapData; 
    195                         dispatchEvent(new ImageManagerEvent(this)); 
    196169                } 
    197170        } 
    198          
     171 
    199172} 
     173