starling-framework
Version:
A fast, productive library for 2D cross-platform development.
390 lines (375 loc) • 15.7 kB
JavaScript
// Class: starling.text.TextField
var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this
$global.Object.defineProperty(exports, "__esModule", {value: true});
var __map_reserved = {};
// Imports
var $hxClasses = require("./../../hxClasses_stub").default;
var $hxEnums = require("./../../hxEnums_stub").default;
var $import = require("./../../import_stub").default;
var $bind = require("./../../bind_stub").default;
var $extend = require("./../../extend_stub").default;
function starling_display_DisplayObjectContainer() {return require("./../../starling/display/DisplayObjectContainer");}
function starling_text_BitmapFont() {return require("./../../starling/text/BitmapFont");}
function starling_core_Starling() {return require("./../../starling/core/Starling");}
function starling_utils_RectangleUtil() {return require("./../../starling/utils/RectangleUtil");}
function js__$Boot_HaxeError() {return require("./../../js/_Boot/HaxeError");}
function openfl_errors_ArgumentError() {return $import(require("openfl/errors/ArgumentError"));}
function starling_display_Sprite() {return require("./../../starling/display/Sprite");}
function starling_display_Quad() {return require("./../../starling/display/Quad");}
function openfl_geom_Matrix() {return $import(require("openfl/geom/Matrix"));}
function starling_text_TrueTypeCompositor() {return require("./../../starling/text/TrueTypeCompositor");}
function starling_utils_SystemUtil() {return require("./../../starling/utils/SystemUtil");}
function js_Boot() {return require("./../../js/Boot");}
function haxe_ds_StringMap() {return require("./../../haxe/ds/StringMap");}
function openfl_geom_Rectangle() {return $import(require("openfl/geom/Rectangle"));}
function starling_text_TextFormat() {return require("./../../starling/text/TextFormat");}
function starling_text_TextOptions() {return require("./../../starling/text/TextOptions");}
function starling_display_MeshBatch() {return require("./../../starling/display/MeshBatch");}
// Constructor
var TextField = function(width,height,text,format,options) {
if(text == null) {
text = "";
}
(starling_display_DisplayObjectContainer().default).call(this);
this._text = text != null ? text : "";
this._hitArea = new (openfl_geom_Rectangle().default)(0,0,width,height);
this._requiresRecomposition = true;
this._compositor = TextField.sDefaultCompositor;
this._format = format != null ? format.clone() : new (starling_text_TextFormat().default)();
this._format.addEventListener("change",$bind(this,this.setRequiresRecomposition));
this._options = options != null ? options.clone() : new (starling_text_TextOptions().default)();
this._options.addEventListener("change",$bind(this,this.setRequiresRecomposition));
this._meshBatch = new (starling_display_MeshBatch().default)();
this._meshBatch.set_touchable(false);
this._meshBatch.set_pixelSnapping(true);
this.addChild(this._meshBatch);
}
// Meta
TextField.__name__ = "starling.text.TextField";
TextField.__isInterface__ = false;
TextField.__super__ = (starling_display_DisplayObjectContainer().default);
TextField.prototype = $extend((starling_display_DisplayObjectContainer().default).prototype, {
dispose: function() {
this._format.removeEventListener("change",$bind(this,this.setRequiresRecomposition));
this._options.removeEventListener("change",$bind(this,this.setRequiresRecomposition));
this._compositor.clearMeshBatch(this._meshBatch);
(starling_display_DisplayObjectContainer().default).prototype.dispose.call(this);
},
render: function(painter) {
if(this._requiresRecomposition) {
this.recompose();
}
(starling_display_DisplayObjectContainer().default).prototype.render.call(this,painter);
},
recompose: function() {
if(this._requiresRecomposition) {
this._recomposing = true;
this._compositor.clearMeshBatch(this._meshBatch);
var fontName = this._format.get_font();
var compositor = TextField.getCompositor(fontName);
if(compositor == null && fontName == "mini") {
compositor = new (starling_text_BitmapFont().default)();
TextField.registerCompositor(compositor,fontName);
}
this._compositor = compositor != null ? compositor : TextField.sDefaultCompositor;
this.updateText();
this.updateBorder();
this._requiresRecomposition = false;
this._recomposing = false;
}
},
updateText: function() {
var width = this._hitArea.width;
var height = this._hitArea.height;
if(this.get_isHorizontalAutoSize() && !this._options.get_isHtmlText()) {
width = 100000;
}
if(this.get_isVerticalAutoSize()) {
height = 100000;
}
this._meshBatch.set_x(this._meshBatch.set_y(0));
this._options.set_textureScale((starling_core_Starling().default).get_current().get_contentScaleFactor());
this._compositor.fillMeshBatch(this._meshBatch,width,height,this._text,this._format,this._options);
if(this._customStyle != null) {
this._meshBatch.set_style(this._customStyle);
} else {
this._defaultStyle = this._compositor.getDefaultMeshStyle(this._defaultStyle,this._format,this._options);
if(this._defaultStyle != null) {
this._meshBatch.set_style(this._defaultStyle);
}
}
if(this._options.get_autoSize() != "none") {
this._textBounds = this._meshBatch.getBounds(this._meshBatch,this._textBounds);
if(this.get_isHorizontalAutoSize()) {
this._meshBatch.set_x(this._textBounds.x = -this._textBounds.x);
this._hitArea.width = this._textBounds.width;
this._textBounds.x = 0;
}
if(this.get_isVerticalAutoSize()) {
this._meshBatch.set_y(this._textBounds.y = -this._textBounds.y);
this._hitArea.height = this._textBounds.height;
this._textBounds.y = 0;
}
} else {
this._textBounds = null;
}
},
updateBorder: function() {
if(this._border == null) {
return;
}
var width = this._hitArea.width;
var height = this._hitArea.height;
var topLine = this._border.getChildAt(0);
var rightLine = this._border.getChildAt(1);
var bottomLine = this._border.getChildAt(2);
var leftLine = this._border.getChildAt(3);
topLine.set_width(width);
topLine.set_height(1);
bottomLine.set_width(width);
bottomLine.set_height(1);
leftLine.set_width(1);
leftLine.set_height(height);
rightLine.set_width(1);
rightLine.set_height(height);
rightLine.set_x(width - 1);
bottomLine.set_y(height - 1);
topLine.set_color(rightLine.set_color(bottomLine.set_color(leftLine.set_color(this._format.get_color()))));
},
setRequiresRecomposition: function() {
if(!this._recomposing) {
this._requiresRecomposition = true;
this.setRequiresRedraw();
}
},
get_isHorizontalAutoSize: function() {
if(this._options.get_autoSize() != "horizontal") {
return this._options.get_autoSize() == "bothDirections";
} else {
return true;
}
},
get_isVerticalAutoSize: function() {
if(this._options.get_autoSize() != "vertical") {
return this._options.get_autoSize() == "bothDirections";
} else {
return true;
}
},
get_textBounds: function() {
return this.getTextBounds(this);
},
getBounds: function(targetSpace,out) {
if(this._requiresRecomposition) {
this.recompose();
}
this.getTransformationMatrix(targetSpace,TextField.sMatrix);
return (starling_utils_RectangleUtil().default).getBounds(this._hitArea,TextField.sMatrix,out);
},
getTextBounds: function(targetSpace,out) {
if(this._requiresRecomposition) {
this.recompose();
}
if(this._textBounds == null) {
this._textBounds = this._meshBatch.getBounds(this);
}
this.getTransformationMatrix(targetSpace,TextField.sMatrix);
return (starling_utils_RectangleUtil().default).getBounds(this._textBounds,TextField.sMatrix,out);
},
hitTest: function(localPoint) {
if(!this.get_visible() || !this.get_touchable() || !this.hitTestMask(localPoint)) {
return null;
} else if(this._hitArea.containsPoint(localPoint)) {
return this;
} else {
return null;
}
},
set_width: function(value) {
var tmp = this.get_scaleX() != 0 ? this.get_scaleX() : 1.0;
this._hitArea.width = value / tmp;
this.setRequiresRecomposition();
return value;
},
set_height: function(value) {
var tmp = this.get_scaleY() != 0 ? this.get_scaleY() : 1.0;
this._hitArea.height = value / tmp;
this.setRequiresRecomposition();
return value;
},
get_text: function() {
return this._text;
},
set_text: function(value) {
if(value == null) {
value = "";
}
if(this._text != value) {
this._text = value;
this.setRequiresRecomposition();
}
return value;
},
get_format: function() {
return this._format;
},
set_format: function(value) {
if(value == null) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("format cannot be null"));
}
this._format.copyFrom(value);
return value;
},
get_options: function() {
return this._options;
},
get_border: function() {
return this._border != null;
},
set_border: function(value) {
if(value && this._border == null) {
this._border = new (starling_display_Sprite().default)();
this.addChild(this._border);
this._border.addChild(new (starling_display_Quad().default)(1.0,1.0));
this._border.addChild(new (starling_display_Quad().default)(1.0,1.0));
this._border.addChild(new (starling_display_Quad().default)(1.0,1.0));
this._border.addChild(new (starling_display_Quad().default)(1.0,1.0));
this.updateBorder();
} else if(!value && this._border != null) {
this._border.removeFromParent(true);
this._border = null;
}
return value;
},
get_autoScale: function() {
return this._options.get_autoScale();
},
set_autoScale: function(value) {
return this._options.set_autoScale(value);
},
get_autoSize: function() {
return this._options.get_autoSize();
},
set_autoSize: function(value) {
return this._options.set_autoSize(value);
},
get_wordWrap: function() {
return this._options.get_wordWrap();
},
set_wordWrap: function(value) {
return this._options.set_wordWrap(value);
},
get_batchable: function() {
return this._meshBatch.get_batchable();
},
set_batchable: function(value) {
return this._meshBatch.set_batchable(value);
},
get_isHtmlText: function() {
return this._options.get_isHtmlText();
},
set_isHtmlText: function(value) {
return this._options.set_isHtmlText(value);
},
get_padding: function() {
return this._options.get_padding();
},
set_padding: function(value) {
return this._options.set_padding(value);
},
get_pixelSnapping: function() {
return this._meshBatch.get_pixelSnapping();
},
set_pixelSnapping: function(value) {
return this._meshBatch.set_pixelSnapping(value);
},
get_style: function() {
if(this._requiresRecomposition) {
this.recompose();
}
return this._meshBatch.get_style();
},
set_style: function(value) {
this._customStyle = value;
this.setRequiresRecomposition();
return value;
}
});
TextField.prototype.__class__ = TextField.prototype.constructor = $hxClasses["starling.text.TextField"] = TextField;
// Init
Object.defineProperties(TextField.prototype,{ isHorizontalAutoSize : { get : function () { return this.get_isHorizontalAutoSize (); }}, isVerticalAutoSize : { get : function () { return this.get_isVerticalAutoSize (); }}, textBounds : { get : function () { return this.get_textBounds (); }}, text : { get : function () { return this.get_text (); }, set : function (v) { return this.set_text (v); }}, format : { get : function () { return this.get_format (); }, set : function (v) { return this.set_format (v); }}, options : { get : function () { return this.get_options (); }}, border : { get : function () { return this.get_border (); }, set : function (v) { return this.set_border (v); }}, autoScale : { get : function () { return this.get_autoScale (); }, set : function (v) { return this.set_autoScale (v); }}, autoSize : { get : function () { return this.get_autoSize (); }, set : function (v) { return this.set_autoSize (v); }}, wordWrap : { get : function () { return this.get_wordWrap (); }, set : function (v) { return this.set_wordWrap (v); }}, batchable : { get : function () { return this.get_batchable (); }, set : function (v) { return this.set_batchable (v); }}, isHtmlText : { get : function () { return this.get_isHtmlText (); }, set : function (v) { return this.set_isHtmlText (v); }}, styleSheet : { get : function () { return this.get_styleSheet (); }, set : function (v) { return this.set_styleSheet (v); }}, pixelSnapping : { get : function () { return this.get_pixelSnapping (); }, set : function (v) { return this.set_pixelSnapping (v); }}, style : { get : function () { return this.get_style (); }, set : function (v) { return this.set_style (v); }}, defaultTextureFormat : { get : function () { return this.get_defaultTextureFormat (); }, set : function (v) { return this.set_defaultTextureFormat (v); }}, defaultCompositor : { get : function () { return this.get_defaultCompositor (); }}});
// Statics
TextField.get_defaultTextureFormat = function() {
return TextField.sDefaultTextureFormat;
}
TextField.set_defaultTextureFormat = function(value) {
return TextField.sDefaultTextureFormat = value;
}
TextField.get_defaultCompositor = function() {
return TextField.sDefaultCompositor;
}
TextField.set_defaultCompositor = function(value) {
return TextField.sDefaultCompositor = value;
}
TextField.updateEmbeddedFonts = function() {
(starling_utils_SystemUtil().default).updateEmbeddedFonts();
}
TextField.registerCompositor = function(compositor,name) {
if(name == null) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("name must not be null"));
}
TextField.get_compositors().set(TextField.convertToLowerCase(name),compositor);
}
TextField.unregisterCompositor = function(name,dispose) {
if(dispose == null) {
dispose = true;
}
name = TextField.convertToLowerCase(name);
var compositors = TextField.get_compositors();
if(dispose && compositors.exists(name)) {
compositors.get(name).dispose();
}
compositors.remove(name);
}
TextField.getCompositor = function(name) {
return TextField.get_compositors().get(TextField.convertToLowerCase(name));
}
TextField.registerBitmapFont = function(bitmapFont,name) {
if(name == null) {
name = bitmapFont.get_name();
}
TextField.registerCompositor(bitmapFont,name);
return name;
}
TextField.unregisterBitmapFont = function(name,dispose) {
if(dispose == null) {
dispose = true;
}
TextField.unregisterCompositor(name,dispose);
}
TextField.getBitmapFont = function(name) {
return (js_Boot().default).__cast(TextField.getCompositor(name) , (starling_text_BitmapFont().default));
}
TextField.get_compositors = function() {
var compositors = (starling_core_Starling().default).get_current().get_painter().get_sharedData().get("starling.display.TextField.compositors");
if(compositors == null) {
compositors = new (haxe_ds_StringMap().default)();
(starling_core_Starling().default).get_current().get_painter().get_sharedData().set("starling.display.TextField.compositors",compositors);
}
return compositors;
}
TextField.convertToLowerCase = function(string) {
var result = TextField.sStringCache.get(string);
if(result == null) {
result = string.toLowerCase();
TextField.sStringCache.set(string,result);
}
return result;
}
TextField.COMPOSITOR_DATA_NAME = "starling.display.TextField.compositors"
TextField.sMatrix = new (openfl_geom_Matrix().default)()
TextField.sDefaultCompositor = new (starling_text_TrueTypeCompositor().default)()
TextField.sDefaultTextureFormat = "bgraPacked4444"
TextField.sStringCache = new (haxe_ds_StringMap().default)()
// Export
exports.default = TextField;