Files
haxework/haxework/gui/TextView.hx
2015-04-24 14:24:04 +03:00

196 lines
5.0 KiB
Haxe
Executable File

package haxework.gui;
import flash.geom.Point;
import flash.text.TextFieldAutoSize;
import haxework.gui.core.HAlign;
import haxework.gui.core.VAlign;
import flash.text.TextFormatAlign;
import haxework.gui.skin.ISize;
import flash.text.TextFormat;
import flash.display.Sprite;
import flash.text.TextField;
class TextView extends SpriteView implements ITextView<Sprite, TextField> {
public var textField(default, null):TextField;
public var text(get, set):String;
private var _text:String;
public var align(default, set):TextFormatAlign;
public var fontFamily(default, set):String;
public var fontEmbed(default, set):Bool;
public var fontColor(default, set):Int;
public var fontSize(default, set):Float;
public var layoutHAlign(default, set):HAlign;
public var layoutVAlign(default, set):VAlign;
public var fill(default, set):Bool = true;
public var paddings(default, set):Float = 0.0;
private var textFormat:TextFormat;
public function new() {
super();
layoutHAlign = HAlign.NONE;
layoutVAlign = VAlign.NONE;
textField = buildTextField();
textField.width = 1;
textField.height = 1;
textField.wordWrap = true;
textFormat = textField.defaultTextFormat;
textFormat.font = "Arial";
textFormat.size = 16;
content.addChild(textField);
}
private function buildTextField():TextField {
return new TextField();
}
private function set_paddings(value:Float):Float {
if (paddings != value) {
paddings = value;
invalidate();
}
return paddings;
}
private function set_fill(value:Bool):Bool {
if (fill != value) {
fill = value;
invalidate();
}
return fill;
}
private function set_layoutHAlign(value:HAlign):HAlign {
if (layoutHAlign != value) {
layoutHAlign = value;
invalidate();
}
return layoutHAlign;
}
private function set_layoutVAlign(value:VAlign):VAlign {
if (layoutVAlign != value) {
layoutVAlign = value;
invalidate();
}
return layoutVAlign;
}
private function get_text():String {
return textField.text;
}
private function set_text(value:String):String {
if (_text != value) {
_text = value;
invalidate();
}
return _text;
}
private function set_align(value:TextFormatAlign):TextFormatAlign {
if (align != value) {
align = value;
textFormat.align = value;
invalidate();
}
return align;
}
private function set_fontFamily(value:String):String {
if (fontFamily != value) {
fontFamily = value;
textFormat.font = fontFamily;
invalidate();
}
return fontFamily;
}
private function set_fontEmbed(value:Bool):Bool {
if (fontEmbed != value) {
fontEmbed = value;
invalidate();
}
return fontEmbed;
}
private function set_fontColor(value:Int):Int {
if (fontColor != value) {
fontColor = value;
textFormat.color = fontColor;
invalidate();
}
return fontColor;
}
private function set_fontSize(value:Float):Float {
if (fontSize != value) {
fontSize = value;
textFormat.size = fontSize;
invalidate();
}
return fontSize;
}
private function currentText():String {
return _text;
}
override public function update():Void {
textField.embedFonts = fontEmbed;
textField.defaultTextFormat = textFormat;
textField.autoSize = fill ? TextFieldAutoSize.NONE : TextFieldAutoSize.LEFT;
var t:String = currentText();
if (t != null) textField.text = t;
textField.setTextFormat(textFormat);
if (contentSize && !Std.is(skin, ISize)) {
width = textField.width + paddings * 2;
height = textField.height + paddings * 2;
textField.x = paddings;
textField.y = paddings;
} else {
placeTextField(textField);
}
//ToDo:
var t:Point = content.localToGlobal(new Point(textField.x, textField.y));
t.x = Math.round(t.x);
t.y = Math.round(t.y);
t = content.globalToLocal(t);
textField.x = t.x;
textField.y = t.y;
super.update();
}
private function placeTextField(textField:TextField):Void {
if (fill) {
textField.width = width - paddings * 2;
textField.height = height - paddings * 2;
textField.x = paddings;
textField.y = paddings;
} else {
#if html5 textField.height = textField.textHeight; #end
textField.x = switch (layoutHAlign) {
case HAlign.NONE: 0;
case HAlign.LEFT: paddings;
case HAlign.CENTER: (width - textField.width) / 2;
case HAlign.RIGHT: width - textField.width - paddings;
default: 0;
}
textField.y = switch (layoutVAlign) {
case VAlign.NONE: 0;
case VAlign.TOP: paddings;
case VAlign.MIDDLE: (height - textField.height) / 2;
case VAlign.BOTTOM: height - textField.height - paddings;
default: 0;
}
}
}
override private function set_mouseEnabled(value:Bool):Bool {
textField.mouseEnabled = value;
return super.set_mouseEnabled(value);
}
}