チェンジセット 3313: as3/Metasequoia/branches
- コミット日時:
- 2010/01/20 00:41:30 (2 年前)
- ファイル:
凡例:
- 変更無し
- 追加
- 削除
- 更新
- コピー
- 移動
as3/Metasequoia/branches/tarotarorg/src/org/tarotaro/flash/pv3d/ImageManager.as
r615 r3313 1 1 /* 2 * Copyright 2008 tarotarorg3 * 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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * 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 */ 16 16 package org.tarotaro.flash.pv3d { 17 17 import flash.display.BitmapData; … … 24 24 import flash.display.Loader; 25 25 import flash.events.EventDispatcher; 26 import flash.net.URLLoader;27 26 import flash.net.URLLoaderDataFormat; 28 27 import flash.net.URLRequest; … … 34 33 35 34 public class ImageManager extends EventDispatcher{ 36 ////////////////////////////////////////////////////////////////////////////////////////37 //クラス変数定義38 ////////////////////////////////////////////////////////////////////////////////////////35 //////////////////////////////////////////////////////////////////////////////////////// 36 //クラス変数定義 37 //////////////////////////////////////////////////////////////////////////////////////// 39 38 /** 40 * ロードしたビットマップのキャッシュ41 * ロード待ち中:null42 * ロード済み:BitmapData43 * ロード中:この画像をロードしているLoader44 */39 * ロードしたビットマップのキャッシュ 40 * ロード待ち中:null 41 * ロード済み:BitmapData 42 * ロード中:この画像をロードしているLoader 43 */ 45 44 private static var _loadedBitmaps:Dictionary = new Dictionary(); 46 ////////////////////////////////////////////////////////////////////////////////////////47 //メンバ変数定義48 ////////////////////////////////////////////////////////////////////////////////////////45 //////////////////////////////////////////////////////////////////////////////////////// 46 //メンバ変数定義 47 //////////////////////////////////////////////////////////////////////////////////////// 49 48 private var _bitmapData:BitmapData; 50 49 private var _path:String; 51 50 private var _name:String; 52 51 53 52 /** 54 * ロードした画像をBitmapData形式で返す55 * @return56 */53 * ロードした画像をBitmapData形式で返す 54 * @return 55 */ 57 56 public function get bitmapData() :BitmapData { 58 57 return this._bitmapData != null ? this._bitmapData.clone() : null; 59 58 } 60 59 61 60 /** 62 * ローダ名63 * @return64 */61 * ローダ名 62 * @return 63 */ 65 64 public function get name():String { 66 65 return this._name; 67 66 } 68 67 69 68 /** 70 * ロード対象のパス71 * @return72 */69 * ロード対象のパス 70 * @return 71 */ 73 72 public function get path():String { 74 73 return this._path; 75 74 } 76 75 77 76 public function ImageManager(name:String):void { 78 77 this._name = name; 79 78 } 80 79 81 80 public function getImage(path:String) : void { 81 if (path.search( /\.(jpg|png|gif|tga|bmp)$/i) == -1) { 82 throw new Error("非対応の画像形式です"); 83 } 82 84 var bd:BitmapData = _loadedBitmaps[this.name] as BitmapData; 83 85 if ( bd ) { … … 94 96 private function load(path:String):void { 95 97 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(); 103 99 } 104 100 105 101 /** 106 * バイナリからエンコードする必要のある画像をロードする107 * @return108 */102 * バイナリからエンコードする必要のある画像をロードする 103 * @return 104 */ 109 105 private function loadBinary():void { 110 106 var loader:ZipLoader = _loadedBitmaps[this.name] as ZipLoader; … … 126 122 } 127 123 } 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 152 125 /** 153 126 * 画像ロード完了待ち … … 159 132 160 133 /** 161 * デフォルトのイベント処理162 * @param evt163 * @return164 */134 * デフォルトのイベント処理 135 * @param evt 136 * @return 137 */ 165 138 private function defaultHandler(evt:Event):void { 166 139 dispatchEvent( evt.clone()); 167 140 } 168 141 169 142 /** 170 * 画像をバイト列として読み込み、デコードする171 * @param evt172 * @return173 */143 * 画像をバイト列として読み込み、デコードする 144 * @param evt 145 * @return 146 */ 174 147 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) { 176 157 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) { 179 162 var bmpdec:BMPDecoder = new BMPDecoder(); 180 163 this._bitmapData = bmpdec.decode(evt.target.data); 164 _loadedBitmaps[this.name] = this._bitmapData; 165 dispatchEvent( new ImageManagerEvent(this)); 166 } else { 167 throw new Error("非対応の画像形式です"); 181 168 } 182 _loadedBitmaps[this.name] = this._bitmapData;183 dispatchEvent( new ImageManagerEvent(this));184 }185 186 /**187 * JPG/PNG/GIF画像読み込み完了時188 * @param evt189 * @return190 */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));196 169 } 197 170 } 198 171 199 172 } 173

