/*----------------------------------------------------------------------------- The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.mozilla.org/MPL/MPL-1.1.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is Fever Framework code. The Initial Developer of the Original Code is Romain Ecarnot. Portions created by Initial Developer are Copyright (C) 2006 the Initial Developer. All Rights Reserved. Contributor(s): Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -----------------------------------------------------------------------------*/ import com.bourre.structures.Point; import fever.log.FeverDebug; import fever.utils.Stringifier; import flash.display.BitmapData; import flash.geom.Rectangle; /** * Use BitmapData as source and implement grid access to bitmap * part ( cell ). * *
Allows to build a bitmap with many icons, or anything else inside and access * specific one using grid access. * * @author Romain Ecarnot */ class fever.display.BitmapGrid { //------------------------------------------------------------------------- // Private properties //------------------------------------------------------------------------- private var _cellWidth : Number; private var _cellHeight : Number; private var _bmp : BitmapData; //------------------------------------------------------------------------- // Public API //------------------------------------------------------------------------- /** * Constructor. * * @param data BitmapData content source * @param cellWidth Sub bitmap cell's width * @param cellWidth ( optional ) Sub bitmap cell's height * ( if not defined, uses {@code cellWidth} ). */ public function BitmapGrid( data : BitmapData, cellWidth : Number, cellHeight : Number ) { _bmp = data; _cellWidth = cellWidth; _cellHeight = ( !isNaN( cellHeight ) ) ? cellHeight : _cellWidth; if( ( _bmp.width % _cellWidth ) ) FeverDebug.WARN( 'Possible problem with bitmap cell width [ ' + _bmp.width + ' / ' + _cellWidth + ' ]' ); if( ( _bmp.height % _cellHeight ) ) FeverDebug.WARN( 'Possible problem with bitmap cell height [ ' + _bmp.height + ' / ' + _cellHeight + ' ]' ); } /** * Returns {@code BitmapData} positionned at {@code v} position in grid. */ public function getBitmapAt( v : Point ) : BitmapData { return _createBitmap( _cellWidth, _cellHeight, new Rectangle( v.x * _cellWidth, v.y * _cellHeight, _cellWidth, _cellHeight ) ); } /** * Returns a new {@code BitmapData} containing row at passed-in * {@code rowIndex} index. */ public function getBitmapRow( rowIndex : Number ) : BitmapData { return _createBitmap( _bmp.width, _cellHeight, new Rectangle( 0, rowIndex * _cellHeight, _bmp.width, _cellHeight ) ); } /** * Returns a new {@code BitmapData} containing column at passed-in * {@code columnIndex} index. */ public function getBitmapColumn( columnIndex : Number ) : BitmapData { return _createBitmap( _cellWidth, _bmp.height, new Rectangle( columnIndex * _cellWidth, 0, _cellWidth, _bmp.height ) ); } /** * Returns a source bitmap clone. */ public function getBitmap( ) : BitmapData { return _bmp.clone(); } /** * Returns sub bitmap cell width. */ public function getCellWidth( ) : Number { return _cellWidth; } /** * Returns sub bitmap cell height. */ public function getCellHeight( ) : Number { return _cellHeight; } /** * Returns string representation. */ public function toString( ) : String { return Stringifier.parse( this ); } //------------------------------------------------------------------------- // Private implementation //------------------------------------------------------------------------- private function _createBitmap( w : Number, h : Number, rect : Rectangle ) : BitmapData { var o : BitmapData = new BitmapData( w, h, true ); o.copyPixels( _bmp, rect, new flash.geom.Point( 0, 0 ) ); return o; } }