Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * GTextureTextExtended
- *
- * Recreates GTextureText
- * Considerations:
- * - no mouse processing!
- * - fixes the align issue, is not based in the texture align, as the original, is based on the text width.
- * to the original node position.
- * - change size with .textSize (must set .originalSize first).
- * - basic support for textformat (font and color), only 1 textformat at a time.
- * - ::setTextureAtlas now public :)
- *
- * Example:
- *
- * // add font textures (or create them in runtime)
- * GTextureAtlasFactory.createFromFont('arial', new TextFormat( new arial_fnt().fontName, 20, 0xFFFFFF), CHAR_LIST) ;
- * GTextureAtlasFactory.createFromFont('bauhaus', new TextFormat( new bauhaus_fnt().fontName, 20, 0xFFFFFF), CHAR_LIST) ;
- *
- * var field:GTextureTextExtended = GNodeFactory.createNodeWithComponent(GTextureTextExtended) as GTextureTextExtended ;
- * tf.originalTextSize = 20 ;
- * tf.textSize = 14 ;
- * tf.textureAtlasId = 'arial' ;
- * tf.tracking = -3 ;
- * tf.lineSeparation = 2 ;
- * tf.text = 'First line of text' ;
- * tf.text += '\nSecond line of text, longer' ;
- * tf.text += '\3rd line of text' ;
- *
- * var alignType:int = 0 ; // equals GTextureTextAlignType.TOP_LEFT
- * tf.align = alignType ;
- *
- * stage.addEventListener('click', function(e:MouseEvent):void {
- * tf.align = ++alignType%3 ;
- * }) ;
- *
- */
- package {
- import ar.com.rodrigolopezpeker.genome.utils.GUtils;
- import com.genome2d.components.GTransform;
- import com.genome2d.components.renderables.GRenderable;
- import com.genome2d.components.renderables.GSprite;
- import com.genome2d.components.renderables.GTextureTextAlignType;
- import com.genome2d.core.GNode;
- import com.genome2d.core.GNodeFactory;
- import com.genome2d.error.GError;
- import com.genome2d.g2d;
- import com.genome2d.textures.GTexture;
- import com.genome2d.textures.GTextureAlignType;
- import com.genome2d.textures.GTextureAtlas;
- import com.genome2d.textures.factories.GTextureFactory;
- import flash.text.TextFormat;
- use namespace g2d ;
- public class GTextureTextExtended extends GRenderable {
- private var _textureAtlas:GTextureAtlas ;
- private var _invalidate:Boolean = false ;
- private var _tracking:Number = 0 ;
- private var _text:String = '' ;
- private var _align:int = 0;
- private var _w:int ;
- private var _h:int ;
- private var _breakLinesIndexes: Array = [];
- private var _textLinesWidth: Array = [];
- private var _lineSeparation: int = 0 ;
- private var _lineH: int = 0 ;
- private var _textScale: Number = 1 ;
- private var _textSize: int = 0 ;
- public var originalTextSize: int = 0 ;
- private static var _backgroundTx:GTexture = GTextureFactory.createFromColor('tf_back', 0x212121, 8, 8 ) ;
- private var _background: GSprite;
- private var _textFormat: Object;
- private var _showBackground = false ;
- public function GTextureTextExtended(pNode:GNode) {
- super(pNode);
- _backgroundTx.alignTexture(GTextureAlignType.TOP_LEFT);
- _background = GNodeFactory.createNodeWithComponent(GSprite) as GSprite ;
- _background.textureId = 'tf_back' ;
- _background.node.transform.alpha = 0.3 ;
- }
- override public function update(p_deltaTime: Number, p_parentTransformUpdate: Boolean, p_parentColorUpdate: Boolean): void {
- if(!_invalidate) return ;
- invalidateText() ;
- }
- private function invalidateText(): void {
- if(!_textureAtlas) return ;
- var offsetX:int = 0 ;
- var charSprite:GSprite ;
- // remove everything....
- while(cNode.numChildren) cNode.removeChild(cNode.firstChild);
- if(_showBackground){
- cNode.addChild(_background.node) ;
- }
- // break lines counter.
- var nextBreakCounter:int = 0 ;
- var nextBreak:int = _breakLinesIndexes.length ? _breakLinesIndexes[0] : -1 ;
- var lineIndex:int = 0 ;
- if( _textSize > 0 && originalTextSize > 0 ){
- _lineH = _textSize ;
- _textScale = _textSize / originalTextSize ;
- } else {
- _lineH = findMaxLineHeight() ;
- _textScale = 1 ;
- }
- _textLinesWidth.length = 0 ;
- var tx:GTexture ;
- var atlasId:String = _textureAtlas.id ;
- for (var i: int = 0; i < _text.length; i++) {
- if( i == nextBreak ){
- _textLinesWidth[lineIndex] = offsetX ;
- if( _w < offsetX ) {
- _w = offsetX - _tracking ;
- }
- offsetX = 0 ;
- if( ++nextBreakCounter < _breakLinesIndexes.length ){
- nextBreak = _breakLinesIndexes[nextBreakCounter] ;
- }
- lineIndex++ ;
- continue ;
- }
- charSprite = GNodeFactory.createNodeWithComponent(GSprite) as GSprite ;
- cNode.addChild(charSprite.node);
- if( _textFormat ) {
- if( i >= _textFormat.from && i <= _textFormat.to ){
- if('font' in _textFormat.format ){
- atlasId = _textFormat.format.font ;
- }
- if('color' in _textFormat.format ){
- Utils.setColor( charSprite.node.transform, _textFormat.format.color ) ;
- }
- } else {
- atlasId = _textureAtlas.id ;
- }
- }
- tx = GTexture.getTextureById( atlasId + '_' + _text.charCodeAt(i)) ;
- if(!tx) throw new GError(GError.NO_TEXTURE_FOR_CHARACTER_FOUND+ _text.charCodeAt(i)+" "+_text.charAt(i));
- var charW: Number = tx.width / 2 * _textScale ;
- var charH: Number = tx.height / 2 * _textScale ;
- charSprite.node.userData.isChar = true ;
- charSprite.node.userData.numLine = lineIndex ;
- charSprite.node.userData.charW = charW ;
- charSprite.node.userData.charH = charH ;
- charSprite.node.cameraGroup = node.cameraGroup ;
- charSprite.setTexture(tx) ;
- charSprite.node.transform.setScale( _textScale, _textScale );
- offsetX += charW ;
- charSprite.cNode.cTransform.x = offsetX ;
- charSprite.cNode.cTransform.y = charH + lineIndex * ( _lineH + _lineSeparation ) ;
- offsetX += charW + _tracking ;
- _h = charSprite.cNode.cTransform.y + charH ;
- }
- if( _w < offsetX ) _w = offsetX - _tracking ;
- _background.node.transform.setScale( _w / _backgroundTx.width, _h / _backgroundTx.height ) ;
- _textLinesWidth.push(offsetX) ;
- invalidateAlign();
- _invalidate = false ;
- }
- private function invalidateAlign(): void {
- var node:GNode, lineW:Number;
- switch (_align) {
- case GTextureTextAlignType.MIDDLE:
- for (node = cNode.firstChild; node; node = node.next) {
- if( node.userData.isChar ){
- lineW = _textLinesWidth[ node.userData.numLine ] ;
- node.transform.x += _w + _tracking - lineW >> 1 ;
- }
- }
- break;
- case GTextureTextAlignType.TOP_RIGHT:
- for (node = cNode.firstChild; node; node = node.next) {
- if( node.userData.isChar ){
- lineW = _textLinesWidth[ node.userData.numLine ] ;
- node.transform.x += _w + _tracking - lineW ;
- }
- }
- break;
- case GTextureTextAlignType.TOP_LEFT:
- break;
- }
- }
- public function get tracking(): Number { return _tracking;}
- public function set tracking(value: Number): void {
- _tracking = value;
- _invalidate = true ;
- }
- public function get text(): String {return _text;}
- public function set text(value: String): void {
- _text = value;
- searchChar(_breakLinesIndexes, '\n') ;
- _invalidate = true ;
- }
- private function findMaxLineHeight(): Number {
- var charH:Number = 0 ;
- for (var i: int = 0; i < _text.length; i++) {
- var tx:GTexture = _textureAtlas.getTexture(String(_text.charCodeAt(i))) ;
- if( tx && tx.height > charH ) {
- charH = tx.height
- }
- }
- return charH ;
- }
- private function searchChar( pOutput:Array, pChar:String ): void {
- var idx:int = 0 ;
- pOutput.length = 0 ;
- while(( idx = _text.indexOf(pChar, idx)) > -1 ) {
- pOutput.push(idx) ;
- idx++ ;
- }
- }
- public function get align(): int {return _align;}
- public function set align(value: int): void {
- _align = value;
- _invalidate = true ;
- }
- public function get textureAtlasId():String {
- return _textureAtlas ? _textureAtlas.id : '';
- }
- public function set textureAtlasId(pId:String):void{
- setTextureAtlas(GTextureAtlas.getTextureAtlasById(pId)) ;
- }
- public function setTextureAtlas( pValue: GTextureAtlas): void {
- _textureAtlas = pValue ;
- _invalidate = true ;
- }
- public function get lineSeparation(): int {return _lineSeparation;}
- public function set lineSeparation(value: int): void {
- _lineSeparation = value;
- _invalidate = true ;
- }
- public function get textSize(): int {return _textSize;}
- public function set textSize(value: int): void {
- _textSize = value;
- _invalidate = true ;
- }
- public function setTextFormat( pFormat: TextFormat, pFrom:int =-1, pTo:int = -1): void {
- _textFormat = {format:pFormat, from:pFrom, to:pTo} ;
- }
- public function get width(): int {
- if (_invalidate) invalidateText();
- return _w;
- }
- public function get height(): int {
- if (_invalidate) invalidateText();
- return _h;
- }
- }
- }
- import com.genome2d.components.GTransform;
- class Utils {
- /**
- * Quick utility function to apply a hex color to a node.
- * @param pTransform
- * @param pHex
- */
- static function setColor(pTransform: GTransform, pHex: uint): void {
- pTransform.red = ((pHex & 0xFF0000) >> 16 ) / 255
- pTransform.green = ((pHex & 0x00FF00) >> 8 ) / 255
- pTransform.blue = (pHex & 0x0000FF) / 255
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment