Created
January 14, 2014 10:45
-
-
Save grapefrukt/8416474 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package flash.text; | |
import flash.display.BitmapData; | |
import flash.display.Sprite; | |
/** | |
* Wraps a regular TextField and renders it to a Bitmap without it ever being on the DisplayList. | |
* Used to work around this bug: https://github.com/openfl/lime/issues/20 | |
* @author Martin Jonasson, [email protected] | |
*/ | |
class RTextField extends Sprite { | |
public var autoSize(get, set):TextFieldAutoSize; | |
public var defaultTextFormat(get, set):TextFormat; | |
public var selectable(get, set):Bool; | |
public var text(get, set):String; | |
public var textColor(get, set):Int; | |
public var textWidth(get, never):Float; | |
public var textHeight(get, never):Float; | |
public var wordWrap(get, set):Bool; | |
private var t:TextField; | |
private var w:Float; | |
private var bitmapData:BitmapData; | |
private var useBitmap:Bool; | |
public function new(useBitmap:Bool) { | |
super(); | |
this.useBitmap = useBitmap; | |
t = new TextField(); | |
mouseEnabled = false; | |
if (!useBitmap) addChild(t); | |
} | |
private function resize() { | |
if (!useBitmap) return; | |
if (bitmapData == null || t.textHeight > bitmapData.height || t.textWidth > bitmapData.width) { | |
if (bitmapData != null) bitmapData.dispose(); | |
trace(t.textHeight, t.numLines); | |
bitmapData = new BitmapData(Math.ceil(t.textWidth), Math.ceil(t.textHeight), true, 0x00000000); | |
} | |
} | |
private function redraw() { | |
if (!useBitmap) return; | |
if (bitmapData == null || t.textHeight > bitmapData.height || t.textWidth > bitmapData.width) resize(); | |
bitmapData.fillRect(bitmapData.rect, 0xff00ffff); | |
bitmapData.draw(t); | |
graphics.clear(); | |
graphics.beginBitmapFill(bitmapData, null, false, true); | |
graphics.drawRect(0, 0, bitmapData.width, bitmapData.height); | |
} | |
private function get_autoSize() { return t.autoSize; } | |
private function set_autoSize(value:TextFieldAutoSize) { t.autoSize = value; return t.autoSize; } | |
private function get_defaultTextFormat() { return t.defaultTextFormat; } | |
private function set_defaultTextFormat(value:TextFormat) { t.defaultTextFormat = value; resize(); redraw(); return t.defaultTextFormat; } | |
private function get_selectable() { return t.selectable; } | |
private function set_selectable(value:Bool) { t.selectable = value; return t.selectable; } | |
private function get_text() { return t.text; } | |
private function set_text(value:String) { | |
if (t.text == value) return value; | |
t.text = value; redraw(); | |
return t.text; | |
} | |
private function get_textColor() { return t.textColor; } | |
private function set_textColor(value:Int) { t.textColor = value; redraw(); return t.textColor; } | |
private function get_textWidth() { return t.textWidth; } | |
private function get_textHeight() { return t.textHeight; } | |
override private function get_width() { return t.width; } | |
override private function set_width(value:Float) { w = value; t.width = value; return w; } | |
private function get_wordWrap() { return t.wordWrap; } | |
private function set_wordWrap(value:Bool) { t.wordWrap = value; return t.wordWrap; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment