282 lines
7.2 KiB
Haxe
Executable File
282 lines
7.2 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> {
|
|
|
|
private static var K_HEIGHT = 1.185;
|
|
|
|
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 fontBold(default, set):Bool;
|
|
|
|
public var layoutHAlign(default, set):HAlign;
|
|
public var layoutVAlign(default, set):VAlign;
|
|
public var fill(default, set):Bool = true;
|
|
|
|
public var leftPadding(default, set):Float = 0.0;
|
|
public var rightPadding(default, set):Float = 0.0;
|
|
public var topPadding(default, set):Float = 0.0;
|
|
public var bottomPadding(default, set):Float = 0.0;
|
|
public var paddings(null, set):Float = 0.0;
|
|
|
|
private var textFormat:TextFormat;
|
|
|
|
private var _textWidth:Float;
|
|
private var _textHeight:Float;
|
|
|
|
public function new() {
|
|
super();
|
|
layoutHAlign = HAlign.CENTER;
|
|
layoutVAlign = VAlign.MIDDLE;
|
|
textField = buildTextField();
|
|
textField.width = 1;
|
|
textField.height = 1;
|
|
textField.multiline = true;
|
|
textField.wordWrap = true;
|
|
#if dev_layout
|
|
textField.borderColor = 0xff0000;
|
|
textField.border = true;
|
|
#end
|
|
textFormat = textField.defaultTextFormat;
|
|
textFormat.font = "Arial";
|
|
textFormat.size = 16;
|
|
textFormat.leading = 0;
|
|
textFormat.align = TextFormatAlign.CENTER;
|
|
content.addChild(textField);
|
|
}
|
|
|
|
private function buildTextField():TextField {
|
|
return new TextField();
|
|
}
|
|
|
|
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 set_fontBold(value:Bool):Bool {
|
|
if (fontBold != value) {
|
|
fontBold = value;
|
|
textFormat.bold = fontBold;
|
|
invalidate();
|
|
}
|
|
return fontBold;
|
|
}
|
|
|
|
private function currentText():String {
|
|
return _text;
|
|
}
|
|
|
|
private function updateTextSize():Void {
|
|
//ToDo: canvas.getContext("2d").measureText very slow
|
|
#if js
|
|
var s = 1;
|
|
var t = currentText();
|
|
if (t != null) for (i in 0...t.length) {
|
|
if (t.charCodeAt(i) == 10) s++;
|
|
}
|
|
_textHeight = (textFormat.size + 2) * s * K_HEIGHT;
|
|
_textWidth = width;
|
|
#else
|
|
_textWidth = textField.textWidth;
|
|
_textHeight = textField.textHeight * K_HEIGHT;
|
|
#end
|
|
}
|
|
|
|
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);
|
|
updateTextSize();
|
|
if (contentSize && !Std.is(skin, ISize)) {
|
|
#if html5
|
|
var h = _textHeight;
|
|
//var w = _textWidth;
|
|
//if (h > textFormat.size * 1.5) h = h / 2;
|
|
textField.height = h;
|
|
//textField.width = w;
|
|
#end
|
|
width = textField.width + leftPadding + rightPadding;
|
|
height = textField.height + topPadding + bottomPadding;
|
|
textField.x = leftPadding;
|
|
textField.y = topPadding;
|
|
} 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 {
|
|
|
|
textField.width = width;
|
|
textField.height = _textHeight;
|
|
|
|
//#if html5 textField.height = _textHeight; #end
|
|
textField.x = switch (layoutHAlign) {
|
|
case HAlign.NONE: 0;
|
|
case HAlign.LEFT: leftPadding;
|
|
case HAlign.CENTER: (width - textField.width) / 2 + leftPadding - rightPadding;
|
|
case HAlign.RIGHT: width - textField.width - rightPadding;
|
|
default: 0;
|
|
}
|
|
textField.y = switch (layoutVAlign) {
|
|
case VAlign.NONE: 0;
|
|
case VAlign.TOP: topPadding;
|
|
case VAlign.MIDDLE: (height - _textHeight) / 2 + topPadding - bottomPadding;
|
|
case VAlign.BOTTOM: height - _textHeight - bottomPadding;
|
|
default: 0;
|
|
}
|
|
//}
|
|
}
|
|
|
|
override private function set_mouseEnabled(value:Bool):Bool {
|
|
textField.mouseEnabled = value;
|
|
return super.set_mouseEnabled(value);
|
|
}
|
|
|
|
|
|
private function set_leftPadding(value:Float):Float {
|
|
if (leftPadding != value) {
|
|
leftPadding = value;
|
|
invalidate();
|
|
}
|
|
return leftPadding;
|
|
}
|
|
|
|
private function set_rightPadding(value:Float):Float {
|
|
if (rightPadding != value) {
|
|
rightPadding = value;
|
|
invalidate();
|
|
}
|
|
return rightPadding;
|
|
}
|
|
|
|
private function set_topPadding(value:Float):Float {
|
|
if (topPadding != value) {
|
|
topPadding = value;
|
|
invalidate();
|
|
}
|
|
return topPadding;
|
|
}
|
|
|
|
private function set_bottomPadding(value:Float):Float {
|
|
if (bottomPadding != value) {
|
|
bottomPadding = value;
|
|
invalidate();
|
|
}
|
|
return bottomPadding;
|
|
}
|
|
|
|
private function set_paddings(value:Float):Float {
|
|
leftPadding = rightPadding = topPadding = bottomPadding = value;
|
|
invalidate();
|
|
return value;
|
|
}
|
|
}
|