211 lines
6.0 KiB
Haxe
Executable File
211 lines
6.0 KiB
Haxe
Executable File
package haxework.gui;
|
|
|
|
import flash.text.TextField;
|
|
import flash.text.TextFieldAutoSize;
|
|
import flash.text.TextFormat;
|
|
import flash.text.TextFormatAlign;
|
|
import haxework.gui.core.HAlign;
|
|
import haxework.gui.core.VAlign;
|
|
import haxework.text.BitmapTextField;
|
|
import haxework.text.TextUtil;
|
|
|
|
class TextLayout {
|
|
public var hAlign(default, default):HAlign = NONE;
|
|
public var vAlign(default, default):VAlign = NONE;
|
|
|
|
public function new() {}
|
|
}
|
|
|
|
class TextView extends SpriteView implements ITextView {
|
|
|
|
public var textField(default, null):TextField;
|
|
public var text(get, set):String;
|
|
public var layout:TextLayout;
|
|
|
|
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):Int;
|
|
public var fontBold(default, set):Bool;
|
|
|
|
public var fill(default, set):Bool = true;
|
|
|
|
public var shadow(default, set):Bool;
|
|
public var shadowColor(default, set):Int;
|
|
|
|
private var textFormat:TextFormat;
|
|
|
|
public function new() {
|
|
super();
|
|
layout = new TextLayout();
|
|
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.LEFT;
|
|
content.addChild(textField);
|
|
}
|
|
|
|
private function buildTextField():TextField {
|
|
#if bitmap_text
|
|
return new BitmapTextField();
|
|
#else
|
|
return new TextField();
|
|
#end
|
|
}
|
|
|
|
private function set_fill(value:Bool):Bool {
|
|
if (fill != value) {
|
|
fill = value;
|
|
toUpdate();
|
|
}
|
|
return fill;
|
|
}
|
|
|
|
private function get_text():String {
|
|
return textField.text;
|
|
}
|
|
|
|
private function set_text(value:String):String {
|
|
if (_text != value) {
|
|
_text = value;
|
|
toUpdate();
|
|
}
|
|
return _text;
|
|
}
|
|
|
|
private function set_align(value:TextFormatAlign):TextFormatAlign {
|
|
if (align != value) {
|
|
align = value;
|
|
textFormat.align = value;
|
|
toUpdate();
|
|
}
|
|
return align;
|
|
}
|
|
|
|
private function set_fontFamily(value:String):String {
|
|
if (fontFamily != value) {
|
|
fontFamily = value;
|
|
textFormat.font = fontFamily;
|
|
toUpdate();
|
|
}
|
|
return fontFamily;
|
|
}
|
|
|
|
private function set_fontEmbed(value:Bool):Bool {
|
|
if (fontEmbed != value) {
|
|
fontEmbed = value;
|
|
toUpdate();
|
|
}
|
|
return fontEmbed;
|
|
}
|
|
|
|
private function set_fontColor(value:Int):Int {
|
|
if (fontColor != value) {
|
|
fontColor = value;
|
|
textFormat.color = fontColor;
|
|
toUpdate();
|
|
}
|
|
return fontColor;
|
|
}
|
|
|
|
private function set_fontSize(value:Int):Int {
|
|
if (fontSize != value) {
|
|
fontSize = value;
|
|
textFormat.size = fontSize;
|
|
toUpdate();
|
|
}
|
|
return fontSize;
|
|
}
|
|
|
|
private function set_fontBold(value:Bool):Bool {
|
|
if (fontBold != value) {
|
|
fontBold = value;
|
|
textFormat.bold = fontBold;
|
|
toUpdate();
|
|
}
|
|
return fontBold;
|
|
}
|
|
|
|
private function currentText():String {
|
|
return _text;
|
|
}
|
|
|
|
private function updateTextSize():Void {
|
|
var size = TextUtil.getSize(textField);
|
|
setContentSize(size.x, size.y);
|
|
}
|
|
|
|
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();
|
|
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 {
|
|
textField.width = width;
|
|
textField.height = geometry.size.content.height;
|
|
|
|
textField.x = switch (layout.hAlign) {
|
|
case LEFT | NONE: geometry.padding.left;
|
|
case CENTER: (width - textField.width) / 2 + geometry.padding.left - geometry.padding.right;
|
|
case RIGHT: width - textField.width - geometry.padding.right;
|
|
default: 0;
|
|
}
|
|
textField.y = switch (layout.vAlign) {
|
|
case TOP | NONE: geometry.padding.top;
|
|
case MIDDLE: (height - geometry.size.content.height) / 2 + geometry.padding.top - geometry.padding.bottom;
|
|
case BOTTOM: height - geometry.size.content.height - geometry.padding.bottom;
|
|
default: 0;
|
|
}
|
|
}
|
|
|
|
override private function set_mouseEnabled(value:Bool):Bool {
|
|
textField.mouseEnabled = value;
|
|
return super.set_mouseEnabled(value);
|
|
}
|
|
|
|
private function set_shadow(value) {
|
|
if (Std.is(textField, BitmapTextField)) {
|
|
cast(textField, BitmapTextField).shadow = value;
|
|
return cast(textField, BitmapTextField).shadow;
|
|
} else {
|
|
return value;
|
|
}
|
|
}
|
|
|
|
private function set_shadowColor(value) {
|
|
if (Std.is(textField, BitmapTextField)) {
|
|
cast(textField, BitmapTextField).shadowColor = value;
|
|
cast(textField, BitmapTextField).shadow = true;
|
|
return cast(textField, BitmapTextField).shadowColor;
|
|
} else {
|
|
return value;
|
|
}
|
|
}
|
|
}
|