| 1 |
/* |
|---|
| 2 |
* Copyright(c) 2006-2008 the Spark project. |
|---|
| 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, |
|---|
| 13 |
* either express or implied. See the License for the specific language |
|---|
| 14 |
* governing permissions and limitations under the License. |
|---|
| 15 |
*/ |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
package org.libspark.utils |
|---|
| 19 |
{ |
|---|
| 20 |
import flash.display.Bitmap; |
|---|
| 21 |
import flash.display.BitmapData; |
|---|
| 22 |
import flash.display.DisplayObject; |
|---|
| 23 |
import flash.display.DisplayObjectContainer; |
|---|
| 24 |
import flash.display.IBitmapDrawable; |
|---|
| 25 |
import flash.display.Loader; |
|---|
| 26 |
import flash.display.Sprite; |
|---|
| 27 |
import flash.filters.BitmapFilter; |
|---|
| 28 |
import flash.filters.ColorMatrixFilter; |
|---|
| 29 |
import flash.geom.Matrix; |
|---|
| 30 |
import flash.geom.Point; |
|---|
| 31 |
import flash.geom.Transform; |
|---|
| 32 |
import flash.errors.IllegalOperationError; |
|---|
| 33 |
|
|---|
| 34 |
/** |
|---|
| 35 |
* BitmapData のためのユーティリティクラスです |
|---|
| 36 |
*/ |
|---|
| 37 |
public class BitmapUtil |
|---|
| 38 |
{ |
|---|
| 39 |
/** |
|---|
| 40 |
* @private |
|---|
| 41 |
*/ |
|---|
| 42 |
public function BitmapUtil() |
|---|
| 43 |
{ |
|---|
| 44 |
throw new IllegalOperationError("Error #2012: BitmapUtil class cannot be instantiated."); |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
/** |
|---|
| 48 |
* ビットマップにフィルタを適応します |
|---|
| 49 |
* |
|---|
| 50 |
* @param bitmap 対象となるBitmapDataインスタンス |
|---|
| 51 |
* @param filter 適応するBitmapFilterインスタンス |
|---|
| 52 |
*/ |
|---|
| 53 |
public static function applyFilter( bitmap:BitmapData, filter:BitmapFilter ):void |
|---|
| 54 |
{ |
|---|
| 55 |
bitmap.applyFilter(bitmap, bitmap.rect, new Point(), filter); |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
/** |
|---|
| 59 |
* BitmapDataを指定のコンテナ内に配置します。 |
|---|
| 60 |
* |
|---|
| 61 |
* @param target コンテナインスタンス |
|---|
| 62 |
* @param bitmapData 配置するBitmapData |
|---|
| 63 |
* @param smoothing スムージングの有無 |
|---|
| 64 |
* @return コンテナに配置されたSpriteインスタンス |
|---|
| 65 |
*/ |
|---|
| 66 |
public static function attachBitmap( target:DisplayObjectContainer, |
|---|
| 67 |
bitmapData:BitmapData , smoothing:Boolean=false ):Sprite |
|---|
| 68 |
{ |
|---|
| 69 |
var bitmap:Bitmap = new Bitmap( bitmapData, "auto", smoothing ); |
|---|
| 70 |
var sprite:Sprite = new Sprite(); |
|---|
| 71 |
sprite.addChild( bitmap ); |
|---|
| 72 |
target.addChild( sprite ); |
|---|
| 73 |
return sprite; |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
/** |
|---|
| 77 |
* DisplayObjectContainer 内に配置されている DisplayObject インスタンスのキャプチャを作成し |
|---|
| 78 |
* 対象と置き換えます。 対象はコンテナ内から削除されます。 |
|---|
| 79 |
* |
|---|
| 80 |
* @param target 対象となるDisplayObjectインスタンス |
|---|
| 81 |
* @return キャプチャBitmapを内包するSpriteインスタンス |
|---|
| 82 |
*/ |
|---|
| 83 |
public static function swapBitmap( target:DisplayObject ):Sprite |
|---|
| 84 |
{ |
|---|
| 85 |
var bitmap:BitmapData = capture( target ); |
|---|
| 86 |
var rect:Object = target.getBounds( target ); |
|---|
| 87 |
var index:int = target.parent.getChildIndex( target ); |
|---|
| 88 |
var transform:Transform = target.transform; |
|---|
| 89 |
var name:String = target.name; |
|---|
| 90 |
var image:Bitmap = new Bitmap( bitmap, "auto", true ); |
|---|
| 91 |
var sprite:Sprite = new Sprite(); |
|---|
| 92 |
sprite.addChild( image ); |
|---|
| 93 |
image.x = rect.x; |
|---|
| 94 |
image.y = rect.y; |
|---|
| 95 |
sprite.transform = transform; |
|---|
| 96 |
target.parent.addChildAt( sprite , index ); |
|---|
| 97 |
target.parent.removeChild( target ); |
|---|
| 98 |
return sprite; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
/** |
|---|
| 102 |
* DisplayObjectContainer 内に配置されている DisplayObject インスタンスのキャプチャを作成し |
|---|
| 103 |
* 対象と同じtransformプロパティを持つSpriteインスタンスを、対象と同階層に配置します |
|---|
| 104 |
* |
|---|
| 105 |
* @param target 対象となるDisplayObjectインスタンス |
|---|
| 106 |
* @return キャプチャBitmapを内包するSpriteインスタンス |
|---|
| 107 |
*/ |
|---|
| 108 |
public static function dummy( target:DisplayObject ):Sprite |
|---|
| 109 |
{ |
|---|
| 110 |
var bitmap:BitmapData = capture( target ); |
|---|
| 111 |
var rect:Object = target.getBounds( target ); |
|---|
| 112 |
var index:int = target.parent.getChildIndex( target ); |
|---|
| 113 |
var transform:Transform = target.transform; |
|---|
| 114 |
var name:String = target.name; |
|---|
| 115 |
var image:Bitmap = new Bitmap( bitmap, "auto", true ); |
|---|
| 116 |
var sprite:Sprite = new Sprite(); |
|---|
| 117 |
image.name = "bitmap"; |
|---|
| 118 |
sprite.addChild( image ); |
|---|
| 119 |
image.x = rect.x; |
|---|
| 120 |
image.y = rect.y; |
|---|
| 121 |
sprite.transform = transform; |
|---|
| 122 |
target.parent.addChildAt( sprite , index+1 ); |
|---|
| 123 |
return sprite; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
/** |
|---|
| 127 |
* DisplayObjectインスタンスのキャプチャデータを作成します |
|---|
| 128 |
* |
|---|
| 129 |
* @param target 対象となる IBitmapDrawable オブジェクト |
|---|
| 130 |
* @return キャプチャデータを持つ BitmapData オブジェクト |
|---|
| 131 |
*/ |
|---|
| 132 |
public static function capture (target:IBitmapDrawable):BitmapData |
|---|
| 133 |
{ |
|---|
| 134 |
if (target is BitmapData) return (target as BitmapData).clone(); |
|---|
| 135 |
var display:DisplayObject = target as DisplayObject; |
|---|
| 136 |
var rect:Object = display.getBounds( display ); |
|---|
| 137 |
var width:Number = rect.width; |
|---|
| 138 |
var height:Number = rect.height; |
|---|
| 139 |
var bitmap:BitmapData = new BitmapData( |
|---|
| 140 |
width , height, true, 0x00000000 ); |
|---|
| 141 |
var matrix:Matrix = new Matrix(); |
|---|
| 142 |
matrix.translate( -rect.x , -rect.y ); |
|---|
| 143 |
bitmap.draw( display , matrix ); |
|---|
| 144 |
return bitmap; |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
} |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|