package org.flixel { import flash.display.BitmapData; import flash.geom.ColorTransform; import flash.geom.Point; import flash.geom.Rectangle; public class FlxBitmapText extends FlxObject { protected static var _defaultFont:FlxBitmapFont; protected var _pixels:BitmapData; protected var _p:Point; protected var _frect:Rectangle; protected var _cTransform:ColorTransform = new ColorTransform(); protected var _font:FlxBitmapFont; protected var _text:String; protected var _alignment:String; protected var _overrideWidth:int; protected var _colorFont:Boolean; protected var _lineWrap:Boolean; protected var _splitLines:Array; protected var _lineWidths:Array; protected var _debugRender:Boolean = false; public var truewidth:int; public function FlxBitmapText(param1:int, param2:int, param3:FlxBitmapFont, param4:String = " ", param5:String = "left", param6:Number = 0, param7:Boolean = false) { if(param3 == null) { param3 = _defaultFont ? _defaultFont : new FlxBitmapFont(); } this._font = param3; this._alignment = param5.toLowerCase(); super(); immovable = true; x = param1; y = param2; width = param6; this.truewidth = param6; this._cTransform.alphaMultiplier = 1; this._lineWrap = true; this._p = new Point(); this._colorFont = param7; this.text = param4; } public function get lines() : Array { return this._splitLines; } public function calcFrame() : void { var _loc1_:uint = 0; var _loc2_:uint = 0; var _loc3_:uint = 0; if(this._pixels) { this._pixels.dispose(); } this._pixels = new BitmapData(width,height,true,0); var _loc4_:uint = 0; _loc1_ = 0; while(_loc1_ < this._splitLines.length) { switch(this._alignment) { case "left": _loc3_ = 0; break; case "center": _loc3_ = uint(int((width - this._lineWidths[_loc1_]) / 2)); break; case "right": _loc3_ = width - this._lineWidths[_loc1_]; } _loc2_ = 0; while(_loc2_ < this._splitLines[_loc1_].length) { if(this._font.rects[this._splitLines[_loc1_].charCodeAt(_loc2_)]) { this._p.x = _loc3_ + (this.overrideWidth ? Math.floor((this.overrideWidth - this._font.rects[this._splitLines[_loc1_].charCodeAt(_loc2_)].width) / 2) : 0); this._p.y = _loc4_; this._pixels.copyPixels(this._font.pixels,this._font.rects[this._splitLines[_loc1_].charCodeAt(_loc2_)],this._p,null,null,true); _loc3_ += (this.overrideWidth ? this.overrideWidth : this._font.rects[this._splitLines[_loc1_].charCodeAt(_loc2_)].width) + this._font.horizontalPadding; } _loc2_++; } _loc4_ += this._font.height + this._font.verticalPadding; _loc1_++; } this._frect = new Rectangle(0,0,width,height); this._pixels.colorTransform(this._frect,this._cTransform); } protected function charWidth(param1:uint) : int { if(this._font.rects[param1] == null) { return 0; } return (this.overrideWidth ? this.overrideWidth : this._font.rects[param1].width) + this._font.horizontalPadding; } public function splitLines(param1:String, param2:int) : Array { var _loc3_:Array = null; var _loc4_:String = null; var _loc5_:int = 0; var _loc6_:String = null; var _loc7_:int = 0; var _loc8_:int = 0; var _loc9_:int = 0; param1 += " "; if(this._lineWrap && width > 0) { _loc3_ = new Array(); _loc4_ = ""; _loc5_ = 0; _loc6_ = ""; _loc7_ = 0; _loc8_ = 0; while(_loc8_ < param1.length) { _loc9_ = param1.charCodeAt(_loc8_); if(_loc9_ == 32) { _loc4_.replace(/^\s+|\s+$/g,""); if(_loc4_.length) { if(_loc7_ + _loc5_ >= param2) { _loc3_.push(_loc6_); _loc6_ = _loc4_; _loc7_ = _loc5_; } else { _loc6_ += (_loc6_.length ? " " : "") + _loc4_; _loc7_ += _loc5_ + (_loc6_.length ? this.charWidth(32) : 0); } } _loc4_ = ""; _loc5_ = 0; } else if(_loc9_ == 10) { _loc3_.push(_loc6_ + " " + _loc4_); _loc6_ = ""; _loc7_ = 0; _loc4_ = ""; _loc5_ = 0; } else { _loc4_ += String.fromCharCode(_loc9_); _loc5_ += this.charWidth(_loc9_) + this._font.horizontalPadding; } _loc8_++; } if(_loc4_.length) { _loc3_.push(_loc6_); _loc6_ = _loc4_; } if(_loc6_.length) { _loc3_.push(_loc6_); } } else { _loc3_ = this._text.split("\n"); } return _loc3_; } protected function calcLineWidths() : void { var _loc2_:int = 0; height = 0; this._lineWidths = new Array(); var _loc1_:int = 0; while(_loc1_ < this._splitLines.length) { this._lineWidths[_loc1_] = 0; _loc2_ = 0; while(_loc2_ < this._splitLines[_loc1_].length) { if(this._font.rects[this._splitLines[_loc1_].charCodeAt(_loc2_)]) { this._lineWidths[_loc1_] += this.charWidth(this._splitLines[_loc1_].charCodeAt(_loc2_)); } _loc2_++; } this._lineWidths[_loc1_] -= this._font.horizontalPadding; if(this._lineWidths[_loc1_] > width) { width = this._lineWidths[_loc1_]; } height += this._font.height + this._font.verticalPadding; _loc1_++; } height -= this._font.verticalPadding; if(width < 1 || height < 1) { width = 1; height = 1; } } override public function draw() : void { super.draw(); getScreenXY(_point); if(this._debugRender) { FlxG.camera.buffer.fillRect(new Rectangle(_point.x,_point.y,width,height),4278255360); FlxG.camera.buffer.fillRect(new Rectangle(_point.x + 1,_point.y + 1,width - 2,height - 2),0); } this._p.x = _point.x; this._p.y = _point.y; FlxG.camera.buffer.copyPixels(this._pixels,this._frect,this._p,null,null,true); } public function set text(param1:String) : void { width = this.truewidth; this._text = param1; this._splitLines = this.splitLines(this._text,width); this.calcLineWidths(); this.calcFrame(); } public function get text() : String { return this._text; } public function set overrideWidth(param1:int) : void { this._overrideWidth = param1; } public function get overrideWidth() : int { return this._overrideWidth; } public function set lineWrap(param1:Boolean) : void { this._lineWrap = param1; } public function get lineWrap() : Boolean { return this._lineWrap; } public function set alignment(param1:String) : void { this._alignment = param1.toLowerCase(); this.calcFrame(); } public function get alignment() : String { return this._alignment; } public function set color(param1:uint) : void { this._cTransform.color = param1; this.calcFrame(); } public function get color() : uint { return this._cTransform.color; } public function set debugRender(param1:Boolean) : void { this._debugRender = param1; } public function set colorFont(param1:Boolean) : void { this._colorFont = param1; this.calcFrame(); } public function get colorFont() : Boolean { return this._colorFont; } public function set alpha(param1:Number) : void { if(param1 > 1) { param1 = 1; } if(param1 < 0) { param1 = 0; } if(param1 == this._cTransform.alphaMultiplier) { return; } this._cTransform.alphaMultiplier = param1; this.calcFrame(); } public function get alpha() : Number { return this._cTransform.alphaMultiplier; } public function set font(param1:FlxBitmapFont) : void { if(param1 == null) { param1 = _defaultFont ? _defaultFont : new FlxBitmapFont(); } this._font = param1; this.calcFrame(); } public function get font() : FlxBitmapFont { return this._font; } override public function destroy() : void { super.destroy(); this._font = null; this._pixels.dispose(); } } }